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