2007-01-25 [paul] 2.7.1cvs2-stable
[claws.git] / src / main.c
blobbadc7940746fcdf3811c82098706edf7ac203da0
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
24 #include "defs.h"
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtkrc.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <unistd.h>
36 #include <time.h>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39 #ifdef G_OS_UNIX
40 # include <signal.h>
41 #endif
42 #ifdef HAVE_LIBSM
43 #include <X11/SM/SMlib.h>
44 #include <fcntl.h>
45 #endif
47 #include "wizard.h"
48 #ifdef HAVE_STARTUP_NOTIFICATION
49 # define SN_API_NOT_YET_FROZEN
50 # include <libsn/sn-launchee.h>
51 # include <gdk/gdkx.h>
52 #endif
54 #include "claws.h"
55 #include "main.h"
56 #include "mainwindow.h"
57 #include "folderview.h"
58 #include "image_viewer.h"
59 #include "summaryview.h"
60 #include "prefs_common.h"
61 #include "prefs_account.h"
62 #include "prefs_actions.h"
63 #include "prefs_ext_prog.h"
64 #include "prefs_fonts.h"
65 #include "prefs_image_viewer.h"
66 #include "prefs_message.h"
67 #include "prefs_receive.h"
68 #include "prefs_msg_colors.h"
69 #include "prefs_quote.h"
70 #include "prefs_spelling.h"
71 #include "prefs_summaries.h"
72 #include "prefs_themes.h"
73 #include "prefs_other.h"
74 #include "prefs_send.h"
75 #include "prefs_wrapping.h"
76 #include "prefs_compose_writing.h"
77 #include "prefs_display_header.h"
78 #include "account.h"
79 #include "procmsg.h"
80 #include "inc.h"
81 #include "import.h"
82 #include "manage_window.h"
83 #include "alertpanel.h"
84 #include "statusbar.h"
85 #include "addressbook.h"
86 #include "compose.h"
87 #include "folder.h"
88 #include "setup.h"
89 #include "utils.h"
90 #include "gtkutils.h"
91 #include "socket.h"
92 #include "log.h"
93 #include "prefs_toolbar.h"
94 #include "plugin.h"
95 #include "mh_gtk.h"
96 #include "imap_gtk.h"
97 #include "news_gtk.h"
98 #include "matcher.h"
99 #ifdef HAVE_LIBETPAN
100 #include "imap-thread.h"
101 #endif
102 #include "stock_pixmap.h"
104 #if USE_OPENSSL
105 # include "ssl.h"
106 #endif
108 #include "version.h"
110 #include "crash.h"
112 #include "timing.h"
114 gchar *prog_version;
115 gchar *argv0;
117 #ifdef HAVE_STARTUP_NOTIFICATION
118 static SnLauncheeContext *sn_context = NULL;
119 static SnDisplay *sn_display = NULL;
120 #endif
122 static gint lock_socket = -1;
123 static gint lock_socket_tag = 0;
125 typedef enum
127 ONLINE_MODE_DONT_CHANGE,
128 ONLINE_MODE_ONLINE,
129 ONLINE_MODE_OFFLINE
130 } OnlineMode;
132 static struct RemoteCmd {
133 gboolean receive;
134 gboolean receive_all;
135 gboolean compose;
136 const gchar *compose_mailto;
137 GPtrArray *attach_files;
138 gboolean status;
139 gboolean status_full;
140 GPtrArray *status_folders;
141 GPtrArray *status_full_folders;
142 gboolean send;
143 gboolean crash;
144 int online_mode;
145 gchar *crash_params;
146 gboolean exit;
147 gboolean subscribe;
148 const gchar *subscribe_uri;
149 const gchar *target;
150 } cmd;
152 static void parse_cmd_opt(int argc, char *argv[]);
154 static gint prohibit_duplicate_launch (void);
155 static gchar * get_crashfile_name (void);
156 static gint lock_socket_remove (void);
157 static void lock_socket_input_cb (gpointer data,
158 gint source,
159 GdkInputCondition condition);
160 #ifndef CLAWS
161 static
162 #endif
163 gchar *get_socket_name (void);
166 static void open_compose_new (const gchar *address,
167 GPtrArray *attach_files);
169 static void send_queue (void);
170 static void initial_processing (FolderItem *item, gpointer data);
171 static void quit_signal_handler (int sig);
172 static void install_basic_sighandlers (void);
173 static void exit_claws (MainWindow *mainwin);
175 #define MAKE_DIR_IF_NOT_EXIST(dir) \
177 if (!is_dir_exist(dir)) { \
178 if (is_file_exist(dir)) { \
179 alertpanel_warning \
180 (_("File '%s' already exists.\n" \
181 "Can't create folder."), \
182 dir); \
183 return 1; \
185 if (make_dir(dir) < 0) \
186 return 1; \
190 static MainWindow *static_mainwindow;
191 static gboolean emergency_exit = FALSE;
193 #ifdef HAVE_STARTUP_NOTIFICATION
194 static void sn_error_trap_push(SnDisplay *display, Display *xdisplay)
196 gdk_error_trap_push();
199 static void sn_error_trap_pop(SnDisplay *display, Display *xdisplay)
201 gdk_error_trap_pop();
204 static void startup_notification_complete(gboolean with_window)
206 Display *xdisplay;
207 GtkWidget *hack = NULL;
209 if (with_window) {
210 /* this is needed to make the startup notification leave,
211 * if we have been launched from a menu.
212 * We have to display a window, so let it be very little */
213 hack = gtk_window_new(GTK_WINDOW_POPUP);
214 gtk_widget_set_uposition(hack, 0, 0);
215 gtk_widget_set_size_request(hack, 1, 1);
216 gtk_widget_show(hack);
219 xdisplay = GDK_DISPLAY();
220 sn_display = sn_display_new(xdisplay,
221 sn_error_trap_push,
222 sn_error_trap_pop);
223 sn_context = sn_launchee_context_new_from_environment(sn_display,
224 DefaultScreen(xdisplay));
226 if (sn_context != NULL) {
227 sn_launchee_context_complete(sn_context);
228 sn_launchee_context_unref(sn_context);
229 sn_display_unref(sn_display);
231 if (with_window) {
232 gtk_widget_destroy(hack);
235 #endif /* HAVE_STARTUP_NOTIFICATION */
237 void claws_gtk_idle(void)
239 while(gtk_events_pending()) {
240 gtk_main_iteration();
242 g_usleep(50000);
245 gboolean defer_check_all(void *data)
247 gboolean autochk = GPOINTER_TO_INT(data);
249 inc_all_account_mail(static_mainwindow, autochk,
250 prefs_common.newmail_notify_manu);
252 return FALSE;
255 gboolean defer_check(void *data)
257 inc_mail(static_mainwindow, prefs_common.newmail_notify_manu);
259 return FALSE;
262 static gboolean defer_jump(void *data)
264 mainwindow_jump_to(data);
265 return FALSE;
268 static void chk_update_val(GtkWidget *widget, gpointer data)
270 gboolean *val = (gboolean *)data;
271 *val = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
274 static gboolean migrate_old_config(const gchar *old_cfg_dir, const gchar *new_cfg_dir, const gchar *oldversion)
276 gchar *message = g_strdup_printf(_("Configuration for %s (or previous) found.\n"
277 "Do you want to migrate this configuration?"), oldversion);
278 gint r = 0;
279 GtkWidget *window = NULL;
280 GtkWidget *keep_backup_chk;
281 GtkTooltips *tips = gtk_tooltips_new();
282 gboolean backup = TRUE;
284 keep_backup_chk = gtk_check_button_new_with_label (_("Keep old configuration"));
285 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_backup_chk), TRUE);
286 gtk_tooltips_set_tip(GTK_TOOLTIPS(tips), keep_backup_chk,
287 _("Keeping a backup will allow you to go back to an "
288 "older version, but may take a while if you have "
289 "cached IMAP or News data, and will take some extra "
290 "room on your disk."),
291 NULL);
293 g_signal_connect(G_OBJECT(keep_backup_chk), "toggled",
294 G_CALLBACK(chk_update_val), &backup);
296 if (alertpanel_full(_("Migration of configuration"), message,
297 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL, FALSE,
298 keep_backup_chk, ALERT_QUESTION, G_ALERTDEFAULT) != G_ALERTALTERNATE) {
299 return FALSE;
302 /* we can either do a fast migration requiring not any extra disk
303 * space, or a slow one that copies the old configuration and leaves
304 * it in place. */
305 if (backup) {
306 backup_mode:
307 window = label_window_create(_("Copying configuration... This may take a while..."));
308 GTK_EVENTS_FLUSH();
310 r = copy_dir(old_cfg_dir, new_cfg_dir);
311 gtk_widget_destroy(window);
313 /* if copy failed, we'll remove the partially copied
314 * new directory */
315 if (r != 0) {
316 alertpanel_error(_("Migration failed!"));
317 remove_dir_recursive(new_cfg_dir);
318 } else {
319 if (!backup) {
320 /* fast mode failed, but we don't want backup */
321 remove_dir_recursive(old_cfg_dir);
324 } else {
325 window = label_window_create(_("Migrating configuration..."));
326 GTK_EVENTS_FLUSH();
328 r = g_rename(old_cfg_dir, new_cfg_dir);
329 gtk_widget_destroy(window);
331 /* if g_rename failed, we'll try to copy */
332 if (r != 0) {
333 FILE_OP_ERROR(new_cfg_dir, "g_rename failed, trying copy\n");
334 goto backup_mode;
337 return (r == 0);
340 static void migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
342 FILE *oldfp, *newfp;
343 gchar *plugin_path, *old_plugin_path, *new_plugin_path;
344 gchar buf[BUFFSIZE];
345 oldfp = g_fopen(old_rc, "r");
346 if (!oldfp)
347 return;
348 newfp = g_fopen(new_rc, "w");
349 if (!newfp) {
350 fclose(oldfp);
351 return;
354 plugin_path = g_strdup(get_plugin_dir());
355 new_plugin_path = g_strdup(plugin_path);
357 if (strstr(plugin_path, "/claws-mail/")) {
358 gchar *end = g_strdup(strstr(plugin_path, "/claws-mail/")+strlen("/claws-mail/"));
359 *(strstr(plugin_path, "/claws-mail/")) = '\0';
360 old_plugin_path = g_strconcat(plugin_path, "/sylpheed-claws/", end, NULL);
361 g_free(end);
362 } else {
363 old_plugin_path = g_strdup(new_plugin_path);
365 debug_print("replacing %s with %s\n", old_plugin_path, new_plugin_path);
366 while (fgets(buf, sizeof(buf), oldfp)) {
367 if (strncmp(buf, old_plugin_path, strlen(old_plugin_path))) {
368 fputs(buf, newfp);
369 } else {
370 debug_print("->replacing %s", buf);
371 debug_print(" with %s%s", new_plugin_path, buf+strlen(old_plugin_path));
372 fputs(new_plugin_path, newfp);
373 fputs(buf+strlen(old_plugin_path), newfp);
376 g_free(plugin_path);
377 g_free(new_plugin_path);
378 g_free(old_plugin_path);
379 fclose(oldfp);
380 fclose(newfp);
383 #ifdef HAVE_LIBSM
384 static void
385 sc_client_set_value (MainWindow *mainwin,
386 gchar *name,
387 char *type,
388 int num_vals,
389 SmPropValue *vals)
391 SmProp *proplist[1];
392 SmProp prop;
394 prop.name = name;
395 prop.type = type;
396 prop.num_vals = num_vals;
397 prop.vals = vals;
399 proplist[0]= &prop;
400 if (mainwin->smc_conn)
401 SmcSetProperties ((SmcConn) mainwin->smc_conn, 1, proplist);
404 static void sc_die_callback (SmcConn smc_conn, SmPointer client_data)
406 clean_quit(NULL);
409 static void sc_save_complete_callback(SmcConn smc_conn, SmPointer client_data)
413 static void sc_shutdown_cancelled_callback (SmcConn smc_conn, SmPointer client_data)
415 MainWindow *mainwin = (MainWindow *)client_data;
416 if (mainwin->smc_conn)
417 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
420 static void sc_save_yourself_callback (SmcConn smc_conn,
421 SmPointer client_data,
422 int save_style,
423 gboolean shutdown,
424 int interact_style,
425 gboolean fast) {
427 MainWindow *mainwin = (MainWindow *)client_data;
428 if (mainwin->smc_conn)
429 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
432 static IceIOErrorHandler sc_ice_installed_handler;
434 static void sc_ice_io_error_handler (IceConn connection)
436 if (sc_ice_installed_handler)
437 (*sc_ice_installed_handler) (connection);
439 static gboolean sc_process_ice_messages (GIOChannel *source,
440 GIOCondition condition,
441 gpointer data)
443 IceConn connection = (IceConn) data;
444 IceProcessMessagesStatus status;
446 status = IceProcessMessages (connection, NULL, NULL);
448 if (status == IceProcessMessagesIOError) {
449 IcePointer context = IceGetConnectionContext (connection);
451 if (context && GTK_IS_OBJECT (context)) {
452 guint disconnect_id = g_signal_lookup ("disconnect", G_OBJECT_TYPE (context));
454 if (disconnect_id > 0)
455 g_signal_emit (context, disconnect_id, 0);
456 } else {
457 IceSetShutdownNegotiation (connection, False);
458 IceCloseConnection (connection);
462 return TRUE;
465 static void new_ice_connection (IceConn connection, IcePointer client_data, Bool opening,
466 IcePointer *watch_data)
468 guint input_id;
470 if (opening) {
471 GIOChannel *channel;
472 /* Make sure we don't pass on these file descriptors to any
473 exec'ed children */
474 fcntl(IceConnectionNumber(connection),F_SETFD,
475 fcntl(IceConnectionNumber(connection),F_GETFD,0) | FD_CLOEXEC);
477 channel = g_io_channel_unix_new (IceConnectionNumber (connection));
478 input_id = g_io_add_watch (channel,
479 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
480 sc_process_ice_messages,
481 connection);
482 g_io_channel_unref (channel);
484 *watch_data = (IcePointer) GUINT_TO_POINTER (input_id);
485 } else {
486 input_id = GPOINTER_TO_UINT ((gpointer) *watch_data);
487 g_source_remove (input_id);
491 static void sc_session_manager_connect(MainWindow *mainwin)
493 static gboolean connected = FALSE;
494 SmcCallbacks callbacks;
495 gchar *client_id;
496 IceIOErrorHandler default_handler;
498 if (connected)
499 return;
500 connected = TRUE;
503 sc_ice_installed_handler = IceSetIOErrorHandler (NULL);
504 default_handler = IceSetIOErrorHandler (sc_ice_io_error_handler);
506 if (sc_ice_installed_handler == default_handler)
507 sc_ice_installed_handler = NULL;
509 IceAddConnectionWatch (new_ice_connection, NULL);
512 callbacks.save_yourself.callback = sc_save_yourself_callback;
513 callbacks.die.callback = sc_die_callback;
514 callbacks.save_complete.callback = sc_save_complete_callback;
515 callbacks.shutdown_cancelled.callback = sc_shutdown_cancelled_callback;
517 callbacks.save_yourself.client_data =
518 callbacks.die.client_data =
519 callbacks.save_complete.client_data =
520 callbacks.shutdown_cancelled.client_data = (SmPointer) mainwin;
521 if (g_getenv ("SESSION_MANAGER")) {
522 gchar error_string_ret[256] = "";
524 mainwin->smc_conn = (gpointer)
525 SmcOpenConnection (NULL, mainwin,
526 SmProtoMajor, SmProtoMinor,
527 SmcSaveYourselfProcMask | SmcDieProcMask |
528 SmcSaveCompleteProcMask |
529 SmcShutdownCancelledProcMask,
530 &callbacks,
531 NULL, &client_id,
532 256, error_string_ret);
534 if (error_string_ret[0] || mainwin->smc_conn == NULL)
535 g_warning ("While connecting to session manager:\n%s.",
536 error_string_ret);
537 else {
538 SmPropValue *vals;
539 vals = g_new (SmPropValue, 1);
540 vals[0].length = strlen(argv0);
541 vals[0].value = argv0;
542 sc_client_set_value (mainwin, SmCloneCommand, SmLISTofARRAY8, 1, vals);
543 sc_client_set_value (mainwin, SmRestartCommand, SmLISTofARRAY8, 1, vals);
544 sc_client_set_value (mainwin, SmProgram, SmARRAY8, 1, vals);
546 vals[0].length = strlen(g_get_user_name()?g_get_user_name():"");
547 vals[0].value = g_strdup(g_get_user_name()?g_get_user_name():"");
548 sc_client_set_value (mainwin, SmUserID, SmARRAY8, 1, vals);
552 #endif
554 static gboolean sc_exiting = FALSE;
555 static gboolean sc_starting = FALSE;
557 int main(int argc, char *argv[])
559 gchar *userrc;
560 MainWindow *mainwin;
561 FolderView *folderview;
562 GdkPixbuf *icon;
563 gboolean crash_file_present = FALSE;
564 gint num_folder_class = 0;
565 gboolean asked_for_migration = FALSE;
567 START_TIMING("startup");
569 sc_starting = TRUE;
571 if (!claws_init(&argc, &argv)) {
572 return 0;
575 prefs_prepare_cache();
576 prog_version = PROG_VERSION;
577 argv0 = g_strdup(argv[0]);
579 parse_cmd_opt(argc, argv);
581 #ifdef CRASH_DIALOG
582 if (cmd.crash) {
583 gtk_set_locale();
584 gtk_init(&argc, &argv);
585 crash_main(cmd.crash_params);
586 return 0;
588 crash_install_handlers();
589 #endif
590 install_basic_sighandlers();
591 sock_init();
593 /* check and create unix domain socket for remote operation */
594 #ifdef G_OS_UNIX
595 lock_socket = prohibit_duplicate_launch();
596 if (lock_socket < 0) {
597 #ifdef HAVE_STARTUP_NOTIFICATION
598 if(gtk_init_check(&argc, &argv))
599 startup_notification_complete(TRUE);
600 #endif
601 return 0;
604 if (cmd.status || cmd.status_full) {
605 puts("0 Claws Mail not running.");
606 lock_socket_remove();
607 return 0;
610 if (cmd.exit)
611 return 0;
612 #endif
613 if (!g_thread_supported())
614 g_thread_init(NULL);
616 gtk_set_locale();
617 gtk_init(&argc, &argv);
619 gdk_rgb_init();
620 gtk_widget_set_default_colormap(gdk_rgb_get_colormap());
621 gtk_widget_set_default_visual(gdk_rgb_get_visual());
623 if (!g_thread_supported()) {
624 g_error(_("g_thread is not supported by glib.\n"));
627 /* check that we're not on a too recent/old gtk+ */
628 #if GTK_CHECK_VERSION(2, 9, 0)
629 if (gtk_check_version(2, 9, 0) != NULL) {
630 alertpanel_error(_("Claws Mail has been compiled with "
631 "a more recent GTK+ library than is "
632 "currently available. This will cause "
633 "crashes. You need to upgrade GTK+ or "
634 "recompile Claws Mail."));
635 exit(1);
637 #else
638 if (gtk_check_version(2, 9, 0) == NULL) {
639 alertpanel_error(_("Claws Mail has been compiled with "
640 "an older GTK+ library than is "
641 "currently available. This will cause "
642 "crashes. You need to recompile "
643 "Claws Mail."));
644 exit(1);
646 #endif
647 /* parse gtkrc files */
648 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc-2.0",
649 NULL);
650 gtk_rc_parse(userrc);
651 g_free(userrc);
652 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
653 G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
654 gtk_rc_parse(userrc);
655 g_free(userrc);
657 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
659 /* no config dir exists. See if we can migrate an old config. */
660 if (!is_dir_exist(RC_DIR)) {
661 prefs_destroy_cache();
662 gboolean r = FALSE;
664 /* if one of the old dirs exist, we'll ask if the user
665 * want to migrates, and r will be TRUE if he said yes
666 * and migration succeeded, and FALSE otherwise.
668 if (is_dir_exist(OLD_GTK2_RC_DIR)) {
669 r = migrate_old_config(OLD_GTK2_RC_DIR, RC_DIR, "Sylpheed-Claws 2.6.0");
670 asked_for_migration = TRUE;
671 } else if (is_dir_exist(OLDER_GTK2_RC_DIR)) {
672 r = migrate_old_config(OLDER_GTK2_RC_DIR, RC_DIR, "Sylpheed-Claws 1.9.15");
673 asked_for_migration = TRUE;
674 } else if (is_dir_exist(OLD_GTK1_RC_DIR)) {
675 r = migrate_old_config(OLD_GTK1_RC_DIR, RC_DIR, "Sylpheed-Claws 1.0.5");
676 asked_for_migration = TRUE;
679 /* If migration failed or the user didn't want to do it,
680 * we create a new one (and we'll hit wizard later).
682 if (r == FALSE && !is_dir_exist(RC_DIR) && make_dir(RC_DIR) < 0)
683 exit(1);
687 if (!is_file_exist(RC_DIR G_DIR_SEPARATOR_S COMMON_RC) &&
688 is_file_exist(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC)) {
689 /* post 2.6 name change */
690 migrate_common_rc(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC,
691 RC_DIR G_DIR_SEPARATOR_S COMMON_RC);
694 if (!cmd.exit)
695 plugin_load_all("Common");
697 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
698 gtk_rc_parse(userrc);
699 g_free(userrc);
701 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
702 gtk_accel_map_load (userrc);
703 g_free(userrc);
705 gtk_settings_set_long_property(gtk_settings_get_default(),
706 "gtk-can-change-accels",
707 (glong)TRUE, "XProperty");
709 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
711 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
712 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
713 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
714 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
715 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
716 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR);
718 crash_file_present = is_file_exist(get_crashfile_name());
719 /* remove temporary files */
720 remove_all_files(get_tmp_dir());
721 remove_all_files(get_mime_tmp_dir());
723 if (is_file_exist("claws.log")) {
724 if (rename_force("claws.log", "claws.log.bak") < 0)
725 FILE_OP_ERROR("claws.log", "rename");
727 set_log_file("claws.log");
729 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
731 folder_system_init();
732 prefs_common_read_config();
734 prefs_themes_init();
735 prefs_fonts_init();
736 prefs_ext_prog_init();
737 prefs_wrapping_init();
738 prefs_compose_writing_init();
739 prefs_msg_colors_init();
740 image_viewer_init();
741 prefs_image_viewer_init();
742 prefs_quote_init();
743 prefs_summaries_init();
744 prefs_message_init();
745 prefs_other_init();
746 prefs_receive_init();
747 prefs_send_init();
748 #ifdef USE_ASPELL
749 gtkaspell_checkers_init();
750 prefs_spelling_init();
751 #endif
753 sock_set_io_timeout(prefs_common.io_timeout_secs);
754 #ifdef HAVE_LIBETPAN
755 imap_main_set_timeout(prefs_common.io_timeout_secs);
756 #endif
757 prefs_actions_read_config();
758 prefs_display_header_read_config();
759 /* prefs_filtering_read_config(); */
760 addressbook_read_file();
761 renderer_read_config();
763 gtkut_widget_init();
764 stock_pixbuf_gdk(NULL, STOCK_PIXMAP_CLAWS_MAIL_ICON, &icon);
765 gtk_window_set_default_icon(icon);
767 folderview_initialize();
769 mh_gtk_init();
770 imap_gtk_init();
771 news_gtk_init();
773 mainwin = main_window_create
774 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
776 manage_window_focus_in(mainwin->window, NULL, NULL);
777 folderview = mainwin->folderview;
779 gtk_clist_freeze(GTK_CLIST(mainwin->folderview->ctree));
780 folder_item_update_freeze();
782 /* register the callback of unix domain socket input */
783 #ifdef G_OS_UNIX
784 lock_socket_tag = gdk_input_add(lock_socket,
785 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
786 lock_socket_input_cb,
787 mainwin);
788 #endif
790 prefs_account_init();
791 account_read_config_all();
793 /* If we can't read a folder list or don't have accounts,
794 * it means the configuration's not done. Either this is
795 * a brand new install, either a failed/refused migration.
796 * So we'll start the wizard.
798 if (folder_read_list() < 0) {
799 prefs_destroy_cache();
801 /* if run_wizard returns FALSE it's because it's
802 * been cancelled. We can't do much but exit.
803 * however, if the user was asked for a migration,
804 * we remove the newly created directory so that
805 * he's asked again for migration on next launch.*/
806 if (!run_wizard(mainwin, TRUE)) {
807 if (asked_for_migration)
808 remove_dir_recursive(RC_DIR);
809 exit(1);
811 main_window_reflect_prefs_all_now();
812 folder_write_list();
815 if (!account_get_list()) {
816 prefs_destroy_cache();
817 if (!run_wizard(mainwin, FALSE)) {
818 if (asked_for_migration)
819 remove_dir_recursive(RC_DIR);
820 exit(1);
822 account_read_config_all();
823 if(!account_get_list()) {
824 exit_claws(mainwin);
825 exit(1);
830 toolbar_main_set_sensitive(mainwin);
831 main_window_set_menu_sensitive(mainwin);
833 main_window_popup(mainwin);
835 #ifdef HAVE_LIBETPAN
836 imap_main_init(prefs_common.skip_ssl_cert_check);
837 #endif
838 account_set_missing_folder();
839 folder_set_missing_folders();
840 folderview_set(folderview);
842 prefs_matcher_read_config();
844 /* make one all-folder processing before using claws */
845 main_window_cursor_wait(mainwin);
846 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
848 /* if claws crashed, rebuild caches */
849 if (!cmd.crash && crash_file_present) {
850 GTK_EVENTS_FLUSH();
851 debug_print("Claws Mail crashed, checking for new messages in local folders\n");
852 folder_item_update_thaw();
853 folderview_check_new(NULL);
854 folder_clean_cache_memory_force();
855 folder_item_update_freeze();
857 /* make the crash-indicator file */
858 str_write_to_file("foo", get_crashfile_name());
860 inc_autocheck_timer_init(mainwin);
862 /* ignore SIGPIPE signal for preventing sudden death of program */
863 #ifdef G_OS_UNIX
864 signal(SIGPIPE, SIG_IGN);
865 #endif
866 if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
867 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
869 if (cmd.online_mode == ONLINE_MODE_ONLINE) {
870 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
873 if (cmd.status_folders) {
874 g_ptr_array_free(cmd.status_folders, TRUE);
875 cmd.status_folders = NULL;
877 if (cmd.status_full_folders) {
878 g_ptr_array_free(cmd.status_full_folders, TRUE);
879 cmd.status_full_folders = NULL;
882 claws_register_idle_function(claws_gtk_idle);
884 prefs_toolbar_init();
886 num_folder_class = g_list_length(folder_get_list());
888 plugin_load_all("GTK2");
890 if (g_list_length(folder_get_list()) != num_folder_class) {
891 debug_print("new folders loaded, reloading processing rules\n");
892 prefs_matcher_read_config();
895 if (plugin_get_unloaded_list() != NULL) {
896 main_window_cursor_normal(mainwin);
897 alertpanel_warning(_("Some plugin(s) failed to load. "
898 "Check the Plugins configuration "
899 "for more information."));
900 main_window_cursor_wait(mainwin);
903 plugin_load_standard_plugins ();
905 if (!folder_have_mailbox()) {
906 prefs_destroy_cache();
907 main_window_cursor_normal(mainwin);
908 if (folder_get_list() != NULL) {
909 alertpanel_error(_("Claws Mail has detected a configured "
910 "mailbox, but is it incomplete. It is "
911 "possibly due to a failing IMAP account. Use "
912 "\"Rebuild folder tree\" on the mailbox's folder "
913 "to try to fix it."));
914 } else {
915 alertpanel_error(_("Claws Mail has detected a configured "
916 "mailbox, but could not load it. It is "
917 "probably provided by an out-of-date "
918 "external plugin. Please reinstall the "
919 "plugin and try again."));
920 exit_claws(mainwin);
921 exit(1);
925 static_mainwindow = mainwin;
927 #ifdef HAVE_STARTUP_NOTIFICATION
928 startup_notification_complete(FALSE);
929 #endif
930 #ifdef HAVE_LIBSM
931 sc_session_manager_connect(mainwin);
932 #endif
933 folder_item_update_thaw();
934 gtk_clist_thaw(GTK_CLIST(mainwin->folderview->ctree));
935 main_window_cursor_normal(mainwin);
937 if (cmd.receive_all) {
938 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(FALSE));
939 } else if (prefs_common.chk_on_startup) {
940 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(TRUE));
941 } else if (cmd.receive) {
942 g_timeout_add(1000, defer_check, NULL);
943 } else {
944 gtk_widget_grab_focus(folderview->ctree);
947 if (cmd.compose) {
948 open_compose_new(cmd.compose_mailto, cmd.attach_files);
950 if (cmd.attach_files) {
951 ptr_array_free_strings(cmd.attach_files);
952 g_ptr_array_free(cmd.attach_files, TRUE);
953 cmd.attach_files = NULL;
955 if (cmd.subscribe) {
956 folder_subscribe(cmd.subscribe_uri);
959 if (cmd.send) {
960 send_queue();
963 if (cmd.target) {
964 g_timeout_add(500, defer_jump, (gpointer)cmd.target);
967 prefs_destroy_cache();
969 sc_starting = FALSE;
970 END_TIMING();
972 gtk_main();
974 exit_claws(mainwin);
976 return 0;
979 static void save_all_caches(FolderItem *item, gpointer data)
981 if (!item->cache) {
982 return;
985 if (item->opened)
986 folder_item_close(item);
988 folder_item_free_cache(item, TRUE);
991 static void exit_claws(MainWindow *mainwin)
993 gchar *filename;
995 sc_exiting = TRUE;
997 debug_print("shutting down\n");
998 inc_autocheck_timer_remove();
1000 if (prefs_common.clean_on_exit && !emergency_exit) {
1001 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
1004 /* save prefs for opened folder */
1005 if(mainwin->folderview->opened) {
1006 FolderItem *item;
1008 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
1009 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
1012 /* save all state before exiting */
1013 folder_func_to_all_folders(save_all_caches, NULL);
1014 folder_write_list();
1016 main_window_get_size(mainwin);
1017 main_window_get_position(mainwin);
1018 prefs_common_write_config();
1019 account_write_config_all();
1020 addressbook_export_to_file();
1022 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1023 gtk_accel_map_save(filename);
1024 g_free(filename);
1026 /* delete temporary files */
1027 remove_all_files(get_tmp_dir());
1028 remove_all_files(get_mime_tmp_dir());
1030 close_log_file();
1032 #ifdef HAVE_LIBETPAN
1033 imap_main_done();
1034 #endif
1035 /* delete crashfile */
1036 if (!cmd.crash)
1037 g_unlink(get_crashfile_name());
1039 lock_socket_remove();
1041 #ifdef HAVE_LIBSM
1042 if (mainwin->smc_conn)
1043 SmcCloseConnection ((SmcConn)mainwin->smc_conn, 0, NULL);
1044 mainwin->smc_conn = NULL;
1045 #endif
1047 main_window_destroy_all();
1049 plugin_unload_all("GTK2");
1051 prefs_toolbar_done();
1053 addressbook_destroy();
1055 prefs_themes_done();
1056 prefs_fonts_done();
1057 prefs_ext_prog_done();
1058 prefs_wrapping_done();
1059 prefs_compose_writing_done();
1060 prefs_msg_colors_done();
1061 prefs_image_viewer_done();
1062 image_viewer_done();
1063 prefs_quote_done();
1064 prefs_summaries_done();
1065 prefs_message_done();
1066 prefs_other_done();
1067 prefs_receive_done();
1068 prefs_send_done();
1069 #ifdef USE_ASPELL
1070 prefs_spelling_done();
1071 gtkaspell_checkers_quit();
1072 #endif
1073 plugin_unload_all("Common");
1074 claws_done();
1077 static void parse_cmd_opt(int argc, char *argv[])
1079 gint i;
1081 for (i = 1; i < argc; i++) {
1082 if (!strncmp(argv[i], "--receive-all", 13)) {
1083 cmd.receive_all = TRUE;
1084 } else if (!strncmp(argv[i], "--receive", 9)) {
1085 cmd.receive = TRUE;
1086 } else if (!strncmp(argv[i], "--compose", 9)) {
1087 const gchar *p = argv[i + 1];
1089 cmd.compose = TRUE;
1090 cmd.compose_mailto = NULL;
1091 if (p && *p != '\0' && *p != '-') {
1092 if (!strncmp(p, "mailto:", 7)) {
1093 cmd.compose_mailto = p + 7;
1094 } else {
1095 cmd.compose_mailto = p;
1097 i++;
1099 } else if (!strncmp(argv[i], "--subscribe", 11)) {
1100 const gchar *p = argv[i + 1];
1101 if (p && *p != '\0' && *p != '-') {
1102 cmd.subscribe = TRUE;
1103 cmd.subscribe_uri = p;
1105 } else if (!strncmp(argv[i], "--attach", 8)) {
1106 const gchar *p = argv[i + 1];
1107 gchar *file;
1109 while (p && *p != '\0' && *p != '-') {
1110 if (!cmd.attach_files) {
1111 cmd.attach_files = g_ptr_array_new();
1113 if (*p != G_DIR_SEPARATOR) {
1114 file = g_strconcat(claws_get_startup_dir(),
1115 G_DIR_SEPARATOR_S,
1116 p, NULL);
1117 } else {
1118 file = g_strdup(p);
1120 g_ptr_array_add(cmd.attach_files, file);
1121 i++;
1122 p = argv[i + 1];
1124 } else if (!strncmp(argv[i], "--send", 6)) {
1125 cmd.send = TRUE;
1126 } else if (!strncmp(argv[i], "--version", 9) ||
1127 !strncmp(argv[i], "-v", 2)) {
1128 puts("Claws Mail version " VERSION);
1129 exit(0);
1130 } else if (!strncmp(argv[i], "--status-full", 13)) {
1131 const gchar *p = argv[i + 1];
1133 cmd.status_full = TRUE;
1134 while (p && *p != '\0' && *p != '-') {
1135 if (!cmd.status_full_folders) {
1136 cmd.status_full_folders =
1137 g_ptr_array_new();
1139 g_ptr_array_add(cmd.status_full_folders,
1140 g_strdup(p));
1141 i++;
1142 p = argv[i + 1];
1144 } else if (!strncmp(argv[i], "--status", 8)) {
1145 const gchar *p = argv[i + 1];
1147 cmd.status = TRUE;
1148 while (p && *p != '\0' && *p != '-') {
1149 if (!cmd.status_folders)
1150 cmd.status_folders = g_ptr_array_new();
1151 g_ptr_array_add(cmd.status_folders,
1152 g_strdup(p));
1153 i++;
1154 p = argv[i + 1];
1156 } else if (!strncmp(argv[i], "--online", 8)) {
1157 cmd.online_mode = ONLINE_MODE_ONLINE;
1158 } else if (!strncmp(argv[i], "--offline", 9)) {
1159 cmd.online_mode = ONLINE_MODE_OFFLINE;
1160 } else if (!strncmp(argv[i], "--help", 6) ||
1161 !strncmp(argv[i], "-h", 2)) {
1162 gchar *base = g_path_get_basename(argv[0]);
1163 g_print(_("Usage: %s [OPTION]...\n"), base);
1165 g_print("%s\n", _(" --compose [address] open composition window"));
1166 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
1167 g_print("%s\n", _(" --attach file1 [file2]...\n"
1168 " open composition window with specified files\n"
1169 " attached"));
1170 g_print("%s\n", _(" --receive receive new messages"));
1171 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
1172 g_print("%s\n", _(" --send send all queued messages"));
1173 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
1174 g_print("%s\n", _(" --status-full [folder]...\n"
1175 " show the status of each folder"));
1176 g_print("%s\n", _(" --select folder[/msg] jumps to the specified folder/message\n"
1177 " folder is a folder id like 'folder/sub_folder'"));
1178 g_print("%s\n", _(" --online switch to online mode"));
1179 g_print("%s\n", _(" --offline switch to offline mode"));
1180 g_print("%s\n", _(" --exit --quit -q exit Claws Mail"));
1181 g_print("%s\n", _(" --debug debug mode"));
1182 g_print("%s\n", _(" --help -h display this help and exit"));
1183 g_print("%s\n", _(" --version -v output version information and exit"));
1184 g_print("%s\n", _(" --config-dir output configuration directory"));
1186 g_free(base);
1187 exit(1);
1188 } else if (!strncmp(argv[i], "--crash", 7)) {
1189 cmd.crash = TRUE;
1190 cmd.crash_params = g_strdup(argv[i + 1]);
1191 i++;
1192 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
1193 puts(RC_DIR);
1194 exit(0);
1195 } else if (!strncmp(argv[i], "--exit", 6) ||
1196 !strncmp(argv[i], "--quit", 6) ||
1197 !strncmp(argv[i], "-q", 2)) {
1198 cmd.exit = TRUE;
1199 } else if (!strncmp(argv[i], "--select", 8) && i+1 < argc) {
1200 cmd.target = argv[i+1];
1201 } else if (i == 1 && argc == 2) {
1202 /* only one parameter. Do something intelligent about it */
1203 if (strstr(argv[i], "@") && !strstr(argv[i], "://")) {
1204 const gchar *p = argv[i];
1206 cmd.compose = TRUE;
1207 cmd.compose_mailto = NULL;
1208 if (p && *p != '\0' && *p != '-') {
1209 if (!strncmp(p, "mailto:", 7)) {
1210 cmd.compose_mailto = p + 7;
1211 } else {
1212 cmd.compose_mailto = p;
1215 } else if (strstr(argv[i], "://")) {
1216 const gchar *p = argv[i];
1217 if (p && *p != '\0' && *p != '-') {
1218 cmd.subscribe = TRUE;
1219 cmd.subscribe_uri = p;
1221 } else if (!strcmp(argv[i], "--sync")) {
1222 /* gtk debug */
1223 } else {
1224 g_print(_("Unknown option\n"));
1225 exit(1);
1230 if (cmd.attach_files && cmd.compose == FALSE) {
1231 cmd.compose = TRUE;
1232 cmd.compose_mailto = NULL;
1236 static gint get_queued_message_num(void)
1238 FolderItem *queue;
1240 queue = folder_get_default_queue();
1241 if (!queue) {
1242 return -1;
1245 folder_item_scan(queue);
1246 return queue->total_msgs;
1249 static void initial_processing(FolderItem *item, gpointer data)
1251 MainWindow *mainwin = (MainWindow *)data;
1252 gchar *buf;
1254 g_return_if_fail(item);
1255 buf = g_strdup_printf(_("Processing (%s)..."),
1256 item->path
1257 ? item->path
1258 : _("top level folder"));
1259 g_free(buf);
1262 if (item->prefs->enable_processing) {
1263 folder_item_apply_processing(item);
1266 STATUSBAR_POP(mainwin);
1269 static void draft_all_messages(void)
1271 GList *compose_list = NULL;
1273 while ((compose_list = compose_get_compose_list()) != NULL) {
1274 Compose *c = (Compose*)compose_list->data;
1275 compose_draft(c);
1278 gboolean clean_quit(gpointer data)
1280 static gboolean firstrun = TRUE;
1282 if (!firstrun) {
1283 return FALSE;
1285 firstrun = FALSE;
1287 /*!< Good idea to have the main window stored in a
1288 * static variable so we can check that variable
1289 * to see if we're really allowed to do things
1290 * that actually the spawner is supposed to
1291 * do (like: sending mail, composing messages).
1292 * Because, really, if we're the spawnee, and
1293 * we touch GTK stuff, we're hosed. See the
1294 * next fixme. */
1296 /* FIXME: Use something else to signal that we're
1297 * in the original spawner, and not in a spawned
1298 * child. */
1299 if (!static_mainwindow) {
1300 return FALSE;
1303 draft_all_messages();
1304 emergency_exit = TRUE;
1305 exit_claws(static_mainwindow);
1306 exit(0);
1308 return FALSE;
1311 void app_will_exit(GtkWidget *widget, gpointer data)
1313 MainWindow *mainwin = data;
1315 if (sc_exiting == TRUE) {
1316 debug_print("exit pending\n");
1317 return;
1319 sc_exiting = TRUE;
1320 debug_print("exiting\n");
1321 if (compose_get_compose_list()) {
1322 gint val = alertpanel(_("Really quit?"),
1323 _("Composing message exists."),
1324 _("_Save to Draft"), _("_Discard them"), _("Do_n't quit"));
1325 switch (val) {
1326 case G_ALERTOTHER:
1327 sc_exiting = FALSE;
1328 return;
1329 case G_ALERTALTERNATE:
1330 break;
1331 default:
1332 draft_all_messages();
1335 manage_window_focus_in(mainwin->window, NULL, NULL);
1338 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
1339 if (alertpanel(_("Queued messages"),
1340 _("Some unsent messages are queued. Exit now?"),
1341 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL)
1342 != G_ALERTALTERNATE) {
1343 sc_exiting = FALSE;
1344 return;
1346 manage_window_focus_in(mainwin->window, NULL, NULL);
1349 sock_cleanup();
1350 if (folderview_get_selected_item(mainwin->folderview))
1351 folder_item_close(folderview_get_selected_item(mainwin->folderview));
1352 gtk_main_quit();
1355 gboolean claws_is_exiting(void)
1357 return sc_exiting;
1360 gboolean claws_is_starting(void)
1362 return sc_starting;
1366 * CLAWS: want this public so crash dialog can delete the
1367 * lock file too
1369 #ifndef CLAWS
1370 static
1371 #endif
1372 gchar *get_socket_name(void)
1374 static gchar *filename = NULL;
1376 if (filename == NULL) {
1377 filename = g_strdup_printf("%s%cclaws-mail-%d",
1378 g_get_tmp_dir(), G_DIR_SEPARATOR,
1379 #if HAVE_GETUID
1380 getuid());
1381 #else
1382 0);
1383 #endif
1386 return filename;
1389 static gchar *get_crashfile_name(void)
1391 static gchar *filename = NULL;
1393 if (filename == NULL) {
1394 filename = g_strdup_printf("%s%cclaws-crashed",
1395 get_tmp_dir(), G_DIR_SEPARATOR);
1398 return filename;
1401 static gint prohibit_duplicate_launch(void)
1403 gint uxsock;
1404 gchar *path;
1406 path = get_socket_name();
1407 uxsock = fd_connect_unix(path);
1408 if (uxsock < 0) {
1409 g_unlink(path);
1410 return fd_open_unix(path);
1413 /* remote command mode */
1415 debug_print("another Claws Mail instance is already running.\n");
1417 if (cmd.receive_all) {
1418 fd_write_all(uxsock, "receive_all\n", 12);
1419 } else if (cmd.receive) {
1420 fd_write_all(uxsock, "receive\n", 8);
1421 } else if (cmd.compose && cmd.attach_files) {
1422 gchar *str, *compose_str;
1423 gint i;
1425 if (cmd.compose_mailto) {
1426 compose_str = g_strdup_printf("compose_attach %s\n",
1427 cmd.compose_mailto);
1428 } else {
1429 compose_str = g_strdup("compose_attach\n");
1432 fd_write_all(uxsock, compose_str, strlen(compose_str));
1433 g_free(compose_str);
1435 for (i = 0; i < cmd.attach_files->len; i++) {
1436 str = g_ptr_array_index(cmd.attach_files, i);
1437 fd_write_all(uxsock, str, strlen(str));
1438 fd_write_all(uxsock, "\n", 1);
1441 fd_write_all(uxsock, ".\n", 2);
1442 } else if (cmd.compose) {
1443 gchar *compose_str;
1445 if (cmd.compose_mailto) {
1446 compose_str = g_strdup_printf
1447 ("compose %s\n", cmd.compose_mailto);
1448 } else {
1449 compose_str = g_strdup("compose\n");
1452 fd_write_all(uxsock, compose_str, strlen(compose_str));
1453 g_free(compose_str);
1454 } else if (cmd.subscribe) {
1455 gchar *str = g_strdup_printf("subscribe %s\n", cmd.subscribe_uri);
1456 fd_write_all(uxsock, str, strlen(str));
1457 g_free(str);
1458 } else if (cmd.send) {
1459 fd_write_all(uxsock, "send\n", 5);
1460 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
1461 fd_write(uxsock, "online\n", 6);
1462 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
1463 fd_write(uxsock, "offline\n", 7);
1464 } else if (cmd.status || cmd.status_full) {
1465 gchar buf[BUFFSIZE];
1466 gint i;
1467 const gchar *command;
1468 GPtrArray *folders;
1469 gchar *folder;
1471 command = cmd.status_full ? "status-full\n" : "status\n";
1472 folders = cmd.status_full ? cmd.status_full_folders :
1473 cmd.status_folders;
1475 fd_write_all(uxsock, command, strlen(command));
1476 for (i = 0; folders && i < folders->len; ++i) {
1477 folder = g_ptr_array_index(folders, i);
1478 fd_write_all(uxsock, folder, strlen(folder));
1479 fd_write_all(uxsock, "\n", 1);
1481 fd_write_all(uxsock, ".\n", 2);
1482 for (;;) {
1483 fd_gets(uxsock, buf, sizeof(buf));
1484 if (!strncmp(buf, ".\n", 2)) break;
1485 fputs(buf, stdout);
1487 } else if (cmd.exit) {
1488 fd_write_all(uxsock, "exit\n", 5);
1489 } else if (cmd.target) {
1490 gchar *str = g_strdup_printf("select %s\n", cmd.target);
1491 fd_write_all(uxsock, str, strlen(str));
1492 g_free(str);
1493 } else
1494 fd_write_all(uxsock, "popup\n", 6);
1496 fd_close(uxsock);
1497 return -1;
1500 static gint lock_socket_remove(void)
1502 #ifdef G_OS_UNIX
1503 gchar *filename;
1505 if (lock_socket < 0) {
1506 return -1;
1509 if (lock_socket_tag > 0) {
1510 gdk_input_remove(lock_socket_tag);
1512 fd_close(lock_socket);
1513 filename = get_socket_name();
1514 g_unlink(filename);
1515 #endif
1517 return 0;
1520 static GPtrArray *get_folder_item_list(gint sock)
1522 gchar buf[BUFFSIZE];
1523 FolderItem *item;
1524 GPtrArray *folders = NULL;
1526 for (;;) {
1527 fd_gets(sock, buf, sizeof(buf));
1528 if (!strncmp(buf, ".\n", 2)) {
1529 break;
1531 strretchomp(buf);
1532 if (!folders) {
1533 folders = g_ptr_array_new();
1535 item = folder_find_item_from_identifier(buf);
1536 if (item) {
1537 g_ptr_array_add(folders, item);
1538 } else {
1539 g_warning("no such folder: %s\n", buf);
1543 return folders;
1546 static void lock_socket_input_cb(gpointer data,
1547 gint source,
1548 GdkInputCondition condition)
1550 MainWindow *mainwin = (MainWindow *)data;
1551 gint sock;
1552 gchar buf[BUFFSIZE];
1554 sock = fd_accept(source);
1555 fd_gets(sock, buf, sizeof(buf));
1557 if (!strncmp(buf, "popup", 5)) {
1558 main_window_popup(mainwin);
1559 } else if (!strncmp(buf, "receive_all", 11)) {
1560 inc_all_account_mail(mainwin, FALSE,
1561 prefs_common.newmail_notify_manu);
1562 } else if (!strncmp(buf, "receive", 7)) {
1563 inc_mail(mainwin, prefs_common.newmail_notify_manu);
1564 } else if (!strncmp(buf, "compose_attach", 14)) {
1565 GPtrArray *files;
1566 gchar *mailto;
1568 mailto = g_strdup(buf + strlen("compose_attach") + 1);
1569 files = g_ptr_array_new();
1570 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
1571 if (buf[0] == '.' && buf[1] == '\n') {
1572 break;
1574 strretchomp(buf);
1575 g_ptr_array_add(files, g_strdup(buf));
1577 open_compose_new(mailto, files);
1578 ptr_array_free_strings(files);
1579 g_ptr_array_free(files, TRUE);
1580 g_free(mailto);
1581 } else if (!strncmp(buf, "compose", 7)) {
1582 open_compose_new(buf + strlen("compose") + 1, NULL);
1583 } else if (!strncmp(buf, "subscribe", 9)) {
1584 main_window_popup(mainwin);
1585 folder_subscribe(buf + strlen("subscribe") + 1);
1586 } else if (!strncmp(buf, "send", 4)) {
1587 send_queue();
1588 } else if (!strncmp(buf, "online", 6)) {
1589 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1590 } else if (!strncmp(buf, "offline", 7)) {
1591 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1592 } else if (!strncmp(buf, "status-full", 11) ||
1593 !strncmp(buf, "status", 6)) {
1594 gchar *status;
1595 GPtrArray *folders;
1597 folders = get_folder_item_list(sock);
1598 status = folder_get_status
1599 (folders, !strncmp(buf, "status-full", 11));
1600 fd_write_all(sock, status, strlen(status));
1601 fd_write_all(sock, ".\n", 2);
1602 g_free(status);
1603 if (folders) g_ptr_array_free(folders, TRUE);
1604 } else if (!strncmp(buf, "select ", 7)) {
1605 const gchar *target = buf+7;
1606 mainwindow_jump_to(target);
1607 } else if (!strncmp(buf, "exit", 4)) {
1608 app_will_exit(NULL, mainwin);
1611 fd_close(sock);
1614 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
1616 gchar *addr = NULL;
1618 if (address) {
1619 Xstrdup_a(addr, address, return);
1620 g_strstrip(addr);
1623 compose_new(NULL, addr, attach_files);
1626 static void send_queue(void)
1628 GList *list;
1629 gchar *errstr = NULL;
1630 for (list = folder_get_list(); list != NULL; list = list->next) {
1631 Folder *folder = list->data;
1633 if (folder->queue) {
1634 gint res = procmsg_send_queue
1635 (folder->queue, prefs_common.savemsg,
1636 &errstr);
1638 if (res) {
1639 folder_item_scan(folder->queue);
1643 if (errstr) {
1644 gchar *tmp = g_strdup_printf(_("Some errors occurred "
1645 "while sending queued messages:\n%s"), errstr);
1646 g_free(errstr);
1647 alertpanel_error_log(tmp);
1648 g_free(tmp);
1649 } else {
1650 alertpanel_error_log("Some errors occurred "
1651 "while sending queued messages.");
1655 static void quit_signal_handler(int sig)
1657 debug_print("Quitting on signal %d\n", sig);
1659 g_timeout_add(0, clean_quit, NULL);
1662 static void install_basic_sighandlers()
1664 #ifndef G_OS_WIN32
1665 sigset_t mask;
1666 struct sigaction act;
1668 sigemptyset(&mask);
1670 #ifdef SIGTERM
1671 sigaddset(&mask, SIGTERM);
1672 #endif
1673 #ifdef SIGINT
1674 sigaddset(&mask, SIGINT);
1675 #endif
1676 #ifdef SIGHUP
1677 sigaddset(&mask, SIGHUP);
1678 #endif
1680 act.sa_handler = quit_signal_handler;
1681 act.sa_mask = mask;
1682 act.sa_flags = 0;
1684 #ifdef SIGTERM
1685 sigaction(SIGTERM, &act, 0);
1686 #endif
1687 #ifdef SIGINT
1688 sigaction(SIGINT, &act, 0);
1689 #endif
1690 #ifdef SIGHUP
1691 sigaction(SIGHUP, &act, 0);
1692 #endif
1694 sigprocmask(SIG_UNBLOCK, &mask, 0);
1695 #endif /* !G_OS_WIN32 */