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