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