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