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