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