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