d06ce8cbb7b8e0f0e929deb735782802ffe6a1cb
[claws.git] / src / account.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2015 Hiroyuki Yamamoto and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include "defs.h"
26
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>
33
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
59 enum {
60         ACCOUNT_IS_DEFAULT,
61         ACCOUNT_ENABLE_GET_ALL,
62         ACCOUNT_NAME,
63         ACCOUNT_PROTOCOL,
64         ACCOUNT_SERVER,
65         ACCOUNT_DATA,
66         N_ACCOUNT_COLUMNS
67 };
68
69
70 typedef enum
71 {
72         COL_DEFAULT     = 0,
73         COL_GETALL,
74         COL_NAME,
75         COL_PROTOCOL,
76         COL_SERVER,
77         N_EDIT_ACCOUNT_COLS
78 } EditAccountColumnPos;
79
80 PrefsAccount *cur_account;
81
82 static GList *account_list = NULL;
83 static gboolean account_list_dirty = FALSE;
84
85 static struct EditAccount {
86         GtkWidget *window;
87         GtkWidget *list_view;
88         GtkWidget *close_btn;
89 } edit_account;
90
91 static void account_edit_create         (void);
92 static void           account_destroy           (PrefsAccount   *ac_prefs);
93 static void           account_set_as_default    (PrefsAccount   *ac_prefs);
94 static void           account_set_menu          (void);
95
96 static void account_edit_prefs          (GtkWidget *widget, gpointer data);
97 static void account_delete              (GtkWidget *widget, gpointer data);
98 static void account_clone               (GtkWidget *widget, gpointer data);
99
100 static void account_up                  (GtkWidget *widget, gpointer data);
101 static void account_down                (GtkWidget *widget, gpointer data);
102
103 static void account_set_default         (GtkWidget *widget, gpointer data);
104
105 static void account_edit_close          (GtkWidget *widget, gpointer data);
106
107 static gint account_delete_event        (GtkWidget      *widget,
108                                          GdkEventAny    *event,
109                                          gpointer        data);
110 static void account_size_allocate_cb(GtkWidget *widget,
111                                          GtkAllocation *allocation);
112 static gboolean account_key_pressed     (GtkWidget      *widget,
113                                          GdkEventKey    *event,
114                                          gpointer        data);
115 static gboolean account_search_func_cb (GtkTreeModel *model, gint column, 
116                                                 const gchar *key, GtkTreeIter *iter, 
117                                                 gpointer search_data);
118 static void account_list_view_set       (void);
119
120 static void account_list_set            (void);
121
122 typedef struct FindAccountInStore {
123         gint             account_id;
124         GtkTreePath     *path;
125         GtkTreeIter      iter;
126 } FindAccountInStore;
127
128 static GtkListStore* account_create_data_store  (void);
129
130 static void account_list_store_insert_account_item (GtkListStore        *list_store,
131                                                    PrefsAccount *account_data);
132
133 static GtkWidget *account_list_view_create      (void);
134 static void account_create_list_view_columns    (GtkWidget *list_view);
135
136 static gint account_list_view_get_selected_account_id           (GtkWidget *list_view);
137 static GtkTreePath *account_list_view_get_selected_account_path (GtkWidget *list_view);
138 static PrefsAccount *account_list_view_get_selected_account             (GtkWidget *list_view);
139 static gboolean account_list_view_select_account                        (GtkWidget *list_view, 
140                                                                  gint       account_id);
141
142 static void account_list_view_set_default_by_id(GtkWidget *list_view,
143                                                 gint account_id);
144
145 static gboolean set_new_default_account         (GtkTreeModel *model,
146                                                  GtkTreePath  *path,
147                                                  GtkTreeIter  *iter,
148                                                  gint         *account_id);
149
150 static gboolean find_account_in_store           (GtkTreeModel *model,
151                                                  GtkTreePath  *path,
152                                                  GtkTreeIter  *iter,
153                                                  FindAccountInStore *data);
154
155 static void account_get_all_toggled             (GtkCellRendererToggle  *widget, 
156                                                  gchar                  *path, 
157                                                  GtkWidget              *list_view);
158
159 static void account_autocheck_toggled           (GtkCellRendererToggle  *widget,
160                                                  gchar                  *path,
161                                                  GtkWidget              *list_view);
162
163 static void account_double_clicked              (GtkTreeView            *list_view,
164                                                  GtkTreePath            *path,
165                                                  GtkTreeViewColumn      *column,
166                                                  gpointer                data);
167                                                  
168 static void drag_begin                          (GtkTreeView *list_view,
169                                                  GdkDragContext *context,
170                                                  gpointer data);
171
172 static void drag_end                            (GtkTreeView *list_view,
173                                                  GdkDragContext *context,
174                                                  gpointer data);
175                       
176 static void account_row_changed_while_drag_drop (GtkTreeModel *model, 
177                                                  GtkTreePath  *path,
178                                                  GtkTreeIter  *iter,
179                                                  gpointer      arg3,
180                                                  GtkTreeView  *list_view);
181
182 static void account_flush_state(void)
183 {
184         account_set_menu();
185         main_window_reflect_prefs_all();
186
187         account_list_dirty = FALSE;
188 }
189
190 void account_read_config_all(void)
191 {
192         GSList *ac_label_list = NULL, *cur;
193         gchar *rcpath;
194         FILE *fp;
195         gchar buf[PREFSBUFSIZE];
196         PrefsAccount *ac_prefs;
197
198         debug_print("Reading all config for each account...\n");
199
200         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACCOUNT_RC, NULL);
201         if ((fp = g_fopen(rcpath, "rb")) == NULL) {
202                 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
203                 g_free(rcpath);
204                 return;
205         }
206         g_free(rcpath);
207
208         while (fgets(buf, sizeof(buf), fp) != NULL) {
209                 if (!strncmp(buf, "[Account: ", 10)) {
210                         strretchomp(buf);
211                         memmove(buf, buf + 1, sizeof(buf) - 1);
212                         buf[strlen(buf) - 1] = '\0';
213                         debug_print("Found label: %s\n", buf);
214                         ac_label_list = g_slist_append(ac_label_list,
215                                                        g_strdup(buf));
216                 }
217         }
218         fclose(fp);
219
220         /* read config data from file */
221         cur_account = NULL;
222         for (cur = ac_label_list; cur != NULL; cur = cur->next) {
223                 ac_prefs = prefs_account_new_from_config((gchar *)cur->data);
224                 account_list = g_list_append(account_list, ac_prefs);
225                 if (ac_prefs->is_default)
226                         cur_account = ac_prefs;
227         }
228         /* if default is not set, assume first account as default */
229         if (!cur_account && account_list) {
230                 ac_prefs = (PrefsAccount *)account_list->data;
231                 account_set_as_default(ac_prefs);
232                 cur_account = ac_prefs;
233         }
234
235         account_set_menu();
236         main_window_reflect_prefs_all_now();
237
238         while (ac_label_list) {
239                 g_free(ac_label_list->data);
240                 ac_label_list = g_slist_remove(ac_label_list,
241                                                ac_label_list->data);
242         }
243 }
244
245 void account_write_config_all(void)
246 {
247         prefs_account_write_config_all(account_list);
248 }
249
250 /*
251  * account_find_all_from_address:
252  * @ac_list: initial list of accounts. NULL to create a new one.
253  * Accounts found in the @address will be appended to this list.
254  * @address: Email address string.
255  *
256  * Find all the mail (not news) accounts within the specified address.
257  *
258  * Return value: the original accounts list with the found accounts appended.
259  */
260 GList *account_find_all_from_address(GList *ac_list, const gchar *address)
261 {
262         GList *cur;
263         PrefsAccount *ac;
264
265         if (address == NULL)
266                 return ac_list;
267
268         for (cur = account_list; cur != NULL; cur = cur->next) {
269                 ac = (PrefsAccount *)cur->data;
270                 if (ac->protocol != A_NNTP && ac->address &&
271                     strcasestr(address, ac->address) != NULL)
272                         ac_list = g_list_append(ac_list, ac);
273         }
274         return ac_list;
275 }
276
277 GList *account_find_all(void)
278 {
279         GList *cur;
280         PrefsAccount *ac;
281         GList *ac_list = NULL;
282
283         for (cur = account_list; cur != NULL; cur = cur->next) {
284                 ac = (PrefsAccount *)cur->data;
285                 ac_list = g_list_append(ac_list, ac);
286         }
287         return ac_list;
288 }
289
290 PrefsAccount *account_find_from_smtp_server(const gchar *address,
291                                             const gchar *smtp_server)
292 {
293         GList *cur;
294         PrefsAccount *ac;
295
296         cm_return_val_if_fail(address != NULL, NULL);
297         cm_return_val_if_fail(smtp_server != NULL, NULL);
298
299         for (cur = account_list; cur != NULL; cur = cur->next) {
300                 ac = (PrefsAccount *)cur->data;
301                 if (!strcmp2(address, ac->address) &&
302                     !strcmp2(smtp_server, ac->smtp_server))
303                         return ac;
304         }
305
306         return NULL;
307 }
308
309 /*
310  * account_find_from_address:
311  * @address: Email address string.
312  *
313  * Find a mail (not news if newsgroups_ok is FALSE) account with the specified email address.
314  *
315  * Return value: The found account, or NULL if not found.
316  */
317 PrefsAccount *account_find_from_address(const gchar *address, gboolean newsgroups_ok)
318 {
319         GList *cur;
320         PrefsAccount *ac;
321
322         cm_return_val_if_fail(address != NULL, NULL);
323
324         for (cur = account_list; cur != NULL; cur = cur->next) {
325                 ac = (PrefsAccount *)cur->data;
326                 if ((ac->protocol != A_NNTP || newsgroups_ok) && ac->address &&
327                     g_ascii_strcasecmp(address, ac->address) == 0)
328                         return ac;
329         }
330
331         return NULL;
332 }
333
334 PrefsAccount *account_find_from_id(gint id)
335 {
336         GList *cur;
337         PrefsAccount *ac;
338
339         for (cur = account_list; cur != NULL; cur = cur->next) {
340                 ac = (PrefsAccount *)cur->data;
341                 if (id == ac->account_id)
342                         return ac;
343         }
344
345         return NULL;
346 }
347
348 PrefsAccount *account_find_from_item(FolderItem *item)
349 {
350         PrefsAccount *ac;
351
352         cm_return_val_if_fail(item != NULL, NULL);
353
354         ac = item->account;
355         if (!ac) {
356                 FolderItem *cur_item = folder_item_parent(item);
357                 while (cur_item != NULL) {
358                         if (cur_item->account && cur_item->apply_sub) {
359                                 ac = cur_item->account;
360                                 break;
361                         }                               
362                         cur_item = folder_item_parent(cur_item);
363                 }
364         }
365         if (!ac)
366                 ac = item->folder->account;
367
368         return ac;
369 }
370
371 static void account_set_menu(void)
372 {
373         main_window_set_account_menu(account_list);
374 }
375
376 void account_set_menu_only_toolbar(void)
377 {
378         main_window_set_account_menu_only_toolbar(account_list);
379 }
380
381 GList *account_get_list(void)
382 {
383         return account_list;
384 }
385
386 void account_edit_focus(void)
387 {
388         if (edit_account.window == NULL) {
389                 return;
390         }
391         manage_window_set_transient(GTK_WINDOW(edit_account.window));
392         gtk_widget_grab_focus(edit_account.close_btn);
393         gtk_widget_show(edit_account.window);
394         gtk_window_set_modal(GTK_WINDOW(edit_account.window), TRUE);
395         manage_window_focus_in(edit_account.window, NULL, NULL);
396 }
397
398 void account_edit_open(gpointer a, gpointer b)
399 {
400         inc_lock();
401
402         account_list_dirty = FALSE;
403
404         if (compose_get_compose_list()) {
405                 alertpanel_error(_("Some composing windows are open.\n"
406                                    "Please close all the composing "
407                                    "windows before editing accounts."));
408                 inc_unlock();
409                 return;
410         }
411
412         debug_print("Opening account edit window...\n");
413
414         if (!edit_account.window)
415                 account_edit_create();
416
417         account_list_view_set();
418
419         account_edit_focus();
420 }
421
422 void account_add(void)
423 {
424         PrefsAccount *ac_prefs;
425
426         ac_prefs = prefs_account_open(NULL, &account_list_dirty);
427
428         if (!ac_prefs) return;
429
430         account_edit_focus();
431
432         account_list = g_list_append(account_list, ac_prefs);
433
434         if (ac_prefs->is_default)
435                 account_set_as_default(ac_prefs);
436
437         account_list_view_set();
438
439         if (ac_prefs->protocol == A_IMAP4 || ac_prefs->protocol == A_NNTP) {
440                 Folder *folder;
441
442                 if (ac_prefs->protocol == A_IMAP4) {
443                         folder = folder_new(folder_get_class_from_string("imap"), ac_prefs->account_name,
444                                             ac_prefs->recv_server);
445                 } else {
446                         folder = folder_new(folder_get_class_from_string("news"), ac_prefs->account_name,
447                                             ac_prefs->nntp_server);
448                 }
449                 if (folder == NULL) {
450                         alertpanel_error(_("Can't create folder."));
451                         return;
452                 }
453                 folder->account = ac_prefs;
454                 ac_prefs->folder = folder;
455                 folder_add(folder);
456                 if (ac_prefs->protocol == A_IMAP4)
457                         folder->klass->create_tree(folder);
458                 folderview_set_all();
459                 folder_write_list();
460         }
461 }
462
463 void account_open(PrefsAccount *ac_prefs, gboolean called_from_acc_list)
464 {
465         gboolean prev_default;
466         gchar *ac_name, *old_prefix, *new_prefix;
467         gboolean account_dirty = FALSE;
468
469         cm_return_if_fail(ac_prefs != NULL);
470
471         if (compose_get_compose_list()) {
472                 alertpanel_error(_("Some composing windows are open.\n"
473                                    "Please close all the composing "
474                                    "windows before editing accounts."));
475                 return;
476         }
477
478         prev_default = ac_prefs->is_default;
479         Xstrdup_a(ac_name, ac_prefs->account_name ? ac_prefs->account_name : "",
480                   return);
481
482         prefs_account_open(ac_prefs, &account_dirty);
483
484         if (called_from_acc_list)
485                 account_edit_focus();
486
487         if (account_dirty) {
488                 if (!prev_default && ac_prefs->is_default)
489                         account_set_as_default(ac_prefs);
490
491                 if (ac_prefs->folder && strcmp2(ac_name, ac_prefs->account_name) != 0) {
492                         old_prefix = folder_get_identifier(FOLDER(ac_prefs->folder));
493                         folder_set_name(FOLDER(ac_prefs->folder),
494                                         ac_prefs->account_name);
495                         folderview_set_all();
496                         folder_prefs_save_config_recursive(FOLDER(ac_prefs->folder));
497                         new_prefix = folder_get_identifier(FOLDER(ac_prefs->folder));
498
499                         account_rename_path(old_prefix, new_prefix);
500                         prefs_filtering_rename_path(old_prefix, new_prefix);
501                         prefs_actions_rename_path(old_prefix, new_prefix);
502                         
503                         g_free(old_prefix);
504                         g_free(new_prefix);
505                 }
506
507                 account_write_config_all();
508
509                 account_flush_state();
510         }
511 }
512
513 static void account_set_as_default(PrefsAccount *ac_prefs)
514 {
515         PrefsAccount *ap;
516         GList *cur;
517
518         for (cur = account_list; cur != NULL; cur = cur->next) {
519                 ap = (PrefsAccount *)cur->data;
520                 if (ap->is_default)
521                         ap->is_default = FALSE;
522         }
523
524         ac_prefs->is_default = TRUE;
525 }
526
527 PrefsAccount *account_get_default(void)
528 {
529         PrefsAccount *ap;
530         GList *cur;
531
532         for (cur = account_list; cur != NULL; cur = cur->next) {
533                 ap = (PrefsAccount *)cur->data;
534                 if (ap->is_default)
535                         return ap;
536         }
537
538         return NULL;
539 }
540
541 void account_set_missing_folder(void)
542 {
543         PrefsAccount *ap;
544         GList *cur;
545
546         for (cur = account_list; cur != NULL; cur = cur->next) {
547                 ap = (PrefsAccount *)cur->data;
548                 if ((ap->protocol == A_IMAP4 || ap->protocol == A_NNTP) &&
549                     !ap->folder) {
550                         Folder *folder;
551
552                         if (ap->protocol == A_IMAP4) {
553                                 folder = folder_new(folder_get_class_from_string("imap"), ap->account_name,
554                                                     ap->recv_server);
555                         } else {
556                                 folder = folder_new(folder_get_class_from_string("news"), ap->account_name,
557                                                     ap->nntp_server);
558                         }
559                         if (folder == NULL)
560                                 return;
561                         folder->account = ap;
562                         ap->folder = folder;
563                         folder_add(folder);
564                         if (ap->protocol == A_IMAP4)
565                                 folder->klass->create_tree(folder);
566                         folder_write_list();
567
568                 }
569         }
570 }
571
572 #define CHECK_CHANGE_FOLDER(folder) {                                           \
573         if (folder && !strncmp(folder, old_id, strlen(old_id))) {               \
574                 if (strlen(folder) == strlen(old_id)) {                         \
575                         g_free(folder);                                         \
576                         folder = g_strdup(new_id);                              \
577                 } else if (strlen(folder) > strlen(old_id)                      \
578                   && folder[strlen(old_id)] == G_DIR_SEPARATOR) {               \
579                         gchar *new_path = g_strdup_printf("%s%s",               \
580                                         new_id, (folder + strlen(old_id)));     \
581                         g_free(folder);                                         \
582                         folder = new_path;                                      \
583                 }                                                               \
584         }                                                                       \
585 }
586
587 void account_rename_path(const gchar *old_id, const gchar *new_id)
588 {
589         GList *cur = account_list;
590         for (; cur != NULL; cur = g_list_next(cur)) {
591                 PrefsAccount *ap = (PrefsAccount *)cur->data;
592                 CHECK_CHANGE_FOLDER(ap->inbox);
593                 CHECK_CHANGE_FOLDER(ap->local_inbox);
594                 CHECK_CHANGE_FOLDER(ap->queue_folder);
595                 CHECK_CHANGE_FOLDER(ap->sent_folder);
596                 CHECK_CHANGE_FOLDER(ap->draft_folder);
597                 CHECK_CHANGE_FOLDER(ap->trash_folder);
598         }
599 }
600
601 FolderItem *account_get_special_folder(PrefsAccount *ac_prefs,
602                                        SpecialFolderItemType type)
603 {
604         FolderItem *item = NULL;
605
606         cm_return_val_if_fail(ac_prefs != NULL, NULL);
607
608         switch (type) {
609         case F_INBOX:
610                 if (ac_prefs->folder)
611                         item = FOLDER(ac_prefs->folder)->inbox;
612                 if (!item)
613                         item = folder_get_default_inbox();
614                 break;
615         case F_OUTBOX:
616                 if (ac_prefs->set_sent_folder && ac_prefs->sent_folder) {
617                         item = folder_find_item_from_identifier
618                                 (ac_prefs->sent_folder);
619                 }
620                 if (!item) {
621                         if (ac_prefs->folder)
622                                 item = FOLDER(ac_prefs->folder)->outbox;
623                         if (!item)
624                                 item = folder_get_default_outbox_for_class(F_MH);
625                         if (!item)
626                                 item = folder_get_default_outbox();
627                 }
628                 break;
629         case F_DRAFT:
630                 if (ac_prefs->set_draft_folder && ac_prefs->draft_folder) {
631                         item = folder_find_item_from_identifier
632                                 (ac_prefs->draft_folder);
633                 }
634                 if (!item) {
635                         if (ac_prefs->folder)
636                                 item = FOLDER(ac_prefs->folder)->draft;
637                         if (!item)
638                                 item = folder_get_default_draft_for_class(F_MH);
639                         if (!item)
640                                 item = folder_get_default_draft();
641                 }
642                 break;
643         case F_QUEUE:
644                 if (ac_prefs->set_queue_folder && ac_prefs->queue_folder) {
645                         item = folder_find_item_from_identifier
646                                 (ac_prefs->queue_folder);
647                 }
648                 if (!item) {
649                         if (ac_prefs->folder)
650                                 item = FOLDER(ac_prefs->folder)->queue;
651                         if (!item)
652                                 item = folder_get_default_queue_for_class(F_MH);
653                         if (!item)
654                                 item = folder_get_default_queue();
655                 }
656                 break;
657         case F_TRASH:
658                 if (ac_prefs->set_trash_folder && ac_prefs->trash_folder) {
659                         item = folder_find_item_from_identifier
660                                 (ac_prefs->trash_folder);
661                 }
662                 if (!item) {
663                         if (ac_prefs->folder)
664                                 item = FOLDER(ac_prefs->folder)->trash;
665                         if (!item)
666                                 item = folder_get_default_trash_for_class(F_MH);
667                         if (!item)
668                                 item = folder_get_default_trash();
669                 }
670                 break;
671         default:
672                 break;
673         }
674
675         return item;
676 }
677
678 void account_destroy(PrefsAccount *ac_prefs)
679 {
680         cm_return_if_fail(ac_prefs != NULL);
681
682         folder_unref_account_all(ac_prefs);
683
684         account_list = g_list_remove(account_list, ac_prefs);
685
686         if (cur_account == ac_prefs) cur_account = NULL;
687         if (!cur_account && account_list) {
688                 cur_account = account_get_default();
689                 if (!cur_account) {
690                         ac_prefs = (PrefsAccount *)account_list->data;
691                         account_set_as_default(ac_prefs);
692                         cur_account = ac_prefs;
693                 }
694         }
695 }
696
697 /*!
698  *\brief        Save Gtk object size to prefs dataset
699  */
700 static void account_size_allocate_cb(GtkWidget *widget,
701                                          GtkAllocation *allocation)
702 {
703         cm_return_if_fail(allocation != NULL);
704
705         prefs_common.accountswin_width = allocation->width;
706         prefs_common.accountswin_height = allocation->height;
707 }
708
709 static void account_edit_create(void)
710 {
711         GtkWidget *window;
712         GtkWidget *vbox;
713         GtkWidget *label;
714         GtkWidget *hbox;
715         GtkWidget *scrolledwin;
716         GtkWidget *list_view;
717
718         GtkWidget *vbox2;
719         GtkWidget *add_btn;
720         GtkWidget *edit_btn;
721         GtkWidget *del_btn;
722         GtkWidget *clone_btn;
723         GtkWidget *up_btn;
724         GtkWidget *down_btn;
725
726         GtkWidget *default_btn;
727
728         GtkWidget *confirm_area;
729         GtkWidget *help_btn;
730         GtkWidget *close_btn;
731
732         static GdkGeometry geometry;
733
734         debug_print("Creating account edit window...\n");
735
736         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "account");
737         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
738         gtk_window_set_title (GTK_WINDOW (window), _("Edit accounts"));
739         g_signal_connect (G_OBJECT (window), "delete_event",
740                           G_CALLBACK (account_delete_event), NULL);
741         g_signal_connect (G_OBJECT (window), "key_press_event",
742                           G_CALLBACK (account_key_pressed), NULL);
743         MANAGE_WINDOW_SIGNALS_CONNECT (window);
744         gtk_widget_realize(window);
745
746         vbox = gtk_vbox_new (FALSE, 10);
747         gtk_widget_show (vbox);
748         gtk_container_add (GTK_CONTAINER (window), vbox);
749
750         hbox = gtk_hbox_new (FALSE, 0);
751         gtk_widget_show (hbox);
752         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
753
754         label = gtk_label_new
755                 (_("Using 'Get Mail' will retrieve messages from your Accounts "
756                    "in the order given, the checkbox indicates which accounts "
757                    "will be included. Bold text indicates the default account."));
758         gtk_widget_show (label);
759         gtk_widget_set_size_request(GTK_WIDGET(label), 
760                                     prefs_common.accountswin_width-8, -1);
761         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 4);
762         gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
763         gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
764
765         hbox = gtk_hbox_new (FALSE, 8);
766         gtk_widget_show (hbox);
767         gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
768         gtk_container_set_border_width (GTK_CONTAINER (hbox), 2);
769
770         scrolledwin = gtk_scrolled_window_new (NULL, NULL);
771         gtk_widget_show (scrolledwin);
772         gtk_box_pack_start (GTK_BOX (hbox), scrolledwin, TRUE, TRUE, 0);
773         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwin),
774                                         GTK_POLICY_AUTOMATIC,
775                                         GTK_POLICY_AUTOMATIC);
776
777         list_view = account_list_view_create();
778         gtk_widget_show(list_view);
779         gtk_container_add(GTK_CONTAINER(scrolledwin), list_view);
780
781         vbox2 = gtk_vbox_new (FALSE, 0);
782         gtk_widget_show (vbox2);
783         gtk_box_pack_start (GTK_BOX (hbox), vbox2, FALSE, FALSE, 0);
784
785         add_btn = gtk_button_new_from_stock(GTK_STOCK_NEW);
786         gtk_widget_show (add_btn);
787         gtk_box_pack_start (GTK_BOX (vbox2), add_btn, FALSE, FALSE, 4);
788         g_signal_connect (G_OBJECT(add_btn), "clicked",
789                           G_CALLBACK (account_add), NULL);
790
791         edit_btn = gtk_button_new_from_stock (GTK_STOCK_EDIT);
792         gtk_widget_show (edit_btn);
793         gtk_box_pack_start (GTK_BOX (vbox2), edit_btn, FALSE, FALSE, 4);
794         g_signal_connect (G_OBJECT(edit_btn), "clicked",
795                           G_CALLBACK (account_edit_prefs), NULL);
796
797         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
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
803         clone_btn = gtk_button_new_from_stock(GTK_STOCK_COPY);
804         gtk_widget_show (clone_btn);
805         gtk_box_pack_start (GTK_BOX (vbox2), clone_btn, FALSE, FALSE, 4);
806         g_signal_connect(G_OBJECT(clone_btn), "clicked",
807                          G_CALLBACK(account_clone), NULL);
808         
809         down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
810         gtk_widget_show (down_btn);
811         gtk_box_pack_end (GTK_BOX (vbox2), down_btn, FALSE, FALSE, 4);
812         g_signal_connect (G_OBJECT(down_btn), "clicked",
813                           G_CALLBACK (account_down), NULL);
814
815         up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
816         gtk_widget_show (up_btn);
817         gtk_box_pack_end (GTK_BOX (vbox2), up_btn, FALSE, FALSE, 4);
818         g_signal_connect (G_OBJECT(up_btn), "clicked",
819                           G_CALLBACK (account_up), NULL);
820
821         hbox = gtk_hbox_new (FALSE, 8);
822         gtk_widget_show (hbox);
823         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
824
825         default_btn = gtk_button_new_with_mnemonic
826                 (_(" _Set as default account "));
827         gtk_widget_show (default_btn);
828         gtk_box_pack_start (GTK_BOX (hbox), default_btn, FALSE, FALSE, 0);
829         g_signal_connect (G_OBJECT(default_btn), "clicked",
830                           G_CALLBACK (account_set_default), NULL);
831
832         gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
833                         &close_btn, GTK_STOCK_CLOSE,
834                         NULL, NULL, NULL, NULL);
835         gtk_widget_show(confirm_area);
836
837         gtk_box_pack_end (GTK_BOX (hbox), confirm_area, FALSE, FALSE, 0);
838         gtk_widget_grab_default (close_btn);
839
840         g_signal_connect (G_OBJECT (close_btn), "clicked",
841                           G_CALLBACK (account_edit_close),
842                           NULL);
843         g_signal_connect(G_OBJECT(help_btn), "clicked",
844                         G_CALLBACK(manual_open_with_anchor_cb),
845                         MANUAL_ANCHOR_ACCOUNTPREFS);
846
847
848         g_signal_connect(G_OBJECT(window), "size_allocate",
849                          G_CALLBACK(account_size_allocate_cb), NULL);
850
851         if (!geometry.min_height) {
852                 geometry.min_width = 500;
853                 geometry.min_height = 380;
854         }
855
856         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
857                                       GDK_HINT_MIN_SIZE);
858         gtk_widget_set_size_request(window, prefs_common.accountswin_width,
859                                     prefs_common.accountswin_height);
860 #ifdef G_OS_WIN32
861         gtk_window_move(GTK_WINDOW(window), 48, 48);
862 #endif
863
864         edit_account.window    = window;
865         edit_account.list_view = list_view;
866         edit_account.close_btn = close_btn;
867 }
868
869 static void account_edit_prefs(GtkWidget *widget, gpointer data)
870 {
871         PrefsAccount *ac_prefs;
872
873         ac_prefs = account_list_view_get_selected_account(edit_account.list_view);
874         
875         if (ac_prefs) {
876                 account_open(ac_prefs, TRUE);
877                 account_list_view_set();
878         }               
879 }
880
881 static gboolean account_delete_references_func(GNode *node, gpointer data)
882 {
883         FolderItem *item;
884         gint account;
885
886         cm_return_val_if_fail(node->data != NULL, FALSE);
887
888         item = FOLDER_ITEM(node->data);
889         account = GPOINTER_TO_INT(data);
890
891         if(!item->prefs) /* && item->prefs->stype == F_NORMAL */
892                 return FALSE;
893         if(item->prefs->default_account != account)
894                 return FALSE;
895         
896         item->prefs->enable_default_account = FALSE;
897         item->prefs->default_account = 0;
898         folder_item_prefs_save_config(item);
899
900         return FALSE;
901 }
902
903         
904 #define ACP_FDUP(fld) ac_clon->fld = ((ac_prefs->fld) != NULL)?\
905                                      g_strdup(ac_prefs->fld): NULL
906 #define ACP_FASSIGN(fld) ac_clon->fld = ac_prefs->fld
907 static void account_clone(GtkWidget *widget, gpointer data)
908 {
909         PrefsAccount *ac_prefs, *ac_clon;
910         GSList *hdrs = NULL;
911         CustomHeader *cch = NULL, *ch = NULL;
912         
913         ac_prefs = account_list_view_get_selected_account(edit_account.list_view);
914         if (ac_prefs == NULL)
915                 return;
916
917         if (ac_prefs->protocol == A_IMAP4 || ac_prefs->protocol == A_NNTP) {
918                 alertpanel_error(_("Accounts with remote folders cannot be copied."));
919                 return;
920         }
921         account_list_dirty = TRUE;
922         
923         ac_clon = prefs_account_new();
924         /* copy fields */
925         ac_clon->account_name = g_strdup_printf(_("Copy of %s"),
926                                                 ac_prefs->account_name);
927         /* personal */
928         ACP_FDUP(name);
929         ACP_FDUP(address);
930         ACP_FDUP(organization);
931
932         /* server */
933         ACP_FASSIGN(protocol);
934         ACP_FDUP(recv_server);
935         ACP_FDUP(smtp_server);
936         ACP_FDUP(nntp_server);
937         ACP_FASSIGN(use_nntp_auth);
938         ACP_FASSIGN(use_nntp_auth_onconnect);
939         ACP_FDUP(userid);
940         ACP_FDUP(passwd);
941
942         ACP_FDUP(local_mbox);
943         ACP_FASSIGN(use_mail_command);
944         ACP_FDUP(mail_command);
945         
946         ACP_FASSIGN(ssl_pop);
947         ACP_FASSIGN(ssl_imap);
948         ACP_FASSIGN(ssl_nntp);
949         ACP_FASSIGN(ssl_smtp);
950         ACP_FASSIGN(use_nonblocking_ssl);
951         
952         /* receive */
953         ACP_FASSIGN(use_apop_auth);
954         ACP_FASSIGN(rmmail);
955         ACP_FASSIGN(msg_leave_time);
956         ACP_FASSIGN(msg_leave_hour);
957         ACP_FASSIGN(recv_at_getall);
958         ACP_FASSIGN(sd_rmmail_on_download);
959         ACP_FASSIGN(enable_size_limit);
960         ACP_FASSIGN(size_limit);
961         ACP_FASSIGN(filter_on_recv);
962         ACP_FASSIGN(filterhook_on_recv);
963         ACP_FDUP(inbox);
964         ACP_FDUP(local_inbox);
965         ACP_FASSIGN(max_articles);
966
967         ACP_FASSIGN(imap_auth_type);
968
969         /* send */
970         ACP_FASSIGN(gen_msgid);
971         ACP_FASSIGN(gen_xmailer);
972         ACP_FASSIGN(add_customhdr);
973         ACP_FASSIGN(use_smtp_auth);
974         ACP_FASSIGN(smtp_auth_type);
975         ACP_FDUP(smtp_userid);
976         ACP_FDUP(smtp_passwd);
977
978         ACP_FASSIGN(pop_before_smtp);
979         ACP_FASSIGN(pop_before_smtp_timeout);
980         ACP_FASSIGN(last_pop_login_time);
981
982         ac_clon->customhdr_list = NULL;
983         hdrs = ac_prefs->customhdr_list;
984         while (hdrs != NULL) {
985                 ch = (CustomHeader *)hdrs->data;
986
987                 cch = g_new0(CustomHeader, 1);
988                 cch->account_id = ac_clon->account_id;  
989                 cch->name = (ch->name != NULL) ? g_strdup(ch->name) : NULL;
990                 cch->value = (ch->value != NULL) ? g_strdup(ch->value) : NULL;
991                 
992                 ac_clon->customhdr_list = g_slist_append(ac_clon->customhdr_list, cch);
993                 
994                 hdrs = g_slist_next(hdrs);
995         }
996
997         /* compose */
998         ACP_FASSIGN(sig_type);
999         ACP_FDUP(sig_path);
1000         ACP_FASSIGN(auto_sig);
1001         ACP_FDUP(sig_sep);
1002         ACP_FASSIGN(set_autocc);
1003         ACP_FDUP(auto_cc);
1004         ACP_FASSIGN(set_autobcc);
1005         ACP_FDUP(auto_bcc);
1006         ACP_FASSIGN(set_autoreplyto);
1007         ACP_FDUP(auto_replyto);
1008         ACP_FASSIGN(enable_default_dictionary);
1009         ACP_FDUP(default_dictionary);
1010         ACP_FASSIGN(enable_default_alt_dictionary);
1011         ACP_FDUP(default_alt_dictionary);
1012         ACP_FASSIGN(compose_with_format);
1013         ACP_FDUP(compose_subject_format);
1014         ACP_FDUP(compose_body_format);
1015         ACP_FASSIGN(reply_with_format);
1016         ACP_FDUP(reply_quotemark);
1017         ACP_FDUP(reply_body_format);
1018         ACP_FASSIGN(forward_with_format);
1019         ACP_FDUP(forward_quotemark);
1020         ACP_FDUP(forward_body_format);
1021
1022         /* privacy */
1023         ACP_FDUP(default_privacy_system);
1024         ACP_FASSIGN(default_encrypt);
1025         ACP_FASSIGN(default_encrypt_reply);
1026         ACP_FASSIGN(default_sign);
1027         ACP_FASSIGN(default_sign_reply);
1028         ACP_FASSIGN(save_encrypted_as_clear_text);
1029         ACP_FASSIGN(encrypt_to_self);
1030         
1031         /* advanced */
1032         ACP_FASSIGN(set_smtpport);
1033         ACP_FASSIGN(smtpport);
1034         ACP_FASSIGN(set_popport);
1035         ACP_FASSIGN(popport);
1036         ACP_FASSIGN(set_imapport);
1037         ACP_FASSIGN(imapport);
1038         ACP_FASSIGN(set_nntpport);
1039         ACP_FASSIGN(nntpport);
1040         ACP_FASSIGN(set_domain);
1041         ACP_FDUP(domain);
1042         ACP_FASSIGN(mark_crosspost_read);
1043         ACP_FASSIGN(crosspost_col);
1044
1045 #ifndef G_OS_WIN32
1046         ACP_FASSIGN(set_tunnelcmd);
1047         ACP_FDUP(tunnelcmd);
1048 #endif
1049
1050         ACP_FDUP(imap_dir);
1051         ACP_FASSIGN(imap_subsonly);
1052         ACP_FASSIGN(low_bandwidth);
1053
1054         ACP_FASSIGN(set_sent_folder);
1055         ACP_FDUP(sent_folder);
1056         ACP_FASSIGN(set_queue_folder);
1057         ACP_FDUP(queue_folder);
1058         ACP_FASSIGN(set_draft_folder);
1059         ACP_FDUP(draft_folder);
1060         ACP_FASSIGN(set_trash_folder);
1061         ACP_FDUP(trash_folder);
1062         /* don't want two default accounts */
1063         ac_clon->is_default = FALSE;
1064         ACP_FASSIGN(folder);
1065
1066         ACP_FASSIGN(config_version);
1067
1068         ACP_FASSIGN(use_proxy);
1069         ACP_FASSIGN(use_default_proxy);
1070         ACP_FASSIGN(use_proxy_for_send);
1071         ACP_FASSIGN(proxy_info.proxy_type);
1072         ACP_FDUP(proxy_info.proxy_host);
1073         ACP_FASSIGN(proxy_info.proxy_port);
1074         ACP_FASSIGN(proxy_info.use_proxy_auth);
1075         ACP_FDUP(proxy_info.proxy_name);
1076         ACP_FDUP(proxy_info.proxy_pass);
1077
1078         account_list = g_list_append(account_list, ac_clon);
1079         account_list_view_set();
1080 }
1081 #undef ACP_FDUP
1082 #undef ACP_FASSIGN
1083
1084 static void account_empty_cache(PrefsAccount *ac_prefs)
1085 {
1086         gchar *cache_dir;
1087
1088         cache_dir = prefs_account_cache_dir(ac_prefs, FALSE);
1089         if (cache_dir == NULL)
1090                 return; /* no cache dir, nothing to do */
1091
1092         if (is_dir_exist(cache_dir) && remove_dir_recursive(cache_dir) < 0) {
1093                 g_warning("can't remove directory '%s'", cache_dir);
1094         } else {
1095                 gchar *server_dir =  prefs_account_cache_dir(ac_prefs, TRUE);
1096                 if (g_rmdir(server_dir) == 0)
1097                         debug_print("Removed empty cache server directory\n");
1098                 else
1099                         debug_print("Cache server directory not empty: not removed\n");
1100                 g_free(server_dir);
1101         }
1102         g_free(cache_dir);
1103 }
1104
1105 static void account_delete(GtkWidget *widget, gpointer data)
1106 {
1107         PrefsAccount *ac_prefs;
1108         gchar buf[BUFFSIZE];
1109         GList *list;
1110         Folder *folder;
1111         GSList *cur;
1112
1113         ac_prefs = account_list_view_get_selected_account(edit_account.list_view);
1114         if (ac_prefs == NULL)
1115                 return;
1116
1117         g_snprintf(buf, sizeof(buf),
1118                    _("Do you really want to delete the account '%s'?"),
1119                    ac_prefs->account_name ? ac_prefs->account_name :
1120                    _("(Untitled)"));
1121         if (alertpanel_full(_("Delete account"), buf,
1122                             GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, ALERTFOCUS_FIRST, FALSE,
1123                             NULL, ALERT_WARNING) != G_ALERTALTERNATE)
1124                 return;
1125         account_list_dirty = TRUE;
1126
1127         if (ac_prefs->folder) {
1128                 FolderItem *item;
1129
1130                 item = mainwindow_get_mainwindow()->summaryview->folder_item;
1131                 if (item && item->folder == FOLDER(ac_prefs->folder))
1132                         summary_clear_all(mainwindow_get_mainwindow()->summaryview);
1133                 folder_destroy(FOLDER(ac_prefs->folder));
1134                 folderview_set_all();
1135         }
1136         account_destroy(ac_prefs);
1137         account_list_view_set();
1138
1139         debug_print("Removing deleted account references for all the folders...\n");
1140         list = folder_get_list();
1141         for (; list != NULL; list = list->next) {
1142                 folder = FOLDER(list->data);
1143                 if (folder->node)  /* && folder->type == F_? */
1144                         g_node_traverse(folder->node, G_PRE_ORDER,
1145                                 G_TRAVERSE_ALL, -1,
1146                                 account_delete_references_func,
1147                                 GINT_TO_POINTER(ac_prefs->account_id));
1148         }
1149
1150         gchar *uid = g_strdup_printf("%d", ac_prefs->account_id);
1151         passwd_store_delete_block(PWS_ACCOUNT, uid);
1152         g_free(uid);
1153
1154         debug_print("Removing filter rules relative to this account...\n");
1155         for(cur = filtering_rules ; cur != NULL ;) {
1156                 FilteringProp * prop = (FilteringProp *) cur->data;
1157
1158                 if (prop && (prop->account_id == ac_prefs->account_id)) {
1159                         /* get next item before we kill the current one */
1160                         cur = g_slist_next(cur);
1161
1162                         /* unallocate filteringprop and unchain it from the list */
1163                         filteringprop_free(prop);
1164                         filtering_rules = g_slist_remove(filtering_rules, prop);
1165                 } else {
1166                         cur = g_slist_next(cur);
1167                 }
1168         }
1169
1170         debug_print("Removing cache directory of this account...\n");
1171         account_empty_cache(ac_prefs);
1172
1173         folder_write_list();
1174 }
1175
1176 static void account_up(GtkWidget *widget, gpointer data)
1177 {
1178         GtkTreePath *sel = account_list_view_get_selected_account_path
1179                                 (edit_account.list_view),
1180                     *up;
1181         GtkTreeIter isel, iup;
1182         GtkTreeModel *model = gtk_tree_view_get_model
1183                                 (GTK_TREE_VIEW(edit_account.list_view));
1184         
1185         if (!sel) 
1186                 return;
1187         account_list_dirty = TRUE;
1188
1189         up = gtk_tree_path_copy(sel);
1190         if (!up) {
1191                 gtk_tree_path_free(sel);
1192                 return;
1193         }
1194
1195         if (!gtk_tree_path_prev(up)) {
1196                 gtk_tree_path_free(up);
1197                 gtk_tree_path_free(sel);
1198                 return;
1199         }
1200
1201         if (!gtk_tree_model_get_iter(model, &isel, sel)
1202         ||  !gtk_tree_model_get_iter(model, &iup,  up)) {
1203                 gtk_tree_path_free(up);
1204                 gtk_tree_path_free(sel);
1205                 return;
1206         }
1207
1208         gtk_list_store_swap(GTK_LIST_STORE(model), &isel, &iup);
1209
1210         account_list_set();
1211         
1212         gtk_tree_path_free(up);
1213         gtk_tree_path_free(sel);
1214 }
1215
1216 static void account_down(GtkWidget *widget, gpointer data)
1217 {
1218         GtkTreePath *sel = account_list_view_get_selected_account_path
1219                                 (edit_account.list_view),
1220                     *dn;
1221         GtkTreeIter isel, idn;
1222         GtkTreeModel *model = gtk_tree_view_get_model
1223                                 (GTK_TREE_VIEW(edit_account.list_view));
1224         
1225         if (!sel) 
1226                 return;
1227         account_list_dirty = TRUE;
1228
1229         dn = gtk_tree_path_copy(sel);
1230         if (!dn) {
1231                 gtk_tree_path_free(sel);
1232                 return;
1233         }
1234
1235         /* XXX no check possible??? however, if down but at bottom, then 
1236          * nothing seems to happen much anyway, so the following seems to 
1237          * be okay */
1238         gtk_tree_path_next(dn);
1239
1240         if (!gtk_tree_model_get_iter(model, &isel, sel)
1241         ||  !gtk_tree_model_get_iter(model, &idn,  dn)) {
1242                 gtk_tree_path_free(dn);
1243                 gtk_tree_path_free(sel);
1244                 return;
1245         }
1246
1247         gtk_list_store_swap(GTK_LIST_STORE(model), &isel, &idn);
1248
1249         account_list_set();
1250         
1251         gtk_tree_path_free(dn);
1252         gtk_tree_path_free(sel);
1253 }
1254
1255 static void account_set_default(GtkWidget *widget, gpointer data)
1256 {
1257         PrefsAccount *ac_prefs;
1258
1259         if (NULL == (ac_prefs = account_list_view_get_selected_account
1260                                         (edit_account.list_view))) 
1261                 return; 
1262
1263         /* we need to change the store variables by resetting everything
1264          * and setting the new default one */
1265         account_list_view_set_default_by_id(edit_account.list_view,
1266                                             ac_prefs->account_id);               
1267         
1268         account_set_as_default(ac_prefs);
1269         account_list_view_set();
1270         
1271         cur_account = ac_prefs;
1272         account_flush_state();
1273 }
1274
1275 static void account_edit_close(GtkWidget *widget, gpointer data)
1276 {
1277         account_list_set();
1278         account_write_config_all();
1279
1280         if (!cur_account && account_list) {
1281                 PrefsAccount *ac_prefs = (PrefsAccount *)account_list->data;
1282                 account_set_as_default(ac_prefs);
1283                 cur_account = ac_prefs;
1284         }
1285
1286         if (account_list_dirty)
1287                 account_flush_state();
1288
1289         gtk_widget_hide(edit_account.window);
1290         gtk_window_set_modal(GTK_WINDOW(edit_account.window), FALSE);
1291         inc_unlock();
1292 }
1293
1294 static gint account_delete_event(GtkWidget *widget, GdkEventAny *event,
1295                                  gpointer data)
1296 {
1297         account_edit_close(NULL, NULL);
1298         return TRUE;
1299 }
1300
1301 static gboolean account_key_pressed(GtkWidget *widget, GdkEventKey *event,
1302                                     gpointer data)
1303 {
1304         if (event && event->keyval == GDK_KEY_Escape)
1305                 account_edit_close(NULL, NULL);
1306         return FALSE;
1307 }
1308
1309 static gboolean account_search_func_cb (GtkTreeModel *model, gint column, const gchar *key, 
1310                                                 GtkTreeIter *iter, gpointer search_data) 
1311 {
1312         gboolean retval;
1313         PrefsAccount *ac;
1314
1315         gtk_tree_model_get (model, iter, ACCOUNT_DATA, &ac, -1);
1316
1317         if (!ac->name || !key) return FALSE;
1318
1319         retval = (strncmp (key, ac->account_name, strlen(key)) != 0);
1320
1321         debug_print("selecting row\n");
1322         account_list_view_select_account(edit_account.list_view, ac->account_id);
1323
1324         return retval;
1325 }
1326
1327 static void account_list_view_set(void)
1328 {
1329         GList *cur;
1330         gint prev_sel_account;
1331         GtkListStore *store;
1332         
1333         store = GTK_LIST_STORE(gtk_tree_view_get_model
1334                 (GTK_TREE_VIEW(edit_account.list_view)));
1335
1336         prev_sel_account = account_list_view_get_selected_account_id
1337                 (edit_account.list_view); 
1338
1339         gtk_list_store_clear(store);
1340         
1341         for (cur = account_list; cur != NULL; cur = cur->next) {
1342                 account_list_store_insert_account_item(store, (PrefsAccount *)cur->data);
1343                 if ((PrefsAccount *)cur->data == cur_account)
1344                         account_list_view_select_account
1345                                 (edit_account.list_view, 
1346                                  cur_account->account_id);
1347         }
1348         
1349         if (prev_sel_account >= 0)
1350                 account_list_view_select_account(edit_account.list_view, 
1351                                                  prev_sel_account); 
1352 }
1353
1354 /* set account list from CList */
1355 static void account_list_set(void)
1356 {
1357         /* want to make sure we iterate *IN ORDER*, so therefore using
1358          * gtk_tree_model_XXXX_nth_child() */
1359         gint row, n_rows;
1360         PrefsAccount *ac_prefs;
1361         GtkTreeModel *model = gtk_tree_view_get_model
1362                                 (GTK_TREE_VIEW(edit_account.list_view));
1363         
1364         while (account_list)
1365                 account_list = g_list_remove(account_list, account_list->data);
1366
1367         n_rows = gtk_tree_model_iter_n_children(model, NULL);
1368
1369         for (row = 0; row < n_rows; row++) {
1370                 GtkTreeIter iter;
1371
1372                 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
1373                         g_warning("%s(%d) - no iter found???", __FILE__, __LINE__);
1374                         continue;
1375                 }
1376         
1377                 ac_prefs = NULL;
1378                 gtk_tree_model_get(model, &iter,
1379                                    ACCOUNT_DATA, &ac_prefs,
1380                                    -1);
1381                 if (ac_prefs)
1382                         account_list = g_list_append(account_list, ac_prefs);
1383         }
1384 }
1385
1386 /*!
1387  *\brief        finds the PrefsAccounts which should be used to answer a mail
1388  *
1389  *\param        msginfo The message to be answered
1390  *\param        reply_autosel Indicates whether reply account autoselection is on
1391  *
1392  *\return       PrefsAccount * the correct account, NULL if not found
1393  */
1394 PrefsAccount *account_get_reply_account(MsgInfo *msginfo, gboolean reply_autosel)
1395 {
1396         PrefsAccount *account = NULL;
1397         /* select the account set in folderitem's property (if enabled) */
1398         if (msginfo->folder->prefs && msginfo->folder->prefs->enable_default_account)
1399                 account = account_find_from_id(msginfo->folder->prefs->default_account);
1400         else if (folder_has_parent_of_type(msginfo->folder, F_QUEUE) ||
1401                  folder_has_parent_of_type(msginfo->folder, F_OUTBOX) ||
1402                  folder_has_parent_of_type(msginfo->folder, F_DRAFT)) {
1403                         gchar *from = NULL;
1404                         if (!procheader_get_header_from_msginfo
1405                                 (msginfo, &from, "From:")) {
1406                                 gchar *buf = from + strlen("From:");
1407                                 extract_address(buf);
1408                                 account = account_find_from_address(buf, FALSE);
1409                         g_free(from);
1410                         }
1411         }
1412         /* select account by to: and cc: header if enabled */
1413         if (reply_autosel) {
1414                 gchar * field = NULL;
1415                 int fieldno = 0;
1416                 for (field = msginfo->to; fieldno++ < 2; field = msginfo->cc) {
1417                         if (!account && field) {
1418                                 gchar *f = g_strdup(field);
1419                                 if (f) {
1420                                         gchar *cur = f;
1421                                         gchar *next = NULL;
1422                                         gchar *to = NULL;
1423                                         do {
1424                                                 next = strchr_with_skip_quote(cur, '"', ',');
1425                                                 if (next)
1426                                                         (*next) = 0;
1427                                                 Xstrdup_a(to, cur, return NULL);
1428                                                 extract_address(to);
1429                                                 account = account_find_from_address(to, FALSE);
1430                                                 if (next)
1431                                                         cur = next + 1;
1432                                                 else
1433                                                         break;
1434                                         } while (!account);
1435                                         g_free(f);
1436                                 }
1437                         }
1438                 }
1439                 if (!account) {
1440                         gchar *deliveredto = NULL;
1441                         if (!procheader_get_header_from_msginfo
1442                                 (msginfo, &deliveredto, "Delivered-To:")) {
1443                                 gchar *buf = deliveredto + strlen("Delivered-To:");
1444                                 extract_address(buf);
1445                                 account = account_find_from_address(buf, FALSE);
1446                         g_free(deliveredto);
1447                         }
1448                 }
1449         }
1450
1451         /* select the account for the whole folder (IMAP / NNTP) */
1452         if (!account) 
1453                 /* FIXME: this is not right, because folder may be nested. we should
1454                  * ascend the tree until we find a parent with proper account 
1455                  * information */
1456                 account = msginfo->folder->folder->account;
1457
1458         /* select current account */
1459         if (!account) account = cur_account;
1460         
1461         return account;
1462 }
1463
1464 /*!
1465  *\brief        Create data store
1466  */
1467 static GtkListStore* account_create_data_store(void)
1468 {
1469         return gtk_list_store_new(N_ACCOUNT_COLUMNS,
1470                                  G_TYPE_INT,            /* ACCOUNT_IS_DEFAULT */
1471                                  G_TYPE_BOOLEAN,        /* ACCOUNT_ENABLE_GET_ALL */
1472                                  G_TYPE_STRING,         /* ACCOUNT_NAME */
1473                                  G_TYPE_STRING,         /* ACCOUNT_PROTOCOL */
1474                                  G_TYPE_STRING,         /* ACCOUNT_SERVER */
1475                                  G_TYPE_POINTER,        /* ACCOUNT_DATA */
1476                                  -1);
1477 }
1478
1479 /*!
1480  *\brief        Insert an account item in the list. 
1481  *
1482  *\return       GtkTreeRowReference * A tree row reference, which is guaranteed to 
1483  *              stable whatever operations are performed on the list.
1484  */
1485 static void account_list_store_insert_account_item(GtkListStore *list_store,
1486                                                   PrefsAccount *ac_prefs)
1487 {
1488         GtkTreeIter iter;
1489         gboolean is_get_all = (ac_prefs->protocol == A_POP3 ||
1490                         ac_prefs->protocol == A_IMAP4 ||
1491                         ac_prefs->protocol == A_NNTP  ||
1492                         ac_prefs->protocol == A_LOCAL) &&
1493                 ac_prefs->recv_at_getall;
1494         gchar *protocol, *server;
1495         
1496 #ifdef USE_GNUTLS
1497         protocol = ac_prefs->protocol == A_POP3 ?
1498                   (ac_prefs->ssl_pop == SSL_TUNNEL ?
1499                    "POP (SSL/TLS)" :
1500                    ac_prefs->ssl_pop == SSL_STARTTLS ?
1501                    "POP (STARTTLS)" : "POP") :
1502                    ac_prefs->protocol == A_IMAP4 ?
1503                   (ac_prefs->ssl_imap == SSL_TUNNEL ?
1504                    "IMAP (SSL/TLS)" :
1505                    ac_prefs->ssl_imap == SSL_STARTTLS ?
1506                    "IMAP (STARTTLS)" : "IMAP") :
1507                    ac_prefs->protocol == A_NNTP ?
1508                   (ac_prefs->ssl_nntp == SSL_TUNNEL ?
1509                    "NNTP (SSL/TLS)" : "NNTP") :
1510                    ac_prefs->protocol == A_LOCAL ? "Local" :
1511                    ac_prefs->protocol == A_NONE ?  "SMTP" : "-";
1512 #else
1513         protocol = ac_prefs->protocol == A_POP3  ? "POP" :
1514                    ac_prefs->protocol == A_IMAP4 ? "IMAP" :
1515                    ac_prefs->protocol == A_LOCAL ? "Local" :
1516                    ac_prefs->protocol == A_NNTP  ? "NNTP" :
1517                    ac_prefs->protocol == A_NONE ?  "SMTP" : "-";
1518 #endif
1519
1520         server= ac_prefs->protocol == A_NNTP ? ac_prefs->nntp_server :
1521                    ac_prefs->protocol == A_LOCAL ?  "-" :
1522                    ac_prefs->protocol == A_NONE ? ac_prefs->smtp_server :
1523                    ac_prefs->recv_server;
1524
1525         gtk_list_store_append(list_store, &iter);
1526         gtk_list_store_set(list_store, &iter, 
1527                            ACCOUNT_IS_DEFAULT,     ac_prefs->is_default ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL,
1528                            ACCOUNT_ENABLE_GET_ALL, is_get_all,
1529                            ACCOUNT_NAME,           ac_prefs->account_name,
1530                            ACCOUNT_PROTOCOL,       protocol,
1531                            ACCOUNT_SERVER,         server,
1532                            ACCOUNT_DATA,           ac_prefs,
1533                            -1);
1534 }
1535
1536 /*!
1537  *\brief        Create and set up account list view, including tasks like
1538  *              creating the data store (\ref account_create_data_store()),
1539  *              and setting up the account list's individual columns (\ref 
1540  *              account_create_list_view_columns()).
1541  *
1542  *\return       GtkWidget * The created list view widget.
1543  */
1544 static GtkWidget *account_list_view_create(void)
1545 {
1546         GtkTreeView *list_view;
1547         GtkTreeSelection *selector;
1548         GtkListStore *store = account_create_data_store();
1549
1550         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)));
1551         g_object_unref(G_OBJECT(store));
1552 #ifdef GENERIC_UMPC
1553         g_object_set(list_view, "allow-checkbox-mode", FALSE, NULL);
1554 #endif
1555         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1556         gtk_tree_view_set_reorderable(list_view, TRUE);
1557         
1558         selector = gtk_tree_view_get_selection(list_view);
1559         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1560
1561         /* create the columns */
1562         account_create_list_view_columns(GTK_WIDGET(list_view));
1563
1564         /* set a double click listener */
1565         g_signal_connect(G_OBJECT(list_view), "row_activated",
1566                          G_CALLBACK(account_double_clicked),
1567                          list_view);
1568
1569         g_signal_connect(G_OBJECT(list_view), "drag_begin",                      
1570                          G_CALLBACK(drag_begin),
1571                          list_view);
1572                          
1573         g_signal_connect(G_OBJECT(list_view), "drag_end",                        
1574                          G_CALLBACK(drag_end),
1575                          list_view);
1576                          
1577         gtk_tree_view_set_reorderable(list_view, TRUE);
1578         return GTK_WIDGET(list_view);
1579 }
1580
1581 static void account_create_list_view_columns(GtkWidget *list_view)
1582 {
1583         GtkTreeViewColumn *column;
1584         GtkCellRenderer *renderer;
1585
1586         renderer = gtk_cell_renderer_toggle_new();
1587         g_object_set(renderer, 
1588                      "radio", FALSE, 
1589                      "activatable", TRUE,
1590                       NULL);
1591         column = gtk_tree_view_column_new_with_attributes
1592                 (C_("Accounts List Get Column Name", "G"), renderer,
1593                  "active", ACCOUNT_ENABLE_GET_ALL,
1594                  NULL);
1595         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1596         gtk_tree_view_column_set_alignment (column, 0.5);
1597         CLAWS_SET_TIP(gtk_tree_view_column_get_widget(column),
1598                         _("'Get Mail' retrieves mail from the checked accounts"));
1599         g_signal_connect(G_OBJECT(renderer), "toggled",                      
1600                          G_CALLBACK(account_get_all_toggled),
1601                          list_view);
1602
1603         renderer = gtk_cell_renderer_text_new();
1604         column = gtk_tree_view_column_new_with_attributes
1605                 (_("Name"), renderer,
1606                  "text", ACCOUNT_NAME,
1607                  "weight", ACCOUNT_IS_DEFAULT,
1608                  NULL);
1609         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1610         
1611         renderer = gtk_cell_renderer_text_new();
1612         column = gtk_tree_view_column_new_with_attributes
1613                 (_("Protocol"), renderer,
1614                  "text", ACCOUNT_PROTOCOL,
1615                  "weight", ACCOUNT_IS_DEFAULT,
1616                  NULL);
1617         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);           
1618         
1619         renderer = gtk_cell_renderer_text_new();
1620         column = gtk_tree_view_column_new_with_attributes
1621                 (_("Server"), renderer,
1622                  "text", ACCOUNT_SERVER,
1623                  "weight", ACCOUNT_IS_DEFAULT,
1624                  NULL);
1625         gtk_tree_view_set_search_column(GTK_TREE_VIEW(list_view), ACCOUNT_NAME);
1626         gtk_tree_view_set_search_equal_func(GTK_TREE_VIEW(list_view), account_search_func_cb , NULL, NULL);
1627         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);           
1628 }
1629
1630 /*!
1631  *\brief        Get currently selected account (by its unique ID)
1632  */
1633 static gint account_list_view_get_selected_account_id(GtkWidget *list_view)
1634 {
1635         PrefsAccount *res = NULL;
1636
1637         res = (PrefsAccount *)gtkut_tree_view_get_selected_pointer(
1638                         GTK_TREE_VIEW(list_view), ACCOUNT_DATA,
1639                         NULL, NULL, NULL);
1640
1641         return (res != NULL ? res->account_id : -1);
1642 }
1643
1644 /*!
1645  *\brief        Get the tree path of the currently selected account
1646  */
1647 static GtkTreePath *account_list_view_get_selected_account_path(GtkWidget *list_view)
1648 {
1649         GtkTreeSelection *selector;
1650         GtkTreeModel *model;
1651         GtkTreeIter iter;
1652
1653         selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
1654         
1655         if (!gtk_tree_selection_get_selected(selector, &model, &iter))
1656                 return NULL;
1657
1658         return gtk_tree_model_get_path(gtk_tree_view_get_model
1659                 (GTK_TREE_VIEW(list_view)), &iter);
1660 }
1661
1662 /*!
1663  *\brief        Get the account data of the currently selected account
1664  */
1665 static PrefsAccount *account_list_view_get_selected_account(GtkWidget *list_view)
1666 {
1667         PrefsAccount *res =
1668                 (PrefsAccount *)gtkut_tree_view_get_selected_pointer(
1669                         GTK_TREE_VIEW(list_view), ACCOUNT_DATA,
1670                         NULL, NULL, NULL);
1671
1672         return res;
1673 }
1674
1675 /*!
1676  *\brief        Select a row by the account it represents
1677  *
1678  *\return       gboolean TRUE if found and selected, FALSE if not.
1679  */
1680 static gboolean account_list_view_select_account(GtkWidget *list_view, gint account_id)
1681 {
1682         FindAccountInStore fis;
1683         GtkTreeModel *model;
1684         
1685         fis.account_id = account_id;
1686         fis.path = NULL;
1687
1688         model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1689
1690         gtk_tree_model_foreach(model, (GtkTreeModelForeachFunc) find_account_in_store,
1691                                &fis);
1692                                
1693         if (fis.path) {
1694                 GtkTreeSelection *selection;
1695                 GtkTreePath* path;
1696
1697                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
1698                 gtk_tree_selection_select_iter(selection, &fis.iter);
1699                 path = gtk_tree_model_get_path(model, &fis.iter);
1700                 /* XXX returned path may not be valid??? create new one to be sure */ 
1701                 gtk_tree_view_set_cursor(GTK_TREE_VIEW(list_view), path, NULL, FALSE);
1702                 gtk_tree_path_free(path);
1703         }
1704
1705         return fis.path != NULL;
1706 }
1707
1708 /*!
1709  *\brief        Set a new default account by its ID. (There is only one
1710  *              default account.)
1711  */
1712 static void account_list_view_set_default_by_id(GtkWidget *list_view,
1713                                                 gint account_id)
1714 {
1715         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1716         
1717         gtk_tree_model_foreach
1718                 (model, (GtkTreeModelForeachFunc) set_new_default_account,
1719                  &account_id);
1720 }
1721
1722 static gboolean set_new_default_account(GtkTreeModel *model,
1723                                         GtkTreePath  *path,
1724                                         GtkTreeIter  *iter,
1725                                         gint         *account_id)
1726 {
1727         PrefsAccount *account = NULL;
1728         PangoWeight weight;
1729         
1730         gtk_tree_model_get(model, iter, 
1731                            ACCOUNT_DATA, &account, 
1732                            -1);
1733
1734         if (*account_id == account->account_id)
1735                 weight = PANGO_WEIGHT_NORMAL;
1736         else
1737                 weight = PANGO_WEIGHT_BOLD;
1738
1739         gtk_list_store_set(GTK_LIST_STORE(model), iter, 
1740                            ACCOUNT_IS_DEFAULT, weight, -1);
1741
1742         return FALSE;
1743 }
1744                                         
1745 static gboolean find_account_in_store(GtkTreeModel *model,
1746                                       GtkTreePath  *path,
1747                                       GtkTreeIter  *iter,
1748                                       FindAccountInStore *data)
1749 {
1750         PrefsAccount *account = NULL;
1751         gtk_tree_model_get(model, iter, ACCOUNT_DATA, &account, -1);
1752
1753         if (data->account_id == account->account_id) {
1754                 data->path = path; /* signal we found it */
1755                 data->iter = *iter;
1756                 return TRUE;
1757         }
1758
1759         return FALSE; 
1760 }
1761
1762 /*!
1763  *\brief        Triggered when "get all" column is activated or de-activated
1764  */
1765 static void account_get_all_toggled(GtkCellRendererToggle *widget, 
1766                                     gchar *path, 
1767                                     GtkWidget *list_view)
1768 {
1769         GtkTreeIter iter;
1770         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1771         PrefsAccount *ac = NULL;
1772         gboolean get_all;
1773         
1774         if (!gtk_tree_model_get_iter_from_string(model, &iter, path))
1775                 return;
1776
1777         gtk_tree_model_get(model, &iter, 
1778                            ACCOUNT_DATA, &ac,
1779                            ACCOUNT_ENABLE_GET_ALL, &get_all,
1780                            -1);
1781
1782         /* check if the account has a selectable get all checkbox anyway... */
1783         if (!(ac->protocol == A_POP3  || 
1784               ac->protocol == A_IMAP4 ||
1785               ac->protocol == A_NNTP  ||
1786               ac->protocol == A_LOCAL))
1787                 return;       
1788
1789         /* set value in store */
1790         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
1791                            ACCOUNT_ENABLE_GET_ALL, !get_all,
1792                            -1);
1793
1794         /* set value in account */
1795         ac->recv_at_getall ^= TRUE;
1796 }
1797
1798 static void account_double_clicked(GtkTreeView          *list_view,
1799                                    GtkTreePath          *path,
1800                                    GtkTreeViewColumn    *column,
1801                                    gpointer              data)
1802 {
1803         account_edit_prefs(NULL, NULL); 
1804 }
1805
1806 static void drag_begin(GtkTreeView *list_view,
1807                       GdkDragContext *context,
1808                       gpointer data)
1809 {
1810         /* XXX unfortunately a completed drag & drop does not emit 
1811          * a "rows_reordered" signal, but a "row_changed" signal.
1812          * So during drag and drop, listen to "row_changed", and
1813          * update the account list accordingly */
1814
1815         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1816         g_signal_connect(G_OBJECT(model), "row_changed",
1817                          G_CALLBACK(account_row_changed_while_drag_drop),
1818                          list_view);
1819 }
1820
1821 static void drag_end(GtkTreeView *list_view,
1822                     GdkDragContext *context,
1823                     gpointer data)
1824 {
1825         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1826         g_signal_handlers_disconnect_by_func(G_OBJECT(model),
1827                                              G_CALLBACK(account_row_changed_while_drag_drop),
1828                                              list_view);
1829 }
1830
1831 static void account_row_changed_while_drag_drop(GtkTreeModel *model, 
1832                                    GtkTreePath  *path,
1833                                    GtkTreeIter  *iter,
1834                                    gpointer      arg3,
1835                                    GtkTreeView  *list_view)
1836 {       
1837         account_list_set();     
1838 }
1839
1840 gchar *account_get_signature_str(PrefsAccount *account)
1841 {
1842         gchar *sig_body = NULL;
1843         gchar *sig_str = NULL;
1844         gchar *utf8_sig_str = NULL;
1845
1846         cm_return_val_if_fail(account != NULL, NULL);
1847
1848         if (!account->sig_path)
1849                 return NULL;
1850
1851         if (account->sig_type == SIG_FILE) {
1852                 gchar *sig_full_path;
1853                 if (!g_path_is_absolute(account->sig_path)) {
1854                         sig_full_path = g_build_filename(get_home_dir(), account->sig_path, NULL);
1855                 } else {
1856                         sig_full_path = g_strdup(account->sig_path);
1857                 }
1858
1859                 if (!is_file_or_fifo_exist(sig_full_path)) {
1860                         g_warning("can't open signature file: '%s'", sig_full_path);
1861                         g_free(sig_full_path);
1862                         return NULL;
1863                 }
1864
1865                 debug_print("Reading signature from file '%s'\n", sig_full_path);
1866                 gchar *tmp = file_read_to_str(sig_full_path);
1867                 g_free(sig_full_path);
1868
1869                 if (!tmp)
1870                         return NULL;
1871
1872                 sig_body = normalize_newlines(tmp);
1873                 g_free(tmp);
1874         } else {
1875                 sig_body = get_command_output(account->sig_path);
1876         }
1877
1878         if (account->sig_sep) {
1879                 sig_str = g_strconcat("\n", account->sig_sep, "\n", sig_body,
1880                                       NULL);
1881                 g_free(sig_body);
1882         } else
1883                 sig_str = g_strconcat("\n", sig_body, NULL);
1884
1885         if (sig_str) {
1886                 if (g_utf8_validate(sig_str, -1, NULL) == TRUE)
1887                         utf8_sig_str = sig_str;
1888                 else {
1889                         utf8_sig_str = conv_codeset_strdup
1890                                 (sig_str, conv_get_locale_charset_str_no_utf8(),
1891                                  CS_INTERNAL);
1892                         g_free(sig_str);
1893                 }
1894         }
1895
1896         return utf8_sig_str;
1897 }
1898
1899 PrefsAccount *account_get_cur_account (void)
1900 {
1901         return cur_account;
1902 }
1903
1904 gboolean password_get(const gchar *user,
1905                     const gchar *server,
1906                     const gchar *protocol,
1907                     guint16 port,
1908                     gchar **password)
1909 {
1910         PasswordRequest req;
1911
1912         /* all have to be set */
1913         cm_return_val_if_fail(user != NULL, FALSE);
1914         cm_return_val_if_fail(server != NULL, FALSE);
1915         cm_return_val_if_fail(protocol != NULL, FALSE);
1916         cm_return_val_if_fail(port != 0, FALSE);
1917
1918         req.user = user;
1919         req.server = server;
1920         req.protocol = protocol;
1921         req.port = port;
1922
1923         if (hooks_invoke(PASSWORD_GET_HOOKLIST, &req)) {
1924                 *password = req.password;
1925                 return TRUE;
1926         }
1927         return FALSE;
1928 }
1929
1930 static GSList *account_signatures_list = NULL;
1931
1932 /* create a list of unique signatures from accounts list */
1933 void account_signatures_matchlist_create(void)
1934 {
1935         GList *cur_ac = NULL;
1936         PrefsAccount *ac_prefs = NULL;
1937
1938         if (account_signatures_list)
1939                 return;
1940
1941         account_signatures_list = g_slist_prepend(account_signatures_list, g_strdup("-- "));
1942         for (cur_ac = account_get_list();
1943                  cur_ac != NULL;
1944                  cur_ac = g_list_next(cur_ac)) {
1945                 ac_prefs = (PrefsAccount *)cur_ac->data;
1946
1947                 if (ac_prefs->sig_sep && *ac_prefs->sig_sep != '\0') {
1948                         if (!g_slist_find_custom(account_signatures_list, ac_prefs->sig_sep,
1949                                         (GCompareFunc)strcmp2)) {
1950                                 account_signatures_list = g_slist_prepend(account_signatures_list,
1951                                                 g_strdup(ac_prefs->sig_sep));
1952                         }
1953                 }
1954         }
1955 }
1956
1957 /* delete the list of signatures created by account_signatures_matchlist_create() */
1958 void account_signatures_matchlist_delete(void)
1959 {
1960         if (account_signatures_list) {
1961                 slist_free_strings_full(account_signatures_list);
1962                 account_signatures_list = NULL;
1963         }
1964 }
1965
1966 /* match a string against all signatures in list, using the specified format */
1967 gboolean account_signatures_matchlist_str_found(const gchar *str, const gchar *format)
1968 {
1969         gchar *tmp = NULL;
1970         gboolean found = FALSE;
1971         GSList *item;
1972
1973         for (item = account_signatures_list;
1974                  item != NULL && !found;
1975                  item = g_slist_next(item)) {
1976                 tmp = g_strdup_printf(format, (gchar *)item->data);
1977                 found = (strcmp(tmp, str) == 0);
1978                 g_free(tmp);
1979         }
1980         return found;
1981 }
1982
1983 /* match M first char of a string against all signatures in list, using the specified format */
1984 gboolean account_signatures_matchlist_nchar_found(const gchar *str, const gchar *format)
1985 {
1986         gchar *tmp = NULL;
1987         gboolean found = FALSE;
1988         GSList *item;
1989         gint len;
1990
1991         for (item = account_signatures_list;
1992                  item != NULL && !found;
1993                  item = g_slist_next(item)) {
1994                 tmp = g_strdup_printf(format, (gchar *)item->data);
1995                 len = strlen(tmp);
1996                 found = (strncmp(tmp, str, len) == 0);
1997                 g_free(tmp);
1998         }
1999         return found;
2000 }