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