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