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