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"
77 #endif /* USE_GPGME */
81 gboolean debug_mode = FALSE;
83 static gint lock_socket = -1;
84 static gint lock_socket_tag = 0;
90 const gchar *compose_mailto;
93 static void parse_cmd_opt(int argc, char *argv[]);
96 static void idle_function_for_gpgme(void);
97 #endif /* USE_GPGME */
99 static gint prohibit_duplicate_launch (void);
100 static void lock_socket_input_cb (gpointer data,
102 GdkInputCondition condition);
103 static gchar *get_socket_name (void);
105 static void open_compose_new_with_recipient (const gchar *address);
109 _("File `%s' already exists.\n"
110 "Can't create folder.")
113 #define MAKE_DIR_IF_NOT_EXIST(dir) \
115 if (!is_dir_exist(dir)) { \
116 if (is_file_exist(dir)) { \
118 (_("File `%s' already exists.\n" \
119 "Can't create folder."), \
123 if (mkdir(dir, S_IRWXU) < 0) { \
124 FILE_OP_ERROR(dir, "mkdir"); \
127 if (chmod(dir, S_IRWXU) < 0) \
128 FILE_OP_ERROR(dir, "chmod"); \
132 int main(int argc, char *argv[])
136 FolderView *folderview;
138 setlocale(LC_ALL, "");
139 bindtextdomain(PACKAGE, LOCALEDIR);
143 gtk_init(&argc, &argv);
147 if (!g_thread_supported())
148 g_error(_("g_thread is not supported by glib.\n"));
153 gtk_widget_push_visual(gdk_imlib_get_visual());
154 gtk_widget_push_colormap(gdk_imlib_get_colormap());
157 /* parse gtkrc files */
158 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
160 gtk_rc_parse(userrc);
162 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
163 G_DIR_SEPARATOR_S, "gtkrc", NULL);
164 gtk_rc_parse(userrc);
166 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
167 gtk_rc_parse(userrc);
170 gtk_rc_parse("./gtkrc");
172 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
173 gtk_item_factory_parse_rc(userrc);
176 prog_version = PROG_VERSION;
177 startup_dir = g_get_current_dir();
179 parse_cmd_opt(argc, argv);
181 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
183 /* check and create unix domain socket */
184 lock_socket = prohibit_duplicate_launch();
185 if (lock_socket < 0) return 0;
187 /* backup if old rc file exists */
188 if (is_file_exist(RC_DIR)) {
189 if (rename(RC_DIR, RC_DIR ".bak") < 0)
190 FILE_OP_ERROR(RC_DIR, "rename");
192 MAKE_DIR_IF_NOT_EXIST(RC_DIR);
193 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
194 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
195 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
197 if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "assortrc") &&
198 !is_file_exist(RC_DIR G_DIR_SEPARATOR_S "filterrc")) {
199 if (rename(RC_DIR G_DIR_SEPARATOR_S "assortrc",
200 RC_DIR G_DIR_SEPARATOR_S "filterrc") < 0)
201 FILE_OP_ERROR(RC_DIR G_DIR_SEPARATOR_S "assortrc",
205 srandom((gint)time(NULL));
208 if (gpgme_check_engine()) { /* Also does some gpgme init */
209 rfc2015_disable_all();
210 debug_print("gpgme_engine_version:\n%s\n",
211 gpgme_get_engine_info());
213 if (prefs_common.gpgme_warning) {
214 AlertValue v = alertpanel_message_with_disable
216 _("GnuPG is not installed properly.\n"
217 "OpenPGP support disabled."));
218 if (v & G_ALERTDISABLE) {
219 prefs_common.gpgme_warning = FALSE;
220 prefs_common_save_config();
224 gpgme_register_idle(idle_function_for_gpgme);
227 prefs_common_read_config();
228 prefs_common_save_config();
229 prefs_filter_read_config();
230 prefs_filter_write_config();
231 prefs_display_header_read_config();
232 prefs_display_header_write_config();
233 prefs_scoring_read_config();
234 prefs_filtering_read_config();
238 mainwin = main_window_create
239 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
240 folderview = mainwin->folderview;
242 /* register the callback of unix domain socket input */
243 lock_socket_tag = gdk_input_add(lock_socket,
244 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
245 lock_socket_input_cb,
248 account_read_config_all();
249 account_save_config_all();
251 if (folder_read_list() < 0) setup(mainwin);
252 account_set_missing_folder();
253 folderview_set(folderview);
255 inc_autocheck_timer_init(mainwin);
257 if (cmd.receive_all || prefs_common.chk_on_startup)
258 inc_all_account_mail(mainwin);
259 else if (cmd.receive)
262 gtk_widget_grab_focus(folderview->ctree);
265 open_compose_new_with_recipient(cmd.compose_mailto);
267 /* ignore SIGPIPE signal for preventing sudden death of program */
268 signal(SIGPIPE, SIG_IGN);
275 static void parse_cmd_opt(int argc, char *argv[])
279 for (i = 1; i < argc; i++) {
280 if (!strncmp(argv[i], "--debug", 7))
282 else if (!strncmp(argv[i], "--receive-all", 13))
283 cmd.receive_all = TRUE;
284 else if (!strncmp(argv[i], "--receive", 9))
286 else if (!strncmp(argv[i], "--compose", 9)) {
287 const gchar *p = argv[i + 1];
290 cmd.compose_mailto = NULL;
291 if (p && *p != '\0' && *p != '-') {
292 if (!strncmp(p, "mailto:", 7))
293 cmd.compose_mailto = p + 7;
295 cmd.compose_mailto = p;
298 } else if (!strncmp(argv[i], "--version", 9)) {
299 puts("Sylpheed version " VERSION);
301 } else if (!strncmp(argv[i], "--help", 6)) {
302 g_print(_("Usage: %s [OPTION]...\n"), g_get_prgname());
304 puts(_(" --compose [address] open composition window"));
305 puts(_(" --receive receive new messages"));
306 puts(_(" --receive-all receive new messages of all accounts"));
307 puts(_(" --debug debug mode"));
308 puts(_(" --help display this help and exit"));
309 puts(_(" --version output version information and exit"));
316 static gint get_queued_message_num(void)
320 queue = folder_get_default_queue();
321 g_return_val_if_fail(queue != NULL, -1);
323 folder_item_scan(queue);
327 void app_will_exit(GtkWidget *widget, gpointer data)
329 MainWindow *mainwin = data;
332 if (compose_get_compose_list()) {
333 if (alertpanel(_("Notice"),
334 _("Composing message exists. Really quit?"),
335 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
337 manage_window_focus_in(mainwin->window, NULL, NULL);
340 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
341 if (alertpanel(_("Queued messages"),
342 _("Some unsent messages are queued. Exit now?"),
343 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
345 manage_window_focus_in(mainwin->window, NULL, NULL);
348 if (prefs_common.clean_on_exit)
349 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
351 /* save all state before exiting */
353 summary_write_cache(mainwin->summaryview);
355 main_window_get_size(mainwin);
356 main_window_get_position(mainwin);
357 prefs_common_save_config();
358 prefs_filter_write_config();
359 account_save_config_all();
360 addressbook_export_to_file();
362 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
363 gtk_item_factory_dump_rc(filename, NULL, TRUE);
366 /* delete temporary files */
367 remove_all_files(get_mime_tmp_dir());
369 /* delete unix domain socket */
370 gdk_input_remove(lock_socket_tag);
371 fd_close(lock_socket);
372 filename = get_socket_name();
379 static void idle_function_for_gpgme(void)
381 while (gtk_events_pending())
382 gtk_main_iteration();
384 #endif /* USE_GPGME */
386 static gchar *get_socket_name(void)
388 static gchar *filename = NULL;
390 if (filename == NULL) {
391 filename = g_strdup_printf("%s%csylpheed-%d",
392 g_get_tmp_dir(), G_DIR_SEPARATOR,
399 static gint prohibit_duplicate_launch(void)
404 path = get_socket_name();
405 uxsock = fd_connect_unix(path);
408 return fd_open_unix(path);
411 /* remote command mode */
413 debug_print(_("another Sylpheed is already running.\n"));
416 fd_write(uxsock, "receive_all\n", 12);
417 else if (cmd.receive)
418 fd_write(uxsock, "receive\n", 8);
419 else if (cmd.compose) {
422 if (cmd.compose_mailto)
423 compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
425 compose_str = g_strdup("compose\n");
427 fd_write(uxsock, compose_str, strlen(compose_str));
430 fd_write(uxsock, "popup\n", 6);
436 static void lock_socket_input_cb(gpointer data,
438 GdkInputCondition condition)
440 MainWindow *mainwin = (MainWindow *)data;
444 sock = fd_accept(source);
445 fd_gets(sock, buf, sizeof(buf));
448 if (!strncmp(buf, "popup", 5)){
449 main_window_popup(mainwin);
450 } else if (!strncmp(buf, "receive_all", 11)){
451 main_window_popup(mainwin);
452 inc_all_account_mail(mainwin);
453 } else if (!strncmp(buf, "receive", 7)){
454 main_window_popup(mainwin);
456 } else if (!strncmp(buf, "compose", 7)) {
457 open_compose_new_with_recipient(buf + strlen("compose") + 1);
461 static void open_compose_new_with_recipient(const gchar *address)
466 Xstrdup_a(addr, address, return);
470 if (addr && *addr != '\0')
471 compose_new_with_recipient(NULL, addr);