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