* src/folderview.c
[claws.git] / src / main.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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 #  include "passphrase.h"
51 #endif
52
53 #include "intl.h"
54 #include "main.h"
55 #include "mainwindow.h"
56 #include "folderview.h"
57 #include "summaryview.h"
58 #include "prefs_common.h"
59 #include "prefs_filter.h"
60 #include "prefs_account.h"
61 #include "prefs_actions.h"
62 #include "scoring.h"
63 #include "prefs_display_header.h"
64 #include "account.h"
65 #include "procmsg.h"
66 #include "inc.h"
67 #include "import.h"
68 #include "manage_window.h"
69 #include "alertpanel.h"
70 #include "statusbar.h"
71 #include "addressbook.h"
72 #include "compose.h"
73 #include "folder.h"
74 #include "setup.h"
75 #include "utils.h"
76 #include "gtkutils.h"
77
78 #if USE_GPGME
79 #  include "rfc2015.h"
80 #endif
81 #if USE_SSL
82 #  include "ssl.h"
83 #endif
84
85 #include "version.h"
86
87 #include "crash.h"
88
89 gchar *prog_version;
90 gchar *startup_dir;
91 gchar *argv0;
92 gboolean debug_mode = FALSE;
93
94 static gint lock_socket = -1;
95 static gint lock_socket_tag = 0;
96
97 static struct Cmd {
98         gboolean receive;
99         gboolean receive_all;
100         gboolean compose;
101         const gchar *compose_mailto;
102         GPtrArray *attach_files;
103         gboolean status;
104         gboolean send;
105         gboolean crash;
106         gchar   *crash_params;
107 } cmd;
108
109 static void parse_cmd_opt(int argc, char *argv[]);
110
111 #if USE_GPGME
112 static void idle_function_for_gpgme(void);
113 #endif /* USE_GPGME */
114
115 static gint prohibit_duplicate_launch   (void);
116 static void lock_socket_input_cb        (gpointer          data,
117                                          gint              source,
118                                          GdkInputCondition condition);
119 static gchar *get_socket_name           (void);
120
121 static void open_compose_new            (const gchar    *address,
122                                          GPtrArray      *attach_files);
123
124 static void send_queue                  (void);
125 static void initial_processing          (FolderItem *item, gpointer data);
126
127 #if 0
128 /* for gettext */
129 _("File `%s' already exists.\n"
130   "Can't create folder.")
131 #endif
132
133 #define MAKE_DIR_IF_NOT_EXIST(dir) \
134 { \
135         if (!is_dir_exist(dir)) { \
136                 if (is_file_exist(dir)) { \
137                         alertpanel_warning \
138                                 (_("File `%s' already exists.\n" \
139                                    "Can't create folder."), \
140                                  dir); \
141                         return 1; \
142                 } \
143                 if (make_dir(dir) < 0) \
144                         return 1; \
145         } \
146 }
147
148 int main(int argc, char *argv[])
149 {
150         gchar *userrc;
151         MainWindow *mainwin;
152         FolderView *folderview;
153
154         setlocale(LC_ALL, "");
155         bindtextdomain(PACKAGE, LOCALEDIR);
156         textdomain(PACKAGE);
157
158         prog_version = PROG_VERSION;
159         startup_dir = g_get_current_dir();
160         argv0 = g_strdup(argv[0]);
161
162         parse_cmd_opt(argc, argv);
163
164         gtk_set_locale();
165         gtk_init(&argc, &argv);
166
167         if (cmd.crash) {
168                 crash_main(cmd.crash_params);
169                 return 0;
170         }
171
172         crash_install_handlers();
173
174 #if USE_THREADS || USE_LDAP
175         g_thread_init(NULL);
176         if (!g_thread_supported())
177                 g_error(_("g_thread is not supported by glib.\n"));
178 #endif
179
180 #if HAVE_GDK_IMLIB
181         gdk_imlib_init();
182         gtk_widget_push_visual(gdk_imlib_get_visual());
183         gtk_widget_push_colormap(gdk_imlib_get_colormap());
184 #endif
185
186 #if USE_SSL
187         ssl_init();
188 #endif
189
190         srandom((gint)time(NULL));
191
192         /* parse gtkrc files */
193         userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
194                              NULL);
195         gtk_rc_parse(userrc);
196         g_free(userrc);
197         userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
198                              G_DIR_SEPARATOR_S, "gtkrc", NULL);
199         gtk_rc_parse(userrc);
200         g_free(userrc);
201         userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
202         gtk_rc_parse(userrc);
203         g_free(userrc);
204
205         gtk_rc_parse("./gtkrc");
206
207         userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
208         gtk_item_factory_parse_rc(userrc);
209         g_free(userrc);
210
211         CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
212
213         /* check and create unix domain socket */
214         lock_socket = prohibit_duplicate_launch();
215         if (lock_socket < 0) return 0;
216
217         if (cmd.status) {
218                 puts("0 Sylpheed not running.");
219                 return 0;
220         }
221
222         /* backup if old rc file exists */
223         if (is_file_exist(RC_DIR)) {
224                 if (rename(RC_DIR, RC_DIR ".bak") < 0)
225                         FILE_OP_ERROR(RC_DIR, "rename");
226         }
227         MAKE_DIR_IF_NOT_EXIST(RC_DIR);
228         MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
229         MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
230         MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
231         MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
232         MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
233
234         if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log")) {
235                 if (rename(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log",
236                            RC_DIR G_DIR_SEPARATOR_S "sylpheed.log.bak") < 0)
237                         FILE_OP_ERROR("sylpheed.log", "rename");
238         }
239         set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
240
241         if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "assortrc") &&
242             !is_file_exist(RC_DIR G_DIR_SEPARATOR_S "filterrc")) {
243                 if (rename(RC_DIR G_DIR_SEPARATOR_S "assortrc",
244                            RC_DIR G_DIR_SEPARATOR_S "filterrc") < 0)
245                         FILE_OP_ERROR(RC_DIR G_DIR_SEPARATOR_S "assortrc",
246                                       "rename");
247         }
248
249         prefs_common_init();
250         prefs_common_read_config();
251
252 #if USE_GPGME
253         if (gpgme_check_engine()) {  /* Also does some gpgme init */
254                 rfc2015_disable_all();
255                 debug_print("gpgme_engine_version:\n%s\n",
256                             gpgme_get_engine_info());
257
258                 if (prefs_common.gpg_warning) {
259                         AlertValue val;
260
261                         val = alertpanel_message_with_disable
262                                 (_("Warning"),
263                                  _("GnuPG is not installed properly.\n"
264                                    "OpenPGP support disabled."));
265                         if (val & G_ALERTDISABLE)
266                                 prefs_common.gpg_warning = FALSE;
267                 }
268         }
269         gpgme_register_idle(idle_function_for_gpgme);
270 #endif
271
272 #if USE_PSPELL
273         gtkpspellcheckers = gtkpspell_checkers_new();
274 #endif
275         
276
277         prefs_common_save_config();
278         prefs_filter_read_config();
279         prefs_filter_write_config();
280         prefs_actions_read_config();
281         prefs_actions_write_config();
282         prefs_display_header_read_config();
283         prefs_display_header_write_config();
284         /* prefs_filtering_read_config(); */
285         addressbook_read_file();
286         renderer_read_config();
287
288         gtkut_widget_init();
289
290         mainwin = main_window_create
291                 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
292         folderview = mainwin->folderview;
293
294         /* register the callback of unix domain socket input */
295         lock_socket_tag = gdk_input_add(lock_socket,
296                                         GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
297                                         lock_socket_input_cb,
298                                         mainwin);
299
300         account_read_config_all();
301         account_save_config_all();
302
303         if (folder_read_list() < 0) {
304                 setup(mainwin);
305                 folder_write_list();
306         }
307         if (!account_get_list()) {
308                 account_edit_open();
309                 account_add();
310         }
311
312         account_set_missing_folder();
313         folder_set_missing_folders();
314         folderview_set(folderview);
315
316         /* prefs_scoring_read_config(); */
317         prefs_matcher_read_config();
318
319         /* make one all-folder processing before using sylpheed */
320         folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
321
322         addressbook_read_file();
323
324         inc_autocheck_timer_init(mainwin);
325
326         /* ignore SIGPIPE signal for preventing sudden death of program */
327         signal(SIGPIPE, SIG_IGN);
328
329         if (cmd.receive_all || prefs_common.chk_on_startup)
330                 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
331         else if (cmd.receive)
332                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
333         else
334                 gtk_widget_grab_focus(folderview->ctree);
335
336         if (cmd.compose)
337                 open_compose_new(cmd.compose_mailto, cmd.attach_files);
338         if (cmd.attach_files) {
339                 ptr_array_free_strings(cmd.attach_files);
340                 g_ptr_array_free(cmd.attach_files, TRUE);
341                 cmd.attach_files = NULL;
342         }
343         if (cmd.send)
344                 send_queue();
345
346         gtk_main();
347
348         addressbook_destroy();
349
350 #if USE_PSPELL       
351         gtkpspell_checkers_delete();
352 #endif
353
354         return 0;
355 }
356
357 static void parse_cmd_opt(int argc, char *argv[])
358 {
359         gint i;
360
361         for (i = 1; i < argc; i++) {
362                 if (!strncmp(argv[i], "--debug", 7))
363                         debug_mode = TRUE;
364                 else if (!strncmp(argv[i], "--receive-all", 13))
365                         cmd.receive_all = TRUE;
366                 else if (!strncmp(argv[i], "--receive", 9))
367                         cmd.receive = TRUE;
368                 else if (!strncmp(argv[i], "--compose", 9)) {
369                         const gchar *p = argv[i + 1];
370
371                         cmd.compose = TRUE;
372                         cmd.compose_mailto = NULL;
373                         if (p && *p != '\0' && *p != '-') {
374                                 if (!strncmp(p, "mailto:", 7))
375                                         cmd.compose_mailto = p + 7;
376                                 else
377                                         cmd.compose_mailto = p;
378                                 i++;
379                         }
380                 } else if (!strncmp(argv[i], "--attach", 8)) {
381                         const gchar *p = argv[i + 1];
382                         gchar *file;
383
384                         while (p && *p != '\0' && *p != '-') {
385                                 if (!cmd.attach_files)
386                                         cmd.attach_files = g_ptr_array_new();
387                                 if (*p != G_DIR_SEPARATOR)
388                                         file = g_strconcat(startup_dir,
389                                                            G_DIR_SEPARATOR_S,
390                                                            p, NULL);
391                                 else
392                                         file = g_strdup(p);
393                                 g_ptr_array_add(cmd.attach_files, file);
394                                 i++;
395                                 p = argv[i + 1];
396                         }
397                 } else if (!strncmp(argv[i], "--send", 6)) {
398                         cmd.send = TRUE;
399                 } else if (!strncmp(argv[i], "--version", 9)) {
400                         puts("Sylpheed version " VERSION);
401                         exit(0);
402                 } else if (!strncmp(argv[i], "--status", 8)) {
403                         cmd.status = TRUE;
404                 } else if (!strncmp(argv[i], "--help", 6)) {
405                         g_print(_("Usage: %s [OPTION]...\n"),
406                                 g_basename(argv[0]));
407
408                         puts(_("  --compose [address]    open composition window"));
409                         puts(_("  --attach file1 [file2]...\n"
410                                "                         open composition window with specified files\n"
411                                "                         attached"));
412                         puts(_("  --receive              receive new messages"));
413                         puts(_("  --receive-all          receive new messages of all accounts"));
414                         puts(_("  --send                 send all queued messages"));
415                         puts(_("  --status               show the total number of messages"));
416                         puts(_("  --debug                debug mode"));
417                         puts(_("  --help                 display this help and exit"));
418                         puts(_("  --version              output version information and exit"));
419
420                         exit(1);
421                 } else if (!strncmp(argv[i], "--crash", 7)) {
422                         cmd.crash = TRUE;
423                         cmd.crash_params = g_strdup(argv[i + 1]);
424                         i++;
425                 }
426                 
427         }
428
429         if (cmd.attach_files && cmd.compose == FALSE) {
430                 cmd.compose = TRUE;
431                 cmd.compose_mailto = NULL;
432         }
433 }
434
435 static gint get_queued_message_num(void)
436 {
437         FolderItem *queue;
438
439         queue = folder_get_default_queue();
440         if (!queue) return -1;
441
442         folder_item_scan(queue);
443         return queue->total;
444 }
445
446 static void save_all_caches(FolderItem *item, gpointer data)
447 {
448         if(!item->cache)
449                 return;
450                 
451         folder_item_write_cache(item);
452 }
453
454 static void initial_processing(FolderItem *item, gpointer data)
455 {
456         MainWindow *mainwin = (MainWindow *)data;
457         gchar *buf;
458
459         g_return_if_fail(item);
460         buf = g_strdup_printf(_("Processing (%s)..."), 
461                               item->path 
462                               ? item->path 
463                               : _("top level folder"));
464         debug_print("%s\n", buf);
465         g_free(buf);
466
467         main_window_cursor_wait(mainwin);
468         
469         folder_item_apply_processing(item);
470
471         debug_print("done.\n");
472         STATUSBAR_POP(mainwin);
473         main_window_cursor_normal(mainwin);
474 }
475
476 void app_will_exit(GtkWidget *widget, gpointer data)
477 {
478         MainWindow *mainwin = data;
479         gchar *filename;
480
481         if (compose_get_compose_list()) {
482                 if (alertpanel(_("Notice"),
483                                _("Composing message exists. Really quit?"),
484                                _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
485                         return;
486                 manage_window_focus_in(mainwin->window, NULL, NULL);
487         }
488
489         if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
490                 if (alertpanel(_("Queued messages"),
491                                _("Some unsent messages are queued. Exit now?"),
492                                _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
493                         return;
494                 manage_window_focus_in(mainwin->window, NULL, NULL);
495         }
496
497         inc_autocheck_timer_remove();
498
499 #if USE_GPGME
500         gpgmegtk_free_passphrase();
501 #endif
502
503         if (prefs_common.clean_on_exit)
504                 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
505
506         /* save prefs for opened folder */
507         if(mainwin->folderview->opened)
508         {
509                 FolderItem *item;
510
511                 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
512                 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
513         }
514
515         /* save all state before exiting */
516         folder_write_list();
517         folder_func_to_all_folders(save_all_caches, NULL);
518
519         main_window_get_size(mainwin);
520         main_window_get_position(mainwin);
521         prefs_common_save_config();
522         prefs_filter_write_config();
523         account_save_config_all();
524         addressbook_export_to_file();
525
526         filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
527         gtk_item_factory_dump_rc(filename, NULL, TRUE);
528         g_free(filename);
529
530         /* delete temporary files */
531         remove_all_files(get_mime_tmp_dir());
532
533         close_log_file();
534
535         /* delete unix domain socket */
536         gdk_input_remove(lock_socket_tag);
537         fd_close(lock_socket);
538         filename = get_socket_name();
539         unlink(filename);
540
541 #if USE_SSL
542         ssl_done();
543 #endif
544
545         gtk_main_quit();
546 }
547
548 #if USE_GPGME
549 static void idle_function_for_gpgme(void)
550 {
551         while (gtk_events_pending())
552                 gtk_main_iteration();
553 }
554 #endif /* USE_GPGME */
555
556 static gchar *get_socket_name(void)
557 {
558         static gchar *filename = NULL;
559
560         if (filename == NULL) {
561                 filename = g_strdup_printf("%s%csylpheed-%d",
562                                            g_get_tmp_dir(), G_DIR_SEPARATOR,
563                                            getuid());
564         }
565
566         return filename;
567 }
568
569 static gint prohibit_duplicate_launch(void)
570 {
571         gint uxsock;
572         gchar *path;
573
574         path = get_socket_name();
575         uxsock = fd_connect_unix(path);
576         if (uxsock < 0) {
577                 unlink(path);
578                 return fd_open_unix(path);
579         }
580
581         /* remote command mode */
582
583         debug_print("another Sylpheed is already running.\n");
584
585         if (cmd.receive_all)
586                 fd_write(uxsock, "receive_all\n", 12);
587         else if (cmd.receive)
588                 fd_write(uxsock, "receive\n", 8);
589         else if (cmd.compose && cmd.attach_files) {
590                 gchar *str, *compose_str;
591                 gint i;
592
593                 if (cmd.compose_mailto)
594                         compose_str = g_strdup_printf("compose_attach %s\n",
595                                                       cmd.compose_mailto);
596                 else
597                         compose_str = g_strdup("compose_attach\n");
598
599                 fd_write(uxsock, compose_str, strlen(compose_str));
600                 g_free(compose_str);
601
602                 for (i = 0; i < cmd.attach_files->len; i++) {
603                         str = g_ptr_array_index(cmd.attach_files, i);
604                         fd_write(uxsock, str, strlen(str));
605                         fd_write(uxsock, "\n", 1);
606                 }
607
608                 fd_write(uxsock, ".\n", 2);
609         } else if (cmd.compose) {
610                 gchar *compose_str;
611
612                 if (cmd.compose_mailto)
613                         compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
614                 else
615                         compose_str = g_strdup("compose\n");
616
617                 fd_write(uxsock, compose_str, strlen(compose_str));
618                 g_free(compose_str);
619         } else if (cmd.send) {
620                 fd_write(uxsock, "send\n", 5);
621         } else if (cmd.status) {
622                 gchar buf[BUFFSIZE];
623
624                 fd_write(uxsock, "status\n", 7);
625                 fd_gets(uxsock, buf, sizeof(buf));
626                 fputs(buf, stdout);
627         } else
628                 fd_write(uxsock, "popup\n", 6);
629
630         fd_close(uxsock);
631         return -1;
632 }
633
634 static void lock_socket_input_cb(gpointer data,
635                                  gint source,
636                                  GdkInputCondition condition)
637 {
638         MainWindow *mainwin = (MainWindow *)data;
639         gint sock;
640         gchar buf[BUFFSIZE];
641
642         sock = fd_accept(source);
643         fd_gets(sock, buf, sizeof(buf));
644
645         if (!strncmp(buf, "popup", 5)) {
646                 main_window_popup(mainwin);
647         } else if (!strncmp(buf, "receive_all", 11)) {
648                 main_window_popup(mainwin);
649                 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
650         } else if (!strncmp(buf, "receive", 7)) {
651                 main_window_popup(mainwin);
652                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
653         } else if (!strncmp(buf, "compose_attach", 14)) {
654                 GPtrArray *files;
655                 gchar *mailto;
656
657                 mailto = g_strdup(buf + strlen("compose_attach") + 1);
658                 files = g_ptr_array_new();
659                 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
660                         if (buf[0] == '.' && buf[1] == '\n') break;
661                         strretchomp(buf);
662                         g_ptr_array_add(files, g_strdup(buf));
663                 }
664                 open_compose_new(mailto, files);
665                 ptr_array_free_strings(files);
666                 g_ptr_array_free(files, TRUE);
667                 g_free(mailto);
668         } else if (!strncmp(buf, "compose", 7)) {
669                 open_compose_new(buf + strlen("compose") + 1, NULL);
670         } else if (!strncmp(buf, "send", 4)) {
671                 send_queue();
672         } else if (!strncmp(buf, "status", 6)) {
673                 guint new, unread, total;
674
675                 folder_count_total_msgs(&new, &unread, &total);
676                 g_snprintf(buf, sizeof(buf), "%d %d %d\n", new, unread, total);
677                 fd_write(sock, buf, strlen(buf));
678         }
679
680         fd_close(sock);
681 }
682
683 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
684 {
685         gchar *addr = NULL;
686
687         if (address) {
688                 Xstrdup_a(addr, address, return);
689                 g_strstrip(addr);
690         }
691
692         compose_new(NULL, addr, attach_files);
693 }
694
695 static void send_queue(void)
696 {
697         GList *list;
698         FolderItem *def_outbox;
699
700         def_outbox = folder_get_default_outbox();
701
702         for (list = folder_get_list(); list != NULL; list = list->next) {
703                 Folder *folder = list->data;
704
705                 if (folder->queue) {
706                         if (procmsg_send_queue
707                                 (folder->queue, prefs_common.savemsg) < 0)
708                                 alertpanel_error(_("Some errors occurred while sending queued messages."));
709                         statusbar_pop_all();
710                         folder_item_scan(folder->queue);
711                         folderview_update_item(folder->queue, TRUE);
712                         if (prefs_common.savemsg && folder->outbox) {
713                                 folderview_update_item(folder->outbox, TRUE);
714                                 if (folder->outbox == def_outbox)
715                                         def_outbox = NULL;
716                         }
717                 }
718         }
719
720         if (prefs_common.savemsg && def_outbox)
721                 folderview_update_item(def_outbox, TRUE);
722 }