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