Yattm - unified GTK instant-messaging client logo
   [Generated for version 0.2-17 - Mon Jan 6 19:01:23 GMT+1 2003]

Home - Main Page - Data Structures - File List - Data Fields - Globals

editcontacts.c

Go to the documentation of this file.
00001 /*
00002  * Yattm 
00003  *
00004  * Copyright (C) 1999, Torrey Searle <tsearle@uci.edu>
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 /*
00023  * editcontacts.c
00024  */
00025 
00026 
00027 #ifdef HAVE_CONFIG_H
00028 #  include <config.h>
00029 #endif
00030 
00031 #include "intl.h"
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <sys/stat.h>
00035 #include <fcntl.h>
00036 
00037 #include "gtk_globals.h"
00038 #include "status.h"
00039 #include "util.h"
00040 #include "dialog.h"
00041 #include "value_pair.h"
00042 
00043 #include "pixmaps/ok.xpm"
00044 #include "pixmaps/cancel.xpm"
00045 #include "pixmaps/tb_trash.xpm"
00046 #include "pixmaps/tb_edit.xpm"
00047 #include "pixmaps/tb_preferences.xpm"
00048 
00049 enum
00050 {
00051     USER_NAME,
00052     PASSWORD,
00053     SERVICE_TYPE
00054 };
00055 
00056 typedef char * account_row[3];
00057 
00058 static GtkWidget * account_list;
00059 static GtkWidget * account_window = NULL;
00060 static GtkWidget * username;
00061 static GtkWidget * password;
00062 static GtkWidget * service_type;
00063 static GtkWidget * mod_button;
00064 static GtkWidget * del_button;
00065 static gint selected_row = -1;
00066 static gboolean is_open = FALSE;
00067 static gint num_accounts = 0;
00068 
00069 static void destroy(GtkWidget * widget, gpointer data)
00070 {
00071     is_open = FALSE;
00072     num_accounts = 0;
00073     selected_row = -1;
00074 }
00075 
00076 static void read_contacts()
00077 {
00078     account_row text;
00079     GList * node;
00080 
00081     for(node = accounts; node; node = node->next)
00082     {
00083         eb_local_account * ela = node->data;
00084         GList * pairs = RUN_SERVICE(ela)->write_local_config(ela);
00085 
00086         text[SERVICE_TYPE] = eb_services[ela->service_id].name;
00087 
00088         text[USER_NAME] = value_pair_get_value(pairs, "SCREEN_NAME");
00089 
00090         text[PASSWORD] = value_pair_get_value(pairs, "PASSWORD");
00091 
00092         /* gtk_clist_append copies our strings, so we don't need to */
00093         gtk_clist_append(GTK_CLIST(account_list), text);
00094         value_pair_free(pairs);
00095         num_accounts++;
00096     }
00097 }
00098 
00099 static void selection_unmade(GtkWidget *clist,
00100                                 gint    row,
00101                                 gint    column,
00102                                 GdkEventButton *event,
00103                                 gpointer    data)
00104 {
00105     gtk_entry_set_text(GTK_ENTRY(username),  "");
00106     gtk_entry_set_text(GTK_ENTRY(password),  "");
00107 }
00108 
00109 static void selection_made(GtkWidget      *clist,
00110                             gint            row,
00111                             gint            column,
00112                             GdkEventButton *event,
00113                             gpointer        data)
00114 {
00115 
00116     gchar *entry_name;
00117     gchar *entry_pass;
00118     gchar *entry_service;
00119 
00120     selected_row = row;
00121 
00122     /* Put data in selected row into the entry boxes for revision */
00123     
00124     gtk_clist_get_text(GTK_CLIST(clist), row, USER_NAME, &entry_name); 
00125     gtk_clist_get_text(GTK_CLIST(clist), row, PASSWORD, &entry_pass); 
00126     gtk_clist_get_text(GTK_CLIST(clist), row, SERVICE_TYPE, &entry_service); 
00127     gtk_entry_set_text(GTK_ENTRY(username),  entry_name);
00128     gtk_entry_set_text(GTK_ENTRY(password),  entry_pass);
00129     gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(service_type)->entry), entry_service);
00130     
00131     gtk_widget_set_sensitive(mod_button, TRUE);
00132     gtk_widget_set_sensitive(del_button, TRUE);
00133     
00134    return;
00135 }
00136 
00137 static void remove_callback(GtkWidget * widget, gpointer data)
00138 {
00139     if(selected_row != -1)
00140     {
00141         gtk_clist_remove(GTK_CLIST(account_list), selected_row);
00142         num_accounts--;
00143         selected_row = -1;
00144     }
00145 }
00146 
00147 static char * check_login_validity(char * text[])
00148 {
00149     GList * services = get_service_list();
00150     GList * l = services;
00151     /* 
00152      * okay, this should really be text[SERVICE_TYPE], text[USER_NAME] ...
00153      * change it if you think this is confusing
00154      */
00155     if (USER_NAME[text] == NULL || strlen(USER_NAME[text]) == 0)
00156         return NULL;
00157 
00158     while (l) {
00159         if(!strcmp(l->data, SERVICE_TYPE[text]))
00160             return eb_services[get_service_id(l->data)].sc->check_login(USER_NAME[text], PASSWORD[text]);
00161         l = l->next;
00162     }
00163     
00164     return NULL;
00165 }
00166 
00167 static void add_callback(GtkWidget * widget, gpointer data)
00168 {
00169     char * text[3];
00170     char * error_message = NULL;
00171     text[USER_NAME] = gtk_entry_get_text(GTK_ENTRY(username));
00172     text[PASSWORD] = gtk_entry_get_text(GTK_ENTRY(password));
00173     text[SERVICE_TYPE] = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(service_type)->entry));
00174     
00175     error_message = check_login_validity(text);
00176     if(error_message)
00177     {
00178         do_error_dialog(error_message,
00179                 _("Wrong parameters"));
00180         return;
00181     }
00182 
00183     gtk_clist_append(GTK_CLIST(account_list), text);
00184     num_accounts++;
00185     printf("num_accounts %d\n",num_accounts);
00186     gtk_entry_set_text(GTK_ENTRY(username), "");
00187     gtk_entry_set_text(GTK_ENTRY(password), "");
00188 }
00189 
00190 static void modify_callback(GtkWidget * widget, gpointer data)
00191 {
00192     char * text[3];
00193     
00194     /* update selected row in list */
00195     
00196     text[USER_NAME] = gtk_entry_get_text(GTK_ENTRY(username));
00197     gtk_clist_set_text(GTK_CLIST(account_list), 
00198             selected_row, USER_NAME, text[USER_NAME]);
00199     
00200     text[PASSWORD] = gtk_entry_get_text(GTK_ENTRY(password));
00201     gtk_clist_set_text(GTK_CLIST(account_list), 
00202                         selected_row, PASSWORD, text[PASSWORD]);
00203 
00204     text[SERVICE_TYPE] = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(service_type)->entry));
00205     gtk_clist_set_text(GTK_CLIST(account_list), 
00206                         selected_row, SERVICE_TYPE, text[SERVICE_TYPE]);
00207 
00208     /* reset the entry fields */
00209 
00210     gtk_entry_set_text(GTK_ENTRY(username), "");
00211     gtk_entry_set_text(GTK_ENTRY(password), "");
00212 }
00213 
00214 static void cancel_callback(GtkWidget *widget, gpointer data)
00215 {
00216     gtk_widget_destroy(account_window);
00217     if(!statuswindow)
00218         gtk_main_quit();
00219 }
00220 
00221 static void ok_callback(GtkWidget * widget, gpointer data)
00222 {
00223     FILE * fp;
00224     char buff[1024];
00225     char * service, *user, *pass;
00226     int i;
00227     int id;
00228     gboolean had_accounts=(accounts != NULL);
00229     GList *pairs=NULL;
00230     GList *existing_accounts = NULL, *new_accounts = NULL, *acc_walk = NULL;
00231     eb_local_account *ela=NULL;
00232 
00233     if (gtk_entry_get_text(GTK_ENTRY(username)) != NULL
00234     &&  strlen(gtk_entry_get_text(GTK_ENTRY(username))) > 0) {
00235         add_callback(widget,data);
00236     }
00237     
00238     g_snprintf(buff, 1024, "%saccounts",config_dir);
00239 
00240     fp=fdopen(creat(buff, 0700), "w");
00241     
00242     if (num_accounts == 0) {
00243         do_error_dialog(_("You didn't define any account."), _("Error"));
00244         return;
00245     }
00246     
00247     for(i = 0; i < num_accounts; i++)
00248     {
00249         gtk_clist_get_text(GTK_CLIST(account_list), i, SERVICE_TYPE, &service); 
00250         gtk_clist_get_text(GTK_CLIST(account_list), i, USER_NAME, &user); 
00251         gtk_clist_get_text(GTK_CLIST(account_list), i, PASSWORD, &pass); 
00252         id=get_service_id(service);
00253         if(accounts && (ela = find_local_account_by_handle(user, id))) {
00254             /* If the account exits, just update the password
00255              */
00256             GList * config = NULL;  
00257             config = eb_services[id].sc->write_local_config(ela);
00258             config = value_pair_remove(config, "PASSWORD");
00259             config = value_pair_add(config, "PASSWORD", pass);
00260             fprintf(fp, "<ACCOUNT %s>\n", service);
00261             value_pair_print_values(config, fp, 1); 
00262             fprintf(fp, "</ACCOUNT>\n");
00263             existing_accounts = g_list_append(existing_accounts, ela);
00264         }
00265         else {
00266             GList * config = NULL;  
00267             eb_debug(DBG_CORE, "Adding new account %s service %s\n", user, service);
00268             pairs = value_pair_add(NULL, "SCREEN_NAME", user);
00269             pairs = value_pair_add(pairs, "PASSWORD", pass);
00270             save_account_info(service, pairs);
00271             ela = eb_services[id].sc->read_local_account_config(pairs);
00272             //prevent segfault
00273             if(ela != NULL) {
00274                 // Is this an account for which a module is not loaded?
00275                 if(ela->service_id==-1)
00276                     ela->service_id=id;
00277                 new_accounts = g_list_append(new_accounts, ela);
00278                 config = eb_services[id].sc->write_local_config(ela);
00279                 fprintf(fp, "<ACCOUNT %s>\n", service);
00280                 value_pair_print_values(config, fp, 1); 
00281                 fprintf(fp, "</ACCOUNT>\n");
00282             } else {
00283                 do_error_dialog(_("Can't add account : unknown service"), _("Error"));
00284             }
00285         }
00286     }
00287 
00288     
00289     fclose(fp);
00290 
00291     acc_walk = accounts;
00292     if (acc_walk) {
00293         while(acc_walk != NULL && acc_walk->data != NULL) {
00294             if (g_list_find(existing_accounts, acc_walk->data) == NULL) {
00295                 eb_local_account *removed = (eb_local_account *)(acc_walk->data);
00296                 /* removed account */
00297                 if(removed->connected 
00298                 && RUN_SERVICE(removed)->logout != NULL)
00299                     RUN_SERVICE(removed)->logout(removed);
00300                 accounts = g_list_remove(accounts,acc_walk->data);
00301             }
00302             acc_walk = acc_walk->next;
00303         }
00304     }
00305     
00306     acc_walk = new_accounts;
00307     if (acc_walk) {
00308         while(acc_walk != NULL) {
00309             accounts = g_list_append(accounts,acc_walk->data);
00310             acc_walk = acc_walk->next;
00311         }       
00312     }
00313     
00314     gtk_widget_destroy(account_window);
00315 
00316     /* if this was an initial launch, start up EB */
00317     if(!had_accounts)
00318     {
00319         load_accounts();
00320         load_contacts();
00321         eb_status_window();
00322     }
00323     else
00324         rebuild_set_status_menu();
00325 }
00326         
00327 void eb_new_user()
00328 {
00329     char * text[] ={_("Screen Name"), 
00330             _("Password"), 
00331             _("Service Type")};
00332     GtkWidget * box;
00333     GtkWidget * window_box;
00334     GtkWidget * hbox;
00335     GtkWidget * button_box;
00336     GtkWidget * label;
00337     GtkWidget * iconwid;
00338     GtkWidget * toolbar;
00339     GtkWidget * toolitem;
00340     GtkWidget * separator;
00341     GList * list;
00342     GdkPixmap *icon;
00343     GdkBitmap *mask;
00344 
00345     if(is_open)
00346         return;
00347 
00348     is_open = 1;
00349 
00350     account_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00351     gtk_window_set_position(GTK_WINDOW(account_window), GTK_WIN_POS_MOUSE);
00352     gtk_widget_realize(account_window);
00353     account_list = gtk_clist_new_with_titles(3,text); 
00354     gtk_clist_set_column_visibility(GTK_CLIST(account_list), PASSWORD, FALSE);
00355     gtk_container_set_border_width(GTK_CONTAINER(account_window), 5);
00356     gtk_signal_connect(GTK_OBJECT(account_list), "select_row",
00357                         GTK_SIGNAL_FUNC(selection_made),
00358                         NULL);
00359     gtk_signal_connect(GTK_OBJECT(account_list), "unselect_row",
00360                         GTK_SIGNAL_FUNC(selection_unmade), 
00361                         NULL);
00362     
00363     box = gtk_vbox_new(FALSE, 0);
00364     window_box = gtk_vbox_new(FALSE,5);
00365     hbox = gtk_hbox_new(FALSE, 5);
00366     gtk_container_set_border_width(GTK_CONTAINER(hbox), 5);
00367 
00368     /*Screen Name Section*/
00369    
00370     label = gtk_label_new(_("Screen Name:"));
00371     gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 0);
00372     gtk_widget_show(label);
00373     username = gtk_entry_new();
00374     gtk_box_pack_start(GTK_BOX(box), username, FALSE, FALSE, 2);
00375     gtk_widget_show(username);
00376 
00377     /*Password Section*/
00378    
00379     label = gtk_label_new(_("Password:"));
00380     gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 5);
00381     gtk_widget_show(label);
00382     password = gtk_entry_new();
00383     gtk_entry_set_visibility(GTK_ENTRY(password), FALSE);
00384     gtk_box_pack_start(GTK_BOX(box), password, FALSE, FALSE, 2);
00385     gtk_widget_show(password);
00386 
00387     /*Service Type Section*/
00388    
00389     label = gtk_label_new(_("Service Type:"));
00390     gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 5);
00391     gtk_widget_show(label);
00392     service_type = gtk_combo_new();
00393     list = get_service_list();
00394     gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(service_type)->entry), FALSE);
00395     gtk_combo_set_popdown_strings(GTK_COMBO(service_type), list);
00396     g_list_free(list);
00397     gtk_widget_show(service_type);
00398     gtk_box_pack_start(GTK_BOX(box), service_type, FALSE, FALSE, 2);
00399     
00400     gtk_box_pack_start(GTK_BOX(hbox), box, FALSE, FALSE, 2);  
00401     gtk_widget_show(box);
00402     
00403     box = gtk_vbox_new(FALSE, 0);
00404 
00405     read_contacts();
00406 
00407     gtk_box_pack_start(GTK_BOX(box), account_list, TRUE, TRUE, 0);
00408     gtk_widget_show(account_list);
00409 
00410     gtk_box_pack_start(GTK_BOX(hbox), box, TRUE, TRUE, 2);
00411     gtk_widget_show(box);
00412 
00413     gtk_box_pack_start(GTK_BOX(window_box), hbox, TRUE, TRUE, 0);
00414     gtk_widget_show(hbox);
00415 
00416     separator = gtk_hseparator_new();
00417     gtk_box_pack_start(GTK_BOX(window_box), separator, TRUE, TRUE, 0);
00418     gtk_widget_show(separator);
00419 
00420     /*Initialize Toolbar*/
00421 
00422     toolbar = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL, GTK_TOOLBAR_BOTH);
00423     gtk_toolbar_set_button_relief(GTK_TOOLBAR(toolbar), GTK_RELIEF_NONE);
00424     gtk_container_set_border_width(GTK_CONTAINER(toolbar), 0);
00425     gtk_toolbar_set_space_size(GTK_TOOLBAR(toolbar), 5);
00426    
00427     /*Add Button*/
00428    
00429     icon = gdk_pixmap_create_from_xpm_d(account_window->window, &mask, NULL, tb_preferences_xpm);
00430     iconwid = gtk_pixmap_new(icon, mask);
00431     gtk_widget_show(iconwid);
00432     toolitem = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
00433                         _("Add"),
00434                                         _("Add Account"),
00435                                         _("Add"),
00436                                         iconwid,
00437                                         GTK_SIGNAL_FUNC(add_callback),
00438                                         NULL);
00439     gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
00440     
00441     /*Delete Button*/
00442     
00443     icon = gdk_pixmap_create_from_xpm_d(account_window->window, &mask, NULL, tb_trash_xpm);
00444     iconwid = gtk_pixmap_new(icon, mask);
00445     gtk_widget_show(iconwid);
00446     del_button = gtk_toolbar_append_item (GTK_TOOLBAR(toolbar),
00447                         _("Delete"),
00448                                         _("Delete Account"),
00449                                         _("Delete"),
00450                                         iconwid,
00451                                         GTK_SIGNAL_FUNC(remove_callback),
00452                                         NULL);
00453     gtk_widget_set_sensitive(del_button, FALSE);
00454     gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
00455    
00456     /* Modify Button */
00457     
00458     icon = gdk_pixmap_create_from_xpm_d(account_window->window, &mask, NULL, tb_edit_xpm);
00459     iconwid = gtk_pixmap_new(icon, mask);
00460     gtk_widget_show(iconwid);
00461     mod_button = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
00462                             _("Modify"),
00463                                             _("Modify Account"),
00464                                             _("Modify"),
00465                                              iconwid,
00466                                              GTK_SIGNAL_FUNC(modify_callback),
00467                                              NULL);
00468     gtk_widget_set_sensitive(mod_button, FALSE);
00469     gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
00470 
00471     separator = gtk_vseparator_new();
00472     gtk_widget_set_usize(GTK_WIDGET(separator), 0, 20);
00473     gtk_toolbar_append_widget(GTK_TOOLBAR(toolbar), separator, NULL, NULL);
00474     gtk_widget_show(separator);
00475     gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
00476    
00477     /*Okay Button*/
00478     
00479     icon = gdk_pixmap_create_from_xpm_d(account_window->window, &mask, NULL, ok_xpm);
00480     iconwid = gtk_pixmap_new(icon, mask);
00481     gtk_widget_show(iconwid);
00482     toolitem = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
00483                         _("Ok"),
00484                                         _("Ok"),
00485                                         _("Ok"),
00486                                         iconwid,
00487                                         GTK_SIGNAL_FUNC(ok_callback),
00488                                         NULL);
00489     gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
00490 
00491     /*Cancel Button*/
00492     
00493     icon = gdk_pixmap_create_from_xpm_d(account_window->window, &mask, NULL, cancel_xpm);
00494     iconwid = gtk_pixmap_new(icon, mask);
00495     gtk_widget_show(iconwid);
00496     toolitem = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
00497                         _("Cancel"),
00498                                         _("Cancel"),
00499                                         _("Cancel"),
00500                                         iconwid,
00501                                         GTK_SIGNAL_FUNC(cancel_callback),
00502                                         NULL);
00503 
00504     /*Buttons End*/
00505    
00506     button_box = gtk_hbox_new(FALSE, 0);
00507    
00508     gtk_box_pack_end(GTK_BOX(button_box), toolbar, FALSE, FALSE, 0);
00509     gtk_widget_show(toolbar);
00510    
00511     gtk_box_pack_start(GTK_BOX(window_box), button_box, FALSE, FALSE, 0);
00512     gtk_widget_show(button_box);
00513    
00514     gtk_widget_show(window_box);
00515 
00516     gtk_container_add(GTK_CONTAINER(account_window), window_box);
00517    
00518     gtk_window_set_title(GTK_WINDOW(account_window), _("Yattm Account Editor"));
00519     eb_icon(account_window->window);
00520 
00521     gtk_signal_connect(GTK_OBJECT(account_window), "destroy",
00522                        GTK_SIGNAL_FUNC(destroy), NULL );
00523     gtk_widget_show(account_window);
00524 }

Contact: Andy Maloney     [Documentation generated by doxygen]