Use the correct type for adjustments
[claws.git] / src / addrgather.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2002-2012 Match Grun 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 /*
21 * Dialog for gathering EMail addresses from mail folder.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #include "claws-features.h"
27 #endif
28
29 #include "defs.h"
30
31 #include <glib.h>
32 #include <glib/gi18n.h>
33 #include <gdk/gdkkeysyms.h>
34 #include <gtk/gtk.h>
35
36 #include "main.h"
37 #include "inc.h"
38 #include "mbox.h"
39 #include "filesel.h"
40 #include "foldersel.h"
41 #include "gtkutils.h"
42 #include "alertpanel.h"
43 #include "manage_window.h"
44 #include "folder.h"
45 #include "utils.h"
46 #include "prefs_common.h"
47
48 #include "addrharvest.h"
49 #ifndef USE_NEW_ADDRBOOK
50         #include "addrindex.h"
51         #include "addrbook.h"
52 #else
53         #include "addressbook-dbus.h"
54 #endif
55 #define PAGE_FIELDS     0
56 #define PAGE_FINISH     1
57
58 #define NUM_FIELDS      6
59
60 #define FIELDS_N_COLS              2
61 #define FIELDS_COL_WIDTH_HEADER    100
62 #define FIELDS_COL_WIDTH_COUNT     140
63
64 #define MIN_FOLDER_SIZE 20
65 #define DFL_FOLDER_SIZE 50
66
67 typedef enum {
68         FIELD_COL_HEADER = 0,
69         FIELD_COL_COUNT  = 1
70 } AddrHarvest;
71
72 /*
73 * The dialog.
74 */
75 static struct _AddrHarvest {
76         GtkWidget *window;
77         GtkWidget *notebook;
78         GtkWidget *labelFolder;
79         GtkWidget *entryBook;
80         GtkWidget *checkHeader[ NUM_FIELDS ];
81 #ifndef USE_NEW_ADDRBOOK
82         GtkWidget *spinbtnFolder;
83 #endif
84         GtkWidget *checkRecurse;
85         GtkWidget *btnOk;
86         GtkWidget *btnCancel;
87         GtkWidget *statusbar;
88         gint      status_cid;
89         gboolean  cancelled;
90         gboolean  done;
91         gchar     *folderPath;
92         GtkWidget *clistCount;
93 } addrgather_dlg;
94
95 #ifndef USE_NEW_ADDRBOOK
96 static AddressIndex *_harv_addressIndex_;
97 static AddressBookFile *_harv_addressBook_;
98 #endif
99 static gchar *_harv_headerNames_[] = {
100         HEADER_FROM,
101         HEADER_REPLY_TO,
102         HEADER_SENDER,
103         HEADER_TO,
104         HEADER_CC,
105         HEADER_ERRORS_TO
106 };
107 static GList *_harv_messageList_;
108
109 static void addrgather_dlg_status_show( gchar *msg ) {
110         if( addrgather_dlg.statusbar != NULL ) {
111                 gtk_statusbar_pop( GTK_STATUSBAR(addrgather_dlg.statusbar),
112                         addrgather_dlg.status_cid );
113                 if( msg ) {
114                         gtk_statusbar_push(
115                                 GTK_STATUSBAR(addrgather_dlg.statusbar),
116                                 addrgather_dlg.status_cid, msg );
117                 }
118         }
119 }
120
121 static gint addrgather_dlg_delete_event(
122         GtkWidget *widget, GdkEventAny *event, gpointer data )
123 {
124         addrgather_dlg.cancelled = TRUE;
125         gtk_main_quit();
126         return TRUE;
127 }
128
129 static gboolean addrgather_dlg_key_pressed(
130         GtkWidget *widget, GdkEventKey *event, gpointer data )
131 {
132         if( event && event->keyval == GDK_KEY_Escape ) {
133                 addrgather_dlg.cancelled = TRUE;
134                 gtk_main_quit();
135         }
136         return FALSE;
137 }
138
139 static void addrgather_size_allocate(
140         GtkWidget *widget, GtkAllocation *allocation )
141 {
142         cm_return_if_fail( allocation != NULL );
143         
144         prefs_common.addrgather_width   = allocation->width;
145         prefs_common.addrgather_height  = allocation->height;
146 }
147
148 #define FMT_BUFSIZE 32
149
150 static gboolean addrgather_dlg_harvest() {
151         GtkCMCList *clist;
152         gchar *text[ FIELDS_N_COLS ];
153         AddressHarvester *harvester;
154         gchar *name;
155 #ifndef USE_NEW_ADDRBOOK
156         AddressBookFile *abf;
157         gchar *newFile;
158 #else
159     GList* list;
160     ContactData* contact;
161     GError* error = NULL;
162 #endif
163         gchar str[ FMT_BUFSIZE ];
164         gint cnt;
165         gint i;
166 #ifndef USE_NEW_ADDRBOOK
167         gint sz;
168
169         name = gtk_editable_get_chars( GTK_EDITABLE(addrgather_dlg.entryBook), 0, -1 );
170         if( name == NULL || strlen( name ) < 1 ) {
171                 addrgather_dlg_status_show(
172                         _( "Please specify name for address book." ) );
173                 g_free( name );
174                 return FALSE;
175 #else
176         name = gtk_combo_box_get_active_text(GTK_COMBO_BOX(addrgather_dlg.entryBook));
177         if( name == NULL || strlen( name ) < 1 ) {
178                 addrgather_dlg_status_show(
179                         _("No available address book."));
180                 g_free( name );
181                 return FALSE;
182 #endif
183         }
184
185         /* Create harvest helper */
186         harvester = addrharvest_create();
187         addrharvest_set_path( harvester, addrgather_dlg.folderPath );
188
189         for( i = 0; i < NUM_FIELDS; i++ ) {
190                 addrharvest_set_header( harvester, _harv_headerNames_[i],
191                 gtk_toggle_button_get_active(
192                         GTK_TOGGLE_BUTTON(addrgather_dlg.checkHeader[i]) ) );
193         }
194         addrharvest_set_recurse( harvester,
195                 gtk_toggle_button_get_active(
196                         GTK_TOGGLE_BUTTON( addrgather_dlg.checkRecurse ) ) );
197
198         if( addrharvest_check_header( harvester ) == FALSE ) {
199                 addrgather_dlg_status_show(
200                         _( "Please select the mail headers to search." ) );
201                 addrharvest_free( harvester );
202                 g_free( name );
203                 return FALSE;
204         }
205
206         /* Go fer it */
207         addrgather_dlg_status_show( _( "Collecting addresses..." ) );
208         GTK_EVENTS_FLUSH();
209 #ifndef USE_NEW_ADDRBOOK
210         sz = gtk_spin_button_get_value_as_int(
211                 GTK_SPIN_BUTTON( addrgather_dlg.spinbtnFolder ) );
212         addrharvest_set_folder_size( harvester, sz );
213 #endif
214
215 #ifndef USE_NEW_ADDRBOOK
216         /* Create address book */
217         abf = addrbook_create_book();
218         addrbook_set_path( abf, _harv_addressIndex_->filePath );
219         newFile = addrbook_guess_next_file( abf );
220         addrbook_set_file( abf, newFile );
221         addrbook_set_name( abf, name );
222         g_free( newFile );
223         g_free( name );
224 #endif
225
226         /* Harvest addresses */
227 #ifndef USE_NEW_ADDRBOOK
228         addrharvest_harvest(
229                 harvester, abf->addressCache, _harv_messageList_ );
230         /* save address book */
231         addrbook_save_data( abf );
232         _harv_addressBook_ = abf;
233 #else
234         addrharvest_harvest(
235                 harvester, NULL, _harv_messageList_ );
236         list = g_hash_table_get_values(harvester->dupTable);
237         for (; list; list = g_list_next(list)) {
238                 contact = g_new0(ContactData, 1);
239                 ContactEntry* person = (ContactEntry *) list->data;
240                 if (person->first_name)
241                         contact->name = g_strconcat(
242                                         person->first_name, " ", person->last_name, NULL);
243                 else
244                         contact->name = g_strdup(person->last_name);
245                 if (! contact->name || strlen(contact->name) < 1)
246                         contact->name = g_strdup(person->email);
247                 contact->remarks = g_strdup(N_("address added by claws-mail"));
248                 contact->email = g_strdup(person->email);
249                 contact->book = g_strdup(name);
250                 addressbook_dbus_add_contact(contact, &error);
251                 contact_data_free(&contact);
252         }
253         g_list_free(list);
254         g_free(name);
255 #endif
256
257         /* Update summary count */
258         clist = GTK_CMCLIST(addrgather_dlg.clistCount);
259         gtk_cmclist_clear( clist );
260         for( i = 0; i < NUM_FIELDS; i++ ) {
261                 cnt = addrharvest_get_count( harvester, _harv_headerNames_[i] );
262                 if( cnt < 1 ) {
263                         strcpy( str, "-" );
264                 }
265                 else {
266                         sprintf( str, "%d", cnt );
267                 }
268                 text[ FIELD_COL_HEADER ] = _harv_headerNames_[i];
269                 text[ FIELD_COL_COUNT  ] = str;
270                 gtk_cmclist_append( clist, text );
271         }
272
273         addrharvest_free( harvester );
274
275         addrgather_dlg_status_show(_("Addresses collected successfully."));
276
277         /* Display summary page */
278         gtk_notebook_set_current_page(
279                 GTK_NOTEBOOK(addrgather_dlg.notebook), PAGE_FINISH );
280         addrgather_dlg.done = TRUE;
281         gtk_widget_set_sensitive( addrgather_dlg.btnCancel, FALSE );
282         gtk_widget_grab_default( addrgather_dlg.btnOk );
283
284         return TRUE;
285 }
286
287 static void addrgather_dlg_ok( GtkWidget *widget, gpointer data ) {
288         if(addrgather_dlg.done) {
289                 addrgather_dlg.done = FALSE;
290                 gtk_main_quit();
291                 return;
292         }
293         if( addrgather_dlg_harvest() ) {
294                 addrgather_dlg.cancelled = FALSE;
295         }
296 }
297
298 static void addrgather_dlg_cancel( GtkWidget *widget, gpointer data ) {
299         gtk_main_quit();
300 }
301
302 #define PACK_CHECK_BUTTON(box, checkbtn, label) \
303 { \
304         checkbtn = gtk_check_button_new_with_label(label); \
305         gtk_widget_show(checkbtn); \
306         gtk_box_pack_start(GTK_BOX(box), checkbtn, FALSE, TRUE, 0); \
307 }
308
309 /*
310  * Create notebook page for mail headers.
311  * Enter: pageNum Page number.
312  *        pageLbl Page label.
313  */
314 static void addrgather_page_fields(gint pageNum, gchar *pageLbl)
315 {
316         GtkWidget *vbox;
317         GtkWidget *vboxf;
318         GtkWidget *table;
319         GtkWidget *label;
320         GtkWidget *labelFolder;
321         GtkWidget *entryBook;
322         GtkWidget *frameHeader;
323         GtkWidget *checkHeader[NUM_FIELDS];
324 #ifndef USE_NEW_ADDRBOOK
325         GtkWidget *hboxs;
326         GtkWidget *spinbtnFolder;
327         GtkAdjustment *adjFolder;
328 #endif
329         GtkWidget *checkRecurse;
330         gint top;
331         gint i;
332 #ifdef USE_NEW_ADDRBOOK
333         GError* error = NULL;
334         GSList *books, *cur;
335 #endif
336
337         /* Container */
338         vbox = gtk_vbox_new(FALSE, 6);
339         gtk_container_add(GTK_CONTAINER(addrgather_dlg.notebook), vbox);
340         gtk_container_set_border_width(GTK_CONTAINER(vbox), 4);
341
342         /* Notebook page */
343         label = gtk_label_new(pageLbl);
344         gtk_widget_show(label);
345         gtk_notebook_set_tab_label(GTK_NOTEBOOK(addrgather_dlg.notebook),
346                                    gtk_notebook_get_nth_page(GTK_NOTEBOOK(addrgather_dlg.notebook),
347                                                              pageNum), label);
348
349         /* Upper area - Field list */
350         table = gtk_table_new(4, 2, FALSE);
351         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
352         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
353         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
354
355         /* First row */
356         top = 0;
357         label = gtk_label_new(_("Current folder:"));
358         gtk_table_attach( GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0 );
359         gtk_misc_set_alignment( GTK_MISC(label), 1.0, 0.5 );
360
361         labelFolder = gtk_label_new("");
362         gtk_table_attach( GTK_TABLE(table), labelFolder, 1, 2, top, (top + 1),
363                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0 );
364         gtk_misc_set_alignment( GTK_MISC(labelFolder), 0, 0.5 );
365
366         /* Second row */
367         top = 1;
368         label = gtk_label_new(_("Address book name:"));
369         gtk_table_attach( GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0 );
370         gtk_misc_set_alignment( GTK_MISC(label), 1.0, 0.5 );
371
372 #ifndef USE_NEW_ADDRBOOK
373         entryBook = gtk_entry_new();
374 #else
375         books = addressbook_dbus_get_books(&error);
376         entryBook = gtk_combo_box_new_text();
377         if (books) {
378             for (cur = books; cur; cur = g_slist_next(cur)) {
379                 gchar* book = (gchar *) cur->data;
380                 gtk_combo_box_prepend_text(GTK_COMBO_BOX(entryBook), book);
381                 g_free(book);
382             }
383             g_slist_free(books);
384         }
385         else
386             gtk_combo_box_prepend_text(GTK_COMBO_BOX(entryBook), "");
387         gtk_combo_box_set_active(GTK_COMBO_BOX(entryBook), 0);
388 #endif
389         gtk_table_attach( GTK_TABLE(table), entryBook, 1, 2, top, (top + 1),
390                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0 );
391
392 #ifndef USE_NEW_ADDRBOOK
393         /* Third row */
394         top = 2;
395         label = gtk_label_new(_("Address book folder size:"));
396         gtk_table_attach( GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0 );
397         gtk_misc_set_alignment( GTK_MISC(label), 1.0, 0.5 );
398         CLAWS_SET_TIP(label,
399                         _("Maximum amount of entries per folder within the newly created address book"));
400
401         hboxs = gtk_hbox_new(FALSE, 8);
402         adjFolder = GTK_ADJUSTMENT(gtk_adjustment_new(DFL_FOLDER_SIZE, MIN_FOLDER_SIZE, G_MAXINT, 1, 10, 0));
403         spinbtnFolder = gtk_spin_button_new(GTK_ADJUSTMENT(adjFolder), 1, 0);
404         gtk_box_pack_start(GTK_BOX(hboxs), spinbtnFolder, FALSE, FALSE, 0);
405         gtk_widget_set_size_request(spinbtnFolder, 100, -1);
406         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(spinbtnFolder), TRUE);
407         gtk_table_attach(GTK_TABLE(table), hboxs, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
408         CLAWS_SET_TIP(spinbtnFolder,
409                         _("Maximum amount of entries per folder within the newly created address book"));
410 #endif
411         /* Fourth row */
412         top = 3;
413         frameHeader = gtk_frame_new(_("Process these mail header fields"));
414         gtk_widget_show(frameHeader);
415         gtk_table_attach(GTK_TABLE(table), frameHeader, 0, 2, top, (top + 4), GTK_FILL, 0, 0, 0);
416         gtk_frame_set_label_align(GTK_FRAME(frameHeader), 0.01, 0.5);
417
418         /* Check boxes */
419         vboxf = gtk_vbox_new(FALSE, 0);
420         gtk_widget_show(vboxf);
421         gtk_container_add(GTK_CONTAINER(frameHeader), vboxf);
422         gtk_container_set_border_width(GTK_CONTAINER(vboxf), 8);
423
424         for (i = 0; i < NUM_FIELDS; i++) {
425                 PACK_CHECK_BUTTON(vboxf, checkHeader[i],
426                         prefs_common_translated_header_name(_harv_headerNames_[i]));
427                 addrgather_dlg.checkHeader[i] = checkHeader[i];
428         }
429
430         /* Recurse folders */
431         top += 4;
432         checkRecurse = gtk_check_button_new_with_label( _("Include subfolders" ) );
433         gtk_table_attach( GTK_TABLE(table), checkRecurse, 0, 2, top, (top + 1),
434                         GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0 );
435
436         addrgather_dlg.labelFolder   = labelFolder;
437         addrgather_dlg.entryBook     = entryBook;
438 #ifndef USE_NEW_ADDRBOOK
439         addrgather_dlg.spinbtnFolder = spinbtnFolder;
440 #endif
441         addrgather_dlg.checkRecurse  = checkRecurse;
442 }
443
444 /*
445  * Create notebook page for summary counts.
446  * Enter: pageNum Page number.
447  *        pageLbl Page label.
448  */
449 static void addrgather_page_finish( gint pageNum, gchar *pageLbl ) {
450         GtkWidget *label;
451         GtkWidget *vbox;
452         GtkWidget *clistSWin;
453         GtkWidget *clistCount;
454         gchar *titles[ FIELDS_N_COLS ];
455         gint i;
456         titles[ FIELD_COL_HEADER ] = _("Header Name");
457         titles[ FIELD_COL_COUNT  ] = _("Address Count");
458
459         vbox = gtk_vbox_new(FALSE, 8);
460         gtk_container_add( GTK_CONTAINER( addrgather_dlg.notebook ), vbox );
461         gtk_container_set_border_width( GTK_CONTAINER (vbox), 8 );
462
463         label = gtk_label_new( pageLbl );
464         gtk_widget_show( label );
465         gtk_notebook_set_tab_label(
466                 GTK_NOTEBOOK( addrgather_dlg.notebook ),
467                 gtk_notebook_get_nth_page( GTK_NOTEBOOK( addrgather_dlg.notebook ), pageNum ),
468                 label );
469
470         /* Summary count */
471         clistSWin = gtk_scrolled_window_new( NULL, NULL );
472         gtk_container_add( GTK_CONTAINER(vbox), clistSWin );
473         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clistSWin),
474                                        GTK_POLICY_AUTOMATIC,
475                                        GTK_POLICY_AUTOMATIC);
476
477         clistCount = gtk_cmclist_new_with_titles( FIELDS_N_COLS, titles );
478         gtk_container_add( GTK_CONTAINER(clistSWin), clistCount );
479         gtk_cmclist_set_selection_mode( GTK_CMCLIST(clistCount), GTK_SELECTION_BROWSE );
480         gtk_cmclist_set_column_width(
481                         GTK_CMCLIST(clistCount), FIELD_COL_HEADER, FIELDS_COL_WIDTH_HEADER );
482         gtk_cmclist_set_column_width(
483                         GTK_CMCLIST(clistCount), FIELD_COL_COUNT, FIELDS_COL_WIDTH_COUNT );
484
485         for( i = 0; i < FIELDS_N_COLS; i++ )
486                 gtkut_widget_set_can_focus(GTK_CMCLIST(clistCount)->column[i].button, FALSE);
487
488         addrgather_dlg.clistCount = clistCount;
489 }
490
491 /*
492  * Create notebook page for warning message.
493  * Enter: pageNum Page number.
494  *        pageLbl Page label.
495  */
496 static void addrgather_dlg_create(void)
497 {
498         GtkWidget *window;
499         GtkWidget *notebook;
500         GtkWidget *btnOk;
501         GtkWidget *btnCancel;
502         GtkWidget *statusbar;
503         GtkWidget *vbox;
504         GtkWidget *hbbox;
505         GtkWidget *hsbox;
506         static GdkGeometry geometry;
507         
508         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "addrgather");
509         gtk_container_set_border_width(GTK_CONTAINER(window), 4);
510         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
511         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
512         
513         g_signal_connect(G_OBJECT(window), "delete_event",
514                          G_CALLBACK(addrgather_dlg_delete_event), NULL);
515         g_signal_connect(G_OBJECT(window), "key_press_event",
516                          G_CALLBACK(addrgather_dlg_key_pressed), NULL);
517         g_signal_connect(G_OBJECT(window), "size_allocate",
518                          G_CALLBACK(addrgather_size_allocate), NULL);
519
520         vbox = gtk_vbox_new(FALSE, 6);
521         gtk_container_add(GTK_CONTAINER(window), vbox);
522
523         /* Notebook */
524         notebook = gtk_notebook_new();
525         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(notebook), FALSE);
526         gtk_widget_show(notebook);
527         gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 0);
528         gtk_container_set_border_width(GTK_CONTAINER(notebook), 6);
529
530         /* Status line */
531         hsbox = gtk_hbox_new(FALSE, 0);
532         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, 0);
533         statusbar = gtk_statusbar_new();
534         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, 0);
535
536         /* Button panel */
537         gtkut_stock_button_set_create(&hbbox, &btnCancel, GTK_STOCK_CANCEL,
538                                       &btnOk, GTK_STOCK_OK,
539                                       NULL, NULL);
540         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
541
542         /* Signal handlers */
543         g_signal_connect(G_OBJECT(btnOk), "clicked",
544                          G_CALLBACK(addrgather_dlg_ok), NULL);
545         g_signal_connect(G_OBJECT(btnCancel), "clicked",
546                          G_CALLBACK(addrgather_dlg_cancel), NULL);
547
548         if (!geometry.min_width) {
549                 geometry.min_width = 450;
550                 geometry.min_height = -1;
551         }
552
553         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
554                                       GDK_HINT_MIN_SIZE);
555         gtk_window_set_default_size(GTK_WINDOW(window), prefs_common.addrgather_width,
556                                     prefs_common.addrgather_height);
557
558         addrgather_dlg.window     = window;
559         addrgather_dlg.notebook   = notebook;
560         addrgather_dlg.btnOk      = btnOk;
561         addrgather_dlg.btnCancel  = btnCancel;
562         addrgather_dlg.statusbar  = statusbar;
563         addrgather_dlg.status_cid = gtk_statusbar_get_context_id(GTK_STATUSBAR(statusbar),
564                                                                  "Collect Email Address Dialog");
565
566         /* Create notebook pages */
567         addrgather_page_fields(PAGE_FIELDS, _("Header Fields"));
568         addrgather_page_finish(PAGE_FINISH, _("Finish"));
569         gtk_widget_show_all(addrgather_dlg.window);
570 }
571
572 /*
573  * Harvest addresses main window.
574  * Enter: folderItem Source folder.
575  *        addrIndex  Address index.
576  *        sourceInd  Source indicator: FALSE - Folder, TRUE - Messages.
577  *        msgList    List of message numbers, or NULL to process folder.
578  * Return: Populated address book file, or NULL if none created.
579  */
580 #ifndef USE_NEW_ADDRBOOK
581 AddressBookFile *addrgather_dlg_execute(FolderItem *folderItem, AddressIndex *addrIndex,
582                                         gboolean sourceInd, GList *msgList)
583 #else
584 void addrgather_dlg_execute(FolderItem *folderItem, gboolean sourceInd, GList *msgList)
585 #endif
586 {
587         gint i;
588
589 #ifndef USE_NEW_ADDRBOOK
590         _harv_addressIndex_ = addrIndex;
591         _harv_addressBook_ = NULL;
592 #endif
593         _harv_messageList_ = msgList;
594
595         /* Create dialog */
596         if (!addrgather_dlg.window)
597                 addrgather_dlg_create();
598         
599         addrgather_dlg.done = FALSE;
600
601         gtk_notebook_set_current_page(GTK_NOTEBOOK(addrgather_dlg.notebook), PAGE_FIELDS);
602         addrgather_dlg.folderPath = folder_item_get_path(folderItem);
603
604         /* Setup some default values */
605
606         gtk_label_set_text(GTK_LABEL(addrgather_dlg.labelFolder), folderItem->path);
607 #ifndef USE_NEW_ADDRBOOK
608         gtk_entry_set_text(GTK_ENTRY(addrgather_dlg.entryBook), folderItem->path);
609 #endif
610
611         for (i = 0; i < NUM_FIELDS; i++) {
612                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(addrgather_dlg.checkHeader[i]),
613                                              FALSE);
614                 if (g_utf8_collate(_harv_headerNames_[i], HEADER_FROM) == 0)
615                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(addrgather_dlg.checkHeader[i]),
616                                                     TRUE);
617         }
618
619         gtk_widget_set_sensitive(addrgather_dlg.btnOk, TRUE);
620         gtk_widget_set_sensitive(addrgather_dlg.btnCancel, TRUE);
621         gtk_widget_grab_default(addrgather_dlg.btnOk);
622
623         /* Apply window title */
624         if (sourceInd) {
625                 gtk_window_set_title(GTK_WINDOW(addrgather_dlg.window),
626                                      _("Collect email addresses from selected messages"));
627                 gtk_widget_set_sensitive(addrgather_dlg.checkRecurse, FALSE);
628         } else {
629                 gtk_window_set_title(GTK_WINDOW(addrgather_dlg.window),
630                                      _("Collect email addresses from folder"));
631                 gtk_widget_set_sensitive(addrgather_dlg.checkRecurse, TRUE);
632         }
633         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(addrgather_dlg.checkRecurse), FALSE);
634
635         addrgather_dlg_status_show("");
636         gtk_widget_show(addrgather_dlg.window);
637         gtk_window_set_modal(GTK_WINDOW(addrgather_dlg.window), TRUE);
638         gtk_widget_grab_focus(addrgather_dlg.entryBook);
639         manage_window_set_transient(GTK_WINDOW(addrgather_dlg.window));
640         gtk_main();
641
642         g_free(addrgather_dlg.folderPath);
643         addrgather_dlg.folderPath = NULL;
644         gtk_widget_hide(addrgather_dlg.window);
645         gtk_window_set_modal(GTK_WINDOW(addrgather_dlg.window), FALSE);
646 #ifndef USE_NEW_ADDRBOOK
647         _harv_addressIndex_ = NULL;
648
649         if (addrgather_dlg.cancelled == TRUE)
650                 return NULL;
651
652         return _harv_addressBook_;
653 #endif
654 }
655 /*
656 * End of Source.
657 */
658
659