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