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