2006-06-29 [wwp] 2.3.1cvs37
[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(gen_msgid);
916         ACP_FASSIGN(add_customhdr);
917         ACP_FASSIGN(use_smtp_auth);
918         ACP_FASSIGN(smtp_auth_type);
919         ACP_FDUP(smtp_userid);
920         ACP_FDUP(smtp_passwd);
921
922         ac_clon->tmp_smtp_pass = NULL;
923
924         ACP_FASSIGN(pop_before_smtp);
925         ACP_FASSIGN(pop_before_smtp_timeout);
926         ACP_FASSIGN(last_pop_login_time);
927
928         ac_clon->customhdr_list = NULL;
929         hdrs = ac_prefs->customhdr_list;
930         while (hdrs != NULL) {
931                 ch = (CustomHeader *)hdrs->data;
932
933                 cch = g_new0(CustomHeader, 1);
934                 cch->account_id = ac_clon->account_id;  
935                 cch->name = (ch->name != NULL) ? g_strdup(ch->name) : NULL;
936                 cch->value = (ch->value != NULL) ? g_strdup(ch->value) : NULL;
937                 
938                 ac_clon->customhdr_list = g_slist_append(ac_clon->customhdr_list, cch);
939                 
940                 hdrs = g_slist_next(hdrs);
941         }
942
943         /* compose */
944         ACP_FASSIGN(sig_type);
945         ACP_FDUP(sig_path);
946         ACP_FASSIGN(auto_sig);
947         ACP_FDUP(sig_sep);
948         ACP_FASSIGN(set_autocc);
949         ACP_FDUP(auto_cc);
950         ACP_FASSIGN(set_autobcc);
951         ACP_FDUP(auto_bcc);
952         ACP_FASSIGN(set_autoreplyto);
953         ACP_FDUP(auto_replyto);
954
955         /* privacy */
956         ACP_FASSIGN(default_encrypt);
957         ACP_FASSIGN(default_sign);
958         
959         /* advanced */
960         ACP_FASSIGN(set_smtpport);
961         ACP_FASSIGN(smtpport);
962         ACP_FASSIGN(set_popport);
963         ACP_FASSIGN(popport);
964         ACP_FASSIGN(set_imapport);
965         ACP_FASSIGN(imapport);
966         ACP_FASSIGN(set_nntpport);
967         ACP_FASSIGN(nntpport);
968         ACP_FASSIGN(set_domain);
969         ACP_FDUP(domain);
970         ACP_FASSIGN(mark_crosspost_read);
971         ACP_FASSIGN(crosspost_col);
972
973         ACP_FASSIGN(set_tunnelcmd);
974         ACP_FDUP(tunnelcmd);
975
976         ACP_FDUP(imap_dir);
977
978         ACP_FASSIGN(set_sent_folder);
979         ACP_FDUP(sent_folder);
980         ACP_FASSIGN(set_draft_folder);
981         ACP_FDUP(draft_folder);
982         ACP_FASSIGN(set_trash_folder);
983         ACP_FDUP(trash_folder);
984         /* don't want two default accounts */
985         ac_clon->is_default = FALSE;
986         ACP_FASSIGN(folder);
987
988         account_list = g_list_append(account_list, ac_clon);
989         account_list_view_set();
990 }
991 #undef ACP_FDUP
992 #undef ACP_FASSIGN
993
994 static void account_delete(GtkWidget *widget, gpointer data)
995 {
996         PrefsAccount *ac_prefs;
997         gchar buf[BUFFSIZE];
998         GList *list;
999         Folder *folder;
1000  
1001         ac_prefs = account_list_view_get_selected_account(edit_account.list_view);
1002         if (ac_prefs == NULL)
1003                 return;
1004
1005         g_snprintf(buf, sizeof(buf),
1006                    _("Do you really want to delete the account '%s'?"),
1007                    ac_prefs->account_name ? ac_prefs->account_name :
1008                    _("(Untitled)"));
1009         if (alertpanel_full(_("Delete account"), buf,
1010                             GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, FALSE,
1011                             NULL, ALERT_WARNING, G_ALERTDEFAULT) != G_ALERTALTERNATE)
1012                 return;
1013
1014         if (ac_prefs->folder) {
1015                 FolderItem *item;
1016
1017                 item = mainwindow_get_mainwindow()->summaryview->folder_item;
1018                 if (item && item->folder == FOLDER(ac_prefs->folder))
1019                         summary_clear_all(mainwindow_get_mainwindow()->summaryview);
1020                 folder_destroy(FOLDER(ac_prefs->folder));
1021                 folderview_set_all();
1022         }
1023         account_destroy(ac_prefs);
1024         account_list_view_set();
1025
1026         debug_print("Removing deleted account references for all the folders...\n");
1027         list = folder_get_list();
1028         for (; list != NULL; list = list->next) {
1029                 folder = FOLDER(list->data);
1030                 if (folder->node)  /* && folder->type == F_? */
1031                         g_node_traverse(folder->node, G_PRE_ORDER,
1032                                 G_TRAVERSE_ALL, -1,
1033                                 account_delete_references_func,
1034                                 GINT_TO_POINTER(ac_prefs->account_id));
1035         }
1036 }
1037
1038 static void account_up(GtkWidget *widget, gpointer data)
1039 {
1040         GtkTreePath *sel = account_list_view_get_selected_account_path
1041                                 (edit_account.list_view),
1042                     *up;
1043         GtkTreeIter isel, iup;
1044         GtkTreeModel *model = gtk_tree_view_get_model
1045                                 (GTK_TREE_VIEW(edit_account.list_view));
1046         
1047         if (!sel) 
1048                 return;
1049
1050         up = gtk_tree_path_copy(sel);
1051         if (!up) {
1052                 gtk_tree_path_free(sel);
1053                 return;
1054         }
1055
1056         if (!gtk_tree_path_prev(up)) {
1057                 gtk_tree_path_free(up);
1058                 gtk_tree_path_free(sel);
1059                 return;
1060         }
1061
1062         if (!gtk_tree_model_get_iter(model, &isel, sel)
1063         ||  !gtk_tree_model_get_iter(model, &iup,  up)) {
1064                 gtk_tree_path_free(up);
1065                 gtk_tree_path_free(sel);
1066                 return;
1067         }
1068
1069         gtk_list_store_swap(GTK_LIST_STORE(model), &isel, &iup);
1070
1071         account_list_set();
1072         
1073         gtk_tree_path_free(up);
1074         gtk_tree_path_free(sel);
1075 }
1076
1077 static void account_down(GtkWidget *widget, gpointer data)
1078 {
1079         GtkTreePath *sel = account_list_view_get_selected_account_path
1080                                 (edit_account.list_view),
1081                     *dn;
1082         GtkTreeIter isel, idn;
1083         GtkTreeModel *model = gtk_tree_view_get_model
1084                                 (GTK_TREE_VIEW(edit_account.list_view));
1085         
1086         if (!sel) 
1087                 return;
1088
1089         dn = gtk_tree_path_copy(sel);
1090         if (!dn) {
1091                 gtk_tree_path_free(sel);
1092                 return;
1093         }
1094
1095         /* XXX no check possible??? however, if down but at bottom, then 
1096          * nothing seems to happen much anyway, so the following seems to 
1097          * be okay */
1098         gtk_tree_path_next(dn);
1099
1100         if (!gtk_tree_model_get_iter(model, &isel, sel)
1101         ||  !gtk_tree_model_get_iter(model, &idn,  dn)) {
1102                 gtk_tree_path_free(dn);
1103                 gtk_tree_path_free(sel);
1104                 return;
1105         }
1106
1107         gtk_list_store_swap(GTK_LIST_STORE(model), &isel, &idn);
1108
1109         account_list_set();
1110         
1111         gtk_tree_path_free(dn);
1112         gtk_tree_path_free(sel);
1113 }
1114
1115 static void account_set_default(GtkWidget *widget, gpointer data)
1116 {
1117         PrefsAccount *ac_prefs;
1118
1119         if (NULL == (ac_prefs = account_list_view_get_selected_account
1120                                         (edit_account.list_view))) 
1121                 return; 
1122
1123         /* we need to change the store variables by resetting everything
1124          * and setting the new default one */
1125         account_list_view_set_default_by_id(edit_account.list_view,
1126                                             ac_prefs->account_id);               
1127         
1128         account_set_as_default(ac_prefs);
1129         account_list_view_set();
1130         
1131         cur_account = ac_prefs;
1132         account_set_menu();
1133         main_window_reflect_prefs_all();
1134 }
1135
1136 static void account_edit_close(GtkWidget *widget, gpointer data)
1137 {
1138         account_list_set();
1139         account_write_config_all();
1140
1141         if (!cur_account && account_list) {
1142                 PrefsAccount *ac_prefs = (PrefsAccount *)account_list->data;
1143                 account_set_as_default(ac_prefs);
1144                 cur_account = ac_prefs;
1145         }
1146
1147         account_set_menu();
1148         main_window_reflect_prefs_all();
1149
1150         gtk_widget_hide(edit_account.window);
1151
1152         inc_unlock();
1153 }
1154
1155 static gint account_delete_event(GtkWidget *widget, GdkEventAny *event,
1156                                  gpointer data)
1157 {
1158         account_edit_close(NULL, NULL);
1159         return TRUE;
1160 }
1161
1162 static gboolean account_key_pressed(GtkWidget *widget, GdkEventKey *event,
1163                                     gpointer data)
1164 {
1165         if (event && event->keyval == GDK_Escape)
1166                 account_edit_close(NULL, NULL);
1167         return FALSE;
1168 }
1169
1170 static void account_list_view_add(PrefsAccount *ac_prefs)
1171 {
1172         GtkTreeView *list_view = GTK_TREE_VIEW(edit_account.list_view);
1173         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model(list_view));
1174         gchar *name, *protocol, *server;
1175         gboolean has_getallbox;
1176         gboolean getall;
1177
1178         name = ac_prefs->account_name;
1179 #if USE_OPENSSL
1180         protocol = ac_prefs->protocol == A_POP3 ?
1181                   (ac_prefs->ssl_pop == SSL_TUNNEL ?
1182                    "POP3 (SSL)" :
1183                    ac_prefs->ssl_pop == SSL_STARTTLS ?
1184                    "POP3 (TLS)" : "POP3") :
1185                    ac_prefs->protocol == A_IMAP4 ?
1186                   (ac_prefs->ssl_imap == SSL_TUNNEL ?
1187                    "IMAP4 (SSL)" :
1188                    ac_prefs->ssl_imap == SSL_STARTTLS ?
1189                    "IMAP4 (TLS)" : "IMAP4") :
1190                    ac_prefs->protocol == A_NNTP ?
1191                   (ac_prefs->ssl_nntp == SSL_TUNNEL ?
1192                    "NNTP (SSL)" : "NNTP") :
1193                    "";
1194 #else
1195         protocol = ac_prefs->protocol == A_POP3  ? "POP3" :
1196                    ac_prefs->protocol == A_IMAP4 ? "IMAP4" :
1197                    ac_prefs->protocol == A_LOCAL ? "Local" :
1198                    ac_prefs->protocol == A_NNTP  ? "NNTP" : "";
1199 #endif
1200         server= ac_prefs->protocol == A_NNTP
1201                 ? ac_prefs->nntp_server : ac_prefs->recv_server;
1202
1203         has_getallbox = (ac_prefs->protocol == A_POP3  ||
1204                          ac_prefs->protocol == A_IMAP4 ||
1205                          ac_prefs->protocol == A_NNTP  ||
1206                          ac_prefs->protocol == A_LOCAL);
1207         getall = has_getallbox && ac_prefs->recv_at_getall;
1208
1209         account_list_view_insert_account_item(list_store,
1210                                              name, protocol, server,
1211                                              ac_prefs->is_default,
1212                                              getall, ac_prefs);
1213         return;
1214 }
1215
1216 static void account_list_view_set(void)
1217 {
1218         GList *cur;
1219         gint prev_sel_account;
1220         GtkListStore *store;
1221         
1222         store = GTK_LIST_STORE(gtk_tree_view_get_model
1223                 (GTK_TREE_VIEW(edit_account.list_view)));
1224
1225         prev_sel_account = account_list_view_get_selected_account_id
1226                 (edit_account.list_view); 
1227
1228         gtk_list_store_clear(store);
1229         
1230         for (cur = account_list; cur != NULL; cur = cur->next) {
1231                 account_list_view_add((PrefsAccount *)cur->data);
1232                 if ((PrefsAccount *)cur->data == cur_account)
1233                         account_list_view_select_account
1234                                 (edit_account.list_view, 
1235                                  cur_account->account_id);
1236         }
1237         
1238         if (prev_sel_account >= 0)
1239                 account_list_view_select_account(edit_account.list_view, 
1240                                                  prev_sel_account); 
1241 }
1242
1243 /* set account list from CList */
1244 static void account_list_set(void)
1245 {
1246         /* want to make sure we iterate *IN ORDER*, so therefore using
1247          * gtk_tree_model_XXXX_nth_child() */
1248         gint row, n_rows;
1249         PrefsAccount *ac_prefs;
1250         GtkTreeModel *model = gtk_tree_view_get_model
1251                                 (GTK_TREE_VIEW(edit_account.list_view));
1252         
1253         while (account_list)
1254                 account_list = g_list_remove(account_list, account_list->data);
1255
1256         n_rows = gtk_tree_model_iter_n_children(model, NULL);
1257
1258         for (row = 0; row < n_rows; row++) {
1259                 GtkTreeIter iter;
1260
1261                 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
1262                         g_warning("%s(%d) - no iter found???\n", __FILE__, __LINE__);                                         
1263                         continue;
1264                 }
1265         
1266                 ac_prefs = NULL;
1267                 gtk_tree_model_get(model, &iter,
1268                                    ACCOUNT_DATA, &ac_prefs,
1269                                    -1);
1270                 if (ac_prefs)
1271                         account_list = g_list_append(account_list, ac_prefs);
1272         }
1273 }
1274
1275 /*!
1276  *\brief        finds the PrefsAccounts which should be used to answer a mail
1277  *
1278  *\param        msginfo The message to be answered
1279  *\param        reply_autosel Indicates whether reply account autoselection is on
1280  *
1281  *\return       PrefsAccount * the correct account, NULL if not found
1282  */
1283 PrefsAccount *account_get_reply_account(MsgInfo *msginfo, gboolean reply_autosel)
1284 {
1285         PrefsAccount *account = NULL;
1286         /* select the account set in folderitem's property (if enabled) */
1287         if (msginfo->folder->prefs && msginfo->folder->prefs->enable_default_account)
1288                 account = account_find_from_id(msginfo->folder->prefs->default_account);
1289         
1290         /* select account by to: and cc: header if enabled */
1291         if (reply_autosel) {
1292                 gchar * field = NULL;
1293                 int fieldno = 0;
1294                 for (field = msginfo->to; fieldno++ < 2; field = msginfo->cc) {
1295                         if (!account && field) {
1296                                 gchar *to = NULL;
1297                                 if (!strchr(field, ',')) {
1298                                         Xstrdup_a(to, field, return NULL);
1299                                         extract_address(to);
1300                                         account = account_find_from_address(to);
1301                                 } else {
1302                                         gchar **split = g_strsplit(field, ",", -1);
1303                                         int i = -1;
1304                                         do {
1305                                                 i++;
1306                                                 if (!split[i])
1307                                                         break;
1308                                                 Xstrdup_a(to, split[i], return NULL);
1309                                                 extract_address(to);
1310                                                 account = account_find_from_address(to);
1311                                         } while (!account);
1312                                         g_strfreev(split);
1313                                 }
1314                         }
1315                 }
1316         }
1317
1318         /* select the account for the whole folder (IMAP / NNTP) */
1319         if (!account) 
1320                 /* FIXME: this is not right, because folder may be nested. we should
1321                  * ascend the tree until we find a parent with proper account 
1322                  * information */
1323                 account = msginfo->folder->folder->account;
1324
1325         /* select current account */
1326         if (!account) account = cur_account;
1327         
1328         return account;
1329 }
1330
1331 /*!
1332  *\brief        Create data store
1333  */
1334 static GtkListStore* account_create_data_store(void)
1335 {
1336         return gtk_list_store_new(N_ACCOUNT_COLUMNS,
1337                                  GDK_TYPE_PIXBUF,       /* ACCOUNT_IS_DEFAULT */
1338                                  G_TYPE_BOOLEAN,        /* ACCOUNT_ENABLE_GET_ALL */
1339                                  G_TYPE_STRING,         /* ACCOUNT_NAME */
1340                                  G_TYPE_STRING,         /* ACCOUNT_PROTOCOL */
1341                                  G_TYPE_STRING,         /* ACCOUNT_SERVER */
1342                                  G_TYPE_POINTER,        /* ACCOUNT_DATA */
1343                                  -1);
1344 }
1345
1346 /*!
1347  *\brief        Insert an account item in the list. 
1348  *
1349  *\return       GtkTreeRowReference * A tree row reference, which is guaranteed to 
1350  *              stable whatever operations are performed on the list.
1351  */
1352 static void account_list_view_insert_account_item(GtkListStore *list_store, 
1353                                                   const gchar *account_name,
1354                                                   const gchar *protocol, 
1355                                                   const gchar *server_name,
1356                                                   gboolean is_default, 
1357                                                   gboolean is_get_all,
1358                                                   PrefsAccount *account_data)
1359 {
1360         GtkTreeIter iter;
1361         
1362         gtk_list_store_append(list_store, &iter);
1363         gtk_list_store_set(list_store, &iter, 
1364                            ACCOUNT_IS_DEFAULT,     is_default ? mark_pixbuf : NULL,
1365                            ACCOUNT_ENABLE_GET_ALL, is_get_all,
1366                            ACCOUNT_NAME,           account_name,
1367                            ACCOUNT_PROTOCOL,       protocol,
1368                            ACCOUNT_SERVER,         server_name,
1369                            ACCOUNT_DATA,           account_data,
1370                            -1);
1371 }
1372
1373 /*!
1374  *\brief        Create and set up account list view, including tasks like
1375  *              creating the data store (\ref account_create_data_store()),
1376  *              creating images for the list view (\ref account_create_list_view_images()),
1377  *              and setting up the account list's individual columns (\ref 
1378  *              account_create_list_view_columns()).
1379  *
1380  *\return       GtkWidget * The created list view widget.
1381  */
1382 static GtkWidget *account_list_view_create(void)
1383 {
1384         GtkTreeView *list_view;
1385         GtkTreeSelection *selector;
1386         GtkListStore *store = account_create_data_store();
1387
1388         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)));
1389         g_object_unref(G_OBJECT(store));
1390
1391         gtk_tree_view_set_rules_hint(list_view, prefs_common.enable_rules_hint);
1392         gtk_tree_view_set_reorderable(list_view, TRUE);
1393         
1394         selector = gtk_tree_view_get_selection(list_view);
1395         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1396
1397         /* create the columns */
1398         account_create_list_view_columns(GTK_WIDGET(list_view));
1399
1400         /* set a double click listener */
1401         g_signal_connect(G_OBJECT(list_view), "row_activated",
1402                          G_CALLBACK(account_double_clicked),
1403                          list_view);
1404
1405         g_signal_connect(G_OBJECT(list_view), "drag_begin",                      
1406                          G_CALLBACK(drag_begin),
1407                          list_view);
1408                          
1409         g_signal_connect(G_OBJECT(list_view), "drag_end",                        
1410                          G_CALLBACK(drag_end),
1411                          list_view);
1412                          
1413         gtk_tree_view_set_reorderable(list_view, TRUE);
1414         return GTK_WIDGET(list_view);
1415 }
1416
1417 static void account_create_list_view_images(GtkWidget *list_view)
1418 {
1419         stock_pixbuf_gdk(list_view, STOCK_PIXMAP_MARK, &mark_pixbuf);
1420 }
1421
1422 static void account_create_list_view_columns(GtkWidget *list_view)
1423 {
1424         GtkTreeViewColumn *column;
1425         GtkCellRenderer *renderer;
1426
1427         renderer = gtk_cell_renderer_pixbuf_new();
1428         column = gtk_tree_view_column_new_with_attributes
1429                 ("D", renderer,
1430                  "pixbuf", ACCOUNT_IS_DEFAULT,
1431                  NULL);
1432         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);           
1433
1434         renderer = gtk_cell_renderer_toggle_new();
1435         g_object_set(renderer, 
1436                      "radio", FALSE, 
1437                      "activatable", TRUE,
1438                       NULL);
1439         column = gtk_tree_view_column_new_with_attributes
1440                 ("G", renderer,
1441                  "active", ACCOUNT_ENABLE_GET_ALL,
1442                  NULL);
1443         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1444         g_signal_connect(G_OBJECT(renderer), "toggled",                      
1445                          G_CALLBACK(account_get_all_toggled),
1446                          list_view);
1447         
1448         renderer = gtk_cell_renderer_text_new();
1449         column = gtk_tree_view_column_new_with_attributes
1450                 (_("Name"), renderer,
1451                  "text", ACCOUNT_NAME,
1452                  NULL);
1453         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1454         
1455         renderer = gtk_cell_renderer_text_new();
1456         column = gtk_tree_view_column_new_with_attributes
1457                 (_("Protocol"), renderer,
1458                  "text", ACCOUNT_PROTOCOL,
1459                  NULL);
1460         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);           
1461         
1462         renderer = gtk_cell_renderer_text_new();
1463         column = gtk_tree_view_column_new_with_attributes
1464                 (_("Server"), renderer,
1465                  "text", ACCOUNT_SERVER,
1466                  NULL);
1467         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);           
1468 }
1469
1470 /*!
1471  *\brief        Get currently selected account (by its unique ID)
1472  */
1473 static gint account_list_view_get_selected_account_id(GtkWidget *list_view)
1474 {
1475         GtkTreeSelection *selector;
1476         GtkTreeModel *model;
1477         GtkTreeIter iter;
1478         PrefsAccount *res = NULL;
1479
1480         selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
1481         
1482         if (!gtk_tree_selection_get_selected(selector, &model, &iter))
1483                 return -1;
1484
1485         gtk_tree_model_get(model, &iter, ACCOUNT_DATA, &res, -1);
1486
1487         return res->account_id;                    
1488 }
1489
1490 /*!
1491  *\brief        Get the tree path of the currently selected account
1492  */
1493 GtkTreePath *account_list_view_get_selected_account_path(GtkWidget *list_view)
1494 {
1495         GtkTreeSelection *selector;
1496         GtkTreeModel *model;
1497         GtkTreeIter iter;
1498
1499         selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
1500         
1501         if (!gtk_tree_selection_get_selected(selector, &model, &iter))
1502                 return NULL;
1503
1504         return gtk_tree_model_get_path(gtk_tree_view_get_model
1505                 (GTK_TREE_VIEW(list_view)), &iter);
1506 }
1507
1508 /*!
1509  *\brief        Get the account data of the currently selected account
1510  */
1511 PrefsAccount *account_list_view_get_selected_account(GtkWidget *list_view)
1512 {
1513         GtkTreeSelection *selector;
1514         GtkTreeModel *model;
1515         GtkTreeIter iter;
1516         PrefsAccount *res = NULL;
1517
1518         selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
1519         
1520         if (!gtk_tree_selection_get_selected(selector, &model, &iter))
1521                 return NULL;
1522
1523         gtk_tree_model_get(model, &iter, ACCOUNT_DATA, &res, -1);
1524
1525         return res;                        
1526 }
1527
1528 /*!
1529  *\brief        Select a row by the account it represents
1530  *
1531  *\return       gboolean TRUE if found and selected, FALSE if not.
1532  */
1533 gboolean account_list_view_select_account(GtkWidget *list_view, gint account_id)
1534 {
1535         FindAccountInStore fis;
1536         GtkTreeModel *model;
1537         
1538         fis.account_id = account_id;
1539         fis.path = NULL;
1540
1541         model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1542
1543         gtk_tree_model_foreach(model, (GtkTreeModelForeachFunc) find_account_in_store,
1544                                &fis);
1545                                
1546         if (fis.path) {
1547                 GtkTreeSelection *selection;
1548                 GtkTreePath* path;
1549
1550                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
1551                 gtk_tree_selection_select_iter(selection, &fis.iter);
1552                 path = gtk_tree_model_get_path(model, &fis.iter);
1553                 /* XXX returned path may not be valid??? create new one to be sure */ 
1554                 gtk_tree_view_set_cursor(GTK_TREE_VIEW(list_view), path, NULL, FALSE);
1555                 gtk_tree_path_free(path);
1556         }
1557
1558         return fis.path != NULL;
1559 }
1560
1561 /*!
1562  *\brief        Set a new default account by its ID. (There is only one
1563  *              default account.)
1564  */
1565 static void account_list_view_set_default_by_id(GtkWidget *list_view,
1566                                                 gint account_id)
1567 {
1568         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1569         
1570         gtk_tree_model_foreach
1571                 (model, (GtkTreeModelForeachFunc) set_new_default_account,
1572                  &account_id);
1573 }
1574
1575 static gboolean set_new_default_account(GtkTreeModel *model,
1576                                         GtkTreePath  *path,
1577                                         GtkTreeIter  *iter,
1578                                         gint         *account_id)
1579 {
1580         PrefsAccount *account = NULL;
1581         GdkPixbuf *pixbuf;
1582         
1583         gtk_tree_model_get(model, iter, 
1584                            ACCOUNT_DATA, &account, 
1585                            -1);
1586
1587         if (*account_id == account->account_id)
1588                 pixbuf = NULL;
1589         else
1590                 pixbuf = mark_pixbuf;
1591         
1592         gtk_list_store_set(GTK_LIST_STORE(model), iter, 
1593                            ACCOUNT_IS_DEFAULT, pixbuf,
1594                            -1);
1595
1596         return FALSE;
1597 }
1598                                         
1599 static gboolean find_account_in_store(GtkTreeModel *model,
1600                                       GtkTreePath  *path,
1601                                       GtkTreeIter  *iter,
1602                                       FindAccountInStore *data)
1603 {
1604         PrefsAccount *account = NULL;
1605         gtk_tree_model_get(model, iter, ACCOUNT_DATA, &account, -1);
1606
1607         if (data->account_id == account->account_id) {
1608                 data->path = path; /* signal we found it */
1609                 data->iter = *iter;
1610                 return TRUE;
1611         }
1612
1613         return FALSE; 
1614 }
1615
1616 /*!
1617  *\brief        Triggered when "get all" column is activated or de-activated
1618  */
1619 static void account_get_all_toggled(GtkCellRendererToggle *widget, 
1620                                     gchar *path, 
1621                                     GtkWidget *list_view)
1622 {
1623         GtkTreeIter iter;
1624         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1625         PrefsAccount *ac = NULL;
1626         gboolean get_all;
1627         
1628         if (!gtk_tree_model_get_iter_from_string(model, &iter, path))
1629                 return;
1630
1631         gtk_tree_model_get(model, &iter, 
1632                            ACCOUNT_DATA, &ac,
1633                            ACCOUNT_ENABLE_GET_ALL, &get_all,
1634                            -1);
1635
1636         /* check if the account has a selectable get all checkbox anyway... */
1637         if (!(ac->protocol == A_POP3  || 
1638               ac->protocol == A_IMAP4 ||
1639               ac->protocol == A_NNTP  ||
1640               ac->protocol == A_LOCAL))
1641                 return;       
1642
1643         /* set value in store */
1644         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
1645                            ACCOUNT_ENABLE_GET_ALL, !get_all,
1646                            -1);
1647
1648         /* set value in account */
1649         ac->recv_at_getall ^= TRUE;
1650 }
1651
1652 static void account_double_clicked(GtkTreeView          *list_view,
1653                                    GtkTreePath          *path,
1654                                    GtkTreeViewColumn    *column,
1655                                    gpointer              data)
1656 {
1657         account_edit_prefs(NULL, NULL); 
1658 }
1659
1660 static void drag_begin(GtkTreeView *list_view,
1661                       GdkDragContext *context,
1662                       gpointer data)
1663 {
1664         /* XXX unfortunately a completed drag & drop does not emit 
1665          * a "rows_reordered" signal, but a "row_changed" signal.
1666          * So during drag and drop, listen to "row_changed", and
1667          * update the account list accordingly */
1668
1669         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1670         g_signal_connect(G_OBJECT(model), "row_changed",
1671                          G_CALLBACK(account_row_changed_while_drag_drop),
1672                          list_view);
1673 }
1674
1675 static void drag_end(GtkTreeView *list_view,
1676                     GdkDragContext *context,
1677                     gpointer data)
1678 {
1679         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1680         g_signal_handlers_disconnect_by_func(G_OBJECT(model),
1681                                              G_CALLBACK(account_row_changed_while_drag_drop),
1682                                              list_view);
1683 }
1684
1685 static void account_row_changed_while_drag_drop(GtkTreeModel *model, 
1686                                    GtkTreePath  *path,
1687                                    GtkTreeIter  *iter,
1688                                    gpointer      arg3,
1689                                    GtkTreeView  *list_view)
1690 {       
1691         account_list_set();     
1692 }