Improve some sieve-related translations
[claws.git] / src / account.c
blob0589f6ccab4eabbf84195190cb8a625dae3d9727
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2022 the Claws Mail team and 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 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"
58 #include "file-utils.h"
60 enum {
61 ACCOUNT_IS_DEFAULT,
62 ACCOUNT_ENABLE_GET_ALL,
63 ACCOUNT_NAME,
64 ACCOUNT_PROTOCOL,
65 ACCOUNT_SERVER,
66 ACCOUNT_DATA,
67 N_ACCOUNT_COLUMNS
71 typedef enum
73 COL_DEFAULT = 0,
74 COL_GETALL,
75 COL_NAME,
76 COL_PROTOCOL,
77 COL_SERVER,
78 N_EDIT_ACCOUNT_COLS
79 } EditAccountColumnPos;
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_set (void);
121 static void account_list_set (void);
123 typedef struct FindAccountInStore {
124 gint account_id;
125 GtkTreePath *path;
126 GtkTreeIter iter;
127 } FindAccountInStore;
129 static GtkListStore* account_create_data_store (void);
131 static void account_list_store_insert_account_item (GtkListStore *list_store,
132 PrefsAccount *account_data);
134 static GtkWidget *account_list_view_create (void);
135 static void account_create_list_view_columns (GtkWidget *list_view);
137 static gint account_list_view_get_selected_account_id (GtkWidget *list_view);
138 static GtkTreePath *account_list_view_get_selected_account_path (GtkWidget *list_view);
139 static PrefsAccount *account_list_view_get_selected_account (GtkWidget *list_view);
140 static gboolean account_list_view_select_account (GtkWidget *list_view,
141 gint account_id);
143 static void account_list_view_set_default_by_id(GtkWidget *list_view,
144 gint account_id);
146 static gboolean set_new_default_account (GtkTreeModel *model,
147 GtkTreePath *path,
148 GtkTreeIter *iter,
149 gint *account_id);
151 static gboolean find_account_in_store (GtkTreeModel *model,
152 GtkTreePath *path,
153 GtkTreeIter *iter,
154 FindAccountInStore *data);
156 static void account_get_all_toggled (GtkCellRendererToggle *widget,
157 gchar *path,
158 GtkWidget *list_view);
160 static void account_double_clicked (GtkTreeView *list_view,
161 GtkTreePath *path,
162 GtkTreeViewColumn *column,
163 gpointer data);
165 static void drag_begin (GtkTreeView *list_view,
166 GdkDragContext *context,
167 gpointer data);
169 static void drag_end (GtkTreeView *list_view,
170 GdkDragContext *context,
171 gpointer data);
173 static void account_row_changed_while_drag_drop (GtkTreeModel *model,
174 GtkTreePath *path,
175 GtkTreeIter *iter,
176 gpointer arg3,
177 GtkTreeView *list_view);
179 static void account_flush_state(void)
181 account_set_menu();
182 main_window_reflect_prefs_all();
184 account_list_dirty = FALSE;
187 void account_read_config_all(void)
189 GSList *ac_label_list = NULL, *cur;
190 gchar *rcpath;
191 FILE *fp;
192 gchar buf[PREFSBUFSIZE];
193 PrefsAccount *ac_prefs;
195 debug_print("Reading all config for each account...\n");
197 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACCOUNT_RC, NULL);
198 if ((fp = claws_fopen(rcpath, "rb")) == NULL) {
199 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "claws_fopen");
200 g_free(rcpath);
201 return;
203 g_free(rcpath);
205 while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
206 if (!strncmp(buf, "[Account: ", 10)) {
207 strretchomp(buf);
208 memmove(buf, buf + 1, sizeof(buf) - 1);
209 buf[strlen(buf) - 1] = '\0';
210 debug_print("Found label: %s\n", buf);
211 ac_label_list = g_slist_append(ac_label_list,
212 g_strdup(buf));
215 claws_fclose(fp);
217 /* read config data from file */
218 cur_account = NULL;
219 for (cur = ac_label_list; cur != NULL; cur = cur->next) {
220 ac_prefs = prefs_account_new_from_config((gchar *)cur->data);
221 account_list = g_list_append(account_list, ac_prefs);
222 if (ac_prefs->is_default)
223 cur_account = ac_prefs;
225 /* if default is not set, assume first account as default */
226 if (!cur_account && account_list) {
227 ac_prefs = (PrefsAccount *)account_list->data;
228 account_set_as_default(ac_prefs);
229 cur_account = ac_prefs;
232 account_set_menu();
233 main_window_reflect_prefs_all_now();
235 while (ac_label_list) {
236 g_free(ac_label_list->data);
237 ac_label_list = g_slist_remove(ac_label_list,
238 ac_label_list->data);
242 void account_write_config_all(void)
244 prefs_account_write_config_all(account_list);
248 * account_find_all_from_address:
249 * @ac_list: initial list of accounts. NULL to create a new one.
250 * Accounts found in the @address will be appended to this list.
251 * @address: Email address string.
253 * Find all the mail (not news) accounts within the specified address.
255 * Return value: the original accounts list with the found accounts appended.
257 GList *account_find_all_from_address(GList *ac_list, const gchar *address)
259 GList *cur;
260 PrefsAccount *ac;
262 if (address == NULL)
263 return ac_list;
265 for (cur = account_list; cur != NULL; cur = cur->next) {
266 ac = (PrefsAccount *)cur->data;
267 if (ac->protocol != A_NNTP && ac->address &&
268 strcasestr(address, ac->address) != NULL)
269 ac_list = g_list_append(ac_list, ac);
271 return ac_list;
274 GList *account_find_all(void)
276 GList *cur;
277 PrefsAccount *ac;
278 GList *ac_list = NULL;
280 for (cur = account_list; cur != NULL; cur = cur->next) {
281 ac = (PrefsAccount *)cur->data;
282 ac_list = g_list_append(ac_list, ac);
284 return ac_list;
287 PrefsAccount *account_find_from_smtp_server(const gchar *address,
288 const gchar *smtp_server)
290 GList *cur;
291 PrefsAccount *ac;
293 cm_return_val_if_fail(address != NULL, NULL);
294 cm_return_val_if_fail(smtp_server != NULL, NULL);
296 for (cur = account_list; cur != NULL; cur = cur->next) {
297 ac = (PrefsAccount *)cur->data;
298 if (!g_strcmp0(address, ac->address) &&
299 !g_strcmp0(smtp_server, ac->smtp_server))
300 return ac;
303 return NULL;
307 * account_find_from_address:
308 * @address: Email address string.
310 * Find a mail (not news if newsgroups_ok is FALSE) account with the specified email address.
312 * Return value: The found account, or NULL if not found.
314 PrefsAccount *account_find_from_address(const gchar *address, gboolean newsgroups_ok)
316 GList *cur;
317 PrefsAccount *ac;
319 cm_return_val_if_fail(address != NULL, NULL);
321 for (cur = account_list; cur != NULL; cur = cur->next) {
322 ac = (PrefsAccount *)cur->data;
323 if ((ac->protocol != A_NNTP || newsgroups_ok) && ac->address &&
324 g_ascii_strcasecmp(address, ac->address) == 0)
325 return ac;
328 return NULL;
331 PrefsAccount *account_find_from_id(gint id)
333 GList *cur;
334 PrefsAccount *ac;
336 for (cur = account_list; cur != NULL; cur = cur->next) {
337 ac = (PrefsAccount *)cur->data;
338 if (id == ac->account_id)
339 return ac;
342 return NULL;
345 PrefsAccount *account_find_from_item(FolderItem *item)
347 PrefsAccount *ac;
349 cm_return_val_if_fail(item != NULL, NULL);
351 ac = item->account;
352 if (!ac) {
353 FolderItem *cur_item = folder_item_parent(item);
354 while (cur_item != NULL) {
355 if (cur_item->account && cur_item->apply_sub) {
356 ac = cur_item->account;
357 break;
359 cur_item = folder_item_parent(cur_item);
362 if (!ac)
363 ac = item->folder->account;
365 return ac;
368 static void account_set_menu(void)
370 main_window_set_account_menu(account_list);
373 void account_set_menu_only_toolbar(void)
375 main_window_set_account_menu_only_toolbar(account_list);
378 GList *account_get_list(void)
380 return account_list;
383 void account_edit_focus(void)
385 if (edit_account.window == NULL) {
386 return;
388 manage_window_set_transient(GTK_WINDOW(edit_account.window));
389 gtk_widget_grab_focus(edit_account.close_btn);
390 gtk_widget_show(edit_account.window);
391 gtk_window_set_modal(GTK_WINDOW(edit_account.window), TRUE);
392 manage_window_focus_in(edit_account.window, NULL, NULL);
395 void account_edit_open(gpointer a, gpointer b)
397 inc_lock();
399 account_list_dirty = FALSE;
401 if (compose_get_compose_list()) {
402 alertpanel_error(_("Some composing windows are open.\n"
403 "Please close all the composing "
404 "windows before editing accounts."));
405 inc_unlock();
406 return;
409 debug_print("Opening account edit window...\n");
411 if (!edit_account.window)
412 account_edit_create();
414 account_list_view_set();
416 account_edit_focus();
419 void account_add(void)
421 PrefsAccount *ac_prefs;
423 ac_prefs = prefs_account_open(NULL, &account_list_dirty);
425 if (!ac_prefs) return;
427 account_edit_focus();
429 account_list = g_list_append(account_list, ac_prefs);
431 if (ac_prefs->is_default)
432 account_set_as_default(ac_prefs);
434 account_list_view_set();
436 if (ac_prefs->protocol == A_IMAP4 || ac_prefs->protocol == A_NNTP) {
437 Folder *folder;
439 if (ac_prefs->protocol == A_IMAP4) {
440 folder = folder_new(folder_get_class_from_string("imap"), ac_prefs->account_name,
441 ac_prefs->recv_server);
442 } else {
443 folder = folder_new(folder_get_class_from_string("news"), ac_prefs->account_name,
444 ac_prefs->nntp_server);
446 if (folder == NULL) {
447 alertpanel_error(_("Can't create folder."));
448 return;
450 folder->account = ac_prefs;
451 ac_prefs->folder = folder;
452 folder_add(folder);
453 if (ac_prefs->protocol == A_IMAP4)
454 folder->klass->create_tree(folder);
455 folderview_set_all();
456 folder_write_list();
460 void account_open(PrefsAccount *ac_prefs, gboolean called_from_acc_list)
462 gboolean prev_default;
463 gchar *ac_name, *old_prefix, *new_prefix;
464 gboolean account_dirty = FALSE;
466 cm_return_if_fail(ac_prefs != NULL);
468 if (compose_get_compose_list()) {
469 alertpanel_error(_("Some composing windows are open.\n"
470 "Please close all the composing "
471 "windows before editing accounts."));
472 return;
475 prev_default = ac_prefs->is_default;
476 Xstrdup_a(ac_name, ac_prefs->account_name ? ac_prefs->account_name : "",
477 return);
479 prefs_account_open(ac_prefs, &account_dirty);
481 if (called_from_acc_list)
482 account_edit_focus();
484 if (account_dirty) {
485 if (!prev_default && ac_prefs->is_default)
486 account_set_as_default(ac_prefs);
488 if (ac_prefs->folder && g_strcmp0(ac_name, ac_prefs->account_name) != 0) {
489 old_prefix = folder_get_identifier(FOLDER(ac_prefs->folder));
490 folder_set_name(FOLDER(ac_prefs->folder),
491 ac_prefs->account_name);
492 folderview_set_all();
493 folder_prefs_save_config_recursive(FOLDER(ac_prefs->folder));
494 new_prefix = folder_get_identifier(FOLDER(ac_prefs->folder));
496 account_rename_path(old_prefix, new_prefix);
497 prefs_filtering_rename_path(old_prefix, new_prefix);
498 prefs_actions_rename_path(old_prefix, new_prefix);
500 g_free(old_prefix);
501 g_free(new_prefix);
504 account_write_config_all();
506 account_flush_state();
510 static void account_set_as_default(PrefsAccount *ac_prefs)
512 PrefsAccount *ap;
513 GList *cur;
515 for (cur = account_list; cur != NULL; cur = cur->next) {
516 ap = (PrefsAccount *)cur->data;
517 if (ap->is_default)
518 ap->is_default = FALSE;
521 ac_prefs->is_default = TRUE;
524 PrefsAccount *account_get_default(void)
526 PrefsAccount *ap;
527 GList *cur;
529 for (cur = account_list; cur != NULL; cur = cur->next) {
530 ap = (PrefsAccount *)cur->data;
531 if (ap->is_default)
532 return ap;
535 return NULL;
538 void account_set_missing_folder(void)
540 PrefsAccount *ap;
541 GList *cur;
543 for (cur = account_list; cur != NULL; cur = cur->next) {
544 ap = (PrefsAccount *)cur->data;
545 if ((ap->protocol == A_IMAP4 || ap->protocol == A_NNTP) &&
546 !ap->folder) {
547 Folder *folder;
549 if (ap->protocol == A_IMAP4) {
550 folder = folder_new(folder_get_class_from_string("imap"), ap->account_name,
551 ap->recv_server);
552 } else {
553 folder = folder_new(folder_get_class_from_string("news"), ap->account_name,
554 ap->nntp_server);
556 if (folder == NULL)
557 return;
558 folder->account = ap;
559 ap->folder = folder;
560 folder_add(folder);
561 if (ap->protocol == A_IMAP4)
562 folder->klass->create_tree(folder);
563 folder_write_list();
569 #define CHECK_CHANGE_FOLDER(folder) { \
570 if (folder && !strncmp(folder, old_id, strlen(old_id))) { \
571 if (strlen(folder) == strlen(old_id)) { \
572 g_free(folder); \
573 folder = g_strdup(new_id); \
574 } else if (strlen(folder) > strlen(old_id) \
575 && folder[strlen(old_id)] == G_DIR_SEPARATOR) { \
576 gchar *new_path = g_strdup_printf("%s%s", \
577 new_id, (folder + strlen(old_id))); \
578 g_free(folder); \
579 folder = new_path; \
584 void account_rename_path(const gchar *old_id, const gchar *new_id)
586 GList *cur = account_list;
587 for (; cur != NULL; cur = g_list_next(cur)) {
588 PrefsAccount *ap = (PrefsAccount *)cur->data;
589 CHECK_CHANGE_FOLDER(ap->inbox);
590 CHECK_CHANGE_FOLDER(ap->local_inbox);
591 CHECK_CHANGE_FOLDER(ap->queue_folder);
592 CHECK_CHANGE_FOLDER(ap->sent_folder);
593 CHECK_CHANGE_FOLDER(ap->draft_folder);
594 CHECK_CHANGE_FOLDER(ap->trash_folder);
598 FolderItem *account_get_special_folder(PrefsAccount *ac_prefs,
599 SpecialFolderItemType type)
601 FolderItem *item = NULL;
603 cm_return_val_if_fail(ac_prefs != NULL, NULL);
605 switch (type) {
606 case F_INBOX:
607 if (ac_prefs->folder)
608 item = FOLDER(ac_prefs->folder)->inbox;
609 if (!item)
610 item = folder_get_default_inbox();
611 break;
612 case F_OUTBOX:
613 if (ac_prefs->set_sent_folder && ac_prefs->sent_folder) {
614 item = folder_find_item_from_identifier
615 (ac_prefs->sent_folder);
617 if (!item) {
618 if (ac_prefs->folder)
619 item = FOLDER(ac_prefs->folder)->outbox;
620 if (!item)
621 item = folder_get_default_outbox_for_class(F_MH);
622 if (!item)
623 item = folder_get_default_outbox();
625 break;
626 case F_DRAFT:
627 if (ac_prefs->set_draft_folder && ac_prefs->draft_folder) {
628 item = folder_find_item_from_identifier
629 (ac_prefs->draft_folder);
631 if (!item) {
632 if (ac_prefs->folder)
633 item = FOLDER(ac_prefs->folder)->draft;
634 if (!item)
635 item = folder_get_default_draft_for_class(F_MH);
636 if (!item)
637 item = folder_get_default_draft();
639 break;
640 case F_QUEUE:
641 if (ac_prefs->set_queue_folder && ac_prefs->queue_folder) {
642 item = folder_find_item_from_identifier
643 (ac_prefs->queue_folder);
645 if (!item) {
646 if (ac_prefs->folder)
647 item = FOLDER(ac_prefs->folder)->queue;
648 if (!item)
649 item = folder_get_default_queue_for_class(F_MH);
650 if (!item)
651 item = folder_get_default_queue();
653 break;
654 case F_TRASH:
655 if (ac_prefs->set_trash_folder && ac_prefs->trash_folder) {
656 item = folder_find_item_from_identifier
657 (ac_prefs->trash_folder);
659 if (!item) {
660 if (ac_prefs->folder)
661 item = FOLDER(ac_prefs->folder)->trash;
662 if (!item)
663 item = folder_get_default_trash_for_class(F_MH);
664 if (!item)
665 item = folder_get_default_trash();
667 break;
668 default:
669 break;
672 return item;
675 void account_destroy(PrefsAccount *ac_prefs)
677 cm_return_if_fail(ac_prefs != NULL);
679 folder_unref_account_all(ac_prefs);
681 account_list = g_list_remove(account_list, ac_prefs);
683 if (cur_account == ac_prefs) cur_account = NULL;
684 if (!cur_account && account_list) {
685 cur_account = account_get_default();
686 if (!cur_account) {
687 ac_prefs = (PrefsAccount *)account_list->data;
688 account_set_as_default(ac_prefs);
689 cur_account = ac_prefs;
695 *\brief Save Gtk object size to prefs dataset
697 static void account_size_allocate_cb(GtkWidget *widget,
698 GtkAllocation *allocation)
700 cm_return_if_fail(allocation != NULL);
702 gtk_window_get_size(GTK_WINDOW(widget),
703 &prefs_common.accountswin_width, &prefs_common.accountswin_height);
706 static void account_edit_create(void)
708 GtkWidget *window;
709 GtkWidget *vbox;
710 GtkWidget *label;
711 GtkWidget *hbox;
712 GtkWidget *scrolledwin;
713 GtkWidget *list_view;
715 GtkWidget *vbox2;
716 GtkWidget *add_btn;
717 GtkWidget *edit_btn;
718 GtkWidget *del_btn;
719 GtkWidget *clone_btn;
720 GtkWidget *up_btn;
721 GtkWidget *down_btn;
723 GtkWidget *default_btn;
725 GtkWidget *confirm_area;
726 GtkWidget *help_btn;
727 GtkWidget *close_btn;
729 static GdkGeometry geometry;
731 debug_print("Creating account edit window...\n");
733 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "account");
734 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
735 gtk_window_set_title (GTK_WINDOW (window), _("Edit accounts"));
736 g_signal_connect (G_OBJECT (window), "delete_event",
737 G_CALLBACK (account_delete_event), NULL);
738 g_signal_connect (G_OBJECT (window), "key_press_event",
739 G_CALLBACK (account_key_pressed), NULL);
740 MANAGE_WINDOW_SIGNALS_CONNECT (window);
741 gtk_widget_realize(window);
743 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
744 gtk_widget_show (vbox);
745 gtk_container_add (GTK_CONTAINER (window), vbox);
747 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
748 gtk_widget_show (hbox);
749 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
751 label = gtk_label_new
752 (_("Using 'Get Mail' will retrieve messages from your Accounts "
753 "in the order given, the checkbox indicates which accounts "
754 "will be included. Bold text indicates the default account."));
755 gtk_widget_show (label);
757 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 4);
758 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
759 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
761 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
762 gtk_widget_show (hbox);
763 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
764 gtk_container_set_border_width (GTK_CONTAINER (hbox), 2);
766 scrolledwin = gtk_scrolled_window_new (NULL, NULL);
767 gtk_widget_show (scrolledwin);
768 gtk_box_pack_start (GTK_BOX (hbox), scrolledwin, TRUE, TRUE, 0);
769 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwin),
770 GTK_POLICY_AUTOMATIC,
771 GTK_POLICY_AUTOMATIC);
773 list_view = account_list_view_create();
774 gtk_widget_show(list_view);
775 gtk_container_add(GTK_CONTAINER(scrolledwin), list_view);
777 vbox2 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
778 gtk_widget_show (vbox2);
779 gtk_box_pack_start (GTK_BOX (hbox), vbox2, FALSE, FALSE, 0);
781 add_btn = gtk_button_new_with_mnemonic(_("_New"));
782 gtk_widget_show (add_btn);
783 gtk_box_pack_start (GTK_BOX (vbox2), add_btn, FALSE, FALSE, 4);
784 g_signal_connect (G_OBJECT(add_btn), "clicked",
785 G_CALLBACK (account_add), NULL);
786 CLAWS_SET_TIP(add_btn,
787 _("Create a new account"));
789 edit_btn = gtk_button_new_with_mnemonic(_("_Edit"));
790 gtk_widget_show (edit_btn);
791 gtk_box_pack_start (GTK_BOX (vbox2), edit_btn, FALSE, FALSE, 4);
792 g_signal_connect (G_OBJECT(edit_btn), "clicked",
793 G_CALLBACK (account_edit_prefs), NULL);
794 CLAWS_SET_TIP(edit_btn,
795 _("Edit preferences for the selected account"));
797 del_btn = gtkut_stock_button("edit-delete", _("D_elete"));
798 gtk_widget_show (del_btn);
799 gtk_box_pack_start (GTK_BOX (vbox2), del_btn, FALSE, FALSE, 4);
800 g_signal_connect (G_OBJECT(del_btn), "clicked",
801 G_CALLBACK (account_delete), NULL);
802 CLAWS_SET_TIP(del_btn,
803 _("Delete the selected account from the list"));
805 clone_btn = gtkut_stock_button("edit-copy", _("_Copy"));
806 gtk_widget_show (clone_btn);
807 gtk_box_pack_start (GTK_BOX (vbox2), clone_btn, FALSE, FALSE, 4);
808 g_signal_connect(G_OBJECT(clone_btn), "clicked",
809 G_CALLBACK(account_clone), NULL);
810 CLAWS_SET_TIP(clone_btn,
811 _("Create a new copy of the selected account"));
813 down_btn = gtkut_stock_button("go-down", _("_Down"));
814 gtk_widget_show (down_btn);
815 gtk_box_pack_end (GTK_BOX (vbox2), down_btn, FALSE, FALSE, 4);
816 g_signal_connect (G_OBJECT(down_btn), "clicked",
817 G_CALLBACK (account_down), NULL);
818 CLAWS_SET_TIP(down_btn,
819 _("Move the selected account down"));
821 up_btn = gtkut_stock_button("go-up", _("_Up"));
822 gtk_widget_show (up_btn);
823 gtk_box_pack_end (GTK_BOX (vbox2), up_btn, FALSE, FALSE, 4);
824 g_signal_connect (G_OBJECT(up_btn), "clicked",
825 G_CALLBACK (account_up), NULL);
826 CLAWS_SET_TIP(up_btn,
827 _("Move the selected account up"));
829 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
830 gtk_widget_show (hbox);
831 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
833 default_btn = gtk_button_new_with_mnemonic
834 (_(" _Set as default account "));
835 gtk_widget_show (default_btn);
836 gtk_box_pack_start (GTK_BOX (hbox), default_btn, FALSE, FALSE, 0);
837 g_signal_connect (G_OBJECT(default_btn), "clicked",
838 G_CALLBACK (account_set_default), NULL);
840 gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
841 &close_btn, "window-close", _("_Close"),
842 NULL, NULL, NULL, NULL, NULL, NULL);
843 gtk_widget_show(confirm_area);
845 gtk_box_pack_end (GTK_BOX (hbox), confirm_area, FALSE, FALSE, 0);
846 gtk_widget_grab_default (close_btn);
848 g_signal_connect (G_OBJECT (close_btn), "clicked",
849 G_CALLBACK (account_edit_close),
850 NULL);
851 g_signal_connect(G_OBJECT(help_btn), "clicked",
852 G_CALLBACK(manual_open_with_anchor_cb),
853 MANUAL_ANCHOR_ACCOUNTPREFS);
856 g_signal_connect(G_OBJECT(window), "size_allocate",
857 G_CALLBACK(account_size_allocate_cb), NULL);
859 if (!geometry.min_height) {
860 geometry.min_width = 500;
861 geometry.min_height = 380;
864 gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
865 GDK_HINT_MIN_SIZE);
866 gtk_window_set_default_size(GTK_WINDOW(window), prefs_common.accountswin_width,
867 prefs_common.accountswin_height);
868 #ifdef G_OS_WIN32
869 gtk_window_move(GTK_WINDOW(window), 48, 48);
870 #endif
872 edit_account.window = window;
873 edit_account.list_view = list_view;
874 edit_account.close_btn = close_btn;
877 static void account_edit_prefs(GtkWidget *widget, gpointer data)
879 PrefsAccount *ac_prefs;
881 ac_prefs = account_list_view_get_selected_account(edit_account.list_view);
883 if (ac_prefs) {
884 account_open(ac_prefs, TRUE);
885 account_list_view_set();
889 static gboolean account_delete_references_func(GNode *node, gpointer data)
891 FolderItem *item;
892 gint account;
894 cm_return_val_if_fail(node->data != NULL, FALSE);
896 item = FOLDER_ITEM(node->data);
897 account = GPOINTER_TO_INT(data);
899 if(!item->prefs) /* && item->prefs->stype == F_NORMAL */
900 return FALSE;
901 if(item->prefs->default_account != account)
902 return FALSE;
904 item->prefs->enable_default_account = FALSE;
905 item->prefs->default_account = 0;
906 folder_item_prefs_save_config(item);
908 return FALSE;
912 #define ACP_FDUP(fld) ac_clon->fld = ((ac_prefs->fld) != NULL)?\
913 g_strdup(ac_prefs->fld): NULL
914 #define ACP_FASSIGN(fld) ac_clon->fld = ac_prefs->fld
915 static void account_clone(GtkWidget *widget, gpointer data)
917 PrefsAccount *ac_prefs, *ac_clon;
918 GSList *hdrs = NULL;
919 CustomHeader *cch = NULL, *ch = NULL;
921 ac_prefs = account_list_view_get_selected_account(edit_account.list_view);
922 if (ac_prefs == NULL)
923 return;
925 if (ac_prefs->protocol == A_IMAP4 || ac_prefs->protocol == A_NNTP) {
926 alertpanel_error(_("Accounts with remote folders cannot be copied."));
927 return;
929 account_list_dirty = TRUE;
931 ac_clon = prefs_account_new();
932 /* copy fields */
933 ac_clon->account_name = g_strdup_printf(_("Copy of %s"),
934 ac_prefs->account_name);
935 /* personal */
936 ACP_FDUP(name);
937 ACP_FDUP(address);
938 ACP_FDUP(organization);
940 /* server */
941 ACP_FASSIGN(protocol);
942 ACP_FDUP(recv_server);
943 ACP_FDUP(smtp_server);
944 ACP_FDUP(nntp_server);
945 ACP_FASSIGN(use_nntp_auth);
946 ACP_FASSIGN(use_nntp_auth_onconnect);
947 ACP_FDUP(userid);
948 ACP_FDUP(passwd);
950 ACP_FDUP(local_mbox);
951 ACP_FASSIGN(use_mail_command);
952 ACP_FDUP(mail_command);
954 ACP_FASSIGN(ssl_pop);
955 ACP_FASSIGN(ssl_imap);
956 ACP_FASSIGN(ssl_nntp);
957 ACP_FASSIGN(ssl_smtp);
958 ACP_FASSIGN(ssl_certs_auto_accept);
959 ACP_FASSIGN(use_nonblocking_ssl);
960 ACP_FASSIGN(use_tls_sni);
961 ACP_FASSIGN(in_ssl_client_cert_file);
962 ACP_FASSIGN(in_ssl_client_cert_pass);
963 ACP_FASSIGN(out_ssl_client_cert_file);
964 ACP_FASSIGN(out_ssl_client_cert_pass);
966 /* receive */
967 ACP_FASSIGN(use_pop_auth);
968 ACP_FASSIGN(pop_auth_type);
969 ACP_FASSIGN(rmmail);
970 ACP_FASSIGN(msg_leave_time);
971 ACP_FASSIGN(msg_leave_hour);
972 ACP_FASSIGN(recv_at_getall);
973 ACP_FASSIGN(sd_rmmail_on_download);
974 ACP_FASSIGN(enable_size_limit);
975 ACP_FASSIGN(size_limit);
976 ACP_FASSIGN(filter_on_recv);
977 ACP_FASSIGN(filterhook_on_recv);
978 ACP_FDUP(inbox);
979 ACP_FDUP(local_inbox);
980 ACP_FASSIGN(max_articles);
981 ACP_FASSIGN(autochk_use_default);
982 ACP_FASSIGN(autochk_use_custom);
983 ACP_FASSIGN(autochk_itv);
984 ac_clon->autocheck_timer = 0;
986 ACP_FASSIGN(imap_auth_type);
988 /* send */
989 ACP_FASSIGN(gen_msgid);
990 ACP_FASSIGN(gen_xmailer);
991 ACP_FASSIGN(add_customhdr);
992 ACP_FASSIGN(use_smtp_auth);
993 ACP_FASSIGN(smtp_auth_type);
994 ACP_FDUP(smtp_userid);
995 ACP_FDUP(smtp_passwd);
997 ACP_FASSIGN(pop_before_smtp);
998 ACP_FASSIGN(pop_before_smtp_timeout);
999 ACP_FASSIGN(last_pop_login_time);
1001 ac_clon->customhdr_list = NULL;
1002 hdrs = ac_prefs->customhdr_list;
1003 while (hdrs != NULL) {
1004 ch = (CustomHeader *)hdrs->data;
1006 cch = g_new0(CustomHeader, 1);
1007 cch->account_id = ac_clon->account_id;
1008 cch->name = (ch->name != NULL) ? g_strdup(ch->name) : NULL;
1009 cch->value = (ch->value != NULL) ? g_strdup(ch->value) : NULL;
1011 ac_clon->customhdr_list = g_slist_append(ac_clon->customhdr_list, cch);
1013 hdrs = g_slist_next(hdrs);
1016 /* compose */
1017 ACP_FASSIGN(sig_type);
1018 ACP_FDUP(sig_path);
1019 ACP_FASSIGN(auto_sig);
1020 ACP_FDUP(sig_sep);
1021 ACP_FASSIGN(set_autocc);
1022 ACP_FDUP(auto_cc);
1023 ACP_FASSIGN(set_autobcc);
1024 ACP_FDUP(auto_bcc);
1025 ACP_FASSIGN(set_autoreplyto);
1026 ACP_FDUP(auto_replyto);
1027 ACP_FASSIGN(enable_default_dictionary);
1028 ACP_FDUP(default_dictionary);
1029 ACP_FASSIGN(enable_default_alt_dictionary);
1030 ACP_FDUP(default_alt_dictionary);
1031 ACP_FASSIGN(compose_with_format);
1032 ACP_FDUP(compose_subject_format);
1033 ACP_FDUP(compose_body_format);
1034 ACP_FASSIGN(reply_with_format);
1035 ACP_FDUP(reply_quotemark);
1036 ACP_FDUP(reply_body_format);
1037 ACP_FASSIGN(forward_with_format);
1038 ACP_FDUP(forward_quotemark);
1039 ACP_FDUP(forward_body_format);
1041 /* privacy */
1042 ACP_FDUP(default_privacy_system);
1043 ACP_FASSIGN(default_encrypt);
1044 ACP_FASSIGN(default_encrypt_reply);
1045 ACP_FASSIGN(default_sign);
1046 ACP_FASSIGN(default_sign_reply);
1047 ACP_FASSIGN(save_encrypted_as_clear_text);
1048 ACP_FASSIGN(encrypt_to_self);
1050 /* advanced */
1051 ACP_FASSIGN(set_smtpport);
1052 ACP_FASSIGN(smtpport);
1053 ACP_FASSIGN(set_popport);
1054 ACP_FASSIGN(popport);
1055 ACP_FASSIGN(set_imapport);
1056 ACP_FASSIGN(imapport);
1057 ACP_FASSIGN(set_nntpport);
1058 ACP_FASSIGN(nntpport);
1059 ACP_FASSIGN(set_domain);
1060 ACP_FDUP(domain);
1061 ACP_FASSIGN(mark_crosspost_read);
1062 ACP_FASSIGN(crosspost_col);
1064 #ifndef G_OS_WIN32
1065 ACP_FASSIGN(set_tunnelcmd);
1066 ACP_FDUP(tunnelcmd);
1067 #endif
1069 ACP_FDUP(imap_dir);
1070 ACP_FASSIGN(imap_subsonly);
1071 ACP_FASSIGN(low_bandwidth);
1073 ACP_FASSIGN(set_sent_folder);
1074 ACP_FDUP(sent_folder);
1075 ACP_FASSIGN(set_queue_folder);
1076 ACP_FDUP(queue_folder);
1077 ACP_FASSIGN(set_draft_folder);
1078 ACP_FDUP(draft_folder);
1079 ACP_FASSIGN(set_trash_folder);
1080 ACP_FDUP(trash_folder);
1081 /* don't want two default accounts */
1082 ac_clon->is_default = FALSE;
1083 ACP_FASSIGN(folder);
1085 ACP_FASSIGN(config_version);
1087 ACP_FASSIGN(use_proxy);
1088 ACP_FASSIGN(use_default_proxy);
1089 ACP_FASSIGN(use_proxy_for_send);
1090 ACP_FASSIGN(proxy_info.proxy_type);
1091 ACP_FDUP(proxy_info.proxy_host);
1092 ACP_FASSIGN(proxy_info.proxy_port);
1093 ACP_FASSIGN(proxy_info.use_proxy_auth);
1094 ACP_FDUP(proxy_info.proxy_name);
1095 ACP_FDUP(proxy_info.proxy_pass);
1097 account_list = g_list_append(account_list, ac_clon);
1098 account_list_view_set();
1100 #undef ACP_FDUP
1101 #undef ACP_FASSIGN
1103 static void account_empty_cache(PrefsAccount *ac_prefs)
1105 gchar *cache_dir;
1107 cache_dir = prefs_account_cache_dir(ac_prefs, FALSE);
1108 if (cache_dir == NULL)
1109 return; /* no cache dir, nothing to do */
1111 if (is_dir_exist(cache_dir) && remove_dir_recursive(cache_dir) < 0) {
1112 g_warning("can't remove directory '%s'", cache_dir);
1113 } else {
1114 gchar *server_dir = prefs_account_cache_dir(ac_prefs, TRUE);
1115 if (g_rmdir(server_dir) == 0)
1116 debug_print("Removed empty cache server directory\n");
1117 else
1118 debug_print("Cache server directory not empty: not removed\n");
1119 g_free(server_dir);
1121 g_free(cache_dir);
1124 static void account_delete(GtkWidget *widget, gpointer data)
1126 PrefsAccount *ac_prefs;
1127 gchar buf[BUFFSIZE];
1128 GList *list;
1129 Folder *folder;
1130 GSList *cur;
1132 ac_prefs = account_list_view_get_selected_account(edit_account.list_view);
1133 if (ac_prefs == NULL)
1134 return;
1136 g_snprintf(buf, sizeof(buf),
1137 _("Do you really want to delete the account '%s'?"),
1138 ac_prefs->account_name ? ac_prefs->account_name :
1139 _("(Untitled)"));
1140 if (alertpanel_full(_("Delete account"), buf,
1141 NULL, _("_Cancel"), "edit-delete", _("_Delete"),
1142 NULL, NULL, ALERTFOCUS_FIRST, FALSE,
1143 NULL, ALERT_WARNING) != G_ALERTALTERNATE)
1144 return;
1145 account_list_dirty = TRUE;
1147 if (ac_prefs->folder) {
1148 FolderItem *item;
1150 item = mainwindow_get_mainwindow()->summaryview->folder_item;
1151 if (item && item->folder == FOLDER(ac_prefs->folder))
1152 summary_clear_all(mainwindow_get_mainwindow()->summaryview);
1153 folder_destroy(FOLDER(ac_prefs->folder));
1154 folderview_set_all();
1157 inc_account_autocheck_timer_remove(ac_prefs);
1159 account_destroy(ac_prefs);
1160 account_list_view_set();
1162 debug_print("Removing deleted account references for all the folders...\n");
1163 list = folder_get_list();
1164 for (; list != NULL; list = list->next) {
1165 folder = FOLDER(list->data);
1166 if (folder->node) /* && folder->type == F_? */
1167 g_node_traverse(folder->node, G_PRE_ORDER,
1168 G_TRAVERSE_ALL, -1,
1169 account_delete_references_func,
1170 GINT_TO_POINTER(ac_prefs->account_id));
1173 gchar *uid = g_strdup_printf("%d", ac_prefs->account_id);
1174 passwd_store_delete_block(PWS_ACCOUNT, uid);
1175 g_free(uid);
1177 debug_print("Removing filter rules relative to this account...\n");
1178 for(cur = filtering_rules ; cur != NULL ;) {
1179 FilteringProp * prop = (FilteringProp *) cur->data;
1181 if (prop && (prop->account_id == ac_prefs->account_id)) {
1182 /* get next item before we kill the current one */
1183 cur = g_slist_next(cur);
1185 /* unallocate filteringprop and unchain it from the list */
1186 filteringprop_free(prop);
1187 filtering_rules = g_slist_remove(filtering_rules, prop);
1188 } else {
1189 cur = g_slist_next(cur);
1193 debug_print("Removing cache directory of this account...\n");
1194 account_empty_cache(ac_prefs);
1196 folder_write_list();
1199 static void account_up(GtkWidget *widget, gpointer data)
1201 GtkTreePath *sel = account_list_view_get_selected_account_path
1202 (edit_account.list_view),
1203 *up;
1204 GtkTreeIter isel, iup;
1205 GtkTreeModel *model = gtk_tree_view_get_model
1206 (GTK_TREE_VIEW(edit_account.list_view));
1208 if (!sel)
1209 return;
1210 account_list_dirty = TRUE;
1212 up = gtk_tree_path_copy(sel);
1213 if (!up) {
1214 gtk_tree_path_free(sel);
1215 return;
1218 if (!gtk_tree_path_prev(up)) {
1219 gtk_tree_path_free(up);
1220 gtk_tree_path_free(sel);
1221 return;
1224 if (!gtk_tree_model_get_iter(model, &isel, sel)
1225 || !gtk_tree_model_get_iter(model, &iup, up)) {
1226 gtk_tree_path_free(up);
1227 gtk_tree_path_free(sel);
1228 return;
1231 gtk_list_store_swap(GTK_LIST_STORE(model), &isel, &iup);
1233 account_list_set();
1235 gtk_tree_path_free(up);
1236 gtk_tree_path_free(sel);
1239 static void account_down(GtkWidget *widget, gpointer data)
1241 GtkTreePath *sel = account_list_view_get_selected_account_path
1242 (edit_account.list_view),
1243 *dn;
1244 GtkTreeIter isel, idn;
1245 GtkTreeModel *model = gtk_tree_view_get_model
1246 (GTK_TREE_VIEW(edit_account.list_view));
1248 if (!sel)
1249 return;
1250 account_list_dirty = TRUE;
1252 dn = gtk_tree_path_copy(sel);
1253 if (!dn) {
1254 gtk_tree_path_free(sel);
1255 return;
1258 /* XXX no check possible??? however, if down but at bottom, then
1259 * nothing seems to happen much anyway, so the following seems to
1260 * be okay */
1261 gtk_tree_path_next(dn);
1263 if (!gtk_tree_model_get_iter(model, &isel, sel)
1264 || !gtk_tree_model_get_iter(model, &idn, dn)) {
1265 gtk_tree_path_free(dn);
1266 gtk_tree_path_free(sel);
1267 return;
1270 gtk_list_store_swap(GTK_LIST_STORE(model), &isel, &idn);
1272 account_list_set();
1274 gtk_tree_path_free(dn);
1275 gtk_tree_path_free(sel);
1278 static void account_set_default(GtkWidget *widget, gpointer data)
1280 PrefsAccount *ac_prefs;
1282 if (NULL == (ac_prefs = account_list_view_get_selected_account
1283 (edit_account.list_view)))
1284 return;
1286 /* we need to change the store variables by resetting everything
1287 * and setting the new default one */
1288 account_list_view_set_default_by_id(edit_account.list_view,
1289 ac_prefs->account_id);
1291 account_set_as_default(ac_prefs);
1292 account_list_view_set();
1294 cur_account = ac_prefs;
1295 account_flush_state();
1298 static void account_edit_close(GtkWidget *widget, gpointer data)
1300 account_list_set();
1301 account_write_config_all();
1303 if (!cur_account && account_list) {
1304 PrefsAccount *ac_prefs = (PrefsAccount *)account_list->data;
1305 account_set_as_default(ac_prefs);
1306 cur_account = ac_prefs;
1309 if (account_list_dirty)
1310 account_flush_state();
1312 gtk_widget_hide(edit_account.window);
1313 gtk_window_set_modal(GTK_WINDOW(edit_account.window), FALSE);
1314 inc_unlock();
1317 static gint account_delete_event(GtkWidget *widget, GdkEventAny *event,
1318 gpointer data)
1320 account_edit_close(NULL, NULL);
1321 return TRUE;
1324 static gboolean account_key_pressed(GtkWidget *widget, GdkEventKey *event,
1325 gpointer data)
1327 if (event && event->keyval == GDK_KEY_Escape)
1328 account_edit_close(NULL, NULL);
1329 return FALSE;
1332 static gboolean account_search_func_cb (GtkTreeModel *model, gint column, const gchar *key,
1333 GtkTreeIter *iter, gpointer search_data)
1335 gboolean retval;
1336 PrefsAccount *ac;
1338 gtk_tree_model_get (model, iter, ACCOUNT_DATA, &ac, -1);
1340 if (!ac->name || !key) return FALSE;
1342 retval = (strncmp (key, ac->account_name, strlen(key)) != 0);
1344 debug_print("selecting row\n");
1345 account_list_view_select_account(edit_account.list_view, ac->account_id);
1347 return retval;
1350 static void account_list_view_set(void)
1352 GList *cur;
1353 gint prev_sel_account;
1354 GtkListStore *store;
1356 store = GTK_LIST_STORE(gtk_tree_view_get_model
1357 (GTK_TREE_VIEW(edit_account.list_view)));
1359 prev_sel_account = account_list_view_get_selected_account_id
1360 (edit_account.list_view);
1362 gtk_list_store_clear(store);
1364 for (cur = account_list; cur != NULL; cur = cur->next) {
1365 account_list_store_insert_account_item(store, (PrefsAccount *)cur->data);
1366 if ((PrefsAccount *)cur->data == cur_account)
1367 account_list_view_select_account
1368 (edit_account.list_view,
1369 cur_account->account_id);
1372 if (prev_sel_account >= 0)
1373 account_list_view_select_account(edit_account.list_view,
1374 prev_sel_account);
1377 /* set account list from CList */
1378 static void account_list_set(void)
1380 /* want to make sure we iterate *IN ORDER*, so therefore using
1381 * gtk_tree_model_XXXX_nth_child() */
1382 gint row, n_rows;
1383 PrefsAccount *ac_prefs;
1384 GtkTreeModel *model = gtk_tree_view_get_model
1385 (GTK_TREE_VIEW(edit_account.list_view));
1387 while (account_list)
1388 account_list = g_list_remove(account_list, account_list->data);
1390 n_rows = gtk_tree_model_iter_n_children(model, NULL);
1392 for (row = 0; row < n_rows; row++) {
1393 GtkTreeIter iter;
1395 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
1396 g_warning("%s(%d) - no iter found???", __FILE__, __LINE__);
1397 continue;
1400 ac_prefs = NULL;
1401 gtk_tree_model_get(model, &iter,
1402 ACCOUNT_DATA, &ac_prefs,
1403 -1);
1404 if (ac_prefs)
1405 account_list = g_list_append(account_list, ac_prefs);
1410 *\brief finds the PrefsAccounts which should be used to answer a mail
1412 *\param msginfo The message to be answered
1413 *\param reply_autosel Indicates whether reply account autoselection is on
1415 *\return PrefsAccount * the correct account, NULL if not found
1417 PrefsAccount *account_get_reply_account(MsgInfo *msginfo, gboolean reply_autosel)
1419 PrefsAccount *account = NULL;
1420 /* select the account set in folderitem's property (if enabled) */
1421 if (msginfo->folder->prefs && msginfo->folder->prefs->enable_default_account)
1422 account = account_find_from_id(msginfo->folder->prefs->default_account);
1423 else if (folder_has_parent_of_type(msginfo->folder, F_QUEUE) ||
1424 folder_has_parent_of_type(msginfo->folder, F_OUTBOX) ||
1425 folder_has_parent_of_type(msginfo->folder, F_DRAFT)) {
1426 gchar *from = NULL;
1427 if (!procheader_get_header_from_msginfo
1428 (msginfo, &from, "From:")) {
1429 gchar *buf = from + strlen("From:");
1430 extract_address(buf);
1431 account = account_find_from_address(buf, FALSE);
1432 g_free(from);
1435 /* select account by to: and cc: header if enabled */
1436 if (reply_autosel) {
1437 gchar * field = NULL;
1438 int fieldno = 0;
1439 for (field = msginfo->to; fieldno++ < 2; field = msginfo->cc) {
1440 if (!account && field) {
1441 gchar *f = g_strdup(field);
1442 if (f) {
1443 gchar *cur = f;
1444 gchar *next = NULL;
1445 gchar *to = NULL;
1446 do {
1447 next = strchr_with_skip_quote(cur, '"', ',');
1448 if (next)
1449 (*next) = 0;
1450 Xstrdup_a(to, cur, return NULL);
1451 extract_address(to);
1452 account = account_find_from_address(to, FALSE);
1453 if (next)
1454 cur = next + 1;
1455 else
1456 break;
1457 } while (!account);
1458 g_free(f);
1462 if (!account) {
1463 gchar *deliveredto = NULL;
1464 if (!procheader_get_header_from_msginfo
1465 (msginfo, &deliveredto, "Delivered-To:")) {
1466 gchar *buf = deliveredto + strlen("Delivered-To:");
1467 extract_address(buf);
1468 account = account_find_from_address(buf, FALSE);
1469 g_free(deliveredto);
1474 /* select the account for the whole folder (IMAP / NNTP) */
1475 if (!account)
1476 /* FIXME: this is not right, because folder may be nested. we should
1477 * ascend the tree until we find a parent with proper account
1478 * information */
1479 account = msginfo->folder->folder->account;
1481 /* select current account */
1482 if (!account) account = cur_account;
1484 return account;
1488 *\brief Create data store
1490 static GtkListStore* account_create_data_store(void)
1492 return gtk_list_store_new(N_ACCOUNT_COLUMNS,
1493 G_TYPE_INT, /* ACCOUNT_IS_DEFAULT */
1494 G_TYPE_BOOLEAN, /* ACCOUNT_ENABLE_GET_ALL */
1495 G_TYPE_STRING, /* ACCOUNT_NAME */
1496 G_TYPE_STRING, /* ACCOUNT_PROTOCOL */
1497 G_TYPE_STRING, /* ACCOUNT_SERVER */
1498 G_TYPE_POINTER, /* ACCOUNT_DATA */
1499 -1);
1503 *\brief Insert an account item in the list.
1505 *\return GtkTreeRowReference * A tree row reference, which is guaranteed to
1506 * stable whatever operations are performed on the list.
1508 static void account_list_store_insert_account_item(GtkListStore *list_store,
1509 PrefsAccount *ac_prefs)
1511 GtkTreeIter iter;
1512 gboolean is_get_all = (ac_prefs->protocol == A_POP3 ||
1513 ac_prefs->protocol == A_IMAP4 ||
1514 ac_prefs->protocol == A_NNTP ||
1515 ac_prefs->protocol == A_LOCAL) &&
1516 ac_prefs->recv_at_getall;
1517 gchar *protocol, *server;
1519 #ifdef USE_GNUTLS
1520 protocol = ac_prefs->protocol == A_POP3 ?
1521 (ac_prefs->ssl_pop == SSL_TUNNEL ?
1522 "POP (TLS)" :
1523 ac_prefs->ssl_pop == SSL_STARTTLS ?
1524 "POP (STARTTLS)" : "POP") :
1525 ac_prefs->protocol == A_IMAP4 ?
1526 (ac_prefs->ssl_imap == SSL_TUNNEL ?
1527 "IMAP (TLS)" :
1528 ac_prefs->ssl_imap == SSL_STARTTLS ?
1529 "IMAP (STARTTLS)" : "IMAP") :
1530 ac_prefs->protocol == A_NNTP ?
1531 (ac_prefs->ssl_nntp == SSL_TUNNEL ?
1532 "NNTP (TLS)" : "NNTP") :
1533 ac_prefs->protocol == A_LOCAL ? "Local" :
1534 ac_prefs->protocol == A_NONE ? "SMTP" : "-";
1535 #else
1536 protocol = ac_prefs->protocol == A_POP3 ? "POP" :
1537 ac_prefs->protocol == A_IMAP4 ? "IMAP" :
1538 ac_prefs->protocol == A_LOCAL ? "Local" :
1539 ac_prefs->protocol == A_NNTP ? "NNTP" :
1540 ac_prefs->protocol == A_NONE ? "SMTP" : "-";
1541 #endif
1543 server= ac_prefs->protocol == A_NNTP ? ac_prefs->nntp_server :
1544 ac_prefs->protocol == A_LOCAL ? "-" :
1545 ac_prefs->protocol == A_NONE ? ac_prefs->smtp_server :
1546 ac_prefs->recv_server;
1548 gtk_list_store_append(list_store, &iter);
1549 gtk_list_store_set(list_store, &iter,
1550 ACCOUNT_IS_DEFAULT, ac_prefs->is_default ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL,
1551 ACCOUNT_ENABLE_GET_ALL, is_get_all,
1552 ACCOUNT_NAME, ac_prefs->account_name,
1553 ACCOUNT_PROTOCOL, protocol,
1554 ACCOUNT_SERVER, server,
1555 ACCOUNT_DATA, ac_prefs,
1556 -1);
1560 *\brief Create and set up account list view, including tasks like
1561 * creating the data store (\ref account_create_data_store()),
1562 * and setting up the account list's individual columns (\ref
1563 * account_create_list_view_columns()).
1565 *\return GtkWidget * The created list view widget.
1567 static GtkWidget *account_list_view_create(void)
1569 GtkTreeView *list_view;
1570 GtkTreeSelection *selector;
1571 GtkListStore *store = account_create_data_store();
1573 list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)));
1574 g_object_unref(G_OBJECT(store));
1575 #ifdef GENERIC_UMPC
1576 g_object_set(list_view, "allow-checkbox-mode", FALSE, NULL);
1577 #endif
1578 gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1579 gtk_tree_view_set_reorderable(list_view, TRUE);
1581 selector = gtk_tree_view_get_selection(list_view);
1582 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1584 /* create the columns */
1585 account_create_list_view_columns(GTK_WIDGET(list_view));
1587 /* set a double click listener */
1588 g_signal_connect(G_OBJECT(list_view), "row_activated",
1589 G_CALLBACK(account_double_clicked),
1590 list_view);
1592 g_signal_connect(G_OBJECT(list_view), "drag_begin",
1593 G_CALLBACK(drag_begin),
1594 list_view);
1596 g_signal_connect(G_OBJECT(list_view), "drag_end",
1597 G_CALLBACK(drag_end),
1598 list_view);
1600 gtk_tree_view_set_reorderable(list_view, TRUE);
1601 return GTK_WIDGET(list_view);
1604 static void account_create_list_view_columns(GtkWidget *list_view)
1606 GtkTreeViewColumn *column;
1607 GtkCellRenderer *renderer;
1609 renderer = gtk_cell_renderer_toggle_new();
1610 g_object_set(renderer,
1611 "radio", FALSE,
1612 "activatable", TRUE,
1613 NULL);
1614 column = gtk_tree_view_column_new_with_attributes
1615 (C_("Accounts List Get Column Name", "G"), renderer,
1616 "active", ACCOUNT_ENABLE_GET_ALL,
1617 NULL);
1618 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1619 gtk_tree_view_column_set_alignment (column, 0.5);
1620 CLAWS_SET_TIP(gtk_tree_view_column_get_widget(column),
1621 _("'Get Mail' retrieves mail from the checked accounts"));
1622 g_signal_connect(G_OBJECT(renderer), "toggled",
1623 G_CALLBACK(account_get_all_toggled),
1624 list_view);
1626 renderer = gtk_cell_renderer_text_new();
1627 column = gtk_tree_view_column_new_with_attributes
1628 (_("Name"), renderer,
1629 "text", ACCOUNT_NAME,
1630 "weight", ACCOUNT_IS_DEFAULT,
1631 NULL);
1632 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1634 renderer = gtk_cell_renderer_text_new();
1635 column = gtk_tree_view_column_new_with_attributes
1636 (_("Protocol"), renderer,
1637 "text", ACCOUNT_PROTOCOL,
1638 "weight", ACCOUNT_IS_DEFAULT,
1639 NULL);
1640 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1642 renderer = gtk_cell_renderer_text_new();
1643 column = gtk_tree_view_column_new_with_attributes
1644 (_("Server"), renderer,
1645 "text", ACCOUNT_SERVER,
1646 "weight", ACCOUNT_IS_DEFAULT,
1647 NULL);
1648 gtk_tree_view_set_search_column(GTK_TREE_VIEW(list_view), ACCOUNT_NAME);
1649 gtk_tree_view_set_search_equal_func(GTK_TREE_VIEW(list_view), account_search_func_cb , NULL, NULL);
1650 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1654 *\brief Get currently selected account (by its unique ID)
1656 static gint account_list_view_get_selected_account_id(GtkWidget *list_view)
1658 PrefsAccount *res = NULL;
1660 res = (PrefsAccount *)gtkut_tree_view_get_selected_pointer(
1661 GTK_TREE_VIEW(list_view), ACCOUNT_DATA,
1662 NULL, NULL, NULL);
1664 return (res != NULL ? res->account_id : -1);
1668 *\brief Get the tree path of the currently selected account
1670 static GtkTreePath *account_list_view_get_selected_account_path(GtkWidget *list_view)
1672 GtkTreeSelection *selector;
1673 GtkTreeModel *model;
1674 GtkTreeIter iter;
1676 selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
1678 if (!gtk_tree_selection_get_selected(selector, &model, &iter))
1679 return NULL;
1681 return gtk_tree_model_get_path(gtk_tree_view_get_model
1682 (GTK_TREE_VIEW(list_view)), &iter);
1686 *\brief Get the account data of the currently selected account
1688 static PrefsAccount *account_list_view_get_selected_account(GtkWidget *list_view)
1690 PrefsAccount *res =
1691 (PrefsAccount *)gtkut_tree_view_get_selected_pointer(
1692 GTK_TREE_VIEW(list_view), ACCOUNT_DATA,
1693 NULL, NULL, NULL);
1695 return res;
1699 *\brief Select a row by the account it represents
1701 *\return gboolean TRUE if found and selected, FALSE if not.
1703 static gboolean account_list_view_select_account(GtkWidget *list_view, gint account_id)
1705 FindAccountInStore fis;
1706 GtkTreeModel *model;
1708 fis.account_id = account_id;
1709 fis.path = NULL;
1711 model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1713 gtk_tree_model_foreach(model, (GtkTreeModelForeachFunc) find_account_in_store,
1714 &fis);
1716 if (fis.path) {
1717 GtkTreeSelection *selection;
1718 GtkTreePath* path;
1720 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
1721 gtk_tree_selection_select_iter(selection, &fis.iter);
1722 path = gtk_tree_model_get_path(model, &fis.iter);
1723 /* XXX returned path may not be valid??? create new one to be sure */
1724 gtk_tree_view_set_cursor(GTK_TREE_VIEW(list_view), path, NULL, FALSE);
1725 gtk_tree_path_free(path);
1728 return fis.path != NULL;
1732 *\brief Set a new default account by its ID. (There is only one
1733 * default account.)
1735 static void account_list_view_set_default_by_id(GtkWidget *list_view,
1736 gint account_id)
1738 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1740 gtk_tree_model_foreach
1741 (model, (GtkTreeModelForeachFunc) set_new_default_account,
1742 &account_id);
1745 static gboolean set_new_default_account(GtkTreeModel *model,
1746 GtkTreePath *path,
1747 GtkTreeIter *iter,
1748 gint *account_id)
1750 PrefsAccount *account = NULL;
1751 PangoWeight weight;
1753 gtk_tree_model_get(model, iter,
1754 ACCOUNT_DATA, &account,
1755 -1);
1757 if (*account_id == account->account_id)
1758 weight = PANGO_WEIGHT_NORMAL;
1759 else
1760 weight = PANGO_WEIGHT_BOLD;
1762 gtk_list_store_set(GTK_LIST_STORE(model), iter,
1763 ACCOUNT_IS_DEFAULT, weight, -1);
1765 return FALSE;
1768 static gboolean find_account_in_store(GtkTreeModel *model,
1769 GtkTreePath *path,
1770 GtkTreeIter *iter,
1771 FindAccountInStore *data)
1773 PrefsAccount *account = NULL;
1774 gtk_tree_model_get(model, iter, ACCOUNT_DATA, &account, -1);
1776 if (data->account_id == account->account_id) {
1777 data->path = path; /* signal we found it */
1778 data->iter = *iter;
1779 return TRUE;
1782 return FALSE;
1786 *\brief Triggered when "get all" column is activated or de-activated
1788 static void account_get_all_toggled(GtkCellRendererToggle *widget,
1789 gchar *path,
1790 GtkWidget *list_view)
1792 GtkTreeIter iter;
1793 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1794 PrefsAccount *ac = NULL;
1795 gboolean get_all;
1797 if (!gtk_tree_model_get_iter_from_string(model, &iter, path))
1798 return;
1800 gtk_tree_model_get(model, &iter,
1801 ACCOUNT_DATA, &ac,
1802 ACCOUNT_ENABLE_GET_ALL, &get_all,
1803 -1);
1805 /* check if the account has a selectable get all checkbox anyway... */
1806 if (!(ac->protocol == A_POP3 ||
1807 ac->protocol == A_IMAP4 ||
1808 ac->protocol == A_NNTP ||
1809 ac->protocol == A_LOCAL))
1810 return;
1812 /* set value in store */
1813 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
1814 ACCOUNT_ENABLE_GET_ALL, !get_all,
1815 -1);
1817 /* set value in account */
1818 ac->recv_at_getall ^= TRUE;
1821 static void account_double_clicked(GtkTreeView *list_view,
1822 GtkTreePath *path,
1823 GtkTreeViewColumn *column,
1824 gpointer data)
1826 account_edit_prefs(NULL, NULL);
1829 static void drag_begin(GtkTreeView *list_view,
1830 GdkDragContext *context,
1831 gpointer data)
1833 /* XXX unfortunately a completed drag & drop does not emit
1834 * a "rows_reordered" signal, but a "row_changed" signal.
1835 * So during drag and drop, listen to "row_changed", and
1836 * update the account list accordingly */
1838 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1839 g_signal_connect(G_OBJECT(model), "row_changed",
1840 G_CALLBACK(account_row_changed_while_drag_drop),
1841 list_view);
1844 static void drag_end(GtkTreeView *list_view,
1845 GdkDragContext *context,
1846 gpointer data)
1848 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1849 g_signal_handlers_disconnect_by_func(G_OBJECT(model),
1850 G_CALLBACK(account_row_changed_while_drag_drop),
1851 list_view);
1854 static void account_row_changed_while_drag_drop(GtkTreeModel *model,
1855 GtkTreePath *path,
1856 GtkTreeIter *iter,
1857 gpointer arg3,
1858 GtkTreeView *list_view)
1860 account_list_set();
1863 gchar *account_get_signature_str(PrefsAccount *account)
1865 gchar *sig_body = NULL;
1866 gchar *sig_str = NULL;
1867 gchar *utf8_sig_str = NULL;
1869 cm_return_val_if_fail(account != NULL, NULL);
1871 if (!account->sig_path)
1872 return NULL;
1874 if (account->sig_type == SIG_FILE) {
1875 gchar *sig_full_path;
1876 if (!g_path_is_absolute(account->sig_path)) {
1877 sig_full_path = g_build_filename(get_home_dir(), account->sig_path, NULL);
1878 } else {
1879 sig_full_path = g_strdup(account->sig_path);
1882 if (!is_file_or_fifo_exist(sig_full_path)) {
1883 g_warning("can't open signature file: '%s'", sig_full_path);
1884 g_free(sig_full_path);
1885 return NULL;
1888 debug_print("Reading signature from file '%s'\n", sig_full_path);
1889 gchar *tmp = file_read_to_str(sig_full_path);
1890 g_free(sig_full_path);
1892 if (!tmp)
1893 return NULL;
1895 sig_body = normalize_newlines(tmp);
1896 g_free(tmp);
1897 } else {
1898 sig_body = get_command_output(account->sig_path);
1901 if (account->sig_sep) {
1902 sig_str = g_strconcat("\n", account->sig_sep, "\n", sig_body,
1903 NULL);
1904 g_free(sig_body);
1905 } else
1906 sig_str = g_strconcat("\n", sig_body, NULL);
1908 if (sig_str) {
1909 if (g_utf8_validate(sig_str, -1, NULL) == TRUE)
1910 utf8_sig_str = sig_str;
1911 else {
1912 utf8_sig_str = conv_codeset_strdup
1913 (sig_str, conv_get_locale_charset_str_no_utf8(),
1914 CS_INTERNAL);
1915 g_free(sig_str);
1919 return utf8_sig_str;
1922 PrefsAccount *account_get_cur_account (void)
1924 return cur_account;
1927 gboolean password_get(const gchar *user,
1928 const gchar *server,
1929 const gchar *protocol,
1930 guint16 port,
1931 gchar **password)
1933 PasswordRequest req;
1935 /* all have to be set */
1936 cm_return_val_if_fail(user != NULL, FALSE);
1937 cm_return_val_if_fail(server != NULL, FALSE);
1938 cm_return_val_if_fail(protocol != NULL, FALSE);
1939 cm_return_val_if_fail(port != 0, FALSE);
1941 req.user = user;
1942 req.server = server;
1943 req.protocol = protocol;
1944 req.port = port;
1946 if (hooks_invoke(PASSWORD_GET_HOOKLIST, &req)) {
1947 *password = req.password;
1948 return TRUE;
1950 return FALSE;
1953 static GSList *account_sigsep_list = NULL;
1955 /* create a list of unique signatures from accounts list */
1956 void account_sigsep_matchlist_create(void)
1958 GList *cur_ac = NULL;
1959 PrefsAccount *ac_prefs = NULL;
1961 if (account_sigsep_list)
1962 return;
1964 account_sigsep_list = g_slist_prepend(account_sigsep_list, g_strdup("-- "));
1965 for (cur_ac = account_get_list();
1966 cur_ac != NULL;
1967 cur_ac = g_list_next(cur_ac)) {
1968 ac_prefs = (PrefsAccount *)cur_ac->data;
1970 if (ac_prefs->sig_sep && *ac_prefs->sig_sep != '\0') {
1971 if (!g_slist_find_custom(account_sigsep_list, ac_prefs->sig_sep,
1972 (GCompareFunc)g_strcmp0)) {
1973 account_sigsep_list = g_slist_prepend(account_sigsep_list,
1974 g_strdup(ac_prefs->sig_sep));
1980 /* delete the list of signatures created by account_sigsep_matchlist_create() */
1981 void account_sigsep_matchlist_delete(void)
1983 if (account_sigsep_list) {
1984 slist_free_strings_full(account_sigsep_list);
1985 account_sigsep_list = NULL;
1989 /* match a string against all signatures in list, using the specified format */
1990 gboolean account_sigsep_matchlist_str_found(const gchar *str, const gchar *format)
1992 gchar *tmp = NULL;
1993 gboolean found = FALSE;
1994 GSList *item;
1996 for (item = account_sigsep_list;
1997 item != NULL && !found;
1998 item = g_slist_next(item)) {
1999 tmp = g_strdup_printf(format, (gchar *)item->data);
2000 if (tmp) {
2001 found = (strcmp(tmp, str) == 0);
2002 g_free(tmp);
2003 } else {
2004 g_warning("account_sigsep_matchlist_str_found: g_strdup_printf failed, check format '%s'",
2005 format);
2006 return FALSE;
2009 return found;
2012 /* match M first char of a string against all signatures in list, using the specified format */
2013 gboolean account_sigsep_matchlist_nchar_found(const gchar *str, const gchar *format)
2015 gchar *tmp = NULL;
2016 gboolean found = FALSE;
2017 GSList *item;
2018 gint len;
2020 for (item = account_sigsep_list;
2021 item != NULL && !found;
2022 item = g_slist_next(item)) {
2023 tmp = g_strdup_printf(format, (gchar *)item->data);
2024 if (tmp) {
2025 len = strlen(tmp);
2026 found = (strncmp(tmp, str, len) == 0);
2027 g_free(tmp);
2028 } else {
2029 g_warning("account_sigsep_matchlist_nchar_found: g_strdup_printf failed, check format '%s'",
2030 format);
2031 return FALSE;
2034 return found;