sync with sylpheed 0.4.66cvs6
[claws.git] / src / account.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999,2000 Hiroyuki Yamamoto
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 2 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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 <gtk/gtk.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <stdio.h>
30 #include <errno.h>
31
32 #include "intl.h"
33 #include "main.h"
34 #include "mainwindow.h"
35 #include "folderview.h"
36 #include "folder.h"
37 #include "account.h"
38 #include "prefs.h"
39 #include "prefs_account.h"
40 #include "compose.h"
41 #include "manage_window.h"
42 #include "inc.h"
43 #include "gtkutils.h"
44 #include "utils.h"
45 #include "alertpanel.h"
46
47 typedef enum
48 {
49         COL_DEFAULT     = 0,
50         COL_NAME        = 1,
51         COL_PROTOCOL    = 2,
52         COL_SERVER      = 3
53 } EditAccountColumnPos;
54
55 # define N_EDIT_ACCOUNT_COLS    4
56
57 #define PREFSBUFSIZE            1024
58
59 PrefsAccount *cur_account;
60
61 static GList *account_list = NULL;
62
63 static struct EditAccount {
64         GtkWidget *window;
65         GtkWidget *clist;
66         GtkWidget *close_btn;
67 } edit_account;
68
69 static void account_edit_create         (void);
70
71 static void account_edit_prefs          (void);
72 static void account_delete              (void);
73
74 static void account_up                  (void);
75 static void account_down                (void);
76
77 static void account_set_default         (void);
78
79 static void account_edit_close          (void);
80 static gint account_delete_event        (GtkWidget      *widget,
81                                          GdkEventAny    *event,
82                                          gpointer        data);
83 static void account_key_pressed         (GtkWidget      *widget,
84                                          GdkEventKey    *event,
85                                          gpointer        data);
86
87 static gint account_clist_set_row       (PrefsAccount   *ac_prefs,
88                                          gint            row);
89 static void account_clist_set           (void);
90
91 static void account_list_set            (void);
92
93 void account_read_config_all(void)
94 {
95         GSList *ac_label_list = NULL, *cur;
96         gchar *rcpath;
97         FILE *fp;
98         gchar buf[PREFSBUFSIZE];
99         PrefsAccount *ac_prefs;
100
101         debug_print(_("Reading all config for each account...\n"));
102
103         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACCOUNT_RC, NULL);
104         if ((fp = fopen(rcpath, "r")) == NULL) {
105                 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
106                 g_free(rcpath);
107                 return;
108         }
109         g_free(rcpath);
110
111         while (fgets(buf, sizeof(buf), fp) != NULL) {
112                 if (!strncmp(buf, "[Account: ", 10)) {
113                         strretchomp(buf);
114                         memmove(buf, buf + 1, strlen(buf));
115                         buf[strlen(buf) - 1] = '\0';
116                         debug_print(_("Found label: %s\n"), buf);
117                         ac_label_list = g_slist_append(ac_label_list,
118                                                        g_strdup(buf));
119                 }
120         }
121         fclose(fp);
122
123         /* read config data from file */
124         cur_account = NULL;
125         for (cur = ac_label_list; cur != NULL; cur = cur->next) {
126                 ac_prefs = g_new0(PrefsAccount, 1);
127                 prefs_account_read_config(ac_prefs, (gchar *)cur->data);
128                 account_list = g_list_append(account_list, ac_prefs);
129                 if (ac_prefs->is_default)
130                         cur_account = ac_prefs;
131         }
132         /* if default is not set, assume first account as default */
133         if (!cur_account && account_list) {
134                 ac_prefs = (PrefsAccount *)account_list->data;
135                 account_set_as_default(ac_prefs);
136                 cur_account = ac_prefs;
137         }
138
139         account_set_menu();
140         main_window_reflect_prefs_all();
141
142         while (ac_label_list) {
143                 g_free(ac_label_list->data);
144                 ac_label_list = g_slist_remove(ac_label_list,
145                                                ac_label_list->data);
146         }
147 }
148
149 void account_save_config_all(void)
150 {
151         prefs_account_save_config_all(account_list);
152 }
153
154 PrefsAccount *account_find_from_smtp_server(const gchar *address,
155                                             const gchar *smtp_server)
156 {
157         GList *cur;
158         PrefsAccount *ac;
159
160         for (cur = account_list; cur != NULL; cur = cur->next) {
161                 ac = (PrefsAccount *)cur->data;
162                 if (!strcmp2(address, ac->address) &&
163                     !strcmp2(smtp_server, ac->smtp_server))
164                         return ac;
165         }
166
167         return NULL;
168 }
169
170 PrefsAccount *account_find_from_id(gint id)
171 {
172         GList *cur;
173         PrefsAccount *ac;
174
175         for (cur = account_list; cur != NULL; cur = cur->next) {
176                 ac = (PrefsAccount *)cur->data;
177                 if (id == ac->account_id)
178                         return ac;
179         }
180
181         return NULL;
182 }
183
184 void account_set_menu(void)
185 {
186         main_window_set_account_menu(account_list);
187 }
188
189 void account_foreach(AccountFunc func, gpointer user_data)
190 {
191         GList *cur;
192
193         for (cur = account_list; cur != NULL; cur = cur->next)
194                 if (func((PrefsAccount *)cur->data, user_data) != 0)
195                         return;
196 }
197
198 GList *account_get_list(void)
199 {
200         return account_list;
201 }
202
203 void account_edit_open(void)
204 {
205         inc_autocheck_timer_remove();
206         
207         if (compose_get_compose_list()) {
208                 alertpanel_notice(_("Some composing windows are open.\n"
209                                     "Please close all the composing windows before editing the accounts."));
210                 inc_autocheck_timer_set();                                      
211                 return;
212         }
213
214         debug_print(_("Opening account edit window...\n"));
215
216         if (!edit_account.window)
217                 account_edit_create();
218
219         account_clist_set();
220
221         manage_window_set_transient(GTK_WINDOW(edit_account.window));
222         gtk_widget_grab_focus(edit_account.close_btn);
223         gtk_widget_show(edit_account.window);
224
225         manage_window_focus_in(edit_account.window, NULL, NULL);
226 }
227
228 void account_add(void)
229 {
230         PrefsAccount *ac_prefs;
231
232         ac_prefs = prefs_account_open(NULL);
233         inc_autocheck_timer_remove();
234
235         if (!ac_prefs) return;
236
237         account_list = g_list_append(account_list, ac_prefs);
238
239         if (ac_prefs->is_default)
240                 account_set_as_default(ac_prefs);
241
242         account_clist_set();
243
244         if (ac_prefs->protocol == A_IMAP4 || ac_prefs->protocol == A_NNTP) {
245                 Folder *folder;
246
247                 if (ac_prefs->protocol == A_IMAP4) {
248                         folder = folder_new(F_IMAP, ac_prefs->account_name,
249                                             ac_prefs->recv_server);
250                 } else {
251                         folder = folder_new(F_NEWS, ac_prefs->account_name,
252                                             ac_prefs->nntp_server);
253                 }
254                 folder->account = ac_prefs;
255                 ac_prefs->folder = REMOTE_FOLDER(folder);
256                 folder_add(folder);
257                 folderview_update_all();
258         }
259 }
260
261 void account_set_as_default(PrefsAccount *ac_prefs)
262 {
263         PrefsAccount *ap;
264         GList *cur;
265
266         for (cur = account_list; cur != NULL; cur = cur->next) {
267                 ap = (PrefsAccount *)cur->data;
268                 if (ap->is_default)
269                         ap->is_default = FALSE;
270         }
271
272         ac_prefs->is_default = TRUE;
273 }
274
275 PrefsAccount *account_get_default(void)
276 {
277         PrefsAccount *ap;
278         GList *cur;
279
280         for (cur = account_list; cur != NULL; cur = cur->next) {
281                 ap = (PrefsAccount *)cur->data;
282                 if (ap->is_default)
283                         return ap;
284         }
285
286         return NULL;
287 }
288
289 void account_set_missing_folder(void)
290 {
291         PrefsAccount *ap;
292         GList *cur;
293
294         for (cur = account_list; cur != NULL; cur = cur->next) {
295                 ap = (PrefsAccount *)cur->data;
296                 if ((ap->protocol == A_IMAP4 || ap->protocol == A_NNTP) &&
297                     !ap->folder) {
298                         Folder *folder;
299
300                         if (ap->protocol == A_IMAP4) {
301                                 folder = folder_new(F_IMAP, ap->account_name,
302                                                     ap->recv_server);
303                         } else {
304                                 folder = folder_new(F_NEWS, ap->account_name,
305                                                     ap->nntp_server);
306                         }
307                         folder->account = ap;
308                         ap->folder = REMOTE_FOLDER(folder);
309                         folder_add(folder);
310                 }
311         }
312 }
313
314 void account_destroy(PrefsAccount *ac_prefs)
315 {
316         g_return_if_fail(ac_prefs != NULL);
317
318         prefs_account_free(ac_prefs);
319         account_list = g_list_remove(account_list, ac_prefs);
320
321         if (cur_account == ac_prefs) cur_account = NULL;
322         if (!cur_account && account_list) {
323                 cur_account = account_get_default();
324                 if (!cur_account) {
325                         ac_prefs = (PrefsAccount *)account_list->data;
326                         account_set_as_default(ac_prefs);
327                         cur_account = ac_prefs;
328                 }
329         }
330 }
331
332
333 static void account_edit_create(void)
334 {
335         GtkWidget *window;
336         GtkWidget *vbox;
337         GtkWidget *hbox;
338         GtkWidget *scrolledwin;
339         GtkWidget *clist;
340         gchar *titles[N_EDIT_ACCOUNT_COLS];
341         gint i;
342
343         GtkWidget *vbox2;
344         GtkWidget *add_btn;
345         GtkWidget *edit_btn;
346         GtkWidget *del_btn;
347         GtkWidget *up_btn;
348         GtkWidget *down_btn;
349
350         GtkWidget *default_btn;
351
352         GtkWidget *hbbox;
353         GtkWidget *close_btn;
354
355         debug_print(_("Creating account edit window...\n"));
356
357         window = gtk_window_new (GTK_WINDOW_DIALOG);
358         gtk_widget_set_usize (window, 500, 320);
359         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
360         gtk_window_set_title (GTK_WINDOW (window), _("Edit accounts"));
361         gtk_window_set_modal (GTK_WINDOW (window), TRUE);
362         gtk_signal_connect (GTK_OBJECT (window), "delete_event",
363                             GTK_SIGNAL_FUNC (account_delete_event), NULL);
364         gtk_signal_connect (GTK_OBJECT (window), "key_press_event",
365                             GTK_SIGNAL_FUNC (account_key_pressed), NULL);
366         gtk_signal_connect (GTK_OBJECT (window), "focus_in_event",
367                             GTK_SIGNAL_FUNC (manage_window_focus_in), NULL);
368         gtk_signal_connect (GTK_OBJECT (window), "focus_out_event",
369                             GTK_SIGNAL_FUNC (manage_window_focus_out), NULL);
370
371         vbox = gtk_vbox_new (FALSE, 12);
372         gtk_widget_show (vbox);
373         gtk_container_add (GTK_CONTAINER (window), vbox);
374
375         hbox = gtk_hbox_new (FALSE, 8);
376         gtk_widget_show (hbox);
377         gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
378         gtk_container_set_border_width (GTK_CONTAINER (hbox), 2);
379
380         scrolledwin = gtk_scrolled_window_new (NULL, NULL);
381         gtk_widget_show (scrolledwin);
382         gtk_box_pack_start (GTK_BOX (hbox), scrolledwin, TRUE, TRUE, 0);
383         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwin),
384                                         GTK_POLICY_AUTOMATIC,
385                                         GTK_POLICY_AUTOMATIC);
386
387         titles[COL_DEFAULT]  = "";
388         titles[COL_NAME]     = _("Name");
389         titles[COL_PROTOCOL] = _("Protocol");
390         titles[COL_SERVER]   = _("Server");
391
392         clist = gtk_clist_new_with_titles (N_EDIT_ACCOUNT_COLS, titles);
393         gtk_widget_show (clist);
394         gtk_container_add (GTK_CONTAINER (scrolledwin), clist);
395         gtk_clist_set_column_width (GTK_CLIST(clist), COL_DEFAULT , 16);
396         gtk_clist_set_column_width (GTK_CLIST(clist), COL_NAME    , 100);
397         gtk_clist_set_column_width (GTK_CLIST(clist), COL_PROTOCOL, 70);
398         gtk_clist_set_column_width (GTK_CLIST(clist), COL_SERVER  , 100);
399         gtk_clist_set_selection_mode (GTK_CLIST(clist), GTK_SELECTION_BROWSE);
400
401         for (i = 0; i < N_EDIT_ACCOUNT_COLS; i++)
402                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist)->column[i].button,
403                                        GTK_CAN_FOCUS);
404
405         vbox2 = gtk_vbox_new (FALSE, 0);
406         gtk_widget_show (vbox2);
407         gtk_box_pack_start (GTK_BOX (hbox), vbox2, FALSE, FALSE, 0);
408
409         add_btn = gtk_button_new_with_label (_("Add"));
410         gtk_widget_show (add_btn);
411         gtk_box_pack_start (GTK_BOX (vbox2), add_btn, FALSE, FALSE, 4);
412         gtk_signal_connect (GTK_OBJECT(add_btn), "clicked",
413                             GTK_SIGNAL_FUNC (account_add), NULL);
414
415         edit_btn = gtk_button_new_with_label (_("Edit"));
416         gtk_widget_show (edit_btn);
417         gtk_box_pack_start (GTK_BOX (vbox2), edit_btn, FALSE, FALSE, 4);
418         gtk_signal_connect (GTK_OBJECT(edit_btn), "clicked",
419                             GTK_SIGNAL_FUNC (account_edit_prefs), NULL);
420
421         del_btn = gtk_button_new_with_label (_(" Delete "));
422         gtk_widget_show (del_btn);
423         gtk_box_pack_start (GTK_BOX (vbox2), del_btn, FALSE, FALSE, 4);
424         gtk_signal_connect (GTK_OBJECT(del_btn), "clicked",
425                             GTK_SIGNAL_FUNC (account_delete), NULL);
426
427         down_btn = gtk_button_new_with_label (_("Down"));
428         gtk_widget_show (down_btn);
429         gtk_box_pack_end (GTK_BOX (vbox2), down_btn, FALSE, FALSE, 4);
430         gtk_signal_connect (GTK_OBJECT(down_btn), "clicked",
431                             GTK_SIGNAL_FUNC (account_down), NULL);
432
433         up_btn = gtk_button_new_with_label (_("Up"));
434         gtk_widget_show (up_btn);
435         gtk_box_pack_end (GTK_BOX (vbox2), up_btn, FALSE, FALSE, 4);
436         gtk_signal_connect (GTK_OBJECT(up_btn), "clicked",
437                             GTK_SIGNAL_FUNC (account_up), NULL);
438
439         hbox = gtk_hbox_new (FALSE, 8);
440         gtk_widget_show (hbox);
441         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
442
443         vbox2 = gtk_vbox_new(FALSE, 0);
444         gtk_widget_show (vbox2);
445         gtk_box_pack_start (GTK_BOX (hbox), vbox2, FALSE, FALSE, 0);
446
447         default_btn = gtk_button_new_with_label (_(" Set as usually used account "));
448         gtk_widget_show (default_btn);
449         gtk_box_pack_start (GTK_BOX (vbox2), default_btn, TRUE, FALSE, 0);
450         gtk_signal_connect (GTK_OBJECT(default_btn), "clicked",
451                             GTK_SIGNAL_FUNC (account_set_default), NULL);
452
453         gtkut_button_set_create(&hbbox, &close_btn, _("Close"),
454                                 NULL, NULL, NULL, NULL);
455         gtk_widget_show(hbbox);
456         gtk_box_pack_end (GTK_BOX (hbox), hbbox, FALSE, FALSE, 0);
457         gtk_widget_grab_default (close_btn);
458
459         gtk_signal_connect (GTK_OBJECT (close_btn), "clicked",
460                             GTK_SIGNAL_FUNC (account_edit_close),
461                             NULL);
462
463         edit_account.window    = window;
464         edit_account.clist     = clist;
465         edit_account.close_btn = close_btn;
466 }
467
468 static void account_edit_prefs(void)
469 {
470         GtkCList *clist = GTK_CLIST(edit_account.clist);
471         PrefsAccount *ac_prefs;
472         gint row;
473         gboolean prev_default;
474         gchar *ac_name;
475
476         if (!clist->selection) return;
477
478         row = GPOINTER_TO_INT(clist->selection->data);
479         ac_prefs = gtk_clist_get_row_data(clist, row);
480         prev_default = ac_prefs->is_default;
481         Xstrdup_a(ac_name, ac_prefs->account_name, return);
482
483         prefs_account_open(ac_prefs);
484         inc_autocheck_timer_remove();
485
486         if (!prev_default && ac_prefs->is_default)
487                 account_set_as_default(ac_prefs);
488
489         if ((ac_prefs->protocol == A_IMAP4 || ac_prefs->protocol == A_NNTP) &&
490             ac_prefs->folder && strcmp(ac_name, ac_prefs->account_name) != 0) {
491                 folder_set_name(FOLDER(ac_prefs->folder),
492                                 ac_prefs->account_name);
493                 folderview_update_all();
494         }
495
496         account_clist_set();
497 }
498
499 static void account_delete(void)
500 {
501         GtkCList *clist = GTK_CLIST(edit_account.clist);
502         PrefsAccount *ac_prefs;
503         gint row;
504
505         if (!clist->selection) return;
506
507         if (alertpanel(_("Delete account"),
508                        _("Do you really want to delete this account?"),
509                        _("Yes"), _("+No"), NULL) != G_ALERTDEFAULT)
510                 return;
511
512         row = GPOINTER_TO_INT(clist->selection->data);
513         ac_prefs = gtk_clist_get_row_data(clist, row);
514         if (ac_prefs->folder) {
515                 folder_destroy(FOLDER(ac_prefs->folder));
516                 folderview_update_all();
517         }
518         account_destroy(ac_prefs);
519         account_clist_set();
520 }
521
522 static void account_up(void)
523 {
524         GtkCList *clist = GTK_CLIST(edit_account.clist);
525         gint row;
526
527         if (!clist->selection) return;
528
529         row = GPOINTER_TO_INT(clist->selection->data);
530         if (row > 0) {
531                 gtk_clist_row_move(clist, row, row - 1);
532                 account_list_set();
533         }
534 }
535
536 static void account_down(void)
537 {
538         GtkCList *clist = GTK_CLIST(edit_account.clist);
539         gint row;
540
541         if (!clist->selection) return;
542
543         row = GPOINTER_TO_INT(clist->selection->data);
544         if (row < clist->rows - 1) {
545                 gtk_clist_row_move(clist, row, row + 1);
546                 account_list_set();
547         }
548 }
549
550 static void account_set_default(void)
551 {
552         GtkCList *clist = GTK_CLIST(edit_account.clist);
553         gint row;
554         PrefsAccount *ac_prefs;
555
556         if (!clist->selection) return;
557
558         row = GPOINTER_TO_INT(clist->selection->data);
559         ac_prefs = gtk_clist_get_row_data(clist, row);
560         account_set_as_default(ac_prefs);
561         account_clist_set();
562
563         cur_account = ac_prefs;
564         account_set_menu();
565         main_window_reflect_prefs_all();
566 }
567
568 static void account_edit_close(void)
569 {
570         account_list_set();
571         account_save_config_all();
572
573         if (!cur_account && account_list) {
574                 PrefsAccount *ac_prefs = (PrefsAccount *)account_list->data;
575                 account_set_as_default(ac_prefs);
576                 cur_account = ac_prefs;
577         }
578
579         account_set_menu();
580         main_window_reflect_prefs_all();
581
582         gtk_widget_hide(edit_account.window);
583
584         inc_autocheck_timer_set();
585 }
586
587 static gint account_delete_event(GtkWidget *widget, GdkEventAny *event,
588                                  gpointer data)
589 {
590         account_edit_close();
591         return TRUE;
592 }
593
594 static void account_key_pressed(GtkWidget *widget, GdkEventKey *event,
595                                 gpointer data)
596 {
597         if (event && event->keyval == GDK_Escape)
598                 account_edit_close();
599 }
600
601 /* set one CList row or add new row */
602 static gint account_clist_set_row(PrefsAccount *ac_prefs, gint row)
603 {
604         GtkCList *clist = GTK_CLIST(edit_account.clist);
605         gchar *text[N_EDIT_ACCOUNT_COLS];
606
607         text[COL_DEFAULT] = ac_prefs->is_default ? "*" : "";
608         text[COL_NAME] = ac_prefs->account_name;
609         text[COL_PROTOCOL] = ac_prefs->protocol == A_POP3  ? "POP3"  :
610                              ac_prefs->protocol == A_APOP  ? "APOP"  :
611                              ac_prefs->protocol == A_IMAP4 ? "IMAP4" :
612                              ac_prefs->protocol == A_LOCAL ? "Local" :
613                              ac_prefs->protocol == A_NNTP  ? "NNTP"  :  "";
614         text[COL_SERVER] = ac_prefs->protocol == A_NNTP
615                 ? ac_prefs->nntp_server : ac_prefs->recv_server;
616
617         if (row < 0)
618                 row = gtk_clist_append(clist, text);
619         else {
620                 gtk_clist_set_text(clist, row, COL_DEFAULT, text[COL_DEFAULT]);
621                 gtk_clist_set_text(clist, row, COL_NAME, text[COL_NAME]);
622                 gtk_clist_set_text(clist, row, COL_PROTOCOL, text[COL_PROTOCOL]);
623                 gtk_clist_set_text(clist, row, COL_SERVER, text[COL_SERVER]);
624         }
625
626         gtk_clist_set_row_data(clist, row, ac_prefs);
627
628         return row;
629 }
630
631 /* set CList from account list */
632 static void account_clist_set(void)
633 {
634         GtkCList *clist = GTK_CLIST(edit_account.clist);
635         GList *cur;
636         gint prev_row;
637
638         if (clist->selection)
639                 prev_row = GPOINTER_TO_INT(clist->selection->data);
640         else
641                 prev_row = -1;
642
643         gtk_clist_freeze(clist);
644         gtk_clist_clear(clist);
645
646         for (cur = account_list; cur != NULL; cur = cur->next) {
647                 gint row;
648
649                 row = account_clist_set_row((PrefsAccount *)cur->data, -1);
650                 if ((PrefsAccount *)cur->data == cur_account)
651                         gtk_clist_select_row(clist, row, -1);
652         }
653
654         if (prev_row >= 0)
655                 gtk_clist_select_row(clist, prev_row, -1);
656
657         gtk_clist_thaw(clist);
658 }
659
660 /* set account list from CList */
661 static void account_list_set(void)
662 {
663         GtkCList *clist = GTK_CLIST(edit_account.clist);
664         gint row;
665         PrefsAccount *ac_prefs;
666
667         while (account_list)
668                 account_list = g_list_remove(account_list, account_list->data);
669
670         for (row = 0; (ac_prefs = gtk_clist_get_row_data(clist, row)) != NULL;
671              row++)
672                 account_list = g_list_append(account_list, ac_prefs);
673 }