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