2006-08-25 [mones] 2.4.0cvs88
[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         edit_btn = gtk_button_new_from_stock (GTK_STOCK_EDIT);
733         gtk_widget_show (edit_btn);
734         gtk_box_pack_start (GTK_BOX (vbox2), edit_btn, FALSE, FALSE, 4);
735         g_signal_connect (G_OBJECT(edit_btn), "clicked",
736                           G_CALLBACK (account_edit_prefs), NULL);
737
738         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
739         gtk_widget_show (del_btn);
740         gtk_box_pack_start (GTK_BOX (vbox2), del_btn, FALSE, FALSE, 4);
741         g_signal_connect (G_OBJECT(del_btn), "clicked",
742                           G_CALLBACK (account_delete), NULL);
743
744         clone_btn = gtk_button_new_from_stock(GTK_STOCK_COPY);
745         gtk_widget_show (clone_btn);
746         gtk_box_pack_start (GTK_BOX (vbox2), clone_btn, FALSE, FALSE, 4);
747         g_signal_connect(G_OBJECT(clone_btn), "clicked",
748                          G_CALLBACK(account_clone), NULL);
749         
750         down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
751         gtk_widget_show (down_btn);
752         gtk_box_pack_end (GTK_BOX (vbox2), down_btn, FALSE, FALSE, 4);
753         g_signal_connect (G_OBJECT(down_btn), "clicked",
754                           G_CALLBACK (account_down), NULL);
755
756         up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
757         gtk_widget_show (up_btn);
758         gtk_box_pack_end (GTK_BOX (vbox2), up_btn, FALSE, FALSE, 4);
759         g_signal_connect (G_OBJECT(up_btn), "clicked",
760                           G_CALLBACK (account_up), NULL);
761
762         hbox = gtk_hbox_new (FALSE, 8);
763         gtk_widget_show (hbox);
764         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
765
766         vbox2 = gtk_vbox_new(FALSE, 0);
767         gtk_widget_show (vbox2);
768         gtk_box_pack_start (GTK_BOX (hbox), vbox2, FALSE, FALSE, 0);
769
770         default_btn = gtk_button_new_with_mnemonic
771                 (_(" _Set as default account "));
772         gtk_widget_show (default_btn);
773         gtk_box_pack_start (GTK_BOX (vbox2), default_btn, TRUE, FALSE, 0);
774         g_signal_connect (G_OBJECT(default_btn), "clicked",
775                           G_CALLBACK (account_set_default), NULL);
776
777         hbbox = gtk_hbutton_box_new();
778         gtk_button_box_set_layout(GTK_BUTTON_BOX(hbbox), GTK_BUTTONBOX_END);
779         gtk_box_set_spacing(GTK_BOX(hbbox), 5);
780
781         gtkut_stock_button_set_create_with_help(&hbbox, &help_btn,
782                         &close_btn, GTK_STOCK_CLOSE,
783                         NULL, NULL, NULL, NULL);
784         gtk_widget_show(hbbox);
785
786         gtk_box_pack_end (GTK_BOX (hbox), hbbox, FALSE, FALSE, 0);
787         gtk_widget_grab_default (close_btn);
788
789         g_signal_connect (G_OBJECT (close_btn), "clicked",
790                           G_CALLBACK (account_edit_close),
791                           NULL);
792         g_signal_connect(G_OBJECT(help_btn), "clicked",
793                         G_CALLBACK(manual_open_with_anchor_cb),
794                         MANUAL_ANCHOR_ACCOUNTPREFS);
795
796         account_create_list_view_images(list_view);
797
798         g_signal_connect(G_OBJECT(window), "size_allocate",
799                          G_CALLBACK(account_size_allocate_cb), NULL);
800
801         if (!geometry.min_height) {
802                 geometry.min_width = 500;
803                 geometry.min_height = 350;
804         }
805
806         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
807                                       GDK_HINT_MIN_SIZE);
808         gtk_widget_set_size_request(window, prefs_common.accountswin_width,
809                                     prefs_common.accountswin_height);
810
811         edit_account.window    = window;
812         edit_account.list_view = list_view;
813         edit_account.close_btn = close_btn;
814 }
815
816 static void account_edit_prefs(GtkWidget *widget, gpointer data)
817 {
818         PrefsAccount *ac_prefs;
819
820         ac_prefs = account_list_view_get_selected_account(edit_account.list_view);
821         
822         if (ac_prefs) {
823                 account_open(ac_prefs);
824                 account_list_view_set();
825         }               
826 }
827
828 static gboolean account_delete_references_func(GNode *node, gpointer data)
829 {
830         FolderItem *item;
831         gint account;
832
833         g_return_val_if_fail(node->data != NULL, FALSE);
834
835         item = FOLDER_ITEM(node->data);
836         account = GPOINTER_TO_INT(data);
837
838         if(!item->prefs) /* && item->prefs->stype == F_NORMAL */
839                 return FALSE;
840         if(item->prefs->default_account != account)
841                 return FALSE;
842         
843         item->prefs->enable_default_account = FALSE;
844         item->prefs->default_account = 0;
845         folder_item_prefs_save_config(item);
846
847         return FALSE;
848 }
849
850         
851 #define ACP_FDUP(fld) ac_clon->fld = ((ac_prefs->fld) != NULL)?\
852                                      g_strdup(ac_prefs->fld): NULL
853 #define ACP_FASSIGN(fld) ac_clon->fld = ac_prefs->fld
854 static void account_clone(GtkWidget *widget, gpointer data)
855 {
856         PrefsAccount *ac_prefs, *ac_clon;
857         GSList *hdrs = NULL;
858         CustomHeader *cch = NULL, *ch = NULL;
859         
860         ac_prefs = account_list_view_get_selected_account(edit_account.list_view);
861         if (ac_prefs == NULL)
862                 return;
863
864         if (ac_prefs->protocol == A_IMAP4 || ac_prefs->protocol == A_NNTP) {
865                 alertpanel_error(_("Accounts with remote folders cannot be copied."));
866                 return;
867         }
868         
869         ac_clon = prefs_account_new();
870         /* copy fields */
871         ac_clon->account_name = g_strdup_printf(_("Copy of %s"),
872                                                 ac_prefs->account_name);
873         /* personal */
874         ACP_FDUP(name);
875         ACP_FDUP(address);
876         ACP_FDUP(organization);
877
878         /* server */
879         ACP_FASSIGN(protocol);
880         ACP_FDUP(recv_server);
881         ACP_FDUP(smtp_server);
882         ACP_FDUP(nntp_server);
883         ACP_FASSIGN(use_nntp_auth);
884         ACP_FASSIGN(use_nntp_auth_onconnect);
885         ACP_FDUP(userid);
886         ACP_FDUP(passwd);
887
888         ACP_FDUP(local_mbox);
889         ACP_FASSIGN(use_mail_command);
890         ACP_FDUP(mail_command);
891         
892 #if USE_OPENSSL
893         ACP_FASSIGN(ssl_pop);
894         ACP_FASSIGN(ssl_imap);
895         ACP_FASSIGN(ssl_nntp);
896         ACP_FASSIGN(ssl_smtp);
897         ACP_FASSIGN(use_nonblocking_ssl);
898 #endif /* USE_OPENSSL */
899         
900         ac_clon->tmp_pass = NULL;
901
902         /* receive */
903         ACP_FASSIGN(rmmail);
904         ACP_FASSIGN(msg_leave_time);
905         ACP_FASSIGN(getall);
906         ACP_FASSIGN(recv_at_getall);
907         ACP_FASSIGN(sd_rmmail_on_download);
908         ACP_FASSIGN(sd_filter_on_recv);
909         ACP_FASSIGN(enable_size_limit);
910         ACP_FASSIGN(size_limit);
911         ACP_FASSIGN(filter_on_recv);
912         ACP_FDUP(inbox);
913         ACP_FASSIGN(max_articles);
914
915         ACP_FASSIGN(imap_auth_type);
916
917         /* send */
918         ACP_FASSIGN(gen_msgid);
919         ACP_FASSIGN(add_customhdr);
920         ACP_FASSIGN(use_smtp_auth);
921         ACP_FASSIGN(smtp_auth_type);
922         ACP_FDUP(smtp_userid);
923         ACP_FDUP(smtp_passwd);
924
925         ac_clon->tmp_smtp_pass = NULL;
926
927         ACP_FASSIGN(pop_before_smtp);
928         ACP_FASSIGN(pop_before_smtp_timeout);
929         ACP_FASSIGN(last_pop_login_time);
930
931         ac_clon->customhdr_list = NULL;
932         hdrs = ac_prefs->customhdr_list;
933         while (hdrs != NULL) {
934                 ch = (CustomHeader *)hdrs->data;
935
936                 cch = g_new0(CustomHeader, 1);
937                 cch->account_id = ac_clon->account_id;  
938                 cch->name = (ch->name != NULL) ? g_strdup(ch->name) : NULL;
939                 cch->value = (ch->value != NULL) ? g_strdup(ch->value) : NULL;
940                 
941                 ac_clon->customhdr_list = g_slist_append(ac_clon->customhdr_list, cch);
942                 
943                 hdrs = g_slist_next(hdrs);
944         }
945
946         /* compose */
947         ACP_FASSIGN(sig_type);
948         ACP_FDUP(sig_path);
949         ACP_FASSIGN(auto_sig);
950         ACP_FDUP(sig_sep);
951         ACP_FASSIGN(set_autocc);
952         ACP_FDUP(auto_cc);
953         ACP_FASSIGN(set_autobcc);
954         ACP_FDUP(auto_bcc);
955         ACP_FASSIGN(set_autoreplyto);
956         ACP_FDUP(auto_replyto);
957
958         /* privacy */
959         ACP_FASSIGN(default_encrypt);
960         ACP_FASSIGN(default_sign);
961         
962         /* advanced */
963         ACP_FASSIGN(set_smtpport);
964         ACP_FASSIGN(smtpport);
965         ACP_FASSIGN(set_popport);
966         ACP_FASSIGN(popport);
967         ACP_FASSIGN(set_imapport);
968         ACP_FASSIGN(imapport);
969         ACP_FASSIGN(set_nntpport);
970         ACP_FASSIGN(nntpport);
971         ACP_FASSIGN(set_domain);
972         ACP_FDUP(domain);
973         ACP_FASSIGN(mark_crosspost_read);
974         ACP_FASSIGN(crosspost_col);
975
976         ACP_FASSIGN(set_tunnelcmd);
977         ACP_FDUP(tunnelcmd);
978
979         ACP_FDUP(imap_dir);
980
981         ACP_FASSIGN(set_sent_folder);
982         ACP_FDUP(sent_folder);
983         ACP_FASSIGN(set_draft_folder);
984         ACP_FDUP(draft_folder);
985         ACP_FASSIGN(set_trash_folder);
986         ACP_FDUP(trash_folder);
987         /* don't want two default accounts */
988         ac_clon->is_default = FALSE;
989         ACP_FASSIGN(folder);
990
991         account_list = g_list_append(account_list, ac_clon);
992         account_list_view_set();
993 }
994 #undef ACP_FDUP
995 #undef ACP_FASSIGN
996
997 static void account_delete(GtkWidget *widget, gpointer data)
998 {
999         PrefsAccount *ac_prefs;
1000         gchar buf[BUFFSIZE];
1001         GList *list;
1002         Folder *folder;
1003         GSList *cur;
1004  
1005         ac_prefs = account_list_view_get_selected_account(edit_account.list_view);
1006         if (ac_prefs == NULL)
1007                 return;
1008
1009         g_snprintf(buf, sizeof(buf),
1010                    _("Do you really want to delete the account '%s'?"),
1011                    ac_prefs->account_name ? ac_prefs->account_name :
1012                    _("(Untitled)"));
1013         if (alertpanel_full(_("Delete account"), buf,
1014                             GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, FALSE,
1015                             NULL, ALERT_WARNING, G_ALERTDEFAULT) != G_ALERTALTERNATE)
1016                 return;
1017
1018         if (ac_prefs->folder) {
1019                 FolderItem *item;
1020
1021                 item = mainwindow_get_mainwindow()->summaryview->folder_item;
1022                 if (item && item->folder == FOLDER(ac_prefs->folder))
1023                         summary_clear_all(mainwindow_get_mainwindow()->summaryview);
1024                 folder_destroy(FOLDER(ac_prefs->folder));
1025                 folderview_set_all();
1026         }
1027         account_destroy(ac_prefs);
1028         account_list_view_set();
1029
1030         debug_print("Removing deleted account references for all the folders...\n");
1031         list = folder_get_list();
1032         for (; list != NULL; list = list->next) {
1033                 folder = FOLDER(list->data);
1034                 if (folder->node)  /* && folder->type == F_? */
1035                         g_node_traverse(folder->node, G_PRE_ORDER,
1036                                 G_TRAVERSE_ALL, -1,
1037                                 account_delete_references_func,
1038                                 GINT_TO_POINTER(ac_prefs->account_id));
1039         }
1040
1041         debug_print("Removing filter rules relative to this account...\n");
1042         for(cur = filtering_rules ; cur != NULL ;) {
1043                 FilteringProp * prop = (FilteringProp *) cur->data;
1044
1045                 if (prop && (prop->account_id == ac_prefs->account_id)) {
1046                         /* get next item before we kill the current one */
1047                         cur = g_slist_next(cur);
1048
1049                         /* unallocate filteringprop and unchain it from the list */
1050                         filteringprop_free(prop);
1051                         filtering_rules = g_slist_remove(filtering_rules, prop);
1052                 } else {
1053                         cur = g_slist_next(cur);
1054                 }
1055         }
1056 }
1057
1058 static void account_up(GtkWidget *widget, gpointer data)
1059 {
1060         GtkTreePath *sel = account_list_view_get_selected_account_path
1061                                 (edit_account.list_view),
1062                     *up;
1063         GtkTreeIter isel, iup;
1064         GtkTreeModel *model = gtk_tree_view_get_model
1065                                 (GTK_TREE_VIEW(edit_account.list_view));
1066         
1067         if (!sel) 
1068                 return;
1069
1070         up = gtk_tree_path_copy(sel);
1071         if (!up) {
1072                 gtk_tree_path_free(sel);
1073                 return;
1074         }
1075
1076         if (!gtk_tree_path_prev(up)) {
1077                 gtk_tree_path_free(up);
1078                 gtk_tree_path_free(sel);
1079                 return;
1080         }
1081
1082         if (!gtk_tree_model_get_iter(model, &isel, sel)
1083         ||  !gtk_tree_model_get_iter(model, &iup,  up)) {
1084                 gtk_tree_path_free(up);
1085                 gtk_tree_path_free(sel);
1086                 return;
1087         }
1088
1089         gtk_list_store_swap(GTK_LIST_STORE(model), &isel, &iup);
1090
1091         account_list_set();
1092         
1093         gtk_tree_path_free(up);
1094         gtk_tree_path_free(sel);
1095 }
1096
1097 static void account_down(GtkWidget *widget, gpointer data)
1098 {
1099         GtkTreePath *sel = account_list_view_get_selected_account_path
1100                                 (edit_account.list_view),
1101                     *dn;
1102         GtkTreeIter isel, idn;
1103         GtkTreeModel *model = gtk_tree_view_get_model
1104                                 (GTK_TREE_VIEW(edit_account.list_view));
1105         
1106         if (!sel) 
1107                 return;
1108
1109         dn = gtk_tree_path_copy(sel);
1110         if (!dn) {
1111                 gtk_tree_path_free(sel);
1112                 return;
1113         }
1114
1115         /* XXX no check possible??? however, if down but at bottom, then 
1116          * nothing seems to happen much anyway, so the following seems to 
1117          * be okay */
1118         gtk_tree_path_next(dn);
1119
1120         if (!gtk_tree_model_get_iter(model, &isel, sel)
1121         ||  !gtk_tree_model_get_iter(model, &idn,  dn)) {
1122                 gtk_tree_path_free(dn);
1123                 gtk_tree_path_free(sel);
1124                 return;
1125         }
1126
1127         gtk_list_store_swap(GTK_LIST_STORE(model), &isel, &idn);
1128
1129         account_list_set();
1130         
1131         gtk_tree_path_free(dn);
1132         gtk_tree_path_free(sel);
1133 }
1134
1135 static void account_set_default(GtkWidget *widget, gpointer data)
1136 {
1137         PrefsAccount *ac_prefs;
1138
1139         if (NULL == (ac_prefs = account_list_view_get_selected_account
1140                                         (edit_account.list_view))) 
1141                 return; 
1142
1143         /* we need to change the store variables by resetting everything
1144          * and setting the new default one */
1145         account_list_view_set_default_by_id(edit_account.list_view,
1146                                             ac_prefs->account_id);               
1147         
1148         account_set_as_default(ac_prefs);
1149         account_list_view_set();
1150         
1151         cur_account = ac_prefs;
1152         account_set_menu();
1153         main_window_reflect_prefs_all();
1154 }
1155
1156 static void account_edit_close(GtkWidget *widget, gpointer data)
1157 {
1158         account_list_set();
1159         account_write_config_all();
1160
1161         if (!cur_account && account_list) {
1162                 PrefsAccount *ac_prefs = (PrefsAccount *)account_list->data;
1163                 account_set_as_default(ac_prefs);
1164                 cur_account = ac_prefs;
1165         }
1166
1167         account_set_menu();
1168         main_window_reflect_prefs_all();
1169
1170         gtk_widget_hide(edit_account.window);
1171
1172         inc_unlock();
1173 }
1174
1175 static gint account_delete_event(GtkWidget *widget, GdkEventAny *event,
1176                                  gpointer data)
1177 {
1178         account_edit_close(NULL, NULL);
1179         return TRUE;
1180 }
1181
1182 static gboolean account_key_pressed(GtkWidget *widget, GdkEventKey *event,
1183                                     gpointer data)
1184 {
1185         if (event && event->keyval == GDK_Escape)
1186                 account_edit_close(NULL, NULL);
1187         return FALSE;
1188 }
1189
1190 static void account_list_view_add(PrefsAccount *ac_prefs)
1191 {
1192         GtkTreeView *list_view = GTK_TREE_VIEW(edit_account.list_view);
1193         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model(list_view));
1194         gchar *name, *protocol, *server;
1195         gboolean has_getallbox;
1196         gboolean getall;
1197
1198         name = ac_prefs->account_name;
1199 #if USE_OPENSSL
1200         protocol = ac_prefs->protocol == A_POP3 ?
1201                   (ac_prefs->ssl_pop == SSL_TUNNEL ?
1202                    "POP3 (SSL)" :
1203                    ac_prefs->ssl_pop == SSL_STARTTLS ?
1204                    "POP3 (TLS)" : "POP3") :
1205                    ac_prefs->protocol == A_IMAP4 ?
1206                   (ac_prefs->ssl_imap == SSL_TUNNEL ?
1207                    "IMAP4 (SSL)" :
1208                    ac_prefs->ssl_imap == SSL_STARTTLS ?
1209                    "IMAP4 (TLS)" : "IMAP4") :
1210                    ac_prefs->protocol == A_NNTP ?
1211                   (ac_prefs->ssl_nntp == SSL_TUNNEL ?
1212                    "NNTP (SSL)" : "NNTP") :
1213                    "";
1214 #else
1215         protocol = ac_prefs->protocol == A_POP3  ? "POP3" :
1216                    ac_prefs->protocol == A_IMAP4 ? "IMAP4" :
1217                    ac_prefs->protocol == A_LOCAL ? "Local" :
1218                    ac_prefs->protocol == A_NNTP  ? "NNTP" : "";
1219 #endif
1220         server= ac_prefs->protocol == A_NNTP
1221                 ? ac_prefs->nntp_server : ac_prefs->recv_server;
1222
1223         has_getallbox = (ac_prefs->protocol == A_POP3  ||
1224                          ac_prefs->protocol == A_IMAP4 ||
1225                          ac_prefs->protocol == A_NNTP  ||
1226                          ac_prefs->protocol == A_LOCAL);
1227         getall = has_getallbox && ac_prefs->recv_at_getall;
1228
1229         account_list_view_insert_account_item(list_store,
1230                                              name, protocol, server,
1231                                              ac_prefs->is_default,
1232                                              getall, ac_prefs);
1233         return;
1234 }
1235
1236 static void account_list_view_set(void)
1237 {
1238         GList *cur;
1239         gint prev_sel_account;
1240         GtkListStore *store;
1241         
1242         store = GTK_LIST_STORE(gtk_tree_view_get_model
1243                 (GTK_TREE_VIEW(edit_account.list_view)));
1244
1245         prev_sel_account = account_list_view_get_selected_account_id
1246                 (edit_account.list_view); 
1247
1248         gtk_list_store_clear(store);
1249         
1250         for (cur = account_list; cur != NULL; cur = cur->next) {
1251                 account_list_view_add((PrefsAccount *)cur->data);
1252                 if ((PrefsAccount *)cur->data == cur_account)
1253                         account_list_view_select_account
1254                                 (edit_account.list_view, 
1255                                  cur_account->account_id);
1256         }
1257         
1258         if (prev_sel_account >= 0)
1259                 account_list_view_select_account(edit_account.list_view, 
1260                                                  prev_sel_account); 
1261 }
1262
1263 /* set account list from CList */
1264 static void account_list_set(void)
1265 {
1266         /* want to make sure we iterate *IN ORDER*, so therefore using
1267          * gtk_tree_model_XXXX_nth_child() */
1268         gint row, n_rows;
1269         PrefsAccount *ac_prefs;
1270         GtkTreeModel *model = gtk_tree_view_get_model
1271                                 (GTK_TREE_VIEW(edit_account.list_view));
1272         
1273         while (account_list)
1274                 account_list = g_list_remove(account_list, account_list->data);
1275
1276         n_rows = gtk_tree_model_iter_n_children(model, NULL);
1277
1278         for (row = 0; row < n_rows; row++) {
1279                 GtkTreeIter iter;
1280
1281                 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
1282                         g_warning("%s(%d) - no iter found???\n", __FILE__, __LINE__);                                         
1283                         continue;
1284                 }
1285         
1286                 ac_prefs = NULL;
1287                 gtk_tree_model_get(model, &iter,
1288                                    ACCOUNT_DATA, &ac_prefs,
1289                                    -1);
1290                 if (ac_prefs)
1291                         account_list = g_list_append(account_list, ac_prefs);
1292         }
1293 }
1294
1295 /*!
1296  *\brief        finds the PrefsAccounts which should be used to answer a mail
1297  *
1298  *\param        msginfo The message to be answered
1299  *\param        reply_autosel Indicates whether reply account autoselection is on
1300  *
1301  *\return       PrefsAccount * the correct account, NULL if not found
1302  */
1303 PrefsAccount *account_get_reply_account(MsgInfo *msginfo, gboolean reply_autosel)
1304 {
1305         PrefsAccount *account = NULL;
1306         /* select the account set in folderitem's property (if enabled) */
1307         if (msginfo->folder->prefs && msginfo->folder->prefs->enable_default_account)
1308                 account = account_find_from_id(msginfo->folder->prefs->default_account);
1309         
1310         /* select account by to: and cc: header if enabled */
1311         if (reply_autosel) {
1312                 gchar * field = NULL;
1313                 int fieldno = 0;
1314                 for (field = msginfo->to; fieldno++ < 2; field = msginfo->cc) {
1315                         if (!account && field) {
1316                                 gchar *to = NULL;
1317                                 if (!strchr(field, ',')) {
1318                                         Xstrdup_a(to, field, return NULL);
1319                                         extract_address(to);
1320                                         account = account_find_from_address(to);
1321                                 } else {
1322                                         gchar **split = g_strsplit(field, ",", -1);
1323                                         int i = -1;
1324                                         do {
1325                                                 i++;
1326                                                 if (!split[i])
1327                                                         break;
1328                                                 Xstrdup_a(to, split[i], return NULL);
1329                                                 extract_address(to);
1330                                                 account = account_find_from_address(to);
1331                                         } while (!account);
1332                                         g_strfreev(split);
1333                                 }
1334                         }
1335                 }
1336         }
1337
1338         /* select the account for the whole folder (IMAP / NNTP) */
1339         if (!account) 
1340                 /* FIXME: this is not right, because folder may be nested. we should
1341                  * ascend the tree until we find a parent with proper account 
1342                  * information */
1343                 account = msginfo->folder->folder->account;
1344
1345         /* select current account */
1346         if (!account) account = cur_account;
1347         
1348         return account;
1349 }
1350
1351 /*!
1352  *\brief        Create data store
1353  */
1354 static GtkListStore* account_create_data_store(void)
1355 {
1356         return gtk_list_store_new(N_ACCOUNT_COLUMNS,
1357                                  GDK_TYPE_PIXBUF,       /* ACCOUNT_IS_DEFAULT */
1358                                  G_TYPE_BOOLEAN,        /* ACCOUNT_ENABLE_GET_ALL */
1359                                  G_TYPE_STRING,         /* ACCOUNT_NAME */
1360                                  G_TYPE_STRING,         /* ACCOUNT_PROTOCOL */
1361                                  G_TYPE_STRING,         /* ACCOUNT_SERVER */
1362                                  G_TYPE_POINTER,        /* ACCOUNT_DATA */
1363                                  -1);
1364 }
1365
1366 /*!
1367  *\brief        Insert an account item in the list. 
1368  *
1369  *\return       GtkTreeRowReference * A tree row reference, which is guaranteed to 
1370  *              stable whatever operations are performed on the list.
1371  */
1372 static void account_list_view_insert_account_item(GtkListStore *list_store, 
1373                                                   const gchar *account_name,
1374                                                   const gchar *protocol, 
1375                                                   const gchar *server_name,
1376                                                   gboolean is_default, 
1377                                                   gboolean is_get_all,
1378                                                   PrefsAccount *account_data)
1379 {
1380         GtkTreeIter iter;
1381         
1382         gtk_list_store_append(list_store, &iter);
1383         gtk_list_store_set(list_store, &iter, 
1384                            ACCOUNT_IS_DEFAULT,     is_default ? mark_pixbuf : NULL,
1385                            ACCOUNT_ENABLE_GET_ALL, is_get_all,
1386                            ACCOUNT_NAME,           account_name,
1387                            ACCOUNT_PROTOCOL,       protocol,
1388                            ACCOUNT_SERVER,         server_name,
1389                            ACCOUNT_DATA,           account_data,
1390                            -1);
1391 }
1392
1393 /*!
1394  *\brief        Create and set up account list view, including tasks like
1395  *              creating the data store (\ref account_create_data_store()),
1396  *              creating images for the list view (\ref account_create_list_view_images()),
1397  *              and setting up the account list's individual columns (\ref 
1398  *              account_create_list_view_columns()).
1399  *
1400  *\return       GtkWidget * The created list view widget.
1401  */
1402 static GtkWidget *account_list_view_create(void)
1403 {
1404         GtkTreeView *list_view;
1405         GtkTreeSelection *selector;
1406         GtkListStore *store = account_create_data_store();
1407
1408         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)));
1409         g_object_unref(G_OBJECT(store));
1410
1411         gtk_tree_view_set_rules_hint(list_view, prefs_common.enable_rules_hint);
1412         gtk_tree_view_set_reorderable(list_view, TRUE);
1413         
1414         selector = gtk_tree_view_get_selection(list_view);
1415         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1416
1417         /* create the columns */
1418         account_create_list_view_columns(GTK_WIDGET(list_view));
1419
1420         /* set a double click listener */
1421         g_signal_connect(G_OBJECT(list_view), "row_activated",
1422                          G_CALLBACK(account_double_clicked),
1423                          list_view);
1424
1425         g_signal_connect(G_OBJECT(list_view), "drag_begin",                      
1426                          G_CALLBACK(drag_begin),
1427                          list_view);
1428                          
1429         g_signal_connect(G_OBJECT(list_view), "drag_end",                        
1430                          G_CALLBACK(drag_end),
1431                          list_view);
1432                          
1433         gtk_tree_view_set_reorderable(list_view, TRUE);
1434         return GTK_WIDGET(list_view);
1435 }
1436
1437 static void account_create_list_view_images(GtkWidget *list_view)
1438 {
1439         stock_pixbuf_gdk(list_view, STOCK_PIXMAP_MARK, &mark_pixbuf);
1440 }
1441
1442 static void account_create_list_view_columns(GtkWidget *list_view)
1443 {
1444         GtkTreeViewColumn *column;
1445         GtkCellRenderer *renderer;
1446
1447         renderer = gtk_cell_renderer_pixbuf_new();
1448         column = gtk_tree_view_column_new_with_attributes
1449                 ("D", renderer,
1450                  "pixbuf", ACCOUNT_IS_DEFAULT,
1451                  NULL);
1452         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);           
1453
1454         renderer = gtk_cell_renderer_toggle_new();
1455         g_object_set(renderer, 
1456                      "radio", FALSE, 
1457                      "activatable", TRUE,
1458                       NULL);
1459         column = gtk_tree_view_column_new_with_attributes
1460                 ("G", renderer,
1461                  "active", ACCOUNT_ENABLE_GET_ALL,
1462                  NULL);
1463         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1464         g_signal_connect(G_OBJECT(renderer), "toggled",                      
1465                          G_CALLBACK(account_get_all_toggled),
1466                          list_view);
1467         
1468         renderer = gtk_cell_renderer_text_new();
1469         column = gtk_tree_view_column_new_with_attributes
1470                 (_("Name"), renderer,
1471                  "text", ACCOUNT_NAME,
1472                  NULL);
1473         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1474         
1475         renderer = gtk_cell_renderer_text_new();
1476         column = gtk_tree_view_column_new_with_attributes
1477                 (_("Protocol"), renderer,
1478                  "text", ACCOUNT_PROTOCOL,
1479                  NULL);
1480         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);           
1481         
1482         renderer = gtk_cell_renderer_text_new();
1483         column = gtk_tree_view_column_new_with_attributes
1484                 (_("Server"), renderer,
1485                  "text", ACCOUNT_SERVER,
1486                  NULL);
1487         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);           
1488 }
1489
1490 /*!
1491  *\brief        Get currently selected account (by its unique ID)
1492  */
1493 static gint account_list_view_get_selected_account_id(GtkWidget *list_view)
1494 {
1495         GtkTreeSelection *selector;
1496         GtkTreeModel *model;
1497         GtkTreeIter iter;
1498         PrefsAccount *res = NULL;
1499
1500         selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
1501         
1502         if (!gtk_tree_selection_get_selected(selector, &model, &iter))
1503                 return -1;
1504
1505         gtk_tree_model_get(model, &iter, ACCOUNT_DATA, &res, -1);
1506
1507         return res->account_id;                    
1508 }
1509
1510 /*!
1511  *\brief        Get the tree path of the currently selected account
1512  */
1513 GtkTreePath *account_list_view_get_selected_account_path(GtkWidget *list_view)
1514 {
1515         GtkTreeSelection *selector;
1516         GtkTreeModel *model;
1517         GtkTreeIter iter;
1518
1519         selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
1520         
1521         if (!gtk_tree_selection_get_selected(selector, &model, &iter))
1522                 return NULL;
1523
1524         return gtk_tree_model_get_path(gtk_tree_view_get_model
1525                 (GTK_TREE_VIEW(list_view)), &iter);
1526 }
1527
1528 /*!
1529  *\brief        Get the account data of the currently selected account
1530  */
1531 PrefsAccount *account_list_view_get_selected_account(GtkWidget *list_view)
1532 {
1533         GtkTreeSelection *selector;
1534         GtkTreeModel *model;
1535         GtkTreeIter iter;
1536         PrefsAccount *res = NULL;
1537
1538         selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
1539         
1540         if (!gtk_tree_selection_get_selected(selector, &model, &iter))
1541                 return NULL;
1542
1543         gtk_tree_model_get(model, &iter, ACCOUNT_DATA, &res, -1);
1544
1545         return res;                        
1546 }
1547
1548 /*!
1549  *\brief        Select a row by the account it represents
1550  *
1551  *\return       gboolean TRUE if found and selected, FALSE if not.
1552  */
1553 gboolean account_list_view_select_account(GtkWidget *list_view, gint account_id)
1554 {
1555         FindAccountInStore fis;
1556         GtkTreeModel *model;
1557         
1558         fis.account_id = account_id;
1559         fis.path = NULL;
1560
1561         model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1562
1563         gtk_tree_model_foreach(model, (GtkTreeModelForeachFunc) find_account_in_store,
1564                                &fis);
1565                                
1566         if (fis.path) {
1567                 GtkTreeSelection *selection;
1568                 GtkTreePath* path;
1569
1570                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
1571                 gtk_tree_selection_select_iter(selection, &fis.iter);
1572                 path = gtk_tree_model_get_path(model, &fis.iter);
1573                 /* XXX returned path may not be valid??? create new one to be sure */ 
1574                 gtk_tree_view_set_cursor(GTK_TREE_VIEW(list_view), path, NULL, FALSE);
1575                 gtk_tree_path_free(path);
1576         }
1577
1578         return fis.path != NULL;
1579 }
1580
1581 /*!
1582  *\brief        Set a new default account by its ID. (There is only one
1583  *              default account.)
1584  */
1585 static void account_list_view_set_default_by_id(GtkWidget *list_view,
1586                                                 gint account_id)
1587 {
1588         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1589         
1590         gtk_tree_model_foreach
1591                 (model, (GtkTreeModelForeachFunc) set_new_default_account,
1592                  &account_id);
1593 }
1594
1595 static gboolean set_new_default_account(GtkTreeModel *model,
1596                                         GtkTreePath  *path,
1597                                         GtkTreeIter  *iter,
1598                                         gint         *account_id)
1599 {
1600         PrefsAccount *account = NULL;
1601         GdkPixbuf *pixbuf;
1602         
1603         gtk_tree_model_get(model, iter, 
1604                            ACCOUNT_DATA, &account, 
1605                            -1);
1606
1607         if (*account_id == account->account_id)
1608                 pixbuf = NULL;
1609         else
1610                 pixbuf = mark_pixbuf;
1611         
1612         gtk_list_store_set(GTK_LIST_STORE(model), iter, 
1613                            ACCOUNT_IS_DEFAULT, pixbuf,
1614                            -1);
1615
1616         return FALSE;
1617 }
1618                                         
1619 static gboolean find_account_in_store(GtkTreeModel *model,
1620                                       GtkTreePath  *path,
1621                                       GtkTreeIter  *iter,
1622                                       FindAccountInStore *data)
1623 {
1624         PrefsAccount *account = NULL;
1625         gtk_tree_model_get(model, iter, ACCOUNT_DATA, &account, -1);
1626
1627         if (data->account_id == account->account_id) {
1628                 data->path = path; /* signal we found it */
1629                 data->iter = *iter;
1630                 return TRUE;
1631         }
1632
1633         return FALSE; 
1634 }
1635
1636 /*!
1637  *\brief        Triggered when "get all" column is activated or de-activated
1638  */
1639 static void account_get_all_toggled(GtkCellRendererToggle *widget, 
1640                                     gchar *path, 
1641                                     GtkWidget *list_view)
1642 {
1643         GtkTreeIter iter;
1644         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1645         PrefsAccount *ac = NULL;
1646         gboolean get_all;
1647         
1648         if (!gtk_tree_model_get_iter_from_string(model, &iter, path))
1649                 return;
1650
1651         gtk_tree_model_get(model, &iter, 
1652                            ACCOUNT_DATA, &ac,
1653                            ACCOUNT_ENABLE_GET_ALL, &get_all,
1654                            -1);
1655
1656         /* check if the account has a selectable get all checkbox anyway... */
1657         if (!(ac->protocol == A_POP3  || 
1658               ac->protocol == A_IMAP4 ||
1659               ac->protocol == A_NNTP  ||
1660               ac->protocol == A_LOCAL))
1661                 return;       
1662
1663         /* set value in store */
1664         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
1665                            ACCOUNT_ENABLE_GET_ALL, !get_all,
1666                            -1);
1667
1668         /* set value in account */
1669         ac->recv_at_getall ^= TRUE;
1670 }
1671
1672 static void account_double_clicked(GtkTreeView          *list_view,
1673                                    GtkTreePath          *path,
1674                                    GtkTreeViewColumn    *column,
1675                                    gpointer              data)
1676 {
1677         account_edit_prefs(NULL, NULL); 
1678 }
1679
1680 static void drag_begin(GtkTreeView *list_view,
1681                       GdkDragContext *context,
1682                       gpointer data)
1683 {
1684         /* XXX unfortunately a completed drag & drop does not emit 
1685          * a "rows_reordered" signal, but a "row_changed" signal.
1686          * So during drag and drop, listen to "row_changed", and
1687          * update the account list accordingly */
1688
1689         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1690         g_signal_connect(G_OBJECT(model), "row_changed",
1691                          G_CALLBACK(account_row_changed_while_drag_drop),
1692                          list_view);
1693 }
1694
1695 static void drag_end(GtkTreeView *list_view,
1696                     GdkDragContext *context,
1697                     gpointer data)
1698 {
1699         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1700         g_signal_handlers_disconnect_by_func(G_OBJECT(model),
1701                                              G_CALLBACK(account_row_changed_while_drag_drop),
1702                                              list_view);
1703 }
1704
1705 static void account_row_changed_while_drag_drop(GtkTreeModel *model, 
1706                                    GtkTreePath  *path,
1707                                    GtkTreeIter  *iter,
1708                                    gpointer      arg3,
1709                                    GtkTreeView  *list_view)
1710 {       
1711         account_list_set();     
1712 }