Fix bug #3574: Template addressing
[claws.git] / src / account.c
blob9c34f971cdb7aaec4cd59807bec978d5a4abade0
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2015 Hiroyuki Yamamoto 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/>.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #include "claws-features.h"
23 #endif
25 #include "defs.h"
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30 #include <gdk/gdkkeysyms.h>
31 #include <stdio.h>
32 #include <errno.h>
34 #include "main.h"
35 #include "mainwindow.h"
36 #include "folderview.h"
37 #include "folder.h"
38 #include "account.h"
39 #include "prefs_gtk.h"
40 #include "prefs_account.h"
41 #include "prefs_common.h"
42 #include "folder_item_prefs.h"
43 #include "compose.h"
44 #include "manage_window.h"
45 #include "stock_pixmap.h"
46 #include "inc.h"
47 #include "gtkutils.h"
48 #include "utils.h"
49 #include "alertpanel.h"
50 #include "procheader.h"
51 #include "customheader.h"
52 #include "remotefolder.h"
53 #include "manual.h"
54 #include "filtering.h"
55 #include "prefs_actions.h"
56 #include "hooks.h"
57 #include "passwordstore.h"
59 enum {
60 ACCOUNT_IS_DEFAULT,
61 ACCOUNT_ENABLE_GET_ALL,
62 ACCOUNT_NAME,
63 ACCOUNT_PROTOCOL,
64 ACCOUNT_SERVER,
65 ACCOUNT_DATA,
66 N_ACCOUNT_COLUMNS
70 typedef enum
72 COL_DEFAULT = 0,
73 COL_GETALL = 1,
74 COL_NAME = 2,
75 COL_PROTOCOL = 3,
76 COL_SERVER = 4
77 } EditAccountColumnPos;
79 # define N_EDIT_ACCOUNT_COLS 5
81 PrefsAccount *cur_account;
83 static GList *account_list = NULL;
84 static gboolean account_list_dirty = FALSE;
86 static struct EditAccount {
87 GtkWidget *window;
88 GtkWidget *list_view;
89 GtkWidget *close_btn;
90 } edit_account;
92 static void account_edit_create (void);
93 static void account_destroy (PrefsAccount *ac_prefs);
94 static void account_set_as_default (PrefsAccount *ac_prefs);
95 static void account_set_menu (void);
97 static void account_edit_prefs (GtkWidget *widget, gpointer data);
98 static void account_delete (GtkWidget *widget, gpointer data);
99 static void account_clone (GtkWidget *widget, gpointer data);
101 static void account_up (GtkWidget *widget, gpointer data);
102 static void account_down (GtkWidget *widget, gpointer data);
104 static void account_set_default (GtkWidget *widget, gpointer data);
106 static void account_edit_close (GtkWidget *widget, gpointer data);
108 static gint account_delete_event (GtkWidget *widget,
109 GdkEventAny *event,
110 gpointer data);
111 static void account_size_allocate_cb(GtkWidget *widget,
112 GtkAllocation *allocation);
113 static gboolean account_key_pressed (GtkWidget *widget,
114 GdkEventKey *event,
115 gpointer data);
116 static gboolean account_search_func_cb (GtkTreeModel *model, gint column,
117 const gchar *key, GtkTreeIter *iter,
118 gpointer search_data);
119 static void account_list_view_add (PrefsAccount *ac_prefs);
120 static void account_list_view_set (void);
122 static void account_list_set (void);
124 typedef struct FindAccountInStore {
125 gint account_id;
126 GtkTreePath *path;
127 GtkTreeIter iter;
128 } FindAccountInStore;
130 static GtkListStore* account_create_data_store (void);
132 static void account_list_view_insert_account_item (GtkListStore *list_store,
133 const gchar *account_name,
134 const gchar *protocol,
135 const gchar *server_name,
136 gboolean is_default,
137 gboolean is_get_all,
138 PrefsAccount *account_data);
140 static GtkWidget *account_list_view_create (void);
141 static void account_create_list_view_columns (GtkWidget *list_view);
143 static gint account_list_view_get_selected_account_id (GtkWidget *list_view);
144 static GtkTreePath *account_list_view_get_selected_account_path (GtkWidget *list_view);
145 static PrefsAccount *account_list_view_get_selected_account (GtkWidget *list_view);
146 static gboolean account_list_view_select_account (GtkWidget *list_view,
147 gint account_id);
149 static void account_list_view_set_default_by_id(GtkWidget *list_view,
150 gint account_id);
152 static gboolean set_new_default_account (GtkTreeModel *model,
153 GtkTreePath *path,
154 GtkTreeIter *iter,
155 gint *account_id);
157 static gboolean find_account_in_store (GtkTreeModel *model,
158 GtkTreePath *path,
159 GtkTreeIter *iter,
160 FindAccountInStore *data);
162 static void account_get_all_toggled (GtkCellRendererToggle *widget,
163 gchar *path,
164 GtkWidget *list_view);
166 static void account_double_clicked (GtkTreeView *list_view,
167 GtkTreePath *path,
168 GtkTreeViewColumn *column,
169 gpointer data);
171 static void drag_begin (GtkTreeView *list_view,
172 GdkDragContext *context,
173 gpointer data);
175 static void drag_end (GtkTreeView *list_view,
176 GdkDragContext *context,
177 gpointer data);
179 static void account_row_changed_while_drag_drop (GtkTreeModel *model,
180 GtkTreePath *path,
181 GtkTreeIter *iter,
182 gpointer arg3,
183 GtkTreeView *list_view);
185 static void account_flush_state(void)
187 account_set_menu();
188 main_window_reflect_prefs_all();
190 account_list_dirty = FALSE;
193 void account_read_config_all(void)
195 GSList *ac_label_list = NULL, *cur;
196 gchar *rcpath;
197 FILE *fp;
198 gchar buf[PREFSBUFSIZE];
199 PrefsAccount *ac_prefs;
201 debug_print("Reading all config for each account...\n");
203 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACCOUNT_RC, NULL);
204 if ((fp = g_fopen(rcpath, "rb")) == NULL) {
205 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
206 g_free(rcpath);
207 return;
209 g_free(rcpath);
211 while (fgets(buf, sizeof(buf), fp) != NULL) {
212 if (!strncmp(buf, "[Account: ", 10)) {
213 strretchomp(buf);
214 memmove(buf, buf + 1, sizeof(buf) - 1);
215 buf[strlen(buf) - 1] = '\0';
216 debug_print("Found label: %s\n", buf);
217 ac_label_list = g_slist_append(ac_label_list,
218 g_strdup(buf));
221 fclose(fp);
223 /* read config data from file */
224 cur_account = NULL;
225 for (cur = ac_label_list; cur != NULL; cur = cur->next) {
226 ac_prefs = prefs_account_new();
227 prefs_account_read_config(ac_prefs, (gchar *)cur->data);
228 account_list = g_list_append(account_list, ac_prefs);
229 if (ac_prefs->is_default)
230 cur_account = ac_prefs;
232 /* if default is not set, assume first account as default */
233 if (!cur_account && account_list) {
234 ac_prefs = (PrefsAccount *)account_list->data;
235 account_set_as_default(ac_prefs);
236 cur_account = ac_prefs;
239 account_set_menu();
240 main_window_reflect_prefs_all_now();
242 while (ac_label_list) {
243 g_free(ac_label_list->data);
244 ac_label_list = g_slist_remove(ac_label_list,
245 ac_label_list->data);
249 void account_write_config_all(void)
251 prefs_account_write_config_all(account_list);
255 * account_find_all_from_address:
256 * @ac_list: initial list of accounts. NULL to create a new one.
257 * Accounts found in the @address will be appended to this list.
258 * @address: Email address string.
260 * Find all the mail (not news) accounts within the specified address.
262 * Return value: the original accounts list with the found accounts appended.
264 GList *account_find_all_from_address(GList *ac_list, const gchar *address)
266 GList *cur;
267 PrefsAccount *ac;
269 if (address == NULL)
270 return ac_list;
272 for (cur = account_list; cur != NULL; cur = cur->next) {
273 ac = (PrefsAccount *)cur->data;
274 if (ac->protocol != A_NNTP && ac->address &&
275 strcasestr(address, ac->address) != NULL)
276 ac_list = g_list_append(ac_list, ac);
278 return ac_list;
281 PrefsAccount *account_find_from_smtp_server(const gchar *address,
282 const gchar *smtp_server)
284 GList *cur;
285 PrefsAccount *ac;
287 cm_return_val_if_fail(address != NULL, NULL);
288 cm_return_val_if_fail(smtp_server != NULL, NULL);
290 for (cur = account_list; cur != NULL; cur = cur->next) {
291 ac = (PrefsAccount *)cur->data;
292 if (!strcmp2(address, ac->address) &&
293 !strcmp2(smtp_server, ac->smtp_server))
294 return ac;
297 return NULL;
301 * account_find_from_address:
302 * @address: Email address string.
304 * Find a mail (not news if newsgroups_ok is FALSE) account with the specified email address.
306 * Return value: The found account, or NULL if not found.
308 PrefsAccount *account_find_from_address(const gchar *address, gboolean newsgroups_ok)
310 GList *cur;
311 PrefsAccount *ac;
313 cm_return_val_if_fail(address != NULL, NULL);
315 for (cur = account_list; cur != NULL; cur = cur->next) {
316 ac = (PrefsAccount *)cur->data;
317 if ((ac->protocol != A_NNTP || newsgroups_ok) && ac->address &&
318 g_ascii_strcasecmp(address, ac->address) == 0)
319 return ac;
322 return NULL;
325 PrefsAccount *account_find_from_id(gint id)
327 GList *cur;
328 PrefsAccount *ac;
330 for (cur = account_list; cur != NULL; cur = cur->next) {
331 ac = (PrefsAccount *)cur->data;
332 if (id == ac->account_id)
333 return ac;
336 return NULL;
339 PrefsAccount *account_find_from_item(FolderItem *item)
341 PrefsAccount *ac;
343 cm_return_val_if_fail(item != NULL, NULL);
345 ac = item->account;
346 if (!ac) {
347 FolderItem *cur_item = folder_item_parent(item);
348 while (cur_item != NULL) {
349 if (cur_item->account && cur_item->apply_sub) {
350 ac = cur_item->account;
351 break;
353 cur_item = folder_item_parent(cur_item);
356 if (!ac)
357 ac = item->folder->account;
359 return ac;
362 static void account_set_menu(void)
364 main_window_set_account_menu(account_list);
367 void account_set_menu_only_toolbar(void)
369 main_window_set_account_menu_only_toolbar(account_list);
372 GList *account_get_list(void)
374 return account_list;
377 void account_edit_focus(void)
379 if (edit_account.window == NULL) {
380 return;
382 manage_window_set_transient(GTK_WINDOW(edit_account.window));
383 gtk_widget_grab_focus(edit_account.close_btn);
384 gtk_widget_show(edit_account.window);
385 gtk_window_set_modal(GTK_WINDOW(edit_account.window), TRUE);
386 manage_window_focus_in(edit_account.window, NULL, NULL);
389 void account_edit_open(gpointer a, gpointer b)
391 inc_lock();
393 account_list_dirty = FALSE;
395 if (compose_get_compose_list()) {
396 alertpanel_error(_("Some composing windows are open.\n"
397 "Please close all the composing "
398 "windows before editing accounts."));
399 inc_unlock();
400 return;
403 debug_print("Opening account edit window...\n");
405 if (!edit_account.window)
406 account_edit_create();
408 account_list_view_set();
410 account_edit_focus();
413 void account_add(void)
415 PrefsAccount *ac_prefs;
417 ac_prefs = prefs_account_open(NULL, &account_list_dirty);
419 if (!ac_prefs) return;
421 account_edit_focus();
423 account_list = g_list_append(account_list, ac_prefs);
425 if (ac_prefs->is_default)
426 account_set_as_default(ac_prefs);
428 account_list_view_set();
430 if (ac_prefs->protocol == A_IMAP4 || ac_prefs->protocol == A_NNTP) {
431 Folder *folder;
433 if (ac_prefs->protocol == A_IMAP4) {
434 folder = folder_new(folder_get_class_from_string("imap"), ac_prefs->account_name,
435 ac_prefs->recv_server);
436 } else {
437 folder = folder_new(folder_get_class_from_string("news"), ac_prefs->account_name,
438 ac_prefs->nntp_server);
440 if (folder == NULL) {
441 alertpanel_error(_("Can't create folder."));
442 return;
444 folder->account = ac_prefs;
445 ac_prefs->folder = folder;
446 folder_add(folder);
447 if (ac_prefs->protocol == A_IMAP4)
448 folder->klass->create_tree(folder);
449 folderview_set_all();
450 folder_write_list();
454 void account_open(PrefsAccount *ac_prefs)
456 gboolean prev_default;
457 gchar *ac_name, *old_prefix, *new_prefix;
458 gboolean account_dirty = FALSE;
460 cm_return_if_fail(ac_prefs != NULL);
462 if (compose_get_compose_list()) {
463 alertpanel_error(_("Some composing windows are open.\n"
464 "Please close all the composing "
465 "windows before editing accounts."));
466 return;
469 prev_default = ac_prefs->is_default;
470 Xstrdup_a(ac_name, ac_prefs->account_name ? ac_prefs->account_name : "",
471 return);
473 prefs_account_open(ac_prefs, &account_dirty);
475 account_edit_focus();
477 if (account_dirty) {
478 if (!prev_default && ac_prefs->is_default)
479 account_set_as_default(ac_prefs);
481 if (ac_prefs->folder && strcmp2(ac_name, ac_prefs->account_name) != 0) {
482 old_prefix = folder_get_identifier(FOLDER(ac_prefs->folder));
483 folder_set_name(FOLDER(ac_prefs->folder),
484 ac_prefs->account_name);
485 folderview_set_all();
486 folder_prefs_save_config_recursive(FOLDER(ac_prefs->folder));
487 new_prefix = folder_get_identifier(FOLDER(ac_prefs->folder));
489 account_rename_path(old_prefix, new_prefix);
490 prefs_filtering_rename_path(old_prefix, new_prefix);
491 prefs_actions_rename_path(old_prefix, new_prefix);
493 g_free(old_prefix);
494 g_free(new_prefix);
497 account_write_config_all();
499 account_flush_state();
503 static void account_set_as_default(PrefsAccount *ac_prefs)
505 PrefsAccount *ap;
506 GList *cur;
508 for (cur = account_list; cur != NULL; cur = cur->next) {
509 ap = (PrefsAccount *)cur->data;
510 if (ap->is_default)
511 ap->is_default = FALSE;
514 ac_prefs->is_default = TRUE;
517 PrefsAccount *account_get_default(void)
519 PrefsAccount *ap;
520 GList *cur;
522 for (cur = account_list; cur != NULL; cur = cur->next) {
523 ap = (PrefsAccount *)cur->data;
524 if (ap->is_default)
525 return ap;
528 return NULL;
531 void account_set_missing_folder(void)
533 PrefsAccount *ap;
534 GList *cur;
536 for (cur = account_list; cur != NULL; cur = cur->next) {
537 ap = (PrefsAccount *)cur->data;
538 if ((ap->protocol == A_IMAP4 || ap->protocol == A_NNTP) &&
539 !ap->folder) {
540 Folder *folder;
542 if (ap->protocol == A_IMAP4) {
543 folder = folder_new(folder_get_class_from_string("imap"), ap->account_name,
544 ap->recv_server);
545 } else {
546 folder = folder_new(folder_get_class_from_string("news"), ap->account_name,
547 ap->nntp_server);
549 if (folder == NULL)
550 return;
551 folder->account = ap;
552 ap->folder = folder;
553 folder_add(folder);
554 if (ap->protocol == A_IMAP4)
555 folder->klass->create_tree(folder);
556 folder_write_list();
562 #define CHECK_CHANGE_FOLDER(folder) { \
563 if (folder && !strncmp(folder, old_id, strlen(old_id))) { \
564 if (strlen(folder) == strlen(old_id)) { \
565 g_free(folder); \
566 folder = g_strdup(new_id); \
567 } else if (strlen(folder) > strlen(old_id) \
568 && folder[strlen(old_id)] == G_DIR_SEPARATOR) { \
569 gchar *new_path = g_strdup_printf("%s%s", \
570 new_id, (folder + strlen(old_id))); \
571 g_free(folder); \
572 folder = new_path; \
577 void account_rename_path(const gchar *old_id, const gchar *new_id)
579 GList *cur = account_list;
580 for (; cur != NULL; cur = g_list_next(cur)) {
581 PrefsAccount *ap = (PrefsAccount *)cur->data;
582 CHECK_CHANGE_FOLDER(ap->inbox);
583 CHECK_CHANGE_FOLDER(ap->local_inbox);
584 CHECK_CHANGE_FOLDER(ap->queue_folder);
585 CHECK_CHANGE_FOLDER(ap->sent_folder);
586 CHECK_CHANGE_FOLDER(ap->draft_folder);
587 CHECK_CHANGE_FOLDER(ap->trash_folder);
591 FolderItem *account_get_special_folder(PrefsAccount *ac_prefs,
592 SpecialFolderItemType type)
594 FolderItem *item = NULL;
596 cm_return_val_if_fail(ac_prefs != NULL, NULL);
598 switch (type) {
599 case F_INBOX:
600 if (ac_prefs->folder)
601 item = FOLDER(ac_prefs->folder)->inbox;
602 if (!item)
603 item = folder_get_default_inbox();
604 break;
605 case F_OUTBOX:
606 if (ac_prefs->set_sent_folder && ac_prefs->sent_folder) {
607 item = folder_find_item_from_identifier
608 (ac_prefs->sent_folder);
610 if (!item) {
611 if (ac_prefs->folder)
612 item = FOLDER(ac_prefs->folder)->outbox;
613 if (!item)
614 item = folder_get_default_outbox_for_class(F_MH);
615 if (!item)
616 item = folder_get_default_outbox();
618 break;
619 case F_DRAFT:
620 if (ac_prefs->set_draft_folder && ac_prefs->draft_folder) {
621 item = folder_find_item_from_identifier
622 (ac_prefs->draft_folder);
624 if (!item) {
625 if (ac_prefs->folder)
626 item = FOLDER(ac_prefs->folder)->draft;
627 if (!item)
628 item = folder_get_default_draft_for_class(F_MH);
629 if (!item)
630 item = folder_get_default_draft();
632 break;
633 case F_QUEUE:
634 if (ac_prefs->set_queue_folder && ac_prefs->queue_folder) {
635 item = folder_find_item_from_identifier
636 (ac_prefs->queue_folder);
638 if (!item) {
639 if (ac_prefs->folder)
640 item = FOLDER(ac_prefs->folder)->queue;
641 if (!item)
642 item = folder_get_default_queue_for_class(F_MH);
643 if (!item)
644 item = folder_get_default_queue();
646 break;
647 case F_TRASH:
648 if (ac_prefs->set_trash_folder && ac_prefs->trash_folder) {
649 item = folder_find_item_from_identifier
650 (ac_prefs->trash_folder);
652 if (!item) {
653 if (ac_prefs->folder)
654 item = FOLDER(ac_prefs->folder)->trash;
655 if (!item)
656 item = folder_get_default_trash_for_class(F_MH);
657 if (!item)
658 item = folder_get_default_trash();
660 break;
661 default:
662 break;
665 return item;
668 void account_destroy(PrefsAccount *ac_prefs)
670 cm_return_if_fail(ac_prefs != NULL);
672 folder_unref_account_all(ac_prefs);
674 account_list = g_list_remove(account_list, ac_prefs);
676 if (cur_account == ac_prefs) cur_account = NULL;
677 if (!cur_account && account_list) {
678 cur_account = account_get_default();
679 if (!cur_account) {
680 ac_prefs = (PrefsAccount *)account_list->data;
681 account_set_as_default(ac_prefs);
682 cur_account = ac_prefs;
688 *\brief Save Gtk object size to prefs dataset
690 static void account_size_allocate_cb(GtkWidget *widget,
691 GtkAllocation *allocation)
693 cm_return_if_fail(allocation != NULL);
695 prefs_common.accountswin_width = allocation->width;
696 prefs_common.accountswin_height = allocation->height;
699 static void account_edit_create(void)
701 GtkWidget *window;
702 GtkWidget *vbox;
703 GtkWidget *label;
704 GtkWidget *hbox;
705 GtkWidget *scrolledwin;
706 GtkWidget *list_view;
708 GtkWidget *vbox2;
709 GtkWidget *add_btn;
710 GtkWidget *edit_btn;
711 GtkWidget *del_btn;
712 GtkWidget *clone_btn;
713 GtkWidget *up_btn;
714 GtkWidget *down_btn;
716 GtkWidget *default_btn;
718 GtkWidget *confirm_area;
719 GtkWidget *help_btn;
720 GtkWidget *close_btn;
722 static GdkGeometry geometry;
724 debug_print("Creating account edit window...\n");
726 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "account");
727 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
728 gtk_window_set_title (GTK_WINDOW (window), _("Edit accounts"));
729 g_signal_connect (G_OBJECT (window), "delete_event",
730 G_CALLBACK (account_delete_event), NULL);
731 g_signal_connect (G_OBJECT (window), "key_press_event",
732 G_CALLBACK (account_key_pressed), NULL);
733 MANAGE_WINDOW_SIGNALS_CONNECT (window);
734 gtk_widget_realize(window);
736 vbox = gtk_vbox_new (FALSE, 10);
737 gtk_widget_show (vbox);
738 gtk_container_add (GTK_CONTAINER (window), vbox);
740 hbox = gtk_hbox_new (FALSE, 0);
741 gtk_widget_show (hbox);
742 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
744 label = gtk_label_new
745 (_("Using 'Get Mail' will retrieve messages from your Accounts "
746 "in the order given, the checkbox indicates which accounts "
747 "will be included. Bold text indicates the default account."));
748 gtk_widget_show (label);
749 gtk_widget_set_size_request(GTK_WIDGET(label),
750 prefs_common.accountswin_width-8, -1);
751 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 4);
752 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
753 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
755 hbox = gtk_hbox_new (FALSE, 8);
756 gtk_widget_show (hbox);
757 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
758 gtk_container_set_border_width (GTK_CONTAINER (hbox), 2);
760 scrolledwin = gtk_scrolled_window_new (NULL, NULL);
761 gtk_widget_show (scrolledwin);
762 gtk_box_pack_start (GTK_BOX (hbox), scrolledwin, TRUE, TRUE, 0);
763 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwin),
764 GTK_POLICY_AUTOMATIC,
765 GTK_POLICY_AUTOMATIC);
767 list_view = account_list_view_create();
768 gtk_widget_show(list_view);
769 gtk_container_add(GTK_CONTAINER(scrolledwin), list_view);
771 vbox2 = gtk_vbox_new (FALSE, 0);
772 gtk_widget_show (vbox2);
773 gtk_box_pack_start (GTK_BOX (hbox), vbox2, FALSE, FALSE, 0);
775 add_btn = gtk_button_new_from_stock(GTK_STOCK_NEW);
776 gtk_widget_show (add_btn);
777 gtk_box_pack_start (GTK_BOX (vbox2), add_btn, FALSE, FALSE, 4);
778 g_signal_connect (G_OBJECT(add_btn), "clicked",
779 G_CALLBACK (account_add), NULL);
781 edit_btn = gtk_button_new_from_stock (GTK_STOCK_EDIT);
782 gtk_widget_show (edit_btn);
783 gtk_box_pack_start (GTK_BOX (vbox2), edit_btn, FALSE, FALSE, 4);
784 g_signal_connect (G_OBJECT(edit_btn), "clicked",
785 G_CALLBACK (account_edit_prefs), NULL);
787 del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
788 gtk_widget_show (del_btn);
789 gtk_box_pack_start (GTK_BOX (vbox2), del_btn, FALSE, FALSE, 4);
790 g_signal_connect (G_OBJECT(del_btn), "clicked",
791 G_CALLBACK (account_delete), NULL);
793 clone_btn = gtk_button_new_from_stock(GTK_STOCK_COPY);
794 gtk_widget_show (clone_btn);
795 gtk_box_pack_start (GTK_BOX (vbox2), clone_btn, FALSE, FALSE, 4);
796 g_signal_connect(G_OBJECT(clone_btn), "clicked",
797 G_CALLBACK(account_clone), NULL);
799 down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
800 gtk_widget_show (down_btn);
801 gtk_box_pack_end (GTK_BOX (vbox2), down_btn, FALSE, FALSE, 4);
802 g_signal_connect (G_OBJECT(down_btn), "clicked",
803 G_CALLBACK (account_down), NULL);
805 up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
806 gtk_widget_show (up_btn);
807 gtk_box_pack_end (GTK_BOX (vbox2), up_btn, FALSE, FALSE, 4);
808 g_signal_connect (G_OBJECT(up_btn), "clicked",
809 G_CALLBACK (account_up), NULL);
811 hbox = gtk_hbox_new (FALSE, 8);
812 gtk_widget_show (hbox);
813 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
815 default_btn = gtk_button_new_with_mnemonic
816 (_(" _Set as default account "));
817 gtk_widget_show (default_btn);
818 gtk_box_pack_start (GTK_BOX (hbox), default_btn, FALSE, FALSE, 0);
819 g_signal_connect (G_OBJECT(default_btn), "clicked",
820 G_CALLBACK (account_set_default), NULL);
822 gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
823 &close_btn, GTK_STOCK_CLOSE,
824 NULL, NULL, NULL, NULL);
825 gtk_widget_show(confirm_area);
827 gtk_box_pack_end (GTK_BOX (hbox), confirm_area, FALSE, FALSE, 0);
828 gtk_widget_grab_default (close_btn);
830 g_signal_connect (G_OBJECT (close_btn), "clicked",
831 G_CALLBACK (account_edit_close),
832 NULL);
833 g_signal_connect(G_OBJECT(help_btn), "clicked",
834 G_CALLBACK(manual_open_with_anchor_cb),
835 MANUAL_ANCHOR_ACCOUNTPREFS);
838 g_signal_connect(G_OBJECT(window), "size_allocate",
839 G_CALLBACK(account_size_allocate_cb), NULL);
841 if (!geometry.min_height) {
842 geometry.min_width = 500;
843 geometry.min_height = 380;
846 gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
847 GDK_HINT_MIN_SIZE);
848 gtk_widget_set_size_request(window, prefs_common.accountswin_width,
849 prefs_common.accountswin_height);
850 #ifdef G_OS_WIN32
851 gtk_window_move(GTK_WINDOW(window), 48, 48);
852 #endif
854 edit_account.window = window;
855 edit_account.list_view = list_view;
856 edit_account.close_btn = close_btn;
859 static void account_edit_prefs(GtkWidget *widget, gpointer data)
861 PrefsAccount *ac_prefs;
863 ac_prefs = account_list_view_get_selected_account(edit_account.list_view);
865 if (ac_prefs) {
866 account_open(ac_prefs);
867 account_list_view_set();
871 static gboolean account_delete_references_func(GNode *node, gpointer data)
873 FolderItem *item;
874 gint account;
876 cm_return_val_if_fail(node->data != NULL, FALSE);
878 item = FOLDER_ITEM(node->data);
879 account = GPOINTER_TO_INT(data);
881 if(!item->prefs) /* && item->prefs->stype == F_NORMAL */
882 return FALSE;
883 if(item->prefs->default_account != account)
884 return FALSE;
886 item->prefs->enable_default_account = FALSE;
887 item->prefs->default_account = 0;
888 folder_item_prefs_save_config(item);
890 return FALSE;
894 #define ACP_FDUP(fld) ac_clon->fld = ((ac_prefs->fld) != NULL)?\
895 g_strdup(ac_prefs->fld): NULL
896 #define ACP_FASSIGN(fld) ac_clon->fld = ac_prefs->fld
897 static void account_clone(GtkWidget *widget, gpointer data)
899 PrefsAccount *ac_prefs, *ac_clon;
900 GSList *hdrs = NULL;
901 CustomHeader *cch = NULL, *ch = NULL;
903 ac_prefs = account_list_view_get_selected_account(edit_account.list_view);
904 if (ac_prefs == NULL)
905 return;
907 if (ac_prefs->protocol == A_IMAP4 || ac_prefs->protocol == A_NNTP) {
908 alertpanel_error(_("Accounts with remote folders cannot be copied."));
909 return;
911 account_list_dirty = TRUE;
913 ac_clon = prefs_account_new();
914 /* copy fields */
915 ac_clon->account_name = g_strdup_printf(_("Copy of %s"),
916 ac_prefs->account_name);
917 /* personal */
918 ACP_FDUP(name);
919 ACP_FDUP(address);
920 ACP_FDUP(organization);
922 /* server */
923 ACP_FASSIGN(protocol);
924 ACP_FDUP(recv_server);
925 ACP_FDUP(smtp_server);
926 ACP_FDUP(nntp_server);
927 ACP_FASSIGN(use_nntp_auth);
928 ACP_FASSIGN(use_nntp_auth_onconnect);
929 ACP_FDUP(userid);
930 ACP_FDUP(passwd);
932 ACP_FDUP(local_mbox);
933 ACP_FASSIGN(use_mail_command);
934 ACP_FDUP(mail_command);
936 ACP_FASSIGN(ssl_pop);
937 ACP_FASSIGN(ssl_imap);
938 ACP_FASSIGN(ssl_nntp);
939 ACP_FASSIGN(ssl_smtp);
940 ACP_FASSIGN(use_nonblocking_ssl);
942 /* receive */
943 ACP_FASSIGN(use_apop_auth);
944 ACP_FASSIGN(rmmail);
945 ACP_FASSIGN(msg_leave_time);
946 ACP_FASSIGN(msg_leave_hour);
947 ACP_FASSIGN(recv_at_getall);
948 ACP_FASSIGN(sd_rmmail_on_download);
949 ACP_FASSIGN(enable_size_limit);
950 ACP_FASSIGN(size_limit);
951 ACP_FASSIGN(filter_on_recv);
952 ACP_FASSIGN(filterhook_on_recv);
953 ACP_FDUP(inbox);
954 ACP_FDUP(local_inbox);
955 ACP_FASSIGN(max_articles);
957 ACP_FASSIGN(imap_auth_type);
959 /* send */
960 ACP_FASSIGN(gen_msgid);
961 ACP_FASSIGN(gen_xmailer);
962 ACP_FASSIGN(add_customhdr);
963 ACP_FASSIGN(use_smtp_auth);
964 ACP_FASSIGN(smtp_auth_type);
965 ACP_FDUP(smtp_userid);
966 ACP_FDUP(smtp_passwd);
968 ACP_FASSIGN(pop_before_smtp);
969 ACP_FASSIGN(pop_before_smtp_timeout);
970 ACP_FASSIGN(last_pop_login_time);
972 ac_clon->customhdr_list = NULL;
973 hdrs = ac_prefs->customhdr_list;
974 while (hdrs != NULL) {
975 ch = (CustomHeader *)hdrs->data;
977 cch = g_new0(CustomHeader, 1);
978 cch->account_id = ac_clon->account_id;
979 cch->name = (ch->name != NULL) ? g_strdup(ch->name) : NULL;
980 cch->value = (ch->value != NULL) ? g_strdup(ch->value) : NULL;
982 ac_clon->customhdr_list = g_slist_append(ac_clon->customhdr_list, cch);
984 hdrs = g_slist_next(hdrs);
987 /* compose */
988 ACP_FASSIGN(sig_type);
989 ACP_FDUP(sig_path);
990 ACP_FASSIGN(auto_sig);
991 ACP_FDUP(sig_sep);
992 ACP_FASSIGN(set_autocc);
993 ACP_FDUP(auto_cc);
994 ACP_FASSIGN(set_autobcc);
995 ACP_FDUP(auto_bcc);
996 ACP_FASSIGN(set_autoreplyto);
997 ACP_FDUP(auto_replyto);
998 ACP_FASSIGN(enable_default_dictionary);
999 ACP_FDUP(default_dictionary);
1000 ACP_FASSIGN(enable_default_alt_dictionary);
1001 ACP_FDUP(default_alt_dictionary);
1002 ACP_FASSIGN(compose_with_format);
1003 ACP_FDUP(compose_subject_format);
1004 ACP_FDUP(compose_body_format);
1005 ACP_FASSIGN(reply_with_format);
1006 ACP_FDUP(reply_quotemark);
1007 ACP_FDUP(reply_body_format);
1008 ACP_FASSIGN(forward_with_format);
1009 ACP_FDUP(forward_quotemark);
1010 ACP_FDUP(forward_body_format);
1012 /* privacy */
1013 ACP_FDUP(default_privacy_system);
1014 ACP_FASSIGN(default_encrypt);
1015 ACP_FASSIGN(default_encrypt_reply);
1016 ACP_FASSIGN(default_sign);
1017 ACP_FASSIGN(default_sign_reply);
1018 ACP_FASSIGN(save_encrypted_as_clear_text);
1019 ACP_FASSIGN(encrypt_to_self);
1021 /* advanced */
1022 ACP_FASSIGN(set_smtpport);
1023 ACP_FASSIGN(smtpport);
1024 ACP_FASSIGN(set_popport);
1025 ACP_FASSIGN(popport);
1026 ACP_FASSIGN(set_imapport);
1027 ACP_FASSIGN(imapport);
1028 ACP_FASSIGN(set_nntpport);
1029 ACP_FASSIGN(nntpport);
1030 ACP_FASSIGN(set_domain);
1031 ACP_FDUP(domain);
1032 ACP_FASSIGN(mark_crosspost_read);
1033 ACP_FASSIGN(crosspost_col);
1035 #ifndef G_OS_WIN32
1036 ACP_FASSIGN(set_tunnelcmd);
1037 ACP_FDUP(tunnelcmd);
1038 #endif
1040 ACP_FDUP(imap_dir);
1041 ACP_FASSIGN(imap_subsonly);
1042 ACP_FASSIGN(low_bandwidth);
1044 ACP_FASSIGN(set_sent_folder);
1045 ACP_FDUP(sent_folder);
1046 ACP_FASSIGN(set_queue_folder);
1047 ACP_FDUP(queue_folder);
1048 ACP_FASSIGN(set_draft_folder);
1049 ACP_FDUP(draft_folder);
1050 ACP_FASSIGN(set_trash_folder);
1051 ACP_FDUP(trash_folder);
1052 /* don't want two default accounts */
1053 ac_clon->is_default = FALSE;
1054 ACP_FASSIGN(folder);
1056 account_list = g_list_append(account_list, ac_clon);
1057 account_list_view_set();
1059 #undef ACP_FDUP
1060 #undef ACP_FASSIGN
1062 static void account_delete(GtkWidget *widget, gpointer data)
1064 PrefsAccount *ac_prefs;
1065 gchar buf[BUFFSIZE];
1066 GList *list;
1067 Folder *folder;
1068 GSList *cur;
1070 ac_prefs = account_list_view_get_selected_account(edit_account.list_view);
1071 if (ac_prefs == NULL)
1072 return;
1074 g_snprintf(buf, sizeof(buf),
1075 _("Do you really want to delete the account '%s'?"),
1076 ac_prefs->account_name ? ac_prefs->account_name :
1077 _("(Untitled)"));
1078 if (alertpanel_full(_("Delete account"), buf,
1079 GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, FALSE,
1080 NULL, ALERT_WARNING, G_ALERTDEFAULT) != G_ALERTALTERNATE)
1081 return;
1082 account_list_dirty = TRUE;
1084 if (ac_prefs->folder) {
1085 FolderItem *item;
1087 item = mainwindow_get_mainwindow()->summaryview->folder_item;
1088 if (item && item->folder == FOLDER(ac_prefs->folder))
1089 summary_clear_all(mainwindow_get_mainwindow()->summaryview);
1090 folder_destroy(FOLDER(ac_prefs->folder));
1091 folderview_set_all();
1093 account_destroy(ac_prefs);
1094 account_list_view_set();
1096 debug_print("Removing deleted account references for all the folders...\n");
1097 list = folder_get_list();
1098 for (; list != NULL; list = list->next) {
1099 folder = FOLDER(list->data);
1100 if (folder->node) /* && folder->type == F_? */
1101 g_node_traverse(folder->node, G_PRE_ORDER,
1102 G_TRAVERSE_ALL, -1,
1103 account_delete_references_func,
1104 GINT_TO_POINTER(ac_prefs->account_id));
1107 gchar *uid = g_strdup_printf("%d", ac_prefs->account_id);
1108 passwd_store_delete_block(PWS_ACCOUNT, uid);
1109 g_free(uid);
1111 debug_print("Removing filter rules relative to this account...\n");
1112 for(cur = filtering_rules ; cur != NULL ;) {
1113 FilteringProp * prop = (FilteringProp *) cur->data;
1115 if (prop && (prop->account_id == ac_prefs->account_id)) {
1116 /* get next item before we kill the current one */
1117 cur = g_slist_next(cur);
1119 /* unallocate filteringprop and unchain it from the list */
1120 filteringprop_free(prop);
1121 filtering_rules = g_slist_remove(filtering_rules, prop);
1122 } else {
1123 cur = g_slist_next(cur);
1126 folder_write_list();
1129 static void account_up(GtkWidget *widget, gpointer data)
1131 GtkTreePath *sel = account_list_view_get_selected_account_path
1132 (edit_account.list_view),
1133 *up;
1134 GtkTreeIter isel, iup;
1135 GtkTreeModel *model = gtk_tree_view_get_model
1136 (GTK_TREE_VIEW(edit_account.list_view));
1138 if (!sel)
1139 return;
1140 account_list_dirty = TRUE;
1142 up = gtk_tree_path_copy(sel);
1143 if (!up) {
1144 gtk_tree_path_free(sel);
1145 return;
1148 if (!gtk_tree_path_prev(up)) {
1149 gtk_tree_path_free(up);
1150 gtk_tree_path_free(sel);
1151 return;
1154 if (!gtk_tree_model_get_iter(model, &isel, sel)
1155 || !gtk_tree_model_get_iter(model, &iup, up)) {
1156 gtk_tree_path_free(up);
1157 gtk_tree_path_free(sel);
1158 return;
1161 gtk_list_store_swap(GTK_LIST_STORE(model), &isel, &iup);
1163 account_list_set();
1165 gtk_tree_path_free(up);
1166 gtk_tree_path_free(sel);
1169 static void account_down(GtkWidget *widget, gpointer data)
1171 GtkTreePath *sel = account_list_view_get_selected_account_path
1172 (edit_account.list_view),
1173 *dn;
1174 GtkTreeIter isel, idn;
1175 GtkTreeModel *model = gtk_tree_view_get_model
1176 (GTK_TREE_VIEW(edit_account.list_view));
1178 if (!sel)
1179 return;
1180 account_list_dirty = TRUE;
1182 dn = gtk_tree_path_copy(sel);
1183 if (!dn) {
1184 gtk_tree_path_free(sel);
1185 return;
1188 /* XXX no check possible??? however, if down but at bottom, then
1189 * nothing seems to happen much anyway, so the following seems to
1190 * be okay */
1191 gtk_tree_path_next(dn);
1193 if (!gtk_tree_model_get_iter(model, &isel, sel)
1194 || !gtk_tree_model_get_iter(model, &idn, dn)) {
1195 gtk_tree_path_free(dn);
1196 gtk_tree_path_free(sel);
1197 return;
1200 gtk_list_store_swap(GTK_LIST_STORE(model), &isel, &idn);
1202 account_list_set();
1204 gtk_tree_path_free(dn);
1205 gtk_tree_path_free(sel);
1208 static void account_set_default(GtkWidget *widget, gpointer data)
1210 PrefsAccount *ac_prefs;
1212 if (NULL == (ac_prefs = account_list_view_get_selected_account
1213 (edit_account.list_view)))
1214 return;
1216 /* we need to change the store variables by resetting everything
1217 * and setting the new default one */
1218 account_list_view_set_default_by_id(edit_account.list_view,
1219 ac_prefs->account_id);
1221 account_set_as_default(ac_prefs);
1222 account_list_view_set();
1224 cur_account = ac_prefs;
1225 account_flush_state();
1228 static void account_edit_close(GtkWidget *widget, gpointer data)
1230 account_list_set();
1231 account_write_config_all();
1233 if (!cur_account && account_list) {
1234 PrefsAccount *ac_prefs = (PrefsAccount *)account_list->data;
1235 account_set_as_default(ac_prefs);
1236 cur_account = ac_prefs;
1239 if (account_list_dirty)
1240 account_flush_state();
1242 gtk_widget_hide(edit_account.window);
1243 gtk_window_set_modal(GTK_WINDOW(edit_account.window), FALSE);
1244 inc_unlock();
1247 static gint account_delete_event(GtkWidget *widget, GdkEventAny *event,
1248 gpointer data)
1250 account_edit_close(NULL, NULL);
1251 return TRUE;
1254 static gboolean account_key_pressed(GtkWidget *widget, GdkEventKey *event,
1255 gpointer data)
1257 if (event && event->keyval == GDK_KEY_Escape)
1258 account_edit_close(NULL, NULL);
1259 return FALSE;
1262 static gboolean account_search_func_cb (GtkTreeModel *model, gint column, const gchar *key,
1263 GtkTreeIter *iter, gpointer search_data)
1265 gboolean retval;
1266 PrefsAccount *ac;
1268 gtk_tree_model_get (model, iter, ACCOUNT_DATA, &ac, -1);
1270 if (!ac->name || !key) return FALSE;
1272 retval = (strncmp (key, ac->account_name, strlen(key)) != 0);
1274 debug_print("selecting row\n");
1275 account_list_view_select_account(edit_account.list_view, ac->account_id);
1277 return retval;
1279 static void account_list_view_add(PrefsAccount *ac_prefs)
1281 GtkTreeView *list_view = GTK_TREE_VIEW(edit_account.list_view);
1282 GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model(list_view));
1283 gchar *name, *protocol, *server;
1284 gboolean has_getallbox;
1285 gboolean getall;
1287 name = ac_prefs->account_name;
1288 #ifdef USE_GNUTLS
1289 protocol = ac_prefs->protocol == A_POP3 ?
1290 (ac_prefs->ssl_pop == SSL_TUNNEL ?
1291 "POP (SSL/TLS)" :
1292 ac_prefs->ssl_pop == SSL_STARTTLS ?
1293 "POP (STARTTLS)" : "POP") :
1294 ac_prefs->protocol == A_IMAP4 ?
1295 (ac_prefs->ssl_imap == SSL_TUNNEL ?
1296 "IMAP (SSL/TLS)" :
1297 ac_prefs->ssl_imap == SSL_STARTTLS ?
1298 "IMAP (STARTTLS)" : "IMAP") :
1299 ac_prefs->protocol == A_NNTP ?
1300 (ac_prefs->ssl_nntp == SSL_TUNNEL ?
1301 "NNTP (SSL/TLS)" : "NNTP") :
1302 ac_prefs->protocol == A_LOCAL ? "Local" :
1303 ac_prefs->protocol == A_NONE ? "SMTP" : "-";
1304 #else
1305 protocol = ac_prefs->protocol == A_POP3 ? "POP" :
1306 ac_prefs->protocol == A_IMAP4 ? "IMAP" :
1307 ac_prefs->protocol == A_LOCAL ? "Local" :
1308 ac_prefs->protocol == A_NNTP ? "NNTP" :
1309 ac_prefs->protocol == A_NONE ? "SMTP" : "-";
1310 #endif
1311 server= ac_prefs->protocol == A_NNTP ? ac_prefs->nntp_server :
1312 ac_prefs->protocol == A_LOCAL ? "-" :
1313 ac_prefs->protocol == A_NONE ? ac_prefs->smtp_server :
1314 ac_prefs->recv_server;
1316 has_getallbox = (ac_prefs->protocol == A_POP3 ||
1317 ac_prefs->protocol == A_IMAP4 ||
1318 ac_prefs->protocol == A_NNTP ||
1319 ac_prefs->protocol == A_LOCAL);
1320 getall = has_getallbox && ac_prefs->recv_at_getall;
1322 account_list_view_insert_account_item(list_store,
1323 name, protocol, server,
1324 ac_prefs->is_default,
1325 getall, ac_prefs);
1326 return;
1329 static void account_list_view_set(void)
1331 GList *cur;
1332 gint prev_sel_account;
1333 GtkListStore *store;
1335 store = GTK_LIST_STORE(gtk_tree_view_get_model
1336 (GTK_TREE_VIEW(edit_account.list_view)));
1338 prev_sel_account = account_list_view_get_selected_account_id
1339 (edit_account.list_view);
1341 gtk_list_store_clear(store);
1343 for (cur = account_list; cur != NULL; cur = cur->next) {
1344 account_list_view_add((PrefsAccount *)cur->data);
1345 if ((PrefsAccount *)cur->data == cur_account)
1346 account_list_view_select_account
1347 (edit_account.list_view,
1348 cur_account->account_id);
1351 if (prev_sel_account >= 0)
1352 account_list_view_select_account(edit_account.list_view,
1353 prev_sel_account);
1356 /* set account list from CList */
1357 static void account_list_set(void)
1359 /* want to make sure we iterate *IN ORDER*, so therefore using
1360 * gtk_tree_model_XXXX_nth_child() */
1361 gint row, n_rows;
1362 PrefsAccount *ac_prefs;
1363 GtkTreeModel *model = gtk_tree_view_get_model
1364 (GTK_TREE_VIEW(edit_account.list_view));
1366 while (account_list)
1367 account_list = g_list_remove(account_list, account_list->data);
1369 n_rows = gtk_tree_model_iter_n_children(model, NULL);
1371 for (row = 0; row < n_rows; row++) {
1372 GtkTreeIter iter;
1374 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
1375 g_warning("%s(%d) - no iter found???", __FILE__, __LINE__);
1376 continue;
1379 ac_prefs = NULL;
1380 gtk_tree_model_get(model, &iter,
1381 ACCOUNT_DATA, &ac_prefs,
1382 -1);
1383 if (ac_prefs)
1384 account_list = g_list_append(account_list, ac_prefs);
1389 *\brief finds the PrefsAccounts which should be used to answer a mail
1391 *\param msginfo The message to be answered
1392 *\param reply_autosel Indicates whether reply account autoselection is on
1394 *\return PrefsAccount * the correct account, NULL if not found
1396 PrefsAccount *account_get_reply_account(MsgInfo *msginfo, gboolean reply_autosel)
1398 PrefsAccount *account = NULL;
1399 /* select the account set in folderitem's property (if enabled) */
1400 if (msginfo->folder->prefs && msginfo->folder->prefs->enable_default_account)
1401 account = account_find_from_id(msginfo->folder->prefs->default_account);
1402 else if (folder_has_parent_of_type(msginfo->folder, F_QUEUE) ||
1403 folder_has_parent_of_type(msginfo->folder, F_OUTBOX) ||
1404 folder_has_parent_of_type(msginfo->folder, F_DRAFT)) {
1405 gchar from[BUFFSIZE];
1406 if (!procheader_get_header_from_msginfo
1407 (msginfo, from, sizeof from, "From:")) {
1408 gchar *buf = from + strlen("From:");
1409 extract_address(buf);
1410 account = account_find_from_address(buf, FALSE);
1413 /* select account by to: and cc: header if enabled */
1414 if (reply_autosel) {
1415 gchar * field = NULL;
1416 int fieldno = 0;
1417 for (field = msginfo->to; fieldno++ < 2; field = msginfo->cc) {
1418 if (!account && field) {
1419 gchar *f = g_strdup(field);
1420 if (f) {
1421 gchar *cur = f;
1422 gchar *next = NULL;
1423 gchar *to = NULL;
1424 do {
1425 next = strchr_with_skip_quote(cur, '"', ',');
1426 if (next)
1427 (*next) = 0;
1428 Xstrdup_a(to, cur, return NULL);
1429 extract_address(to);
1430 account = account_find_from_address(to, FALSE);
1431 if (next)
1432 cur = next + 1;
1433 else
1434 break;
1435 } while (!account);
1436 g_free(f);
1440 if (!account) {
1441 gchar deliveredto[BUFFSIZE];
1442 if (!procheader_get_header_from_msginfo
1443 (msginfo, deliveredto,sizeof deliveredto , "Delivered-To:")) {
1444 gchar *buf = deliveredto + strlen("Delivered-To:");
1445 extract_address(buf);
1446 account = account_find_from_address(buf, FALSE);
1451 /* select the account for the whole folder (IMAP / NNTP) */
1452 if (!account)
1453 /* FIXME: this is not right, because folder may be nested. we should
1454 * ascend the tree until we find a parent with proper account
1455 * information */
1456 account = msginfo->folder->folder->account;
1458 /* select current account */
1459 if (!account) account = cur_account;
1461 return account;
1465 *\brief Create data store
1467 static GtkListStore* account_create_data_store(void)
1469 return gtk_list_store_new(N_ACCOUNT_COLUMNS,
1470 G_TYPE_INT, /* ACCOUNT_IS_DEFAULT */
1471 G_TYPE_BOOLEAN, /* ACCOUNT_ENABLE_GET_ALL */
1472 G_TYPE_STRING, /* ACCOUNT_NAME */
1473 G_TYPE_STRING, /* ACCOUNT_PROTOCOL */
1474 G_TYPE_STRING, /* ACCOUNT_SERVER */
1475 G_TYPE_POINTER, /* ACCOUNT_DATA */
1476 -1);
1480 *\brief Insert an account item in the list.
1482 *\return GtkTreeRowReference * A tree row reference, which is guaranteed to
1483 * stable whatever operations are performed on the list.
1485 static void account_list_view_insert_account_item(GtkListStore *list_store,
1486 const gchar *account_name,
1487 const gchar *protocol,
1488 const gchar *server_name,
1489 gboolean is_default,
1490 gboolean is_get_all,
1491 PrefsAccount *account_data)
1493 GtkTreeIter iter;
1495 gtk_list_store_append(list_store, &iter);
1496 gtk_list_store_set(list_store, &iter,
1497 ACCOUNT_IS_DEFAULT, is_default ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL,
1498 ACCOUNT_ENABLE_GET_ALL, is_get_all,
1499 ACCOUNT_NAME, account_name,
1500 ACCOUNT_PROTOCOL, protocol,
1501 ACCOUNT_SERVER, server_name,
1502 ACCOUNT_DATA, account_data,
1503 -1);
1507 *\brief Create and set up account list view, including tasks like
1508 * creating the data store (\ref account_create_data_store()),
1509 * and setting up the account list's individual columns (\ref
1510 * account_create_list_view_columns()).
1512 *\return GtkWidget * The created list view widget.
1514 static GtkWidget *account_list_view_create(void)
1516 GtkTreeView *list_view;
1517 GtkTreeSelection *selector;
1518 GtkListStore *store = account_create_data_store();
1520 list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)));
1521 g_object_unref(G_OBJECT(store));
1522 #ifdef GENERIC_UMPC
1523 g_object_set(list_view, "allow-checkbox-mode", FALSE, NULL);
1524 #endif
1525 gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1526 gtk_tree_view_set_reorderable(list_view, TRUE);
1528 selector = gtk_tree_view_get_selection(list_view);
1529 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1531 /* create the columns */
1532 account_create_list_view_columns(GTK_WIDGET(list_view));
1534 /* set a double click listener */
1535 g_signal_connect(G_OBJECT(list_view), "row_activated",
1536 G_CALLBACK(account_double_clicked),
1537 list_view);
1539 g_signal_connect(G_OBJECT(list_view), "drag_begin",
1540 G_CALLBACK(drag_begin),
1541 list_view);
1543 g_signal_connect(G_OBJECT(list_view), "drag_end",
1544 G_CALLBACK(drag_end),
1545 list_view);
1547 gtk_tree_view_set_reorderable(list_view, TRUE);
1548 return GTK_WIDGET(list_view);
1551 static void account_create_list_view_columns(GtkWidget *list_view)
1553 GtkTreeViewColumn *column;
1554 GtkCellRenderer *renderer;
1556 renderer = gtk_cell_renderer_toggle_new();
1557 g_object_set(renderer,
1558 "radio", FALSE,
1559 "activatable", TRUE,
1560 NULL);
1561 column = gtk_tree_view_column_new_with_attributes
1562 (C_("Accounts List Get Column Name", "G"), renderer,
1563 "active", ACCOUNT_ENABLE_GET_ALL,
1564 NULL);
1565 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1566 gtk_tree_view_column_set_alignment (column, 0.5);
1567 CLAWS_SET_TIP(gtk_tree_view_column_get_widget(column),
1568 _("'Get Mail' retrieves mail from the checked accounts"));
1569 g_signal_connect(G_OBJECT(renderer), "toggled",
1570 G_CALLBACK(account_get_all_toggled),
1571 list_view);
1573 renderer = gtk_cell_renderer_text_new();
1574 column = gtk_tree_view_column_new_with_attributes
1575 (_("Name"), renderer,
1576 "text", ACCOUNT_NAME,
1577 "weight", ACCOUNT_IS_DEFAULT,
1578 NULL);
1579 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1581 renderer = gtk_cell_renderer_text_new();
1582 column = gtk_tree_view_column_new_with_attributes
1583 (_("Protocol"), renderer,
1584 "text", ACCOUNT_PROTOCOL,
1585 "weight", ACCOUNT_IS_DEFAULT,
1586 NULL);
1587 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1589 renderer = gtk_cell_renderer_text_new();
1590 column = gtk_tree_view_column_new_with_attributes
1591 (_("Server"), renderer,
1592 "text", ACCOUNT_SERVER,
1593 "weight", ACCOUNT_IS_DEFAULT,
1594 NULL);
1595 gtk_tree_view_set_search_column(GTK_TREE_VIEW(list_view), ACCOUNT_NAME);
1596 gtk_tree_view_set_search_equal_func(GTK_TREE_VIEW(list_view), account_search_func_cb , NULL, NULL);
1597 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1601 *\brief Get currently selected account (by its unique ID)
1603 static gint account_list_view_get_selected_account_id(GtkWidget *list_view)
1605 GtkTreeSelection *selector;
1606 GtkTreeModel *model;
1607 GtkTreeIter iter;
1608 PrefsAccount *res = NULL;
1610 selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
1612 if (!gtk_tree_selection_get_selected(selector, &model, &iter))
1613 return -1;
1615 gtk_tree_model_get(model, &iter, ACCOUNT_DATA, &res, -1);
1617 return res->account_id;
1621 *\brief Get the tree path of the currently selected account
1623 static GtkTreePath *account_list_view_get_selected_account_path(GtkWidget *list_view)
1625 GtkTreeSelection *selector;
1626 GtkTreeModel *model;
1627 GtkTreeIter iter;
1629 selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
1631 if (!gtk_tree_selection_get_selected(selector, &model, &iter))
1632 return NULL;
1634 return gtk_tree_model_get_path(gtk_tree_view_get_model
1635 (GTK_TREE_VIEW(list_view)), &iter);
1639 *\brief Get the account data of the currently selected account
1641 static PrefsAccount *account_list_view_get_selected_account(GtkWidget *list_view)
1643 GtkTreeSelection *selector;
1644 GtkTreeModel *model;
1645 GtkTreeIter iter;
1646 PrefsAccount *res = NULL;
1648 selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
1650 if (!gtk_tree_selection_get_selected(selector, &model, &iter))
1651 return NULL;
1653 gtk_tree_model_get(model, &iter, ACCOUNT_DATA, &res, -1);
1655 return res;
1659 *\brief Select a row by the account it represents
1661 *\return gboolean TRUE if found and selected, FALSE if not.
1663 static gboolean account_list_view_select_account(GtkWidget *list_view, gint account_id)
1665 FindAccountInStore fis;
1666 GtkTreeModel *model;
1668 fis.account_id = account_id;
1669 fis.path = NULL;
1671 model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1673 gtk_tree_model_foreach(model, (GtkTreeModelForeachFunc) find_account_in_store,
1674 &fis);
1676 if (fis.path) {
1677 GtkTreeSelection *selection;
1678 GtkTreePath* path;
1680 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
1681 gtk_tree_selection_select_iter(selection, &fis.iter);
1682 path = gtk_tree_model_get_path(model, &fis.iter);
1683 /* XXX returned path may not be valid??? create new one to be sure */
1684 gtk_tree_view_set_cursor(GTK_TREE_VIEW(list_view), path, NULL, FALSE);
1685 gtk_tree_path_free(path);
1688 return fis.path != NULL;
1692 *\brief Set a new default account by its ID. (There is only one
1693 * default account.)
1695 static void account_list_view_set_default_by_id(GtkWidget *list_view,
1696 gint account_id)
1698 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1700 gtk_tree_model_foreach
1701 (model, (GtkTreeModelForeachFunc) set_new_default_account,
1702 &account_id);
1705 static gboolean set_new_default_account(GtkTreeModel *model,
1706 GtkTreePath *path,
1707 GtkTreeIter *iter,
1708 gint *account_id)
1710 PrefsAccount *account = NULL;
1711 PangoWeight weight;
1713 gtk_tree_model_get(model, iter,
1714 ACCOUNT_DATA, &account,
1715 -1);
1717 if (*account_id == account->account_id)
1718 weight = PANGO_WEIGHT_NORMAL;
1719 else
1720 weight = PANGO_WEIGHT_BOLD;
1722 gtk_list_store_set(GTK_LIST_STORE(model), iter,
1723 ACCOUNT_IS_DEFAULT, weight, -1);
1725 return FALSE;
1728 static gboolean find_account_in_store(GtkTreeModel *model,
1729 GtkTreePath *path,
1730 GtkTreeIter *iter,
1731 FindAccountInStore *data)
1733 PrefsAccount *account = NULL;
1734 gtk_tree_model_get(model, iter, ACCOUNT_DATA, &account, -1);
1736 if (data->account_id == account->account_id) {
1737 data->path = path; /* signal we found it */
1738 data->iter = *iter;
1739 return TRUE;
1742 return FALSE;
1746 *\brief Triggered when "get all" column is activated or de-activated
1748 static void account_get_all_toggled(GtkCellRendererToggle *widget,
1749 gchar *path,
1750 GtkWidget *list_view)
1752 GtkTreeIter iter;
1753 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1754 PrefsAccount *ac = NULL;
1755 gboolean get_all;
1757 if (!gtk_tree_model_get_iter_from_string(model, &iter, path))
1758 return;
1760 gtk_tree_model_get(model, &iter,
1761 ACCOUNT_DATA, &ac,
1762 ACCOUNT_ENABLE_GET_ALL, &get_all,
1763 -1);
1765 /* check if the account has a selectable get all checkbox anyway... */
1766 if (!(ac->protocol == A_POP3 ||
1767 ac->protocol == A_IMAP4 ||
1768 ac->protocol == A_NNTP ||
1769 ac->protocol == A_LOCAL))
1770 return;
1772 /* set value in store */
1773 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
1774 ACCOUNT_ENABLE_GET_ALL, !get_all,
1775 -1);
1777 /* set value in account */
1778 ac->recv_at_getall ^= TRUE;
1781 static void account_double_clicked(GtkTreeView *list_view,
1782 GtkTreePath *path,
1783 GtkTreeViewColumn *column,
1784 gpointer data)
1786 account_edit_prefs(NULL, NULL);
1789 static void drag_begin(GtkTreeView *list_view,
1790 GdkDragContext *context,
1791 gpointer data)
1793 /* XXX unfortunately a completed drag & drop does not emit
1794 * a "rows_reordered" signal, but a "row_changed" signal.
1795 * So during drag and drop, listen to "row_changed", and
1796 * update the account list accordingly */
1798 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1799 g_signal_connect(G_OBJECT(model), "row_changed",
1800 G_CALLBACK(account_row_changed_while_drag_drop),
1801 list_view);
1804 static void drag_end(GtkTreeView *list_view,
1805 GdkDragContext *context,
1806 gpointer data)
1808 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1809 g_signal_handlers_disconnect_by_func(G_OBJECT(model),
1810 G_CALLBACK(account_row_changed_while_drag_drop),
1811 list_view);
1814 static void account_row_changed_while_drag_drop(GtkTreeModel *model,
1815 GtkTreePath *path,
1816 GtkTreeIter *iter,
1817 gpointer arg3,
1818 GtkTreeView *list_view)
1820 account_list_set();
1823 gchar *account_get_signature_str(PrefsAccount *account)
1825 gchar *sig_body = NULL;
1826 gchar *sig_str = NULL;
1827 gchar *utf8_sig_str = NULL;
1829 cm_return_val_if_fail(account != NULL, NULL);
1831 if (!account->sig_path)
1832 return NULL;
1834 if (account->sig_type == SIG_FILE) {
1835 if (!is_file_or_fifo_exist(account->sig_path)) {
1836 g_warning("can't open signature file: '%s'",
1837 account->sig_path);
1838 return NULL;
1842 if (account->sig_type == SIG_COMMAND)
1843 sig_body = get_command_output(account->sig_path);
1844 else {
1845 gchar *tmp;
1847 tmp = file_read_to_str(account->sig_path);
1848 if (!tmp)
1849 return NULL;
1850 sig_body = normalize_newlines(tmp);
1851 g_free(tmp);
1854 if (account->sig_sep) {
1855 sig_str = g_strconcat("\n", account->sig_sep, "\n", sig_body,
1856 NULL);
1857 g_free(sig_body);
1858 } else
1859 sig_str = g_strconcat("\n", sig_body, NULL);
1861 if (sig_str) {
1862 if (g_utf8_validate(sig_str, -1, NULL) == TRUE)
1863 utf8_sig_str = sig_str;
1864 else {
1865 utf8_sig_str = conv_codeset_strdup
1866 (sig_str, conv_get_locale_charset_str_no_utf8(),
1867 CS_INTERNAL);
1868 g_free(sig_str);
1872 return utf8_sig_str;
1875 PrefsAccount *account_get_cur_account (void)
1877 return cur_account;
1880 gboolean password_get(const gchar *user,
1881 const gchar *server,
1882 const gchar *protocol,
1883 guint16 port,
1884 gchar **password)
1886 PasswordRequest req;
1888 /* all have to be set */
1889 cm_return_val_if_fail(user != NULL, FALSE);
1890 cm_return_val_if_fail(server != NULL, FALSE);
1891 cm_return_val_if_fail(protocol != NULL, FALSE);
1892 cm_return_val_if_fail(port != 0, FALSE);
1894 req.user = user;
1895 req.server = server;
1896 req.protocol = protocol;
1897 req.port = port;
1899 if (hooks_invoke(PASSWORD_GET_HOOKLIST, &req)) {
1900 *password = req.password;
1901 return TRUE;
1903 return FALSE;