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