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