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