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