2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2013 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/>.
22 #include "claws-features.h"
28 #include <glib/gi18n.h>
38 #include <sys/types.h>
45 #include <X11/SM/SMlib.h>
53 #ifdef HAVE_STARTUP_NOTIFICATION
54 # define SN_API_NOT_YET_FROZEN
55 # include <libsn/sn-launchee.h>
56 # include <gdk/gdkx.h>
60 #include <dbus/dbus-glib.h>
62 #ifdef HAVE_NETWORKMANAGER_SUPPORT
63 #include <NetworkManager.h>
71 #include "mainwindow.h"
72 #include "folderview.h"
73 #include "image_viewer.h"
74 #include "summaryview.h"
75 #include "prefs_common.h"
76 #include "prefs_account.h"
77 #include "prefs_actions.h"
78 #include "prefs_ext_prog.h"
79 #include "prefs_fonts.h"
80 #include "prefs_image_viewer.h"
81 #include "prefs_message.h"
82 #include "prefs_receive.h"
83 #include "prefs_msg_colors.h"
84 #include "prefs_quote.h"
85 #include "prefs_spelling.h"
86 #include "prefs_summaries.h"
87 #include "prefs_themes.h"
88 #include "prefs_other.h"
89 #include "prefs_logging.h"
90 #include "prefs_send.h"
91 #include "prefs_wrapping.h"
92 #include "prefs_compose_writing.h"
93 #include "prefs_display_header.h"
98 #include "send_message.h"
101 #include "manage_window.h"
102 #include "alertpanel.h"
103 #include "statusbar.h"
104 #ifndef USE_NEW_ADDRBOOK
105 #include "addressbook.h"
107 #include "addressbook-dbus.h"
111 #include "folder_item_prefs.h"
114 #include "gtkutils.h"
117 #include "prefs_toolbar.h"
120 #include "imap_gtk.h"
121 #include "news_gtk.h"
126 #include "quicksearch.h"
127 #include "advsearch.h"
130 #include "imap-thread.h"
131 #include "nntp-thread.h"
133 #include "stock_pixmap.h"
144 #ifdef HAVE_NETWORKMANAGER_SUPPORT
145 /* Went offline due to NetworkManager */
146 static gboolean went_offline_nm;
149 #if !defined(NM_CHECK_VERSION)
150 #define NM_CHECK_VERSION(x,y,z) 0
153 #ifdef HAVE_DBUS_GLIB
154 static DBusGProxy *awn_proxy = NULL;
160 #ifdef HAVE_STARTUP_NOTIFICATION
161 static SnLauncheeContext *sn_context = NULL;
162 static SnDisplay *sn_display = NULL;
165 static gint lock_socket = -1;
166 static gint lock_socket_tag = 0;
167 static gchar *x_display = NULL;
170 ONLINE_MODE_DONT_CHANGE,
175 static struct RemoteCmd {
177 gboolean receive_all;
178 gboolean cancel_receiving;
179 gboolean cancel_sending;
181 const gchar *compose_mailto;
184 const gchar *search_folder;
185 const gchar *search_type;
186 const gchar *search_request;
187 gboolean search_recursive;
189 gboolean status_full;
191 gboolean reset_statistics;
192 GPtrArray *status_folders;
193 GPtrArray *status_full_folders;
200 const gchar *subscribe_uri;
205 SessionStats session_stats;
207 static void reset_statistics(void);
209 static void parse_cmd_opt(int argc, char *argv[]);
211 static gint prohibit_duplicate_launch (void);
212 static gchar * get_crashfile_name (void);
213 static gint lock_socket_remove (void);
214 static void lock_socket_input_cb (gpointer data,
216 GIOCondition condition);
218 static void open_compose_new (const gchar *address,
219 GList *attach_files);
221 static void send_queue (void);
222 static void initial_processing (FolderItem *item, gpointer data);
223 static void quit_signal_handler (int sig);
224 static void install_basic_sighandlers (void);
225 #if (defined linux && defined SIGIO)
226 static void install_memory_sighandler (void);
228 static void exit_claws (MainWindow *mainwin);
230 #ifdef HAVE_NETWORKMANAGER_SUPPORT
231 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
235 #define MAKE_DIR_IF_NOT_EXIST(dir) \
237 if (!is_dir_exist(dir)) { \
238 if (is_file_exist(dir)) { \
240 (_("File '%s' already exists.\n" \
241 "Can't create folder."), \
245 if (make_dir(dir) < 0) \
250 static MainWindow *static_mainwindow;
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_window_move(GTK_WINDOW(hack), 0, 0);
276 gtk_widget_set_size_request(hack, 1, 1);
277 gtk_widget_show(hack);
280 xdisplay = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
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 gboolean backup = TRUE;
375 keep_backup_chk = gtk_check_button_new_with_label (_("Keep old configuration"));
376 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_backup_chk), TRUE);
377 CLAWS_SET_TIP(keep_backup_chk,
378 _("Keeping a backup will allow you to go back to an "
379 "older version, but may take a while if you have "
380 "cached IMAP or News data, and will take some extra "
381 "room on your disk."));
383 g_signal_connect(G_OBJECT(keep_backup_chk), "toggled",
384 G_CALLBACK(chk_update_val), &backup);
386 if (alertpanel_full(_("Migration of configuration"), message,
387 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL, FALSE,
388 keep_backup_chk, ALERT_QUESTION, G_ALERTDEFAULT) != G_ALERTALTERNATE) {
392 /* we can either do a fast migration requiring not any extra disk
393 * space, or a slow one that copies the old configuration and leaves
397 window = label_window_create(_("Copying configuration... This may take a while..."));
400 r = copy_dir(old_cfg_dir, new_cfg_dir);
401 label_window_destroy(window);
403 /* if copy failed, we'll remove the partially copied
406 alertpanel_error(_("Migration failed!"));
407 remove_dir_recursive(new_cfg_dir);
410 /* fast mode failed, but we don't want backup */
411 remove_dir_recursive(old_cfg_dir);
415 window = label_window_create(_("Migrating configuration..."));
418 r = g_rename(old_cfg_dir, new_cfg_dir);
419 label_window_destroy(window);
421 /* if g_rename failed, we'll try to copy */
423 FILE_OP_ERROR(new_cfg_dir, "g_rename failed, trying copy\n");
430 static int migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
433 gchar *plugin_path, *old_plugin_path, *new_plugin_path;
435 gboolean err = FALSE;
437 oldfp = g_fopen(old_rc, "r");
440 newfp = g_fopen(new_rc, "w");
446 plugin_path = g_strdup(get_plugin_dir());
447 new_plugin_path = g_strdup(plugin_path);
449 if (strstr(plugin_path, "/claws-mail/")) {
450 gchar *end = g_strdup(strstr(plugin_path, "/claws-mail/")+strlen("/claws-mail/"));
451 *(strstr(plugin_path, "/claws-mail/")) = '\0';
452 old_plugin_path = g_strconcat(plugin_path, "/sylpheed-claws/", end, NULL);
455 old_plugin_path = g_strdup(new_plugin_path);
457 debug_print("replacing %s with %s\n", old_plugin_path, new_plugin_path);
458 while (fgets(buf, sizeof(buf), oldfp)) {
459 if (strncmp(buf, old_plugin_path, strlen(old_plugin_path))) {
460 err |= (fputs(buf, newfp) == EOF);
462 debug_print("->replacing %s", buf);
463 debug_print(" with %s%s", new_plugin_path, buf+strlen(old_plugin_path));
464 err |= (fputs(new_plugin_path, newfp) == EOF);
465 err |= (fputs(buf+strlen(old_plugin_path), newfp) == EOF);
469 g_free(new_plugin_path);
470 g_free(old_plugin_path);
472 if (fclose(newfp) == EOF)
480 sc_client_set_value (MainWindow *mainwin,
491 prop.num_vals = num_vals;
495 if (mainwin->smc_conn)
496 SmcSetProperties ((SmcConn) mainwin->smc_conn, 1, proplist);
499 static void sc_die_callback (SmcConn smc_conn, SmPointer client_data)
504 static void sc_save_complete_callback(SmcConn smc_conn, SmPointer client_data)
508 static void sc_shutdown_cancelled_callback (SmcConn smc_conn, SmPointer client_data)
510 MainWindow *mainwin = (MainWindow *)client_data;
511 if (mainwin->smc_conn)
512 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
515 static void sc_save_yourself_callback (SmcConn smc_conn,
516 SmPointer client_data,
522 MainWindow *mainwin = (MainWindow *)client_data;
523 if (mainwin->smc_conn)
524 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
527 static IceIOErrorHandler sc_ice_installed_handler;
529 static void sc_ice_io_error_handler (IceConn connection)
531 if (sc_ice_installed_handler)
532 (*sc_ice_installed_handler) (connection);
534 static gboolean sc_process_ice_messages (GIOChannel *source,
535 GIOCondition condition,
538 IceConn connection = (IceConn) data;
539 IceProcessMessagesStatus status;
541 status = IceProcessMessages (connection, NULL, NULL);
543 if (status == IceProcessMessagesIOError) {
544 IcePointer context = IceGetConnectionContext (connection);
546 if (context && G_IS_OBJECT(context)) {
547 guint disconnect_id = g_signal_lookup ("disconnect", G_OBJECT_TYPE (context));
549 if (disconnect_id > 0)
550 g_signal_emit (context, disconnect_id, 0);
552 IceSetShutdownNegotiation (connection, False);
553 IceCloseConnection (connection);
560 static void new_ice_connection (IceConn connection, IcePointer client_data, Bool opening,
561 IcePointer *watch_data)
567 /* Make sure we don't pass on these file descriptors to any
569 fcntl(IceConnectionNumber(connection),F_SETFD,
570 fcntl(IceConnectionNumber(connection),F_GETFD,0) | FD_CLOEXEC);
572 channel = g_io_channel_unix_new (IceConnectionNumber (connection));
573 input_id = g_io_add_watch (channel,
574 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
575 sc_process_ice_messages,
577 g_io_channel_unref (channel);
579 *watch_data = (IcePointer) GUINT_TO_POINTER (input_id);
581 input_id = GPOINTER_TO_UINT ((gpointer) *watch_data);
582 g_source_remove (input_id);
586 static void sc_session_manager_connect(MainWindow *mainwin)
588 static gboolean connected = FALSE;
589 SmcCallbacks callbacks;
591 IceIOErrorHandler default_handler;
598 sc_ice_installed_handler = IceSetIOErrorHandler (NULL);
599 default_handler = IceSetIOErrorHandler (sc_ice_io_error_handler);
601 if (sc_ice_installed_handler == default_handler)
602 sc_ice_installed_handler = NULL;
604 IceAddConnectionWatch (new_ice_connection, NULL);
607 callbacks.save_yourself.callback = sc_save_yourself_callback;
608 callbacks.die.callback = sc_die_callback;
609 callbacks.save_complete.callback = sc_save_complete_callback;
610 callbacks.shutdown_cancelled.callback = sc_shutdown_cancelled_callback;
612 callbacks.save_yourself.client_data =
613 callbacks.die.client_data =
614 callbacks.save_complete.client_data =
615 callbacks.shutdown_cancelled.client_data = (SmPointer) mainwin;
616 if (g_getenv ("SESSION_MANAGER")) {
617 gchar error_string_ret[256] = "";
619 mainwin->smc_conn = (gpointer)
620 SmcOpenConnection (NULL, mainwin,
621 SmProtoMajor, SmProtoMinor,
622 SmcSaveYourselfProcMask | SmcDieProcMask |
623 SmcSaveCompleteProcMask |
624 SmcShutdownCancelledProcMask,
627 256, error_string_ret);
629 if (error_string_ret[0] || mainwin->smc_conn == NULL)
630 g_warning ("While connecting to session manager:\n%s.",
634 vals = g_new (SmPropValue, 1);
635 vals[0].length = strlen(argv0);
636 vals[0].value = argv0;
637 sc_client_set_value (mainwin, SmCloneCommand, SmLISTofARRAY8, 1, vals);
638 sc_client_set_value (mainwin, SmRestartCommand, SmLISTofARRAY8, 1, vals);
639 sc_client_set_value (mainwin, SmProgram, SmARRAY8, 1, vals);
641 vals[0].length = strlen(g_get_user_name()?g_get_user_name():"");
642 vals[0].value = g_strdup(g_get_user_name()?g_get_user_name():"");
643 sc_client_set_value (mainwin, SmUserID, SmARRAY8, 1, vals);
649 static gboolean sc_exiting = FALSE;
650 static gboolean show_at_startup = TRUE;
651 static gboolean claws_crashed_bool = FALSE;
653 gboolean claws_crashed(void) {
654 return claws_crashed_bool;
657 void main_set_show_at_startup(gboolean show)
659 show_at_startup = show;
663 static FILE* win32_debug_fp=NULL;
664 static guint win32_log_handler_app_id;
665 static guint win32_log_handler_glib_id;
666 static guint win32_log_handler_gtk_id;
668 static void win32_print_stdout(const gchar *string)
670 if (win32_debug_fp) {
671 fprintf(win32_debug_fp, "%s", string);
672 fflush(win32_debug_fp);
676 static void win32_print_stderr(const gchar *string)
678 if (win32_debug_fp) {
679 fprintf(win32_debug_fp, "%s", string);
680 fflush(win32_debug_fp);
684 static void win32_log(const gchar *log_domain, GLogLevelFlags log_level, const gchar* message, gpointer user_data)
686 if (win32_debug_fp) {
689 switch(log_level & G_LOG_LEVEL_MASK)
691 case G_LOG_LEVEL_ERROR:
694 case G_LOG_LEVEL_CRITICAL:
697 case G_LOG_LEVEL_WARNING:
700 case G_LOG_LEVEL_MESSAGE:
703 case G_LOG_LEVEL_INFO:
706 case G_LOG_LEVEL_DEBUG:
713 fprintf(win32_debug_fp, "%s: %s: %s", log_domain, type, message);
715 fprintf(win32_debug_fp, "%s: %s", type, message);
716 fflush(win32_debug_fp);
720 static void win32_open_log(void)
722 gchar *logfile = g_strconcat(g_get_tmp_dir(), G_DIR_SEPARATOR_S, "claws-win32.log", NULL);
723 gchar *oldlogfile = g_strconcat(g_get_tmp_dir(), G_DIR_SEPARATOR_S, "claws-win32.log.bak", NULL);
725 if (is_file_exist(logfile)) {
726 if (rename_force(logfile, oldlogfile) < 0)
727 FILE_OP_ERROR(logfile, "rename");
729 win32_debug_fp = g_fopen(logfile, "w");
734 g_set_print_handler(win32_print_stdout);
735 g_set_printerr_handler(win32_print_stdout);
736 win32_log_handler_app_id = g_log_set_handler(NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
737 | G_LOG_FLAG_RECURSION, win32_log, NULL);
738 win32_log_handler_glib_id = g_log_set_handler("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
739 | G_LOG_FLAG_RECURSION, win32_log, NULL);
740 win32_log_handler_gtk_id = g_log_set_handler("Gtk", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
741 | G_LOG_FLAG_RECURSION, win32_log, NULL);
745 static void win32_close_log(void)
749 g_log_remove_handler("", win32_log_handler_app_id);
750 g_log_remove_handler("GLib", win32_log_handler_glib_id);
751 g_log_remove_handler("Gtk", win32_log_handler_gtk_id);
752 fclose(win32_debug_fp);
758 static void main_dump_features_list(gboolean show_debug_only)
759 /* display compiled-in features list */
761 if (show_debug_only && !debug_get_mode())
765 debug_print("runtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
766 gtk_major_version, gtk_minor_version, gtk_micro_version,
767 glib_major_version, glib_minor_version, glib_micro_version);
769 g_print("runtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
770 gtk_major_version, gtk_minor_version, gtk_micro_version,
771 glib_major_version, glib_minor_version, glib_micro_version);
773 debug_print("buildtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
774 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
775 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
777 g_print("buildtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
778 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
779 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
782 debug_print("Compiled-in features:\n");
784 g_print("Compiled-in features:\n");
787 debug_print(" compface\n");
789 g_print(" compface\n");
793 debug_print(" aspell\n");
795 g_print(" aspell\n");
799 debug_print(" gnutls\n");
801 g_print(" gnutls\n");
805 debug_print(" ipv6\n");
811 debug_print(" iconv\n");
817 debug_print(" jpilot\n");
819 g_print(" jpilot\n");
823 debug_print(" ldap\n");
829 debug_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
831 g_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
835 debug_print(" libsm\n");
839 #if HAVE_NETWORKMANAGER_SUPPORT
841 debug_print(" NetworkManager\n");
843 g_print(" NetworkManager\n");
847 #ifdef HAVE_DBUS_GLIB
848 static guint dbus_item_hook_id = -1;
849 static guint dbus_folder_hook_id = -1;
851 static void uninstall_dbus_status_handler(void)
854 g_object_unref(awn_proxy);
856 if (dbus_item_hook_id != -1)
857 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, dbus_item_hook_id);
858 if (dbus_folder_hook_id != -1)
859 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, dbus_folder_hook_id);
862 static void dbus_update(FolderItem *removed_item)
864 guint new, unread, unreadmarked, marked, total;
865 guint replied, forwarded, locked, ignored, watched;
867 GError *error = NULL;
869 folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total,
870 &replied, &forwarded, &locked, &ignored,
873 total -= removed_item->total_msgs;
874 new -= removed_item->new_msgs;
875 unread -= removed_item->unread_msgs;
879 buf = g_strdup_printf("%d", new);
880 dbus_g_proxy_call(awn_proxy, "SetInfoByName", &error,
881 G_TYPE_STRING, "claws-mail",
883 G_TYPE_INVALID, G_TYPE_INVALID);
887 dbus_g_proxy_call(awn_proxy, "UnsetInfoByName", &error, G_TYPE_STRING,
888 "claws-mail", G_TYPE_INVALID, G_TYPE_INVALID);
891 debug_print("%s\n", error->message);
896 static gboolean dbus_status_update_folder_hook(gpointer source, gpointer data)
898 FolderUpdateData *hookdata;
900 if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
901 dbus_update(hookdata->item);
908 static gboolean dbus_status_update_item_hook(gpointer source, gpointer data)
915 static void install_dbus_status_handler(void)
917 GError *tmp_error = NULL;
918 DBusGConnection *connection = dbus_g_bus_get(DBUS_BUS_SESSION, &tmp_error);
921 /* If calling code doesn't do error checking, at least print some debug */
922 debug_print("Failed to open connection to session bus: %s\n",
924 g_error_free(tmp_error);
927 awn_proxy = dbus_g_proxy_new_for_name(connection,
928 "com.google.code.Awn",
929 "/com/google/code/Awn",
930 "com.google.code.Awn");
931 dbus_item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, dbus_status_update_item_hook, NULL);
932 if (dbus_item_hook_id == -1) {
933 g_warning(_("Failed to register folder item update hook"));
934 uninstall_dbus_status_handler();
938 dbus_folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, dbus_status_update_folder_hook, NULL);
939 if (dbus_folder_hook_id == -1) {
940 g_warning(_("Failed to register folder update hook"));
941 uninstall_dbus_status_handler();
947 static void reset_statistics(void)
949 /* (re-)initialize session statistics */
950 session_stats.received = 0;
951 session_stats.sent = 0;
952 session_stats.replied = 0;
953 session_stats.forwarded = 0;
954 session_stats.time_started = time(NULL);
957 int main(int argc, char *argv[])
959 #ifdef HAVE_DBUS_GLIB
960 DBusGConnection *connection;
963 #ifdef HAVE_NETWORKMANAGER_SUPPORT
964 DBusGProxy *nm_proxy;
968 FolderView *folderview;
970 gboolean crash_file_present = FALSE;
971 gint num_folder_class = 0;
972 gboolean asked_for_migration = FALSE;
973 gboolean start_done = TRUE;
974 GSList *plug_list = NULL;
975 gboolean never_ran = FALSE;
976 gboolean mainwin_shown = FALSE;
978 START_TIMING("startup");
985 if (!claws_init(&argc, &argv)) {
992 prog_version = PROG_VERSION;
993 argv0 = g_strdup(argv[0]);
995 parse_cmd_opt(argc, argv);
999 /* check and create unix domain socket for remote operation */
1000 lock_socket = prohibit_duplicate_launch();
1001 if (lock_socket < 0) {
1002 #ifdef HAVE_STARTUP_NOTIFICATION
1003 if(gtk_init_check(&argc, &argv))
1004 startup_notification_complete(TRUE);
1009 main_dump_features_list(TRUE);
1010 prefs_prepare_cache();
1014 #if !GTK_CHECK_VERSION(3, 0, 0)
1017 gtk_init(&argc, &argv);
1018 crash_main(cmd.crash_params);
1024 crash_install_handlers();
1026 install_basic_sighandlers();
1027 #if (defined linux && defined SIGIO)
1028 install_memory_sighandler();
1031 if (cmd.status || cmd.status_full || cmd.search ||
1032 cmd.statistics || cmd.reset_statistics ||
1033 cmd.cancel_receiving || cmd.cancel_sending ||
1035 puts("0 Claws Mail not running.");
1036 lock_socket_remove();
1042 if (!g_thread_supported())
1043 g_thread_init(NULL);
1047 #if !GTK_CHECK_VERSION(3, 0, 0)
1050 gtk_init(&argc, &argv);
1053 gtk_settings_set_string_property(gtk_settings_get_default(),
1059 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1060 went_offline_nm = FALSE;
1063 #ifdef HAVE_DBUS_GLIB
1065 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
1068 debug_print("Failed to open connection to system bus: %s\n", error->message);
1069 g_error_free(error);
1072 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1073 nm_proxy = dbus_g_proxy_new_for_name(connection,
1074 "org.freedesktop.NetworkManager",
1075 "/org/freedesktop/NetworkManager",
1076 "org.freedesktop.NetworkManager");
1078 #if NM_CHECK_VERSION(0,8,992)
1079 dbus_g_proxy_add_signal(nm_proxy, "StateChanged", G_TYPE_UINT, G_TYPE_INVALID);
1080 dbus_g_proxy_connect_signal(nm_proxy, "StateChanged",
1081 G_CALLBACK(networkmanager_state_change_cb),
1084 dbus_g_proxy_add_signal(nm_proxy, "StateChange", G_TYPE_UINT, G_TYPE_INVALID);
1085 dbus_g_proxy_connect_signal(nm_proxy, "StateChange",
1086 G_CALLBACK(networkmanager_state_change_cb),
1091 install_dbus_status_handler();
1095 #if !GTK_CHECK_VERSION(3, 0, 0)
1096 gtk_widget_set_default_colormap(
1097 gdk_screen_get_system_colormap(
1098 gdk_screen_get_default()));
1101 gtkut_create_ui_manager();
1103 /* Create container for all the menus we will be adding */
1104 MENUITEM_ADDUI("/", "Menus", NULL, GTK_UI_MANAGER_MENUBAR);
1106 if (!g_thread_supported()) {
1107 g_error(_("g_thread is not supported by glib.\n"));
1111 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1113 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1116 /* no config dir exists. See if we can migrate an old config. */
1117 if (!is_dir_exist(RC_DIR)) {
1118 prefs_destroy_cache();
1121 /* if one of the old dirs exist, we'll ask if the user
1122 * want to migrates, and r will be TRUE if he said yes
1123 * and migration succeeded, and FALSE otherwise.
1125 if (is_dir_exist(OLD_GTK2_RC_DIR)) {
1126 r = migrate_old_config(OLD_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 2.6.0 (or older)"));
1127 asked_for_migration = TRUE;
1128 } else if (is_dir_exist(OLDER_GTK2_RC_DIR)) {
1129 r = migrate_old_config(OLDER_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.9.15 (or older)"));
1130 asked_for_migration = TRUE;
1131 } else if (is_dir_exist(OLD_GTK1_RC_DIR)) {
1132 r = migrate_old_config(OLD_GTK1_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.0.5 (or older)"));
1133 asked_for_migration = TRUE;
1134 } else if (is_dir_exist(SYLPHEED_RC_DIR)) {
1135 r = migrate_old_config(SYLPHEED_RC_DIR, RC_DIR, "Sylpheed");
1136 asked_for_migration = TRUE;
1139 /* If migration failed or the user didn't want to do it,
1140 * we create a new one (and we'll hit wizard later).
1142 if (r == FALSE && !is_dir_exist(RC_DIR)) {
1144 if (copy_dir(SYSCONFDIR "/skel/.claws-mail", RC_DIR) < 0) {
1146 if (!is_dir_exist(RC_DIR) && make_dir(RC_DIR) < 0) {
1159 if (!is_file_exist(RC_DIR G_DIR_SEPARATOR_S COMMON_RC) &&
1160 is_file_exist(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC)) {
1161 /* post 2.6 name change */
1162 migrate_common_rc(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC,
1163 RC_DIR G_DIR_SEPARATOR_S COMMON_RC);
1167 plugin_load_all("Common");
1169 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
1170 gtk_rc_parse(userrc);
1173 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1174 gtk_accel_map_load (userrc);
1178 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_rc_dir(), 1, win32_close_log(););
1180 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
1183 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
1184 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
1185 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
1186 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
1187 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
1188 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR);
1190 crash_file_present = is_file_exist(get_crashfile_name());
1191 /* remove temporary files */
1192 remove_all_files(get_tmp_dir());
1193 remove_all_files(get_mime_tmp_dir());
1195 if (!cmd.crash && crash_file_present)
1196 claws_crashed_bool = TRUE;
1198 if (is_file_exist("claws.log")) {
1199 if (rename_force("claws.log", "claws.log.bak") < 0)
1200 FILE_OP_ERROR("claws.log", "rename");
1202 set_log_file(LOG_PROTOCOL, "claws.log");
1204 if (is_file_exist("filtering.log")) {
1205 if (rename_force("filtering.log", "filtering.log.bak") < 0)
1206 FILE_OP_ERROR("filtering.log", "rename");
1208 set_log_file(LOG_DEBUG_FILTERING, "filtering.log");
1211 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1213 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1216 folder_system_init();
1217 prefs_common_read_config();
1219 prefs_themes_init();
1221 prefs_ext_prog_init();
1222 prefs_wrapping_init();
1223 prefs_compose_writing_init();
1224 prefs_msg_colors_init();
1225 image_viewer_init();
1226 prefs_image_viewer_init();
1228 prefs_summaries_init();
1229 prefs_message_init();
1231 prefs_logging_init();
1232 prefs_receive_init();
1237 gtkaspell_checkers_init();
1238 prefs_spelling_init();
1242 if(prefs_common.gtk_theme && strcmp(prefs_common.gtk_theme, DEFAULT_W32_GTK_THEME))
1243 gtk_settings_set_string_property(gtk_settings_get_default(),
1245 prefs_common.gtk_theme,
1250 sock_set_io_timeout(prefs_common.io_timeout_secs);
1251 prefs_actions_read_config();
1252 prefs_display_header_read_config();
1253 /* prefs_filtering_read_config(); */
1254 #ifndef USE_NEW_ADDRBOOK
1255 addressbook_read_file();
1257 g_clear_error(&error);
1258 if (! addressbook_start_service(&error)) {
1259 g_warning("%s", error->message);
1260 g_clear_error(&error);
1263 addressbook_install_hooks(&error);
1266 gtkut_widget_init();
1267 stock_pixbuf_gdk(NULL, STOCK_PIXMAP_CLAWS_MAIL_ICON, &icon);
1268 gtk_window_set_default_icon(icon);
1270 folderview_initialize();
1276 mainwin = main_window_create();
1278 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1279 networkmanager_state_change_cb(nm_proxy,NULL,mainwin);
1282 manage_window_focus_in(mainwin->window, NULL, NULL);
1283 folderview = mainwin->folderview;
1285 gtk_cmclist_freeze(GTK_CMCLIST(mainwin->folderview->ctree));
1286 folder_item_update_freeze();
1288 prefs_account_init();
1289 account_read_config_all();
1291 #ifdef HAVE_LIBETPAN
1292 imap_main_init(prefs_common.skip_ssl_cert_check);
1293 imap_main_set_timeout(prefs_common.io_timeout_secs);
1294 nntp_main_init(prefs_common.skip_ssl_cert_check);
1296 /* If we can't read a folder list or don't have accounts,
1297 * it means the configuration's not done. Either this is
1298 * a brand new install, either a failed/refused migration.
1299 * So we'll start the wizard.
1301 if (folder_read_list() < 0) {
1302 prefs_destroy_cache();
1304 /* if run_wizard returns FALSE it's because it's
1305 * been cancelled. We can't do much but exit.
1306 * however, if the user was asked for a migration,
1307 * we remove the newly created directory so that
1308 * he's asked again for migration on next launch.*/
1309 if (!run_wizard(mainwin, TRUE)) {
1310 if (asked_for_migration)
1311 remove_dir_recursive(RC_DIR);
1317 main_window_reflect_prefs_all_now();
1318 folder_write_list();
1322 if (!account_get_list()) {
1323 prefs_destroy_cache();
1324 if (!run_wizard(mainwin, FALSE)) {
1325 if (asked_for_migration)
1326 remove_dir_recursive(RC_DIR);
1332 if(!account_get_list()) {
1333 exit_claws(mainwin);
1343 toolbar_main_set_sensitive(mainwin);
1344 main_window_set_menu_sensitive(mainwin);
1346 /* if crashed, show window early so that the user
1347 * sees what's happening */
1348 if (claws_crashed()) {
1349 main_window_popup(mainwin);
1350 mainwin_shown = TRUE;
1353 account_set_missing_folder();
1354 folder_set_missing_folders();
1355 folderview_set(folderview);
1357 prefs_matcher_read_config();
1358 quicksearch_set_search_strings(mainwin->summaryview->quicksearch);
1360 /* make one all-folder processing before using claws */
1361 main_window_cursor_wait(mainwin);
1362 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
1364 /* if claws crashed, rebuild caches */
1365 if (claws_crashed()) {
1367 debug_print("Claws Mail crashed, checking for new messages in local folders\n");
1368 folder_item_update_thaw();
1369 folderview_check_new(NULL);
1370 folder_clean_cache_memory_force();
1371 folder_item_update_freeze();
1373 /* make the crash-indicator file */
1374 str_write_to_file("foo", get_crashfile_name());
1376 inc_autocheck_timer_init(mainwin);
1378 /* ignore SIGPIPE signal for preventing sudden death of program */
1380 signal(SIGPIPE, SIG_IGN);
1382 if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
1383 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1385 if (cmd.online_mode == ONLINE_MODE_ONLINE) {
1386 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1389 if (cmd.status_folders) {
1390 g_ptr_array_free(cmd.status_folders, TRUE);
1391 cmd.status_folders = NULL;
1393 if (cmd.status_full_folders) {
1394 g_ptr_array_free(cmd.status_full_folders, TRUE);
1395 cmd.status_full_folders = NULL;
1398 claws_register_idle_function(claws_gtk_idle);
1400 prefs_toolbar_init();
1402 num_folder_class = g_list_length(folder_get_list());
1404 plugin_load_all("GTK2");
1406 if (g_list_length(folder_get_list()) != num_folder_class) {
1407 debug_print("new folders loaded, reloading processing rules\n");
1408 prefs_matcher_read_config();
1411 if ((plug_list = plugin_get_unloaded_list()) != NULL) {
1414 gint num_plugins = 0;
1415 for (cur = plug_list; cur; cur = cur->next) {
1416 Plugin *plugin = (Plugin *)cur->data;
1417 gchar *tmp = g_strdup_printf("%s\n%s",
1419 plugin_get_name(plugin));
1424 main_window_cursor_normal(mainwin);
1425 main_window_popup(mainwin);
1426 mainwin_shown = TRUE;
1427 alertpanel_warning(ngettext(
1428 "The following plugin failed to load. "
1429 "Check the Plugins configuration "
1430 "for more information:\n%s",
1431 "The following plugins failed to load. "
1432 "Check the Plugins configuration "
1433 "for more information:\n%s",
1436 main_window_cursor_wait(mainwin);
1438 g_slist_free(plug_list);
1442 prefs_common_write_config();
1443 plugin_load_standard_plugins ();
1445 /* if not crashed, show window now */
1446 if (!mainwin_shown) {
1447 /* apart if something told not to show */
1448 if (show_at_startup)
1449 main_window_popup(mainwin);
1452 if (!folder_have_mailbox()) {
1453 prefs_destroy_cache();
1454 main_window_cursor_normal(mainwin);
1455 if (folder_get_list() != NULL) {
1456 alertpanel_error(_("Claws Mail has detected a configured "
1457 "mailbox, but it is incomplete. It is "
1458 "possibly due to a failing IMAP account. Use "
1459 "\"Rebuild folder tree\" on the mailbox parent "
1460 "folder's context menu to try to fix it."));
1462 alertpanel_error(_("Claws Mail has detected a configured "
1463 "mailbox, but could not load it. It is "
1464 "probably provided by an out-of-date "
1465 "external plugin. Please reinstall the "
1466 "plugin and try again."));
1467 exit_claws(mainwin);
1475 static_mainwindow = mainwin;
1477 #ifdef HAVE_STARTUP_NOTIFICATION
1478 startup_notification_complete(FALSE);
1481 sc_session_manager_connect(mainwin);
1484 folder_item_update_thaw();
1485 gtk_cmclist_thaw(GTK_CMCLIST(mainwin->folderview->ctree));
1486 main_window_cursor_normal(mainwin);
1488 if (!cmd.target && prefs_common.goto_last_folder_on_startup &&
1489 folder_find_item_from_identifier(prefs_common.last_opened_folder) != NULL &&
1491 cmd.target = prefs_common.last_opened_folder;
1494 if (cmd.receive_all && !cmd.target) {
1496 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(FALSE));
1497 } else if (prefs_common.chk_on_startup && !cmd.target) {
1499 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(TRUE));
1500 } else if (cmd.receive && !cmd.target) {
1502 g_timeout_add(1000, defer_check, NULL);
1504 gtk_widget_grab_focus(folderview->ctree);
1507 open_compose_new(cmd.compose_mailto, cmd.attach_files);
1509 if (cmd.attach_files) {
1510 list_free_strings(cmd.attach_files);
1511 g_list_free(cmd.attach_files);
1512 cmd.attach_files = NULL;
1514 if (cmd.subscribe) {
1515 folder_subscribe(cmd.subscribe_uri);
1524 g_timeout_add(500, defer_jump, (gpointer)cmd.target);
1527 prefs_destroy_cache();
1529 compose_reopen_exit_drafts();
1532 sc_starting = FALSE;
1533 main_window_set_menu_sensitive(mainwin);
1534 toolbar_main_set_sensitive(mainwin);
1537 /* register the callback of unix domain socket input */
1538 lock_socket_tag = claws_input_add(lock_socket,
1539 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
1540 lock_socket_input_cb,
1547 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1549 g_object_unref(nm_proxy);
1551 #ifdef HAVE_DBUS_GLIB
1552 uninstall_dbus_status_handler();
1554 dbus_g_connection_unref(connection);
1560 exit_claws(mainwin);
1565 static void save_all_caches(FolderItem *item, gpointer data)
1572 folder_item_close(item);
1575 folder_item_free_cache(item, TRUE);
1578 static void exit_claws(MainWindow *mainwin)
1581 gboolean have_connectivity;
1585 debug_print("shutting down\n");
1586 inc_autocheck_timer_remove();
1588 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1589 if (prefs_common.work_offline && went_offline_nm)
1590 prefs_common.work_offline = FALSE;
1593 /* save prefs for opened folder */
1594 if(mainwin->folderview->opened) {
1597 item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
1598 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
1599 prefs_common.last_opened_folder = folder_item_get_identifier(item);
1602 /* save all state before exiting */
1603 folder_func_to_all_folders(save_all_caches, NULL);
1604 folder_write_list();
1606 main_window_get_size(mainwin);
1607 main_window_get_position(mainwin);
1609 prefs_common_write_config();
1610 account_write_config_all();
1611 #ifndef USE_NEW_ADDRBOOK
1612 addressbook_export_to_file();
1614 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1615 gtk_accel_map_save(filename);
1618 /* delete temporary files */
1619 remove_all_files(get_tmp_dir());
1620 remove_all_files(get_mime_tmp_dir());
1622 close_log_file(LOG_PROTOCOL);
1623 close_log_file(LOG_DEBUG_FILTERING);
1625 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1626 have_connectivity = networkmanager_is_online(NULL);
1628 have_connectivity = TRUE;
1630 #ifdef HAVE_LIBETPAN
1631 imap_main_done(have_connectivity);
1632 nntp_main_done(have_connectivity);
1634 /* delete crashfile */
1636 claws_unlink(get_crashfile_name());
1638 lock_socket_remove();
1641 if (mainwin->smc_conn)
1642 SmcCloseConnection ((SmcConn)mainwin->smc_conn, 0, NULL);
1643 mainwin->smc_conn = NULL;
1646 main_window_destroy_all();
1648 plugin_unload_all("GTK2");
1651 prefs_toolbar_done();
1653 #ifndef USE_NEW_ADDRBOOK
1654 addressbook_destroy();
1656 prefs_themes_done();
1658 prefs_ext_prog_done();
1659 prefs_wrapping_done();
1660 prefs_compose_writing_done();
1661 prefs_msg_colors_done();
1662 prefs_image_viewer_done();
1663 image_viewer_done();
1665 prefs_summaries_done();
1666 prefs_message_done();
1668 prefs_receive_done();
1669 prefs_logging_done();
1673 prefs_spelling_done();
1674 gtkaspell_checkers_quit();
1676 plugin_unload_all("Common");
1680 #define G_STRING_APPEND_ENCODED_URI(gstring,source) \
1682 gchar tmpbuf[BUFFSIZE]; \
1683 encode_uri(tmpbuf, BUFFSIZE, (source)); \
1684 g_string_append((gstring), tmpbuf); \
1687 #define G_PRINT_EXIT(msg) \
1693 static GString * parse_cmd_compose_from_file(const gchar *fn)
1695 GString *headers = g_string_new(NULL);
1696 GString *body = g_string_new(NULL);
1704 if (fn == NULL || *fn == '\0')
1705 G_PRINT_EXIT(_("Missing filename\n"));
1706 isstdin = (*fn == '-' && *(fn + 1) == '\0');
1710 fp = g_fopen(fn, "r");
1712 G_PRINT_EXIT(_("Cannot open filename for reading\n"));
1715 while (fgets(fb, sizeof(fb), fp)) {
1721 while (*h && *h != ':') { ++h; } /* search colon */
1723 G_PRINT_EXIT(_("Malformed header\n"));
1725 while (*v && *v == ' ') { ++v; } /* trim value start */
1727 tmp = g_ascii_strdown(fb, -1); /* get header name */
1728 if (!strcmp(tmp, "to")) {
1730 G_PRINT_EXIT(_("Duplicated 'To:' header\n"));
1733 g_string_append_c(headers, '&');
1734 g_string_append(headers, tmp);
1735 g_string_append_c(headers, '=');
1736 #if GLIB_CHECK_VERSION(2,16,0)
1737 g_string_append_uri_escaped(headers, v, NULL, TRUE);
1739 G_STRING_APPEND_ENCODED_URI(headers, v);
1745 G_PRINT_EXIT(_("Missing required 'To:' header\n"));
1746 g_string_append(body, to);
1748 g_string_append(body, "?body=");
1749 while (fgets(fb, sizeof(fb), fp)) {
1750 #if GLIB_CHECK_VERSION(2,16,0)
1751 g_string_append_uri_escaped(body, fb, NULL, TRUE);
1753 G_STRING_APPEND_ENCODED_URI(body, fb);
1758 /* append the remaining headers */
1759 g_string_append(body, headers->str);
1760 g_string_free(headers, TRUE);
1765 #undef G_STRING_APPEND_ENCODED_URI
1768 static void parse_cmd_opt(int argc, char *argv[])
1773 for (i = 1; i < argc; i++) {
1774 if (!strncmp(argv[i], "--receive-all", 13)) {
1775 cmd.receive_all = TRUE;
1776 } else if (!strncmp(argv[i], "--receive", 9)) {
1778 } else if (!strncmp(argv[i], "--cancel-receiving", 18)) {
1779 cmd.cancel_receiving = TRUE;
1780 } else if (!strncmp(argv[i], "--cancel-sending", 16)) {
1781 cmd.cancel_sending = TRUE;
1782 } else if (!strncmp(argv[i], "--compose-from-file", 19)) {
1783 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1785 GString *mailto = parse_cmd_compose_from_file(p);
1787 cmd.compose_mailto = mailto->str;
1789 } else if (!strncmp(argv[i], "--compose", 9)) {
1790 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1793 cmd.compose_mailto = NULL;
1794 if (p && *p != '\0' && *p != '-') {
1795 if (!strncmp(p, "mailto:", 7)) {
1796 cmd.compose_mailto = p + 7;
1798 cmd.compose_mailto = p;
1802 } else if (!strncmp(argv[i], "--subscribe", 11)) {
1803 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1804 if (p && *p != '\0' && *p != '-') {
1805 cmd.subscribe = TRUE;
1806 cmd.subscribe_uri = p;
1808 } else if (!strncmp(argv[i], "--attach", 8)) {
1809 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1812 while (p && *p != '\0' && *p != '-') {
1813 if ((file = g_filename_from_uri(p, NULL, NULL)) != NULL) {
1814 if (!is_file_exist(file)) {
1819 if (file == NULL && *p != G_DIR_SEPARATOR) {
1820 file = g_strconcat(claws_get_startup_dir(),
1823 } else if (file == NULL) {
1826 ainfo = g_new0(AttachInfo, 1);
1828 cmd.attach_files = g_list_append(cmd.attach_files, ainfo);
1830 p = (i+1 < argc)?argv[i+1]:NULL;
1832 } else if (!strncmp(argv[i], "--send", 6)) {
1834 } else if (!strncmp(argv[i], "--version-full", 14) ||
1835 !strncmp(argv[i], "-V", 2)) {
1836 g_print("Claws Mail version " VERSION "\n");
1837 main_dump_features_list(FALSE);
1839 } else if (!strncmp(argv[i], "--version", 9) ||
1840 !strncmp(argv[i], "-v", 2)) {
1841 g_print("Claws Mail version " VERSION "\n");
1843 } else if (!strncmp(argv[i], "--status-full", 13)) {
1844 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1846 cmd.status_full = TRUE;
1847 while (p && *p != '\0' && *p != '-') {
1848 if (!cmd.status_full_folders) {
1849 cmd.status_full_folders =
1852 g_ptr_array_add(cmd.status_full_folders,
1855 p = (i+1 < argc)?argv[i+1]:NULL;
1857 } else if (!strncmp(argv[i], "--status", 8)) {
1858 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1861 while (p && *p != '\0' && *p != '-') {
1862 if (!cmd.status_folders)
1863 cmd.status_folders = g_ptr_array_new();
1864 g_ptr_array_add(cmd.status_folders,
1867 p = (i+1 < argc)?argv[i+1]:NULL;
1869 } else if (!strncmp(argv[i], "--search", 8)) {
1870 cmd.search_folder = (i+1 < argc)?argv[i+1]:NULL;
1871 cmd.search_type = (i+2 < argc)?argv[i+2]:NULL;
1872 cmd.search_request = (i+3 < argc)?argv[i+3]:NULL;
1873 const char* rec = (i+4 < argc)?argv[i+4]:NULL;
1874 cmd.search_recursive = TRUE;
1875 if (rec && (tolower(*rec)=='n' || tolower(*rec)=='f' || *rec=='0'))
1876 cmd.search_recursive = FALSE;
1877 if (cmd.search_folder && cmd.search_type && cmd.search_request)
1879 } else if (!strncmp(argv[i], "--online", 8)) {
1880 cmd.online_mode = ONLINE_MODE_ONLINE;
1881 } else if (!strncmp(argv[i], "--offline", 9)) {
1882 cmd.online_mode = ONLINE_MODE_OFFLINE;
1883 } else if (!strncmp(argv[i], "--toggle-debug", 14)) {
1885 } else if (!strncmp(argv[i], "--statistics", 12)) {
1886 cmd.statistics = TRUE;
1887 } else if (!strncmp(argv[i], "--reset-statistics", 18)) {
1888 cmd.reset_statistics = TRUE;
1889 } else if (!strncmp(argv[i], "--help", 6) ||
1890 !strncmp(argv[i], "-h", 2)) {
1891 gchar *base = g_path_get_basename(argv[0]);
1892 g_print(_("Usage: %s [OPTION]...\n"), base);
1894 g_print("%s\n", _(" --compose [address] open composition window"));
1895 g_print("%s\n", _(" --compose-from-file file\n"
1896 " open composition window with data from given file;\n"
1897 " use - as file name for reading from standard input;\n"
1898 " content format: headers first (To: required) until an\n"
1899 " empty line, then mail body until end of file."));
1900 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
1901 g_print("%s\n", _(" --attach file1 [file2]...\n"
1902 " open composition window with specified files\n"
1904 g_print("%s\n", _(" --receive receive new messages"));
1905 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
1906 g_print("%s\n", _(" --cancel-receiving cancel receiving of messages"));
1907 g_print("%s\n", _(" --cancel-sending cancel sending of messages"));
1908 g_print("%s\n", _(" --search folder type request [recursive]\n"
1910 " folder ex.: \"#mh/Mailbox/inbox\" or \"Mail\"\n"
1911 " type: s[ubject],f[rom],t[o],e[xtended],m[ixed] or g: tag\n"
1912 " request: search string\n"
1913 " recursive: false if arg. starts with 0, n, N, f or F"));
1915 g_print("%s\n", _(" --send send all queued messages"));
1916 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
1917 g_print("%s\n", _(" --status-full [folder]...\n"
1918 " show the status of each folder"));
1919 g_print("%s\n", _(" --statistics show session statistics"));
1920 g_print("%s\n", _(" --reset-statistics reset session statistics"));
1921 g_print("%s\n", _(" --select folder[/msg] jumps to the specified folder/message\n"
1922 " folder is a folder id like 'folder/sub_folder'"));
1923 g_print("%s\n", _(" --online switch to online mode"));
1924 g_print("%s\n", _(" --offline switch to offline mode"));
1925 g_print("%s\n", _(" --exit --quit -q exit Claws Mail"));
1926 g_print("%s\n", _(" --debug debug mode"));
1927 g_print("%s\n", _(" --toggle-debug toggle debug mode"));
1928 g_print("%s\n", _(" --help -h display this help and exit"));
1929 g_print("%s\n", _(" --version -v output version information and exit"));
1930 g_print("%s\n", _(" --version-full -V output version and built-in features information and exit"));
1931 g_print("%s\n", _(" --config-dir output configuration directory"));
1932 g_print("%s\n", _(" --alternate-config-dir [dir]\n"
1933 " use specified configuration directory"));
1937 } else if (!strncmp(argv[i], "--crash", 7)) {
1939 cmd.crash_params = g_strdup((i+1 < argc)?argv[i+1]:NULL);
1941 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
1942 g_print(RC_DIR "\n");
1944 } else if (!strncmp(argv[i], "--alternate-config-dir", sizeof "--alternate-config-dir" - 1) && i+1 < argc) {
1945 set_rc_dir(argv[i+1]);
1946 } else if (!strncmp(argv[i], "--exit", 6) ||
1947 !strncmp(argv[i], "--quit", 6) ||
1948 !strncmp(argv[i], "-q", 2)) {
1950 } else if (!strncmp(argv[i], "--select", 8) && i+1 < argc) {
1951 cmd.target = argv[i+1];
1952 } else if (i == 1 && argc == 2) {
1953 /* only one parameter. Do something intelligent about it */
1954 if ((strstr(argv[i], "@")||!strncmp(argv[i], "mailto:", 7)) && !strstr(argv[i], "://")) {
1955 const gchar *p = argv[i];
1958 cmd.compose_mailto = NULL;
1959 if (p && *p != '\0' && *p != '-') {
1960 if (!strncmp(p, "mailto:", 7)) {
1961 cmd.compose_mailto = p + 7;
1963 cmd.compose_mailto = p;
1966 } else if (!strncmp(argv[i], "file://", 7)) {
1967 cmd.target = argv[i];
1968 } else if (!strncmp(argv[i], "?attach=file://", strlen("?attach=file://"))) {
1970 cmd.compose_mailto = argv[i];
1971 } else if (strstr(argv[i], "://")) {
1972 const gchar *p = argv[i];
1973 if (p && *p != '\0' && *p != '-') {
1974 cmd.subscribe = TRUE;
1975 cmd.subscribe_uri = p;
1977 } else if (!strcmp(argv[i], "--sync")) {
1979 } else if (is_dir_exist(argv[i]) || is_file_exist(argv[i])) {
1980 cmd.target = argv[i];
1982 g_print(_("Unknown option\n"));
1988 if (cmd.attach_files && cmd.compose == FALSE) {
1990 cmd.compose_mailto = NULL;
1994 static void initial_processing(FolderItem *item, gpointer data)
1996 MainWindow *mainwin = (MainWindow *)data;
1999 cm_return_if_fail(item);
2000 buf = g_strdup_printf(_("Processing (%s)..."),
2003 : _("top level folder"));
2007 if (item->prefs->enable_processing) {
2008 item->processing_pending = TRUE;
2009 folder_item_apply_processing(item);
2010 item->processing_pending = FALSE;
2013 STATUSBAR_POP(mainwin);
2016 static gboolean draft_all_messages(void)
2018 GList *compose_list = NULL;
2020 compose_clear_exit_drafts();
2021 compose_list = compose_get_compose_list();
2022 while (compose_list != NULL) {
2023 Compose *c = (Compose*)compose_list->data;
2024 if (!compose_draft(c, COMPOSE_DRAFT_FOR_EXIT))
2026 compose_list = compose_get_compose_list();
2030 gboolean clean_quit(gpointer data)
2032 static gboolean firstrun = TRUE;
2039 /*!< Good idea to have the main window stored in a
2040 * static variable so we can check that variable
2041 * to see if we're really allowed to do things
2042 * that actually the spawner is supposed to
2043 * do (like: sending mail, composing messages).
2044 * Because, really, if we're the spawnee, and
2045 * we touch GTK stuff, we're hosed. See the
2048 /* FIXME: Use something else to signal that we're
2049 * in the original spawner, and not in a spawned
2051 if (!static_mainwindow) {
2055 draft_all_messages();
2056 emergency_exit = TRUE;
2057 exit_claws(static_mainwindow);
2063 void app_will_exit(GtkWidget *widget, gpointer data)
2065 MainWindow *mainwin = data;
2067 if (gtk_main_level() == 0) {
2068 debug_print("not even started\n");
2071 if (sc_exiting == TRUE) {
2072 debug_print("exit pending\n");
2076 debug_print("exiting\n");
2077 if (compose_get_compose_list()) {
2078 if (!draft_all_messages()) {
2079 main_window_popup(mainwin);
2085 if (prefs_common.warn_queued_on_exit && procmsg_have_queued_mails_fast()) {
2086 if (alertpanel(_("Queued messages"),
2087 _("Some unsent messages are queued. Exit now?"),
2088 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL)
2089 != G_ALERTALTERNATE) {
2090 main_window_popup(mainwin);
2094 manage_window_focus_in(mainwin->window, NULL, NULL);
2098 #ifdef HAVE_VALGRIND
2099 if (RUNNING_ON_VALGRIND) {
2100 summary_clear_list(mainwin->summaryview);
2103 if (folderview_get_selected_item(mainwin->folderview))
2104 folder_item_close(folderview_get_selected_item(mainwin->folderview));
2108 gboolean claws_is_exiting(void)
2113 gboolean claws_is_starting(void)
2120 * CLAWS: want this public so crash dialog can delete the
2123 gchar *claws_get_socket_name(void)
2125 static gchar *filename = NULL;
2126 const gchar *socket_dir = NULL;
2129 if (filename == NULL) {
2131 socket_dir = g_strdup_printf("%s%cclaws-mail-%d",
2132 g_get_tmp_dir(), G_DIR_SEPARATOR,
2138 if (stat(socket_dir, &st) < 0 && errno != ENOENT) {
2139 g_print("Error stat'ing socket_dir %s: %s\n",
2140 socket_dir, strerror(errno));
2141 } else if (S_ISSOCK(st.st_mode)) {
2142 /* old versions used a sock in $TMPDIR/claws-mail-$UID */
2143 debug_print("Using legacy socket %s\n", socket_dir);
2144 filename = g_strdup(socket_dir);
2148 if (!is_dir_exist(socket_dir) && make_dir(socket_dir) < 0) {
2149 g_print("Error creating socket_dir %s: %s\n",
2150 socket_dir, strerror(errno));
2153 md5_hex_digest(md5sum, get_rc_dir());
2155 filename = g_strdup_printf("%s%c%s", socket_dir, G_DIR_SEPARATOR,
2157 debug_print("Using control socket %s\n", filename);
2164 static gchar *get_crashfile_name(void)
2166 static gchar *filename = NULL;
2168 if (filename == NULL) {
2169 filename = g_strdup_printf("%s%cclaws-crashed",
2170 get_tmp_dir(), G_DIR_SEPARATOR);
2176 static gint prohibit_duplicate_launch(void)
2183 path = claws_get_socket_name();
2184 /* Try to connect to the control socket */
2185 uxsock = fd_connect_unix(path);
2187 if (x_display == NULL)
2188 x_display = g_strdup(g_getenv("DISPLAY"));
2195 /* If connect failed, no other process is running.
2196 * Unlink the potentially existing socket, then
2197 * open it. This has to be done locking a temporary
2198 * file to avoid the race condition where another
2199 * process could have created the socket just in
2202 socket_lock = g_strconcat(path, ".lock",
2204 lock_fd = g_open(socket_lock, O_RDWR|O_CREAT, 0);
2206 debug_print("Couldn't open %s: %s (%d)\n", socket_lock,
2207 strerror(errno), errno);
2208 g_free(socket_lock);
2211 if (flock(lock_fd, LOCK_EX) < 0) {
2212 debug_print("Couldn't lock %s: %s (%d)\n", socket_lock,
2213 strerror(errno), errno);
2215 g_free(socket_lock);
2221 debug_print("Opening socket %s\n", path);
2222 ret = fd_open_unix(path);
2224 flock(lock_fd, LOCK_UN);
2225 claws_unlink(socket_lock);
2226 g_free(socket_lock);
2233 hmutex = CreateMutexA(NULL, FALSE, "ClawsMail");
2235 debug_print("cannot create Mutex\n");
2238 if (GetLastError() != ERROR_ALREADY_EXISTS) {
2239 uxsock = fd_open_inet(50216);
2245 uxsock = fd_connect_inet(50216);
2249 /* remote command mode */
2251 debug_print("another Claws Mail instance is already running.\n");
2253 if (cmd.receive_all) {
2254 fd_write_all(uxsock, "receive_all\n", 12);
2255 } else if (cmd.receive) {
2256 fd_write_all(uxsock, "receive\n", 8);
2257 } else if (cmd.cancel_receiving) {
2258 fd_write_all(uxsock, "cancel_receiving\n", 17);
2259 } else if (cmd.cancel_sending) {
2260 fd_write_all(uxsock, "cancel_sending\n", 15);
2261 } else if (cmd.compose && cmd.attach_files) {
2262 gchar *str, *compose_str;
2264 if (cmd.compose_mailto) {
2265 compose_str = g_strdup_printf("compose_attach %s\n",
2266 cmd.compose_mailto);
2268 compose_str = g_strdup("compose_attach\n");
2271 fd_write_all(uxsock, compose_str, strlen(compose_str));
2272 g_free(compose_str);
2274 for (curr = cmd.attach_files; curr != NULL ; curr = curr->next) {
2275 str = (gchar *) ((AttachInfo *)curr->data)->file;
2276 fd_write_all(uxsock, str, strlen(str));
2277 fd_write_all(uxsock, "\n", 1);
2280 fd_write_all(uxsock, ".\n", 2);
2281 } else if (cmd.compose) {
2284 if (cmd.compose_mailto) {
2285 compose_str = g_strdup_printf
2286 ("compose %s\n", cmd.compose_mailto);
2288 compose_str = g_strdup("compose\n");
2291 fd_write_all(uxsock, compose_str, strlen(compose_str));
2292 g_free(compose_str);
2293 } else if (cmd.subscribe) {
2294 gchar *str = g_strdup_printf("subscribe %s\n", cmd.subscribe_uri);
2295 fd_write_all(uxsock, str, strlen(str));
2297 } else if (cmd.send) {
2298 fd_write_all(uxsock, "send\n", 5);
2299 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
2300 fd_write(uxsock, "online\n", 6);
2301 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
2302 fd_write(uxsock, "offline\n", 7);
2303 } else if (cmd.debug) {
2304 fd_write(uxsock, "debug\n", 7);
2305 } else if (cmd.status || cmd.status_full) {
2306 gchar buf[BUFFSIZE];
2308 const gchar *command;
2312 command = cmd.status_full ? "status-full\n" : "status\n";
2313 folders = cmd.status_full ? cmd.status_full_folders :
2316 fd_write_all(uxsock, command, strlen(command));
2317 for (i = 0; folders && i < folders->len; ++i) {
2318 folder = g_ptr_array_index(folders, i);
2319 fd_write_all(uxsock, folder, strlen(folder));
2320 fd_write_all(uxsock, "\n", 1);
2322 fd_write_all(uxsock, ".\n", 2);
2324 fd_gets(uxsock, buf, sizeof(buf));
2325 if (!strncmp(buf, ".\n", 2)) break;
2328 } else if (cmd.exit) {
2329 fd_write_all(uxsock, "exit\n", 5);
2330 } else if (cmd.statistics) {
2332 fd_write(uxsock, "statistics\n", 11);
2334 fd_gets(uxsock, buf, sizeof(buf));
2335 if (!strncmp(buf, ".\n", 2)) break;
2338 } else if (cmd.reset_statistics) {
2339 fd_write(uxsock, "reset_statistics\n", 17);
2340 } else if (cmd.target) {
2341 gchar *str = g_strdup_printf("select %s\n", cmd.target);
2342 fd_write_all(uxsock, str, strlen(str));
2344 } else if (cmd.search) {
2345 gchar buf[BUFFSIZE];
2347 g_strdup_printf("search %s\n%s\n%s\n%c\n",
2348 cmd.search_folder, cmd.search_type, cmd.search_request,
2349 (cmd.search_recursive==TRUE)?'1':'0');
2350 fd_write_all(uxsock, str, strlen(str));
2353 fd_gets(uxsock, buf, sizeof(buf));
2354 if (!strncmp(buf, ".\n", 2)) break;
2360 fd_write_all(uxsock, "get_display\n", 12);
2361 memset(buf, 0, sizeof(buf));
2362 fd_gets(uxsock, buf, sizeof(buf));
2363 if (strcmp2(buf, x_display)) {
2364 g_print("Claws Mail is already running on display %s.\n",
2368 uxsock = fd_connect_unix(path);
2369 fd_write_all(uxsock, "popup\n", 6);
2372 fd_write_all(uxsock, "popup\n", 6);
2380 static gint lock_socket_remove(void)
2383 gchar *filename, *dirname;
2385 if (lock_socket < 0) {
2389 if (lock_socket_tag > 0) {
2390 g_source_remove(lock_socket_tag);
2392 fd_close(lock_socket);
2395 filename = claws_get_socket_name();
2396 dirname = g_path_get_dirname(filename);
2397 claws_unlink(filename);
2405 static GPtrArray *get_folder_item_list(gint sock)
2407 gchar buf[BUFFSIZE];
2409 GPtrArray *folders = NULL;
2412 fd_gets(sock, buf, sizeof(buf));
2413 if (!strncmp(buf, ".\n", 2)) {
2418 folders = g_ptr_array_new();
2420 item = folder_find_item_from_identifier(buf);
2422 g_ptr_array_add(folders, item);
2424 g_warning("no such folder: %s\n", buf);
2431 static void lock_socket_input_cb(gpointer data,
2433 GIOCondition condition)
2435 MainWindow *mainwin = (MainWindow *)data;
2437 gchar buf[BUFFSIZE];
2439 sock = fd_accept(source);
2440 fd_gets(sock, buf, sizeof(buf));
2442 if (!strncmp(buf, "popup", 5)) {
2443 main_window_popup(mainwin);
2444 } else if (!strncmp(buf, "get_display", 11)) {
2445 fd_write_all(sock, x_display, strlen(x_display));
2446 } else if (!strncmp(buf, "receive_all", 11)) {
2447 inc_all_account_mail(mainwin, FALSE,
2448 prefs_common.newmail_notify_manu);
2449 } else if (!strncmp(buf, "receive", 7)) {
2450 inc_mail(mainwin, prefs_common.newmail_notify_manu);
2451 } else if (!strncmp(buf, "cancel_receiving", 16)) {
2454 } else if (!strncmp(buf, "cancel_sending", 14)) {
2456 } else if (!strncmp(buf, "compose_attach", 14)) {
2457 GList *files = NULL, *curr;
2461 mailto = g_strdup(buf + strlen("compose_attach") + 1);
2462 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
2464 if (!strcmp2(buf, "."))
2467 ainfo = g_new0(AttachInfo, 1);
2468 ainfo->file = g_strdup(buf);
2469 files = g_list_append(files, ainfo);
2471 open_compose_new(mailto, files);
2473 curr = g_list_first(files);
2474 while (curr != NULL) {
2475 ainfo = (AttachInfo *)curr->data;
2476 g_free(ainfo->file);
2482 } else if (!strncmp(buf, "compose", 7)) {
2483 open_compose_new(buf + strlen("compose") + 1, NULL);
2484 } else if (!strncmp(buf, "subscribe", 9)) {
2485 main_window_popup(mainwin);
2486 folder_subscribe(buf + strlen("subscribe") + 1);
2487 } else if (!strncmp(buf, "send", 4)) {
2489 } else if (!strncmp(buf, "online", 6)) {
2490 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
2491 } else if (!strncmp(buf, "offline", 7)) {
2492 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
2493 } else if (!strncmp(buf, "debug", 5)) {
2494 debug_set_mode(debug_get_mode() ? FALSE : TRUE);
2495 } else if (!strncmp(buf, "status-full", 11) ||
2496 !strncmp(buf, "status", 6)) {
2500 folders = get_folder_item_list(sock);
2501 status = folder_get_status
2502 (folders, !strncmp(buf, "status-full", 11));
2503 fd_write_all(sock, status, strlen(status));
2504 fd_write_all(sock, ".\n", 2);
2506 if (folders) g_ptr_array_free(folders, TRUE);
2507 } else if (!strncmp(buf, "statistics", 10)) {
2510 g_snprintf(tmp, sizeof(tmp), _("Session statistics\n"));
2511 fd_write_all(sock, tmp, strlen(tmp));
2513 if (prefs_common.date_format) {
2518 lt = localtime(&session_stats.time_started);
2519 fast_strftime(date, len, prefs_common.date_format, lt);
2520 g_snprintf(tmp, sizeof(tmp), _("Started: %s\n"),
2521 lt ? date : ctime(&session_stats.time_started));
2523 g_snprintf(tmp, sizeof(tmp), _("Started: %s\n"),
2524 ctime(&session_stats.time_started));
2525 fd_write_all(sock, tmp, strlen(tmp));
2527 fd_write_all(sock, "\n", 1);
2529 g_snprintf(tmp, sizeof(tmp), _("Incoming traffic\n"));
2530 fd_write_all(sock, tmp, strlen(tmp));
2532 g_snprintf(tmp, sizeof(tmp), _("Received messages: %d\n"),
2533 session_stats.received);
2534 fd_write_all(sock, tmp, strlen(tmp));
2536 fd_write_all(sock, "\n", 1);
2538 g_snprintf(tmp, sizeof(tmp), _("Outgoing traffic\n"));
2539 fd_write_all(sock, tmp, strlen(tmp));
2541 g_snprintf(tmp, sizeof(tmp), _("New/redirected messages: %d\n"),
2542 session_stats.sent);
2543 fd_write_all(sock, tmp, strlen(tmp));
2545 g_snprintf(tmp, sizeof(tmp), _("Replied messages: %d\n"),
2546 session_stats.replied);
2547 fd_write_all(sock, tmp, strlen(tmp));
2549 g_snprintf(tmp, sizeof(tmp), _("Forwarded messages: %d\n"),
2550 session_stats.forwarded);
2551 fd_write_all(sock, tmp, strlen(tmp));
2553 g_snprintf(tmp, sizeof(tmp), _("Total outgoing messages: %d\n"),
2554 (session_stats.sent + session_stats.replied +
2555 session_stats.forwarded));
2556 fd_write_all(sock, tmp, strlen(tmp));
2558 fd_write_all(sock, ".\n", 2);
2559 } else if (!strncmp(buf, "reset_statistics", 16)) {
2561 } else if (!strncmp(buf, "select ", 7)) {
2562 const gchar *target = buf+7;
2563 mainwindow_jump_to(target, TRUE);
2564 } else if (!strncmp(buf, "search ", 7)) {
2565 FolderItem* folderItem = NULL;
2566 GSList *messages = NULL;
2567 gchar *folder_name = NULL;
2568 gchar *request = NULL;
2569 AdvancedSearch *search;
2571 AdvancedSearchType searchType = ADVANCED_SEARCH_EXTENDED;
2573 search = advsearch_new();
2575 folder_name = g_strdup(buf+7);
2576 strretchomp(folder_name);
2578 if (fd_gets(sock, buf, sizeof(buf)) <= 0)
2581 switch (toupper(buf[0])) {
2582 case 'S': searchType = ADVANCED_SEARCH_SUBJECT; break;
2583 case 'F': searchType = ADVANCED_SEARCH_FROM; break;
2584 case 'T': searchType = ADVANCED_SEARCH_TO; break;
2585 case 'M': searchType = ADVANCED_SEARCH_MIXED; break;
2586 case 'G': searchType = ADVANCED_SEARCH_TAG; break;
2587 case 'E': searchType = ADVANCED_SEARCH_EXTENDED; break;
2590 if (fd_gets(sock, buf, sizeof(buf)) <= 0)
2593 request = g_strdup(buf);
2594 strretchomp(request);
2597 if (fd_gets(sock, buf, sizeof(buf)) > 0)
2598 recursive = buf[0] != '0';
2600 debug_print("search: %s %i %s %i\n", folder_name, searchType, request, recursive);
2603 folderItem = folder_find_item_from_identifier(folder_name);
2605 if (folder_name && folderItem == NULL) {
2606 debug_print("Unknown folder item : '%s', searching folder\n",folder_name);
2607 Folder* folder = folder_find_from_path(folder_name);
2609 folderItem = FOLDER_ITEM(folder->node->data);
2611 debug_print("Unknown folder: '%s'\n",folder_name);
2613 debug_print("%s %s\n",folderItem->name, folderItem->path);
2616 if (folderItem != NULL) {
2617 advsearch_set(search, searchType, request);
2618 advsearch_search_msgs_in_folders(search, &messages, folderItem, recursive);
2620 g_print("Folder '%s' not found.\n'", folder_name);
2624 for (cur = messages; cur != NULL; cur = cur->next) {
2625 MsgInfo* msg = (MsgInfo *)cur->data;
2626 gchar *file = procmsg_get_message_file_path(msg);
2627 fd_write_all(sock, file, strlen(file));
2628 fd_write_all(sock, "\n", 1);
2631 fd_write_all(sock, ".\n", 2);
2634 g_free(folder_name);
2636 advsearch_free(search);
2637 if (messages != NULL)
2638 procmsg_msg_list_free(messages);
2639 } else if (!strncmp(buf, "exit", 4)) {
2640 if (prefs_common.clean_on_exit && !prefs_common.ask_on_clean) {
2641 procmsg_empty_all_trash();
2643 app_will_exit(NULL, mainwin);
2649 static void open_compose_new(const gchar *address, GList *attach_files)
2654 Xstrdup_a(addr, address, return);
2658 compose_new(NULL, addr, attach_files);
2661 static void send_queue(void)
2664 gchar *errstr = NULL;
2665 gboolean error = FALSE;
2666 for (list = folder_get_list(); list != NULL; list = list->next) {
2667 Folder *folder = list->data;
2669 if (folder->queue) {
2670 gint res = procmsg_send_queue
2671 (folder->queue, prefs_common.savemsg,
2675 folder_item_scan(folder->queue);
2683 alertpanel_error_log(_("Some errors occurred "
2684 "while sending queued messages:\n%s"), errstr);
2687 alertpanel_error_log("Some errors occurred "
2688 "while sending queued messages.");
2692 static void quit_signal_handler(int sig)
2694 debug_print("Quitting on signal %d\n", sig);
2696 g_timeout_add(0, clean_quit, NULL);
2699 static void install_basic_sighandlers()
2703 struct sigaction act;
2708 sigaddset(&mask, SIGTERM);
2711 sigaddset(&mask, SIGINT);
2714 sigaddset(&mask, SIGHUP);
2717 act.sa_handler = quit_signal_handler;
2722 sigaction(SIGTERM, &act, 0);
2725 sigaction(SIGINT, &act, 0);
2728 sigaction(SIGHUP, &act, 0);
2731 sigprocmask(SIG_UNBLOCK, &mask, 0);
2732 #endif /* !G_OS_WIN32 */
2735 #if (defined linux && defined SIGIO)
2736 static int mem_notify_fd = 0;
2738 static gboolean clean_caches(gpointer unused)
2740 if (static_mainwindow && static_mainwindow->lock_count > 0)
2742 debug_print("/dev/mem_notify: callback: Freeing some memory now!\n");
2743 folder_clean_cache_memory_force();
2747 static void memory_signal_handler(int sig)
2749 debug_print("/dev/mem_notify: Kernel says we should free up some memory!\n");
2750 g_timeout_add(10, clean_caches, NULL);
2753 static void install_memory_sighandler()
2756 struct sigaction act;
2759 mem_notify_fd = g_open("/dev/mem_notify", O_RDONLY|O_NONBLOCK, 0);
2760 if (mem_notify_fd == -1) {
2761 debug_print("/dev/mem_notify not available (%s)\n",
2766 fcntl(mem_notify_fd, F_SETOWN, getpid());
2767 flags = fcntl(mem_notify_fd, F_GETFL);
2768 fcntl(mem_notify_fd, flags|FASYNC);
2772 sigaddset(&mask, SIGIO);
2774 act.sa_handler = memory_signal_handler;
2778 sigaction(SIGIO, &act, 0);
2780 sigprocmask(SIG_UNBLOCK, &mask, 0);
2782 debug_print("/dev/mem_notify: installed handler\n");
2784 #endif /* linux && SIGIO */
2786 #ifdef HAVE_NETWORKMANAGER_SUPPORT
2787 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
2790 MainWindow *mainWin;
2793 if (static_mainwindow)
2794 mainWin = static_mainwindow;
2796 mainWin = (MainWindow*)data;
2798 if (!prefs_common.use_networkmanager)
2806 online = networkmanager_is_online(&error);
2808 if(online && went_offline_nm) {
2809 went_offline_nm = FALSE;
2810 main_window_toggle_work_offline(mainWin, FALSE, FALSE);
2811 debug_print("NetworkManager: Went online\n");
2812 log_message(LOG_PROTOCOL, _("NetworkManager: network is online.\n"));
2815 went_offline_nm = TRUE;
2816 main_window_toggle_work_offline(mainWin, TRUE, FALSE);
2817 debug_print("NetworkManager: Went offline\n");
2818 log_message(LOG_PROTOCOL, _("NetworkManager: network is offline.\n"));
2822 debug_print("Failed to get online information from NetworkManager: %s\n",
2824 g_error_free(error);
2828 debug_print("NetworkManager: Cannot change connection state because "
2829 "main window does not exist\n");
2832 /* Returns true (and sets error appropriately, if given) in case of error */
2833 gboolean networkmanager_is_online(GError **error)
2835 DBusGConnection *connection;
2837 GError *tmp_error = NULL;
2841 if (!prefs_common.use_networkmanager)
2846 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &tmp_error);
2849 /* If calling code doesn't do error checking, at least print some debug */
2850 if((error == NULL) || (*error == NULL))
2851 debug_print("Failed to open connection to system bus: %s\n",
2852 tmp_error->message);
2853 g_propagate_error(error, tmp_error);
2857 proxy = dbus_g_proxy_new_for_name(connection,
2858 "org.freedesktop.NetworkManager",
2859 "/org/freedesktop/NetworkManager",
2860 "org.freedesktop.NetworkManager");
2862 retVal = dbus_g_proxy_call(proxy,"state",&tmp_error, G_TYPE_INVALID,
2863 G_TYPE_UINT, &state, G_TYPE_INVALID);
2866 g_object_unref(proxy);
2868 dbus_g_connection_unref(connection);
2871 /* If calling code doesn't do error checking, at least print some debug */
2872 if((error == NULL) || (*error == NULL))
2873 debug_print("Failed to get state info from NetworkManager: %s\n",
2874 tmp_error->message);
2875 g_propagate_error(error, tmp_error);
2878 #if NM_CHECK_VERSION(0,8,992)
2879 return (state == NM_STATE_CONNECTED_LOCAL ||
2880 state == NM_STATE_CONNECTED_SITE ||
2881 state == NM_STATE_CONNECTED_GLOBAL ||
2882 state == NM_STATE_UNKNOWN);
2884 return (state == NM_STATE_CONNECTED ||
2885 state == NM_STATE_UNKNOWN);