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