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