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