2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2005 Hiroyuki Yamamoto
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 2 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <glib/gi18n.h>
29 #include <gdk/gdkkeysyms.h>
34 #include "mainwindow.h"
35 #include "folderview.h"
38 #include "prefs_gtk.h"
39 #include "prefs_account.h"
40 #include "prefs_common.h"
41 #include "folder_item_prefs.h"
43 #include "manage_window.h"
44 #include "stock_pixmap.h"
48 #include "alertpanel.h"
49 #include "procheader.h"
50 #include "customheader.h"
51 #include "remotefolder.h"
54 ACCOUNT_IS_DEFAULT, /* GDK_TYPE_PIXMAP! */
55 ACCOUNT_ENABLE_GET_ALL,
71 } EditAccountColumnPos;
73 # define N_EDIT_ACCOUNT_COLS 5
75 #define PREFSBUFSIZE 1024
77 PrefsAccount *cur_account;
79 static GList *account_list = NULL;
81 static struct EditAccount {
87 static GdkPixbuf *mark_pixbuf;
89 static void account_edit_create (void);
91 static void account_edit_prefs (GtkWidget *widget, gpointer data);
92 static void account_delete (GtkWidget *widget, gpointer data);
93 static void account_clone (GtkWidget *widget, gpointer data);
95 static void account_up (GtkWidget *widget, gpointer data);
96 static void account_down (GtkWidget *widget, gpointer data);
98 static void account_set_default (GtkWidget *widget, gpointer data);
100 static void account_edit_close (GtkWidget *widget, gpointer data);
102 static gint account_delete_event (GtkWidget *widget,
105 static void account_size_allocate_cb(GtkWidget *widget,
106 GtkAllocation *allocation);
107 static gboolean account_key_pressed (GtkWidget *widget,
111 static void account_list_view_add (PrefsAccount *ac_prefs);
112 static void account_list_view_set (void);
114 static void account_list_set (void);
116 typedef struct FindAccountInStore {
120 } FindAccountInStore;
122 static GtkListStore* account_create_data_store (void);
124 static void account_list_view_insert_account_item (GtkListStore *list_store,
125 const gchar *account_name,
126 const gchar *protocol,
127 const gchar *server_name,
130 PrefsAccount *account_data);
132 static GtkWidget *account_list_view_create (void);
133 static void account_create_list_view_images (GtkWidget *list_view);
134 static void account_create_list_view_columns (GtkWidget *list_view);
136 static gint account_list_view_get_selected_account_id (GtkWidget *list_view);
137 GtkTreePath *account_list_view_get_selected_account_path (GtkWidget *list_view);
138 PrefsAccount *account_list_view_get_selected_account (GtkWidget *list_view);
139 gboolean account_list_view_select_account (GtkWidget *list_view,
142 static void account_list_view_set_default_by_id(GtkWidget *list_view,
145 static gboolean set_new_default_account (GtkTreeModel *model,
150 static gboolean find_account_in_store (GtkTreeModel *model,
153 FindAccountInStore *data);
155 static void account_get_all_toggled (GtkCellRendererToggle *widget,
157 GtkWidget *list_view);
159 static void account_double_clicked (GtkTreeView *list_view,
161 GtkTreeViewColumn *column,
164 static void drag_begin (GtkTreeView *list_view,
165 GdkDragContext *context,
168 static void drag_end (GtkTreeView *list_view,
169 GdkDragContext *context,
172 static void account_row_changed_while_drag_drop (GtkTreeModel *model,
176 GtkTreeView *list_view);
178 void account_read_config_all(void)
180 GSList *ac_label_list = NULL, *cur;
183 gchar buf[PREFSBUFSIZE];
184 PrefsAccount *ac_prefs;
186 debug_print("Reading all config for each account...\n");
188 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACCOUNT_RC, NULL);
189 if ((fp = g_fopen(rcpath, "rb")) == NULL) {
190 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
196 while (fgets(buf, sizeof(buf), fp) != NULL) {
197 if (!strncmp(buf, "[Account: ", 10)) {
199 memmove(buf, buf + 1, strlen(buf));
200 buf[strlen(buf) - 1] = '\0';
201 debug_print("Found label: %s\n", buf);
202 ac_label_list = g_slist_append(ac_label_list,
208 /* read config data from file */
210 for (cur = ac_label_list; cur != NULL; cur = cur->next) {
211 ac_prefs = prefs_account_new();
212 prefs_account_read_config(ac_prefs, (gchar *)cur->data);
213 account_list = g_list_append(account_list, ac_prefs);
214 if (ac_prefs->is_default)
215 cur_account = ac_prefs;
217 /* if default is not set, assume first account as default */
218 if (!cur_account && account_list) {
219 ac_prefs = (PrefsAccount *)account_list->data;
220 account_set_as_default(ac_prefs);
221 cur_account = ac_prefs;
225 main_window_reflect_prefs_all();
227 while (ac_label_list) {
228 g_free(ac_label_list->data);
229 ac_label_list = g_slist_remove(ac_label_list,
230 ac_label_list->data);
234 void account_write_config_all(void)
236 prefs_account_write_config_all(account_list);
240 * account_find_all_from_address:
241 * @ac_list: initial list of accounts. NULL to create a new one.
242 * Accounts found in the @address will be appended to this list.
243 * @address: Email address string.
245 * Find all the mail (not news) accounts within the specified address.
247 * Return value: the original accounts list with the found accounts appended.
249 GList *account_find_all_from_address(GList *ac_list, const gchar *address)
257 for (cur = account_list; cur != NULL; cur = cur->next) {
258 ac = (PrefsAccount *)cur->data;
259 if (ac->protocol != A_NNTP && ac->address &&
260 strcasestr(address, ac->address) != NULL)
261 ac_list = g_list_append(ac_list, ac);
266 PrefsAccount *account_find_from_smtp_server(const gchar *address,
267 const gchar *smtp_server)
272 g_return_val_if_fail(address != NULL, NULL);
273 g_return_val_if_fail(smtp_server != NULL, NULL);
275 for (cur = account_list; cur != NULL; cur = cur->next) {
276 ac = (PrefsAccount *)cur->data;
277 if (!strcmp2(address, ac->address) &&
278 !strcmp2(smtp_server, ac->smtp_server))
286 * account_find_from_address:
287 * @address: Email address string.
289 * Find a mail (not news) account with the specified email address.
291 * Return value: The found account, or NULL if not found.
293 PrefsAccount *account_find_from_address(const gchar *address)
298 g_return_val_if_fail(address != NULL, NULL);
300 for (cur = account_list; cur != NULL; cur = cur->next) {
301 ac = (PrefsAccount *)cur->data;
302 if (ac->protocol != A_NNTP && ac->address &&
303 g_ascii_strcasecmp(address, ac->address) == 0)
310 PrefsAccount *account_find_from_id(gint id)
315 for (cur = account_list; cur != NULL; cur = cur->next) {
316 ac = (PrefsAccount *)cur->data;
317 if (id == ac->account_id)
324 PrefsAccount *account_find_from_item(FolderItem *item)
328 g_return_val_if_fail(item != NULL, NULL);
332 FolderItem *cur_item = folder_item_parent(item);
333 while (cur_item != NULL) {
334 if (cur_item->account && cur_item->apply_sub) {
335 ac = cur_item->account;
338 cur_item = folder_item_parent(cur_item);
342 ac = item->folder->account;
347 void account_set_menu(void)
349 main_window_set_account_menu(account_list);
352 void account_foreach(AccountFunc func, gpointer user_data)
356 for (cur = account_list; cur != NULL; cur = cur->next)
357 if (func((PrefsAccount *)cur->data, user_data) != 0)
361 GList *account_get_list(void)
366 void account_edit_open(void)
370 if (compose_get_compose_list()) {
371 alertpanel_error(_("Some composing windows are open.\n"
372 "Please close all the composing "
373 "windows before editing the accounts."));
378 debug_print("Opening account edit window...\n");
380 if (!edit_account.window)
381 account_edit_create();
383 account_list_view_set();
385 manage_window_set_transient(GTK_WINDOW(edit_account.window));
386 gtk_widget_grab_focus(edit_account.close_btn);
387 gtk_widget_show(edit_account.window);
389 manage_window_focus_in(edit_account.window, NULL, NULL);
392 void account_add(void)
394 PrefsAccount *ac_prefs;
396 ac_prefs = prefs_account_open(NULL);
398 if (!ac_prefs) return;
400 account_list = g_list_append(account_list, ac_prefs);
402 if (ac_prefs->is_default)
403 account_set_as_default(ac_prefs);
405 account_list_view_set();
407 if (ac_prefs->protocol == A_IMAP4 || ac_prefs->protocol == A_NNTP) {
410 if (ac_prefs->protocol == A_IMAP4) {
411 folder = folder_new(folder_get_class_from_string("imap"), ac_prefs->account_name,
412 ac_prefs->recv_server);
414 folder = folder_new(folder_get_class_from_string("news"), ac_prefs->account_name,
415 ac_prefs->nntp_server);
417 if (folder == NULL) {
418 alertpanel_error(_("Can't create folder."));
421 folder->account = ac_prefs;
422 ac_prefs->folder = folder;
424 if (ac_prefs->protocol == A_IMAP4)
425 folder->klass->create_tree(folder);
426 folderview_set_all();
430 void account_open(PrefsAccount *ac_prefs)
432 gboolean prev_default;
435 g_return_if_fail(ac_prefs != NULL);
437 prev_default = ac_prefs->is_default;
438 Xstrdup_a(ac_name, ac_prefs->account_name ? ac_prefs->account_name : "",
441 prefs_account_open(ac_prefs);
443 if (!prev_default && ac_prefs->is_default)
444 account_set_as_default(ac_prefs);
446 if (ac_prefs->folder && strcmp2(ac_name, ac_prefs->account_name) != 0) {
447 folder_set_name(FOLDER(ac_prefs->folder),
448 ac_prefs->account_name);
449 folderview_set_all();
452 account_write_config_all();
454 main_window_reflect_prefs_all();
457 void account_set_as_default(PrefsAccount *ac_prefs)
462 for (cur = account_list; cur != NULL; cur = cur->next) {
463 ap = (PrefsAccount *)cur->data;
465 ap->is_default = FALSE;
468 ac_prefs->is_default = TRUE;
471 PrefsAccount *account_get_default(void)
476 for (cur = account_list; cur != NULL; cur = cur->next) {
477 ap = (PrefsAccount *)cur->data;
485 void account_set_missing_folder(void)
490 for (cur = account_list; cur != NULL; cur = cur->next) {
491 ap = (PrefsAccount *)cur->data;
492 if ((ap->protocol == A_IMAP4 || ap->protocol == A_NNTP) &&
496 if (ap->protocol == A_IMAP4) {
497 folder = folder_new(folder_get_class_from_string("imap"), ap->account_name,
500 folder = folder_new(folder_get_class_from_string("news"), ap->account_name,
505 folder->account = ap;
508 if (ap->protocol == A_IMAP4)
509 folder->klass->create_tree(folder);
514 FolderItem *account_get_special_folder(PrefsAccount *ac_prefs,
515 SpecialFolderItemType type)
517 FolderItem *item = NULL;
519 g_return_val_if_fail(ac_prefs != NULL, NULL);
523 if (ac_prefs->folder)
524 item = FOLDER(ac_prefs->folder)->inbox;
526 item = folder_get_default_inbox();
529 if (ac_prefs->set_sent_folder && ac_prefs->sent_folder) {
530 item = folder_find_item_from_identifier
531 (ac_prefs->sent_folder);
534 if (ac_prefs->folder)
535 item = FOLDER(ac_prefs->folder)->outbox;
537 item = folder_get_default_outbox();
541 if (ac_prefs->set_draft_folder && ac_prefs->draft_folder) {
542 item = folder_find_item_from_identifier
543 (ac_prefs->draft_folder);
546 if (ac_prefs->folder)
547 item = FOLDER(ac_prefs->folder)->draft;
549 item = folder_get_default_draft();
553 if (ac_prefs->set_queue_folder && ac_prefs->queue_folder) {
554 item = folder_find_item_from_identifier
555 (ac_prefs->queue_folder);
558 if (ac_prefs->folder)
559 item = FOLDER(ac_prefs->folder)->queue;
561 item = folder_get_default_queue();
565 if (ac_prefs->set_trash_folder && ac_prefs->trash_folder) {
566 item = folder_find_item_from_identifier
567 (ac_prefs->trash_folder);
570 if (ac_prefs->folder)
571 item = FOLDER(ac_prefs->folder)->trash;
573 item = folder_get_default_trash();
583 void account_destroy(PrefsAccount *ac_prefs)
585 g_return_if_fail(ac_prefs != NULL);
587 folder_unref_account_all(ac_prefs);
589 prefs_account_free(ac_prefs);
590 account_list = g_list_remove(account_list, ac_prefs);
592 if (cur_account == ac_prefs) cur_account = NULL;
593 if (!cur_account && account_list) {
594 cur_account = account_get_default();
596 ac_prefs = (PrefsAccount *)account_list->data;
597 account_set_as_default(ac_prefs);
598 cur_account = ac_prefs;
604 *\brief Save Gtk object size to prefs dataset
606 static void account_size_allocate_cb(GtkWidget *widget,
607 GtkAllocation *allocation)
609 g_return_if_fail(allocation != NULL);
611 prefs_common.accountswin_width = allocation->width;
612 prefs_common.accountswin_height = allocation->height;
615 static void account_edit_create(void)
621 GtkWidget *scrolledwin;
622 GtkWidget *list_view;
628 GtkWidget *clone_btn;
632 GtkWidget *default_btn;
635 GtkWidget *close_btn;
637 static GdkGeometry geometry;
639 debug_print("Creating account edit window...\n");
641 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
642 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
643 gtk_window_set_title (GTK_WINDOW (window), _("Edit accounts"));
644 gtk_window_set_modal (GTK_WINDOW (window), TRUE);
645 g_signal_connect (G_OBJECT (window), "delete_event",
646 G_CALLBACK (account_delete_event), NULL);
647 g_signal_connect (G_OBJECT (window), "key_press_event",
648 G_CALLBACK (account_key_pressed), NULL);
649 MANAGE_WINDOW_SIGNALS_CONNECT (window);
650 gtk_widget_realize(window);
652 vbox = gtk_vbox_new (FALSE, 10);
653 gtk_widget_show (vbox);
654 gtk_container_add (GTK_CONTAINER (window), vbox);
656 hbox = gtk_hbox_new (FALSE, 0);
657 gtk_widget_show (hbox);
658 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
660 label = gtk_label_new
661 (_("New messages will be checked in this order. Check the boxes\n"
662 "on the 'G' column to enable message retrieval by 'Get all'."));
663 gtk_widget_show (label);
664 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 4);
665 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
667 hbox = gtk_hbox_new (FALSE, 8);
668 gtk_widget_show (hbox);
669 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
670 gtk_container_set_border_width (GTK_CONTAINER (hbox), 2);
672 scrolledwin = gtk_scrolled_window_new (NULL, NULL);
673 gtk_widget_show (scrolledwin);
674 gtk_box_pack_start (GTK_BOX (hbox), scrolledwin, TRUE, TRUE, 0);
675 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwin),
676 GTK_POLICY_AUTOMATIC,
677 GTK_POLICY_AUTOMATIC);
679 list_view = account_list_view_create();
680 gtk_widget_show(list_view);
681 gtk_container_add(GTK_CONTAINER(scrolledwin), list_view);
683 vbox2 = gtk_vbox_new (FALSE, 0);
684 gtk_widget_show (vbox2);
685 gtk_box_pack_start (GTK_BOX (hbox), vbox2, FALSE, FALSE, 0);
687 add_btn = gtk_button_new_from_stock(GTK_STOCK_NEW);
688 gtk_widget_show (add_btn);
689 gtk_box_pack_start (GTK_BOX (vbox2), add_btn, FALSE, FALSE, 4);
690 g_signal_connect (G_OBJECT(add_btn), "clicked",
691 G_CALLBACK (account_add), NULL);
693 #if GTK_CHECK_VERSION(2, 6, 0)
694 edit_btn = gtk_button_new_from_stock (GTK_STOCK_EDIT);
696 edit_btn = gtk_button_new_from_stock(GTK_STOCK_PROPERTIES);
698 gtk_widget_show (edit_btn);
699 gtk_box_pack_start (GTK_BOX (vbox2), edit_btn, FALSE, FALSE, 4);
700 g_signal_connect (G_OBJECT(edit_btn), "clicked",
701 G_CALLBACK (account_edit_prefs), NULL);
703 del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
704 gtk_widget_show (del_btn);
705 gtk_box_pack_start (GTK_BOX (vbox2), del_btn, FALSE, FALSE, 4);
706 g_signal_connect (G_OBJECT(del_btn), "clicked",
707 G_CALLBACK (account_delete), NULL);
709 clone_btn = gtk_button_new_from_stock(GTK_STOCK_COPY);
710 gtk_widget_show (clone_btn);
711 gtk_box_pack_start (GTK_BOX (vbox2), clone_btn, FALSE, FALSE, 4);
712 g_signal_connect(G_OBJECT(clone_btn), "clicked",
713 G_CALLBACK(account_clone), NULL);
715 down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
716 gtk_widget_show (down_btn);
717 gtk_box_pack_end (GTK_BOX (vbox2), down_btn, FALSE, FALSE, 4);
718 g_signal_connect (G_OBJECT(down_btn), "clicked",
719 G_CALLBACK (account_down), NULL);
721 up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
722 gtk_widget_show (up_btn);
723 gtk_box_pack_end (GTK_BOX (vbox2), up_btn, FALSE, FALSE, 4);
724 g_signal_connect (G_OBJECT(up_btn), "clicked",
725 G_CALLBACK (account_up), NULL);
727 hbox = gtk_hbox_new (FALSE, 8);
728 gtk_widget_show (hbox);
729 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
731 vbox2 = gtk_vbox_new(FALSE, 0);
732 gtk_widget_show (vbox2);
733 gtk_box_pack_start (GTK_BOX (hbox), vbox2, FALSE, FALSE, 0);
735 default_btn = gtk_button_new_with_mnemonic
736 (_(" _Set as default account "));
737 gtk_widget_show (default_btn);
738 gtk_box_pack_start (GTK_BOX (vbox2), default_btn, TRUE, FALSE, 0);
739 g_signal_connect (G_OBJECT(default_btn), "clicked",
740 G_CALLBACK (account_set_default), NULL);
742 hbbox = gtk_hbutton_box_new();
743 gtk_button_box_set_layout(GTK_BUTTON_BOX(hbbox), GTK_BUTTONBOX_END);
744 gtk_box_set_spacing(GTK_BOX(hbbox), 5);
746 gtkut_stock_button_set_create(&hbbox, &close_btn, GTK_STOCK_CLOSE,
747 NULL, NULL, NULL, NULL);
748 gtk_widget_show(hbbox);
750 gtk_box_pack_end (GTK_BOX (hbox), hbbox, FALSE, FALSE, 0);
751 gtk_widget_grab_default (close_btn);
753 g_signal_connect (G_OBJECT (close_btn), "clicked",
754 G_CALLBACK (account_edit_close),
757 account_create_list_view_images(list_view);
759 g_signal_connect(G_OBJECT(window), "size_allocate",
760 G_CALLBACK(account_size_allocate_cb), NULL);
762 if (!geometry.min_height) {
763 geometry.min_width = 500;
764 geometry.min_height = 350;
767 gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
769 gtk_widget_set_size_request(window, prefs_common.accountswin_width,
770 prefs_common.accountswin_height);
772 edit_account.window = window;
773 edit_account.list_view = list_view;
774 edit_account.close_btn = close_btn;
777 static void account_edit_prefs(GtkWidget *widget, gpointer data)
779 PrefsAccount *ac_prefs;
781 ac_prefs = account_list_view_get_selected_account(edit_account.list_view);
784 account_open(ac_prefs);
785 account_list_view_set();
789 static gboolean account_delete_references_func(GNode *node, gpointer data)
794 g_return_val_if_fail(node->data != NULL, FALSE);
796 item = FOLDER_ITEM(node->data);
797 account = GPOINTER_TO_INT(data);
799 if(!item->prefs) /* && item->prefs->stype == F_NORMAL */
801 if(item->prefs->default_account != account)
804 item->prefs->enable_default_account = FALSE;
805 item->prefs->default_account = 0;
806 folder_item_prefs_save_config(item);
812 #define ACP_FDUP(fld) ac_clon->fld = ((ac_prefs->fld) != NULL)?\
813 g_strdup(ac_prefs->fld): NULL
814 #define ACP_FASSIGN(fld) ac_clon->fld = ac_prefs->fld
815 static void account_clone(GtkWidget *widget, gpointer data)
817 PrefsAccount *ac_prefs, *ac_clon;
819 CustomHeader *cch = NULL, *ch = NULL;
821 ac_prefs = account_list_view_get_selected_account(edit_account.list_view);
822 if (ac_prefs == NULL)
825 if (ac_prefs->protocol == A_IMAP4 || ac_prefs->protocol == A_NNTP) {
826 alertpanel_error(_("Accounts with remote folders cannot be copied."));
830 ac_clon = prefs_account_new();
832 ac_clon->account_name = g_strdup_printf(_("Copy of %s"),
833 ac_prefs->account_name);
837 ACP_FDUP(organization);
840 ACP_FASSIGN(protocol);
841 ACP_FDUP(recv_server);
842 ACP_FDUP(smtp_server);
843 ACP_FDUP(nntp_server);
844 ACP_FASSIGN(use_nntp_auth);
845 ACP_FASSIGN(use_nntp_auth_onconnect);
849 ACP_FDUP(local_mbox);
850 ACP_FASSIGN(use_mail_command);
851 ACP_FDUP(mail_command);
854 ACP_FASSIGN(ssl_pop);
855 ACP_FASSIGN(ssl_imap);
856 ACP_FASSIGN(ssl_nntp);
857 ACP_FASSIGN(ssl_smtp);
858 ACP_FASSIGN(use_nonblocking_ssl);
859 #endif /* USE_OPENSSL */
861 ac_clon->tmp_pass = NULL;
865 ACP_FASSIGN(msg_leave_time);
867 ACP_FASSIGN(recv_at_getall);
868 ACP_FASSIGN(sd_rmmail_on_download);
869 ACP_FASSIGN(sd_filter_on_recv);
870 ACP_FASSIGN(enable_size_limit);
871 ACP_FASSIGN(size_limit);
872 ACP_FASSIGN(filter_on_recv);
874 ACP_FASSIGN(max_articles);
876 ACP_FASSIGN(imap_auth_type);
879 ACP_FASSIGN(add_date);
880 ACP_FASSIGN(gen_msgid);
881 ACP_FASSIGN(add_customhdr);
882 ACP_FASSIGN(use_smtp_auth);
883 ACP_FASSIGN(smtp_auth_type);
884 ACP_FDUP(smtp_userid);
885 ACP_FDUP(smtp_passwd);
887 ac_clon->tmp_smtp_pass = NULL;
889 ACP_FASSIGN(pop_before_smtp);
890 ACP_FASSIGN(pop_before_smtp_timeout);
891 ACP_FASSIGN(last_pop_login_time);
893 ac_clon->customhdr_list = NULL;
894 hdrs = ac_prefs->customhdr_list;
895 while (hdrs != NULL) {
896 ch = (CustomHeader *)hdrs->data;
898 cch = g_new0(CustomHeader, 1);
899 cch->account_id = ac_clon->account_id;
900 cch->name = (ch->name != NULL) ? g_strdup(ch->name) : NULL;
901 cch->value = (ch->value != NULL) ? g_strdup(ch->value) : NULL;
903 ac_clon->customhdr_list = g_slist_append(ac_clon->customhdr_list, cch);
905 hdrs = g_slist_next(hdrs);
909 ACP_FASSIGN(sig_type);
911 ACP_FASSIGN(auto_sig);
913 ACP_FASSIGN(set_autocc);
915 ACP_FASSIGN(set_autobcc);
917 ACP_FASSIGN(set_autoreplyto);
918 ACP_FDUP(auto_replyto);
921 ACP_FASSIGN(default_encrypt);
922 ACP_FASSIGN(default_sign);
925 ACP_FASSIGN(set_smtpport);
926 ACP_FASSIGN(smtpport);
927 ACP_FASSIGN(set_popport);
928 ACP_FASSIGN(popport);
929 ACP_FASSIGN(set_imapport);
930 ACP_FASSIGN(imapport);
931 ACP_FASSIGN(set_nntpport);
932 ACP_FASSIGN(nntpport);
933 ACP_FASSIGN(set_domain);
935 ACP_FASSIGN(mark_crosspost_read);
936 ACP_FASSIGN(crosspost_col);
938 ACP_FASSIGN(set_tunnelcmd);
943 ACP_FASSIGN(set_sent_folder);
944 ACP_FDUP(sent_folder);
945 ACP_FASSIGN(set_draft_folder);
946 ACP_FDUP(draft_folder);
947 ACP_FASSIGN(set_trash_folder);
948 ACP_FDUP(trash_folder);
949 /* don't want two default accounts */
950 ac_clon->is_default = FALSE;
953 account_list = g_list_append(account_list, ac_clon);
954 account_list_view_set();
959 static void account_delete(GtkWidget *widget, gpointer data)
961 PrefsAccount *ac_prefs;
966 ac_prefs = account_list_view_get_selected_account(edit_account.list_view);
967 if (ac_prefs == NULL)
970 g_snprintf(buf, sizeof(buf),
971 _("Do you really want to delete the account '%s'?"),
972 ac_prefs->account_name ? ac_prefs->account_name :
974 if (alertpanel_full(_("Delete account"), buf,
975 GTK_STOCK_YES, GTK_STOCK_NO, NULL, FALSE,
976 NULL, ALERT_WARNING, G_ALERTALTERNATE) != G_ALERTDEFAULT)
979 if (ac_prefs->folder) {
982 item = mainwindow_get_mainwindow()->summaryview->folder_item;
983 if (item && item->folder == FOLDER(ac_prefs->folder))
984 summary_clear_all(mainwindow_get_mainwindow()->summaryview);
985 folder_destroy(FOLDER(ac_prefs->folder));
986 folderview_set_all();
988 account_destroy(ac_prefs);
989 account_list_view_set();
991 debug_print("Removing deleted account references for all the folders...\n");
992 list = folder_get_list();
993 for (; list != NULL; list = list->next) {
994 folder = FOLDER(list->data);
995 if (folder->node) /* && folder->type == F_? */
996 g_node_traverse(folder->node, G_PRE_ORDER,
998 account_delete_references_func,
999 GINT_TO_POINTER(ac_prefs->account_id));
1003 static void account_up(GtkWidget *widget, gpointer data)
1005 GtkTreePath *sel = account_list_view_get_selected_account_path
1006 (edit_account.list_view),
1008 GtkTreeIter isel, iup;
1009 GtkTreeModel *model = gtk_tree_view_get_model
1010 (GTK_TREE_VIEW(edit_account.list_view));
1015 up = gtk_tree_path_copy(sel);
1017 gtk_tree_path_free(sel);
1021 if (!gtk_tree_path_prev(up)) {
1022 gtk_tree_path_free(up);
1023 gtk_tree_path_free(sel);
1027 if (!gtk_tree_model_get_iter(model, &isel, sel)
1028 || !gtk_tree_model_get_iter(model, &iup, up)) {
1029 gtk_tree_path_free(up);
1030 gtk_tree_path_free(sel);
1034 gtk_list_store_swap(GTK_LIST_STORE(model), &isel, &iup);
1038 gtk_tree_path_free(up);
1039 gtk_tree_path_free(sel);
1042 static void account_down(GtkWidget *widget, gpointer data)
1044 GtkTreePath *sel = account_list_view_get_selected_account_path
1045 (edit_account.list_view),
1047 GtkTreeIter isel, idn;
1048 GtkTreeModel *model = gtk_tree_view_get_model
1049 (GTK_TREE_VIEW(edit_account.list_view));
1054 dn = gtk_tree_path_copy(sel);
1056 gtk_tree_path_free(sel);
1060 /* XXX no check possible??? however, if down but at bottom, then
1061 * nothing seems to happen much anyway, so the following seems to
1063 gtk_tree_path_next(dn);
1065 if (!gtk_tree_model_get_iter(model, &isel, sel)
1066 || !gtk_tree_model_get_iter(model, &idn, dn)) {
1067 gtk_tree_path_free(dn);
1068 gtk_tree_path_free(sel);
1072 gtk_list_store_swap(GTK_LIST_STORE(model), &isel, &idn);
1076 gtk_tree_path_free(dn);
1077 gtk_tree_path_free(sel);
1080 static void account_set_default(GtkWidget *widget, gpointer data)
1082 PrefsAccount *ac_prefs;
1084 if (NULL == (ac_prefs = account_list_view_get_selected_account
1085 (edit_account.list_view)))
1088 /* we need to change the store variables by resetting everything
1089 * and setting the new default one */
1090 account_list_view_set_default_by_id(edit_account.list_view,
1091 ac_prefs->account_id);
1093 account_set_as_default(ac_prefs);
1094 account_list_view_set();
1096 cur_account = ac_prefs;
1098 main_window_reflect_prefs_all();
1101 static void account_edit_close(GtkWidget *widget, gpointer data)
1104 account_write_config_all();
1106 if (!cur_account && account_list) {
1107 PrefsAccount *ac_prefs = (PrefsAccount *)account_list->data;
1108 account_set_as_default(ac_prefs);
1109 cur_account = ac_prefs;
1113 main_window_reflect_prefs_all();
1115 gtk_widget_hide(edit_account.window);
1120 static gint account_delete_event(GtkWidget *widget, GdkEventAny *event,
1123 account_edit_close(NULL, NULL);
1127 static gboolean account_key_pressed(GtkWidget *widget, GdkEventKey *event,
1130 if (event && event->keyval == GDK_Escape)
1131 account_edit_close(NULL, NULL);
1135 static void account_list_view_add(PrefsAccount *ac_prefs)
1137 GtkTreeView *list_view = GTK_TREE_VIEW(edit_account.list_view);
1138 GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model(list_view));
1139 gchar *name, *protocol, *server;
1140 gboolean has_getallbox;
1143 name = ac_prefs->account_name;
1145 protocol = ac_prefs->protocol == A_POP3 ?
1146 (ac_prefs->ssl_pop == SSL_TUNNEL ?
1148 ac_prefs->ssl_pop == SSL_STARTTLS ?
1149 "POP3 (TLS)" : "POP3") :
1150 ac_prefs->protocol == A_IMAP4 ?
1151 (ac_prefs->ssl_imap == SSL_TUNNEL ?
1153 ac_prefs->ssl_imap == SSL_STARTTLS ?
1154 "IMAP4 (TLS)" : "IMAP4") :
1155 ac_prefs->protocol == A_NNTP ?
1156 (ac_prefs->ssl_nntp == SSL_TUNNEL ?
1157 "NNTP (SSL)" : "NNTP") :
1160 protocol = ac_prefs->protocol == A_POP3 ? "POP3" :
1161 ac_prefs->protocol == A_IMAP4 ? "IMAP4" :
1162 ac_prefs->protocol == A_LOCAL ? "Local" :
1163 ac_prefs->protocol == A_NNTP ? "NNTP" : "";
1165 server= ac_prefs->protocol == A_NNTP
1166 ? ac_prefs->nntp_server : ac_prefs->recv_server;
1168 has_getallbox = (ac_prefs->protocol == A_POP3 ||
1169 ac_prefs->protocol == A_IMAP4 ||
1170 ac_prefs->protocol == A_NNTP ||
1171 ac_prefs->protocol == A_LOCAL);
1172 getall = has_getallbox && ac_prefs->recv_at_getall;
1174 account_list_view_insert_account_item(list_store,
1175 name, protocol, server,
1176 ac_prefs->is_default,
1181 static void account_list_view_set(void)
1184 gint prev_sel_account;
1185 GtkListStore *store;
1187 store = GTK_LIST_STORE(gtk_tree_view_get_model
1188 (GTK_TREE_VIEW(edit_account.list_view)));
1190 prev_sel_account = account_list_view_get_selected_account_id
1191 (edit_account.list_view);
1193 gtk_list_store_clear(store);
1195 for (cur = account_list; cur != NULL; cur = cur->next) {
1196 account_list_view_add((PrefsAccount *)cur->data);
1197 if ((PrefsAccount *)cur->data == cur_account)
1198 account_list_view_select_account
1199 (edit_account.list_view,
1200 cur_account->account_id);
1203 if (prev_sel_account >= 0)
1204 account_list_view_select_account(edit_account.list_view,
1208 /* set account list from CList */
1209 static void account_list_set(void)
1211 /* want to make sure we iterate *IN ORDER*, so therefore using
1212 * gtk_tree_model_XXXX_nth_child() */
1214 PrefsAccount *ac_prefs;
1215 GtkTreeModel *model = gtk_tree_view_get_model
1216 (GTK_TREE_VIEW(edit_account.list_view));
1218 while (account_list)
1219 account_list = g_list_remove(account_list, account_list->data);
1221 n_rows = gtk_tree_model_iter_n_children(model, NULL);
1223 for (row = 0; row < n_rows; row++) {
1226 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
1227 g_warning("%s(%d) - no iter found???\n", __FILE__, __LINE__);
1232 gtk_tree_model_get(model, &iter,
1233 ACCOUNT_DATA, &ac_prefs,
1236 account_list = g_list_append(account_list, ac_prefs);
1241 *\brief finds the PrefsAccounts which should be used to answer a mail
1243 *\param msginfo The message to be answered
1244 *\param reply_autosel Indicates whether reply account autoselection is on
1246 *\return PrefsAccount * the correct account, NULL if not found
1248 PrefsAccount *account_get_reply_account(MsgInfo *msginfo, gboolean reply_autosel)
1250 PrefsAccount *account = NULL;
1251 /* select the account set in folderitem's property (if enabled) */
1252 if (msginfo->folder->prefs && msginfo->folder->prefs->enable_default_account)
1253 account = account_find_from_id(msginfo->folder->prefs->default_account);
1255 /* select account by to: and cc: header if enabled */
1256 if (reply_autosel) {
1257 gchar * field = NULL;
1259 for (field = msginfo->to; fieldno++ < 2; field = msginfo->cc) {
1260 if (!account && field) {
1262 if (!strchr(field, ',')) {
1263 Xstrdup_a(to, field, return NULL);
1264 extract_address(to);
1265 account = account_find_from_address(to);
1267 gchar **split = g_strsplit(field, ",", -1);
1273 Xstrdup_a(to, split[i], return NULL);
1274 extract_address(to);
1275 account = account_find_from_address(to);
1283 /* select the account for the whole folder (IMAP / NNTP) */
1285 /* FIXME: this is not right, because folder may be nested. we should
1286 * ascend the tree until we find a parent with proper account
1288 account = msginfo->folder->folder->account;
1290 /* select current account */
1291 if (!account) account = cur_account;
1297 *\brief Create data store
1299 static GtkListStore* account_create_data_store(void)
1301 return gtk_list_store_new(N_ACCOUNT_COLUMNS,
1302 GDK_TYPE_PIXBUF, /* ACCOUNT_IS_DEFAULT */
1303 G_TYPE_BOOLEAN, /* ACCOUNT_ENABLE_GET_ALL */
1304 G_TYPE_STRING, /* ACCOUNT_NAME */
1305 G_TYPE_STRING, /* ACCOUNT_PROTOCOL */
1306 G_TYPE_STRING, /* ACCOUNT_SERVER */
1307 G_TYPE_POINTER, /* ACCOUNT_DATA */
1312 *\brief Insert an account item in the list.
1314 *\return GtkTreeRowReference * A tree row reference, which is guaranteed to
1315 * stable whatever operations are performed on the list.
1317 static void account_list_view_insert_account_item(GtkListStore *list_store,
1318 const gchar *account_name,
1319 const gchar *protocol,
1320 const gchar *server_name,
1321 gboolean is_default,
1322 gboolean is_get_all,
1323 PrefsAccount *account_data)
1327 gtk_list_store_append(list_store, &iter);
1328 gtk_list_store_set(list_store, &iter,
1329 ACCOUNT_IS_DEFAULT, is_default ? mark_pixbuf : NULL,
1330 ACCOUNT_ENABLE_GET_ALL, is_get_all,
1331 ACCOUNT_NAME, account_name,
1332 ACCOUNT_PROTOCOL, protocol,
1333 ACCOUNT_SERVER, server_name,
1334 ACCOUNT_DATA, account_data,
1339 *\brief Create and set up account list view, including tasks like
1340 * creating the data store (\ref account_create_data_store()),
1341 * creating images for the list view (\ref account_create_list_view_images()),
1342 * and setting up the account list's individual columns (\ref
1343 * account_create_list_view_columns()).
1345 *\return GtkWidget * The created list view widget.
1347 static GtkWidget *account_list_view_create(void)
1349 GtkTreeView *list_view;
1350 GtkTreeSelection *selector;
1351 GtkListStore *store = account_create_data_store();
1353 list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)));
1354 g_object_unref(G_OBJECT(store));
1356 gtk_tree_view_set_rules_hint(list_view, prefs_common.enable_rules_hint);
1357 gtk_tree_view_set_reorderable(list_view, TRUE);
1359 selector = gtk_tree_view_get_selection(list_view);
1360 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1362 /* create the columns */
1363 account_create_list_view_columns(GTK_WIDGET(list_view));
1365 /* set a double click listener */
1366 g_signal_connect(G_OBJECT(list_view), "row_activated",
1367 G_CALLBACK(account_double_clicked),
1370 g_signal_connect(G_OBJECT(list_view), "drag_begin",
1371 G_CALLBACK(drag_begin),
1374 g_signal_connect(G_OBJECT(list_view), "drag_end",
1375 G_CALLBACK(drag_end),
1378 gtk_tree_view_set_reorderable(list_view, TRUE);
1379 return GTK_WIDGET(list_view);
1382 static void account_create_list_view_images(GtkWidget *list_view)
1384 stock_pixbuf_gdk(list_view, STOCK_PIXMAP_MARK, &mark_pixbuf);
1387 static void account_create_list_view_columns(GtkWidget *list_view)
1389 GtkTreeViewColumn *column;
1390 GtkCellRenderer *renderer;
1392 renderer = gtk_cell_renderer_pixbuf_new();
1393 column = gtk_tree_view_column_new_with_attributes
1395 "pixbuf", ACCOUNT_IS_DEFAULT,
1397 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1399 renderer = gtk_cell_renderer_toggle_new();
1400 g_object_set(renderer,
1402 "activatable", TRUE,
1404 column = gtk_tree_view_column_new_with_attributes
1406 "active", ACCOUNT_ENABLE_GET_ALL,
1408 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1409 g_signal_connect(G_OBJECT(renderer), "toggled",
1410 G_CALLBACK(account_get_all_toggled),
1413 renderer = gtk_cell_renderer_text_new();
1414 column = gtk_tree_view_column_new_with_attributes
1415 (_("Name"), renderer,
1416 "text", ACCOUNT_NAME,
1418 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1420 renderer = gtk_cell_renderer_text_new();
1421 column = gtk_tree_view_column_new_with_attributes
1422 (_("Protocol"), renderer,
1423 "text", ACCOUNT_PROTOCOL,
1425 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1427 renderer = gtk_cell_renderer_text_new();
1428 column = gtk_tree_view_column_new_with_attributes
1429 (_("Server"), renderer,
1430 "text", ACCOUNT_SERVER,
1432 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1436 *\brief Get currently selected account (by its unique ID)
1438 static gint account_list_view_get_selected_account_id(GtkWidget *list_view)
1440 GtkTreeSelection *selector;
1441 GtkTreeModel *model;
1443 PrefsAccount *res = NULL;
1445 selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
1447 if (!gtk_tree_selection_get_selected(selector, &model, &iter))
1450 gtk_tree_model_get(model, &iter, ACCOUNT_DATA, &res, -1);
1452 return res->account_id;
1456 *\brief Get the tree path of the currently selected account
1458 GtkTreePath *account_list_view_get_selected_account_path(GtkWidget *list_view)
1460 GtkTreeSelection *selector;
1461 GtkTreeModel *model;
1464 selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
1466 if (!gtk_tree_selection_get_selected(selector, &model, &iter))
1469 return gtk_tree_model_get_path(gtk_tree_view_get_model
1470 (GTK_TREE_VIEW(list_view)), &iter);
1474 *\brief Get the account data of the currently selected account
1476 PrefsAccount *account_list_view_get_selected_account(GtkWidget *list_view)
1478 GtkTreeSelection *selector;
1479 GtkTreeModel *model;
1481 PrefsAccount *res = NULL;
1483 selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
1485 if (!gtk_tree_selection_get_selected(selector, &model, &iter))
1488 gtk_tree_model_get(model, &iter, ACCOUNT_DATA, &res, -1);
1494 *\brief Select a row by the account it represents
1496 *\return gboolean TRUE if found and selected, FALSE if not.
1498 gboolean account_list_view_select_account(GtkWidget *list_view, gint account_id)
1500 FindAccountInStore fis;
1501 GtkTreeModel *model;
1503 fis.account_id = account_id;
1506 model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1508 gtk_tree_model_foreach(model, (GtkTreeModelForeachFunc) find_account_in_store,
1512 GtkTreeSelection *selection;
1515 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
1516 gtk_tree_selection_select_iter(selection, &fis.iter);
1517 path = gtk_tree_model_get_path(model, &fis.iter);
1518 /* XXX returned path may not be valid??? create new one to be sure */
1519 gtk_tree_view_set_cursor(GTK_TREE_VIEW(list_view), path, NULL, FALSE);
1520 gtk_tree_path_free(path);
1523 return fis.path != NULL;
1527 *\brief Set a new default account by its ID. (There is only one
1530 static void account_list_view_set_default_by_id(GtkWidget *list_view,
1533 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1535 gtk_tree_model_foreach
1536 (model, (GtkTreeModelForeachFunc) set_new_default_account,
1540 static gboolean set_new_default_account(GtkTreeModel *model,
1545 PrefsAccount *account = NULL;
1548 gtk_tree_model_get(model, iter,
1549 ACCOUNT_DATA, &account,
1552 if (*account_id == account->account_id)
1555 pixbuf = mark_pixbuf;
1557 gtk_list_store_set(GTK_LIST_STORE(model), iter,
1558 ACCOUNT_IS_DEFAULT, pixbuf,
1564 static gboolean find_account_in_store(GtkTreeModel *model,
1567 FindAccountInStore *data)
1569 PrefsAccount *account = NULL;
1570 gtk_tree_model_get(model, iter, ACCOUNT_DATA, &account, -1);
1572 if (data->account_id == account->account_id) {
1573 data->path = path; /* signal we found it */
1582 *\brief Triggered when "get all" column is activated or de-activated
1584 static void account_get_all_toggled(GtkCellRendererToggle *widget,
1586 GtkWidget *list_view)
1589 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1590 PrefsAccount *ac = NULL;
1593 if (!gtk_tree_model_get_iter_from_string(model, &iter, path))
1596 gtk_tree_model_get(model, &iter,
1598 ACCOUNT_ENABLE_GET_ALL, &get_all,
1601 /* check if the account has a selectable get all checkbox anyway... */
1602 if (!(ac->protocol == A_POP3 ||
1603 ac->protocol == A_IMAP4 ||
1604 ac->protocol == A_NNTP ||
1605 ac->protocol == A_LOCAL))
1608 /* set value in store */
1609 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
1610 ACCOUNT_ENABLE_GET_ALL, !get_all,
1613 /* set value in account */
1614 ac->recv_at_getall ^= TRUE;
1617 static void account_double_clicked(GtkTreeView *list_view,
1619 GtkTreeViewColumn *column,
1622 account_edit_prefs(NULL, NULL);
1625 static void drag_begin(GtkTreeView *list_view,
1626 GdkDragContext *context,
1629 /* XXX unfortunately a completed drag & drop does not emit
1630 * a "rows_reordered" signal, but a "row_changed" signal.
1631 * So during drag and drop, listen to "row_changed", and
1632 * update the account list accordingly */
1634 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1635 g_signal_connect(G_OBJECT(model), "row_changed",
1636 G_CALLBACK(account_row_changed_while_drag_drop),
1640 static void drag_end(GtkTreeView *list_view,
1641 GdkDragContext *context,
1644 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1645 g_signal_handlers_disconnect_by_func(G_OBJECT(model),
1646 G_CALLBACK(account_row_changed_while_drag_drop),
1650 static void account_row_changed_while_drag_drop(GtkTreeModel *model,
1654 GtkTreeView *list_view)