sync account.c with sylpheed 0.5.3 plus button
[claws.git] / src / main.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 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/gtkmain.h>
28 #include <gtk/gtkrc.h>
29
30 #if HAVE_GDK_IMLIB
31 #  include <gdk_imlib.h>
32 #endif
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <unistd.h>
39 #include <time.h>
40 #include <sys/stat.h>
41 #include <sys/types.h>
42 #include <signal.h>
43
44 #if HAVE_LOCALE_H
45 #  include <locale.h>
46 #endif
47
48 #if USE_GPGME
49 #  include <gpgme.h>
50 #endif
51
52 #if USE_SSL
53 #  include "ssl.h"
54 #endif
55
56 #include "intl.h"
57 #include "main.h"
58 #include "mainwindow.h"
59 #include "folderview.h"
60 #include "summaryview.h"
61 #include "prefs_common.h"
62 #include "prefs_filter.h"
63 #include "prefs_account.h"
64 #include "scoring.h"
65 #include "prefs_display_header.h"
66 #include "account.h"
67 #include "procmsg.h"
68 #include "inc.h"
69 #include "import.h"
70 #include "manage_window.h"
71 #include "alertpanel.h"
72 #include "addressbook.h"
73 #include "compose.h"
74 #include "folder.h"
75 #include "setup.h"
76 #include "utils.h"
77 #include "gtkutils.h"
78
79 #if USE_GPGME
80 #  include "rfc2015.h"
81 #endif
82
83 gchar *prog_version;
84 gchar *startup_dir;
85 gboolean debug_mode = FALSE;
86
87 static gint lock_socket = -1;
88 static gint lock_socket_tag = 0;
89
90 static struct Cmd {
91         gboolean receive;
92         gboolean receive_all;
93         gboolean compose;
94         const gchar *compose_mailto;
95 } cmd;
96
97 static void parse_cmd_opt(int argc, char *argv[]);
98
99 #if USE_GPGME
100 static void idle_function_for_gpgme(void);
101 #endif /* USE_GPGME */
102
103 static gint prohibit_duplicate_launch   (void);
104 static void lock_socket_input_cb        (gpointer          data,
105                                          gint              source,
106                                          GdkInputCondition condition);
107 static gchar *get_socket_name           (void);
108
109 static void open_compose_new_with_recipient     (const gchar    *address);
110
111 #if 0
112 /* for gettext */
113 _("File `%s' already exists.\n"
114   "Can't create folder.")
115 #endif
116
117 #define MAKE_DIR_IF_NOT_EXIST(dir) \
118 { \
119         if (!is_dir_exist(dir)) { \
120                 if (is_file_exist(dir)) { \
121                         alertpanel_warning \
122                                 (_("File `%s' already exists.\n" \
123                                    "Can't create folder."), \
124                                  dir); \
125                         return 1; \
126                 } \
127                 if (mkdir(dir, S_IRWXU) < 0) { \
128                         FILE_OP_ERROR(dir, "mkdir"); \
129                         return 1; \
130                 } \
131                 if (chmod(dir, S_IRWXU) < 0) \
132                         FILE_OP_ERROR(dir, "chmod"); \
133         } \
134 }
135
136 int main(int argc, char *argv[])
137 {
138         gchar *userrc;
139         MainWindow *mainwin;
140         FolderView *folderview;
141
142         setlocale(LC_ALL, "");
143         bindtextdomain(PACKAGE, LOCALEDIR);
144         textdomain(PACKAGE);
145
146         parse_cmd_opt(argc, argv);
147
148         gtk_set_locale();
149         gtk_init(&argc, &argv);
150
151 #if USE_THREADS
152         g_thread_init(NULL);
153         if (!g_thread_supported())
154                 g_error(_("g_thread is not supported by glib.\n"));
155 #endif
156
157 #if HAVE_GDK_IMLIB
158         gdk_imlib_init();
159         gtk_widget_push_visual(gdk_imlib_get_visual());
160         gtk_widget_push_colormap(gdk_imlib_get_colormap());
161 #endif
162
163         srandom((gint)time(NULL));
164
165         /* parse gtkrc files */
166         userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
167                              NULL);
168         gtk_rc_parse(userrc);
169         g_free(userrc);
170         userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
171                              G_DIR_SEPARATOR_S, "gtkrc", NULL);
172         gtk_rc_parse(userrc);
173         g_free(userrc);
174         userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
175         gtk_rc_parse(userrc);
176         g_free(userrc);
177
178         gtk_rc_parse("./gtkrc");
179
180         userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
181         gtk_item_factory_parse_rc(userrc);
182         g_free(userrc);
183
184         prog_version = PROG_VERSION;
185         startup_dir = g_get_current_dir();
186
187         CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
188
189         /* check and create unix domain socket */
190         lock_socket = prohibit_duplicate_launch();
191         if (lock_socket < 0) return 0;
192     
193         /* backup if old rc file exists */
194         if (is_file_exist(RC_DIR)) {
195                 if (rename(RC_DIR, RC_DIR ".bak") < 0)
196                         FILE_OP_ERROR(RC_DIR, "rename");
197         }
198         MAKE_DIR_IF_NOT_EXIST(RC_DIR);
199         MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
200         MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
201         MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
202
203         if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log")) {
204                 if (rename(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log",
205                            RC_DIR G_DIR_SEPARATOR_S "sylpheed.log.bak") < 0)
206                         FILE_OP_ERROR("sylpheed.log", "rename");
207         }
208         set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
209
210         if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "assortrc") &&
211             !is_file_exist(RC_DIR G_DIR_SEPARATOR_S "filterrc")) {
212                 if (rename(RC_DIR G_DIR_SEPARATOR_S "assortrc",
213                            RC_DIR G_DIR_SEPARATOR_S "filterrc") < 0)
214                         FILE_OP_ERROR(RC_DIR G_DIR_SEPARATOR_S "assortrc",
215                                       "rename");
216         }
217
218         prefs_common_read_config();
219
220 #if USE_GPGME
221         if (gpgme_check_engine()) {  /* Also does some gpgme init */
222                 rfc2015_disable_all();
223                 debug_print("gpgme_engine_version:\n%s\n",
224                             gpgme_get_engine_info());
225
226                 if (prefs_common.gpg_warning) {
227                         AlertValue val;
228
229                         val = alertpanel_message_with_disable
230                                 (_("Warning"),
231                                  _("GnuPG is not installed properly.\n"
232                                    "OpenPGP support disabled."));
233                         if (val & G_ALERTDISABLE)
234                                 prefs_common.gpg_warning = FALSE;
235                 }
236         }
237         gpgme_register_idle(idle_function_for_gpgme);
238 #endif
239
240 #if USE_SSL
241         ssl_init();
242 #endif
243
244         prefs_common_save_config();
245         prefs_filter_read_config();
246         prefs_filter_write_config();
247         prefs_display_header_read_config();
248         prefs_display_header_write_config();
249         prefs_filtering_read_config();
250
251         gtkut_widget_init();
252
253         mainwin = main_window_create
254                 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
255         folderview = mainwin->folderview;
256
257         /* register the callback of unix domain socket input */
258         lock_socket_tag = gdk_input_add(lock_socket,
259                                         GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
260                                         lock_socket_input_cb,
261                                         mainwin);
262
263         account_read_config_all();
264         account_save_config_all();
265
266         if (folder_read_list() < 0) {
267                 setup(mainwin);
268                 folder_write_list();
269         }
270         account_set_missing_folder();
271         folderview_set(folderview);
272
273         prefs_scoring_read_config();
274
275         inc_autocheck_timer_init(mainwin);
276
277         if (cmd.receive_all || prefs_common.chk_on_startup)
278                 inc_all_account_mail(mainwin);
279         else if (cmd.receive)
280                 inc_mail(mainwin);
281         else
282                 gtk_widget_grab_focus(folderview->ctree);
283
284         if (cmd.compose)
285                 open_compose_new_with_recipient(cmd.compose_mailto);
286
287         /* ignore SIGPIPE signal for preventing sudden death of program */
288         signal(SIGPIPE, SIG_IGN);
289
290         gtk_main();
291
292 #if USE_SSL
293         ssl_done();
294 #endif
295
296         return 0;
297 }
298
299 static void parse_cmd_opt(int argc, char *argv[])
300 {
301         gint i;
302
303         for (i = 1; i < argc; i++) {
304                 if (!strncmp(argv[i], "--debug", 7))
305                         debug_mode = TRUE;
306                 else if (!strncmp(argv[i], "--receive-all", 13))
307                         cmd.receive_all = TRUE;
308                 else if (!strncmp(argv[i], "--receive", 9))
309                         cmd.receive = TRUE;
310                 else if (!strncmp(argv[i], "--compose", 9)) {
311                         const gchar *p = argv[i + 1];
312
313                         cmd.compose = TRUE;
314                         cmd.compose_mailto = NULL;
315                         if (p && *p != '\0' && *p != '-') {
316                                 if (!strncmp(p, "mailto:", 7))
317                                         cmd.compose_mailto = p + 7;
318                                 else
319                                         cmd.compose_mailto = p;
320                                 i++;
321                         }
322                 } else if (!strncmp(argv[i], "--version", 9)) {
323                         puts("Sylpheed version " VERSION);
324                         exit(0);
325                 } else if (!strncmp(argv[i], "--help", 6)) {
326                         g_print(_("Usage: %s [OPTION]...\n"),
327                                 g_basename(argv[0]));
328
329                         puts(_("  --compose [address]    open composition window"));
330                         puts(_("  --receive              receive new messages"));
331                         puts(_("  --receive-all          receive new messages of all accounts"));
332                         puts(_("  --debug                debug mode"));
333                         puts(_("  --help                 display this help and exit"));
334                         puts(_("  --version              output version information and exit"));
335
336                         exit(1);
337                 }
338         }
339 }
340
341 static gint get_queued_message_num(void)
342 {
343         FolderItem *queue;
344
345         queue = folder_get_default_queue();
346         g_return_val_if_fail(queue != NULL, -1);
347
348         folder_item_scan(queue);
349         return queue->total;
350 }
351
352 void app_will_exit(GtkWidget *widget, gpointer data)
353 {
354         MainWindow *mainwin = data;
355         gchar *filename;
356
357         if (compose_get_compose_list()) {
358                 if (alertpanel(_("Notice"),
359                                _("Composing message exists. Really quit?"),
360                                _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
361                         return;
362                 manage_window_focus_in(mainwin->window, NULL, NULL);
363         }
364
365         if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
366                 if (alertpanel(_("Queued messages"),
367                                _("Some unsent messages are queued. Exit now?"),
368                                _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
369                         return;
370                 manage_window_focus_in(mainwin->window, NULL, NULL);
371         }
372
373         if (prefs_common.clean_on_exit)
374                 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
375
376         /* save all state before exiting */
377         folder_write_list();
378         summary_write_cache(mainwin->summaryview);
379
380         main_window_get_size(mainwin);
381         main_window_get_position(mainwin);
382         prefs_common_save_config();
383         prefs_filter_write_config();
384         account_save_config_all();
385         addressbook_export_to_file();
386
387         filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
388         gtk_item_factory_dump_rc(filename, NULL, TRUE);
389         g_free(filename);
390
391         /* delete temporary files */
392         remove_all_files(get_mime_tmp_dir());
393
394         close_log_file();
395
396         /* delete unix domain socket */
397         gdk_input_remove(lock_socket_tag);
398         fd_close(lock_socket);
399         filename = get_socket_name();
400         unlink(filename);
401
402         gtk_main_quit();
403 }
404
405 #if USE_GPGME
406 static void idle_function_for_gpgme(void)
407 {
408         while (gtk_events_pending())
409                 gtk_main_iteration();
410 }
411 #endif /* USE_GPGME */
412
413 static gchar *get_socket_name(void)
414 {
415         static gchar *filename = NULL;
416
417         if (filename == NULL) {
418                 filename = g_strdup_printf("%s%csylpheed-%d",
419                                            g_get_tmp_dir(), G_DIR_SEPARATOR,
420                                            getuid());
421         }
422
423         return filename;
424 }
425
426 static gint prohibit_duplicate_launch(void)
427 {
428         gint uxsock;
429         gchar *path;
430
431         path = get_socket_name();
432         uxsock = fd_connect_unix(path);
433         if (uxsock < 0) {
434                 unlink(path);
435                 return fd_open_unix(path);
436         }
437
438         /* remote command mode */
439
440         debug_print(_("another Sylpheed is already running.\n"));
441
442         if (cmd.receive_all)
443                 fd_write(uxsock, "receive_all\n", 12);
444         else if (cmd.receive)
445                 fd_write(uxsock, "receive\n", 8);
446         else if (cmd.compose) {
447                 gchar *compose_str;
448
449                 if (cmd.compose_mailto)
450                         compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
451                 else
452                         compose_str = g_strdup("compose\n");
453
454                 fd_write(uxsock, compose_str, strlen(compose_str));
455                 g_free(compose_str);
456         } else
457                 fd_write(uxsock, "popup\n", 6);
458
459         fd_close(uxsock);
460         return -1;
461 }
462
463 static void lock_socket_input_cb(gpointer data,
464                                  gint source,
465                                  GdkInputCondition condition)
466 {
467         MainWindow *mainwin = (MainWindow *)data;
468         gint sock;
469         gchar buf[BUFFSIZE];
470
471         sock = fd_accept(source);
472         fd_gets(sock, buf, sizeof(buf));
473         fd_close(sock);
474
475         if (!strncmp(buf, "popup", 5)){
476                 main_window_popup(mainwin);
477         } else if (!strncmp(buf, "receive_all", 11)){
478                 main_window_popup(mainwin);
479                 inc_all_account_mail(mainwin);
480         } else if (!strncmp(buf, "receive", 7)){
481                 main_window_popup(mainwin);
482                 inc_mail(mainwin);
483         } else if (!strncmp(buf, "compose", 7)) {
484                 open_compose_new_with_recipient(buf + strlen("compose") + 1);
485         }
486 }
487
488 static void open_compose_new_with_recipient(const gchar *address)
489 {
490         gchar *addr = NULL;
491
492         if (address) {
493                 Xstrdup_a(addr, address, return);
494                 g_strstrip(addr);
495         }
496
497         if (addr && *addr != '\0')
498                 compose_new_with_recipient(NULL, addr);
499         else
500                 compose_new(NULL);
501 }