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