2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2012 Match Grun and the Claws Mail team
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.
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.
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/>.
21 * Dialog for gathering EMail addresses from mail folder.
26 #include "claws-features.h"
32 #include <glib/gi18n.h>
33 #include <gdk/gdkkeysyms.h>
40 #include "foldersel.h"
42 #include "alertpanel.h"
43 #include "manage_window.h"
46 #include "prefs_common.h"
47 #include "prefs_gtk.h"
49 #include "addrharvest.h"
50 #ifndef USE_ALT_ADDRBOOK
51 #include "addrindex.h"
54 #include "addressbook-dbus.h"
61 #define FIELDS_N_COLS 2
62 #define FIELDS_COL_WIDTH_HEADER 100
63 #define FIELDS_COL_WIDTH_COUNT 140
65 #define MIN_FOLDER_SIZE 20
66 #define DFL_FOLDER_SIZE 50
76 static struct _AddrHarvest
{
79 GtkWidget
*labelFolder
;
81 GtkWidget
*checkHeader
[ NUM_FIELDS
];
82 #ifndef USE_ALT_ADDRBOOK
83 GtkWidget
*spinbtnFolder
;
85 GtkWidget
*checkRecurse
;
93 GtkWidget
*clistCount
;
96 #ifndef USE_ALT_ADDRBOOK
97 static AddressIndex
*_harv_addressIndex_
;
98 static AddressBookFile
*_harv_addressBook_
;
100 static gchar
*_harv_headerNames_
[] = {
108 static GList
*_harv_messageList_
;
110 static void addrgather_dlg_status_show( gchar
*msg
) {
111 if( addrgather_dlg
.statusbar
!= NULL
) {
112 gtk_statusbar_pop( GTK_STATUSBAR(addrgather_dlg
.statusbar
),
113 addrgather_dlg
.status_cid
);
116 GTK_STATUSBAR(addrgather_dlg
.statusbar
),
117 addrgather_dlg
.status_cid
, msg
);
122 static gint
addrgather_dlg_delete_event(
123 GtkWidget
*widget
, GdkEventAny
*event
, gpointer data
)
125 addrgather_dlg
.cancelled
= TRUE
;
130 static gboolean
addrgather_dlg_key_pressed(
131 GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
133 if( event
&& event
->keyval
== GDK_KEY_Escape
) {
134 addrgather_dlg
.cancelled
= TRUE
;
140 static void addrgather_size_allocate(
141 GtkWidget
*widget
, GtkAllocation
*allocation
)
143 cm_return_if_fail( allocation
!= NULL
);
145 prefs_common
.addrgather_width
= allocation
->width
;
146 prefs_common
.addrgather_height
= allocation
->height
;
149 #define FMT_BUFSIZE 32
151 static gboolean
addrgather_dlg_harvest() {
153 gchar
*text
[ FIELDS_N_COLS
];
154 AddressHarvester
*harvester
;
156 #ifndef USE_ALT_ADDRBOOK
157 AddressBookFile
*abf
;
161 ContactData
* contact
;
162 GError
* error
= NULL
;
164 gchar str
[ FMT_BUFSIZE
];
167 #ifndef USE_ALT_ADDRBOOK
170 name
= gtk_editable_get_chars( GTK_EDITABLE(addrgather_dlg
.entryBook
), 0, -1 );
171 if( name
== NULL
|| strlen( name
) < 1 ) {
172 addrgather_dlg_status_show(
173 _( "Please specify name for address book." ) );
177 name
= gtk_combo_box_get_active_text(GTK_COMBO_BOX(addrgather_dlg
.entryBook
));
178 if( name
== NULL
|| strlen( name
) < 1 ) {
179 addrgather_dlg_status_show(
180 _("No available address book."));
186 /* Create harvest helper */
187 harvester
= addrharvest_create();
188 addrharvest_set_path( harvester
, addrgather_dlg
.folderPath
);
190 for( i
= 0; i
< NUM_FIELDS
; i
++ ) {
191 addrharvest_set_header( harvester
, _harv_headerNames_
[i
],
192 gtk_toggle_button_get_active(
193 GTK_TOGGLE_BUTTON(addrgather_dlg
.checkHeader
[i
]) ) );
195 addrharvest_set_recurse( harvester
,
196 gtk_toggle_button_get_active(
197 GTK_TOGGLE_BUTTON( addrgather_dlg
.checkRecurse
) ) );
199 if( addrharvest_check_header( harvester
) == FALSE
) {
200 addrgather_dlg_status_show(
201 _( "Please select the mail headers to search." ) );
202 addrharvest_free( harvester
);
208 addrgather_dlg_status_show( _( "Collecting addresses..." ) );
210 #ifndef USE_ALT_ADDRBOOK
211 sz
= gtk_spin_button_get_value_as_int(
212 GTK_SPIN_BUTTON( addrgather_dlg
.spinbtnFolder
) );
213 addrharvest_set_folder_size( harvester
, sz
);
216 #ifndef USE_ALT_ADDRBOOK
217 /* Create address book */
218 abf
= addrbook_create_book();
219 addrbook_set_path( abf
, _harv_addressIndex_
->filePath
);
220 newFile
= addrbook_guess_next_file( abf
);
221 addrbook_set_file( abf
, newFile
);
222 addrbook_set_name( abf
, name
);
227 /* Harvest addresses */
228 #ifndef USE_ALT_ADDRBOOK
230 harvester
, abf
->addressCache
, _harv_messageList_
);
231 /* save address book */
232 addrbook_save_data( abf
);
233 _harv_addressBook_
= abf
;
236 harvester
, NULL
, _harv_messageList_
);
237 list
= g_hash_table_get_values(harvester
->dupTable
);
238 for (; list
; list
= g_list_next(list
)) {
239 contact
= g_new0(ContactData
, 1);
240 ContactEntry
* person
= (ContactEntry
*) list
->data
;
241 if (person
->first_name
)
242 contact
->name
= g_strconcat(
243 person
->first_name
, " ", person
->last_name
, NULL
);
245 contact
->name
= g_strdup(person
->last_name
);
246 if (! contact
->name
|| strlen(contact
->name
) < 1)
247 contact
->name
= g_strdup(person
->email
);
248 contact
->remarks
= g_strdup(N_("address added by claws-mail"));
249 contact
->email
= g_strdup(person
->email
);
250 contact
->book
= g_strdup(name
);
251 addressbook_dbus_add_contact(contact
, &error
);
252 contact_data_free(&contact
);
258 /* Update summary count */
259 clist
= GTK_CMCLIST(addrgather_dlg
.clistCount
);
260 gtk_cmclist_clear( clist
);
261 for( i
= 0; i
< NUM_FIELDS
; i
++ ) {
262 cnt
= addrharvest_get_count( harvester
, _harv_headerNames_
[i
] );
267 sprintf( str
, "%d", cnt
);
269 text
[ FIELD_COL_HEADER
] = _harv_headerNames_
[i
];
270 text
[ FIELD_COL_COUNT
] = str
;
271 gtk_cmclist_append( clist
, text
);
274 addrharvest_free( harvester
);
276 addrgather_dlg_status_show(_("Addresses collected successfully."));
278 /* Display summary page */
279 gtk_notebook_set_current_page(
280 GTK_NOTEBOOK(addrgather_dlg
.notebook
), PAGE_FINISH
);
281 addrgather_dlg
.done
= TRUE
;
282 gtk_widget_set_sensitive( addrgather_dlg
.btnCancel
, FALSE
);
283 gtk_widget_grab_default( addrgather_dlg
.btnOk
);
288 static void addrgather_dlg_ok( GtkWidget
*widget
, gpointer data
) {
289 if(addrgather_dlg
.done
) {
290 addrgather_dlg
.done
= FALSE
;
294 if( addrgather_dlg_harvest() ) {
295 addrgather_dlg
.cancelled
= FALSE
;
299 static void addrgather_dlg_cancel( GtkWidget
*widget
, gpointer data
) {
304 * Create notebook page for mail headers.
305 * Enter: pageNum Page number.
306 * pageLbl Page label.
308 static void addrgather_page_fields(gint pageNum
, gchar
*pageLbl
)
314 GtkWidget
*labelFolder
;
315 GtkWidget
*entryBook
;
316 GtkWidget
*frameHeader
;
317 GtkWidget
*checkHeader
[NUM_FIELDS
];
318 #ifndef USE_ALT_ADDRBOOK
320 GtkWidget
*spinbtnFolder
;
321 GtkAdjustment
*adjFolder
;
323 GtkWidget
*checkRecurse
;
326 #ifdef USE_ALT_ADDRBOOK
327 GError
* error
= NULL
;
332 vbox
= gtk_vbox_new(FALSE
, 6);
333 gtk_container_add(GTK_CONTAINER(addrgather_dlg
.notebook
), vbox
);
334 gtk_container_set_border_width(GTK_CONTAINER(vbox
), 4);
337 label
= gtk_label_new(pageLbl
);
338 gtk_widget_show(label
);
339 gtk_notebook_set_tab_label(GTK_NOTEBOOK(addrgather_dlg
.notebook
),
340 gtk_notebook_get_nth_page(GTK_NOTEBOOK(addrgather_dlg
.notebook
),
343 /* Upper area - Field list */
344 table
= gtk_table_new(4, 2, FALSE
);
345 gtk_box_pack_start(GTK_BOX(vbox
), table
, FALSE
, FALSE
, 0);
346 gtk_table_set_row_spacings(GTK_TABLE(table
), 4);
347 gtk_table_set_col_spacings(GTK_TABLE(table
), 4);
351 label
= gtk_label_new(_("Current folder:"));
352 gtk_table_attach( GTK_TABLE(table
), label
, 0, 1, top
, (top
+ 1), GTK_FILL
, 0, 0, 0 );
353 gtk_misc_set_alignment( GTK_MISC(label
), 1.0, 0.5 );
355 labelFolder
= gtk_label_new("");
356 gtk_table_attach( GTK_TABLE(table
), labelFolder
, 1, 2, top
, (top
+ 1),
357 GTK_EXPAND
|GTK_SHRINK
|GTK_FILL
, 0, 0, 0 );
358 gtk_misc_set_alignment( GTK_MISC(labelFolder
), 0, 0.5 );
362 label
= gtk_label_new(_("Address book name:"));
363 gtk_table_attach( GTK_TABLE(table
), label
, 0, 1, top
, (top
+ 1), GTK_FILL
, 0, 0, 0 );
364 gtk_misc_set_alignment( GTK_MISC(label
), 1.0, 0.5 );
366 #ifndef USE_ALT_ADDRBOOK
367 entryBook
= gtk_entry_new();
369 books
= addressbook_dbus_get_books(&error
);
370 entryBook
= gtk_combo_box_new_text();
372 for (cur
= books
; cur
; cur
= g_slist_next(cur
)) {
373 gchar
* book
= (gchar
*) cur
->data
;
374 gtk_combo_box_prepend_text(GTK_COMBO_BOX(entryBook
), book
);
380 gtk_combo_box_prepend_text(GTK_COMBO_BOX(entryBook
), "");
381 gtk_combo_box_set_active(GTK_COMBO_BOX(entryBook
), 0);
383 gtk_table_attach( GTK_TABLE(table
), entryBook
, 1, 2, top
, (top
+ 1),
384 GTK_EXPAND
|GTK_SHRINK
|GTK_FILL
, 0, 0, 0 );
386 #ifndef USE_ALT_ADDRBOOK
389 label
= gtk_label_new(_("Address book folder size:"));
390 gtk_table_attach( GTK_TABLE(table
), label
, 0, 1, top
, (top
+ 1), GTK_FILL
, 0, 0, 0 );
391 gtk_misc_set_alignment( GTK_MISC(label
), 1.0, 0.5 );
393 _("Maximum amount of entries per folder within the newly created address book"));
395 hboxs
= gtk_hbox_new(FALSE
, 8);
396 adjFolder
= GTK_ADJUSTMENT(gtk_adjustment_new(DFL_FOLDER_SIZE
, MIN_FOLDER_SIZE
, G_MAXINT
, 1, 10, 0));
397 spinbtnFolder
= gtk_spin_button_new(GTK_ADJUSTMENT(adjFolder
), 1, 0);
398 gtk_box_pack_start(GTK_BOX(hboxs
), spinbtnFolder
, FALSE
, FALSE
, 0);
399 gtk_widget_set_size_request(spinbtnFolder
, 100, -1);
400 gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(spinbtnFolder
), TRUE
);
401 gtk_table_attach(GTK_TABLE(table
), hboxs
, 1, 2, top
, (top
+ 1), GTK_FILL
, 0, 0, 0);
402 CLAWS_SET_TIP(spinbtnFolder
,
403 _("Maximum amount of entries per folder within the newly created address book"));
407 frameHeader
= gtk_frame_new(_("Process these mail header fields"));
408 gtk_widget_show(frameHeader
);
409 gtk_table_attach(GTK_TABLE(table
), frameHeader
, 0, 2, top
, (top
+ 4), GTK_FILL
, 0, 0, 0);
410 gtk_frame_set_label_align(GTK_FRAME(frameHeader
), 0.01, 0.5);
413 vboxf
= gtk_vbox_new(FALSE
, 0);
414 gtk_widget_show(vboxf
);
415 gtk_container_add(GTK_CONTAINER(frameHeader
), vboxf
);
416 gtk_container_set_border_width(GTK_CONTAINER(vboxf
), 8);
418 for (i
= 0; i
< NUM_FIELDS
; i
++) {
419 PACK_CHECK_BUTTON(vboxf
, checkHeader
[i
],
420 prefs_common_translated_header_name(_harv_headerNames_
[i
]));
421 addrgather_dlg
.checkHeader
[i
] = checkHeader
[i
];
424 /* Recurse folders */
426 checkRecurse
= gtk_check_button_new_with_label( _("Include subfolders" ) );
427 gtk_table_attach( GTK_TABLE(table
), checkRecurse
, 0, 2, top
, (top
+ 1),
428 GTK_EXPAND
|GTK_SHRINK
|GTK_FILL
, 0, 0, 0 );
430 addrgather_dlg
.labelFolder
= labelFolder
;
431 addrgather_dlg
.entryBook
= entryBook
;
432 #ifndef USE_ALT_ADDRBOOK
433 addrgather_dlg
.spinbtnFolder
= spinbtnFolder
;
435 addrgather_dlg
.checkRecurse
= checkRecurse
;
439 * Create notebook page for summary counts.
440 * Enter: pageNum Page number.
441 * pageLbl Page label.
443 static void addrgather_page_finish( gint pageNum
, gchar
*pageLbl
) {
446 GtkWidget
*clistSWin
;
447 GtkWidget
*clistCount
;
448 gchar
*titles
[ FIELDS_N_COLS
];
450 titles
[ FIELD_COL_HEADER
] = _("Header Name");
451 titles
[ FIELD_COL_COUNT
] = _("Address Count");
453 vbox
= gtk_vbox_new(FALSE
, 8);
454 gtk_container_add( GTK_CONTAINER( addrgather_dlg
.notebook
), vbox
);
455 gtk_container_set_border_width( GTK_CONTAINER (vbox
), 8 );
457 label
= gtk_label_new( pageLbl
);
458 gtk_widget_show( label
);
459 gtk_notebook_set_tab_label(
460 GTK_NOTEBOOK( addrgather_dlg
.notebook
),
461 gtk_notebook_get_nth_page( GTK_NOTEBOOK( addrgather_dlg
.notebook
), pageNum
),
465 clistSWin
= gtk_scrolled_window_new( NULL
, NULL
);
466 gtk_container_add( GTK_CONTAINER(vbox
), clistSWin
);
467 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clistSWin
),
468 GTK_POLICY_AUTOMATIC
,
469 GTK_POLICY_AUTOMATIC
);
471 clistCount
= gtk_cmclist_new_with_titles( FIELDS_N_COLS
, titles
);
472 gtk_container_add( GTK_CONTAINER(clistSWin
), clistCount
);
473 gtk_cmclist_set_selection_mode( GTK_CMCLIST(clistCount
), GTK_SELECTION_BROWSE
);
474 gtk_cmclist_set_column_width(
475 GTK_CMCLIST(clistCount
), FIELD_COL_HEADER
, FIELDS_COL_WIDTH_HEADER
);
476 gtk_cmclist_set_column_width(
477 GTK_CMCLIST(clistCount
), FIELD_COL_COUNT
, FIELDS_COL_WIDTH_COUNT
);
479 for( i
= 0; i
< FIELDS_N_COLS
; i
++ )
480 gtkut_widget_set_can_focus(GTK_CMCLIST(clistCount
)->column
[i
].button
, FALSE
);
482 addrgather_dlg
.clistCount
= clistCount
;
486 * Create notebook page for warning message.
487 * Enter: pageNum Page number.
488 * pageLbl Page label.
490 static void addrgather_dlg_create(void)
495 GtkWidget
*btnCancel
;
496 GtkWidget
*statusbar
;
500 static GdkGeometry geometry
;
502 window
= gtkut_window_new(GTK_WINDOW_TOPLEVEL
, "addrgather");
503 gtk_container_set_border_width(GTK_CONTAINER(window
), 4);
504 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
505 gtk_window_set_resizable(GTK_WINDOW(window
), TRUE
);
507 g_signal_connect(G_OBJECT(window
), "delete_event",
508 G_CALLBACK(addrgather_dlg_delete_event
), NULL
);
509 g_signal_connect(G_OBJECT(window
), "key_press_event",
510 G_CALLBACK(addrgather_dlg_key_pressed
), NULL
);
511 g_signal_connect(G_OBJECT(window
), "size_allocate",
512 G_CALLBACK(addrgather_size_allocate
), NULL
);
514 vbox
= gtk_vbox_new(FALSE
, 6);
515 gtk_container_add(GTK_CONTAINER(window
), vbox
);
518 notebook
= gtk_notebook_new();
519 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(notebook
), FALSE
);
520 gtk_widget_show(notebook
);
521 gtk_box_pack_start(GTK_BOX(vbox
), notebook
, TRUE
, TRUE
, 0);
522 gtk_container_set_border_width(GTK_CONTAINER(notebook
), 6);
525 hsbox
= gtk_hbox_new(FALSE
, 0);
526 gtk_box_pack_end(GTK_BOX(vbox
), hsbox
, FALSE
, FALSE
, 0);
527 statusbar
= gtk_statusbar_new();
528 gtk_box_pack_start(GTK_BOX(hsbox
), statusbar
, TRUE
, TRUE
, 0);
531 gtkut_stock_button_set_create(&hbbox
, &btnCancel
, GTK_STOCK_CANCEL
,
532 &btnOk
, GTK_STOCK_OK
,
534 gtk_box_pack_end(GTK_BOX(vbox
), hbbox
, FALSE
, FALSE
, 0);
536 /* Signal handlers */
537 g_signal_connect(G_OBJECT(btnOk
), "clicked",
538 G_CALLBACK(addrgather_dlg_ok
), NULL
);
539 g_signal_connect(G_OBJECT(btnCancel
), "clicked",
540 G_CALLBACK(addrgather_dlg_cancel
), NULL
);
542 if (!geometry
.min_width
) {
543 geometry
.min_width
= 450;
544 geometry
.min_height
= -1;
547 gtk_window_set_geometry_hints(GTK_WINDOW(window
), NULL
, &geometry
,
549 gtk_window_set_default_size(GTK_WINDOW(window
), prefs_common
.addrgather_width
,
550 prefs_common
.addrgather_height
);
552 addrgather_dlg
.window
= window
;
553 addrgather_dlg
.notebook
= notebook
;
554 addrgather_dlg
.btnOk
= btnOk
;
555 addrgather_dlg
.btnCancel
= btnCancel
;
556 addrgather_dlg
.statusbar
= statusbar
;
557 addrgather_dlg
.status_cid
= gtk_statusbar_get_context_id(GTK_STATUSBAR(statusbar
),
558 "Collect Email Address Dialog");
560 /* Create notebook pages */
561 addrgather_page_fields(PAGE_FIELDS
, _("Header Fields"));
562 addrgather_page_finish(PAGE_FINISH
, _("Finish"));
563 gtk_widget_show_all(addrgather_dlg
.window
);
567 * Harvest addresses main window.
568 * Enter: folderItem Source folder.
569 * addrIndex Address index.
570 * sourceInd Source indicator: FALSE - Folder, TRUE - Messages.
571 * msgList List of message numbers, or NULL to process folder.
572 * Return: Populated address book file, or NULL if none created.
574 #ifndef USE_ALT_ADDRBOOK
575 AddressBookFile
*addrgather_dlg_execute(FolderItem
*folderItem
, AddressIndex
*addrIndex
,
576 gboolean sourceInd
, GList
*msgList
)
578 void addrgather_dlg_execute(FolderItem
*folderItem
, gboolean sourceInd
, GList
*msgList
)
583 #ifndef USE_ALT_ADDRBOOK
584 _harv_addressIndex_
= addrIndex
;
585 _harv_addressBook_
= NULL
;
587 _harv_messageList_
= msgList
;
590 if (!addrgather_dlg
.window
)
591 addrgather_dlg_create();
593 addrgather_dlg
.done
= FALSE
;
595 gtk_notebook_set_current_page(GTK_NOTEBOOK(addrgather_dlg
.notebook
), PAGE_FIELDS
);
596 addrgather_dlg
.folderPath
= folder_item_get_path(folderItem
);
598 /* Setup some default values */
600 gtk_label_set_text(GTK_LABEL(addrgather_dlg
.labelFolder
), folderItem
->path
);
601 #ifndef USE_ALT_ADDRBOOK
602 gtk_entry_set_text(GTK_ENTRY(addrgather_dlg
.entryBook
), folderItem
->path
);
605 for (i
= 0; i
< NUM_FIELDS
; i
++) {
606 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(addrgather_dlg
.checkHeader
[i
]),
608 if (g_utf8_collate(_harv_headerNames_
[i
], HEADER_FROM
) == 0)
609 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(addrgather_dlg
.checkHeader
[i
]),
613 gtk_widget_set_sensitive(addrgather_dlg
.btnOk
, TRUE
);
614 gtk_widget_set_sensitive(addrgather_dlg
.btnCancel
, TRUE
);
615 gtk_widget_grab_default(addrgather_dlg
.btnOk
);
617 /* Apply window title */
619 gtk_window_set_title(GTK_WINDOW(addrgather_dlg
.window
),
620 _("Collect email addresses from selected messages"));
621 gtk_widget_set_sensitive(addrgather_dlg
.checkRecurse
, FALSE
);
623 gtk_window_set_title(GTK_WINDOW(addrgather_dlg
.window
),
624 _("Collect email addresses from folder"));
625 gtk_widget_set_sensitive(addrgather_dlg
.checkRecurse
, TRUE
);
627 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(addrgather_dlg
.checkRecurse
), FALSE
);
629 addrgather_dlg_status_show("");
630 gtk_widget_show(addrgather_dlg
.window
);
631 gtk_window_set_modal(GTK_WINDOW(addrgather_dlg
.window
), TRUE
);
632 gtk_widget_grab_focus(addrgather_dlg
.entryBook
);
633 manage_window_set_transient(GTK_WINDOW(addrgather_dlg
.window
));
636 g_free(addrgather_dlg
.folderPath
);
637 addrgather_dlg
.folderPath
= NULL
;
638 gtk_widget_hide(addrgather_dlg
.window
);
639 gtk_window_set_modal(GTK_WINDOW(addrgather_dlg
.window
), FALSE
);
640 #ifndef USE_ALT_ADDRBOOK
641 _harv_addressIndex_
= NULL
;
643 if (addrgather_dlg
.cancelled
== TRUE
)
646 return _harv_addressBook_
;