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