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