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))
120 #include <hildon-widgets/hildon-banner.h>
121 #include <hildon-widgets/hildon-program.h>
122 #include <gtk/gtkmain.h>
124 #include <libgnomevfs/gnome-vfs-volume.h>
125 #include <libgnomevfs/gnome-vfs-volume-monitor.h>
126 #include <libgnomevfs/gnome-vfs-utils.h>
128 #define OSSO_NAME "claws_mail"
129 #define OSSO_SERVICE "com.nokia."OSSO_NAME
130 #define OSSO_OBJECT "/com/nokia/"OSSO_NAME
131 #define OSSO_IFACE "com.nokia."OSSO_NAME
133 typedef struct _AppData AppData;
135 HildonProgram *program;
136 HildonWindow *window;
137 osso_context_t *osso_context;
140 static GnomeVFSVolumeMonitor *volmon;
147 HildonProgram *hildon_program;
150 #ifdef HAVE_STARTUP_NOTIFICATION
151 static SnLauncheeContext *sn_context = NULL;
152 static SnDisplay *sn_display = NULL;
155 static gint lock_socket = -1;
156 static gint lock_socket_tag = 0;
157 static gchar *x_display = NULL;
160 ONLINE_MODE_DONT_CHANGE,
165 static struct RemoteCmd {
167 gboolean receive_all;
169 const gchar *compose_mailto;
170 GPtrArray *attach_files;
172 gboolean status_full;
173 GPtrArray *status_folders;
174 GPtrArray *status_full_folders;
181 const gchar *subscribe_uri;
185 static void parse_cmd_opt(int argc, char *argv[]);
187 static gint prohibit_duplicate_launch (void);
188 static gchar * get_crashfile_name (void);
189 static gint lock_socket_remove (void);
190 static void lock_socket_input_cb (gpointer data,
192 GdkInputCondition condition);
194 static void open_compose_new (const gchar *address,
195 GPtrArray *attach_files);
197 static void send_queue (void);
198 static void initial_processing (FolderItem *item, gpointer data);
199 static void quit_signal_handler (int sig);
200 static void install_basic_sighandlers (void);
201 static void exit_claws (MainWindow *mainwin);
203 #define MAKE_DIR_IF_NOT_EXIST(dir) \
205 if (!is_dir_exist(dir)) { \
206 if (is_file_exist(dir)) { \
208 (_("File '%s' already exists.\n" \
209 "Can't create folder."), \
213 if (make_dir(dir) < 0) \
218 static MainWindow *static_mainwindow;
221 static osso_context_t *static_osso_context;
223 void exit_event_handler(gboolean die_now, gpointer data)
226 appdata = (AppData *) data;
227 /* Do whatever application needs to do before exiting */
228 exit_claws(static_mainwindow);
229 hildon_banner_show_information(GTK_WIDGET(appdata->window), NULL,
234 /* Callback for normal D-BUS messages */
235 gint dbus_req_handler(const gchar * interface, const gchar * method,
236 GArray * arguments, gpointer data,
240 appdata = (AppData *) data;
242 if (!strcmp(method, "top_application")) {
243 osso_rpc_free_val(retval);
246 osso_system_note_infoprint(appdata->osso_context, method, retval);
247 osso_rpc_free_val(retval);
252 static gboolean emergency_exit = FALSE;
254 #ifdef HAVE_STARTUP_NOTIFICATION
255 static void sn_error_trap_push(SnDisplay *display, Display *xdisplay)
257 gdk_error_trap_push();
260 static void sn_error_trap_pop(SnDisplay *display, Display *xdisplay)
262 gdk_error_trap_pop();
265 static void startup_notification_complete(gboolean with_window)
268 GtkWidget *hack = NULL;
271 /* this is needed to make the startup notification leave,
272 * if we have been launched from a menu.
273 * We have to display a window, so let it be very little */
274 hack = gtk_window_new(GTK_WINDOW_POPUP);
275 gtk_widget_set_uposition(hack, 0, 0);
276 gtk_widget_set_size_request(hack, 1, 1);
277 gtk_widget_show(hack);
280 xdisplay = GDK_DISPLAY();
281 sn_display = sn_display_new(xdisplay,
284 sn_context = sn_launchee_context_new_from_environment(sn_display,
285 DefaultScreen(xdisplay));
287 if (sn_context != NULL) {
288 sn_launchee_context_complete(sn_context);
289 sn_launchee_context_unref(sn_context);
290 sn_display_unref(sn_display);
293 gtk_widget_destroy(hack);
296 #endif /* HAVE_STARTUP_NOTIFICATION */
298 static void claws_gtk_idle(void)
300 while(gtk_events_pending()) {
301 gtk_main_iteration();
306 static gboolean sc_starting = FALSE;
308 static gboolean defer_check_all(void *data)
310 gboolean autochk = GPOINTER_TO_INT(data);
312 inc_all_account_mail(static_mainwindow, autochk,
313 prefs_common.newmail_notify_manu);
317 main_window_set_menu_sensitive(static_mainwindow);
318 toolbar_main_set_sensitive(static_mainwindow);
323 static gboolean defer_check(void *data)
325 inc_mail(static_mainwindow, prefs_common.newmail_notify_manu);
329 main_window_set_menu_sensitive(static_mainwindow);
330 toolbar_main_set_sensitive(static_mainwindow);
335 static gboolean defer_jump(void *data)
337 if (cmd.receive_all) {
338 defer_check_all(GINT_TO_POINTER(FALSE));
339 } else if (prefs_common.chk_on_startup) {
340 defer_check_all(GINT_TO_POINTER(TRUE));
341 } else if (cmd.receive) {
344 mainwindow_jump_to(data, FALSE);
347 main_window_set_menu_sensitive(static_mainwindow);
348 toolbar_main_set_sensitive(static_mainwindow);
353 static void chk_update_val(GtkWidget *widget, gpointer data)
355 gboolean *val = (gboolean *)data;
356 *val = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
359 static gboolean migrate_old_config(const gchar *old_cfg_dir, const gchar *new_cfg_dir, const gchar *oldversion)
361 gchar *message = g_strdup_printf(_("Configuration for %s found.\n"
362 "Do you want to migrate this configuration?"), oldversion);
363 gchar *message2 = g_strdup_printf(_("\n\nYour Sylpheed filtering rules can be converted by a\n"
364 "script available at %s."), TOOLS_URI);
366 if (!strcmp(oldversion, "Sylpheed"))
367 message = g_strconcat(message, message2, NULL);
371 GtkWidget *window = NULL;
372 GtkWidget *keep_backup_chk;
373 GtkTooltips *tips = gtk_tooltips_new();
374 gboolean backup = TRUE;
376 keep_backup_chk = gtk_check_button_new_with_label (_("Keep old configuration"));
377 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_backup_chk), TRUE);
378 gtk_tooltips_set_tip(GTK_TOOLTIPS(tips), keep_backup_chk,
379 _("Keeping a backup will allow you to go back to an "
380 "older version, but may take a while if you have "
381 "cached IMAP or News data, and will take some extra "
382 "room on your disk."),
385 g_signal_connect(G_OBJECT(keep_backup_chk), "toggled",
386 G_CALLBACK(chk_update_val), &backup);
388 if (alertpanel_full(_("Migration of configuration"), message,
389 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL, FALSE,
390 keep_backup_chk, ALERT_QUESTION, G_ALERTDEFAULT) != G_ALERTALTERNATE) {
394 /* we can either do a fast migration requiring not any extra disk
395 * space, or a slow one that copies the old configuration and leaves
399 window = label_window_create(_("Copying configuration... This may take a while..."));
402 r = copy_dir(old_cfg_dir, new_cfg_dir);
403 label_window_destroy(window);
405 /* if copy failed, we'll remove the partially copied
408 alertpanel_error(_("Migration failed!"));
409 remove_dir_recursive(new_cfg_dir);
412 /* fast mode failed, but we don't want backup */
413 remove_dir_recursive(old_cfg_dir);
417 window = label_window_create(_("Migrating configuration..."));
420 r = g_rename(old_cfg_dir, new_cfg_dir);
421 label_window_destroy(window);
423 /* if g_rename failed, we'll try to copy */
425 FILE_OP_ERROR(new_cfg_dir, "g_rename failed, trying copy\n");
432 static int migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
435 gchar *plugin_path, *old_plugin_path, *new_plugin_path;
437 gboolean err = FALSE;
439 oldfp = g_fopen(old_rc, "r");
442 newfp = g_fopen(new_rc, "w");
448 plugin_path = g_strdup(get_plugin_dir());
449 new_plugin_path = g_strdup(plugin_path);
451 if (strstr(plugin_path, "/claws-mail/")) {
452 gchar *end = g_strdup(strstr(plugin_path, "/claws-mail/")+strlen("/claws-mail/"));
453 *(strstr(plugin_path, "/claws-mail/")) = '\0';
454 old_plugin_path = g_strconcat(plugin_path, "/sylpheed-claws/", end, NULL);
457 old_plugin_path = g_strdup(new_plugin_path);
459 debug_print("replacing %s with %s\n", old_plugin_path, new_plugin_path);
460 while (fgets(buf, sizeof(buf), oldfp)) {
461 if (strncmp(buf, old_plugin_path, strlen(old_plugin_path))) {
462 err |= (fputs(buf, newfp) == EOF);
464 debug_print("->replacing %s", buf);
465 debug_print(" with %s%s", new_plugin_path, buf+strlen(old_plugin_path));
466 err |= (fputs(new_plugin_path, newfp) == EOF);
467 err |= (fputs(buf+strlen(old_plugin_path), newfp) == EOF);
471 g_free(new_plugin_path);
472 g_free(old_plugin_path);
474 if (fclose(newfp) == EOF)
482 sc_client_set_value (MainWindow *mainwin,
493 prop.num_vals = num_vals;
497 if (mainwin->smc_conn)
498 SmcSetProperties ((SmcConn) mainwin->smc_conn, 1, proplist);
501 static void sc_die_callback (SmcConn smc_conn, SmPointer client_data)
506 static void sc_save_complete_callback(SmcConn smc_conn, SmPointer client_data)
510 static void sc_shutdown_cancelled_callback (SmcConn smc_conn, SmPointer client_data)
512 MainWindow *mainwin = (MainWindow *)client_data;
513 if (mainwin->smc_conn)
514 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
517 static void sc_save_yourself_callback (SmcConn smc_conn,
518 SmPointer client_data,
524 MainWindow *mainwin = (MainWindow *)client_data;
525 if (mainwin->smc_conn)
526 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
529 static IceIOErrorHandler sc_ice_installed_handler;
531 static void sc_ice_io_error_handler (IceConn connection)
533 if (sc_ice_installed_handler)
534 (*sc_ice_installed_handler) (connection);
536 static gboolean sc_process_ice_messages (GIOChannel *source,
537 GIOCondition condition,
540 IceConn connection = (IceConn) data;
541 IceProcessMessagesStatus status;
543 status = IceProcessMessages (connection, NULL, NULL);
545 if (status == IceProcessMessagesIOError) {
546 IcePointer context = IceGetConnectionContext (connection);
548 if (context && GTK_IS_OBJECT (context)) {
549 guint disconnect_id = g_signal_lookup ("disconnect", G_OBJECT_TYPE (context));
551 if (disconnect_id > 0)
552 g_signal_emit (context, disconnect_id, 0);
554 IceSetShutdownNegotiation (connection, False);
555 IceCloseConnection (connection);
562 static void new_ice_connection (IceConn connection, IcePointer client_data, Bool opening,
563 IcePointer *watch_data)
569 /* Make sure we don't pass on these file descriptors to any
571 fcntl(IceConnectionNumber(connection),F_SETFD,
572 fcntl(IceConnectionNumber(connection),F_GETFD,0) | FD_CLOEXEC);
574 channel = g_io_channel_unix_new (IceConnectionNumber (connection));
575 input_id = g_io_add_watch (channel,
576 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
577 sc_process_ice_messages,
579 g_io_channel_unref (channel);
581 *watch_data = (IcePointer) GUINT_TO_POINTER (input_id);
583 input_id = GPOINTER_TO_UINT ((gpointer) *watch_data);
584 g_source_remove (input_id);
588 static void sc_session_manager_connect(MainWindow *mainwin)
590 static gboolean connected = FALSE;
591 SmcCallbacks callbacks;
593 IceIOErrorHandler default_handler;
600 sc_ice_installed_handler = IceSetIOErrorHandler (NULL);
601 default_handler = IceSetIOErrorHandler (sc_ice_io_error_handler);
603 if (sc_ice_installed_handler == default_handler)
604 sc_ice_installed_handler = NULL;
606 IceAddConnectionWatch (new_ice_connection, NULL);
609 callbacks.save_yourself.callback = sc_save_yourself_callback;
610 callbacks.die.callback = sc_die_callback;
611 callbacks.save_complete.callback = sc_save_complete_callback;
612 callbacks.shutdown_cancelled.callback = sc_shutdown_cancelled_callback;
614 callbacks.save_yourself.client_data =
615 callbacks.die.client_data =
616 callbacks.save_complete.client_data =
617 callbacks.shutdown_cancelled.client_data = (SmPointer) mainwin;
618 if (g_getenv ("SESSION_MANAGER")) {
619 gchar error_string_ret[256] = "";
621 mainwin->smc_conn = (gpointer)
622 SmcOpenConnection (NULL, mainwin,
623 SmProtoMajor, SmProtoMinor,
624 SmcSaveYourselfProcMask | SmcDieProcMask |
625 SmcSaveCompleteProcMask |
626 SmcShutdownCancelledProcMask,
629 256, error_string_ret);
631 if (error_string_ret[0] || mainwin->smc_conn == NULL)
632 g_warning ("While connecting to session manager:\n%s.",
636 vals = g_new (SmPropValue, 1);
637 vals[0].length = strlen(argv0);
638 vals[0].value = argv0;
639 sc_client_set_value (mainwin, SmCloneCommand, SmLISTofARRAY8, 1, vals);
640 sc_client_set_value (mainwin, SmRestartCommand, SmLISTofARRAY8, 1, vals);
641 sc_client_set_value (mainwin, SmProgram, SmARRAY8, 1, vals);
643 vals[0].length = strlen(g_get_user_name()?g_get_user_name():"");
644 vals[0].value = g_strdup(g_get_user_name()?g_get_user_name():"");
645 sc_client_set_value (mainwin, SmUserID, SmARRAY8, 1, vals);
651 static gboolean sc_exiting = FALSE;
652 static gboolean show_at_startup = TRUE;
653 static gboolean claws_crashed_bool = FALSE;
655 gboolean claws_crashed(void) {
656 return claws_crashed_bool;
659 void main_set_show_at_startup(gboolean show)
661 show_at_startup = show;
665 static void main_vol_mount_cb(GnomeVFSVolumeMonitor *vfs, GnomeVFSVolume *vol, MainWindow *mainwin)
667 gchar *uri = gnome_vfs_volume_get_activation_uri (vol);
668 gchar *mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
671 if(!strcmp(mount_path, prefs_common.data_root)) {
672 gtk_widget_set_sensitive(mainwin->window, TRUE);
678 static void main_vol_unmount_cb(GnomeVFSVolumeMonitor *vfs, GnomeVFSVolume *vol, MainWindow *mainwin)
680 gchar *uri = gnome_vfs_volume_get_activation_uri (vol);
681 gchar *mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
684 if(!strcmp(mount_path, prefs_common.data_root)) {
685 gtk_widget_set_sensitive(mainwin->window, FALSE);
694 static FILE* win32_debug_fp=NULL;
695 static guint win32_log_handler_app_id;
696 static guint win32_log_handler_glib_id;
697 static guint win32_log_handler_gtk_id;
699 static void win32_print_stdout(const gchar *string)
701 if (win32_debug_fp) {
702 fprintf(win32_debug_fp, string);
703 fflush(win32_debug_fp);
707 static void win32_print_stderr(const gchar *string)
709 if (win32_debug_fp) {
710 fprintf(win32_debug_fp, string);
711 fflush(win32_debug_fp);
715 static void win32_log(const gchar *log_domain, GLogLevelFlags log_level, const gchar* message, gpointer user_data)
717 if (win32_debug_fp) {
720 switch(log_level & G_LOG_LEVEL_MASK)
722 case G_LOG_LEVEL_ERROR:
725 case G_LOG_LEVEL_CRITICAL:
728 case G_LOG_LEVEL_WARNING:
731 case G_LOG_LEVEL_MESSAGE:
734 case G_LOG_LEVEL_INFO:
737 case G_LOG_LEVEL_DEBUG:
744 fprintf(win32_debug_fp, "%s: %s: %s", log_domain, type, message);
746 fprintf(win32_debug_fp, "%s: %s", type, message);
747 fflush(win32_debug_fp);
751 static void win32_open_log(void)
753 if (is_file_exist("claws-win32.log")) {
754 if (rename_force("claws-win32.log", "claws-win32.log.bak") < 0)
755 FILE_OP_ERROR("claws-win32.log", "rename");
757 win32_debug_fp = fopen("claws-win32.log", "w");
760 g_set_print_handler(win32_print_stdout);
761 g_set_printerr_handler(win32_print_stdout);
762 win32_log_handler_app_id = g_log_set_handler(NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
763 | G_LOG_FLAG_RECURSION, win32_log, NULL);
764 win32_log_handler_glib_id = g_log_set_handler("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
765 | G_LOG_FLAG_RECURSION, win32_log, NULL);
766 win32_log_handler_gtk_id = g_log_set_handler("Gtk", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
767 | G_LOG_FLAG_RECURSION, win32_log, NULL);
771 static void win32_close_log(void)
775 g_log_remove_handler("", win32_log_handler_app_id);
776 g_log_remove_handler("GLib", win32_log_handler_glib_id);
777 g_log_remove_handler("Gtk", win32_log_handler_gtk_id);
778 fclose(win32_debug_fp);
784 int main(int argc, char *argv[])
787 osso_context_t *osso_context;
788 osso_return_t result;
792 FolderView *folderview;
794 gboolean crash_file_present = FALSE;
795 gint num_folder_class = 0;
796 gboolean asked_for_migration = FALSE;
797 gboolean start_done = TRUE;
799 START_TIMING("startup");
806 if (!claws_init(&argc, &argv)) {
813 prog_version = PROG_VERSION;
814 argv0 = g_strdup(argv[0]);
816 parse_cmd_opt(argc, argv);
818 prefs_prepare_cache();
823 gtk_init(&argc, &argv);
824 crash_main(cmd.crash_params);
830 crash_install_handlers();
832 install_basic_sighandlers();
835 /* check and create unix domain socket for remote operation */
837 lock_socket = prohibit_duplicate_launch();
838 if (lock_socket < 0) {
839 #ifdef HAVE_STARTUP_NOTIFICATION
840 if(gtk_init_check(&argc, &argv))
841 startup_notification_complete(TRUE);
846 if (cmd.status || cmd.status_full) {
847 puts("0 Claws Mail not running.");
848 lock_socket_remove();
855 if (!g_thread_supported())
859 gtk_init(&argc, &argv);
862 osso_context = osso_initialize(OSSO_SERVICE, "2.8.1", TRUE, NULL);
863 if (osso_context == NULL) {
866 hildon_program = HILDON_PROGRAM(hildon_program_get_instance());
867 static_osso_context = osso_context;
870 gtk_widget_set_default_colormap(gdk_rgb_get_colormap());
871 gtk_widget_set_default_visual(gdk_rgb_get_visual());
873 if (!g_thread_supported()) {
874 g_error(_("g_thread is not supported by glib.\n"));
877 /* check that we're not on a too recent/old gtk+ */
878 #if GTK_CHECK_VERSION(2, 9, 0)
879 if (gtk_check_version(2, 9, 0) != NULL) {
880 alertpanel_error(_("Claws Mail has been compiled with "
881 "a more recent GTK+ library than is "
882 "currently available. This will cause "
883 "crashes. You need to upgrade GTK+ or "
884 "recompile Claws Mail."));
891 if (gtk_check_version(2, 9, 0) == NULL) {
892 alertpanel_error(_("Claws Mail has been compiled with "
893 "an older GTK+ library than is "
894 "currently available. This will cause "
895 "crashes. You need to recompile "
903 /* parse gtkrc files */
904 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc-2.0",
906 gtk_rc_parse(userrc);
908 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
909 G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
910 gtk_rc_parse(userrc);
914 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
916 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
919 /* no config dir exists. See if we can migrate an old config. */
920 if (!is_dir_exist(RC_DIR)) {
921 prefs_destroy_cache();
924 /* if one of the old dirs exist, we'll ask if the user
925 * want to migrates, and r will be TRUE if he said yes
926 * and migration succeeded, and FALSE otherwise.
928 if (is_dir_exist(OLD_GTK2_RC_DIR)) {
929 r = migrate_old_config(OLD_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 2.6.0 (or older)"));
930 asked_for_migration = TRUE;
931 } else if (is_dir_exist(OLDER_GTK2_RC_DIR)) {
932 r = migrate_old_config(OLDER_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.9.15 (or older)"));
933 asked_for_migration = TRUE;
934 } else if (is_dir_exist(OLD_GTK1_RC_DIR)) {
935 r = migrate_old_config(OLD_GTK1_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.0.5 (or older)"));
936 asked_for_migration = TRUE;
937 } else if (is_dir_exist(SYLPHEED_RC_DIR)) {
938 r = migrate_old_config(SYLPHEED_RC_DIR, RC_DIR, "Sylpheed");
939 asked_for_migration = TRUE;
942 /* If migration failed or the user didn't want to do it,
943 * we create a new one (and we'll hit wizard later).
945 if (r == FALSE && !is_dir_exist(RC_DIR) && make_dir(RC_DIR) < 0) {
954 if (!is_file_exist(RC_DIR G_DIR_SEPARATOR_S COMMON_RC) &&
955 is_file_exist(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC)) {
956 /* post 2.6 name change */
957 migrate_common_rc(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC,
958 RC_DIR G_DIR_SEPARATOR_S COMMON_RC);
962 plugin_load_all("Common");
964 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
965 gtk_rc_parse(userrc);
968 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
969 gtk_accel_map_load (userrc);
973 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_rc_dir(), 1, win32_close_log(););
975 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
978 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
979 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
980 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
981 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
982 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
983 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR);
985 crash_file_present = is_file_exist(get_crashfile_name());
986 /* remove temporary files */
987 remove_all_files(get_tmp_dir());
988 remove_all_files(get_mime_tmp_dir());
990 if (!cmd.crash && crash_file_present)
991 claws_crashed_bool = TRUE;
993 if (is_file_exist("claws.log")) {
994 if (rename_force("claws.log", "claws.log.bak") < 0)
995 FILE_OP_ERROR("claws.log", "rename");
997 set_log_file(LOG_PROTOCOL, "claws.log");
999 if (is_file_exist("filtering.log")) {
1000 if (rename_force("filtering.log", "filtering.log.bak") < 0)
1001 FILE_OP_ERROR("filtering.log", "rename");
1003 set_log_file(LOG_DEBUG_FILTERING, "filtering.log");
1006 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1008 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1011 folder_system_init();
1012 prefs_common_read_config();
1014 prefs_themes_init();
1016 prefs_ext_prog_init();
1017 prefs_wrapping_init();
1018 prefs_compose_writing_init();
1019 prefs_msg_colors_init();
1020 image_viewer_init();
1021 prefs_image_viewer_init();
1023 prefs_summaries_init();
1024 prefs_message_init();
1026 prefs_logging_init();
1027 prefs_receive_init();
1031 gtkaspell_checkers_init();
1032 prefs_spelling_init();
1035 sock_set_io_timeout(prefs_common.io_timeout_secs);
1036 prefs_actions_read_config();
1037 prefs_display_header_read_config();
1038 /* prefs_filtering_read_config(); */
1039 addressbook_read_file();
1041 gtkut_widget_init();
1042 stock_pixbuf_gdk(NULL, STOCK_PIXMAP_CLAWS_MAIL_ICON, &icon);
1043 gtk_window_set_default_icon(icon);
1045 folderview_initialize();
1051 mainwin = main_window_create();
1054 appdata = g_new0(AppData, 1);
1055 appdata->program = hildon_program;
1056 appdata->window = HILDON_WINDOW(mainwin->window);
1057 appdata->osso_context = osso_context;
1058 result = osso_rpc_set_cb_f(appdata->osso_context,
1062 dbus_req_handler, appdata);
1063 if (result != OSSO_OK) {
1067 /* Add handler for Exit D-BUS messages */
1068 result = osso_application_set_exit_cb(appdata->osso_context,
1070 (gpointer) appdata);
1071 if (result != OSSO_OK) {
1075 manage_window_focus_in(mainwin->window, NULL, NULL);
1076 folderview = mainwin->folderview;
1078 gtk_clist_freeze(GTK_CLIST(mainwin->folderview->ctree));
1079 folder_item_update_freeze();
1081 /* register the callback of unix domain socket input */
1083 lock_socket_tag = gdk_input_add(lock_socket,
1084 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
1085 lock_socket_input_cb,
1089 prefs_account_init();
1090 account_read_config_all();
1092 /* If we can't read a folder list or don't have accounts,
1093 * it means the configuration's not done. Either this is
1094 * a brand new install, either a failed/refused migration.
1095 * So we'll start the wizard.
1097 if (folder_read_list() < 0) {
1098 prefs_destroy_cache();
1100 /* if run_wizard returns FALSE it's because it's
1101 * been cancelled. We can't do much but exit.
1102 * however, if the user was asked for a migration,
1103 * we remove the newly created directory so that
1104 * he's asked again for migration on next launch.*/
1105 if (!run_wizard(mainwin, TRUE)) {
1106 if (asked_for_migration)
1107 remove_dir_recursive(RC_DIR);
1113 main_window_reflect_prefs_all_now();
1114 folder_write_list();
1117 if (!account_get_list()) {
1118 prefs_destroy_cache();
1119 if (!run_wizard(mainwin, FALSE)) {
1120 if (asked_for_migration)
1121 remove_dir_recursive(RC_DIR);
1127 if(!account_get_list()) {
1128 exit_claws(mainwin);
1137 toolbar_main_set_sensitive(mainwin);
1138 main_window_set_menu_sensitive(mainwin);
1140 /* if crashed, show window early so that the user
1141 * sees what's happening */
1142 if (claws_crashed())
1143 main_window_popup(mainwin);
1145 #ifdef HAVE_LIBETPAN
1146 imap_main_init(prefs_common.skip_ssl_cert_check);
1147 imap_main_set_timeout(prefs_common.io_timeout_secs);
1148 nntp_main_init(prefs_common.skip_ssl_cert_check);
1150 account_set_missing_folder();
1151 folder_set_missing_folders();
1152 folderview_set(folderview);
1154 prefs_matcher_read_config();
1156 /* make one all-folder processing before using claws */
1157 main_window_cursor_wait(mainwin);
1158 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
1160 /* if claws crashed, rebuild caches */
1161 if (claws_crashed()) {
1163 debug_print("Claws Mail crashed, checking for new messages in local folders\n");
1164 folder_item_update_thaw();
1165 folderview_check_new(NULL);
1166 folder_clean_cache_memory_force();
1167 folder_item_update_freeze();
1169 /* make the crash-indicator file */
1170 str_write_to_file("foo", get_crashfile_name());
1172 inc_autocheck_timer_init(mainwin);
1174 /* ignore SIGPIPE signal for preventing sudden death of program */
1176 signal(SIGPIPE, SIG_IGN);
1178 if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
1179 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1181 if (cmd.online_mode == ONLINE_MODE_ONLINE) {
1182 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1185 if (cmd.status_folders) {
1186 g_ptr_array_free(cmd.status_folders, TRUE);
1187 cmd.status_folders = NULL;
1189 if (cmd.status_full_folders) {
1190 g_ptr_array_free(cmd.status_full_folders, TRUE);
1191 cmd.status_full_folders = NULL;
1194 claws_register_idle_function(claws_gtk_idle);
1196 prefs_toolbar_init();
1198 num_folder_class = g_list_length(folder_get_list());
1200 plugin_load_all("GTK2");
1202 if (g_list_length(folder_get_list()) != num_folder_class) {
1203 debug_print("new folders loaded, reloading processing rules\n");
1204 prefs_matcher_read_config();
1207 if (plugin_get_unloaded_list() != NULL) {
1208 main_window_cursor_normal(mainwin);
1209 alertpanel_warning(_("Some plugin(s) failed to load. "
1210 "Check the Plugins configuration "
1211 "for more information."));
1212 main_window_cursor_wait(mainwin);
1215 plugin_load_standard_plugins ();
1217 /* if not crashed, show window now */
1218 if (!claws_crashed()) {
1219 /* apart if something told not to show */
1220 if (show_at_startup)
1221 main_window_popup(mainwin);
1224 if (!folder_have_mailbox()) {
1225 prefs_destroy_cache();
1226 main_window_cursor_normal(mainwin);
1227 if (folder_get_list() != NULL) {
1228 alertpanel_error(_("Claws Mail has detected a configured "
1229 "mailbox, but it is incomplete. It is "
1230 "possibly due to a failing IMAP account. Use "
1231 "\"Rebuild folder tree\" on the mailbox parent "
1232 "folder's context menu to try to fix it."));
1234 alertpanel_error(_("Claws Mail has detected a configured "
1235 "mailbox, but could not load it. It is "
1236 "probably provided by an out-of-date "
1237 "external plugin. Please reinstall the "
1238 "plugin and try again."));
1239 exit_claws(mainwin);
1247 static_mainwindow = mainwin;
1250 if (prefs_common.data_root != NULL && *prefs_common.data_root != '\0') {
1251 GnomeVFSVolume *vol = NULL;
1252 gchar *uri, *mount_path;
1254 volmon = gnome_vfs_get_volume_monitor();
1255 vol = gnome_vfs_volume_monitor_get_volume_for_path(volmon, prefs_common.data_root);
1257 uri = gnome_vfs_volume_get_activation_uri (vol);
1258 mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
1261 if (vol == NULL || !gnome_vfs_volume_is_mounted(vol)
1262 || strcmp(mount_path, prefs_common.data_root)) {
1263 alertpanel_error(_("Claws Mail can not start without its data volume (%s)."),
1264 prefs_common.data_root);
1266 gnome_vfs_volume_unref(vol);
1267 exit_claws(mainwin);
1271 gnome_vfs_volume_unref(vol);
1272 g_signal_connect(G_OBJECT(volmon),
1273 "volume-mounted", G_CALLBACK(main_vol_mount_cb), mainwin);
1274 g_signal_connect(G_OBJECT(volmon),
1275 "volume-unmounted", G_CALLBACK(main_vol_unmount_cb), mainwin);
1279 #ifdef HAVE_STARTUP_NOTIFICATION
1280 startup_notification_complete(FALSE);
1283 sc_session_manager_connect(mainwin);
1285 folder_item_update_thaw();
1286 gtk_clist_thaw(GTK_CLIST(mainwin->folderview->ctree));
1287 main_window_cursor_normal(mainwin);
1289 if (!cmd.target && prefs_common.goto_last_folder_on_startup &&
1290 folder_find_item_from_identifier(prefs_common.last_opened_folder) != NULL &&
1292 cmd.target = prefs_common.last_opened_folder;
1295 if (cmd.receive_all && !cmd.target) {
1297 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(FALSE));
1298 } else if (prefs_common.chk_on_startup && !cmd.target) {
1300 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(TRUE));
1301 } else if (cmd.receive && !cmd.target) {
1303 g_timeout_add(1000, defer_check, NULL);
1305 gtk_widget_grab_focus(folderview->ctree);
1309 open_compose_new(cmd.compose_mailto, cmd.attach_files);
1311 if (cmd.attach_files) {
1312 ptr_array_free_strings(cmd.attach_files);
1313 g_ptr_array_free(cmd.attach_files, TRUE);
1314 cmd.attach_files = NULL;
1316 if (cmd.subscribe) {
1317 folder_subscribe(cmd.subscribe_uri);
1326 g_timeout_add(500, defer_jump, (gpointer)cmd.target);
1329 prefs_destroy_cache();
1331 compose_reopen_exit_drafts();
1334 sc_starting = FALSE;
1335 main_window_set_menu_sensitive(mainwin);
1336 toolbar_main_set_sensitive(mainwin);
1343 osso_deinitialize(osso_context);
1348 exit_claws(mainwin);
1353 static void save_all_caches(FolderItem *item, gpointer data)
1360 folder_item_close(item);
1363 folder_item_free_cache(item, TRUE);
1366 static void exit_claws(MainWindow *mainwin)
1372 debug_print("shutting down\n");
1373 inc_autocheck_timer_remove();
1375 if (prefs_common.clean_on_exit && !emergency_exit) {
1376 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
1379 /* save prefs for opened folder */
1380 if(mainwin->folderview->opened) {
1383 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
1384 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
1385 prefs_common.last_opened_folder = folder_item_get_identifier(item);
1388 /* save all state before exiting */
1389 folder_func_to_all_folders(save_all_caches, NULL);
1390 folder_write_list();
1392 main_window_get_size(mainwin);
1393 main_window_get_position(mainwin);
1395 prefs_common_write_config();
1396 account_write_config_all();
1397 addressbook_export_to_file();
1399 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1400 gtk_accel_map_save(filename);
1403 /* delete temporary files */
1404 remove_all_files(get_tmp_dir());
1405 remove_all_files(get_mime_tmp_dir());
1407 close_log_file(LOG_PROTOCOL);
1408 close_log_file(LOG_DEBUG_FILTERING);
1410 #ifdef HAVE_LIBETPAN
1414 /* delete crashfile */
1416 g_unlink(get_crashfile_name());
1418 lock_socket_remove();
1421 if (mainwin->smc_conn)
1422 SmcCloseConnection ((SmcConn)mainwin->smc_conn, 0, NULL);
1423 mainwin->smc_conn = NULL;
1426 main_window_destroy_all();
1428 plugin_unload_all("GTK2");
1430 prefs_toolbar_done();
1432 addressbook_destroy();
1434 prefs_themes_done();
1436 prefs_ext_prog_done();
1437 prefs_wrapping_done();
1438 prefs_compose_writing_done();
1439 prefs_msg_colors_done();
1440 prefs_image_viewer_done();
1441 image_viewer_done();
1443 prefs_summaries_done();
1444 prefs_message_done();
1446 prefs_receive_done();
1447 prefs_logging_done();
1451 prefs_spelling_done();
1452 gtkaspell_checkers_quit();
1454 plugin_unload_all("Common");
1458 static void parse_cmd_opt(int argc, char *argv[])
1462 for (i = 1; i < argc; i++) {
1463 if (!strncmp(argv[i], "--receive-all", 13)) {
1464 cmd.receive_all = TRUE;
1465 } else if (!strncmp(argv[i], "--receive", 9)) {
1467 } else if (!strncmp(argv[i], "--compose", 9)) {
1468 const gchar *p = argv[i + 1];
1471 cmd.compose_mailto = NULL;
1472 if (p && *p != '\0' && *p != '-') {
1473 if (!strncmp(p, "mailto:", 7)) {
1474 cmd.compose_mailto = p + 7;
1476 cmd.compose_mailto = p;
1480 } else if (!strncmp(argv[i], "--subscribe", 11)) {
1481 const gchar *p = argv[i + 1];
1482 if (p && *p != '\0' && *p != '-') {
1483 cmd.subscribe = TRUE;
1484 cmd.subscribe_uri = p;
1486 } else if (!strncmp(argv[i], "--attach", 8)) {
1487 const gchar *p = argv[i + 1];
1490 while (p && *p != '\0' && *p != '-') {
1491 if (!cmd.attach_files) {
1492 cmd.attach_files = g_ptr_array_new();
1494 if ((file = g_filename_from_uri(p, NULL, NULL)) != NULL) {
1495 if (!is_file_exist(file)) {
1500 if (file == NULL && *p != G_DIR_SEPARATOR) {
1501 file = g_strconcat(claws_get_startup_dir(),
1504 } else if (file == NULL) {
1507 g_ptr_array_add(cmd.attach_files, file);
1511 } else if (!strncmp(argv[i], "--send", 6)) {
1513 } else if (!strncmp(argv[i], "--version", 9) ||
1514 !strncmp(argv[i], "-v", 2)) {
1515 g_print("Claws Mail version " VERSION "\n");
1517 } else if (!strncmp(argv[i], "--status-full", 13)) {
1518 const gchar *p = argv[i + 1];
1520 cmd.status_full = TRUE;
1521 while (p && *p != '\0' && *p != '-') {
1522 if (!cmd.status_full_folders) {
1523 cmd.status_full_folders =
1526 g_ptr_array_add(cmd.status_full_folders,
1531 } else if (!strncmp(argv[i], "--status", 8)) {
1532 const gchar *p = argv[i + 1];
1535 while (p && *p != '\0' && *p != '-') {
1536 if (!cmd.status_folders)
1537 cmd.status_folders = g_ptr_array_new();
1538 g_ptr_array_add(cmd.status_folders,
1543 } else if (!strncmp(argv[i], "--online", 8)) {
1544 cmd.online_mode = ONLINE_MODE_ONLINE;
1545 } else if (!strncmp(argv[i], "--offline", 9)) {
1546 cmd.online_mode = ONLINE_MODE_OFFLINE;
1547 } else if (!strncmp(argv[i], "--help", 6) ||
1548 !strncmp(argv[i], "-h", 2)) {
1549 gchar *base = g_path_get_basename(argv[0]);
1550 g_print(_("Usage: %s [OPTION]...\n"), base);
1552 g_print("%s\n", _(" --compose [address] open composition window"));
1553 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
1554 g_print("%s\n", _(" --attach file1 [file2]...\n"
1555 " open composition window with specified files\n"
1557 g_print("%s\n", _(" --receive receive new messages"));
1558 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
1559 g_print("%s\n", _(" --send send all queued messages"));
1560 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
1561 g_print("%s\n", _(" --status-full [folder]...\n"
1562 " show the status of each folder"));
1563 g_print("%s\n", _(" --select folder[/msg] jumps to the specified folder/message\n"
1564 " folder is a folder id like 'folder/sub_folder'"));
1565 g_print("%s\n", _(" --online switch to online mode"));
1566 g_print("%s\n", _(" --offline switch to offline mode"));
1567 g_print("%s\n", _(" --exit --quit -q exit Claws Mail"));
1568 g_print("%s\n", _(" --debug debug mode"));
1569 g_print("%s\n", _(" --help -h display this help and exit"));
1570 g_print("%s\n", _(" --version -v output version information and exit"));
1571 g_print("%s\n", _(" --config-dir output configuration directory"));
1572 g_print("%s\n", _(" --alternate-config-dir [dir]\n"
1573 " use specified configuration directory"));
1577 } else if (!strncmp(argv[i], "--crash", 7)) {
1579 cmd.crash_params = g_strdup(argv[i + 1]);
1581 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
1582 g_print(RC_DIR "\n");
1584 } else if (!strncmp(argv[i], "--alternate-config-dir", sizeof "--alternate-config-dir" - 1) && i+1 < argc) {
1585 set_rc_dir(argv[i+1]);
1586 } else if (!strncmp(argv[i], "--exit", 6) ||
1587 !strncmp(argv[i], "--quit", 6) ||
1588 !strncmp(argv[i], "-q", 2)) {
1590 } else if (!strncmp(argv[i], "--select", 8) && i+1 < argc) {
1591 cmd.target = argv[i+1];
1592 } else if (i == 1 && argc == 2) {
1593 /* only one parameter. Do something intelligent about it */
1594 if (strstr(argv[i], "@") && !strstr(argv[i], "://")) {
1595 const gchar *p = argv[i];
1598 cmd.compose_mailto = NULL;
1599 if (p && *p != '\0' && *p != '-') {
1600 if (!strncmp(p, "mailto:", 7)) {
1601 cmd.compose_mailto = p + 7;
1603 cmd.compose_mailto = p;
1606 } else if (strstr(argv[i], "://")) {
1607 const gchar *p = argv[i];
1608 if (p && *p != '\0' && *p != '-') {
1609 cmd.subscribe = TRUE;
1610 cmd.subscribe_uri = p;
1612 } else if (!strcmp(argv[i], "--sync")) {
1615 g_print(_("Unknown option\n"));
1621 if (cmd.attach_files && cmd.compose == FALSE) {
1623 cmd.compose_mailto = NULL;
1627 static void initial_processing(FolderItem *item, gpointer data)
1629 MainWindow *mainwin = (MainWindow *)data;
1632 g_return_if_fail(item);
1633 buf = g_strdup_printf(_("Processing (%s)..."),
1636 : _("top level folder"));
1640 if (item->prefs->enable_processing) {
1641 folder_item_apply_processing(item);
1644 STATUSBAR_POP(mainwin);
1647 static gboolean draft_all_messages(void)
1649 GList *compose_list = NULL;
1651 compose_clear_exit_drafts();
1652 compose_list = compose_get_compose_list();
1653 while (compose_list != NULL) {
1654 Compose *c = (Compose*)compose_list->data;
1655 if (!compose_draft(c, COMPOSE_DRAFT_FOR_EXIT))
1657 compose_list = compose_get_compose_list();
1661 gboolean clean_quit(gpointer data)
1663 static gboolean firstrun = TRUE;
1670 /*!< Good idea to have the main window stored in a
1671 * static variable so we can check that variable
1672 * to see if we're really allowed to do things
1673 * that actually the spawner is supposed to
1674 * do (like: sending mail, composing messages).
1675 * Because, really, if we're the spawnee, and
1676 * we touch GTK stuff, we're hosed. See the
1679 /* FIXME: Use something else to signal that we're
1680 * in the original spawner, and not in a spawned
1682 if (!static_mainwindow) {
1686 draft_all_messages();
1687 emergency_exit = TRUE;
1688 exit_claws(static_mainwindow);
1694 void app_will_exit(GtkWidget *widget, gpointer data)
1696 MainWindow *mainwin = data;
1698 if (sc_exiting == TRUE) {
1699 debug_print("exit pending\n");
1703 debug_print("exiting\n");
1704 if (compose_get_compose_list()) {
1705 if (!draft_all_messages()) {
1706 main_window_popup(mainwin);
1712 if (prefs_common.warn_queued_on_exit && procmsg_have_queued_mails_fast()) {
1713 if (alertpanel(_("Queued messages"),
1714 _("Some unsent messages are queued. Exit now?"),
1715 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL)
1716 != G_ALERTALTERNATE) {
1717 main_window_popup(mainwin);
1721 manage_window_focus_in(mainwin->window, NULL, NULL);
1725 #ifdef HAVE_VALGRIND
1726 if (RUNNING_ON_VALGRIND) {
1727 summary_clear_list(mainwin->summaryview);
1730 if (folderview_get_selected_item(mainwin->folderview))
1731 folder_item_close(folderview_get_selected_item(mainwin->folderview));
1735 gboolean claws_is_exiting(void)
1740 gboolean claws_is_starting(void)
1746 * CLAWS: want this public so crash dialog can delete the
1749 gchar *claws_get_socket_name(void)
1751 static gchar *filename = NULL;
1752 const gchar *socket_dir = NULL;
1754 if (rc_dir_is_alt())
1755 socket_dir = get_rc_dir();
1757 socket_dir = g_get_tmp_dir();
1758 if (filename == NULL) {
1759 filename = g_strdup_printf("%s%cclaws-mail-%d",
1760 socket_dir, G_DIR_SEPARATOR,
1771 static gchar *get_crashfile_name(void)
1773 static gchar *filename = NULL;
1775 if (filename == NULL) {
1776 filename = g_strdup_printf("%s%cclaws-crashed",
1777 get_tmp_dir(), G_DIR_SEPARATOR);
1783 static gint prohibit_duplicate_launch(void)
1788 path = claws_get_socket_name();
1789 uxsock = fd_connect_unix(path);
1791 if (x_display == NULL)
1792 x_display = g_strdup(g_getenv("DISPLAY"));
1796 return fd_open_unix(path);
1799 /* remote command mode */
1801 debug_print("another Claws Mail instance is already running.\n");
1803 if (cmd.receive_all) {
1804 fd_write_all(uxsock, "receive_all\n", 12);
1805 } else if (cmd.receive) {
1806 fd_write_all(uxsock, "receive\n", 8);
1807 } else if (cmd.compose && cmd.attach_files) {
1808 gchar *str, *compose_str;
1811 if (cmd.compose_mailto) {
1812 compose_str = g_strdup_printf("compose_attach %s\n",
1813 cmd.compose_mailto);
1815 compose_str = g_strdup("compose_attach\n");
1818 fd_write_all(uxsock, compose_str, strlen(compose_str));
1819 g_free(compose_str);
1821 for (i = 0; i < cmd.attach_files->len; i++) {
1822 str = g_ptr_array_index(cmd.attach_files, i);
1823 fd_write_all(uxsock, str, strlen(str));
1824 fd_write_all(uxsock, "\n", 1);
1827 fd_write_all(uxsock, ".\n", 2);
1828 } else if (cmd.compose) {
1831 if (cmd.compose_mailto) {
1832 compose_str = g_strdup_printf
1833 ("compose %s\n", cmd.compose_mailto);
1835 compose_str = g_strdup("compose\n");
1838 fd_write_all(uxsock, compose_str, strlen(compose_str));
1839 g_free(compose_str);
1840 } else if (cmd.subscribe) {
1841 gchar *str = g_strdup_printf("subscribe %s\n", cmd.subscribe_uri);
1842 fd_write_all(uxsock, str, strlen(str));
1844 } else if (cmd.send) {
1845 fd_write_all(uxsock, "send\n", 5);
1846 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
1847 fd_write(uxsock, "online\n", 6);
1848 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
1849 fd_write(uxsock, "offline\n", 7);
1850 } else if (cmd.status || cmd.status_full) {
1851 gchar buf[BUFFSIZE];
1853 const gchar *command;
1857 command = cmd.status_full ? "status-full\n" : "status\n";
1858 folders = cmd.status_full ? cmd.status_full_folders :
1861 fd_write_all(uxsock, command, strlen(command));
1862 for (i = 0; folders && i < folders->len; ++i) {
1863 folder = g_ptr_array_index(folders, i);
1864 fd_write_all(uxsock, folder, strlen(folder));
1865 fd_write_all(uxsock, "\n", 1);
1867 fd_write_all(uxsock, ".\n", 2);
1869 fd_gets(uxsock, buf, sizeof(buf));
1870 if (!strncmp(buf, ".\n", 2)) break;
1873 } else if (cmd.exit) {
1874 fd_write_all(uxsock, "exit\n", 5);
1875 } else if (cmd.target) {
1876 gchar *str = g_strdup_printf("select %s\n", cmd.target);
1877 fd_write_all(uxsock, str, strlen(str));
1881 fd_write_all(uxsock, "get_display\n", 12);
1882 memset(buf, 0, sizeof(buf));
1883 fd_gets(uxsock, buf, sizeof(buf));
1884 if (strcmp2(buf, x_display)) {
1885 g_print("Claws Mail is already running on display %s.\n",
1889 uxsock = fd_connect_unix(path);
1890 fd_write_all(uxsock, "popup\n", 6);
1898 static gint lock_socket_remove(void)
1903 if (lock_socket < 0) {
1907 if (lock_socket_tag > 0) {
1908 gdk_input_remove(lock_socket_tag);
1910 fd_close(lock_socket);
1911 filename = claws_get_socket_name();
1918 static GPtrArray *get_folder_item_list(gint sock)
1920 gchar buf[BUFFSIZE];
1922 GPtrArray *folders = NULL;
1925 fd_gets(sock, buf, sizeof(buf));
1926 if (!strncmp(buf, ".\n", 2)) {
1931 folders = g_ptr_array_new();
1933 item = folder_find_item_from_identifier(buf);
1935 g_ptr_array_add(folders, item);
1937 g_warning("no such folder: %s\n", buf);
1944 static void lock_socket_input_cb(gpointer data,
1946 GdkInputCondition condition)
1948 MainWindow *mainwin = (MainWindow *)data;
1950 gchar buf[BUFFSIZE];
1952 sock = fd_accept(source);
1953 fd_gets(sock, buf, sizeof(buf));
1955 if (!strncmp(buf, "popup", 5)) {
1956 main_window_popup(mainwin);
1957 } else if (!strncmp(buf, "get_display", 11)) {
1958 fd_write_all(sock, x_display, strlen(x_display));
1959 } else if (!strncmp(buf, "receive_all", 11)) {
1960 inc_all_account_mail(mainwin, FALSE,
1961 prefs_common.newmail_notify_manu);
1962 } else if (!strncmp(buf, "receive", 7)) {
1963 inc_mail(mainwin, prefs_common.newmail_notify_manu);
1964 } else if (!strncmp(buf, "compose_attach", 14)) {
1968 mailto = g_strdup(buf + strlen("compose_attach") + 1);
1969 files = g_ptr_array_new();
1970 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
1971 if (buf[0] == '.' && buf[1] == '\n') {
1975 g_ptr_array_add(files, g_strdup(buf));
1977 open_compose_new(mailto, files);
1978 ptr_array_free_strings(files);
1979 g_ptr_array_free(files, TRUE);
1981 } else if (!strncmp(buf, "compose", 7)) {
1982 open_compose_new(buf + strlen("compose") + 1, NULL);
1983 } else if (!strncmp(buf, "subscribe", 9)) {
1984 main_window_popup(mainwin);
1985 folder_subscribe(buf + strlen("subscribe") + 1);
1986 } else if (!strncmp(buf, "send", 4)) {
1988 } else if (!strncmp(buf, "online", 6)) {
1989 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1990 } else if (!strncmp(buf, "offline", 7)) {
1991 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1992 } else if (!strncmp(buf, "status-full", 11) ||
1993 !strncmp(buf, "status", 6)) {
1997 folders = get_folder_item_list(sock);
1998 status = folder_get_status
1999 (folders, !strncmp(buf, "status-full", 11));
2000 fd_write_all(sock, status, strlen(status));
2001 fd_write_all(sock, ".\n", 2);
2003 if (folders) g_ptr_array_free(folders, TRUE);
2004 } else if (!strncmp(buf, "select ", 7)) {
2005 const gchar *target = buf+7;
2006 mainwindow_jump_to(target, TRUE);
2007 } else if (!strncmp(buf, "exit", 4)) {
2008 app_will_exit(NULL, mainwin);
2014 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
2019 Xstrdup_a(addr, address, return);
2023 compose_new(NULL, addr, attach_files);
2026 static void send_queue(void)
2029 gchar *errstr = NULL;
2030 gboolean error = FALSE;
2031 for (list = folder_get_list(); list != NULL; list = list->next) {
2032 Folder *folder = list->data;
2034 if (folder->queue) {
2035 gint res = procmsg_send_queue
2036 (folder->queue, prefs_common.savemsg,
2040 folder_item_scan(folder->queue);
2048 alertpanel_error_log(_("Some errors occurred "
2049 "while sending queued messages:\n%s"), errstr);
2052 alertpanel_error_log("Some errors occurred "
2053 "while sending queued messages.");
2057 static void quit_signal_handler(int sig)
2059 debug_print("Quitting on signal %d\n", sig);
2061 g_timeout_add(0, clean_quit, NULL);
2064 static void install_basic_sighandlers()
2068 struct sigaction act;
2073 sigaddset(&mask, SIGTERM);
2076 sigaddset(&mask, SIGINT);
2079 sigaddset(&mask, SIGHUP);
2082 act.sa_handler = quit_signal_handler;
2087 sigaction(SIGTERM, &act, 0);
2090 sigaction(SIGINT, &act, 0);
2093 sigaction(SIGHUP, &act, 0);
2096 sigprocmask(SIG_UNBLOCK, &mask, 0);
2097 #endif /* !G_OS_WIN32 */
2101 osso_context_t *get_osso_context(void)
2103 return static_osso_context;