2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 Hiroyuki Yamamoto
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.
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.
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.
27 #include <gtk/gtkmain.h>
28 #include <gtk/gtkrc.h>
31 # include <gdk_imlib.h>
41 #include <sys/types.h>
50 # include "passphrase.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"
62 #include "prefs_display_header.h"
67 #include "manage_window.h"
68 #include "alertpanel.h"
69 #include "statusbar.h"
70 #include "addressbook.h"
88 gboolean debug_mode = FALSE;
90 static gint lock_socket = -1;
91 static gint lock_socket_tag = 0;
97 const gchar *compose_mailto;
102 static void parse_cmd_opt(int argc, char *argv[]);
105 static void idle_function_for_gpgme(void);
106 #endif /* USE_GPGME */
108 static gint prohibit_duplicate_launch (void);
109 static void lock_socket_input_cb (gpointer data,
111 GdkInputCondition condition);
112 static gchar *get_socket_name (void);
114 static void open_compose_new_with_recipient (const gchar *address);
116 static void send_queue (void);
120 _("File `%s' already exists.\n"
121 "Can't create folder.")
124 #define MAKE_DIR_IF_NOT_EXIST(dir) \
126 if (!is_dir_exist(dir)) { \
127 if (is_file_exist(dir)) { \
129 (_("File `%s' already exists.\n" \
130 "Can't create folder."), \
134 if (make_dir(dir) < 0) \
139 int main(int argc, char *argv[])
143 FolderView *folderview;
145 setlocale(LC_ALL, "");
146 bindtextdomain(PACKAGE, LOCALEDIR);
149 parse_cmd_opt(argc, argv);
152 gtk_init(&argc, &argv);
154 #if USE_THREADS || USE_LDAP
156 if (!g_thread_supported())
157 g_error(_("g_thread is not supported by glib.\n"));
162 gtk_widget_push_visual(gdk_imlib_get_visual());
163 gtk_widget_push_colormap(gdk_imlib_get_colormap());
170 srandom((gint)time(NULL));
172 /* parse gtkrc files */
173 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
175 gtk_rc_parse(userrc);
177 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
178 G_DIR_SEPARATOR_S, "gtkrc", NULL);
179 gtk_rc_parse(userrc);
181 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
182 gtk_rc_parse(userrc);
185 gtk_rc_parse("./gtkrc");
187 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
188 gtk_item_factory_parse_rc(userrc);
191 prog_version = PROG_VERSION;
192 startup_dir = g_get_current_dir();
194 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
196 /* check and create unix domain socket */
197 lock_socket = prohibit_duplicate_launch();
198 if (lock_socket < 0) return 0;
201 puts("0 Sylpheed not running.");
205 /* backup if old rc file exists */
206 if (is_file_exist(RC_DIR)) {
207 if (rename(RC_DIR, RC_DIR ".bak") < 0)
208 FILE_OP_ERROR(RC_DIR, "rename");
210 MAKE_DIR_IF_NOT_EXIST(RC_DIR);
211 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
212 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
213 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
214 MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
216 if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log")) {
217 if (rename(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log",
218 RC_DIR G_DIR_SEPARATOR_S "sylpheed.log.bak") < 0)
219 FILE_OP_ERROR("sylpheed.log", "rename");
221 set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
223 if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "assortrc") &&
224 !is_file_exist(RC_DIR G_DIR_SEPARATOR_S "filterrc")) {
225 if (rename(RC_DIR G_DIR_SEPARATOR_S "assortrc",
226 RC_DIR G_DIR_SEPARATOR_S "filterrc") < 0)
227 FILE_OP_ERROR(RC_DIR G_DIR_SEPARATOR_S "assortrc",
232 prefs_common_read_config();
235 if (gpgme_check_engine()) { /* Also does some gpgme init */
236 rfc2015_disable_all();
237 debug_print("gpgme_engine_version:\n%s\n",
238 gpgme_get_engine_info());
240 if (prefs_common.gpg_warning) {
243 val = alertpanel_message_with_disable
245 _("GnuPG is not installed properly.\n"
246 "OpenPGP support disabled."));
247 if (val & G_ALERTDISABLE)
248 prefs_common.gpg_warning = FALSE;
251 gpgme_register_idle(idle_function_for_gpgme);
255 gtkpspellcheckers = gtkpspell_checkers_new();
259 prefs_common_save_config();
260 prefs_filter_read_config();
261 prefs_filter_write_config();
262 prefs_actions_read_config();
263 prefs_actions_write_config();
264 prefs_display_header_read_config();
265 prefs_display_header_write_config();
266 /* prefs_filtering_read_config(); */
267 addressbook_read_file();
268 renderer_read_config();
272 mainwin = main_window_create
273 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
274 folderview = mainwin->folderview;
276 /* register the callback of unix domain socket input */
277 lock_socket_tag = gdk_input_add(lock_socket,
278 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
279 lock_socket_input_cb,
282 account_read_config_all();
283 account_save_config_all();
285 if (folder_read_list() < 0) {
289 if (!account_get_list()) {
294 account_set_missing_folder();
295 folder_set_missing_folders();
296 folderview_set(folderview);
298 /* prefs_scoring_read_config(); */
299 prefs_matcher_read_config();
300 /* make one all-folder processing before using sylpheed */
301 processing_apply(mainwin->summaryview);
303 addressbook_read_file();
305 inc_autocheck_timer_init(mainwin);
307 /* ignore SIGPIPE signal for preventing sudden death of program */
308 signal(SIGPIPE, SIG_IGN);
310 if (cmd.receive_all || prefs_common.chk_on_startup)
311 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
312 else if (cmd.receive)
313 inc_mail(mainwin, prefs_common.newmail_notify_manu);
315 gtk_widget_grab_focus(folderview->ctree);
318 open_compose_new_with_recipient(cmd.compose_mailto);
324 addressbook_destroy();
327 gtkpspell_checkers_delete();
333 static void parse_cmd_opt(int argc, char *argv[])
337 for (i = 1; i < argc; i++) {
338 if (!strncmp(argv[i], "--debug", 7))
340 else if (!strncmp(argv[i], "--receive-all", 13))
341 cmd.receive_all = TRUE;
342 else if (!strncmp(argv[i], "--receive", 9))
344 else if (!strncmp(argv[i], "--compose", 9)) {
345 const gchar *p = argv[i + 1];
348 cmd.compose_mailto = NULL;
349 if (p && *p != '\0' && *p != '-') {
350 if (!strncmp(p, "mailto:", 7))
351 cmd.compose_mailto = p + 7;
353 cmd.compose_mailto = p;
356 } else if (!strncmp(argv[i], "--send", 6)) {
358 } else if (!strncmp(argv[i], "--version", 9)) {
359 puts("Sylpheed version " VERSION);
361 } else if (!strncmp(argv[i], "--status", 8)) {
363 } else if (!strncmp(argv[i], "--help", 6)) {
364 g_print(_("Usage: %s [OPTION]...\n"),
365 g_basename(argv[0]));
367 puts(_(" --compose [address] open composition window"));
368 puts(_(" --receive receive new messages"));
369 puts(_(" --receive-all receive new messages of all accounts"));
370 puts(_(" --send send all queued messages"));
371 puts(_(" --status show the total number of messages"));
372 puts(_(" --debug debug mode"));
373 puts(_(" --help display this help and exit"));
374 puts(_(" --version output version information and exit"));
381 static gint get_queued_message_num(void)
385 queue = folder_get_default_queue();
386 if (!queue) return -1;
388 folder_item_scan(queue);
392 void app_will_exit(GtkWidget *widget, gpointer data)
394 MainWindow *mainwin = data;
397 if (compose_get_compose_list()) {
398 if (alertpanel(_("Notice"),
399 _("Composing message exists. Really quit?"),
400 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
402 manage_window_focus_in(mainwin->window, NULL, NULL);
405 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
406 if (alertpanel(_("Queued messages"),
407 _("Some unsent messages are queued. Exit now?"),
408 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
410 manage_window_focus_in(mainwin->window, NULL, NULL);
413 inc_autocheck_timer_remove();
416 gpgmegtk_free_passphrase();
419 if (prefs_common.clean_on_exit)
420 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
422 /* save all state before exiting */
424 summary_write_cache(mainwin->summaryview);
426 main_window_get_size(mainwin);
427 main_window_get_position(mainwin);
428 prefs_common_save_config();
429 prefs_filter_write_config();
430 account_save_config_all();
431 addressbook_export_to_file();
433 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
434 gtk_item_factory_dump_rc(filename, NULL, TRUE);
437 /* delete temporary files */
438 remove_all_files(get_mime_tmp_dir());
442 /* delete unix domain socket */
443 gdk_input_remove(lock_socket_tag);
444 fd_close(lock_socket);
445 filename = get_socket_name();
456 static void idle_function_for_gpgme(void)
458 while (gtk_events_pending())
459 gtk_main_iteration();
461 #endif /* USE_GPGME */
463 static gchar *get_socket_name(void)
465 static gchar *filename = NULL;
467 if (filename == NULL) {
468 filename = g_strdup_printf("%s%csylpheed-%d",
469 g_get_tmp_dir(), G_DIR_SEPARATOR,
476 static gint prohibit_duplicate_launch(void)
481 path = get_socket_name();
482 uxsock = fd_connect_unix(path);
485 return fd_open_unix(path);
488 /* remote command mode */
490 debug_print(_("another Sylpheed is already running.\n"));
493 fd_write(uxsock, "receive_all\n", 12);
494 else if (cmd.receive)
495 fd_write(uxsock, "receive\n", 8);
496 else if (cmd.compose) {
499 if (cmd.compose_mailto)
500 compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
502 compose_str = g_strdup("compose\n");
504 fd_write(uxsock, compose_str, strlen(compose_str));
506 } else if (cmd.send) {
507 fd_write(uxsock, "send\n", 5);
508 } else if (cmd.status) {
511 fd_write(uxsock, "status\n", 7);
512 fd_gets(uxsock, buf, sizeof(buf));
515 fd_write(uxsock, "popup\n", 6);
521 static void lock_socket_input_cb(gpointer data,
523 GdkInputCondition condition)
525 MainWindow *mainwin = (MainWindow *)data;
529 sock = fd_accept(source);
530 fd_gets(sock, buf, sizeof(buf));
532 if (!strncmp(buf, "popup", 5)){
533 main_window_popup(mainwin);
534 } else if (!strncmp(buf, "receive_all", 11)){
535 main_window_popup(mainwin);
536 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
537 } else if (!strncmp(buf, "receive", 7)){
538 main_window_popup(mainwin);
539 inc_mail(mainwin, prefs_common.newmail_notify_manu);
540 } else if (!strncmp(buf, "compose", 7)) {
541 open_compose_new_with_recipient(buf + strlen("compose") + 1);
542 } else if (!strncmp(buf, "send", 4)) {
544 } else if (!strncmp(buf, "status", 6)) {
545 guint new, unread, total;
547 folder_count_total_msgs(&new, &unread, &total);
548 g_snprintf(buf, sizeof(buf), "%d %d %d\n", new, unread, total);
549 fd_write(sock, buf, strlen(buf));
555 static void open_compose_new_with_recipient(const gchar *address)
560 Xstrdup_a(addr, address, return);
564 if (addr && *addr != '\0')
565 compose_new_with_recipient(NULL, addr);
570 static void send_queue(void)
573 FolderItem *def_outbox;
575 def_outbox = folder_get_default_outbox();
577 for (list = folder_get_list(); list != NULL; list = list->next) {
578 Folder *folder = list->data;
581 if (procmsg_send_queue
582 (folder->queue, prefs_common.savemsg) < 0)
583 alertpanel_error(_("Some errors occurred while sending queued messages."));
585 folder_item_scan(folder->queue);
586 folderview_update_item(folder->queue, TRUE);
587 if (prefs_common.savemsg && folder->outbox) {
588 folderview_update_item(folder->outbox, TRUE);
589 if (folder->outbox == def_outbox)
595 if (prefs_common.savemsg && def_outbox)
596 folderview_update_item(def_outbox, TRUE);