inital gtk2 patch
[claws.git] / src / editgroup.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <gdk/gdkkeysyms.h>
28
29 #include "intl.h"
30 #include "addressbook.h"
31 #include "addressitem.h"
32 #include "addrbook.h"
33 #include "addritem.h"
34
35 #include "mgutils.h"
36
37 #include "prefs_common.h"
38
39 #include "alertpanel.h"
40 #include "inputdialog.h"
41 #include "manage_window.h"
42 #include "gtkutils.h"
43
44 #define ADDRESSBOOK_GUESS_FOLDER_NAME   "NewFolder"
45 #define ADDRESSBOOK_GUESS_GROUP_NAME    "NewGroup"
46
47 #define EDITGROUP_WIDTH      580
48 #define EDITGROUP_HEIGHT     340
49
50 typedef enum {
51         GROUP_COL_NAME    = 0,
52         GROUP_COL_EMAIL   = 1,
53         GROUP_COL_REMARKS = 2
54 } GroupEditEMailColumnPos;
55
56 #define GROUP_N_COLS          3
57 #define GROUP_COL_WIDTH_NAME  140
58 #define GROUP_COL_WIDTH_EMAIL 120
59
60 static struct _GroupEdit_dlg {
61         GtkWidget *window;
62         GtkWidget *ok_btn;
63         GtkWidget *cancel_btn;
64         GtkWidget *statusbar;
65         gint status_cid;
66
67         /* Basic data tab */
68         GtkWidget *entry_name;
69         GtkCList *clist_group;
70         GtkCList *clist_avail;
71
72         GHashTable *hashEMail;
73
74 } groupeditdlg;
75
76
77 static gchar *_edit_group_dfl_message_ = NULL;
78
79 static void edit_group_status_show( gchar *msg ) {
80         if( groupeditdlg.statusbar != NULL ) {
81                 gtk_statusbar_pop( GTK_STATUSBAR(groupeditdlg.statusbar), groupeditdlg.status_cid );
82                 if( msg ) {
83                         gtk_statusbar_push( GTK_STATUSBAR(groupeditdlg.statusbar), groupeditdlg.status_cid, msg );
84                 }
85         }
86 }
87
88 static void edit_group_ok(GtkWidget *widget, gboolean *cancelled) {
89         gchar *sName;
90         gboolean errFlag = TRUE;
91
92         sName = gtk_editable_get_chars( GTK_EDITABLE(groupeditdlg.entry_name), 0, -1 );
93         if( sName ) {
94                 g_strstrip( sName );
95                 if( *sName != '\0' ) {
96                         gtk_entry_set_text(GTK_ENTRY(groupeditdlg.entry_name), sName );
97                         *cancelled = FALSE;
98                         gtk_main_quit();
99                         errFlag = FALSE;
100                 }
101         }
102         if( errFlag ) {
103                 edit_group_status_show( _( "A Group Name must be supplied." ) );
104         }
105         g_free( sName );
106 }
107         
108 static void edit_group_cancel(GtkWidget *widget, gboolean *cancelled) {
109         *cancelled = TRUE;
110         gtk_main_quit();
111 }
112
113 static gint edit_group_delete_event(GtkWidget *widget, GdkEventAny *event, gboolean *cancelled) {
114         *cancelled = TRUE;
115         gtk_main_quit();
116         return TRUE;
117 }
118
119 static gboolean edit_group_key_pressed(GtkWidget *widget, GdkEventKey *event, gboolean *cancelled) {
120         if (event && event->keyval == GDK_Escape) {
121                 *cancelled = TRUE;
122                 gtk_main_quit();
123         }
124         return FALSE;
125 }
126
127 static gchar *edit_group_format_item_clist( ItemPerson *person, ItemEMail *email ) {
128         gchar *str = NULL;
129         gchar *aName = ADDRITEM_NAME(email);
130         if( aName == NULL || *aName == '\0' ) return str;
131         if( person ) {
132                 str = g_strdup_printf( "%s - %s", ADDRITEM_NAME(person), aName );
133         }
134         else {
135                 str = g_strdup( aName );
136         }
137         return str;
138 }
139
140 static gint edit_group_clist_add_email( GtkCList *clist, ItemEMail *email ) {
141         ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(email);
142         gchar *str = edit_group_format_item_clist( person, email );
143         gchar *text[ GROUP_N_COLS ];
144         gint row;
145         if( str ) {
146                 text[ GROUP_COL_NAME ] = str;
147         }
148         else {
149                 text[ GROUP_COL_NAME ] = ADDRITEM_NAME(person);
150         }
151         text[ GROUP_COL_EMAIL   ] = email->address;
152         text[ GROUP_COL_REMARKS ] = email->remarks;
153         row = gtk_clist_append( clist, text );
154         gtk_clist_set_row_data( clist, row, email );
155         return row;
156 }
157
158 static void edit_group_load_clist( GtkCList *clist, GList *listEMail ) {
159         GList *node = listEMail;
160         while( node ) {
161                 ItemEMail *email = node->data;
162                 edit_group_clist_add_email( clist, email );
163                 node = g_list_next( node );
164         }
165 }
166
167
168 static gint edit_group_move_email( GtkCList *clist_from, GtkCList *clist_to, gint row ) {
169         ItemEMail *email = gtk_clist_get_row_data( clist_from, row );
170         gint rrow = -1;
171         if( email ) {
172                 gtk_clist_remove( clist_from, row );
173                 rrow = edit_group_clist_add_email( clist_to, email );
174                 gtk_clist_select_row( clist_to, rrow, 0 );
175         }
176         return rrow;
177 }
178
179 static void edit_group_to_group( GtkWidget *widget, gpointer data ) {
180         GList *selected = GTK_CLIST(groupeditdlg.clist_avail)->selection;
181         /* Clear the selected rows on destination clist */
182         gtk_clist_unselect_all(groupeditdlg.clist_group);
183         while (selected) {
184                 edit_group_move_email( groupeditdlg.clist_avail,
185                                         groupeditdlg.clist_group, GPOINTER_TO_UINT(selected->data) );
186                 /* cannot use g_list_next as the selection list will have changed */
187                 selected = GTK_CLIST(groupeditdlg.clist_avail)->selection;
188         }
189 }
190
191 static void edit_group_to_avail( GtkWidget *widget, gpointer data ) {
192         GList *selected = GTK_CLIST(groupeditdlg.clist_group)->selection;
193         gtk_clist_unselect_all(groupeditdlg.clist_avail);
194         while (selected) {
195                 edit_group_move_email( groupeditdlg.clist_group,
196                                         groupeditdlg.clist_avail, GPOINTER_TO_UINT(selected->data) );
197                 selected = GTK_CLIST(groupeditdlg.clist_group)->selection;
198         }
199 }
200
201 static gboolean edit_group_list_group_button( GtkCList *clist, GdkEventButton *event, gpointer data ) {
202         if( ! event ) return FALSE;
203         if( event->button == 1 ) {
204                 if( event->type == GDK_2BUTTON_PRESS ) {
205                         edit_group_to_avail( NULL, NULL );
206                 }
207         }
208         return FALSE;
209 }
210
211 static gboolean edit_group_list_avail_button( GtkCList *clist, GdkEventButton *event, gpointer data ) {
212         if( ! event ) return FALSE;
213         if( event->button == 1 ) {
214                 if( event->type == GDK_2BUTTON_PRESS ) {
215                         edit_group_to_group( NULL, NULL );
216                 }
217         }
218         return FALSE;
219 }
220
221 static gint edit_group_list_compare_func( GtkCList *clist, gconstpointer ptr1, gconstpointer ptr2 ) {
222         GtkCell *cell1 = ((GtkCListRow *)ptr1)->cell;
223         GtkCell *cell2 = ((GtkCListRow *)ptr2)->cell;
224         gchar *name1 = NULL, *name2 = NULL;
225         if( cell1 ) name1 = cell1->u.text;
226         if( cell2 ) name2 = cell2->u.text;
227         if( ! name1 ) return ( name2 != NULL );
228         if( ! name2 ) return -1;
229         return strcasecmp( name1, name2 );
230 }
231
232 static void addressbook_edit_group_create( gboolean *cancelled ) {
233         GtkWidget *window;
234         GtkWidget *vbox;
235         GtkWidget *hbbox;
236         GtkWidget *ok_btn;
237         GtkWidget *cancel_btn;
238         GtkWidget *hsbox;
239         GtkWidget *statusbar;
240
241         GtkWidget *hboxg;
242         GtkWidget *table;
243         GtkWidget *label;
244         GtkWidget *entry_name;
245         GtkWidget *hboxl;
246         GtkWidget *vboxl;
247         GtkWidget *hboxh;
248
249         GtkWidget *clist_swin;
250         GtkWidget *clist_group;
251         GtkWidget *clist_avail;
252
253         GtkWidget *buttonGroup;
254         GtkWidget *buttonAvail;
255         gint top;
256
257         gchar *titles[ GROUP_N_COLS ];
258         gint i;
259
260         titles[ GROUP_COL_NAME    ] = _( "Name" );
261         titles[ GROUP_COL_EMAIL   ] = _("E-Mail Address");
262         titles[ GROUP_COL_REMARKS ] = _("Remarks");
263
264         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
265         gtk_widget_set_size_request(window, EDITGROUP_WIDTH, EDITGROUP_HEIGHT );
266         gtk_container_set_border_width(GTK_CONTAINER(window), 0);
267         gtk_window_set_title(GTK_WINDOW(window), _("Edit Group Data"));
268         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
269         gtk_window_set_modal(GTK_WINDOW(window), TRUE); 
270         g_signal_connect(G_OBJECT(window), "delete_event",
271                          G_CALLBACK(edit_group_delete_event),
272                          cancelled);
273         g_signal_connect(G_OBJECT(window), "key_press_event",
274                          G_CALLBACK(edit_group_key_pressed),
275                          cancelled);
276
277         vbox = gtk_vbox_new( FALSE, 6 );
278         gtk_container_set_border_width(GTK_CONTAINER(vbox), BORDER_WIDTH);
279         gtk_widget_show( vbox );
280         gtk_container_add( GTK_CONTAINER( window ), vbox );
281
282         /* Group area */
283         hboxg = gtk_hbox_new( FALSE, 0 );
284         gtk_box_pack_start(GTK_BOX(vbox), hboxg, FALSE, FALSE, 0);
285
286         /* Data entry area */
287         table = gtk_table_new( 1, 3, FALSE);
288         gtk_box_pack_start(GTK_BOX(hboxg), table, TRUE, TRUE, 0);
289         gtk_container_set_border_width( GTK_CONTAINER(table), 4 );
290         gtk_table_set_row_spacings(GTK_TABLE(table), 0);
291         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
292
293         /* First row */
294         top = 0;
295         label = gtk_label_new(_("Group Name"));
296         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
297         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
298
299         entry_name = gtk_entry_new();
300         gtk_table_attach(GTK_TABLE(table), entry_name, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
301
302         /* List area */
303         hboxl = gtk_hbox_new( FALSE, 6 );
304         gtk_container_set_border_width( GTK_CONTAINER(hboxl), 8 );
305         gtk_box_pack_start(GTK_BOX(vbox), hboxl, TRUE, TRUE, 0);
306
307         /* Group list */
308         vboxl = gtk_vbox_new( FALSE, 0 );
309         gtk_box_pack_start(GTK_BOX(hboxl), vboxl, TRUE, TRUE, 0);
310
311         hboxh = gtk_hbox_new( FALSE, 0 );
312         gtk_container_set_border_width( GTK_CONTAINER(hboxh), 4 );
313         gtk_box_pack_start(GTK_BOX(vboxl), hboxh, FALSE, FALSE, 0);
314         label = gtk_label_new(_("Addresses in Group"));
315         gtk_box_pack_start(GTK_BOX(hboxh), label, TRUE, TRUE, 0);
316         buttonAvail = gtk_button_new_with_label( _( " -> " ) );
317         gtk_box_pack_end(GTK_BOX(hboxh), buttonAvail, FALSE, FALSE, 0);
318
319         clist_swin = gtk_scrolled_window_new( NULL, NULL );
320         gtk_box_pack_start(GTK_BOX(vboxl), clist_swin, TRUE, TRUE, 0);
321         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
322                                        GTK_POLICY_AUTOMATIC,
323                                        GTK_POLICY_ALWAYS);
324
325         clist_group = gtk_clist_new_with_titles( GROUP_N_COLS, titles );
326         gtk_container_add( GTK_CONTAINER(clist_swin), clist_group );
327         gtk_clist_set_selection_mode( GTK_CLIST(clist_group), GTK_SELECTION_EXTENDED );
328         gtk_clist_set_column_width( GTK_CLIST(clist_group), GROUP_COL_NAME, GROUP_COL_WIDTH_NAME );
329         gtk_clist_set_column_width( GTK_CLIST(clist_group), GROUP_COL_EMAIL, GROUP_COL_WIDTH_EMAIL );
330         gtk_clist_set_compare_func( GTK_CLIST(clist_group), edit_group_list_compare_func );
331         gtk_clist_set_auto_sort( GTK_CLIST(clist_group), TRUE );
332
333         for( i = 0; i < GROUP_N_COLS; i++ )
334                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist_group)->column[i].button, GTK_CAN_FOCUS);
335
336         /* Available list */
337         vboxl = gtk_vbox_new( FALSE, 0 );
338         gtk_box_pack_start(GTK_BOX(hboxl), vboxl, TRUE, TRUE, 0);
339
340         hboxh = gtk_hbox_new( FALSE, 0 );
341         gtk_container_set_border_width( GTK_CONTAINER(hboxh), 4 );
342         gtk_box_pack_start(GTK_BOX(vboxl), hboxh, FALSE, FALSE, 0);
343         buttonGroup = gtk_button_new_with_label( _( " <- " ) );
344         gtk_box_pack_start(GTK_BOX(hboxh), buttonGroup, FALSE, FALSE, 0);
345         label = gtk_label_new(_("Available Addresses"));
346         gtk_box_pack_end(GTK_BOX(hboxh), label, TRUE, TRUE, 0);
347
348         clist_swin = gtk_scrolled_window_new( NULL, NULL );
349         gtk_box_pack_start(GTK_BOX(vboxl), clist_swin, TRUE, TRUE, 0);
350         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
351                                        GTK_POLICY_AUTOMATIC,
352                                        GTK_POLICY_ALWAYS);
353
354         clist_avail = gtk_clist_new_with_titles( GROUP_N_COLS, titles );
355         gtk_container_add( GTK_CONTAINER(clist_swin), clist_avail );
356         gtk_clist_set_selection_mode( GTK_CLIST(clist_avail), GTK_SELECTION_EXTENDED );
357         gtk_clist_set_column_width( GTK_CLIST(clist_avail), GROUP_COL_NAME, GROUP_COL_WIDTH_NAME );
358         gtk_clist_set_column_width( GTK_CLIST(clist_avail), GROUP_COL_EMAIL, GROUP_COL_WIDTH_EMAIL );
359         gtk_clist_set_compare_func( GTK_CLIST(clist_avail), edit_group_list_compare_func );
360         gtk_clist_set_auto_sort( GTK_CLIST(clist_avail), TRUE );
361
362         for( i = 0; i < GROUP_N_COLS; i++ )
363                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist_avail)->column[i].button, GTK_CAN_FOCUS);
364
365         /* Status line */
366         hsbox = gtk_hbox_new(FALSE, 0);
367         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
368         statusbar = gtk_statusbar_new();
369         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
370
371         /* Button panel */
372         gtkut_button_set_create(&hbbox, &ok_btn, _("OK"),
373                                 &cancel_btn, _("Cancel"), NULL, NULL);
374         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
375         gtk_widget_grab_default(ok_btn);
376
377         g_signal_connect(G_OBJECT(ok_btn), "clicked",
378                          G_CALLBACK(edit_group_ok), cancelled);
379         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
380                          G_CALLBACK(edit_group_cancel), cancelled);
381
382         gtk_widget_show_all(vbox);
383
384         /* Event handlers */
385         g_signal_connect( G_OBJECT(buttonGroup), "clicked",
386                           G_CALLBACK( edit_group_to_group ), NULL );
387         g_signal_connect( G_OBJECT(buttonAvail), "clicked",
388                           G_CALLBACK( edit_group_to_avail ), NULL );
389         g_signal_connect(G_OBJECT(clist_avail), "button_press_event",
390                          G_CALLBACK(edit_group_list_avail_button), NULL);
391         g_signal_connect(G_OBJECT(clist_group), "button_press_event",
392                          G_CALLBACK(edit_group_list_group_button), NULL);
393
394         groupeditdlg.window     = window;
395         groupeditdlg.ok_btn     = ok_btn;
396         groupeditdlg.cancel_btn = cancel_btn;
397         groupeditdlg.statusbar  = statusbar;
398         groupeditdlg.status_cid = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Edit Group Dialog" );
399
400         groupeditdlg.entry_name  = entry_name;
401         groupeditdlg.clist_group = GTK_CLIST( clist_group );
402         groupeditdlg.clist_avail = GTK_CLIST( clist_avail );
403
404         if( ! _edit_group_dfl_message_ ) {
405                 _edit_group_dfl_message_ = _( "Move E-Mail Addresses to or from Group with arrow buttons" );
406         }
407 }
408
409 /*
410 * Return list of email items.
411 */
412 static GList *edit_group_build_email_list() {
413         GtkCList *clist = GTK_CLIST(groupeditdlg.clist_group);
414         GList *listEMail = NULL;
415         ItemEMail *email;
416         gint row = 0;
417         while( (email = gtk_clist_get_row_data( clist, row )) ) {
418                 listEMail = g_list_append( listEMail, email );
419                 row++;
420         }
421         return listEMail;
422 }
423
424 /*
425 * Edit group.
426 * Enter: abf    Address book.
427 *        folder Parent folder for group (or NULL if adding to root folder). Argument is
428 *               only required for new objects).
429 *        group  Group to edit, or NULL for a new group object.
430 * Return: Edited object, or NULL if cancelled.
431 */
432 ItemGroup *addressbook_edit_group( AddressBookFile *abf, ItemFolder *parent, ItemGroup *group ) {
433         static gboolean cancelled;
434         GList *listEMail = NULL;
435         gchar *name;
436
437         if (!groupeditdlg.window)
438                 addressbook_edit_group_create(&cancelled);
439         gtk_widget_grab_focus(groupeditdlg.ok_btn);
440         gtk_widget_grab_focus(groupeditdlg.entry_name);
441         gtk_widget_show(groupeditdlg.window);
442         manage_window_set_transient(GTK_WINDOW(groupeditdlg.window));
443
444         /* Clear all fields */
445         edit_group_status_show( "" );
446         gtk_clist_clear( GTK_CLIST(groupeditdlg.clist_group) );
447         gtk_clist_clear( GTK_CLIST(groupeditdlg.clist_avail) );
448
449         if( group ) {
450                 if( ADDRITEM_NAME(group) )
451                         gtk_entry_set_text(GTK_ENTRY(groupeditdlg.entry_name), ADDRITEM_NAME(group) );
452                 edit_group_load_clist( groupeditdlg.clist_group, group->listEMail );
453                 gtk_window_set_title( GTK_WINDOW(groupeditdlg.window), _("Edit Group Details"));
454         }
455         else {
456                 gtk_window_set_title( GTK_WINDOW(groupeditdlg.window), _("Add New Group"));
457                 gtk_entry_set_text(GTK_ENTRY(groupeditdlg.entry_name), ADDRESSBOOK_GUESS_GROUP_NAME );
458         }
459
460         listEMail = addrbook_get_available_email_list( abf, group );
461         edit_group_load_clist( groupeditdlg.clist_avail, listEMail );
462         mgu_clear_list( listEMail );
463         listEMail = NULL;
464         gtk_clist_select_row( groupeditdlg.clist_group, 0, 0 );
465         gtk_clist_select_row( groupeditdlg.clist_avail, 0, 0 );
466
467         edit_group_status_show( _edit_group_dfl_message_ );
468
469         gtk_main();
470         gtk_widget_hide( groupeditdlg.window );
471
472         if( cancelled ) {
473                 return NULL;
474         }
475
476         listEMail = edit_group_build_email_list();
477         if( group ) {
478                 /* Update email list */
479                 addrbook_update_group_list( abf, group, listEMail );
480         }
481         else {
482                 /* Create new person and email list */
483                 group = addrbook_add_group_list( abf, parent, listEMail );
484         }
485         name = gtk_editable_get_chars( GTK_EDITABLE(groupeditdlg.entry_name), 0, -1 );
486         addritem_group_set_name( group, name );
487         g_free( name );
488
489         listEMail = NULL;
490         return group;
491 }
492
493 /*
494 * Edit folder.
495 * Enter: abf    Address book.
496 *        parent Parent folder for folder (or NULL if adding to root folder). Argument is
497 *               only required for new objects).
498 *        folder Folder to edit, or NULL for a new folder object.
499 * Return: Edited object, or NULL if cancelled.
500 */
501 ItemFolder *addressbook_edit_folder( AddressBookFile *abf, ItemFolder *parent, ItemFolder *folder ) {
502         gchar *name = NULL;
503
504         if( folder ) {
505                 name = g_strdup( ADDRITEM_NAME(folder) );
506                 name = input_dialog( _("Edit folder"), _("Input the new name of folder:"), name );
507         }
508         else {
509                 name = input_dialog( _("New folder"),
510                                 _("Input the name of new folder:"),
511                                 _(ADDRESSBOOK_GUESS_FOLDER_NAME) );
512         }
513         if( ! name ) return NULL;
514         g_strstrip( name );
515         if( *name == '\0' ) {
516                 g_free( name );
517                 return NULL;
518         }
519         if( folder ) {
520                 if( strcmp( name, ADDRITEM_NAME(folder) ) == 0 ) {
521                         g_free( name );
522                         return NULL;
523                 }
524         }
525
526         if( ! folder ) {
527                 folder = addrbook_add_new_folder( abf, parent );
528         }
529         addritem_folder_set_name( folder, name );
530         g_free( name );
531         return folder;
532 }
533
534 /*
535 * End of Source.
536 */
537