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