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