1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24 
25 #include "defs.h"
26 
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gdk/gdkkeysyms.h>
30 
31 #include "addressbook.h"
32 #include "addressitem.h"
33 #include "addrbook.h"
34 #include "addritem.h"
35 
36 #include "mgutils.h"
37 
38 #include "prefs_common.h"
39 
40 #include "alertpanel.h"
41 #include "inputdialog.h"
42 #include "manage_window.h"
43 #include "gtkutils.h"
44 
45 #define ADDRESSBOOK_GUESS_FOLDER_NAME	"NewFolder"
46 #define ADDRESSBOOK_GUESS_GROUP_NAME	"NewGroup"
47 
48 typedef enum {
49 	GROUP_COL_NAME    = 0,
50 	GROUP_COL_EMAIL   = 1,
51 	GROUP_COL_REMARKS = 2
52 } GroupEditEMailColumnPos;
53 
54 #define GROUP_N_COLS          3
55 #define GROUP_COL_WIDTH_NAME  140
56 #define GROUP_COL_WIDTH_EMAIL 120
57 
58 static struct _GroupEdit_dlg {
59 	GtkWidget *window;
60 	GtkWidget *ok_btn;
61 	GtkWidget *cancel_btn;
62 	GtkWidget *statusbar;
63 	gint status_cid;
64 
65 	/* Basic data tab */
66 	GtkWidget *entry_name;
67 	GtkCMCList *clist_group;
68 	GtkCMCList *clist_avail;
69 
70 	GHashTable *hashEMail;
71 
72 } groupeditdlg;
73 
74 
75 static gchar *_edit_group_dfl_message_ = NULL;
76 
edit_group_status_show(gchar * msg)77 static void edit_group_status_show( gchar *msg ) {
78 	if( groupeditdlg.statusbar != NULL ) {
79 		gtk_statusbar_pop( GTK_STATUSBAR(groupeditdlg.statusbar), groupeditdlg.status_cid );
80 		if( msg ) {
81 			gtk_statusbar_push( GTK_STATUSBAR(groupeditdlg.statusbar), groupeditdlg.status_cid, msg );
82 		}
83 	}
84 }
85 
edit_group_ok(GtkWidget * widget,gboolean * cancelled)86 static void edit_group_ok(GtkWidget *widget, gboolean *cancelled) {
87 	gchar *sName;
88 	gboolean errFlag = TRUE;
89 
90 	sName = gtk_editable_get_chars( GTK_EDITABLE(groupeditdlg.entry_name), 0, -1 );
91 	if( sName ) {
92 		g_strstrip( sName );
93 		if( *sName != '\0' ) {
94 			gtk_entry_set_text(GTK_ENTRY(groupeditdlg.entry_name), sName );
95 			*cancelled = FALSE;
96 			gtk_main_quit();
97 			errFlag = FALSE;
98 		}
99 	}
100 	if( errFlag ) {
101 		edit_group_status_show( _( "A Group Name must be supplied." ) );
102 	}
103 	g_free( sName );
104 }
105 
edit_group_cancel(GtkWidget * widget,gboolean * cancelled)106 static void edit_group_cancel(GtkWidget *widget, gboolean *cancelled) {
107 	*cancelled = TRUE;
108 	gtk_main_quit();
109 }
110 
edit_group_delete_event(GtkWidget * widget,GdkEventAny * event,gboolean * cancelled)111 static gint edit_group_delete_event(GtkWidget *widget, GdkEventAny *event, gboolean *cancelled) {
112 	*cancelled = TRUE;
113 	gtk_main_quit();
114 	return TRUE;
115 }
116 
edit_group_key_pressed(GtkWidget * widget,GdkEventKey * event,gboolean * cancelled)117 static gboolean edit_group_key_pressed(GtkWidget *widget, GdkEventKey *event, gboolean *cancelled) {
118 	if (event && event->keyval == GDK_KEY_Escape) {
119 		*cancelled = TRUE;
120 		gtk_main_quit();
121 	}
122 	return FALSE;
123 }
124 
edit_group_format_item_clist(ItemPerson * person,ItemEMail * email)125 static gchar *edit_group_format_item_clist( ItemPerson *person, ItemEMail *email ) {
126 	gchar *str = NULL;
127 	gchar *aName = ADDRITEM_NAME(email);
128 	if( aName == NULL || *aName == '\0' ) return str;
129 	if( person ) {
130 		str = g_strdup_printf( "%s - %s", ADDRITEM_NAME(person), aName );
131 	}
132 	else {
133 		str = g_strdup( aName );
134 	}
135 	return str;
136 }
137 
edit_group_clist_add_email(GtkCMCList * clist,ItemEMail * email)138 static gint edit_group_clist_add_email( GtkCMCList *clist, ItemEMail *email ) {
139 	ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(email);
140 	gchar *str = edit_group_format_item_clist( person, email );
141 	gchar *text[ GROUP_N_COLS ];
142 	gint row;
143 	if( str ) {
144 		text[ GROUP_COL_NAME ] = addressbook_set_col_name_guard(str);
145 	}
146 	else {
147 		text[ GROUP_COL_NAME ] = addressbook_set_col_name_guard(ADDRITEM_NAME(person));
148 	}
149 	text[ GROUP_COL_EMAIL   ] = email->address;
150 	text[ GROUP_COL_REMARKS ] = email->remarks;
151 
152 	row = gtk_cmclist_append( clist, text );
153 	gtk_cmclist_set_row_data( clist, row, email );
154 	return row;
155 }
156 
edit_group_load_clist(GtkCMCList * clist,GList * listEMail)157 static void edit_group_load_clist( GtkCMCList *clist, GList *listEMail ) {
158 	GList *node = listEMail;
159 	gtk_cmclist_freeze(clist);
160 	while( node ) {
161 		ItemEMail *email = node->data;
162 		edit_group_clist_add_email( clist, email );
163 		node = g_list_next( node );
164 	}
165 	gtk_cmclist_thaw(clist);
166 }
167 
168 
edit_group_move_email(GtkCMCList * clist_from,GtkCMCList * clist_to,GtkCMCTreeNode * node)169 static void edit_group_move_email( GtkCMCList *clist_from, GtkCMCList *clist_to, GtkCMCTreeNode *node ) {
170 	ItemEMail *email = gtk_cmctree_node_get_row_data( GTK_CMCTREE(clist_from), node );
171 	GtkCMCTreeNode *next = gtkut_ctree_node_next(GTK_CMCTREE(clist_from), node);
172 	GtkCMCTreeNode *prev = gtkut_ctree_node_prev(GTK_CMCTREE(clist_from), node);
173 	int rrow = 0;
174 	if( email ) {
175 		gtk_cmctree_remove_node(GTK_CMCTREE(clist_from), node);
176 		rrow = edit_group_clist_add_email( clist_to, email );
177 		gtk_cmclist_select_row( clist_to, rrow, 0 );
178 		if (next)
179 			gtk_cmctree_select(GTK_CMCTREE(clist_from), next);
180 		else if (prev)
181 			gtk_cmctree_select(GTK_CMCTREE(clist_from), prev);
182 	}
183 }
184 
edit_group_to_group(GtkWidget * widget,gpointer data)185 static void edit_group_to_group( GtkWidget *widget, gpointer data ) {
186 	GList *selected = g_list_copy(GTK_CMCLIST(groupeditdlg.clist_avail)->selection);
187 	/* Clear the selected rows on destination clist */
188 	gtk_cmclist_freeze(groupeditdlg.clist_avail);
189 	gtk_cmclist_freeze(groupeditdlg.clist_group);
190 	gtk_cmclist_unselect_all(groupeditdlg.clist_group);
191 	while (selected) {
192 		edit_group_move_email( groupeditdlg.clist_avail,
193 					groupeditdlg.clist_group, GTK_CMCTREE_NODE(selected->data) );
194 		selected = selected->next;
195 	}
196 	g_list_free(selected);
197 	gtk_cmclist_thaw(groupeditdlg.clist_avail);
198 	gtk_cmclist_thaw(groupeditdlg.clist_group);
199 }
200 
edit_group_to_avail(GtkWidget * widget,gpointer data)201 static void edit_group_to_avail( GtkWidget *widget, gpointer data ) {
202 	GList *selected = g_list_copy(GTK_CMCLIST(groupeditdlg.clist_group)->selection);
203 	gtk_cmclist_freeze(groupeditdlg.clist_avail);
204 	gtk_cmclist_freeze(groupeditdlg.clist_group);
205 	gtk_cmclist_unselect_all(groupeditdlg.clist_avail);
206 	while (selected) {
207 		edit_group_move_email( groupeditdlg.clist_group,
208 					groupeditdlg.clist_avail, GTK_CMCTREE_NODE(selected->data) );
209 		selected = selected->next;
210 	}
211 	g_list_free(selected);
212 	gtk_cmclist_thaw(groupeditdlg.clist_avail);
213 	gtk_cmclist_thaw(groupeditdlg.clist_group);
214 }
215 
edit_group_list_group_button(GtkCMCList * clist,GdkEventButton * event,gpointer data)216 static gboolean edit_group_list_group_button( GtkCMCList *clist, GdkEventButton *event, gpointer data ) {
217 	if( ! event ) return FALSE;
218 	if( event->window != clist->clist_window ) return FALSE;
219 
220 	if( event->button == 1 ) {
221 		if( event->type == GDK_2BUTTON_PRESS ) {
222 			edit_group_to_avail( NULL, NULL );
223 		}
224 	}
225 	return FALSE;
226 }
227 
edit_group_list_avail_button(GtkCMCList * clist,GdkEventButton * event,gpointer data)228 static gboolean edit_group_list_avail_button( GtkCMCList *clist, GdkEventButton *event, gpointer data ) {
229 	if( ! event ) return FALSE;
230 	if( event->window != clist->clist_window ) return FALSE;
231 
232 	if( event->button == 1 ) {
233 		if( event->type == GDK_2BUTTON_PRESS ) {
234 			edit_group_to_group( NULL, NULL );
235 		}
236 	}
237 	return FALSE;
238 }
239 
edit_group_list_compare_func(GtkCMCList * clist,gconstpointer ptr1,gconstpointer ptr2)240 static gint edit_group_list_compare_func( GtkCMCList *clist, gconstpointer ptr1, gconstpointer ptr2 ) {
241 	GtkCMCell *cell1 = ((GtkCMCListRow *)ptr1)->cell;
242 	GtkCMCell *cell2 = ((GtkCMCListRow *)ptr2)->cell;
243 	gchar *name1 = NULL, *name2 = NULL;
244 	if( cell1 ) name1 = cell1->u.text;
245 	if( cell2 ) name2 = cell2->u.text;
246 	if( ! name1 ) return ( name2 != NULL );
247 	if( ! name2 ) return -1;
248 	return g_utf8_collate( name1, name2 );
249 }
250 
addressbook_edit_group_size_allocate_cb(GtkWidget * widget,GtkAllocation * allocation)251 static void addressbook_edit_group_size_allocate_cb(GtkWidget *widget,
252 					 GtkAllocation *allocation)
253 {
254 	cm_return_if_fail(allocation != NULL);
255 
256 	prefs_common.addressbookeditgroupwin_width = allocation->width;
257 	prefs_common.addressbookeditgroupwin_height = allocation->height;
258 }
259 
addressbook_edit_group_create(gboolean * cancelled)260 static void addressbook_edit_group_create( gboolean *cancelled ) {
261 	GtkWidget *window;
262 	GtkWidget *vbox;
263 	GtkWidget *hbbox;
264 	GtkWidget *ok_btn;
265 	GtkWidget *cancel_btn;
266 	GtkWidget *hsbox;
267 	GtkWidget *statusbar;
268 
269 	GtkWidget *hboxg;
270 	GtkWidget *table;
271 	GtkWidget *label;
272 	GtkWidget *entry_name;
273 	GtkWidget *hboxl;
274 	GtkWidget *vboxl;
275 	GtkWidget *hboxh;
276 
277 	GtkWidget *clist_swin;
278 	GtkWidget *clist_group;
279 	GtkWidget *clist_avail;
280 
281 	GtkWidget *buttonGroup;
282 	GtkWidget *buttonAvail;
283 	gint top;
284 
285 	gchar *titles[ GROUP_N_COLS ];
286 	gint i;
287 
288 	static GdkGeometry geometry;
289 
290 	titles[ GROUP_COL_NAME    ] = _( "Name" );
291 	titles[ GROUP_COL_EMAIL   ] = _("Email Address");
292 	titles[ GROUP_COL_REMARKS ] = _("Remarks");
293 
294 	window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "editgroup");
295 	gtk_container_set_border_width(GTK_CONTAINER(window), 0);
296 	gtk_window_set_title(GTK_WINDOW(window), _("Edit Group Data"));
297 	gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
298 	gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DIALOG);
299 	g_signal_connect(G_OBJECT(window), "delete_event",
300 			 G_CALLBACK(edit_group_delete_event),
301 			 cancelled);
302 	g_signal_connect(G_OBJECT(window), "key_press_event",
303 			 G_CALLBACK(edit_group_key_pressed),
304 			 cancelled);
305 	g_signal_connect(G_OBJECT(window), "size_allocate",
306 			 G_CALLBACK(addressbook_edit_group_size_allocate_cb), NULL);
307 
308 	vbox = gtk_vbox_new( FALSE, 6 );
309 	gtk_container_set_border_width(GTK_CONTAINER(vbox), BORDER_WIDTH);
310 	gtk_widget_show( vbox );
311 	gtk_container_add( GTK_CONTAINER( window ), vbox );
312 
313 	/* Group area */
314 	hboxg = gtk_hbox_new( FALSE, 0 );
315 	gtk_box_pack_start(GTK_BOX(vbox), hboxg, FALSE, FALSE, 0);
316 
317 	/* Data entry area */
318 	table = gtk_table_new( 1, 3, FALSE);
319 	gtk_box_pack_start(GTK_BOX(hboxg), table, TRUE, TRUE, 0);
320 	gtk_container_set_border_width( GTK_CONTAINER(table), 4 );
321 	gtk_table_set_row_spacings(GTK_TABLE(table), 0);
322 	gtk_table_set_col_spacings(GTK_TABLE(table), 4);
323 
324 	/* First row */
325 	top = 0;
326 	label = gtk_label_new(_("Group Name"));
327 	gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
328 	gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
329 
330 	entry_name = gtk_entry_new();
331 	gtk_table_attach(GTK_TABLE(table), entry_name, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
332 
333 	/* List area */
334 	hboxl = gtk_hbox_new( FALSE, 6 );
335 	gtk_container_set_border_width( GTK_CONTAINER(hboxl), 8 );
336 	gtk_box_pack_start(GTK_BOX(vbox), hboxl, TRUE, TRUE, 0);
337 
338 	/* Group list */
339 	vboxl = gtk_vbox_new( FALSE, 0 );
340 	gtk_box_pack_start(GTK_BOX(hboxl), vboxl, TRUE, TRUE, 0);
341 
342 	hboxh = gtk_hbox_new( FALSE, 0 );
343 	gtk_container_set_border_width( GTK_CONTAINER(hboxh), 4 );
344 	gtk_box_pack_start(GTK_BOX(vboxl), hboxh, FALSE, FALSE, 0);
345 	label = gtk_label_new(_("Addresses in Group"));
346 	gtk_box_pack_start(GTK_BOX(hboxh), label, TRUE, TRUE, 0);
347 	buttonAvail = gtk_button_new_from_stock(GTK_STOCK_REMOVE);
348 	gtk_box_pack_end(GTK_BOX(hboxh), buttonAvail, FALSE, FALSE, 0);
349 
350 	clist_swin = gtk_scrolled_window_new( NULL, NULL );
351 	gtk_box_pack_start(GTK_BOX(vboxl), clist_swin, TRUE, TRUE, 0);
352 	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
353 				       GTK_POLICY_AUTOMATIC,
354 				       GTK_POLICY_AUTOMATIC);
355 
356 	clist_group = gtk_sctree_new_with_titles( GROUP_N_COLS, 0, titles );
357 	gtk_container_add( GTK_CONTAINER(clist_swin), clist_group );
358 	gtk_cmctree_set_expander_style(GTK_CMCTREE(clist_group),
359 			     GTK_CMCTREE_EXPANDER_TRIANGLE);
360 	gtk_sctree_set_stripes(GTK_SCTREE(clist_group), prefs_common.use_stripes_in_summaries);
361 	gtk_cmclist_set_selection_mode( GTK_CMCLIST(clist_group), GTK_SELECTION_MULTIPLE );
362 	gtk_cmclist_set_column_width( GTK_CMCLIST(clist_group), GROUP_COL_NAME, GROUP_COL_WIDTH_NAME );
363 	gtk_cmclist_set_column_width( GTK_CMCLIST(clist_group), GROUP_COL_EMAIL, GROUP_COL_WIDTH_EMAIL );
364 	gtk_cmclist_set_compare_func( GTK_CMCLIST(clist_group), edit_group_list_compare_func );
365 	gtk_cmclist_set_auto_sort( GTK_CMCLIST(clist_group), TRUE );
366 
367 	for( i = 0; i < GROUP_N_COLS; i++ )
368 		gtk_widget_set_can_focus(GTK_CMCLIST(clist_group)->column[i].button, FALSE);
369 
370 	/* Available list */
371 	vboxl = gtk_vbox_new( FALSE, 0 );
372 	gtk_box_pack_start(GTK_BOX(hboxl), vboxl, TRUE, TRUE, 0);
373 
374 	hboxh = gtk_hbox_new( FALSE, 0 );
375 	gtk_container_set_border_width( GTK_CONTAINER(hboxh), 4 );
376 	gtk_box_pack_start(GTK_BOX(vboxl), hboxh, FALSE, FALSE, 0);
377 	buttonGroup = gtk_button_new_from_stock(GTK_STOCK_ADD);
378 	gtk_box_pack_start(GTK_BOX(hboxh), buttonGroup, FALSE, FALSE, 0);
379 	label = gtk_label_new(_("Available Addresses"));
380 	gtk_box_pack_end(GTK_BOX(hboxh), label, TRUE, TRUE, 0);
381 
382 	clist_swin = gtk_scrolled_window_new( NULL, NULL );
383 	gtk_box_pack_start(GTK_BOX(vboxl), clist_swin, TRUE, TRUE, 0);
384 	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
385 				       GTK_POLICY_AUTOMATIC,
386 				       GTK_POLICY_AUTOMATIC);
387 
388 	clist_avail = gtk_sctree_new_with_titles( GROUP_N_COLS, 0, titles );
389 	gtk_container_add( GTK_CONTAINER(clist_swin), clist_avail );
390 	gtk_cmctree_set_expander_style(GTK_CMCTREE(clist_avail),
391 			     GTK_CMCTREE_EXPANDER_TRIANGLE);
392 	gtk_cmclist_set_selection_mode( GTK_CMCLIST(clist_avail), GTK_SELECTION_MULTIPLE );
393 	gtk_cmclist_set_column_width( GTK_CMCLIST(clist_avail), GROUP_COL_NAME, GROUP_COL_WIDTH_NAME );
394 	gtk_cmclist_set_column_width( GTK_CMCLIST(clist_avail), GROUP_COL_EMAIL, GROUP_COL_WIDTH_EMAIL );
395 	gtk_cmclist_set_compare_func( GTK_CMCLIST(clist_avail), edit_group_list_compare_func );
396 	gtk_cmclist_set_auto_sort( GTK_CMCLIST(clist_avail), TRUE );
397 
398 	for( i = 0; i < GROUP_N_COLS; i++ )
399 		gtk_widget_set_can_focus(GTK_CMCLIST(clist_avail)->column[i].button, FALSE);
400 
401 	/* Status line */
402 	hsbox = gtk_hbox_new(FALSE, 0);
403 	gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
404 	statusbar = gtk_statusbar_new();
405 	gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
406 
407 	/* Button panel */
408 	gtkut_stock_button_set_create(&hbbox, &cancel_btn, GTK_STOCK_CANCEL,
409 				      &ok_btn, GTK_STOCK_OK,
410 				      NULL, NULL);
411 	gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
412 	gtk_widget_grab_default(ok_btn);
413 
414 	g_signal_connect(G_OBJECT(ok_btn), "clicked",
415 			 G_CALLBACK(edit_group_ok), cancelled);
416 	g_signal_connect(G_OBJECT(cancel_btn), "clicked",
417 			 G_CALLBACK(edit_group_cancel), cancelled);
418 
419 	gtk_widget_show_all(vbox);
420 
421 	/* Event handlers */
422 	g_signal_connect( G_OBJECT(buttonGroup), "clicked",
423 			  G_CALLBACK( edit_group_to_group ), NULL );
424 	g_signal_connect( G_OBJECT(buttonAvail), "clicked",
425 			  G_CALLBACK( edit_group_to_avail ), NULL );
426 	g_signal_connect(G_OBJECT(clist_avail), "button_press_event",
427 			 G_CALLBACK(edit_group_list_avail_button), NULL);
428 	g_signal_connect(G_OBJECT(clist_group), "button_press_event",
429 			 G_CALLBACK(edit_group_list_group_button), NULL);
430 
431 	if (!geometry.min_height) {
432 		geometry.min_width = 580;
433 		geometry.min_height = 340;
434 	}
435 
436 	gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
437 				      GDK_HINT_MIN_SIZE);
438 	gtk_widget_set_size_request(window,
439 					prefs_common.addressbookeditgroupwin_width,
440 				    prefs_common.addressbookeditgroupwin_height);
441 
442 	groupeditdlg.window     = window;
443 	groupeditdlg.ok_btn     = ok_btn;
444 	groupeditdlg.cancel_btn = cancel_btn;
445 	groupeditdlg.statusbar  = statusbar;
446 	groupeditdlg.status_cid = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Edit Group Dialog" );
447 
448 	groupeditdlg.entry_name  = entry_name;
449 	groupeditdlg.clist_group = GTK_CMCLIST( clist_group );
450 	groupeditdlg.clist_avail = GTK_CMCLIST( clist_avail );
451 
452 	if( ! _edit_group_dfl_message_ ) {
453 		_edit_group_dfl_message_ = _( "Move Email Addresses to or from Group with arrow buttons" );
454 	}
455 }
456 
457 /*
458 * Return list of email items.
459 */
edit_group_build_email_list()460 static GList *edit_group_build_email_list() {
461 	GtkCMCList *clist = GTK_CMCLIST(groupeditdlg.clist_group);
462 	GList *listEMail = NULL;
463 	ItemEMail *email;
464 	gint row = 0;
465 	while( (email = gtk_cmclist_get_row_data( clist, row )) ) {
466 		listEMail = g_list_append( listEMail, email );
467 		row++;
468 	}
469 	return listEMail;
470 }
471 
472 /*
473 * Edit group.
474 * Enter: abf    Address book.
475 *        folder Parent folder for group (or NULL if adding to root folder). Argument is
476 *               only required for new objects).
477 *        group  Group to edit, or NULL for a new group object.
478 * Return: Edited object, or NULL if cancelled.
479 */
addressbook_edit_group(AddressBookFile * abf,ItemFolder * parent,ItemGroup * group)480 ItemGroup *addressbook_edit_group( AddressBookFile *abf, ItemFolder *parent, ItemGroup *group ) {
481 	static gboolean cancelled;
482 	GList *listEMail = NULL;
483 	gchar *name;
484 
485 	if (!groupeditdlg.window)
486 		addressbook_edit_group_create(&cancelled);
487 	gtk_widget_grab_focus(groupeditdlg.ok_btn);
488 	gtk_widget_grab_focus(groupeditdlg.entry_name);
489 	gtk_widget_show(groupeditdlg.window);
490 	manage_window_set_transient(GTK_WINDOW(groupeditdlg.window));
491 	gtk_window_set_modal(GTK_WINDOW(groupeditdlg.window), TRUE);
492 	/* Clear all fields */
493 	edit_group_status_show( "" );
494 	gtk_cmclist_clear( GTK_CMCLIST(groupeditdlg.clist_group) );
495 	gtk_cmclist_clear( GTK_CMCLIST(groupeditdlg.clist_avail) );
496 
497 	if( group ) {
498 		if( ADDRITEM_NAME(group) )
499 			gtk_entry_set_text(GTK_ENTRY(groupeditdlg.entry_name), ADDRITEM_NAME(group) );
500 		edit_group_load_clist( groupeditdlg.clist_group, group->listEMail );
501 		gtk_window_set_title( GTK_WINDOW(groupeditdlg.window), _("Edit Group Details"));
502 	}
503 	else {
504 		gtk_window_set_title( GTK_WINDOW(groupeditdlg.window), _("Add New Group"));
505 		gtk_entry_set_text(GTK_ENTRY(groupeditdlg.entry_name), ADDRESSBOOK_GUESS_GROUP_NAME );
506 	}
507 
508 	listEMail = addrbook_get_available_email_list( abf, group );
509 	edit_group_load_clist( groupeditdlg.clist_avail, listEMail );
510 	g_list_free( listEMail );
511 	gtk_cmclist_select_row( groupeditdlg.clist_group, 0, 0 );
512 	gtk_cmclist_select_row( groupeditdlg.clist_avail, 0, 0 );
513 
514 	edit_group_status_show( _edit_group_dfl_message_ );
515 
516 	gtk_main();
517 	gtk_widget_hide( groupeditdlg.window );
518 	gtk_window_set_modal(GTK_WINDOW(groupeditdlg.window), FALSE);
519 	if( cancelled ) {
520 		return NULL;
521 	}
522 
523 	listEMail = edit_group_build_email_list();
524 	if( group ) {
525 		/* Update email list */
526 		addrbook_update_group_list( abf, group, listEMail );
527 	}
528 	else {
529 		/* Create new person and email list */
530 		group = addrbook_add_group_list( abf, parent, listEMail );
531 	}
532 	name = gtk_editable_get_chars( GTK_EDITABLE(groupeditdlg.entry_name), 0, -1 );
533 	addritem_group_set_name( group, name );
534 	g_free( name );
535 
536 	listEMail = NULL;
537 	return group;
538 }
539 
540 /*
541 * Edit folder.
542 * Enter: abf    Address book.
543 *        parent Parent folder for folder (or NULL if adding to root folder). Argument is
544 *               only required for new objects).
545 *        folder	Folder to edit, or NULL for a new folder object.
546 * Return: Edited object, or NULL if cancelled.
547 */
addressbook_edit_folder(AddressBookFile * abf,ItemFolder * parent,ItemFolder * folder)548 ItemFolder *addressbook_edit_folder( AddressBookFile *abf, ItemFolder *parent, ItemFolder *folder ) {
549 	gchar *name = NULL;
550 
551 	if( folder ) {
552 		name = g_strdup( ADDRITEM_NAME(folder) );
553 		name = input_dialog( _("Edit folder"), _("Input the new name of folder:"), name );
554 	}
555 	else {
556 		name = input_dialog( _("New folder"),
557 				_("Input the name of new folder:"),
558 				_(ADDRESSBOOK_GUESS_FOLDER_NAME) );
559 	}
560 	if( ! name ) return NULL;
561 	g_strstrip( name );
562 	if( *name == '\0' ) {
563 		g_free( name );
564 		return NULL;
565 	}
566 	if( folder ) {
567 		if( strcmp( name, ADDRITEM_NAME(folder) ) == 0 ) {
568 			g_free( name );
569 			return NULL;
570 		}
571 	}
572 
573 	if( ! folder ) {
574 		folder = addrbook_add_new_folder( abf, parent );
575 	}
576 	addritem_folder_set_name( folder, name );
577 	g_free( name );
578 	return folder;
579 }
580 
581 /*
582 * End of Source.
583 */
584 
585