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