2007-01-03 [paul] 2.6.1cvs96
[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
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                 if (!account) {
1339                         gchar deliveredto[BUFFSIZE];
1340                         if (!procheader_get_header_from_msginfo
1341                                 (msginfo, deliveredto,sizeof deliveredto , "Delivered-To:")) { 
1342                                 gchar *buf = deliveredto + strlen("Delivered-To:");
1343                                 extract_address(buf);
1344                                 account = account_find_from_address(buf);
1345                         }
1346                 }
1347         }
1348
1349         /* select the account for the whole folder (IMAP / NNTP) */
1350         if (!account) 
1351                 /* FIXME: this is not right, because folder may be nested. we should
1352                  * ascend the tree until we find a parent with proper account 
1353                  * information */
1354                 account = msginfo->folder->folder->account;
1355
1356         /* select current account */
1357         if (!account) account = cur_account;
1358         
1359         return account;
1360 }
1361
1362 /*!
1363  *\brief        Create data store
1364  */
1365 static GtkListStore* account_create_data_store(void)
1366 {
1367         return gtk_list_store_new(N_ACCOUNT_COLUMNS,
1368                                  GDK_TYPE_PIXBUF,       /* ACCOUNT_IS_DEFAULT */
1369                                  G_TYPE_BOOLEAN,        /* ACCOUNT_ENABLE_GET_ALL */
1370                                  G_TYPE_STRING,         /* ACCOUNT_NAME */
1371                                  G_TYPE_STRING,         /* ACCOUNT_PROTOCOL */
1372                                  G_TYPE_STRING,         /* ACCOUNT_SERVER */
1373                                  G_TYPE_POINTER,        /* ACCOUNT_DATA */
1374                                  -1);
1375 }
1376
1377 /*!
1378  *\brief        Insert an account item in the list. 
1379  *
1380  *\return       GtkTreeRowReference * A tree row reference, which is guaranteed to 
1381  *              stable whatever operations are performed on the list.
1382  */
1383 static void account_list_view_insert_account_item(GtkListStore *list_store, 
1384                                                   const gchar *account_name,
1385                                                   const gchar *protocol, 
1386                                                   const gchar *server_name,
1387                                                   gboolean is_default, 
1388                                                   gboolean is_get_all,
1389                                                   PrefsAccount *account_data)
1390 {
1391         GtkTreeIter iter;
1392         
1393         gtk_list_store_append(list_store, &iter);
1394         gtk_list_store_set(list_store, &iter, 
1395                            ACCOUNT_IS_DEFAULT,     is_default ? mark_pixbuf : NULL,
1396                            ACCOUNT_ENABLE_GET_ALL, is_get_all,
1397                            ACCOUNT_NAME,           account_name,
1398                            ACCOUNT_PROTOCOL,       protocol,
1399                            ACCOUNT_SERVER,         server_name,
1400                            ACCOUNT_DATA,           account_data,
1401                            -1);
1402 }
1403
1404 /*!
1405  *\brief        Create and set up account list view, including tasks like
1406  *              creating the data store (\ref account_create_data_store()),
1407  *              creating images for the list view (\ref account_create_list_view_images()),
1408  *              and setting up the account list's individual columns (\ref 
1409  *              account_create_list_view_columns()).
1410  *
1411  *\return       GtkWidget * The created list view widget.
1412  */
1413 static GtkWidget *account_list_view_create(void)
1414 {
1415         GtkTreeView *list_view;
1416         GtkTreeSelection *selector;
1417         GtkListStore *store = account_create_data_store();
1418
1419         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)));
1420         g_object_unref(G_OBJECT(store));
1421
1422         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1423         gtk_tree_view_set_reorderable(list_view, TRUE);
1424         
1425         selector = gtk_tree_view_get_selection(list_view);
1426         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1427
1428         /* create the columns */
1429         account_create_list_view_columns(GTK_WIDGET(list_view));
1430
1431         /* set a double click listener */
1432         g_signal_connect(G_OBJECT(list_view), "row_activated",
1433                          G_CALLBACK(account_double_clicked),
1434                          list_view);
1435
1436         g_signal_connect(G_OBJECT(list_view), "drag_begin",                      
1437                          G_CALLBACK(drag_begin),
1438                          list_view);
1439                          
1440         g_signal_connect(G_OBJECT(list_view), "drag_end",                        
1441                          G_CALLBACK(drag_end),
1442                          list_view);
1443                          
1444         gtk_tree_view_set_reorderable(list_view, TRUE);
1445         return GTK_WIDGET(list_view);
1446 }
1447
1448 static void account_create_list_view_images(GtkWidget *list_view)
1449 {
1450         stock_pixbuf_gdk(list_view, STOCK_PIXMAP_MARK, &mark_pixbuf);
1451 }
1452
1453 static void account_create_list_view_columns(GtkWidget *list_view)
1454 {
1455         GtkTreeViewColumn *column;
1456         GtkCellRenderer *renderer;
1457         GtkTooltips *tooltips = gtk_tooltips_new();
1458
1459         renderer = gtk_cell_renderer_pixbuf_new();
1460         column = gtk_tree_view_column_new_with_attributes
1461                 (Q_("Accounts List Default Column Name|D"), renderer,
1462                  "pixbuf", ACCOUNT_IS_DEFAULT,
1463                  NULL);
1464         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);           
1465         gtk_tree_view_column_set_alignment (column, 0.5);
1466         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), column->button,
1467                         _("Default account"), NULL);
1468
1469         renderer = gtk_cell_renderer_toggle_new();
1470         g_object_set(renderer, 
1471                      "radio", FALSE, 
1472                      "activatable", TRUE,
1473                       NULL);
1474         column = gtk_tree_view_column_new_with_attributes
1475                 (Q_("Accounts List Get Column Name|G"), renderer,
1476                  "active", ACCOUNT_ENABLE_GET_ALL,
1477                  NULL);
1478         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1479         gtk_tree_view_column_set_alignment (column, 0.5);
1480         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), column->button,
1481                         _("'Get Mail' retrieves mail from the checked accounts"), NULL);
1482         g_signal_connect(G_OBJECT(renderer), "toggled",                      
1483                          G_CALLBACK(account_get_all_toggled),
1484                          list_view);
1485
1486         renderer = gtk_cell_renderer_text_new();
1487         column = gtk_tree_view_column_new_with_attributes
1488                 (_("Name"), renderer,
1489                  "text", ACCOUNT_NAME,
1490                  NULL);
1491         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1492         
1493         renderer = gtk_cell_renderer_text_new();
1494         column = gtk_tree_view_column_new_with_attributes
1495                 (_("Protocol"), renderer,
1496                  "text", ACCOUNT_PROTOCOL,
1497                  NULL);
1498         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);           
1499         
1500         renderer = gtk_cell_renderer_text_new();
1501         column = gtk_tree_view_column_new_with_attributes
1502                 (_("Server"), renderer,
1503                  "text", ACCOUNT_SERVER,
1504                  NULL);
1505         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);           
1506 }
1507
1508 /*!
1509  *\brief        Get currently selected account (by its unique ID)
1510  */
1511 static gint account_list_view_get_selected_account_id(GtkWidget *list_view)
1512 {
1513         GtkTreeSelection *selector;
1514         GtkTreeModel *model;
1515         GtkTreeIter iter;
1516         PrefsAccount *res = NULL;
1517
1518         selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
1519         
1520         if (!gtk_tree_selection_get_selected(selector, &model, &iter))
1521                 return -1;
1522
1523         gtk_tree_model_get(model, &iter, ACCOUNT_DATA, &res, -1);
1524
1525         return res->account_id;                    
1526 }
1527
1528 /*!
1529  *\brief        Get the tree path of the currently selected account
1530  */
1531 GtkTreePath *account_list_view_get_selected_account_path(GtkWidget *list_view)
1532 {
1533         GtkTreeSelection *selector;
1534         GtkTreeModel *model;
1535         GtkTreeIter iter;
1536
1537         selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
1538         
1539         if (!gtk_tree_selection_get_selected(selector, &model, &iter))
1540                 return NULL;
1541
1542         return gtk_tree_model_get_path(gtk_tree_view_get_model
1543                 (GTK_TREE_VIEW(list_view)), &iter);
1544 }
1545
1546 /*!
1547  *\brief        Get the account data of the currently selected account
1548  */
1549 PrefsAccount *account_list_view_get_selected_account(GtkWidget *list_view)
1550 {
1551         GtkTreeSelection *selector;
1552         GtkTreeModel *model;
1553         GtkTreeIter iter;
1554         PrefsAccount *res = NULL;
1555
1556         selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
1557         
1558         if (!gtk_tree_selection_get_selected(selector, &model, &iter))
1559                 return NULL;
1560
1561         gtk_tree_model_get(model, &iter, ACCOUNT_DATA, &res, -1);
1562
1563         return res;                        
1564 }
1565
1566 /*!
1567  *\brief        Select a row by the account it represents
1568  *
1569  *\return       gboolean TRUE if found and selected, FALSE if not.
1570  */
1571 gboolean account_list_view_select_account(GtkWidget *list_view, gint account_id)
1572 {
1573         FindAccountInStore fis;
1574         GtkTreeModel *model;
1575         
1576         fis.account_id = account_id;
1577         fis.path = NULL;
1578
1579         model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1580
1581         gtk_tree_model_foreach(model, (GtkTreeModelForeachFunc) find_account_in_store,
1582                                &fis);
1583                                
1584         if (fis.path) {
1585                 GtkTreeSelection *selection;
1586                 GtkTreePath* path;
1587
1588                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
1589                 gtk_tree_selection_select_iter(selection, &fis.iter);
1590                 path = gtk_tree_model_get_path(model, &fis.iter);
1591                 /* XXX returned path may not be valid??? create new one to be sure */ 
1592                 gtk_tree_view_set_cursor(GTK_TREE_VIEW(list_view), path, NULL, FALSE);
1593                 gtk_tree_path_free(path);
1594         }
1595
1596         return fis.path != NULL;
1597 }
1598
1599 /*!
1600  *\brief        Set a new default account by its ID. (There is only one
1601  *              default account.)
1602  */
1603 static void account_list_view_set_default_by_id(GtkWidget *list_view,
1604                                                 gint account_id)
1605 {
1606         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1607         
1608         gtk_tree_model_foreach
1609                 (model, (GtkTreeModelForeachFunc) set_new_default_account,
1610                  &account_id);
1611 }
1612
1613 static gboolean set_new_default_account(GtkTreeModel *model,
1614                                         GtkTreePath  *path,
1615                                         GtkTreeIter  *iter,
1616                                         gint         *account_id)
1617 {
1618         PrefsAccount *account = NULL;
1619         GdkPixbuf *pixbuf;
1620         
1621         gtk_tree_model_get(model, iter, 
1622                            ACCOUNT_DATA, &account, 
1623                            -1);
1624
1625         if (*account_id == account->account_id)
1626                 pixbuf = NULL;
1627         else
1628                 pixbuf = mark_pixbuf;
1629         
1630         gtk_list_store_set(GTK_LIST_STORE(model), iter, 
1631                            ACCOUNT_IS_DEFAULT, pixbuf,
1632                            -1);
1633
1634         return FALSE;
1635 }
1636                                         
1637 static gboolean find_account_in_store(GtkTreeModel *model,
1638                                       GtkTreePath  *path,
1639                                       GtkTreeIter  *iter,
1640                                       FindAccountInStore *data)
1641 {
1642         PrefsAccount *account = NULL;
1643         gtk_tree_model_get(model, iter, ACCOUNT_DATA, &account, -1);
1644
1645         if (data->account_id == account->account_id) {
1646                 data->path = path; /* signal we found it */
1647                 data->iter = *iter;
1648                 return TRUE;
1649         }
1650
1651         return FALSE; 
1652 }
1653
1654 /*!
1655  *\brief        Triggered when "get all" column is activated or de-activated
1656  */
1657 static void account_get_all_toggled(GtkCellRendererToggle *widget, 
1658                                     gchar *path, 
1659                                     GtkWidget *list_view)
1660 {
1661         GtkTreeIter iter;
1662         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1663         PrefsAccount *ac = NULL;
1664         gboolean get_all;
1665         
1666         if (!gtk_tree_model_get_iter_from_string(model, &iter, path))
1667                 return;
1668
1669         gtk_tree_model_get(model, &iter, 
1670                            ACCOUNT_DATA, &ac,
1671                            ACCOUNT_ENABLE_GET_ALL, &get_all,
1672                            -1);
1673
1674         /* check if the account has a selectable get all checkbox anyway... */
1675         if (!(ac->protocol == A_POP3  || 
1676               ac->protocol == A_IMAP4 ||
1677               ac->protocol == A_NNTP  ||
1678               ac->protocol == A_LOCAL))
1679                 return;       
1680
1681         /* set value in store */
1682         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
1683                            ACCOUNT_ENABLE_GET_ALL, !get_all,
1684                            -1);
1685
1686         /* set value in account */
1687         ac->recv_at_getall ^= TRUE;
1688 }
1689
1690 static void account_double_clicked(GtkTreeView          *list_view,
1691                                    GtkTreePath          *path,
1692                                    GtkTreeViewColumn    *column,
1693                                    gpointer              data)
1694 {
1695         account_edit_prefs(NULL, NULL); 
1696 }
1697
1698 static void drag_begin(GtkTreeView *list_view,
1699                       GdkDragContext *context,
1700                       gpointer data)
1701 {
1702         /* XXX unfortunately a completed drag & drop does not emit 
1703          * a "rows_reordered" signal, but a "row_changed" signal.
1704          * So during drag and drop, listen to "row_changed", and
1705          * update the account list accordingly */
1706
1707         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1708         g_signal_connect(G_OBJECT(model), "row_changed",
1709                          G_CALLBACK(account_row_changed_while_drag_drop),
1710                          list_view);
1711 }
1712
1713 static void drag_end(GtkTreeView *list_view,
1714                     GdkDragContext *context,
1715                     gpointer data)
1716 {
1717         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1718         g_signal_handlers_disconnect_by_func(G_OBJECT(model),
1719                                              G_CALLBACK(account_row_changed_while_drag_drop),
1720                                              list_view);
1721 }
1722
1723 static void account_row_changed_while_drag_drop(GtkTreeModel *model, 
1724                                    GtkTreePath  *path,
1725                                    GtkTreeIter  *iter,
1726                                    gpointer      arg3,
1727                                    GtkTreeView  *list_view)
1728 {       
1729         account_list_set();     
1730 }