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