0.8.6claws68
[claws.git] / src / editgroup.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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 void 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 }
125
126 static gchar *edit_group_format_item_clist( ItemPerson *person, ItemEMail *email ) {
127         gchar *str = NULL;
128         gchar *aName = ADDRITEM_NAME(email);
129         if( aName == NULL || *aName == '\0' ) return str;
130         if( person ) {
131                 str = g_strdup_printf( "%s - %s", ADDRITEM_NAME(person), aName );
132         }
133         else {
134                 str = g_strdup( aName );
135         }
136         return str;
137 }
138
139 static gint edit_group_clist_add_email( GtkCList *clist, ItemEMail *email ) {
140         ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(email);
141         gchar *str = edit_group_format_item_clist( person, email );
142         gchar *text[ GROUP_N_COLS ];
143         gint row;
144         if( str ) {
145                 text[ GROUP_COL_NAME ] = str;
146         }
147         else {
148                 text[ GROUP_COL_NAME ] = ADDRITEM_NAME(person);
149         }
150         text[ GROUP_COL_EMAIL   ] = email->address;
151         text[ GROUP_COL_REMARKS ] = email->remarks;
152         row = gtk_clist_append( clist, text );
153         gtk_clist_set_row_data( clist, row, email );
154         return row;
155 }
156
157 static void edit_group_load_clist( GtkCList *clist, GList *listEMail ) {
158         GList *node = listEMail;
159         while( node ) {
160                 ItemEMail *email = node->data;
161                 edit_group_clist_add_email( clist, email );
162                 node = g_list_next( node );
163         }
164 }
165
166
167 static gint edit_group_move_email( GtkCList *clist_from, GtkCList *clist_to, gint row ) {
168         ItemEMail *email = gtk_clist_get_row_data( clist_from, row );
169         gint rrow = -1;
170         if( email ) {
171                 gtk_clist_remove( clist_from, row );
172                 rrow = edit_group_clist_add_email( clist_to, email );
173                 gtk_clist_select_row( clist_to, rrow, 0 );
174         }
175         return rrow;
176 }
177
178 static void edit_group_to_group( GtkWidget *widget, gpointer data ) {
179         GList *selected = GTK_CLIST(groupeditdlg.clist_avail)->selection;
180         /* Clear the selected rows on destination clist */
181         gtk_clist_unselect_all(groupeditdlg.clist_group);
182         while (selected) {
183                 edit_group_move_email( groupeditdlg.clist_avail,
184                                         groupeditdlg.clist_group, GPOINTER_TO_UINT(selected->data) );
185                 /* cannot use g_list_next as the selection list will have changed */
186                 selected = GTK_CLIST(groupeditdlg.clist_avail)->selection;
187         }
188 }
189
190 static void edit_group_to_avail( GtkWidget *widget, gpointer data ) {
191         GList *selected = GTK_CLIST(groupeditdlg.clist_group)->selection;
192         gtk_clist_unselect_all(groupeditdlg.clist_avail);
193         while (selected) {
194                 edit_group_move_email( groupeditdlg.clist_group,
195                                         groupeditdlg.clist_avail, GPOINTER_TO_UINT(selected->data) );
196                 selected = GTK_CLIST(groupeditdlg.clist_group)->selection;
197         }
198 }
199
200 static void edit_group_list_group_button( GtkCList *clist, GdkEventButton *event, gpointer data ) {
201         if( ! event ) return;
202         if( event->button == 1 ) {
203                 if( event->type == GDK_2BUTTON_PRESS ) {
204                         edit_group_to_avail( NULL, NULL );
205                 }
206         }
207 }
208
209 static void edit_group_list_avail_button( GtkCList *clist, GdkEventButton *event, gpointer data ) {
210         if( ! event ) return;
211         if( event->button == 1 ) {
212                 if( event->type == GDK_2BUTTON_PRESS ) {
213                         edit_group_to_group( NULL, NULL );
214                 }
215         }
216 }
217
218 static gint edit_group_list_compare_func( GtkCList *clist, gconstpointer ptr1, gconstpointer ptr2 ) {
219         GtkCell *cell1 = ((GtkCListRow *)ptr1)->cell;
220         GtkCell *cell2 = ((GtkCListRow *)ptr2)->cell;
221         gchar *name1 = NULL, *name2 = NULL;
222         if( cell1 ) name1 = cell1->u.text;
223         if( cell2 ) name2 = cell2->u.text;
224         if( ! name1 ) return ( name2 != NULL );
225         if( ! name2 ) return -1;
226         return strcasecmp( name1, name2 );
227 }
228
229 static void addressbook_edit_group_create( gboolean *cancelled ) {
230         GtkWidget *window;
231         GtkWidget *vbox;
232         GtkWidget *hbbox;
233         GtkWidget *ok_btn;
234         GtkWidget *cancel_btn;
235         GtkWidget *hsbox;
236         GtkWidget *statusbar;
237
238         GtkWidget *hboxg;
239         GtkWidget *table;
240         GtkWidget *label;
241         GtkWidget *entry_name;
242         GtkWidget *hboxl;
243         GtkWidget *vboxl;
244         GtkWidget *hboxh;
245
246         GtkWidget *clist_swin;
247         GtkWidget *clist_group;
248         GtkWidget *clist_avail;
249
250         GtkWidget *buttonGroup;
251         GtkWidget *buttonAvail;
252         gint top;
253
254         gchar *titles[ GROUP_N_COLS ];
255         gint i;
256
257         titles[ GROUP_COL_NAME    ] = _( "Name" );
258         titles[ GROUP_COL_EMAIL   ] = _("E-Mail Address");
259         titles[ GROUP_COL_REMARKS ] = _("Remarks");
260
261         window = gtk_window_new(GTK_WINDOW_DIALOG);
262         gtk_widget_set_usize(window, EDITGROUP_WIDTH, EDITGROUP_HEIGHT );
263         gtk_container_set_border_width(GTK_CONTAINER(window), 0);
264         gtk_window_set_title(GTK_WINDOW(window), _("Edit Group Data"));
265         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
266         gtk_window_set_modal(GTK_WINDOW(window), TRUE); 
267         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
268                            GTK_SIGNAL_FUNC(edit_group_delete_event),
269                            cancelled);
270         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
271                            GTK_SIGNAL_FUNC(edit_group_key_pressed),
272                            cancelled);
273
274         vbox = gtk_vbox_new( FALSE, 6 );
275         gtk_container_set_border_width(GTK_CONTAINER(vbox), BORDER_WIDTH);
276         gtk_widget_show( vbox );
277         gtk_container_add( GTK_CONTAINER( window ), vbox );
278
279         /* Group area */
280         hboxg = gtk_hbox_new( FALSE, 0 );
281         gtk_box_pack_start(GTK_BOX(vbox), hboxg, FALSE, FALSE, 0);
282
283         /* Data entry area */
284         table = gtk_table_new( 1, 3, FALSE);
285         gtk_box_pack_start(GTK_BOX(hboxg), table, TRUE, TRUE, 0);
286         gtk_container_set_border_width( GTK_CONTAINER(table), 4 );
287         gtk_table_set_row_spacings(GTK_TABLE(table), 0);
288         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
289
290         /* First row */
291         top = 0;
292         label = gtk_label_new(_("Group Name"));
293         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
294         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
295
296         entry_name = gtk_entry_new();
297         gtk_table_attach(GTK_TABLE(table), entry_name, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
298
299         /* List area */
300         hboxl = gtk_hbox_new( FALSE, 6 );
301         gtk_container_set_border_width( GTK_CONTAINER(hboxl), 8 );
302         gtk_box_pack_start(GTK_BOX(vbox), hboxl, TRUE, TRUE, 0);
303
304         /* Group list */
305         vboxl = gtk_vbox_new( FALSE, 0 );
306         gtk_box_pack_start(GTK_BOX(hboxl), vboxl, TRUE, TRUE, 0);
307
308         hboxh = gtk_hbox_new( FALSE, 0 );
309         gtk_container_set_border_width( GTK_CONTAINER(hboxh), 4 );
310         gtk_box_pack_start(GTK_BOX(vboxl), hboxh, FALSE, FALSE, 0);
311         label = gtk_label_new(_("Addresses in Group"));
312         gtk_box_pack_start(GTK_BOX(hboxh), label, TRUE, TRUE, 0);
313         buttonAvail = gtk_button_new_with_label( _( " -> " ) );
314         gtk_box_pack_end(GTK_BOX(hboxh), buttonAvail, FALSE, FALSE, 0);
315
316         clist_swin = gtk_scrolled_window_new( NULL, NULL );
317         gtk_box_pack_start(GTK_BOX(vboxl), clist_swin, TRUE, TRUE, 0);
318         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
319                                        GTK_POLICY_AUTOMATIC,
320                                        GTK_POLICY_ALWAYS);
321
322         clist_group = gtk_clist_new_with_titles( GROUP_N_COLS, titles );
323         gtk_container_add( GTK_CONTAINER(clist_swin), clist_group );
324         gtk_clist_set_selection_mode( GTK_CLIST(clist_group), GTK_SELECTION_EXTENDED );
325         gtk_clist_set_column_width( GTK_CLIST(clist_group), GROUP_COL_NAME, GROUP_COL_WIDTH_NAME );
326         gtk_clist_set_column_width( GTK_CLIST(clist_group), GROUP_COL_EMAIL, GROUP_COL_WIDTH_EMAIL );
327         gtk_clist_set_compare_func( GTK_CLIST(clist_group), edit_group_list_compare_func );
328         gtk_clist_set_auto_sort( GTK_CLIST(clist_group), TRUE );
329
330         for( i = 0; i < GROUP_N_COLS; i++ )
331                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist_group)->column[i].button, GTK_CAN_FOCUS);
332
333         /* Available list */
334         vboxl = gtk_vbox_new( FALSE, 0 );
335         gtk_box_pack_start(GTK_BOX(hboxl), vboxl, TRUE, TRUE, 0);
336
337         hboxh = gtk_hbox_new( FALSE, 0 );
338         gtk_container_set_border_width( GTK_CONTAINER(hboxh), 4 );
339         gtk_box_pack_start(GTK_BOX(vboxl), hboxh, FALSE, FALSE, 0);
340         buttonGroup = gtk_button_new_with_label( _( " <- " ) );
341         gtk_box_pack_start(GTK_BOX(hboxh), buttonGroup, FALSE, FALSE, 0);
342         label = gtk_label_new(_("Available Addresses"));
343         gtk_box_pack_end(GTK_BOX(hboxh), label, TRUE, TRUE, 0);
344
345         clist_swin = gtk_scrolled_window_new( NULL, NULL );
346         gtk_box_pack_start(GTK_BOX(vboxl), clist_swin, TRUE, TRUE, 0);
347         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
348                                        GTK_POLICY_AUTOMATIC,
349                                        GTK_POLICY_ALWAYS);
350
351         clist_avail = gtk_clist_new_with_titles( GROUP_N_COLS, titles );
352         gtk_container_add( GTK_CONTAINER(clist_swin), clist_avail );
353         gtk_clist_set_selection_mode( GTK_CLIST(clist_avail), GTK_SELECTION_EXTENDED );
354         gtk_clist_set_column_width( GTK_CLIST(clist_avail), GROUP_COL_NAME, GROUP_COL_WIDTH_NAME );
355         gtk_clist_set_column_width( GTK_CLIST(clist_avail), GROUP_COL_EMAIL, GROUP_COL_WIDTH_EMAIL );
356         gtk_clist_set_compare_func( GTK_CLIST(clist_avail), edit_group_list_compare_func );
357         gtk_clist_set_auto_sort( GTK_CLIST(clist_avail), TRUE );
358
359         for( i = 0; i < GROUP_N_COLS; i++ )
360                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist_avail)->column[i].button, GTK_CAN_FOCUS);
361
362         /* Status line */
363         hsbox = gtk_hbox_new(FALSE, 0);
364         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
365         statusbar = gtk_statusbar_new();
366         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
367
368         /* Button panel */
369         gtkut_button_set_create(&hbbox, &ok_btn, _("OK"),
370                                 &cancel_btn, _("Cancel"), NULL, NULL);
371         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
372         gtk_widget_grab_default(ok_btn);
373
374         gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
375                            GTK_SIGNAL_FUNC(edit_group_ok), cancelled);
376         gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
377                            GTK_SIGNAL_FUNC(edit_group_cancel), cancelled);
378
379         gtk_widget_show_all(vbox);
380
381         /* Event handlers */
382         gtk_signal_connect( GTK_OBJECT(buttonGroup), "clicked",
383                         GTK_SIGNAL_FUNC( edit_group_to_group ), NULL );
384         gtk_signal_connect( GTK_OBJECT(buttonAvail), "clicked",
385                         GTK_SIGNAL_FUNC( edit_group_to_avail ), NULL );
386         gtk_signal_connect(GTK_OBJECT(clist_avail), "button_press_event",
387                            GTK_SIGNAL_FUNC(edit_group_list_avail_button), NULL);
388         gtk_signal_connect(GTK_OBJECT(clist_group), "button_press_event",
389                            GTK_SIGNAL_FUNC(edit_group_list_group_button), NULL);
390
391         groupeditdlg.window     = window;
392         groupeditdlg.ok_btn     = ok_btn;
393         groupeditdlg.cancel_btn = cancel_btn;
394         groupeditdlg.statusbar  = statusbar;
395         groupeditdlg.status_cid = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Edit Group Dialog" );
396
397         groupeditdlg.entry_name  = entry_name;
398         groupeditdlg.clist_group = GTK_CLIST( clist_group );
399         groupeditdlg.clist_avail = GTK_CLIST( clist_avail );
400
401         if( ! _edit_group_dfl_message_ ) {
402                 _edit_group_dfl_message_ = _( "Move E-Mail Addresses to or from Group with arrow buttons" );
403         }
404 }
405
406 /*
407 * Return list of email items.
408 */
409 static GList *edit_group_build_email_list() {
410         GtkCList *clist = GTK_CLIST(groupeditdlg.clist_group);
411         GList *listEMail = NULL;
412         ItemEMail *email;
413         gint row = 0;
414         while( (email = gtk_clist_get_row_data( clist, row )) ) {
415                 listEMail = g_list_append( listEMail, email );
416                 row++;
417         }
418         return listEMail;
419 }
420
421 /*
422 * Edit group.
423 * Enter: abf    Address book.
424 *        folder Parent folder for group (or NULL if adding to root folder). Argument is
425 *               only required for new objects).
426 *        group  Group to edit, or NULL for a new group object.
427 * Return: Edited object, or NULL if cancelled.
428 */
429 ItemGroup *addressbook_edit_group( AddressBookFile *abf, ItemFolder *parent, ItemGroup *group ) {
430         static gboolean cancelled;
431         GList *listEMail = NULL;
432         gchar *name;
433
434         if (!groupeditdlg.window)
435                 addressbook_edit_group_create(&cancelled);
436         gtk_widget_grab_focus(groupeditdlg.ok_btn);
437         gtk_widget_grab_focus(groupeditdlg.entry_name);
438         gtk_widget_show(groupeditdlg.window);
439         manage_window_set_transient(GTK_WINDOW(groupeditdlg.window));
440
441         /* Clear all fields */
442         edit_group_status_show( "" );
443         gtk_clist_clear( GTK_CLIST(groupeditdlg.clist_group) );
444         gtk_clist_clear( GTK_CLIST(groupeditdlg.clist_avail) );
445
446         if( group ) {
447                 if( ADDRITEM_NAME(group) )
448                         gtk_entry_set_text(GTK_ENTRY(groupeditdlg.entry_name), ADDRITEM_NAME(group) );
449                 edit_group_load_clist( groupeditdlg.clist_group, group->listEMail );
450                 gtk_window_set_title( GTK_WINDOW(groupeditdlg.window), _("Edit Group Details"));
451         }
452         else {
453                 gtk_window_set_title( GTK_WINDOW(groupeditdlg.window), _("Add New Group"));
454                 gtk_entry_set_text(GTK_ENTRY(groupeditdlg.entry_name), ADDRESSBOOK_GUESS_GROUP_NAME );
455         }
456
457         listEMail = addrbook_get_available_email_list( abf, group );
458         edit_group_load_clist( groupeditdlg.clist_avail, listEMail );
459         mgu_clear_list( listEMail );
460         listEMail = NULL;
461         gtk_clist_select_row( groupeditdlg.clist_group, 0, 0 );
462         gtk_clist_select_row( groupeditdlg.clist_avail, 0, 0 );
463
464         edit_group_status_show( _edit_group_dfl_message_ );
465
466         gtk_main();
467         gtk_widget_hide( groupeditdlg.window );
468
469         if( cancelled ) {
470                 return NULL;
471         }
472
473         listEMail = edit_group_build_email_list();
474         if( group ) {
475                 /* Update email list */
476                 addrbook_update_group_list( abf, group, listEMail );
477         }
478         else {
479                 /* Create new person and email list */
480                 group = addrbook_add_group_list( abf, parent, listEMail );
481         }
482         name = gtk_editable_get_chars( GTK_EDITABLE(groupeditdlg.entry_name), 0, -1 );
483         addritem_group_set_name( group, name );
484         g_free( name );
485
486         listEMail = NULL;
487         return group;
488 }
489
490 /*
491 * Edit folder.
492 * Enter: abf    Address book.
493 *        parent Parent folder for folder (or NULL if adding to root folder). Argument is
494 *               only required for new objects).
495 *        folder Folder to edit, or NULL for a new folder object.
496 * Return: Edited object, or NULL if cancelled.
497 */
498 ItemFolder *addressbook_edit_folder( AddressBookFile *abf, ItemFolder *parent, ItemFolder *folder ) {
499         gchar *name = NULL;
500
501         if( folder ) {
502                 name = g_strdup( ADDRITEM_NAME(folder) );
503                 name = input_dialog( _("Edit folder"), _("Input the new name of folder:"), name );
504         }
505         else {
506                 name = input_dialog( _("New folder"),
507                                 _("Input the name of new folder:"),
508                                 _(ADDRESSBOOK_GUESS_FOLDER_NAME) );
509         }
510         if( ! name ) return NULL;
511         g_strstrip( name );
512         if( *name == '\0' ) {
513                 g_free( name );
514                 return NULL;
515         }
516         if( folder ) {
517                 if( strcasecmp( name, ADDRITEM_NAME(folder) ) == 0 ) {
518                         g_free( name );
519                         return NULL;
520                 }
521         }
522
523         if( ! folder ) {
524                 folder = addrbook_add_new_folder( abf, parent );
525         }
526         addritem_folder_set_name( folder, name );
527         g_free( name );
528         return folder;
529 }
530
531 /*
532 * End of Source.
533 */
534