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