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 3 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, see <http://www.gnu.org/licenses/>.
27 #include <glib/gi18n.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtkrc.h>
38 #include <sys/types.h>
43 #include <X11/SM/SMlib.h>
48 #ifdef HAVE_STARTUP_NOTIFICATION
49 # define SN_API_NOT_YET_FROZEN
50 # include <libsn/sn-launchee.h>
51 # include <gdk/gdkx.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_logging.h"
75 #include "prefs_send.h"
76 #include "prefs_wrapping.h"
77 #include "prefs_compose_writing.h"
78 #include "prefs_display_header.h"
83 #include "manage_window.h"
84 #include "alertpanel.h"
85 #include "statusbar.h"
86 #include "addressbook.h"
94 #include "prefs_toolbar.h"
102 #include "imap-thread.h"
103 #include "nntp-thread.h"
105 #include "stock_pixmap.h"
107 #include "valgrind.h"
109 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
121 #include <hildon/hildon-banner.h>
122 #include <hildon/hildon-program.h>
124 #include <hildon-widgets/hildon-banner.h>
125 #include <hildon-widgets/hildon-program.h>
127 #include <gtk/gtkmain.h>
129 #include <libgnomevfs/gnome-vfs-volume.h>
130 #include <libgnomevfs/gnome-vfs-volume-monitor.h>
131 #include <libgnomevfs/gnome-vfs-utils.h>
133 #define OSSO_NAME "claws_mail"
134 #define OSSO_SERVICE "com.nokia."OSSO_NAME
135 #define OSSO_OBJECT "/com/nokia/"OSSO_NAME
136 #define OSSO_IFACE "com.nokia."OSSO_NAME
138 typedef struct _AppData AppData;
140 HildonProgram *program;
141 HildonWindow *window;
142 osso_context_t *osso_context;
145 static GnomeVFSVolumeMonitor *volmon;
152 HildonProgram *hildon_program;
155 #ifdef HAVE_STARTUP_NOTIFICATION
156 static SnLauncheeContext *sn_context = NULL;
157 static SnDisplay *sn_display = NULL;
160 static gint lock_socket = -1;
161 static gint lock_socket_tag = 0;
162 static gchar *x_display = NULL;
165 ONLINE_MODE_DONT_CHANGE,
170 static struct RemoteCmd {
172 gboolean receive_all;
174 const gchar *compose_mailto;
175 GPtrArray *attach_files;
177 gboolean status_full;
178 GPtrArray *status_folders;
179 GPtrArray *status_full_folders;
186 const gchar *subscribe_uri;
190 static void parse_cmd_opt(int argc, char *argv[]);
192 static gint prohibit_duplicate_launch (void);
193 static gchar * get_crashfile_name (void);
194 static gint lock_socket_remove (void);
195 static void lock_socket_input_cb (gpointer data,
197 GdkInputCondition condition);
199 static void open_compose_new (const gchar *address,
200 GPtrArray *attach_files);
202 static void send_queue (void);
203 static void initial_processing (FolderItem *item, gpointer data);
204 static void quit_signal_handler (int sig);
205 static void install_basic_sighandlers (void);
206 static void exit_claws (MainWindow *mainwin);
208 #define MAKE_DIR_IF_NOT_EXIST(dir) \
210 if (!is_dir_exist(dir)) { \
211 if (is_file_exist(dir)) { \
213 (_("File '%s' already exists.\n" \
214 "Can't create folder."), \
218 if (make_dir(dir) < 0) \
223 static MainWindow *static_mainwindow;
226 static osso_context_t *static_osso_context;
228 void exit_event_handler(gboolean die_now, gpointer data)
231 appdata = (AppData *) data;
232 /* Do whatever application needs to do before exiting */
233 exit_claws(static_mainwindow);
234 hildon_banner_show_information(GTK_WIDGET(appdata->window), NULL,
239 /* Callback for normal D-BUS messages */
240 gint dbus_req_handler(const gchar * interface, const gchar * method,
241 GArray * arguments, gpointer data,
245 appdata = (AppData *) data;
247 if (!strcmp(method, "top_application")) {
248 osso_rpc_free_val(retval);
251 osso_system_note_infoprint(appdata->osso_context, method, retval);
252 osso_rpc_free_val(retval);
257 static gboolean emergency_exit = FALSE;
259 #ifdef HAVE_STARTUP_NOTIFICATION
260 static void sn_error_trap_push(SnDisplay *display, Display *xdisplay)
262 gdk_error_trap_push();
265 static void sn_error_trap_pop(SnDisplay *display, Display *xdisplay)
267 gdk_error_trap_pop();
270 static void startup_notification_complete(gboolean with_window)
273 GtkWidget *hack = NULL;
276 /* this is needed to make the startup notification leave,
277 * if we have been launched from a menu.
278 * We have to display a window, so let it be very little */
279 hack = gtk_window_new(GTK_WINDOW_POPUP);
280 gtk_widget_set_uposition(hack, 0, 0);
281 gtk_widget_set_size_request(hack, 1, 1);
282 gtk_widget_show(hack);
285 xdisplay = GDK_DISPLAY();
286 sn_display = sn_display_new(xdisplay,
289 sn_context = sn_launchee_context_new_from_environment(sn_display,
290 DefaultScreen(xdisplay));
292 if (sn_context != NULL) {
293 sn_launchee_context_complete(sn_context);
294 sn_launchee_context_unref(sn_context);
295 sn_display_unref(sn_display);
298 gtk_widget_destroy(hack);
301 #endif /* HAVE_STARTUP_NOTIFICATION */
303 static void claws_gtk_idle(void)
305 while(gtk_events_pending()) {
306 gtk_main_iteration();
311 static gboolean sc_starting = FALSE;
313 static gboolean defer_check_all(void *data)
315 gboolean autochk = GPOINTER_TO_INT(data);
317 inc_all_account_mail(static_mainwindow, autochk,
318 prefs_common.newmail_notify_manu);
322 main_window_set_menu_sensitive(static_mainwindow);
323 toolbar_main_set_sensitive(static_mainwindow);
328 static gboolean defer_check(void *data)
330 inc_mail(static_mainwindow, prefs_common.newmail_notify_manu);
334 main_window_set_menu_sensitive(static_mainwindow);
335 toolbar_main_set_sensitive(static_mainwindow);
340 static gboolean defer_jump(void *data)
342 if (cmd.receive_all) {
343 defer_check_all(GINT_TO_POINTER(FALSE));
344 } else if (prefs_common.chk_on_startup) {
345 defer_check_all(GINT_TO_POINTER(TRUE));
346 } else if (cmd.receive) {
349 mainwindow_jump_to(data, FALSE);
352 main_window_set_menu_sensitive(static_mainwindow);
353 toolbar_main_set_sensitive(static_mainwindow);
358 static void chk_update_val(GtkWidget *widget, gpointer data)
360 gboolean *val = (gboolean *)data;
361 *val = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
364 static gboolean migrate_old_config(const gchar *old_cfg_dir, const gchar *new_cfg_dir, const gchar *oldversion)
366 gchar *message = g_strdup_printf(_("Configuration for %s found.\n"
367 "Do you want to migrate this configuration?"), oldversion);
368 gchar *message2 = g_strdup_printf(_("\n\nYour Sylpheed filtering rules can be converted by a\n"
369 "script available at %s."), TOOLS_URI);
371 if (!strcmp(oldversion, "Sylpheed"))
372 message = g_strconcat(message, message2, NULL);
376 GtkWidget *window = NULL;
377 GtkWidget *keep_backup_chk;
378 GtkTooltips *tips = gtk_tooltips_new();
379 gboolean backup = TRUE;
381 keep_backup_chk = gtk_check_button_new_with_label (_("Keep old configuration"));
382 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_backup_chk), TRUE);
383 gtk_tooltips_set_tip(GTK_TOOLTIPS(tips), keep_backup_chk,
384 _("Keeping a backup will allow you to go back to an "
385 "older version, but may take a while if you have "
386 "cached IMAP or News data, and will take some extra "
387 "room on your disk."),
390 g_signal_connect(G_OBJECT(keep_backup_chk), "toggled",
391 G_CALLBACK(chk_update_val), &backup);
393 if (alertpanel_full(_("Migration of configuration"), message,
394 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL, FALSE,
395 keep_backup_chk, ALERT_QUESTION, G_ALERTDEFAULT) != G_ALERTALTERNATE) {
399 /* we can either do a fast migration requiring not any extra disk
400 * space, or a slow one that copies the old configuration and leaves
404 window = label_window_create(_("Copying configuration... This may take a while..."));
407 r = copy_dir(old_cfg_dir, new_cfg_dir);
408 label_window_destroy(window);
410 /* if copy failed, we'll remove the partially copied
413 alertpanel_error(_("Migration failed!"));
414 remove_dir_recursive(new_cfg_dir);
417 /* fast mode failed, but we don't want backup */
418 remove_dir_recursive(old_cfg_dir);
422 window = label_window_create(_("Migrating configuration..."));
425 r = g_rename(old_cfg_dir, new_cfg_dir);
426 label_window_destroy(window);
428 /* if g_rename failed, we'll try to copy */
430 FILE_OP_ERROR(new_cfg_dir, "g_rename failed, trying copy\n");
437 static int migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
440 gchar *plugin_path, *old_plugin_path, *new_plugin_path;
442 gboolean err = FALSE;
444 oldfp = g_fopen(old_rc, "r");
447 newfp = g_fopen(new_rc, "w");
453 plugin_path = g_strdup(get_plugin_dir());
454 new_plugin_path = g_strdup(plugin_path);
456 if (strstr(plugin_path, "/claws-mail/")) {
457 gchar *end = g_strdup(strstr(plugin_path, "/claws-mail/")+strlen("/claws-mail/"));
458 *(strstr(plugin_path, "/claws-mail/")) = '\0';
459 old_plugin_path = g_strconcat(plugin_path, "/sylpheed-claws/", end, NULL);
462 old_plugin_path = g_strdup(new_plugin_path);
464 debug_print("replacing %s with %s\n", old_plugin_path, new_plugin_path);
465 while (fgets(buf, sizeof(buf), oldfp)) {
466 if (strncmp(buf, old_plugin_path, strlen(old_plugin_path))) {
467 err |= (fputs(buf, newfp) == EOF);
469 debug_print("->replacing %s", buf);
470 debug_print(" with %s%s", new_plugin_path, buf+strlen(old_plugin_path));
471 err |= (fputs(new_plugin_path, newfp) == EOF);
472 err |= (fputs(buf+strlen(old_plugin_path), newfp) == EOF);
476 g_free(new_plugin_path);
477 g_free(old_plugin_path);
479 if (fclose(newfp) == EOF)
487 sc_client_set_value (MainWindow *mainwin,
498 prop.num_vals = num_vals;
502 if (mainwin->smc_conn)
503 SmcSetProperties ((SmcConn) mainwin->smc_conn, 1, proplist);
506 static void sc_die_callback (SmcConn smc_conn, SmPointer client_data)
511 static void sc_save_complete_callback(SmcConn smc_conn, SmPointer client_data)
515 static void sc_shutdown_cancelled_callback (SmcConn smc_conn, SmPointer client_data)
517 MainWindow *mainwin = (MainWindow *)client_data;
518 if (mainwin->smc_conn)
519 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
522 static void sc_save_yourself_callback (SmcConn smc_conn,
523 SmPointer client_data,
529 MainWindow *mainwin = (MainWindow *)client_data;
530 if (mainwin->smc_conn)
531 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
534 static IceIOErrorHandler sc_ice_installed_handler;
536 static void sc_ice_io_error_handler (IceConn connection)
538 if (sc_ice_installed_handler)
539 (*sc_ice_installed_handler) (connection);
541 static gboolean sc_process_ice_messages (GIOChannel *source,
542 GIOCondition condition,
545 IceConn connection = (IceConn) data;
546 IceProcessMessagesStatus status;
548 status = IceProcessMessages (connection, NULL, NULL);
550 if (status == IceProcessMessagesIOError) {
551 IcePointer context = IceGetConnectionContext (connection);
553 if (context && GTK_IS_OBJECT (context)) {
554 guint disconnect_id = g_signal_lookup ("disconnect", G_OBJECT_TYPE (context));
556 if (disconnect_id > 0)
557 g_signal_emit (context, disconnect_id, 0);
559 IceSetShutdownNegotiation (connection, False);
560 IceCloseConnection (connection);
567 static void new_ice_connection (IceConn connection, IcePointer client_data, Bool opening,
568 IcePointer *watch_data)
574 /* Make sure we don't pass on these file descriptors to any
576 fcntl(IceConnectionNumber(connection),F_SETFD,
577 fcntl(IceConnectionNumber(connection),F_GETFD,0) | FD_CLOEXEC);
579 channel = g_io_channel_unix_new (IceConnectionNumber (connection));
580 input_id = g_io_add_watch (channel,
581 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
582 sc_process_ice_messages,
584 g_io_channel_unref (channel);
586 *watch_data = (IcePointer) GUINT_TO_POINTER (input_id);
588 input_id = GPOINTER_TO_UINT ((gpointer) *watch_data);
589 g_source_remove (input_id);
593 static void sc_session_manager_connect(MainWindow *mainwin)
595 static gboolean connected = FALSE;
596 SmcCallbacks callbacks;
598 IceIOErrorHandler default_handler;
605 sc_ice_installed_handler = IceSetIOErrorHandler (NULL);
606 default_handler = IceSetIOErrorHandler (sc_ice_io_error_handler);
608 if (sc_ice_installed_handler == default_handler)
609 sc_ice_installed_handler = NULL;
611 IceAddConnectionWatch (new_ice_connection, NULL);
614 callbacks.save_yourself.callback = sc_save_yourself_callback;
615 callbacks.die.callback = sc_die_callback;
616 callbacks.save_complete.callback = sc_save_complete_callback;
617 callbacks.shutdown_cancelled.callback = sc_shutdown_cancelled_callback;
619 callbacks.save_yourself.client_data =
620 callbacks.die.client_data =
621 callbacks.save_complete.client_data =
622 callbacks.shutdown_cancelled.client_data = (SmPointer) mainwin;
623 if (g_getenv ("SESSION_MANAGER")) {
624 gchar error_string_ret[256] = "";
626 mainwin->smc_conn = (gpointer)
627 SmcOpenConnection (NULL, mainwin,
628 SmProtoMajor, SmProtoMinor,
629 SmcSaveYourselfProcMask | SmcDieProcMask |
630 SmcSaveCompleteProcMask |
631 SmcShutdownCancelledProcMask,
634 256, error_string_ret);
636 if (error_string_ret[0] || mainwin->smc_conn == NULL)
637 g_warning ("While connecting to session manager:\n%s.",
641 vals = g_new (SmPropValue, 1);
642 vals[0].length = strlen(argv0);
643 vals[0].value = argv0;
644 sc_client_set_value (mainwin, SmCloneCommand, SmLISTofARRAY8, 1, vals);
645 sc_client_set_value (mainwin, SmRestartCommand, SmLISTofARRAY8, 1, vals);
646 sc_client_set_value (mainwin, SmProgram, SmARRAY8, 1, vals);
648 vals[0].length = strlen(g_get_user_name()?g_get_user_name():"");
649 vals[0].value = g_strdup(g_get_user_name()?g_get_user_name():"");
650 sc_client_set_value (mainwin, SmUserID, SmARRAY8, 1, vals);
656 static gboolean sc_exiting = FALSE;
657 static gboolean show_at_startup = TRUE;
658 static gboolean claws_crashed_bool = FALSE;
660 gboolean claws_crashed(void) {
661 return claws_crashed_bool;
664 void main_set_show_at_startup(gboolean show)
666 show_at_startup = show;
670 static void main_vol_mount_cb(GnomeVFSVolumeMonitor *vfs, GnomeVFSVolume *vol, MainWindow *mainwin)
672 gchar *uri = gnome_vfs_volume_get_activation_uri (vol);
673 gchar *mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
676 if(!strcmp(mount_path, prefs_common.data_root)) {
677 gtk_widget_set_sensitive(mainwin->window, TRUE);
683 static void main_vol_unmount_cb(GnomeVFSVolumeMonitor *vfs, GnomeVFSVolume *vol, MainWindow *mainwin)
685 gchar *uri = gnome_vfs_volume_get_activation_uri (vol);
686 gchar *mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
689 if(!strcmp(mount_path, prefs_common.data_root)) {
690 gtk_widget_set_sensitive(mainwin->window, FALSE);
699 static FILE* win32_debug_fp=NULL;
700 static guint win32_log_handler_app_id;
701 static guint win32_log_handler_glib_id;
702 static guint win32_log_handler_gtk_id;
704 static void win32_print_stdout(const gchar *string)
706 if (win32_debug_fp) {
707 fprintf(win32_debug_fp, string);
708 fflush(win32_debug_fp);
712 static void win32_print_stderr(const gchar *string)
714 if (win32_debug_fp) {
715 fprintf(win32_debug_fp, string);
716 fflush(win32_debug_fp);
720 static void win32_log(const gchar *log_domain, GLogLevelFlags log_level, const gchar* message, gpointer user_data)
722 if (win32_debug_fp) {
725 switch(log_level & G_LOG_LEVEL_MASK)
727 case G_LOG_LEVEL_ERROR:
730 case G_LOG_LEVEL_CRITICAL:
733 case G_LOG_LEVEL_WARNING:
736 case G_LOG_LEVEL_MESSAGE:
739 case G_LOG_LEVEL_INFO:
742 case G_LOG_LEVEL_DEBUG:
749 fprintf(win32_debug_fp, "%s: %s: %s", log_domain, type, message);
751 fprintf(win32_debug_fp, "%s: %s", type, message);
752 fflush(win32_debug_fp);
756 static void win32_open_log(void)
758 if (is_file_exist("claws-win32.log")) {
759 if (rename_force("claws-win32.log", "claws-win32.log.bak") < 0)
760 FILE_OP_ERROR("claws-win32.log", "rename");
762 win32_debug_fp = fopen("claws-win32.log", "w");
765 g_set_print_handler(win32_print_stdout);
766 g_set_printerr_handler(win32_print_stdout);
767 win32_log_handler_app_id = g_log_set_handler(NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
768 | G_LOG_FLAG_RECURSION, win32_log, NULL);
769 win32_log_handler_glib_id = g_log_set_handler("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
770 | G_LOG_FLAG_RECURSION, win32_log, NULL);
771 win32_log_handler_gtk_id = g_log_set_handler("Gtk", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
772 | G_LOG_FLAG_RECURSION, win32_log, NULL);
776 static void win32_close_log(void)
780 g_log_remove_handler("", win32_log_handler_app_id);
781 g_log_remove_handler("GLib", win32_log_handler_glib_id);
782 g_log_remove_handler("Gtk", win32_log_handler_gtk_id);
783 fclose(win32_debug_fp);
789 int main(int argc, char *argv[])
792 osso_context_t *osso_context;
793 osso_return_t result;
797 FolderView *folderview;
799 gboolean crash_file_present = FALSE;
800 gint num_folder_class = 0;
801 gboolean asked_for_migration = FALSE;
802 gboolean start_done = TRUE;
804 START_TIMING("startup");
811 if (!claws_init(&argc, &argv)) {
818 debug_print("GTK+ %d.%d.%d / GLib %d.%d.%d\n",
819 gtk_major_version, gtk_minor_version, gtk_micro_version,
820 glib_major_version, glib_minor_version, glib_micro_version);
821 debug_print("Compiled-in features:\n");
823 debug_print(" compface\n");
826 debug_print(" aspell\n");
829 debug_print(" gnutls\n");
832 debug_print(" ipv6\n");
835 debug_print(" iconv\n");
838 debug_print(" jpilot\n");
841 debug_print(" ldap\n");
844 debug_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
847 debug_print(" gnomeprint\n");
850 debug_print(" libsm\n");
853 debug_print(" openssl\n");
856 prog_version = PROG_VERSION;
857 argv0 = g_strdup(argv[0]);
859 parse_cmd_opt(argc, argv);
861 prefs_prepare_cache();
866 gtk_init(&argc, &argv);
867 crash_main(cmd.crash_params);
873 crash_install_handlers();
875 install_basic_sighandlers();
878 /* check and create unix domain socket for remote operation */
880 lock_socket = prohibit_duplicate_launch();
881 if (lock_socket < 0) {
882 #ifdef HAVE_STARTUP_NOTIFICATION
883 if(gtk_init_check(&argc, &argv))
884 startup_notification_complete(TRUE);
889 if (cmd.status || cmd.status_full) {
890 puts("0 Claws Mail not running.");
891 lock_socket_remove();
898 if (!g_thread_supported())
902 gtk_init(&argc, &argv);
905 osso_context = osso_initialize(OSSO_SERVICE, "2.8.1", TRUE, NULL);
906 if (osso_context == NULL) {
909 hildon_program = HILDON_PROGRAM(hildon_program_get_instance());
910 static_osso_context = osso_context;
913 gtk_widget_set_default_colormap(gdk_rgb_get_colormap());
914 gtk_widget_set_default_visual(gdk_rgb_get_visual());
916 if (!g_thread_supported()) {
917 g_error(_("g_thread is not supported by glib.\n"));
920 /* check that we're not on a too recent/old gtk+ */
921 #if GTK_CHECK_VERSION(2, 9, 0)
922 if (gtk_check_version(2, 9, 0) != NULL) {
923 alertpanel_error(_("Claws Mail has been compiled with "
924 "a more recent GTK+ library than is "
925 "currently available. This will cause "
926 "crashes. You need to upgrade GTK+ or "
927 "recompile Claws Mail."));
934 if (gtk_check_version(2, 9, 0) == NULL) {
935 alertpanel_error(_("Claws Mail has been compiled with "
936 "an older GTK+ library than is "
937 "currently available. This will cause "
938 "crashes. You need to recompile "
946 /* parse gtkrc files */
947 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc-2.0",
949 gtk_rc_parse(userrc);
951 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
952 G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
953 gtk_rc_parse(userrc);
957 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
959 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
962 /* no config dir exists. See if we can migrate an old config. */
963 if (!is_dir_exist(RC_DIR)) {
964 prefs_destroy_cache();
967 /* if one of the old dirs exist, we'll ask if the user
968 * want to migrates, and r will be TRUE if he said yes
969 * and migration succeeded, and FALSE otherwise.
971 if (is_dir_exist(OLD_GTK2_RC_DIR)) {
972 r = migrate_old_config(OLD_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 2.6.0 (or older)"));
973 asked_for_migration = TRUE;
974 } else if (is_dir_exist(OLDER_GTK2_RC_DIR)) {
975 r = migrate_old_config(OLDER_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.9.15 (or older)"));
976 asked_for_migration = TRUE;
977 } else if (is_dir_exist(OLD_GTK1_RC_DIR)) {
978 r = migrate_old_config(OLD_GTK1_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.0.5 (or older)"));
979 asked_for_migration = TRUE;
980 } else if (is_dir_exist(SYLPHEED_RC_DIR)) {
981 r = migrate_old_config(SYLPHEED_RC_DIR, RC_DIR, "Sylpheed");
982 asked_for_migration = TRUE;
985 /* If migration failed or the user didn't want to do it,
986 * we create a new one (and we'll hit wizard later).
988 if (r == FALSE && !is_dir_exist(RC_DIR) && make_dir(RC_DIR) < 0) {
997 if (!is_file_exist(RC_DIR G_DIR_SEPARATOR_S COMMON_RC) &&
998 is_file_exist(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC)) {
999 /* post 2.6 name change */
1000 migrate_common_rc(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC,
1001 RC_DIR G_DIR_SEPARATOR_S COMMON_RC);
1005 plugin_load_all("Common");
1007 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
1008 gtk_rc_parse(userrc);
1011 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1012 gtk_accel_map_load (userrc);
1016 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_rc_dir(), 1, win32_close_log(););
1018 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
1021 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
1022 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
1023 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
1024 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
1025 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
1026 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR);
1028 crash_file_present = is_file_exist(get_crashfile_name());
1029 /* remove temporary files */
1030 remove_all_files(get_tmp_dir());
1031 remove_all_files(get_mime_tmp_dir());
1033 if (!cmd.crash && crash_file_present)
1034 claws_crashed_bool = TRUE;
1036 if (is_file_exist("claws.log")) {
1037 if (rename_force("claws.log", "claws.log.bak") < 0)
1038 FILE_OP_ERROR("claws.log", "rename");
1040 set_log_file(LOG_PROTOCOL, "claws.log");
1042 if (is_file_exist("filtering.log")) {
1043 if (rename_force("filtering.log", "filtering.log.bak") < 0)
1044 FILE_OP_ERROR("filtering.log", "rename");
1046 set_log_file(LOG_DEBUG_FILTERING, "filtering.log");
1049 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1051 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1054 folder_system_init();
1055 prefs_common_read_config();
1057 prefs_themes_init();
1059 prefs_ext_prog_init();
1060 prefs_wrapping_init();
1061 prefs_compose_writing_init();
1062 prefs_msg_colors_init();
1063 image_viewer_init();
1064 prefs_image_viewer_init();
1066 prefs_summaries_init();
1067 prefs_message_init();
1069 prefs_logging_init();
1070 prefs_receive_init();
1074 gtkaspell_checkers_init();
1075 prefs_spelling_init();
1078 sock_set_io_timeout(prefs_common.io_timeout_secs);
1079 prefs_actions_read_config();
1080 prefs_display_header_read_config();
1081 /* prefs_filtering_read_config(); */
1082 addressbook_read_file();
1084 gtkut_widget_init();
1085 stock_pixbuf_gdk(NULL, STOCK_PIXMAP_CLAWS_MAIL_ICON, &icon);
1086 gtk_window_set_default_icon(icon);
1088 folderview_initialize();
1094 mainwin = main_window_create();
1097 appdata = g_new0(AppData, 1);
1098 appdata->program = hildon_program;
1099 appdata->window = HILDON_WINDOW(mainwin->window);
1100 appdata->osso_context = osso_context;
1101 result = osso_rpc_set_cb_f(appdata->osso_context,
1105 dbus_req_handler, appdata);
1106 if (result != OSSO_OK) {
1111 /* Add handler for Exit D-BUS messages */
1112 result = osso_application_set_exit_cb(appdata->osso_context,
1114 (gpointer) appdata);
1115 if (result != OSSO_OK) {
1120 manage_window_focus_in(mainwin->window, NULL, NULL);
1121 folderview = mainwin->folderview;
1123 gtk_clist_freeze(GTK_CLIST(mainwin->folderview->ctree));
1124 folder_item_update_freeze();
1126 /* register the callback of unix domain socket input */
1128 lock_socket_tag = gdk_input_add(lock_socket,
1129 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
1130 lock_socket_input_cb,
1134 prefs_account_init();
1135 account_read_config_all();
1137 /* If we can't read a folder list or don't have accounts,
1138 * it means the configuration's not done. Either this is
1139 * a brand new install, either a failed/refused migration.
1140 * So we'll start the wizard.
1142 if (folder_read_list() < 0) {
1143 prefs_destroy_cache();
1145 /* if run_wizard returns FALSE it's because it's
1146 * been cancelled. We can't do much but exit.
1147 * however, if the user was asked for a migration,
1148 * we remove the newly created directory so that
1149 * he's asked again for migration on next launch.*/
1150 if (!run_wizard(mainwin, TRUE)) {
1151 if (asked_for_migration)
1152 remove_dir_recursive(RC_DIR);
1158 main_window_reflect_prefs_all_now();
1159 folder_write_list();
1162 if (!account_get_list()) {
1163 prefs_destroy_cache();
1164 if (!run_wizard(mainwin, FALSE)) {
1165 if (asked_for_migration)
1166 remove_dir_recursive(RC_DIR);
1172 if(!account_get_list()) {
1173 exit_claws(mainwin);
1182 toolbar_main_set_sensitive(mainwin);
1183 main_window_set_menu_sensitive(mainwin);
1185 /* if crashed, show window early so that the user
1186 * sees what's happening */
1187 if (claws_crashed())
1188 main_window_popup(mainwin);
1190 #ifdef HAVE_LIBETPAN
1191 imap_main_init(prefs_common.skip_ssl_cert_check);
1192 imap_main_set_timeout(prefs_common.io_timeout_secs);
1193 nntp_main_init(prefs_common.skip_ssl_cert_check);
1195 account_set_missing_folder();
1196 folder_set_missing_folders();
1197 folderview_set(folderview);
1199 prefs_matcher_read_config();
1201 /* make one all-folder processing before using claws */
1202 main_window_cursor_wait(mainwin);
1203 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
1205 /* if claws crashed, rebuild caches */
1206 if (claws_crashed()) {
1208 debug_print("Claws Mail crashed, checking for new messages in local folders\n");
1209 folder_item_update_thaw();
1210 folderview_check_new(NULL);
1211 folder_clean_cache_memory_force();
1212 folder_item_update_freeze();
1214 /* make the crash-indicator file */
1215 str_write_to_file("foo", get_crashfile_name());
1217 inc_autocheck_timer_init(mainwin);
1219 /* ignore SIGPIPE signal for preventing sudden death of program */
1221 signal(SIGPIPE, SIG_IGN);
1223 if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
1224 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1226 if (cmd.online_mode == ONLINE_MODE_ONLINE) {
1227 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1230 if (cmd.status_folders) {
1231 g_ptr_array_free(cmd.status_folders, TRUE);
1232 cmd.status_folders = NULL;
1234 if (cmd.status_full_folders) {
1235 g_ptr_array_free(cmd.status_full_folders, TRUE);
1236 cmd.status_full_folders = NULL;
1239 claws_register_idle_function(claws_gtk_idle);
1241 prefs_toolbar_init();
1243 num_folder_class = g_list_length(folder_get_list());
1245 plugin_load_all("GTK2");
1247 if (g_list_length(folder_get_list()) != num_folder_class) {
1248 debug_print("new folders loaded, reloading processing rules\n");
1249 prefs_matcher_read_config();
1252 if (plugin_get_unloaded_list() != NULL) {
1253 main_window_cursor_normal(mainwin);
1254 alertpanel_warning(_("Some plugin(s) failed to load. "
1255 "Check the Plugins configuration "
1256 "for more information."));
1257 main_window_cursor_wait(mainwin);
1260 plugin_load_standard_plugins ();
1262 /* if not crashed, show window now */
1263 if (!claws_crashed()) {
1264 /* apart if something told not to show */
1265 if (show_at_startup)
1266 main_window_popup(mainwin);
1269 if (!folder_have_mailbox()) {
1270 prefs_destroy_cache();
1271 main_window_cursor_normal(mainwin);
1272 if (folder_get_list() != NULL) {
1273 alertpanel_error(_("Claws Mail has detected a configured "
1274 "mailbox, but it is incomplete. It is "
1275 "possibly due to a failing IMAP account. Use "
1276 "\"Rebuild folder tree\" on the mailbox parent "
1277 "folder's context menu to try to fix it."));
1279 alertpanel_error(_("Claws Mail has detected a configured "
1280 "mailbox, but could not load it. It is "
1281 "probably provided by an out-of-date "
1282 "external plugin. Please reinstall the "
1283 "plugin and try again."));
1284 exit_claws(mainwin);
1292 static_mainwindow = mainwin;
1295 if (prefs_common.data_root != NULL && *prefs_common.data_root != '\0') {
1296 GnomeVFSVolume *vol = NULL;
1297 gchar *uri, *mount_path;
1299 volmon = gnome_vfs_get_volume_monitor();
1300 vol = gnome_vfs_volume_monitor_get_volume_for_path(volmon, prefs_common.data_root);
1302 uri = gnome_vfs_volume_get_activation_uri (vol);
1303 mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
1306 if (vol == NULL || !gnome_vfs_volume_is_mounted(vol)
1307 || strcmp(mount_path, prefs_common.data_root)) {
1308 alertpanel_error(_("Claws Mail can not start without its data volume (%s)."),
1309 prefs_common.data_root);
1311 gnome_vfs_volume_unref(vol);
1312 exit_claws(mainwin);
1316 gnome_vfs_volume_unref(vol);
1317 g_signal_connect(G_OBJECT(volmon),
1318 "volume-mounted", G_CALLBACK(main_vol_mount_cb), mainwin);
1319 g_signal_connect(G_OBJECT(volmon),
1320 "volume-unmounted", G_CALLBACK(main_vol_unmount_cb), mainwin);
1324 #ifdef HAVE_STARTUP_NOTIFICATION
1325 startup_notification_complete(FALSE);
1328 sc_session_manager_connect(mainwin);
1330 folder_item_update_thaw();
1331 gtk_clist_thaw(GTK_CLIST(mainwin->folderview->ctree));
1332 main_window_cursor_normal(mainwin);
1334 if (!cmd.target && prefs_common.goto_last_folder_on_startup &&
1335 folder_find_item_from_identifier(prefs_common.last_opened_folder) != NULL &&
1337 cmd.target = prefs_common.last_opened_folder;
1340 if (cmd.receive_all && !cmd.target) {
1342 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(FALSE));
1343 } else if (prefs_common.chk_on_startup && !cmd.target) {
1345 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(TRUE));
1346 } else if (cmd.receive && !cmd.target) {
1348 g_timeout_add(1000, defer_check, NULL);
1350 gtk_widget_grab_focus(folderview->ctree);
1354 open_compose_new(cmd.compose_mailto, cmd.attach_files);
1356 if (cmd.attach_files) {
1357 ptr_array_free_strings(cmd.attach_files);
1358 g_ptr_array_free(cmd.attach_files, TRUE);
1359 cmd.attach_files = NULL;
1361 if (cmd.subscribe) {
1362 folder_subscribe(cmd.subscribe_uri);
1371 g_timeout_add(500, defer_jump, (gpointer)cmd.target);
1374 prefs_destroy_cache();
1376 compose_reopen_exit_drafts();
1379 sc_starting = FALSE;
1380 main_window_set_menu_sensitive(mainwin);
1381 toolbar_main_set_sensitive(mainwin);
1388 osso_deinitialize(osso_context);
1393 exit_claws(mainwin);
1398 static void save_all_caches(FolderItem *item, gpointer data)
1405 folder_item_close(item);
1408 folder_item_free_cache(item, TRUE);
1411 static void exit_claws(MainWindow *mainwin)
1417 debug_print("shutting down\n");
1418 inc_autocheck_timer_remove();
1420 if (prefs_common.clean_on_exit && !emergency_exit) {
1421 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
1424 /* save prefs for opened folder */
1425 if(mainwin->folderview->opened) {
1428 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
1429 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
1430 prefs_common.last_opened_folder = folder_item_get_identifier(item);
1433 /* save all state before exiting */
1434 folder_func_to_all_folders(save_all_caches, NULL);
1435 folder_write_list();
1437 main_window_get_size(mainwin);
1438 main_window_get_position(mainwin);
1440 prefs_common_write_config();
1441 account_write_config_all();
1442 addressbook_export_to_file();
1444 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1445 gtk_accel_map_save(filename);
1448 /* delete temporary files */
1449 remove_all_files(get_tmp_dir());
1450 remove_all_files(get_mime_tmp_dir());
1452 close_log_file(LOG_PROTOCOL);
1453 close_log_file(LOG_DEBUG_FILTERING);
1455 #ifdef HAVE_LIBETPAN
1459 /* delete crashfile */
1461 g_unlink(get_crashfile_name());
1463 lock_socket_remove();
1466 if (mainwin->smc_conn)
1467 SmcCloseConnection ((SmcConn)mainwin->smc_conn, 0, NULL);
1468 mainwin->smc_conn = NULL;
1471 main_window_destroy_all();
1473 plugin_unload_all("GTK2");
1475 prefs_toolbar_done();
1477 addressbook_destroy();
1479 prefs_themes_done();
1481 prefs_ext_prog_done();
1482 prefs_wrapping_done();
1483 prefs_compose_writing_done();
1484 prefs_msg_colors_done();
1485 prefs_image_viewer_done();
1486 image_viewer_done();
1488 prefs_summaries_done();
1489 prefs_message_done();
1491 prefs_receive_done();
1492 prefs_logging_done();
1496 prefs_spelling_done();
1497 gtkaspell_checkers_quit();
1499 plugin_unload_all("Common");
1503 static void parse_cmd_opt(int argc, char *argv[])
1507 for (i = 1; i < argc; i++) {
1508 if (!strncmp(argv[i], "--receive-all", 13)) {
1509 cmd.receive_all = TRUE;
1510 } else if (!strncmp(argv[i], "--receive", 9)) {
1512 } else if (!strncmp(argv[i], "--compose", 9)) {
1513 const gchar *p = argv[i + 1];
1516 cmd.compose_mailto = NULL;
1517 if (p && *p != '\0' && *p != '-') {
1518 if (!strncmp(p, "mailto:", 7)) {
1519 cmd.compose_mailto = p + 7;
1521 cmd.compose_mailto = p;
1525 } else if (!strncmp(argv[i], "--subscribe", 11)) {
1526 const gchar *p = argv[i + 1];
1527 if (p && *p != '\0' && *p != '-') {
1528 cmd.subscribe = TRUE;
1529 cmd.subscribe_uri = p;
1531 } else if (!strncmp(argv[i], "--attach", 8)) {
1532 const gchar *p = argv[i + 1];
1535 while (p && *p != '\0' && *p != '-') {
1536 if (!cmd.attach_files) {
1537 cmd.attach_files = g_ptr_array_new();
1539 if ((file = g_filename_from_uri(p, NULL, NULL)) != NULL) {
1540 if (!is_file_exist(file)) {
1545 if (file == NULL && *p != G_DIR_SEPARATOR) {
1546 file = g_strconcat(claws_get_startup_dir(),
1549 } else if (file == NULL) {
1552 g_ptr_array_add(cmd.attach_files, file);
1556 } else if (!strncmp(argv[i], "--send", 6)) {
1558 } else if (!strncmp(argv[i], "--version", 9) ||
1559 !strncmp(argv[i], "-v", 2)) {
1560 g_print("Claws Mail version " VERSION "\n");
1562 } else if (!strncmp(argv[i], "--status-full", 13)) {
1563 const gchar *p = argv[i + 1];
1565 cmd.status_full = TRUE;
1566 while (p && *p != '\0' && *p != '-') {
1567 if (!cmd.status_full_folders) {
1568 cmd.status_full_folders =
1571 g_ptr_array_add(cmd.status_full_folders,
1576 } else if (!strncmp(argv[i], "--status", 8)) {
1577 const gchar *p = argv[i + 1];
1580 while (p && *p != '\0' && *p != '-') {
1581 if (!cmd.status_folders)
1582 cmd.status_folders = g_ptr_array_new();
1583 g_ptr_array_add(cmd.status_folders,
1588 } else if (!strncmp(argv[i], "--online", 8)) {
1589 cmd.online_mode = ONLINE_MODE_ONLINE;
1590 } else if (!strncmp(argv[i], "--offline", 9)) {
1591 cmd.online_mode = ONLINE_MODE_OFFLINE;
1592 } else if (!strncmp(argv[i], "--help", 6) ||
1593 !strncmp(argv[i], "-h", 2)) {
1594 gchar *base = g_path_get_basename(argv[0]);
1595 g_print(_("Usage: %s [OPTION]...\n"), base);
1597 g_print("%s\n", _(" --compose [address] open composition window"));
1598 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
1599 g_print("%s\n", _(" --attach file1 [file2]...\n"
1600 " open composition window with specified files\n"
1602 g_print("%s\n", _(" --receive receive new messages"));
1603 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
1604 g_print("%s\n", _(" --send send all queued messages"));
1605 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
1606 g_print("%s\n", _(" --status-full [folder]...\n"
1607 " show the status of each folder"));
1608 g_print("%s\n", _(" --select folder[/msg] jumps to the specified folder/message\n"
1609 " folder is a folder id like 'folder/sub_folder'"));
1610 g_print("%s\n", _(" --online switch to online mode"));
1611 g_print("%s\n", _(" --offline switch to offline mode"));
1612 g_print("%s\n", _(" --exit --quit -q exit Claws Mail"));
1613 g_print("%s\n", _(" --debug debug mode"));
1614 g_print("%s\n", _(" --help -h display this help and exit"));
1615 g_print("%s\n", _(" --version -v output version information and exit"));
1616 g_print("%s\n", _(" --config-dir output configuration directory"));
1617 g_print("%s\n", _(" --alternate-config-dir [dir]\n"
1618 " use specified configuration directory"));
1622 } else if (!strncmp(argv[i], "--crash", 7)) {
1624 cmd.crash_params = g_strdup(argv[i + 1]);
1626 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
1627 g_print(RC_DIR "\n");
1629 } else if (!strncmp(argv[i], "--alternate-config-dir", sizeof "--alternate-config-dir" - 1) && i+1 < argc) {
1630 set_rc_dir(argv[i+1]);
1631 } else if (!strncmp(argv[i], "--exit", 6) ||
1632 !strncmp(argv[i], "--quit", 6) ||
1633 !strncmp(argv[i], "-q", 2)) {
1635 } else if (!strncmp(argv[i], "--select", 8) && i+1 < argc) {
1636 cmd.target = argv[i+1];
1637 } else if (i == 1 && argc == 2) {
1638 /* only one parameter. Do something intelligent about it */
1639 if (strstr(argv[i], "@") && !strstr(argv[i], "://")) {
1640 const gchar *p = argv[i];
1643 cmd.compose_mailto = NULL;
1644 if (p && *p != '\0' && *p != '-') {
1645 if (!strncmp(p, "mailto:", 7)) {
1646 cmd.compose_mailto = p + 7;
1648 cmd.compose_mailto = p;
1651 } else if (strstr(argv[i], "://")) {
1652 const gchar *p = argv[i];
1653 if (p && *p != '\0' && *p != '-') {
1654 cmd.subscribe = TRUE;
1655 cmd.subscribe_uri = p;
1657 } else if (!strcmp(argv[i], "--sync")) {
1660 g_print(_("Unknown option\n"));
1666 if (cmd.attach_files && cmd.compose == FALSE) {
1668 cmd.compose_mailto = NULL;
1672 static void initial_processing(FolderItem *item, gpointer data)
1674 MainWindow *mainwin = (MainWindow *)data;
1677 g_return_if_fail(item);
1678 buf = g_strdup_printf(_("Processing (%s)..."),
1681 : _("top level folder"));
1685 if (item->prefs->enable_processing) {
1686 item->processing_pending = TRUE;
1687 folder_item_apply_processing(item);
1688 item->processing_pending = FALSE;
1691 STATUSBAR_POP(mainwin);
1694 static gboolean draft_all_messages(void)
1696 GList *compose_list = NULL;
1698 compose_clear_exit_drafts();
1699 compose_list = compose_get_compose_list();
1700 while (compose_list != NULL) {
1701 Compose *c = (Compose*)compose_list->data;
1702 if (!compose_draft(c, COMPOSE_DRAFT_FOR_EXIT))
1704 compose_list = compose_get_compose_list();
1708 gboolean clean_quit(gpointer data)
1710 static gboolean firstrun = TRUE;
1717 /*!< Good idea to have the main window stored in a
1718 * static variable so we can check that variable
1719 * to see if we're really allowed to do things
1720 * that actually the spawner is supposed to
1721 * do (like: sending mail, composing messages).
1722 * Because, really, if we're the spawnee, and
1723 * we touch GTK stuff, we're hosed. See the
1726 /* FIXME: Use something else to signal that we're
1727 * in the original spawner, and not in a spawned
1729 if (!static_mainwindow) {
1733 draft_all_messages();
1734 emergency_exit = TRUE;
1735 exit_claws(static_mainwindow);
1741 void app_will_exit(GtkWidget *widget, gpointer data)
1743 MainWindow *mainwin = data;
1745 if (sc_exiting == TRUE) {
1746 debug_print("exit pending\n");
1750 debug_print("exiting\n");
1751 if (compose_get_compose_list()) {
1752 if (!draft_all_messages()) {
1753 main_window_popup(mainwin);
1759 if (prefs_common.warn_queued_on_exit && procmsg_have_queued_mails_fast()) {
1760 if (alertpanel(_("Queued messages"),
1761 _("Some unsent messages are queued. Exit now?"),
1762 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL)
1763 != G_ALERTALTERNATE) {
1764 main_window_popup(mainwin);
1768 manage_window_focus_in(mainwin->window, NULL, NULL);
1772 #ifdef HAVE_VALGRIND
1773 if (RUNNING_ON_VALGRIND) {
1774 summary_clear_list(mainwin->summaryview);
1777 if (folderview_get_selected_item(mainwin->folderview))
1778 folder_item_close(folderview_get_selected_item(mainwin->folderview));
1782 gboolean claws_is_exiting(void)
1787 gboolean claws_is_starting(void)
1793 * CLAWS: want this public so crash dialog can delete the
1796 gchar *claws_get_socket_name(void)
1798 static gchar *filename = NULL;
1799 const gchar *socket_dir = NULL;
1801 if (rc_dir_is_alt())
1802 socket_dir = get_rc_dir();
1804 socket_dir = g_get_tmp_dir();
1805 if (filename == NULL) {
1806 filename = g_strdup_printf("%s%cclaws-mail-%d",
1807 socket_dir, G_DIR_SEPARATOR,
1818 static gchar *get_crashfile_name(void)
1820 static gchar *filename = NULL;
1822 if (filename == NULL) {
1823 filename = g_strdup_printf("%s%cclaws-crashed",
1824 get_tmp_dir(), G_DIR_SEPARATOR);
1830 static gint prohibit_duplicate_launch(void)
1835 path = claws_get_socket_name();
1836 uxsock = fd_connect_unix(path);
1838 if (x_display == NULL)
1839 x_display = g_strdup(g_getenv("DISPLAY"));
1843 return fd_open_unix(path);
1846 /* remote command mode */
1848 debug_print("another Claws Mail instance is already running.\n");
1850 if (cmd.receive_all) {
1851 fd_write_all(uxsock, "receive_all\n", 12);
1852 } else if (cmd.receive) {
1853 fd_write_all(uxsock, "receive\n", 8);
1854 } else if (cmd.compose && cmd.attach_files) {
1855 gchar *str, *compose_str;
1858 if (cmd.compose_mailto) {
1859 compose_str = g_strdup_printf("compose_attach %s\n",
1860 cmd.compose_mailto);
1862 compose_str = g_strdup("compose_attach\n");
1865 fd_write_all(uxsock, compose_str, strlen(compose_str));
1866 g_free(compose_str);
1868 for (i = 0; i < cmd.attach_files->len; i++) {
1869 str = g_ptr_array_index(cmd.attach_files, i);
1870 fd_write_all(uxsock, str, strlen(str));
1871 fd_write_all(uxsock, "\n", 1);
1874 fd_write_all(uxsock, ".\n", 2);
1875 } else if (cmd.compose) {
1878 if (cmd.compose_mailto) {
1879 compose_str = g_strdup_printf
1880 ("compose %s\n", cmd.compose_mailto);
1882 compose_str = g_strdup("compose\n");
1885 fd_write_all(uxsock, compose_str, strlen(compose_str));
1886 g_free(compose_str);
1887 } else if (cmd.subscribe) {
1888 gchar *str = g_strdup_printf("subscribe %s\n", cmd.subscribe_uri);
1889 fd_write_all(uxsock, str, strlen(str));
1891 } else if (cmd.send) {
1892 fd_write_all(uxsock, "send\n", 5);
1893 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
1894 fd_write(uxsock, "online\n", 6);
1895 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
1896 fd_write(uxsock, "offline\n", 7);
1897 } else if (cmd.status || cmd.status_full) {
1898 gchar buf[BUFFSIZE];
1900 const gchar *command;
1904 command = cmd.status_full ? "status-full\n" : "status\n";
1905 folders = cmd.status_full ? cmd.status_full_folders :
1908 fd_write_all(uxsock, command, strlen(command));
1909 for (i = 0; folders && i < folders->len; ++i) {
1910 folder = g_ptr_array_index(folders, i);
1911 fd_write_all(uxsock, folder, strlen(folder));
1912 fd_write_all(uxsock, "\n", 1);
1914 fd_write_all(uxsock, ".\n", 2);
1916 fd_gets(uxsock, buf, sizeof(buf));
1917 if (!strncmp(buf, ".\n", 2)) break;
1920 } else if (cmd.exit) {
1921 fd_write_all(uxsock, "exit\n", 5);
1922 } else if (cmd.target) {
1923 gchar *str = g_strdup_printf("select %s\n", cmd.target);
1924 fd_write_all(uxsock, str, strlen(str));
1928 fd_write_all(uxsock, "get_display\n", 12);
1929 memset(buf, 0, sizeof(buf));
1930 fd_gets(uxsock, buf, sizeof(buf));
1931 if (strcmp2(buf, x_display)) {
1932 g_print("Claws Mail is already running on display %s.\n",
1936 uxsock = fd_connect_unix(path);
1937 fd_write_all(uxsock, "popup\n", 6);
1945 static gint lock_socket_remove(void)
1950 if (lock_socket < 0) {
1954 if (lock_socket_tag > 0) {
1955 gdk_input_remove(lock_socket_tag);
1957 fd_close(lock_socket);
1958 filename = claws_get_socket_name();
1965 static GPtrArray *get_folder_item_list(gint sock)
1967 gchar buf[BUFFSIZE];
1969 GPtrArray *folders = NULL;
1972 fd_gets(sock, buf, sizeof(buf));
1973 if (!strncmp(buf, ".\n", 2)) {
1978 folders = g_ptr_array_new();
1980 item = folder_find_item_from_identifier(buf);
1982 g_ptr_array_add(folders, item);
1984 g_warning("no such folder: %s\n", buf);
1991 static void lock_socket_input_cb(gpointer data,
1993 GdkInputCondition condition)
1995 MainWindow *mainwin = (MainWindow *)data;
1997 gchar buf[BUFFSIZE];
1999 sock = fd_accept(source);
2000 fd_gets(sock, buf, sizeof(buf));
2002 if (!strncmp(buf, "popup", 5)) {
2003 main_window_popup(mainwin);
2004 } else if (!strncmp(buf, "get_display", 11)) {
2005 fd_write_all(sock, x_display, strlen(x_display));
2006 } else if (!strncmp(buf, "receive_all", 11)) {
2007 inc_all_account_mail(mainwin, FALSE,
2008 prefs_common.newmail_notify_manu);
2009 } else if (!strncmp(buf, "receive", 7)) {
2010 inc_mail(mainwin, prefs_common.newmail_notify_manu);
2011 } else if (!strncmp(buf, "compose_attach", 14)) {
2015 mailto = g_strdup(buf + strlen("compose_attach") + 1);
2016 files = g_ptr_array_new();
2017 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
2018 if (buf[0] == '.' && buf[1] == '\n') {
2022 g_ptr_array_add(files, g_strdup(buf));
2024 open_compose_new(mailto, files);
2025 ptr_array_free_strings(files);
2026 g_ptr_array_free(files, TRUE);
2028 } else if (!strncmp(buf, "compose", 7)) {
2029 open_compose_new(buf + strlen("compose") + 1, NULL);
2030 } else if (!strncmp(buf, "subscribe", 9)) {
2031 main_window_popup(mainwin);
2032 folder_subscribe(buf + strlen("subscribe") + 1);
2033 } else if (!strncmp(buf, "send", 4)) {
2035 } else if (!strncmp(buf, "online", 6)) {
2036 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
2037 } else if (!strncmp(buf, "offline", 7)) {
2038 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
2039 } else if (!strncmp(buf, "status-full", 11) ||
2040 !strncmp(buf, "status", 6)) {
2044 folders = get_folder_item_list(sock);
2045 status = folder_get_status
2046 (folders, !strncmp(buf, "status-full", 11));
2047 fd_write_all(sock, status, strlen(status));
2048 fd_write_all(sock, ".\n", 2);
2050 if (folders) g_ptr_array_free(folders, TRUE);
2051 } else if (!strncmp(buf, "select ", 7)) {
2052 const gchar *target = buf+7;
2053 mainwindow_jump_to(target, TRUE);
2054 } else if (!strncmp(buf, "exit", 4)) {
2055 app_will_exit(NULL, mainwin);
2061 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
2066 Xstrdup_a(addr, address, return);
2070 compose_new(NULL, addr, attach_files);
2073 static void send_queue(void)
2076 gchar *errstr = NULL;
2077 gboolean error = FALSE;
2078 for (list = folder_get_list(); list != NULL; list = list->next) {
2079 Folder *folder = list->data;
2081 if (folder->queue) {
2082 gint res = procmsg_send_queue
2083 (folder->queue, prefs_common.savemsg,
2087 folder_item_scan(folder->queue);
2095 alertpanel_error_log(_("Some errors occurred "
2096 "while sending queued messages:\n%s"), errstr);
2099 alertpanel_error_log("Some errors occurred "
2100 "while sending queued messages.");
2104 static void quit_signal_handler(int sig)
2106 debug_print("Quitting on signal %d\n", sig);
2108 g_timeout_add(0, clean_quit, NULL);
2111 static void install_basic_sighandlers()
2115 struct sigaction act;
2120 sigaddset(&mask, SIGTERM);
2123 sigaddset(&mask, SIGINT);
2126 sigaddset(&mask, SIGHUP);
2129 act.sa_handler = quit_signal_handler;
2134 sigaction(SIGTERM, &act, 0);
2137 sigaction(SIGINT, &act, 0);
2140 sigaction(SIGHUP, &act, 0);
2143 sigprocmask(SIG_UNBLOCK, &mask, 0);
2144 #endif /* !G_OS_WIN32 */
2148 osso_context_t *get_osso_context(void)
2150 return static_osso_context;