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;
98 static void parse_cmd_opt(int argc, char *argv[]);
101 static void idle_function_for_gpgme(void);
102 #endif /* USE_GPGME */
104 static gint prohibit_duplicate_launch (void);
105 static void lock_socket_input_cb (gpointer data,
107 GdkInputCondition condition);
108 static gchar *get_socket_name (void);
110 static void open_compose_new_with_recipient (const gchar *address);
114 _("File `%s' already exists.\n"
115 "Can't create folder.")
118 #define MAKE_DIR_IF_NOT_EXIST(dir) \
120 if (!is_dir_exist(dir)) { \
121 if (is_file_exist(dir)) { \
123 (_("File `%s' already exists.\n" \
124 "Can't create folder."), \
128 if (mkdir(dir, S_IRWXU) < 0) { \
129 FILE_OP_ERROR(dir, "mkdir"); \
132 if (chmod(dir, S_IRWXU) < 0) \
133 FILE_OP_ERROR(dir, "chmod"); \
137 int main(int argc, char *argv[])
141 FolderView *folderview;
143 setlocale(LC_ALL, "");
144 bindtextdomain(PACKAGE, LOCALEDIR);
147 parse_cmd_opt(argc, argv);
150 gtk_init(&argc, &argv);
154 if (!g_thread_supported())
155 g_error(_("g_thread is not supported by glib.\n"));
160 gtk_widget_push_visual(gdk_imlib_get_visual());
161 gtk_widget_push_colormap(gdk_imlib_get_colormap());
168 srandom((gint)time(NULL));
170 /* parse gtkrc files */
171 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
173 gtk_rc_parse(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);
179 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
180 gtk_rc_parse(userrc);
183 gtk_rc_parse("./gtkrc");
185 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
186 gtk_item_factory_parse_rc(userrc);
189 prog_version = PROG_VERSION;
190 startup_dir = g_get_current_dir();
192 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
194 /* check and create unix domain socket */
195 lock_socket = prohibit_duplicate_launch();
196 if (lock_socket < 0) return 0;
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");
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());
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");
213 set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
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",
223 prefs_common_read_config();
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());
231 if (prefs_common.gpg_warning) {
234 val = alertpanel_message_with_disable
236 _("GnuPG is not installed properly.\n"
237 "OpenPGP support disabled."));
238 if (val & G_ALERTDISABLE)
239 prefs_common.gpg_warning = FALSE;
242 gpgme_register_idle(idle_function_for_gpgme);
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();
255 mainwin = main_window_create
256 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
257 folderview = mainwin->folderview;
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,
265 account_read_config_all();
266 account_save_config_all();
268 if (folder_read_list() < 0) {
272 if (!account_get_list()) {
277 account_set_missing_folder();
278 folderview_set(folderview);
280 prefs_scoring_read_config();
282 addressbook_read_file();
284 inc_autocheck_timer_init(mainwin);
286 if (cmd.receive_all || prefs_common.chk_on_startup)
287 inc_all_account_mail(mainwin);
288 else if (cmd.receive)
291 gtk_widget_grab_focus(folderview->ctree);
294 open_compose_new_with_recipient(cmd.compose_mailto);
296 /* ignore SIGPIPE signal for preventing sudden death of program */
297 signal(SIGPIPE, SIG_IGN);
302 gtkpspell_finished(gtkpspellconfig);
308 static void parse_cmd_opt(int argc, char *argv[])
312 for (i = 1; i < argc; i++) {
313 if (!strncmp(argv[i], "--debug", 7))
315 else if (!strncmp(argv[i], "--receive-all", 13))
316 cmd.receive_all = TRUE;
317 else if (!strncmp(argv[i], "--receive", 9))
319 else if (!strncmp(argv[i], "--compose", 9)) {
320 const gchar *p = argv[i + 1];
323 cmd.compose_mailto = NULL;
324 if (p && *p != '\0' && *p != '-') {
325 if (!strncmp(p, "mailto:", 7))
326 cmd.compose_mailto = p + 7;
328 cmd.compose_mailto = p;
331 } else if (!strncmp(argv[i], "--version", 9)) {
332 puts("Sylpheed version " VERSION);
334 } else if (!strncmp(argv[i], "--help", 6)) {
335 g_print(_("Usage: %s [OPTION]...\n"),
336 g_basename(argv[0]));
338 puts(_(" --compose [address] open composition window"));
339 puts(_(" --receive receive new messages"));
340 puts(_(" --receive-all receive new messages of all accounts"));
341 puts(_(" --debug debug mode"));
342 puts(_(" --help display this help and exit"));
343 puts(_(" --version output version information and exit"));
350 static gint get_queued_message_num(void)
354 queue = folder_get_default_queue();
355 g_return_val_if_fail(queue != NULL, -1);
357 folder_item_scan(queue);
361 void app_will_exit(GtkWidget *widget, gpointer data)
363 MainWindow *mainwin = data;
366 if (compose_get_compose_list()) {
367 if (alertpanel(_("Notice"),
368 _("Composing message exists. Really quit?"),
369 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
371 manage_window_focus_in(mainwin->window, NULL, NULL);
374 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
375 if (alertpanel(_("Queued messages"),
376 _("Some unsent messages are queued. Exit now?"),
377 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
379 manage_window_focus_in(mainwin->window, NULL, NULL);
382 inc_autocheck_timer_remove();
384 if (prefs_common.clean_on_exit)
385 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
387 /* save all state before exiting */
389 summary_write_cache(mainwin->summaryview);
391 main_window_get_size(mainwin);
392 main_window_get_position(mainwin);
393 prefs_common_save_config();
394 prefs_filter_write_config();
395 account_save_config_all();
396 addressbook_export_to_file();
398 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
399 gtk_item_factory_dump_rc(filename, NULL, TRUE);
402 /* delete temporary files */
403 remove_all_files(get_mime_tmp_dir());
407 /* delete unix domain socket */
408 gdk_input_remove(lock_socket_tag);
409 fd_close(lock_socket);
410 filename = get_socket_name();
421 static void idle_function_for_gpgme(void)
423 while (gtk_events_pending())
424 gtk_main_iteration();
426 #endif /* USE_GPGME */
428 static gchar *get_socket_name(void)
430 static gchar *filename = NULL;
432 if (filename == NULL) {
433 filename = g_strdup_printf("%s%csylpheed-%d",
434 g_get_tmp_dir(), G_DIR_SEPARATOR,
441 static gint prohibit_duplicate_launch(void)
446 path = get_socket_name();
447 uxsock = fd_connect_unix(path);
450 return fd_open_unix(path);
453 /* remote command mode */
455 debug_print(_("another Sylpheed is already running.\n"));
458 fd_write(uxsock, "receive_all\n", 12);
459 else if (cmd.receive)
460 fd_write(uxsock, "receive\n", 8);
461 else if (cmd.compose) {
464 if (cmd.compose_mailto)
465 compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
467 compose_str = g_strdup("compose\n");
469 fd_write(uxsock, compose_str, strlen(compose_str));
472 fd_write(uxsock, "popup\n", 6);
478 static void lock_socket_input_cb(gpointer data,
480 GdkInputCondition condition)
482 MainWindow *mainwin = (MainWindow *)data;
486 sock = fd_accept(source);
487 fd_gets(sock, buf, sizeof(buf));
490 if (!strncmp(buf, "popup", 5)){
491 main_window_popup(mainwin);
492 } else if (!strncmp(buf, "receive_all", 11)){
493 main_window_popup(mainwin);
494 inc_all_account_mail(mainwin);
495 } else if (!strncmp(buf, "receive", 7)){
496 main_window_popup(mainwin);
498 } else if (!strncmp(buf, "compose", 7)) {
499 open_compose_new_with_recipient(buf + strlen("compose") + 1);
503 static void open_compose_new_with_recipient(const gchar *address)
508 Xstrdup_a(addr, address, return);
512 if (addr && *addr != '\0')
513 compose_new_with_recipient(NULL, addr);