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>
45 #include <X11/SM/SMlib.h>
50 GCRY_THREAD_OPTION_PTHREAD_IMPL;
54 #ifdef HAVE_STARTUP_NOTIFICATION
55 # define SN_API_NOT_YET_FROZEN
56 # include <libsn/sn-launchee.h>
57 # include <gdk/gdkx.h>
60 #ifdef HAVE_NETWORKMANAGER_SUPPORT
61 #include <dbus/dbus-glib.h>
62 #include <NetworkManager.h>
67 #include "mainwindow.h"
68 #include "folderview.h"
69 #include "image_viewer.h"
70 #include "summaryview.h"
71 #include "prefs_common.h"
72 #include "prefs_account.h"
73 #include "prefs_actions.h"
74 #include "prefs_ext_prog.h"
75 #include "prefs_fonts.h"
76 #include "prefs_image_viewer.h"
77 #include "prefs_message.h"
78 #include "prefs_receive.h"
79 #include "prefs_msg_colors.h"
80 #include "prefs_quote.h"
81 #include "prefs_spelling.h"
82 #include "prefs_summaries.h"
83 #include "prefs_themes.h"
84 #include "prefs_other.h"
85 #include "prefs_logging.h"
86 #include "prefs_send.h"
87 #include "prefs_wrapping.h"
88 #include "prefs_compose_writing.h"
89 #include "prefs_display_header.h"
94 #include "manage_window.h"
95 #include "alertpanel.h"
96 #include "statusbar.h"
97 #include "addressbook.h"
102 #include "gtkutils.h"
105 #include "prefs_toolbar.h"
108 #include "imap_gtk.h"
109 #include "news_gtk.h"
113 #include "imap-thread.h"
114 #include "nntp-thread.h"
116 #include "stock_pixmap.h"
118 #include "valgrind.h"
120 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
132 #include <hildon/hildon-banner.h>
133 #include <hildon/hildon-program.h>
135 #include <hildon-widgets/hildon-banner.h>
136 #include <hildon-widgets/hildon-program.h>
138 #include <gtk/gtkmain.h>
140 #include <libgnomevfs/gnome-vfs-volume.h>
141 #include <libgnomevfs/gnome-vfs-volume-monitor.h>
142 #include <libgnomevfs/gnome-vfs-utils.h>
144 #define OSSO_NAME "claws_mail"
145 #define OSSO_SERVICE "com.nokia."OSSO_NAME
146 #define OSSO_OBJECT "/com/nokia/"OSSO_NAME
147 #define OSSO_IFACE "com.nokia."OSSO_NAME
149 typedef struct _AppData AppData;
151 HildonProgram *program;
152 HildonWindow *window;
153 osso_context_t *osso_context;
156 static GnomeVFSVolumeMonitor *volmon;
159 #ifdef HAVE_NETWORKMANAGER_SUPPORT
160 /* Went offline due to NetworkManager */
161 static gboolean went_offline_nm;
168 HildonProgram *hildon_program;
171 #ifdef HAVE_STARTUP_NOTIFICATION
172 static SnLauncheeContext *sn_context = NULL;
173 static SnDisplay *sn_display = NULL;
176 static gint lock_socket = -1;
177 static gint lock_socket_tag = 0;
178 static gchar *x_display = NULL;
181 ONLINE_MODE_DONT_CHANGE,
186 static struct RemoteCmd {
188 gboolean receive_all;
190 const gchar *compose_mailto;
191 GPtrArray *attach_files;
193 gboolean status_full;
194 GPtrArray *status_folders;
195 GPtrArray *status_full_folders;
202 const gchar *subscribe_uri;
206 static void parse_cmd_opt(int argc, char *argv[]);
208 static gint prohibit_duplicate_launch (void);
209 static gchar * get_crashfile_name (void);
210 static gint lock_socket_remove (void);
211 static void lock_socket_input_cb (gpointer data,
213 GdkInputCondition condition);
215 static void open_compose_new (const gchar *address,
216 GPtrArray *attach_files);
218 static void send_queue (void);
219 static void initial_processing (FolderItem *item, gpointer data);
220 static void quit_signal_handler (int sig);
221 static void install_basic_sighandlers (void);
222 #if (defined linux && defined SIGIO)
223 static void install_memory_sighandler (void);
225 static void exit_claws (MainWindow *mainwin);
227 #ifdef HAVE_NETWORKMANAGER_SUPPORT
228 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
232 #define MAKE_DIR_IF_NOT_EXIST(dir) \
234 if (!is_dir_exist(dir)) { \
235 if (is_file_exist(dir)) { \
237 (_("File '%s' already exists.\n" \
238 "Can't create folder."), \
242 if (make_dir(dir) < 0) \
247 static MainWindow *static_mainwindow;
250 static osso_context_t *static_osso_context;
252 void exit_event_handler(gboolean die_now, gpointer data)
255 appdata = (AppData *) data;
256 /* Do whatever application needs to do before exiting */
257 exit_claws(static_mainwindow);
258 hildon_banner_show_information(GTK_WIDGET(appdata->window), NULL,
263 /* Callback for hardware D-BUS events */
264 void hw_event_handler(osso_hw_state_t *state, gpointer data)
267 appdata = (AppData *) data;
269 if (state->shutdown_ind) {
270 exit_claws(static_mainwindow);
271 hildon_banner_show_information(GTK_WIDGET(appdata->window), NULL,
276 /* Callback for normal D-BUS messages */
277 gint dbus_req_handler(const gchar * interface, const gchar * method,
278 GArray * arguments, gpointer data,
282 appdata = (AppData *) data;
284 if (!strcmp(method, "top_application")) {
285 osso_rpc_free_val(retval);
288 osso_system_note_infoprint(appdata->osso_context, method, retval);
289 osso_rpc_free_val(retval);
294 static gboolean emergency_exit = FALSE;
296 #ifdef HAVE_STARTUP_NOTIFICATION
297 static void sn_error_trap_push(SnDisplay *display, Display *xdisplay)
299 gdk_error_trap_push();
302 static void sn_error_trap_pop(SnDisplay *display, Display *xdisplay)
304 gdk_error_trap_pop();
307 static void startup_notification_complete(gboolean with_window)
310 GtkWidget *hack = NULL;
313 /* this is needed to make the startup notification leave,
314 * if we have been launched from a menu.
315 * We have to display a window, so let it be very little */
316 hack = gtk_window_new(GTK_WINDOW_POPUP);
317 gtk_widget_set_uposition(hack, 0, 0);
318 gtk_widget_set_size_request(hack, 1, 1);
319 gtk_widget_show(hack);
322 xdisplay = GDK_DISPLAY();
323 sn_display = sn_display_new(xdisplay,
326 sn_context = sn_launchee_context_new_from_environment(sn_display,
327 DefaultScreen(xdisplay));
329 if (sn_context != NULL) {
330 sn_launchee_context_complete(sn_context);
331 sn_launchee_context_unref(sn_context);
332 sn_display_unref(sn_display);
335 gtk_widget_destroy(hack);
338 #endif /* HAVE_STARTUP_NOTIFICATION */
340 static void claws_gtk_idle(void)
342 while(gtk_events_pending()) {
343 gtk_main_iteration();
348 static gboolean sc_starting = FALSE;
350 static gboolean defer_check_all(void *data)
352 gboolean autochk = GPOINTER_TO_INT(data);
354 inc_all_account_mail(static_mainwindow, autochk,
355 prefs_common.newmail_notify_manu);
359 main_window_set_menu_sensitive(static_mainwindow);
360 toolbar_main_set_sensitive(static_mainwindow);
365 static gboolean defer_check(void *data)
367 inc_mail(static_mainwindow, prefs_common.newmail_notify_manu);
371 main_window_set_menu_sensitive(static_mainwindow);
372 toolbar_main_set_sensitive(static_mainwindow);
377 static gboolean defer_jump(void *data)
379 if (cmd.receive_all) {
380 defer_check_all(GINT_TO_POINTER(FALSE));
381 } else if (prefs_common.chk_on_startup) {
382 defer_check_all(GINT_TO_POINTER(TRUE));
383 } else if (cmd.receive) {
386 mainwindow_jump_to(data, FALSE);
389 main_window_set_menu_sensitive(static_mainwindow);
390 toolbar_main_set_sensitive(static_mainwindow);
395 static void chk_update_val(GtkWidget *widget, gpointer data)
397 gboolean *val = (gboolean *)data;
398 *val = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
401 static gboolean migrate_old_config(const gchar *old_cfg_dir, const gchar *new_cfg_dir, const gchar *oldversion)
403 gchar *message = g_strdup_printf(_("Configuration for %s found.\n"
404 "Do you want to migrate this configuration?"), oldversion);
405 gchar *message2 = g_strdup_printf(_("\n\nYour Sylpheed filtering rules can be converted by a\n"
406 "script available at %s."), TOOLS_URI);
408 if (!strcmp(oldversion, "Sylpheed"))
409 message = g_strconcat(message, message2, NULL);
413 GtkWidget *window = NULL;
414 GtkWidget *keep_backup_chk;
415 GtkTooltips *tips = gtk_tooltips_new();
416 gboolean backup = TRUE;
418 keep_backup_chk = gtk_check_button_new_with_label (_("Keep old configuration"));
419 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_backup_chk), TRUE);
420 gtk_tooltips_set_tip(GTK_TOOLTIPS(tips), keep_backup_chk,
421 _("Keeping a backup will allow you to go back to an "
422 "older version, but may take a while if you have "
423 "cached IMAP or News data, and will take some extra "
424 "room on your disk."),
427 g_signal_connect(G_OBJECT(keep_backup_chk), "toggled",
428 G_CALLBACK(chk_update_val), &backup);
430 if (alertpanel_full(_("Migration of configuration"), message,
431 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL, FALSE,
432 keep_backup_chk, ALERT_QUESTION, G_ALERTDEFAULT) != G_ALERTALTERNATE) {
436 /* we can either do a fast migration requiring not any extra disk
437 * space, or a slow one that copies the old configuration and leaves
441 window = label_window_create(_("Copying configuration... This may take a while..."));
444 r = copy_dir(old_cfg_dir, new_cfg_dir);
445 label_window_destroy(window);
447 /* if copy failed, we'll remove the partially copied
450 alertpanel_error(_("Migration failed!"));
451 remove_dir_recursive(new_cfg_dir);
454 /* fast mode failed, but we don't want backup */
455 remove_dir_recursive(old_cfg_dir);
459 window = label_window_create(_("Migrating configuration..."));
462 r = g_rename(old_cfg_dir, new_cfg_dir);
463 label_window_destroy(window);
465 /* if g_rename failed, we'll try to copy */
467 FILE_OP_ERROR(new_cfg_dir, "g_rename failed, trying copy\n");
474 static int migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
477 gchar *plugin_path, *old_plugin_path, *new_plugin_path;
479 gboolean err = FALSE;
481 oldfp = g_fopen(old_rc, "r");
484 newfp = g_fopen(new_rc, "w");
490 plugin_path = g_strdup(get_plugin_dir());
491 new_plugin_path = g_strdup(plugin_path);
493 if (strstr(plugin_path, "/claws-mail/")) {
494 gchar *end = g_strdup(strstr(plugin_path, "/claws-mail/")+strlen("/claws-mail/"));
495 *(strstr(plugin_path, "/claws-mail/")) = '\0';
496 old_plugin_path = g_strconcat(plugin_path, "/sylpheed-claws/", end, NULL);
499 old_plugin_path = g_strdup(new_plugin_path);
501 debug_print("replacing %s with %s\n", old_plugin_path, new_plugin_path);
502 while (fgets(buf, sizeof(buf), oldfp)) {
503 if (strncmp(buf, old_plugin_path, strlen(old_plugin_path))) {
504 err |= (fputs(buf, newfp) == EOF);
506 debug_print("->replacing %s", buf);
507 debug_print(" with %s%s", new_plugin_path, buf+strlen(old_plugin_path));
508 err |= (fputs(new_plugin_path, newfp) == EOF);
509 err |= (fputs(buf+strlen(old_plugin_path), newfp) == EOF);
513 g_free(new_plugin_path);
514 g_free(old_plugin_path);
516 if (fclose(newfp) == EOF)
524 sc_client_set_value (MainWindow *mainwin,
535 prop.num_vals = num_vals;
539 if (mainwin->smc_conn)
540 SmcSetProperties ((SmcConn) mainwin->smc_conn, 1, proplist);
543 static void sc_die_callback (SmcConn smc_conn, SmPointer client_data)
548 static void sc_save_complete_callback(SmcConn smc_conn, SmPointer client_data)
552 static void sc_shutdown_cancelled_callback (SmcConn smc_conn, SmPointer client_data)
554 MainWindow *mainwin = (MainWindow *)client_data;
555 if (mainwin->smc_conn)
556 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
559 static void sc_save_yourself_callback (SmcConn smc_conn,
560 SmPointer client_data,
566 MainWindow *mainwin = (MainWindow *)client_data;
567 if (mainwin->smc_conn)
568 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
571 static IceIOErrorHandler sc_ice_installed_handler;
573 static void sc_ice_io_error_handler (IceConn connection)
575 if (sc_ice_installed_handler)
576 (*sc_ice_installed_handler) (connection);
578 static gboolean sc_process_ice_messages (GIOChannel *source,
579 GIOCondition condition,
582 IceConn connection = (IceConn) data;
583 IceProcessMessagesStatus status;
585 status = IceProcessMessages (connection, NULL, NULL);
587 if (status == IceProcessMessagesIOError) {
588 IcePointer context = IceGetConnectionContext (connection);
590 if (context && GTK_IS_OBJECT (context)) {
591 guint disconnect_id = g_signal_lookup ("disconnect", G_OBJECT_TYPE (context));
593 if (disconnect_id > 0)
594 g_signal_emit (context, disconnect_id, 0);
596 IceSetShutdownNegotiation (connection, False);
597 IceCloseConnection (connection);
604 static void new_ice_connection (IceConn connection, IcePointer client_data, Bool opening,
605 IcePointer *watch_data)
611 /* Make sure we don't pass on these file descriptors to any
613 fcntl(IceConnectionNumber(connection),F_SETFD,
614 fcntl(IceConnectionNumber(connection),F_GETFD,0) | FD_CLOEXEC);
616 channel = g_io_channel_unix_new (IceConnectionNumber (connection));
617 input_id = g_io_add_watch (channel,
618 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
619 sc_process_ice_messages,
621 g_io_channel_unref (channel);
623 *watch_data = (IcePointer) GUINT_TO_POINTER (input_id);
625 input_id = GPOINTER_TO_UINT ((gpointer) *watch_data);
626 g_source_remove (input_id);
630 static void sc_session_manager_connect(MainWindow *mainwin)
632 static gboolean connected = FALSE;
633 SmcCallbacks callbacks;
635 IceIOErrorHandler default_handler;
642 sc_ice_installed_handler = IceSetIOErrorHandler (NULL);
643 default_handler = IceSetIOErrorHandler (sc_ice_io_error_handler);
645 if (sc_ice_installed_handler == default_handler)
646 sc_ice_installed_handler = NULL;
648 IceAddConnectionWatch (new_ice_connection, NULL);
651 callbacks.save_yourself.callback = sc_save_yourself_callback;
652 callbacks.die.callback = sc_die_callback;
653 callbacks.save_complete.callback = sc_save_complete_callback;
654 callbacks.shutdown_cancelled.callback = sc_shutdown_cancelled_callback;
656 callbacks.save_yourself.client_data =
657 callbacks.die.client_data =
658 callbacks.save_complete.client_data =
659 callbacks.shutdown_cancelled.client_data = (SmPointer) mainwin;
660 if (g_getenv ("SESSION_MANAGER")) {
661 gchar error_string_ret[256] = "";
663 mainwin->smc_conn = (gpointer)
664 SmcOpenConnection (NULL, mainwin,
665 SmProtoMajor, SmProtoMinor,
666 SmcSaveYourselfProcMask | SmcDieProcMask |
667 SmcSaveCompleteProcMask |
668 SmcShutdownCancelledProcMask,
671 256, error_string_ret);
673 if (error_string_ret[0] || mainwin->smc_conn == NULL)
674 g_warning ("While connecting to session manager:\n%s.",
678 vals = g_new (SmPropValue, 1);
679 vals[0].length = strlen(argv0);
680 vals[0].value = argv0;
681 sc_client_set_value (mainwin, SmCloneCommand, SmLISTofARRAY8, 1, vals);
682 sc_client_set_value (mainwin, SmRestartCommand, SmLISTofARRAY8, 1, vals);
683 sc_client_set_value (mainwin, SmProgram, SmARRAY8, 1, vals);
685 vals[0].length = strlen(g_get_user_name()?g_get_user_name():"");
686 vals[0].value = g_strdup(g_get_user_name()?g_get_user_name():"");
687 sc_client_set_value (mainwin, SmUserID, SmARRAY8, 1, vals);
693 static gboolean sc_exiting = FALSE;
694 static gboolean show_at_startup = TRUE;
695 static gboolean claws_crashed_bool = FALSE;
697 gboolean claws_crashed(void) {
698 return claws_crashed_bool;
701 void main_set_show_at_startup(gboolean show)
703 show_at_startup = show;
707 static void main_vol_mount_cb(GnomeVFSVolumeMonitor *vfs, GnomeVFSVolume *vol, MainWindow *mainwin)
709 gchar *uri = gnome_vfs_volume_get_activation_uri (vol);
710 gchar *mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
713 if(!strcmp(mount_path, prefs_common.data_root)) {
714 gtk_widget_set_sensitive(mainwin->window, TRUE);
720 static void main_vol_unmount_cb(GnomeVFSVolumeMonitor *vfs, GnomeVFSVolume *vol, MainWindow *mainwin)
722 gchar *uri = gnome_vfs_volume_get_activation_uri (vol);
723 gchar *mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
726 if(!strcmp(mount_path, prefs_common.data_root)) {
727 gtk_widget_set_sensitive(mainwin->window, FALSE);
736 static FILE* win32_debug_fp=NULL;
737 static guint win32_log_handler_app_id;
738 static guint win32_log_handler_glib_id;
739 static guint win32_log_handler_gtk_id;
741 static void win32_print_stdout(const gchar *string)
743 if (win32_debug_fp) {
744 fprintf(win32_debug_fp, string);
745 fflush(win32_debug_fp);
749 static void win32_print_stderr(const gchar *string)
751 if (win32_debug_fp) {
752 fprintf(win32_debug_fp, string);
753 fflush(win32_debug_fp);
757 static void win32_log(const gchar *log_domain, GLogLevelFlags log_level, const gchar* message, gpointer user_data)
759 if (win32_debug_fp) {
762 switch(log_level & G_LOG_LEVEL_MASK)
764 case G_LOG_LEVEL_ERROR:
767 case G_LOG_LEVEL_CRITICAL:
770 case G_LOG_LEVEL_WARNING:
773 case G_LOG_LEVEL_MESSAGE:
776 case G_LOG_LEVEL_INFO:
779 case G_LOG_LEVEL_DEBUG:
786 fprintf(win32_debug_fp, "%s: %s: %s", log_domain, type, message);
788 fprintf(win32_debug_fp, "%s: %s", type, message);
789 fflush(win32_debug_fp);
793 static void win32_open_log(void)
795 if (is_file_exist("claws-win32.log")) {
796 if (rename_force("claws-win32.log", "claws-win32.log.bak") < 0)
797 FILE_OP_ERROR("claws-win32.log", "rename");
799 win32_debug_fp = fopen("claws-win32.log", "w");
802 g_set_print_handler(win32_print_stdout);
803 g_set_printerr_handler(win32_print_stdout);
804 win32_log_handler_app_id = g_log_set_handler(NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
805 | G_LOG_FLAG_RECURSION, win32_log, NULL);
806 win32_log_handler_glib_id = g_log_set_handler("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
807 | G_LOG_FLAG_RECURSION, win32_log, NULL);
808 win32_log_handler_gtk_id = g_log_set_handler("Gtk", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
809 | G_LOG_FLAG_RECURSION, win32_log, NULL);
813 static void win32_close_log(void)
817 g_log_remove_handler("", win32_log_handler_app_id);
818 g_log_remove_handler("GLib", win32_log_handler_glib_id);
819 g_log_remove_handler("Gtk", win32_log_handler_gtk_id);
820 fclose(win32_debug_fp);
826 static void main_dump_features_list(gboolean show_debug_only)
827 /* display compiled-in features list */
829 if (show_debug_only && !debug_get_mode())
833 debug_print("GTK+ %d.%d.%d / GLib %d.%d.%d\n",
834 gtk_major_version, gtk_minor_version, gtk_micro_version,
835 glib_major_version, glib_minor_version, glib_micro_version);
837 g_print("GTK+ %d.%d.%d / GLib %d.%d.%d\n",
838 gtk_major_version, gtk_minor_version, gtk_micro_version,
839 glib_major_version, glib_minor_version, glib_micro_version);
841 debug_print("Compiled-in features:\n");
843 g_print("Compiled-in features:\n");
846 debug_print(" compface\n");
848 g_print(" compface\n");
852 debug_print(" aspell\n");
854 g_print(" aspell\n");
858 debug_print(" gnutls\n");
860 g_print(" gnutls\n");
864 debug_print(" ipv6\n");
870 debug_print(" iconv\n");
876 debug_print(" jpilot\n");
878 g_print(" jpilot\n");
882 debug_print(" ldap\n");
888 debug_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
890 g_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
894 debug_print(" gnomeprint\n");
896 g_print(" gnomeprint\n");
900 debug_print(" libsm\n");
904 #if HAVE_NETWORKMANAGER_SUPPORT
906 debug_print(" NetworkManager\n");
908 g_print(" NetworkManager\n");
912 debug_print(" openssl\n");
914 g_print(" openssl\n");
918 int main(int argc, char *argv[])
921 osso_context_t *osso_context;
922 osso_return_t result;
924 #ifdef HAVE_NETWORKMANAGER_SUPPORT
925 DBusGConnection *connection;
931 FolderView *folderview;
933 gboolean crash_file_present = FALSE;
934 gint num_folder_class = 0;
935 gboolean asked_for_migration = FALSE;
936 gboolean start_done = TRUE;
938 START_TIMING("startup");
945 if (!claws_init(&argc, &argv)) {
952 main_dump_features_list(TRUE);
954 prog_version = PROG_VERSION;
955 argv0 = g_strdup(argv[0]);
957 parse_cmd_opt(argc, argv);
959 prefs_prepare_cache();
964 gtk_init(&argc, &argv);
965 crash_main(cmd.crash_params);
971 crash_install_handlers();
973 install_basic_sighandlers();
974 #if (defined linux && defined SIGIO)
975 install_memory_sighandler();
979 /* check and create unix domain socket for remote operation */
981 lock_socket = prohibit_duplicate_launch();
982 if (lock_socket < 0) {
983 #ifdef HAVE_STARTUP_NOTIFICATION
984 if(gtk_init_check(&argc, &argv))
985 startup_notification_complete(TRUE);
990 if (cmd.status || cmd.status_full) {
991 puts("0 Claws Mail not running.");
992 lock_socket_remove();
999 if (!g_thread_supported())
1000 g_thread_init(NULL);
1003 gtk_init(&argc, &argv);
1005 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1006 went_offline_nm = FALSE;
1009 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
1012 debug_print("Failed to open connection to system bus: %s\n", error->message);
1013 g_error_free(error);
1016 proxy = dbus_g_proxy_new_for_name(connection,
1017 "org.freedesktop.NetworkManager",
1018 "/org/freedesktop/NetworkManager",
1019 "org.freedesktop.NetworkManager");
1020 dbus_g_proxy_add_signal(proxy,"StateChange", G_TYPE_UINT, G_TYPE_INVALID);
1021 dbus_g_proxy_connect_signal(proxy, "StateChange",
1022 G_CALLBACK(networkmanager_state_change_cb),
1029 osso_context = osso_initialize(OSSO_SERVICE, "2.8.1", TRUE, NULL);
1030 if (osso_context == NULL) {
1033 hildon_program = HILDON_PROGRAM(hildon_program_get_instance());
1034 static_osso_context = osso_context;
1037 gtk_widget_set_default_colormap(gdk_rgb_get_colormap());
1038 gtk_widget_set_default_visual(gdk_rgb_get_visual());
1040 if (!g_thread_supported()) {
1041 g_error(_("g_thread is not supported by glib.\n"));
1044 /* check that we're not on a too recent/old gtk+ */
1045 #if GTK_CHECK_VERSION(2, 9, 0)
1046 if (gtk_check_version(2, 9, 0) != NULL) {
1047 alertpanel_error(_("Claws Mail has been compiled with "
1048 "a more recent GTK+ library than is "
1049 "currently available. This will cause "
1050 "crashes. You need to upgrade GTK+ or "
1051 "recompile Claws Mail."));
1058 if (gtk_check_version(2, 9, 0) == NULL) {
1059 alertpanel_error(_("Claws Mail has been compiled with "
1060 "an older GTK+ library than is "
1061 "currently available. This will cause "
1062 "crashes. You need to recompile "
1072 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1074 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1077 /* no config dir exists. See if we can migrate an old config. */
1078 if (!is_dir_exist(RC_DIR)) {
1079 prefs_destroy_cache();
1082 /* if one of the old dirs exist, we'll ask if the user
1083 * want to migrates, and r will be TRUE if he said yes
1084 * and migration succeeded, and FALSE otherwise.
1086 if (is_dir_exist(OLD_GTK2_RC_DIR)) {
1087 r = migrate_old_config(OLD_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 2.6.0 (or older)"));
1088 asked_for_migration = TRUE;
1089 } else if (is_dir_exist(OLDER_GTK2_RC_DIR)) {
1090 r = migrate_old_config(OLDER_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.9.15 (or older)"));
1091 asked_for_migration = TRUE;
1092 } else if (is_dir_exist(OLD_GTK1_RC_DIR)) {
1093 r = migrate_old_config(OLD_GTK1_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.0.5 (or older)"));
1094 asked_for_migration = TRUE;
1095 } else if (is_dir_exist(SYLPHEED_RC_DIR)) {
1096 r = migrate_old_config(SYLPHEED_RC_DIR, RC_DIR, "Sylpheed");
1097 asked_for_migration = TRUE;
1100 /* If migration failed or the user didn't want to do it,
1101 * we create a new one (and we'll hit wizard later).
1103 if (r == FALSE && !is_dir_exist(RC_DIR) && make_dir(RC_DIR) < 0) {
1112 if (!is_file_exist(RC_DIR G_DIR_SEPARATOR_S COMMON_RC) &&
1113 is_file_exist(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC)) {
1114 /* post 2.6 name change */
1115 migrate_common_rc(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC,
1116 RC_DIR G_DIR_SEPARATOR_S COMMON_RC);
1120 plugin_load_all("Common");
1122 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
1123 gtk_rc_parse(userrc);
1126 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1127 gtk_accel_map_load (userrc);
1131 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_rc_dir(), 1, win32_close_log(););
1133 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
1136 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
1137 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
1138 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
1139 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
1140 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
1141 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR);
1143 crash_file_present = is_file_exist(get_crashfile_name());
1144 /* remove temporary files */
1145 remove_all_files(get_tmp_dir());
1146 remove_all_files(get_mime_tmp_dir());
1148 if (!cmd.crash && crash_file_present)
1149 claws_crashed_bool = TRUE;
1151 if (is_file_exist("claws.log")) {
1152 if (rename_force("claws.log", "claws.log.bak") < 0)
1153 FILE_OP_ERROR("claws.log", "rename");
1155 set_log_file(LOG_PROTOCOL, "claws.log");
1157 if (is_file_exist("filtering.log")) {
1158 if (rename_force("filtering.log", "filtering.log.bak") < 0)
1159 FILE_OP_ERROR("filtering.log", "rename");
1161 set_log_file(LOG_DEBUG_FILTERING, "filtering.log");
1164 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1166 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1169 folder_system_init();
1170 prefs_common_read_config();
1172 prefs_themes_init();
1174 prefs_ext_prog_init();
1175 prefs_wrapping_init();
1176 prefs_compose_writing_init();
1177 prefs_msg_colors_init();
1178 image_viewer_init();
1179 prefs_image_viewer_init();
1181 prefs_summaries_init();
1182 prefs_message_init();
1184 prefs_logging_init();
1185 prefs_receive_init();
1189 gtkaspell_checkers_init();
1190 prefs_spelling_init();
1193 sock_set_io_timeout(prefs_common.io_timeout_secs);
1194 prefs_actions_read_config();
1195 prefs_display_header_read_config();
1196 /* prefs_filtering_read_config(); */
1197 addressbook_read_file();
1199 gtkut_widget_init();
1200 stock_pixbuf_gdk(NULL, STOCK_PIXMAP_CLAWS_MAIL_ICON, &icon);
1201 gtk_window_set_default_icon(icon);
1203 folderview_initialize();
1209 mainwin = main_window_create();
1211 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1212 networkmanager_state_change_cb(proxy,NULL,mainwin);
1217 appdata = g_new0(AppData, 1);
1218 appdata->program = hildon_program;
1219 appdata->window = HILDON_WINDOW(mainwin->window);
1220 appdata->osso_context = osso_context;
1221 result = osso_rpc_set_cb_f(appdata->osso_context,
1225 dbus_req_handler, appdata);
1226 if (result != OSSO_OK) {
1231 /* Add handler for Exit D-BUS messages */
1232 result = osso_application_set_exit_cb(appdata->osso_context,
1234 (gpointer) appdata);
1235 if (result != OSSO_OK) {
1239 osso_hw_set_event_cb( appdata->osso_context,
1240 NULL, hw_event_handler, (gpointer) appdata );
1242 manage_window_focus_in(mainwin->window, NULL, NULL);
1243 folderview = mainwin->folderview;
1245 gtk_clist_freeze(GTK_CLIST(mainwin->folderview->ctree));
1246 folder_item_update_freeze();
1248 /* register the callback of unix domain socket input */
1250 lock_socket_tag = gdk_input_add(lock_socket,
1251 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
1252 lock_socket_input_cb,
1257 prefs_account_init();
1258 account_read_config_all();
1260 /* If we can't read a folder list or don't have accounts,
1261 * it means the configuration's not done. Either this is
1262 * a brand new install, either a failed/refused migration.
1263 * So we'll start the wizard.
1265 if (folder_read_list() < 0) {
1266 prefs_destroy_cache();
1268 /* if run_wizard returns FALSE it's because it's
1269 * been cancelled. We can't do much but exit.
1270 * however, if the user was asked for a migration,
1271 * we remove the newly created directory so that
1272 * he's asked again for migration on next launch.*/
1273 if (!run_wizard(mainwin, TRUE)) {
1274 if (asked_for_migration)
1275 remove_dir_recursive(RC_DIR);
1281 main_window_reflect_prefs_all_now();
1282 folder_write_list();
1285 if (!account_get_list()) {
1286 prefs_destroy_cache();
1287 if (!run_wizard(mainwin, FALSE)) {
1288 if (asked_for_migration)
1289 remove_dir_recursive(RC_DIR);
1295 if(!account_get_list()) {
1296 exit_claws(mainwin);
1305 toolbar_main_set_sensitive(mainwin);
1306 main_window_set_menu_sensitive(mainwin);
1308 /* if crashed, show window early so that the user
1309 * sees what's happening */
1310 if (claws_crashed())
1311 main_window_popup(mainwin);
1314 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
1317 #ifdef HAVE_LIBETPAN
1318 imap_main_init(prefs_common.skip_ssl_cert_check);
1319 imap_main_set_timeout(prefs_common.io_timeout_secs);
1320 nntp_main_init(prefs_common.skip_ssl_cert_check);
1322 account_set_missing_folder();
1323 folder_set_missing_folders();
1324 folderview_set(folderview);
1326 prefs_matcher_read_config();
1328 /* make one all-folder processing before using claws */
1329 main_window_cursor_wait(mainwin);
1330 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
1332 /* if claws crashed, rebuild caches */
1333 if (claws_crashed()) {
1335 debug_print("Claws Mail crashed, checking for new messages in local folders\n");
1336 folder_item_update_thaw();
1337 folderview_check_new(NULL);
1338 folder_clean_cache_memory_force();
1339 folder_item_update_freeze();
1341 /* make the crash-indicator file */
1342 str_write_to_file("foo", get_crashfile_name());
1344 inc_autocheck_timer_init(mainwin);
1346 /* ignore SIGPIPE signal for preventing sudden death of program */
1348 signal(SIGPIPE, SIG_IGN);
1350 if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
1351 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1353 if (cmd.online_mode == ONLINE_MODE_ONLINE) {
1354 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1357 if (cmd.status_folders) {
1358 g_ptr_array_free(cmd.status_folders, TRUE);
1359 cmd.status_folders = NULL;
1361 if (cmd.status_full_folders) {
1362 g_ptr_array_free(cmd.status_full_folders, TRUE);
1363 cmd.status_full_folders = NULL;
1366 claws_register_idle_function(claws_gtk_idle);
1368 prefs_toolbar_init();
1370 num_folder_class = g_list_length(folder_get_list());
1372 plugin_load_all("GTK2");
1374 if (g_list_length(folder_get_list()) != num_folder_class) {
1375 debug_print("new folders loaded, reloading processing rules\n");
1376 prefs_matcher_read_config();
1379 if (plugin_get_unloaded_list() != NULL) {
1380 main_window_cursor_normal(mainwin);
1381 alertpanel_warning(_("Some plugin(s) failed to load. "
1382 "Check the Plugins configuration "
1383 "for more information."));
1384 main_window_cursor_wait(mainwin);
1387 plugin_load_standard_plugins ();
1389 /* if not crashed, show window now */
1390 if (!claws_crashed()) {
1391 /* apart if something told not to show */
1392 if (show_at_startup)
1393 main_window_popup(mainwin);
1396 if (!folder_have_mailbox()) {
1397 prefs_destroy_cache();
1398 main_window_cursor_normal(mainwin);
1399 if (folder_get_list() != NULL) {
1400 alertpanel_error(_("Claws Mail has detected a configured "
1401 "mailbox, but it is incomplete. It is "
1402 "possibly due to a failing IMAP account. Use "
1403 "\"Rebuild folder tree\" on the mailbox parent "
1404 "folder's context menu to try to fix it."));
1406 alertpanel_error(_("Claws Mail has detected a configured "
1407 "mailbox, but could not load it. It is "
1408 "probably provided by an out-of-date "
1409 "external plugin. Please reinstall the "
1410 "plugin and try again."));
1411 exit_claws(mainwin);
1419 static_mainwindow = mainwin;
1422 if (prefs_common.data_root != NULL && *prefs_common.data_root != '\0') {
1423 GnomeVFSVolume *vol = NULL;
1424 gchar *uri, *mount_path;
1426 volmon = gnome_vfs_get_volume_monitor();
1427 vol = gnome_vfs_volume_monitor_get_volume_for_path(volmon, prefs_common.data_root);
1429 uri = gnome_vfs_volume_get_activation_uri (vol);
1430 mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
1433 if (vol == NULL || !gnome_vfs_volume_is_mounted(vol)
1434 || strcmp(mount_path, prefs_common.data_root)) {
1435 alertpanel_error(_("Claws Mail can not start without its data volume (%s)."),
1436 prefs_common.data_root);
1438 gnome_vfs_volume_unref(vol);
1439 exit_claws(mainwin);
1443 gnome_vfs_volume_unref(vol);
1444 g_signal_connect(G_OBJECT(volmon),
1445 "volume-mounted", G_CALLBACK(main_vol_mount_cb), mainwin);
1446 g_signal_connect(G_OBJECT(volmon),
1447 "volume-unmounted", G_CALLBACK(main_vol_unmount_cb), mainwin);
1451 #ifdef HAVE_STARTUP_NOTIFICATION
1452 startup_notification_complete(FALSE);
1455 sc_session_manager_connect(mainwin);
1458 folder_item_update_thaw();
1459 gtk_clist_thaw(GTK_CLIST(mainwin->folderview->ctree));
1460 main_window_cursor_normal(mainwin);
1462 if (!cmd.target && prefs_common.goto_last_folder_on_startup &&
1463 folder_find_item_from_identifier(prefs_common.last_opened_folder) != NULL &&
1465 cmd.target = prefs_common.last_opened_folder;
1468 if (cmd.receive_all && !cmd.target) {
1470 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(FALSE));
1471 } else if (prefs_common.chk_on_startup && !cmd.target) {
1473 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(TRUE));
1474 } else if (cmd.receive && !cmd.target) {
1476 g_timeout_add(1000, defer_check, NULL);
1478 gtk_widget_grab_focus(folderview->ctree);
1481 open_compose_new(cmd.compose_mailto, cmd.attach_files);
1483 if (cmd.attach_files) {
1484 ptr_array_free_strings(cmd.attach_files);
1485 g_ptr_array_free(cmd.attach_files, TRUE);
1486 cmd.attach_files = NULL;
1488 if (cmd.subscribe) {
1489 folder_subscribe(cmd.subscribe_uri);
1498 g_timeout_add(500, defer_jump, (gpointer)cmd.target);
1501 prefs_destroy_cache();
1503 compose_reopen_exit_drafts();
1506 sc_starting = FALSE;
1507 main_window_set_menu_sensitive(mainwin);
1508 toolbar_main_set_sensitive(mainwin);
1515 osso_deinitialize(osso_context);
1517 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1519 g_object_unref(proxy);
1521 dbus_g_connection_unref(connection);
1527 exit_claws(mainwin);
1532 static void save_all_caches(FolderItem *item, gpointer data)
1539 folder_item_close(item);
1542 folder_item_free_cache(item, TRUE);
1545 static void exit_claws(MainWindow *mainwin)
1551 debug_print("shutting down\n");
1552 inc_autocheck_timer_remove();
1554 if (prefs_common.clean_on_exit && !emergency_exit) {
1555 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
1558 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1559 if (prefs_common.work_offline && went_offline_nm)
1560 prefs_common.work_offline = FALSE;
1563 /* save prefs for opened folder */
1564 if(mainwin->folderview->opened) {
1567 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
1568 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
1569 prefs_common.last_opened_folder = folder_item_get_identifier(item);
1572 /* save all state before exiting */
1573 folder_func_to_all_folders(save_all_caches, NULL);
1574 folder_write_list();
1576 main_window_get_size(mainwin);
1577 main_window_get_position(mainwin);
1579 prefs_common_write_config();
1580 account_write_config_all();
1581 addressbook_export_to_file();
1583 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1584 gtk_accel_map_save(filename);
1587 /* delete temporary files */
1588 remove_all_files(get_tmp_dir());
1589 remove_all_files(get_mime_tmp_dir());
1591 close_log_file(LOG_PROTOCOL);
1592 close_log_file(LOG_DEBUG_FILTERING);
1594 #ifdef HAVE_LIBETPAN
1598 /* delete crashfile */
1600 claws_unlink(get_crashfile_name());
1602 lock_socket_remove();
1605 if (mainwin->smc_conn)
1606 SmcCloseConnection ((SmcConn)mainwin->smc_conn, 0, NULL);
1607 mainwin->smc_conn = NULL;
1610 main_window_destroy_all();
1612 plugin_unload_all("GTK2");
1614 prefs_toolbar_done();
1616 addressbook_destroy();
1618 prefs_themes_done();
1620 prefs_ext_prog_done();
1621 prefs_wrapping_done();
1622 prefs_compose_writing_done();
1623 prefs_msg_colors_done();
1624 prefs_image_viewer_done();
1625 image_viewer_done();
1627 prefs_summaries_done();
1628 prefs_message_done();
1630 prefs_receive_done();
1631 prefs_logging_done();
1635 prefs_spelling_done();
1636 gtkaspell_checkers_quit();
1638 plugin_unload_all("Common");
1642 static void parse_cmd_opt(int argc, char *argv[])
1646 for (i = 1; i < argc; i++) {
1647 if (!strncmp(argv[i], "--receive-all", 13)) {
1648 cmd.receive_all = TRUE;
1649 } else if (!strncmp(argv[i], "--receive", 9)) {
1651 } else if (!strncmp(argv[i], "--compose", 9)) {
1652 const gchar *p = argv[i + 1];
1655 cmd.compose_mailto = NULL;
1656 if (p && *p != '\0' && *p != '-') {
1657 if (!strncmp(p, "mailto:", 7)) {
1658 cmd.compose_mailto = p + 7;
1660 cmd.compose_mailto = p;
1664 } else if (!strncmp(argv[i], "--subscribe", 11)) {
1665 const gchar *p = argv[i + 1];
1666 if (p && *p != '\0' && *p != '-') {
1667 cmd.subscribe = TRUE;
1668 cmd.subscribe_uri = p;
1670 } else if (!strncmp(argv[i], "--attach", 8)) {
1671 const gchar *p = argv[i + 1];
1674 while (p && *p != '\0' && *p != '-') {
1675 if (!cmd.attach_files) {
1676 cmd.attach_files = g_ptr_array_new();
1678 if ((file = g_filename_from_uri(p, NULL, NULL)) != NULL) {
1679 if (!is_file_exist(file)) {
1684 if (file == NULL && *p != G_DIR_SEPARATOR) {
1685 file = g_strconcat(claws_get_startup_dir(),
1688 } else if (file == NULL) {
1691 g_ptr_array_add(cmd.attach_files, file);
1695 } else if (!strncmp(argv[i], "--send", 6)) {
1697 } else if (!strncmp(argv[i], "--version-full", 14) ||
1698 !strncmp(argv[i], "-V", 2)) {
1699 g_print("Claws Mail version " VERSION "\n");
1700 main_dump_features_list(FALSE);
1702 } else if (!strncmp(argv[i], "--version", 9) ||
1703 !strncmp(argv[i], "-v", 2)) {
1704 g_print("Claws Mail version " VERSION "\n");
1706 } else if (!strncmp(argv[i], "--status-full", 13)) {
1707 const gchar *p = argv[i + 1];
1709 cmd.status_full = TRUE;
1710 while (p && *p != '\0' && *p != '-') {
1711 if (!cmd.status_full_folders) {
1712 cmd.status_full_folders =
1715 g_ptr_array_add(cmd.status_full_folders,
1720 } else if (!strncmp(argv[i], "--status", 8)) {
1721 const gchar *p = argv[i + 1];
1724 while (p && *p != '\0' && *p != '-') {
1725 if (!cmd.status_folders)
1726 cmd.status_folders = g_ptr_array_new();
1727 g_ptr_array_add(cmd.status_folders,
1732 } else if (!strncmp(argv[i], "--online", 8)) {
1733 cmd.online_mode = ONLINE_MODE_ONLINE;
1734 } else if (!strncmp(argv[i], "--offline", 9)) {
1735 cmd.online_mode = ONLINE_MODE_OFFLINE;
1736 } else if (!strncmp(argv[i], "--help", 6) ||
1737 !strncmp(argv[i], "-h", 2)) {
1738 gchar *base = g_path_get_basename(argv[0]);
1739 g_print(_("Usage: %s [OPTION]...\n"), base);
1741 g_print("%s\n", _(" --compose [address] open composition window"));
1742 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
1743 g_print("%s\n", _(" --attach file1 [file2]...\n"
1744 " open composition window with specified files\n"
1746 g_print("%s\n", _(" --receive receive new messages"));
1747 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
1748 g_print("%s\n", _(" --send send all queued messages"));
1749 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
1750 g_print("%s\n", _(" --status-full [folder]...\n"
1751 " show the status of each folder"));
1752 g_print("%s\n", _(" --select folder[/msg] jumps to the specified folder/message\n"
1753 " folder is a folder id like 'folder/sub_folder'"));
1754 g_print("%s\n", _(" --online switch to online mode"));
1755 g_print("%s\n", _(" --offline switch to offline mode"));
1756 g_print("%s\n", _(" --exit --quit -q exit Claws Mail"));
1757 g_print("%s\n", _(" --debug debug mode"));
1758 g_print("%s\n", _(" --help -h display this help and exit"));
1759 g_print("%s\n", _(" --version -v output version information and exit"));
1760 g_print("%s\n", _(" --version-full -V output version and built-in features information and exit"));
1761 g_print("%s\n", _(" --config-dir output configuration directory"));
1762 g_print("%s\n", _(" --alternate-config-dir [dir]\n"
1763 " use specified configuration directory"));
1767 } else if (!strncmp(argv[i], "--crash", 7)) {
1769 cmd.crash_params = g_strdup(argv[i + 1]);
1771 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
1772 g_print(RC_DIR "\n");
1774 } else if (!strncmp(argv[i], "--alternate-config-dir", sizeof "--alternate-config-dir" - 1) && i+1 < argc) {
1775 set_rc_dir(argv[i+1]);
1776 } else if (!strncmp(argv[i], "--exit", 6) ||
1777 !strncmp(argv[i], "--quit", 6) ||
1778 !strncmp(argv[i], "-q", 2)) {
1780 } else if (!strncmp(argv[i], "--select", 8) && i+1 < argc) {
1781 cmd.target = argv[i+1];
1782 } else if (i == 1 && argc == 2) {
1783 /* only one parameter. Do something intelligent about it */
1784 if (strstr(argv[i], "@") && !strstr(argv[i], "://")) {
1785 const gchar *p = argv[i];
1788 cmd.compose_mailto = NULL;
1789 if (p && *p != '\0' && *p != '-') {
1790 if (!strncmp(p, "mailto:", 7)) {
1791 cmd.compose_mailto = p + 7;
1793 cmd.compose_mailto = p;
1796 } else if (!strncmp(argv[i], "file://", 7)) {
1797 cmd.target = argv[i];
1798 } else if (!strncmp(argv[i], "?attach=file://", strlen("?attach=file://"))) {
1800 cmd.compose_mailto = argv[i];
1801 } else if (strstr(argv[i], "://")) {
1802 const gchar *p = argv[i];
1803 if (p && *p != '\0' && *p != '-') {
1804 cmd.subscribe = TRUE;
1805 cmd.subscribe_uri = p;
1807 } else if (!strcmp(argv[i], "--sync")) {
1809 } else if (is_dir_exist(argv[i]) || is_file_exist(argv[i])) {
1810 cmd.target = argv[i];
1812 g_print(_("Unknown option\n"));
1818 if (cmd.attach_files && cmd.compose == FALSE) {
1820 cmd.compose_mailto = NULL;
1824 static void initial_processing(FolderItem *item, gpointer data)
1826 MainWindow *mainwin = (MainWindow *)data;
1829 g_return_if_fail(item);
1830 buf = g_strdup_printf(_("Processing (%s)..."),
1833 : _("top level folder"));
1837 if (item->prefs->enable_processing) {
1838 item->processing_pending = TRUE;
1839 folder_item_apply_processing(item);
1840 item->processing_pending = FALSE;
1843 STATUSBAR_POP(mainwin);
1846 static gboolean draft_all_messages(void)
1848 GList *compose_list = NULL;
1850 compose_clear_exit_drafts();
1851 compose_list = compose_get_compose_list();
1852 while (compose_list != NULL) {
1853 Compose *c = (Compose*)compose_list->data;
1854 if (!compose_draft(c, COMPOSE_DRAFT_FOR_EXIT))
1856 compose_list = compose_get_compose_list();
1860 gboolean clean_quit(gpointer data)
1862 static gboolean firstrun = TRUE;
1869 /*!< Good idea to have the main window stored in a
1870 * static variable so we can check that variable
1871 * to see if we're really allowed to do things
1872 * that actually the spawner is supposed to
1873 * do (like: sending mail, composing messages).
1874 * Because, really, if we're the spawnee, and
1875 * we touch GTK stuff, we're hosed. See the
1878 /* FIXME: Use something else to signal that we're
1879 * in the original spawner, and not in a spawned
1881 if (!static_mainwindow) {
1885 draft_all_messages();
1886 emergency_exit = TRUE;
1887 exit_claws(static_mainwindow);
1893 void app_will_exit(GtkWidget *widget, gpointer data)
1895 MainWindow *mainwin = data;
1897 if (sc_exiting == TRUE) {
1898 debug_print("exit pending\n");
1902 debug_print("exiting\n");
1903 if (compose_get_compose_list()) {
1904 if (!draft_all_messages()) {
1905 main_window_popup(mainwin);
1911 if (prefs_common.warn_queued_on_exit && procmsg_have_queued_mails_fast()) {
1912 if (alertpanel(_("Queued messages"),
1913 _("Some unsent messages are queued. Exit now?"),
1914 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL)
1915 != G_ALERTALTERNATE) {
1916 main_window_popup(mainwin);
1920 manage_window_focus_in(mainwin->window, NULL, NULL);
1924 #ifdef HAVE_VALGRIND
1925 if (RUNNING_ON_VALGRIND) {
1926 summary_clear_list(mainwin->summaryview);
1929 if (folderview_get_selected_item(mainwin->folderview))
1930 folder_item_close(folderview_get_selected_item(mainwin->folderview));
1934 gboolean claws_is_exiting(void)
1939 gboolean claws_is_starting(void)
1945 * CLAWS: want this public so crash dialog can delete the
1948 gchar *claws_get_socket_name(void)
1950 static gchar *filename = NULL;
1951 const gchar *socket_dir = NULL;
1953 if (rc_dir_is_alt())
1954 socket_dir = get_rc_dir();
1956 socket_dir = g_get_tmp_dir();
1957 if (filename == NULL) {
1958 filename = g_strdup_printf("%s%cclaws-mail-%d",
1959 socket_dir, G_DIR_SEPARATOR,
1970 static gchar *get_crashfile_name(void)
1972 static gchar *filename = NULL;
1974 if (filename == NULL) {
1975 filename = g_strdup_printf("%s%cclaws-crashed",
1976 get_tmp_dir(), G_DIR_SEPARATOR);
1982 static gint prohibit_duplicate_launch(void)
1987 path = claws_get_socket_name();
1988 uxsock = fd_connect_unix(path);
1990 if (x_display == NULL)
1991 x_display = g_strdup(g_getenv("DISPLAY"));
1995 return fd_open_unix(path);
1998 /* remote command mode */
2000 debug_print("another Claws Mail instance is already running.\n");
2002 if (cmd.receive_all) {
2003 fd_write_all(uxsock, "receive_all\n", 12);
2004 } else if (cmd.receive) {
2005 fd_write_all(uxsock, "receive\n", 8);
2006 } else if (cmd.compose && cmd.attach_files) {
2007 gchar *str, *compose_str;
2010 if (cmd.compose_mailto) {
2011 compose_str = g_strdup_printf("compose_attach %s\n",
2012 cmd.compose_mailto);
2014 compose_str = g_strdup("compose_attach\n");
2017 fd_write_all(uxsock, compose_str, strlen(compose_str));
2018 g_free(compose_str);
2020 for (i = 0; i < cmd.attach_files->len; i++) {
2021 str = g_ptr_array_index(cmd.attach_files, i);
2022 fd_write_all(uxsock, str, strlen(str));
2023 fd_write_all(uxsock, "\n", 1);
2026 fd_write_all(uxsock, ".\n", 2);
2027 } else if (cmd.compose) {
2030 if (cmd.compose_mailto) {
2031 compose_str = g_strdup_printf
2032 ("compose %s\n", cmd.compose_mailto);
2034 compose_str = g_strdup("compose\n");
2037 fd_write_all(uxsock, compose_str, strlen(compose_str));
2038 g_free(compose_str);
2039 } else if (cmd.subscribe) {
2040 gchar *str = g_strdup_printf("subscribe %s\n", cmd.subscribe_uri);
2041 fd_write_all(uxsock, str, strlen(str));
2043 } else if (cmd.send) {
2044 fd_write_all(uxsock, "send\n", 5);
2045 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
2046 fd_write(uxsock, "online\n", 6);
2047 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
2048 fd_write(uxsock, "offline\n", 7);
2049 } else if (cmd.status || cmd.status_full) {
2050 gchar buf[BUFFSIZE];
2052 const gchar *command;
2056 command = cmd.status_full ? "status-full\n" : "status\n";
2057 folders = cmd.status_full ? cmd.status_full_folders :
2060 fd_write_all(uxsock, command, strlen(command));
2061 for (i = 0; folders && i < folders->len; ++i) {
2062 folder = g_ptr_array_index(folders, i);
2063 fd_write_all(uxsock, folder, strlen(folder));
2064 fd_write_all(uxsock, "\n", 1);
2066 fd_write_all(uxsock, ".\n", 2);
2068 fd_gets(uxsock, buf, sizeof(buf));
2069 if (!strncmp(buf, ".\n", 2)) break;
2072 } else if (cmd.exit) {
2073 fd_write_all(uxsock, "exit\n", 5);
2074 } else if (cmd.target) {
2075 gchar *str = g_strdup_printf("select %s\n", cmd.target);
2076 fd_write_all(uxsock, str, strlen(str));
2080 fd_write_all(uxsock, "get_display\n", 12);
2081 memset(buf, 0, sizeof(buf));
2082 fd_gets(uxsock, buf, sizeof(buf));
2083 if (strcmp2(buf, x_display)) {
2084 g_print("Claws Mail is already running on display %s.\n",
2088 uxsock = fd_connect_unix(path);
2089 fd_write_all(uxsock, "popup\n", 6);
2097 static gint lock_socket_remove(void)
2102 if (lock_socket < 0) {
2106 if (lock_socket_tag > 0) {
2107 gdk_input_remove(lock_socket_tag);
2109 fd_close(lock_socket);
2110 filename = claws_get_socket_name();
2111 claws_unlink(filename);
2117 static GPtrArray *get_folder_item_list(gint sock)
2119 gchar buf[BUFFSIZE];
2121 GPtrArray *folders = NULL;
2124 fd_gets(sock, buf, sizeof(buf));
2125 if (!strncmp(buf, ".\n", 2)) {
2130 folders = g_ptr_array_new();
2132 item = folder_find_item_from_identifier(buf);
2134 g_ptr_array_add(folders, item);
2136 g_warning("no such folder: %s\n", buf);
2143 static void lock_socket_input_cb(gpointer data,
2145 GdkInputCondition condition)
2147 MainWindow *mainwin = (MainWindow *)data;
2149 gchar buf[BUFFSIZE];
2151 sock = fd_accept(source);
2152 fd_gets(sock, buf, sizeof(buf));
2154 if (!strncmp(buf, "popup", 5)) {
2155 main_window_popup(mainwin);
2156 } else if (!strncmp(buf, "get_display", 11)) {
2157 fd_write_all(sock, x_display, strlen(x_display));
2158 } else if (!strncmp(buf, "receive_all", 11)) {
2159 inc_all_account_mail(mainwin, FALSE,
2160 prefs_common.newmail_notify_manu);
2161 } else if (!strncmp(buf, "receive", 7)) {
2162 inc_mail(mainwin, prefs_common.newmail_notify_manu);
2163 } else if (!strncmp(buf, "compose_attach", 14)) {
2167 mailto = g_strdup(buf + strlen("compose_attach") + 1);
2168 files = g_ptr_array_new();
2169 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
2170 if (buf[0] == '.' && buf[1] == '\n') {
2174 g_ptr_array_add(files, g_strdup(buf));
2176 open_compose_new(mailto, files);
2177 ptr_array_free_strings(files);
2178 g_ptr_array_free(files, TRUE);
2180 } else if (!strncmp(buf, "compose", 7)) {
2181 open_compose_new(buf + strlen("compose") + 1, NULL);
2182 } else if (!strncmp(buf, "subscribe", 9)) {
2183 main_window_popup(mainwin);
2184 folder_subscribe(buf + strlen("subscribe") + 1);
2185 } else if (!strncmp(buf, "send", 4)) {
2187 } else if (!strncmp(buf, "online", 6)) {
2188 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
2189 } else if (!strncmp(buf, "offline", 7)) {
2190 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
2191 } else if (!strncmp(buf, "status-full", 11) ||
2192 !strncmp(buf, "status", 6)) {
2196 folders = get_folder_item_list(sock);
2197 status = folder_get_status
2198 (folders, !strncmp(buf, "status-full", 11));
2199 fd_write_all(sock, status, strlen(status));
2200 fd_write_all(sock, ".\n", 2);
2202 if (folders) g_ptr_array_free(folders, TRUE);
2203 } else if (!strncmp(buf, "select ", 7)) {
2204 const gchar *target = buf+7;
2205 mainwindow_jump_to(target, TRUE);
2206 } else if (!strncmp(buf, "exit", 4)) {
2207 app_will_exit(NULL, mainwin);
2213 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
2218 Xstrdup_a(addr, address, return);
2222 compose_new(NULL, addr, attach_files);
2225 static void send_queue(void)
2228 gchar *errstr = NULL;
2229 gboolean error = FALSE;
2230 for (list = folder_get_list(); list != NULL; list = list->next) {
2231 Folder *folder = list->data;
2233 if (folder->queue) {
2234 gint res = procmsg_send_queue
2235 (folder->queue, prefs_common.savemsg,
2239 folder_item_scan(folder->queue);
2247 alertpanel_error_log(_("Some errors occurred "
2248 "while sending queued messages:\n%s"), errstr);
2251 alertpanel_error_log("Some errors occurred "
2252 "while sending queued messages.");
2256 static void quit_signal_handler(int sig)
2258 debug_print("Quitting on signal %d\n", sig);
2260 g_timeout_add(0, clean_quit, NULL);
2263 static void install_basic_sighandlers()
2267 struct sigaction act;
2272 sigaddset(&mask, SIGTERM);
2275 sigaddset(&mask, SIGINT);
2278 sigaddset(&mask, SIGHUP);
2281 act.sa_handler = quit_signal_handler;
2286 sigaction(SIGTERM, &act, 0);
2289 sigaction(SIGINT, &act, 0);
2292 sigaction(SIGHUP, &act, 0);
2295 sigprocmask(SIG_UNBLOCK, &mask, 0);
2296 #endif /* !G_OS_WIN32 */
2299 #if (defined linux && defined SIGIO)
2300 static int mem_notify_fd = 0;
2302 static gboolean clean_caches(gpointer unused)
2304 if (static_mainwindow && static_mainwindow->lock_count > 0)
2306 debug_print("/dev/mem_notify: callback: Freeing some memory now!\n");
2307 folder_clean_cache_memory_force();
2311 static void memory_signal_handler(int sig)
2313 debug_print("/dev/mem_notify: Kernel says we should free up some memory!\n");
2314 g_timeout_add(10, clean_caches, NULL);
2317 static void install_memory_sighandler()
2320 struct sigaction act;
2323 mem_notify_fd = open("/dev/mem_notify", O_RDONLY|O_NONBLOCK);
2324 if (mem_notify_fd == -1) {
2325 debug_print("/dev/mem_notify not available (%s)\n",
2330 fcntl(mem_notify_fd, F_SETOWN, getpid());
2331 flags = fcntl(mem_notify_fd, F_GETFL);
2332 fcntl(mem_notify_fd, flags|FASYNC);
2336 sigaddset(&mask, SIGIO);
2338 act.sa_handler = memory_signal_handler;
2342 sigaction(SIGIO, &act, 0);
2344 sigprocmask(SIG_UNBLOCK, &mask, 0);
2346 debug_print("/dev/mem_notify: installed handler\n");
2348 #endif /* linux && SIGIO */
2351 osso_context_t *get_osso_context(void)
2353 return static_osso_context;
2358 #ifdef HAVE_NETWORKMANAGER_SUPPORT
2359 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
2362 MainWindow *mainWin;
2365 if (static_mainwindow)
2366 mainWin = static_mainwindow;
2368 mainWin = (MainWindow*)data;
2370 if (!prefs_common.use_networkmanager)
2378 online = networkmanager_is_online(&error);
2380 if(online && went_offline_nm) {
2381 went_offline_nm = FALSE;
2382 main_window_toggle_work_offline(mainWin, FALSE, FALSE);
2383 debug_print("NetworkManager: Went online\n");
2384 log_message(LOG_PROTOCOL, _("NetworkManager: network is online.\n"));
2387 went_offline_nm = TRUE;
2388 main_window_toggle_work_offline(mainWin, TRUE, FALSE);
2389 debug_print("NetworkManager: Went offline\n");
2390 log_message(LOG_PROTOCOL, _("NetworkManager: network is offline.\n"));
2394 debug_print("Failed to get online information from NetworkManager: %s\n",
2396 g_error_free(error);
2400 debug_print("NetworkManager: Cannot change connection state because "
2401 "main window does not exist\n");
2404 /* Returns true (and sets error appropriately, if given) in case of error */
2405 gboolean networkmanager_is_online(GError **error)
2407 DBusGConnection *connection;
2413 if (!prefs_common.use_networkmanager)
2418 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &tmp_error);
2421 /* If calling code doesn't do error checking, at least print some debug */
2422 if((error == NULL) || (*error == NULL))
2423 debug_print("Failed to open connection to system bus: %s\n",
2424 tmp_error->message);
2425 g_propagate_error(error, tmp_error);
2429 proxy = dbus_g_proxy_new_for_name(connection,
2430 "org.freedesktop.NetworkManager",
2431 "/org/freedesktop/NetworkManager",
2432 "org.freedesktop.NetworkManager");
2434 retVal = dbus_g_proxy_call(proxy,"state",&tmp_error, G_TYPE_INVALID,
2435 G_TYPE_UINT, &state, G_TYPE_INVALID);
2438 g_object_unref(proxy);
2440 dbus_g_connection_unref(connection);
2443 /* If calling code doesn't do error checking, at least print some debug */
2444 if((error == NULL) || (*error == NULL))
2445 debug_print("Failed to get state info from NetworkManager: %s\n",
2446 tmp_error->message);
2447 g_propagate_error(error, tmp_error);
2451 return (state == NM_STATE_CONNECTED || state == NM_STATE_UNKNOWN);