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"
64 #include "manage_window.h"
65 #include "alertpanel.h"
66 #include "addressbook.h"
75 gboolean debug_mode = FALSE;
77 static gint lock_socket = -1;
78 static gint lock_socket_tag = 0;
84 const gchar *compose_mailto;
87 static void parse_cmd_opt(int argc, char *argv[]);
90 static void idle_function_for_gpgme(void);
91 #endif /* USE_GPGME */
93 static gint prohibit_duplicate_launch (void);
94 static void lock_socket_input_cb (gpointer data,
96 GdkInputCondition condition);
97 static gchar *get_socket_name (void);
99 static void open_compose_new_with_recipient (const gchar *address);
103 _("File `%s' already exists.\n"
104 "Can't create folder.")
107 #define MAKE_DIR_IF_NOT_EXIST(dir) \
109 if (!is_dir_exist(dir)) { \
110 if (is_file_exist(dir)) { \
112 (_("File `%s' already exists.\n" \
113 "Can't create folder."), \
117 if (mkdir(dir, S_IRWXU) < 0) { \
118 FILE_OP_ERROR(dir, "mkdir"); \
121 if (chmod(dir, S_IRWXU) < 0) \
122 FILE_OP_ERROR(dir, "chmod"); \
126 int main(int argc, char *argv[])
130 FolderView *folderview;
132 setlocale(LC_ALL, "");
133 bindtextdomain(PACKAGE, LOCALEDIR);
137 gtk_init(&argc, &argv);
141 if (!g_thread_supported())
142 g_error(_("g_thread is not supported by glib.\n"));
147 gtk_widget_push_visual(gdk_imlib_get_visual());
148 gtk_widget_push_colormap(gdk_imlib_get_colormap());
151 /* parse gtkrc files */
152 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
154 gtk_rc_parse(userrc);
156 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
157 G_DIR_SEPARATOR_S, "gtkrc", NULL);
158 gtk_rc_parse(userrc);
160 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
161 gtk_rc_parse(userrc);
164 gtk_rc_parse("./gtkrc");
166 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
167 gtk_item_factory_parse_rc(userrc);
170 prog_version = PROG_VERSION;
171 startup_dir = g_get_current_dir();
173 parse_cmd_opt(argc, argv);
175 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
177 /* check and create unix domain socket */
178 lock_socket = prohibit_duplicate_launch();
179 if (lock_socket < 0) return 0;
181 /* backup if old rc file exists */
182 if (is_file_exist(RC_DIR)) {
183 if (rename(RC_DIR, RC_DIR ".bak") < 0)
184 FILE_OP_ERROR(RC_DIR, "rename");
186 MAKE_DIR_IF_NOT_EXIST(RC_DIR);
187 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
188 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
189 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
191 if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "assortrc") &&
192 !is_file_exist(RC_DIR G_DIR_SEPARATOR_S "filterrc")) {
193 if (rename(RC_DIR G_DIR_SEPARATOR_S "assortrc",
194 RC_DIR G_DIR_SEPARATOR_S "filterrc") < 0)
195 FILE_OP_ERROR(RC_DIR G_DIR_SEPARATOR_S "assortrc",
199 srandom((gint)time(NULL));
202 if (gpgme_check_engine()) { /* Also does some gpgme init */
203 rfc2015_disable_all();
204 debug_print("gpgme_engine_version:\n%s\n",
205 gpgme_get_engine_info());
206 alertpanel_warning(_("GnuPG is not installed properly.\n"
207 "OpenPGP support disabled."));
209 gpgme_register_idle(idle_function_for_gpgme);
212 prefs_common_read_config();
213 prefs_common_save_config();
214 prefs_filter_read_config();
215 prefs_filter_write_config();
216 prefs_display_headers_read_config();
217 prefs_display_headers_write_config();
221 mainwin = main_window_create
222 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
223 folderview = mainwin->folderview;
225 /* register the callback of unix domain socket input */
226 lock_socket_tag = gdk_input_add(lock_socket,
227 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
228 lock_socket_input_cb,
231 account_read_config_all();
232 account_save_config_all();
234 if (folder_read_list() < 0) setup(mainwin);
235 account_set_missing_folder();
236 folderview_set(folderview);
239 inc_all_account_mail(mainwin);
240 else if (prefs_common.chk_on_startup || cmd.receive)
243 gtk_widget_grab_focus(folderview->ctree);
246 open_compose_new_with_recipient(cmd.compose_mailto);
248 /* ignore SIGPIPE signal for preventing sudden death of program */
249 signal(SIGPIPE, SIG_IGN);
256 static void parse_cmd_opt(int argc, char *argv[])
260 for (i = 1; i < argc; i++) {
261 if (!strncmp(argv[i], "--debug", 7))
263 else if (!strncmp(argv[i], "--receive-all", 13))
264 cmd.receive_all = TRUE;
265 else if (!strncmp(argv[i], "--receive", 9))
267 else if (!strncmp(argv[i], "--compose", 9)) {
268 const gchar *p = argv[i + 1];
271 cmd.compose_mailto = NULL;
272 if (p && *p != '\0' && *p != '-') {
273 if (!strncmp(p, "mailto:", 7))
274 cmd.compose_mailto = p + 7;
276 cmd.compose_mailto = p;
279 } else if (!strncmp(argv[i], "--version", 9)) {
280 puts("Sylpheed version " VERSION);
282 } else if (!strncmp(argv[i], "--help", 6)) {
283 g_print(_("Usage: %s [OPTION]...\n"), g_get_prgname());
285 puts(_(" --compose [address] open composition window"));
286 puts(_(" --receive receive new messages"));
287 puts(_(" --receive-all receive new messages of all accounts"));
288 puts(_(" --debug debug mode"));
289 puts(_(" --help display this help and exit"));
290 puts(_(" --version output version information and exit"));
297 static gint get_queued_message_num(void)
301 queue = folder_get_default_queue();
302 g_return_val_if_fail(queue != NULL, -1);
304 folder_item_scan(queue);
308 void app_will_exit(GtkWidget *widget, gpointer data)
310 MainWindow *mainwin = data;
313 if (compose_get_compose_list()) {
314 if (alertpanel(_("Notice"),
315 _("Composing message exists. Really quit?"),
316 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
318 manage_window_focus_in(mainwin->window, NULL, NULL);
321 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
322 if (alertpanel(_("Queued messages"),
323 _("Some unsent messages are queued. Exit now?"),
324 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
326 manage_window_focus_in(mainwin->window, NULL, NULL);
329 if (prefs_common.clean_on_exit)
330 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
332 /* save all state before exiting */
334 summary_write_cache(mainwin->summaryview);
336 main_window_get_size(mainwin);
337 main_window_get_position(mainwin);
338 prefs_common_save_config();
339 prefs_filter_write_config();
340 account_save_config_all();
341 addressbook_export_to_file();
343 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
344 gtk_item_factory_dump_rc(filename, NULL, TRUE);
347 /* delete temporary files */
348 remove_all_files(get_mime_tmp_dir());
350 /* delete unix domain socket */
351 gdk_input_remove(lock_socket_tag);
352 fd_close(lock_socket);
353 filename = get_socket_name();
360 static void idle_function_for_gpgme(void)
362 while (gtk_events_pending())
363 gtk_main_iteration();
365 #endif /* USE_GPGME */
367 static gchar *get_socket_name(void)
369 static gchar *filename = NULL;
371 if (filename == NULL) {
372 filename = g_strdup_printf("%s%csylpheed-%d",
373 g_get_tmp_dir(), G_DIR_SEPARATOR,
380 static gint prohibit_duplicate_launch(void)
385 path = get_socket_name();
386 uxsock = fd_connect_unix(path);
389 return fd_open_unix(path);
392 /* remote command mode */
394 debug_print(_("another Sylpheed is already running.\n"));
397 fd_write(uxsock, "receive_all\n", 12);
398 else if (cmd.receive)
399 fd_write(uxsock, "receive\n", 8);
400 else if (cmd.compose) {
403 if (cmd.compose_mailto)
404 compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
406 compose_str = g_strdup("compose\n");
408 fd_write(uxsock, compose_str, strlen(compose_str));
411 fd_write(uxsock, "popup\n", 6);
417 static void lock_socket_input_cb(gpointer data,
419 GdkInputCondition condition)
421 MainWindow *mainwin = (MainWindow *)data;
425 sock = fd_accept(source);
426 fd_read(sock, buf, sizeof(buf));
429 if (!strncmp(buf, "popup", 5)){
430 main_window_popup(mainwin);
431 } else if (!strncmp(buf, "receive_all", 11)){
432 main_window_popup(mainwin);
433 inc_all_account_mail(mainwin);
434 } else if (!strncmp(buf, "receive", 7)){
435 main_window_popup(mainwin);
437 } else if (!strncmp(buf, "compose", 7)) {
438 open_compose_new_with_recipient(buf + strlen("compose") + 1);
442 static void open_compose_new_with_recipient(const gchar *address)
447 Xstrdup_a(addr, address, return);
451 if (addr && *addr != '\0')
452 compose_new_with_recipient(NULL, addr);