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