2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 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>
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"
61 #include "prefs_display_header.h"
66 #include "manage_window.h"
67 #include "alertpanel.h"
68 #include "addressbook.h"
86 gboolean debug_mode = FALSE;
88 static gint lock_socket = -1;
89 static gint lock_socket_tag = 0;
95 const gchar *compose_mailto;
100 static void parse_cmd_opt(int argc, char *argv[]);
103 static void idle_function_for_gpgme(void);
104 #endif /* USE_GPGME */
106 static gint prohibit_duplicate_launch (void);
107 static void lock_socket_input_cb (gpointer data,
109 GdkInputCondition condition);
110 static gchar *get_socket_name (void);
112 static void open_compose_new_with_recipient (const gchar *address);
116 _("File `%s' already exists.\n"
117 "Can't create folder.")
120 #define MAKE_DIR_IF_NOT_EXIST(dir) \
122 if (!is_dir_exist(dir)) { \
123 if (is_file_exist(dir)) { \
125 (_("File `%s' already exists.\n" \
126 "Can't create folder."), \
130 if (mkdir(dir, S_IRWXU) < 0) { \
131 FILE_OP_ERROR(dir, "mkdir"); \
134 if (chmod(dir, S_IRWXU) < 0) \
135 FILE_OP_ERROR(dir, "chmod"); \
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());
215 if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log")) {
216 if (rename(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log",
217 RC_DIR G_DIR_SEPARATOR_S "sylpheed.log.bak") < 0)
218 FILE_OP_ERROR("sylpheed.log", "rename");
220 set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
222 if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "assortrc") &&
223 !is_file_exist(RC_DIR G_DIR_SEPARATOR_S "filterrc")) {
224 if (rename(RC_DIR G_DIR_SEPARATOR_S "assortrc",
225 RC_DIR G_DIR_SEPARATOR_S "filterrc") < 0)
226 FILE_OP_ERROR(RC_DIR G_DIR_SEPARATOR_S "assortrc",
231 prefs_common_read_config();
234 if (gpgme_check_engine()) { /* Also does some gpgme init */
235 rfc2015_disable_all();
236 debug_print("gpgme_engine_version:\n%s\n",
237 gpgme_get_engine_info());
239 if (prefs_common.gpg_warning) {
242 val = alertpanel_message_with_disable
244 _("GnuPG is not installed properly.\n"
245 "OpenPGP support disabled."));
246 if (val & G_ALERTDISABLE)
247 prefs_common.gpg_warning = FALSE;
250 gpgme_register_idle(idle_function_for_gpgme);
254 gtkpspellcheckers = gtkpspell_checkers_new();
258 prefs_common_save_config();
259 prefs_filter_read_config();
260 prefs_filter_write_config();
261 prefs_display_header_read_config();
262 prefs_display_header_write_config();
263 /* prefs_filtering_read_config(); */
264 addressbook_read_file();
265 renderer_read_config();
269 mainwin = main_window_create
270 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
271 folderview = mainwin->folderview;
273 /* register the callback of unix domain socket input */
274 lock_socket_tag = gdk_input_add(lock_socket,
275 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
276 lock_socket_input_cb,
279 account_read_config_all();
280 account_save_config_all();
282 if (folder_read_list() < 0) {
286 if (!account_get_list()) {
291 account_set_missing_folder();
292 folderview_set(folderview);
294 /* prefs_scoring_read_config(); */
295 prefs_matcher_read_config();
296 /* make one all-folder processing before using sylpheed */
297 processing_apply(mainwin->summaryview);
299 addressbook_read_file();
301 inc_autocheck_timer_init(mainwin);
303 if (cmd.receive_all || prefs_common.chk_on_startup)
304 inc_all_account_mail(mainwin);
305 else if (cmd.receive)
308 gtk_widget_grab_focus(folderview->ctree);
311 open_compose_new_with_recipient(cmd.compose_mailto);
314 if (procmsg_send_queue() < 0)
315 alertpanel_error(_("Some errors occurred while sending queued messages."));
318 /* ignore SIGPIPE signal for preventing sudden death of program */
319 signal(SIGPIPE, SIG_IGN);
324 gtkpspell_checkers_delete();
330 static void parse_cmd_opt(int argc, char *argv[])
334 for (i = 1; i < argc; i++) {
335 if (!strncmp(argv[i], "--debug", 7))
337 else if (!strncmp(argv[i], "--receive-all", 13))
338 cmd.receive_all = TRUE;
339 else if (!strncmp(argv[i], "--receive", 9))
341 else if (!strncmp(argv[i], "--compose", 9)) {
342 const gchar *p = argv[i + 1];
345 cmd.compose_mailto = NULL;
346 if (p && *p != '\0' && *p != '-') {
347 if (!strncmp(p, "mailto:", 7))
348 cmd.compose_mailto = p + 7;
350 cmd.compose_mailto = p;
353 } else if (!strncmp(argv[i], "--version", 9)) {
354 puts("Sylpheed version " VERSION);
356 } else if (!strncmp(argv[i], "--status", 8)) {
358 } else if (!strncmp(argv[i], "--send", 6)) {
360 } else if (!strncmp(argv[i], "--help", 6)) {
361 g_print(_("Usage: %s [OPTION]...\n"),
362 g_basename(argv[0]));
364 puts(_(" --compose [address] open composition window"));
365 puts(_(" --receive receive new messages"));
366 puts(_(" --receive-all receive new messages of all accounts"));
367 puts(_(" --send send all queued messages"));
368 puts(_(" --status show the total number of messages"));
369 puts(_(" --debug debug mode"));
370 puts(_(" --help display this help and exit"));
371 puts(_(" --version output version information and exit"));
378 static gint get_queued_message_num(void)
382 queue = folder_get_default_queue();
383 if (!queue) return -1;
385 folder_item_scan(queue);
389 void app_will_exit(GtkWidget *widget, gpointer data)
391 MainWindow *mainwin = data;
394 if (compose_get_compose_list()) {
395 if (alertpanel(_("Notice"),
396 _("Composing message exists. Really quit?"),
397 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
399 manage_window_focus_in(mainwin->window, NULL, NULL);
402 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
403 if (alertpanel(_("Queued messages"),
404 _("Some unsent messages are queued. Exit now?"),
405 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
407 manage_window_focus_in(mainwin->window, NULL, NULL);
410 inc_autocheck_timer_remove();
412 if (prefs_common.clean_on_exit)
413 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
415 /* save all state before exiting */
417 summary_write_cache(mainwin->summaryview);
419 main_window_get_size(mainwin);
420 main_window_get_position(mainwin);
421 prefs_common_save_config();
422 prefs_filter_write_config();
423 account_save_config_all();
424 addressbook_export_to_file();
426 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
427 gtk_item_factory_dump_rc(filename, NULL, TRUE);
430 /* delete temporary files */
431 remove_all_files(get_mime_tmp_dir());
435 /* delete unix domain socket */
436 gdk_input_remove(lock_socket_tag);
437 fd_close(lock_socket);
438 filename = get_socket_name();
449 static void idle_function_for_gpgme(void)
451 while (gtk_events_pending())
452 gtk_main_iteration();
454 #endif /* USE_GPGME */
456 static gchar *get_socket_name(void)
458 static gchar *filename = NULL;
460 if (filename == NULL) {
461 filename = g_strdup_printf("%s%csylpheed-%d",
462 g_get_tmp_dir(), G_DIR_SEPARATOR,
469 static gint prohibit_duplicate_launch(void)
474 path = get_socket_name();
475 uxsock = fd_connect_unix(path);
478 return fd_open_unix(path);
481 /* remote command mode */
483 debug_print(_("another Sylpheed is already running.\n"));
486 fd_write(uxsock, "receive_all\n", 12);
487 else if (cmd.receive)
488 fd_write(uxsock, "receive\n", 8);
492 fd_write(uxsock, "send\n", 5);
493 fd_gets(uxsock, buf, sizeof(buf));
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.status) {
509 fd_write(uxsock, "status\n", 7);
510 fd_gets(uxsock, buf, sizeof(buf));
513 fd_write(uxsock, "popup\n", 6);
519 static void lock_socket_input_cb(gpointer data,
521 GdkInputCondition condition)
523 MainWindow *mainwin = (MainWindow *)data;
527 sock = fd_accept(source);
528 fd_gets(sock, buf, sizeof(buf));
530 if (!strncmp(buf, "popup", 5)){
531 main_window_popup(mainwin);
532 } else if (!strncmp(buf, "receive_all", 11)){
533 main_window_popup(mainwin);
534 inc_all_account_mail(mainwin);
535 } else if (!strncmp(buf, "receive", 7)){
536 main_window_popup(mainwin);
538 } else if (!strncmp(buf, "send", 4)) {
539 gint queued = get_queued_message_num();
542 if (procmsg_send_queue() < 0)
543 g_snprintf(buf, sizeof(buf),
544 "%s\n", /* avoids adding another translatable */
545 _("Some errors occurred while sending queued messages."));
546 else /* queue sent ok */
547 g_snprintf(buf, sizeof(buf), _("\%d queued message(s) sent\n"), queued);
550 g_snprintf(buf, sizeof(buf), "%s\n", _("No queued messages found."));
552 fd_write(sock, buf, strlen(buf));
553 } else if (!strncmp(buf, "compose", 7)) {
554 open_compose_new_with_recipient(buf + strlen("compose") + 1);
555 } else if (!strncmp(buf, "status", 6)) {
556 guint new, unread, total;
558 folder_count_total_msgs(&new, &unread, &total);
559 g_snprintf(buf, sizeof(buf), "%d %d %d\n", new, unread, total);
560 fd_write(sock, buf, strlen(buf));
566 static void open_compose_new_with_recipient(const gchar *address)
571 Xstrdup_a(addr, address, return);
575 if (addr && *addr != '\0')
576 compose_new_with_recipient(NULL, addr);