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