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