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(" Enchant\n");
795 g_print(" Enchant\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 !GLIB_CHECK_VERSION(2,32,0)
1043 if (!g_thread_supported())
1044 g_thread_init(NULL);
1049 #if !GTK_CHECK_VERSION(3, 0, 0)
1052 gtk_init(&argc, &argv);
1055 gtk_settings_set_string_property(gtk_settings_get_default(),
1061 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1062 went_offline_nm = FALSE;
1065 #ifdef HAVE_DBUS_GLIB
1067 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
1070 debug_print("Failed to open connection to system bus: %s\n", error->message);
1071 g_error_free(error);
1074 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1075 nm_proxy = dbus_g_proxy_new_for_name(connection,
1076 "org.freedesktop.NetworkManager",
1077 "/org/freedesktop/NetworkManager",
1078 "org.freedesktop.NetworkManager");
1080 #if NM_CHECK_VERSION(0,8,992)
1081 dbus_g_proxy_add_signal(nm_proxy, "StateChanged", G_TYPE_UINT, G_TYPE_INVALID);
1082 dbus_g_proxy_connect_signal(nm_proxy, "StateChanged",
1083 G_CALLBACK(networkmanager_state_change_cb),
1086 dbus_g_proxy_add_signal(nm_proxy, "StateChange", G_TYPE_UINT, G_TYPE_INVALID);
1087 dbus_g_proxy_connect_signal(nm_proxy, "StateChange",
1088 G_CALLBACK(networkmanager_state_change_cb),
1093 install_dbus_status_handler();
1097 #if !GTK_CHECK_VERSION(3, 0, 0)
1098 gtk_widget_set_default_colormap(
1099 gdk_screen_get_system_colormap(
1100 gdk_screen_get_default()));
1103 gtkut_create_ui_manager();
1105 /* Create container for all the menus we will be adding */
1106 MENUITEM_ADDUI("/", "Menus", NULL, GTK_UI_MANAGER_MENUBAR);
1108 if (!g_thread_supported()) {
1109 g_error(_("g_thread is not supported by glib.\n"));
1113 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1115 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1118 /* no config dir exists. See if we can migrate an old config. */
1119 if (!is_dir_exist(RC_DIR)) {
1120 prefs_destroy_cache();
1123 /* if one of the old dirs exist, we'll ask if the user
1124 * want to migrates, and r will be TRUE if he said yes
1125 * and migration succeeded, and FALSE otherwise.
1127 if (is_dir_exist(OLD_GTK2_RC_DIR)) {
1128 r = migrate_old_config(OLD_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 2.6.0 (or older)"));
1129 asked_for_migration = TRUE;
1130 } else if (is_dir_exist(OLDER_GTK2_RC_DIR)) {
1131 r = migrate_old_config(OLDER_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.9.15 (or older)"));
1132 asked_for_migration = TRUE;
1133 } else if (is_dir_exist(OLD_GTK1_RC_DIR)) {
1134 r = migrate_old_config(OLD_GTK1_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.0.5 (or older)"));
1135 asked_for_migration = TRUE;
1136 } else if (is_dir_exist(SYLPHEED_RC_DIR)) {
1137 r = migrate_old_config(SYLPHEED_RC_DIR, RC_DIR, "Sylpheed");
1138 asked_for_migration = TRUE;
1141 /* If migration failed or the user didn't want to do it,
1142 * we create a new one (and we'll hit wizard later).
1144 if (r == FALSE && !is_dir_exist(RC_DIR)) {
1146 if (copy_dir(SYSCONFDIR "/skel/.claws-mail", RC_DIR) < 0) {
1148 if (!is_dir_exist(RC_DIR) && make_dir(RC_DIR) < 0) {
1161 if (!is_file_exist(RC_DIR G_DIR_SEPARATOR_S COMMON_RC) &&
1162 is_file_exist(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC)) {
1163 /* post 2.6 name change */
1164 migrate_common_rc(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC,
1165 RC_DIR G_DIR_SEPARATOR_S COMMON_RC);
1169 plugin_load_all("Common");
1171 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
1172 gtk_rc_parse(userrc);
1175 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1176 gtk_accel_map_load (userrc);
1180 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_rc_dir(), 1, win32_close_log(););
1182 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
1185 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
1186 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
1187 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
1188 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
1189 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
1190 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR);
1192 crash_file_present = is_file_exist(get_crashfile_name());
1193 /* remove temporary files */
1194 remove_all_files(get_tmp_dir());
1195 remove_all_files(get_mime_tmp_dir());
1197 if (!cmd.crash && crash_file_present)
1198 claws_crashed_bool = TRUE;
1200 if (is_file_exist("claws.log")) {
1201 if (rename_force("claws.log", "claws.log.bak") < 0)
1202 FILE_OP_ERROR("claws.log", "rename");
1204 set_log_file(LOG_PROTOCOL, "claws.log");
1206 if (is_file_exist("filtering.log")) {
1207 if (rename_force("filtering.log", "filtering.log.bak") < 0)
1208 FILE_OP_ERROR("filtering.log", "rename");
1210 set_log_file(LOG_DEBUG_FILTERING, "filtering.log");
1213 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1215 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1218 folder_system_init();
1219 prefs_common_read_config();
1221 prefs_themes_init();
1223 prefs_ext_prog_init();
1224 prefs_wrapping_init();
1225 prefs_compose_writing_init();
1226 prefs_msg_colors_init();
1227 image_viewer_init();
1228 prefs_image_viewer_init();
1230 prefs_summaries_init();
1231 prefs_message_init();
1233 prefs_logging_init();
1234 prefs_receive_init();
1239 gtkaspell_checkers_init();
1240 prefs_spelling_init();
1244 if(prefs_common.gtk_theme && strcmp(prefs_common.gtk_theme, DEFAULT_W32_GTK_THEME))
1245 gtk_settings_set_string_property(gtk_settings_get_default(),
1247 prefs_common.gtk_theme,
1252 sock_set_io_timeout(prefs_common.io_timeout_secs);
1253 prefs_actions_read_config();
1254 prefs_display_header_read_config();
1255 /* prefs_filtering_read_config(); */
1256 #ifndef USE_NEW_ADDRBOOK
1257 addressbook_read_file();
1259 g_clear_error(&error);
1260 if (! addressbook_start_service(&error)) {
1261 g_warning("%s", error->message);
1262 g_clear_error(&error);
1265 addressbook_install_hooks(&error);
1268 gtkut_widget_init();
1269 stock_pixbuf_gdk(NULL, STOCK_PIXMAP_CLAWS_MAIL_ICON, &icon);
1270 gtk_window_set_default_icon(icon);
1272 folderview_initialize();
1278 mainwin = main_window_create();
1280 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1281 networkmanager_state_change_cb(nm_proxy,NULL,mainwin);
1284 manage_window_focus_in(mainwin->window, NULL, NULL);
1285 folderview = mainwin->folderview;
1287 gtk_cmclist_freeze(GTK_CMCLIST(mainwin->folderview->ctree));
1288 folder_item_update_freeze();
1290 prefs_account_init();
1291 account_read_config_all();
1293 #ifdef HAVE_LIBETPAN
1294 imap_main_init(prefs_common.skip_ssl_cert_check);
1295 imap_main_set_timeout(prefs_common.io_timeout_secs);
1296 nntp_main_init(prefs_common.skip_ssl_cert_check);
1298 /* If we can't read a folder list or don't have accounts,
1299 * it means the configuration's not done. Either this is
1300 * a brand new install, either a failed/refused migration.
1301 * So we'll start the wizard.
1303 if (folder_read_list() < 0) {
1304 prefs_destroy_cache();
1306 /* if run_wizard returns FALSE it's because it's
1307 * been cancelled. We can't do much but exit.
1308 * however, if the user was asked for a migration,
1309 * we remove the newly created directory so that
1310 * he's asked again for migration on next launch.*/
1311 if (!run_wizard(mainwin, TRUE)) {
1312 if (asked_for_migration)
1313 remove_dir_recursive(RC_DIR);
1319 main_window_reflect_prefs_all_now();
1320 folder_write_list();
1324 if (!account_get_list()) {
1325 prefs_destroy_cache();
1326 if (!run_wizard(mainwin, FALSE)) {
1327 if (asked_for_migration)
1328 remove_dir_recursive(RC_DIR);
1334 if(!account_get_list()) {
1335 exit_claws(mainwin);
1345 toolbar_main_set_sensitive(mainwin);
1346 main_window_set_menu_sensitive(mainwin);
1348 /* if crashed, show window early so that the user
1349 * sees what's happening */
1350 if (claws_crashed()) {
1351 main_window_popup(mainwin);
1352 mainwin_shown = TRUE;
1355 account_set_missing_folder();
1356 folder_set_missing_folders();
1357 folderview_set(folderview);
1359 prefs_matcher_read_config();
1360 quicksearch_set_search_strings(mainwin->summaryview->quicksearch);
1362 /* make one all-folder processing before using claws */
1363 main_window_cursor_wait(mainwin);
1364 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
1366 /* if claws crashed, rebuild caches */
1367 if (claws_crashed()) {
1369 debug_print("Claws Mail crashed, checking for new messages in local folders\n");
1370 folder_item_update_thaw();
1371 folderview_check_new(NULL);
1372 folder_clean_cache_memory_force();
1373 folder_item_update_freeze();
1375 /* make the crash-indicator file */
1376 str_write_to_file("foo", get_crashfile_name());
1378 inc_autocheck_timer_init(mainwin);
1380 /* ignore SIGPIPE signal for preventing sudden death of program */
1382 signal(SIGPIPE, SIG_IGN);
1384 if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
1385 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1387 if (cmd.online_mode == ONLINE_MODE_ONLINE) {
1388 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1391 if (cmd.status_folders) {
1392 g_ptr_array_free(cmd.status_folders, TRUE);
1393 cmd.status_folders = NULL;
1395 if (cmd.status_full_folders) {
1396 g_ptr_array_free(cmd.status_full_folders, TRUE);
1397 cmd.status_full_folders = NULL;
1400 claws_register_idle_function(claws_gtk_idle);
1402 prefs_toolbar_init();
1404 num_folder_class = g_list_length(folder_get_list());
1406 plugin_load_all("GTK2");
1408 if (g_list_length(folder_get_list()) != num_folder_class) {
1409 debug_print("new folders loaded, reloading processing rules\n");
1410 prefs_matcher_read_config();
1413 if ((plug_list = plugin_get_unloaded_list()) != NULL) {
1416 gint num_plugins = 0;
1417 for (cur = plug_list; cur; cur = cur->next) {
1418 Plugin *plugin = (Plugin *)cur->data;
1419 gchar *tmp = g_strdup_printf("%s\n%s",
1421 plugin_get_name(plugin));
1426 main_window_cursor_normal(mainwin);
1427 main_window_popup(mainwin);
1428 mainwin_shown = TRUE;
1429 alertpanel_warning(ngettext(
1430 "The following plugin failed to load. "
1431 "Check the Plugins configuration "
1432 "for more information:\n%s",
1433 "The following plugins failed to load. "
1434 "Check the Plugins configuration "
1435 "for more information:\n%s",
1438 main_window_cursor_wait(mainwin);
1440 g_slist_free(plug_list);
1444 prefs_common_write_config();
1445 plugin_load_standard_plugins ();
1447 /* if not crashed, show window now */
1448 if (!mainwin_shown) {
1449 /* apart if something told not to show */
1450 if (show_at_startup)
1451 main_window_popup(mainwin);
1454 if (!folder_have_mailbox()) {
1455 prefs_destroy_cache();
1456 main_window_cursor_normal(mainwin);
1457 if (folder_get_list() != NULL) {
1458 alertpanel_error(_("Claws Mail has detected a configured "
1459 "mailbox, but it is incomplete. It is "
1460 "possibly due to a failing IMAP account. Use "
1461 "\"Rebuild folder tree\" on the mailbox parent "
1462 "folder's context menu to try to fix it."));
1464 alertpanel_error(_("Claws Mail has detected a configured "
1465 "mailbox, but could not load it. It is "
1466 "probably provided by an out-of-date "
1467 "external plugin. Please reinstall the "
1468 "plugin and try again."));
1469 exit_claws(mainwin);
1477 static_mainwindow = mainwin;
1479 #ifdef HAVE_STARTUP_NOTIFICATION
1480 startup_notification_complete(FALSE);
1483 sc_session_manager_connect(mainwin);
1486 folder_item_update_thaw();
1487 gtk_cmclist_thaw(GTK_CMCLIST(mainwin->folderview->ctree));
1488 main_window_cursor_normal(mainwin);
1490 if (!cmd.target && prefs_common.goto_last_folder_on_startup &&
1491 folder_find_item_from_identifier(prefs_common.last_opened_folder) != NULL &&
1493 cmd.target = prefs_common.last_opened_folder;
1496 if (cmd.receive_all && !cmd.target) {
1498 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(FALSE));
1499 } else if (prefs_common.chk_on_startup && !cmd.target) {
1501 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(TRUE));
1502 } else if (cmd.receive && !cmd.target) {
1504 g_timeout_add(1000, defer_check, NULL);
1506 gtk_widget_grab_focus(folderview->ctree);
1509 open_compose_new(cmd.compose_mailto, cmd.attach_files);
1511 if (cmd.attach_files) {
1512 list_free_strings(cmd.attach_files);
1513 g_list_free(cmd.attach_files);
1514 cmd.attach_files = NULL;
1516 if (cmd.subscribe) {
1517 folder_subscribe(cmd.subscribe_uri);
1526 g_timeout_add(500, defer_jump, (gpointer)cmd.target);
1529 prefs_destroy_cache();
1531 compose_reopen_exit_drafts();
1534 sc_starting = FALSE;
1535 main_window_set_menu_sensitive(mainwin);
1536 toolbar_main_set_sensitive(mainwin);
1539 /* register the callback of unix domain socket input */
1540 lock_socket_tag = claws_input_add(lock_socket,
1541 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
1542 lock_socket_input_cb,
1549 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1551 g_object_unref(nm_proxy);
1553 #ifdef HAVE_DBUS_GLIB
1554 uninstall_dbus_status_handler();
1556 dbus_g_connection_unref(connection);
1562 exit_claws(mainwin);
1567 static void save_all_caches(FolderItem *item, gpointer data)
1574 folder_item_close(item);
1577 folder_item_free_cache(item, TRUE);
1580 static void exit_claws(MainWindow *mainwin)
1583 gboolean have_connectivity;
1587 debug_print("shutting down\n");
1588 inc_autocheck_timer_remove();
1590 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1591 if (prefs_common.work_offline && went_offline_nm)
1592 prefs_common.work_offline = FALSE;
1595 /* save prefs for opened folder */
1596 if(mainwin->folderview->opened) {
1599 item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
1600 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
1601 prefs_common.last_opened_folder = folder_item_get_identifier(item);
1604 /* save all state before exiting */
1605 folder_func_to_all_folders(save_all_caches, NULL);
1606 folder_write_list();
1608 main_window_get_size(mainwin);
1609 main_window_get_position(mainwin);
1611 prefs_common_write_config();
1612 account_write_config_all();
1613 #ifndef USE_NEW_ADDRBOOK
1614 addressbook_export_to_file();
1616 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1617 gtk_accel_map_save(filename);
1620 /* delete temporary files */
1621 remove_all_files(get_tmp_dir());
1622 remove_all_files(get_mime_tmp_dir());
1624 close_log_file(LOG_PROTOCOL);
1625 close_log_file(LOG_DEBUG_FILTERING);
1627 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1628 have_connectivity = networkmanager_is_online(NULL);
1630 have_connectivity = TRUE;
1632 #ifdef HAVE_LIBETPAN
1633 imap_main_done(have_connectivity);
1634 nntp_main_done(have_connectivity);
1636 /* delete crashfile */
1638 claws_unlink(get_crashfile_name());
1640 lock_socket_remove();
1643 if (mainwin->smc_conn)
1644 SmcCloseConnection ((SmcConn)mainwin->smc_conn, 0, NULL);
1645 mainwin->smc_conn = NULL;
1648 main_window_destroy_all();
1650 plugin_unload_all("GTK2");
1653 prefs_toolbar_done();
1655 #ifndef USE_NEW_ADDRBOOK
1656 addressbook_destroy();
1658 prefs_themes_done();
1660 prefs_ext_prog_done();
1661 prefs_wrapping_done();
1662 prefs_compose_writing_done();
1663 prefs_msg_colors_done();
1664 prefs_image_viewer_done();
1665 image_viewer_done();
1667 prefs_summaries_done();
1668 prefs_message_done();
1670 prefs_receive_done();
1671 prefs_logging_done();
1675 prefs_spelling_done();
1676 gtkaspell_checkers_quit();
1678 plugin_unload_all("Common");
1682 #define G_STRING_APPEND_ENCODED_URI(gstring,source) \
1684 gchar tmpbuf[BUFFSIZE]; \
1685 encode_uri(tmpbuf, BUFFSIZE, (source)); \
1686 g_string_append((gstring), tmpbuf); \
1689 #define G_PRINT_EXIT(msg) \
1695 static GString * parse_cmd_compose_from_file(const gchar *fn)
1697 GString *headers = g_string_new(NULL);
1698 GString *body = g_string_new(NULL);
1706 if (fn == NULL || *fn == '\0')
1707 G_PRINT_EXIT(_("Missing filename\n"));
1708 isstdin = (*fn == '-' && *(fn + 1) == '\0');
1712 fp = g_fopen(fn, "r");
1714 G_PRINT_EXIT(_("Cannot open filename for reading\n"));
1717 while (fgets(fb, sizeof(fb), fp)) {
1723 while (*h && *h != ':') { ++h; } /* search colon */
1725 G_PRINT_EXIT(_("Malformed header\n"));
1727 while (*v && *v == ' ') { ++v; } /* trim value start */
1729 tmp = g_ascii_strdown(fb, -1); /* get header name */
1730 if (!strcmp(tmp, "to")) {
1732 G_PRINT_EXIT(_("Duplicated 'To:' header\n"));
1735 g_string_append_c(headers, '&');
1736 g_string_append(headers, tmp);
1737 g_string_append_c(headers, '=');
1738 #if GLIB_CHECK_VERSION(2,16,0)
1739 g_string_append_uri_escaped(headers, v, NULL, TRUE);
1741 G_STRING_APPEND_ENCODED_URI(headers, v);
1747 G_PRINT_EXIT(_("Missing required 'To:' header\n"));
1748 g_string_append(body, to);
1750 g_string_append(body, "?body=");
1751 while (fgets(fb, sizeof(fb), fp)) {
1752 #if GLIB_CHECK_VERSION(2,16,0)
1753 g_string_append_uri_escaped(body, fb, NULL, TRUE);
1755 G_STRING_APPEND_ENCODED_URI(body, fb);
1760 /* append the remaining headers */
1761 g_string_append(body, headers->str);
1762 g_string_free(headers, TRUE);
1767 #undef G_STRING_APPEND_ENCODED_URI
1770 static void parse_cmd_opt(int argc, char *argv[])
1775 for (i = 1; i < argc; i++) {
1776 if (!strncmp(argv[i], "--receive-all", 13)) {
1777 cmd.receive_all = TRUE;
1778 } else if (!strncmp(argv[i], "--receive", 9)) {
1780 } else if (!strncmp(argv[i], "--cancel-receiving", 18)) {
1781 cmd.cancel_receiving = TRUE;
1782 } else if (!strncmp(argv[i], "--cancel-sending", 16)) {
1783 cmd.cancel_sending = TRUE;
1784 } else if (!strncmp(argv[i], "--compose-from-file", 19)) {
1785 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1787 GString *mailto = parse_cmd_compose_from_file(p);
1789 cmd.compose_mailto = mailto->str;
1791 } else if (!strncmp(argv[i], "--compose", 9)) {
1792 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1795 cmd.compose_mailto = NULL;
1796 if (p && *p != '\0' && *p != '-') {
1797 if (!strncmp(p, "mailto:", 7)) {
1798 cmd.compose_mailto = p + 7;
1800 cmd.compose_mailto = p;
1804 } else if (!strncmp(argv[i], "--subscribe", 11)) {
1805 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1806 if (p && *p != '\0' && *p != '-') {
1807 cmd.subscribe = TRUE;
1808 cmd.subscribe_uri = p;
1810 } else if (!strncmp(argv[i], "--attach", 8)) {
1811 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1814 while (p && *p != '\0' && *p != '-') {
1815 if ((file = g_filename_from_uri(p, NULL, NULL)) != NULL) {
1816 if (!is_file_exist(file)) {
1821 if (file == NULL && *p != G_DIR_SEPARATOR) {
1822 file = g_strconcat(claws_get_startup_dir(),
1825 } else if (file == NULL) {
1828 ainfo = g_new0(AttachInfo, 1);
1830 cmd.attach_files = g_list_append(cmd.attach_files, ainfo);
1832 p = (i+1 < argc)?argv[i+1]:NULL;
1834 } else if (!strncmp(argv[i], "--send", 6)) {
1836 } else if (!strncmp(argv[i], "--version-full", 14) ||
1837 !strncmp(argv[i], "-V", 2)) {
1838 g_print("Claws Mail version " VERSION "\n");
1839 main_dump_features_list(FALSE);
1841 } else if (!strncmp(argv[i], "--version", 9) ||
1842 !strncmp(argv[i], "-v", 2)) {
1843 g_print("Claws Mail version " VERSION "\n");
1845 } else if (!strncmp(argv[i], "--status-full", 13)) {
1846 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1848 cmd.status_full = TRUE;
1849 while (p && *p != '\0' && *p != '-') {
1850 if (!cmd.status_full_folders) {
1851 cmd.status_full_folders =
1854 g_ptr_array_add(cmd.status_full_folders,
1857 p = (i+1 < argc)?argv[i+1]:NULL;
1859 } else if (!strncmp(argv[i], "--status", 8)) {
1860 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1863 while (p && *p != '\0' && *p != '-') {
1864 if (!cmd.status_folders)
1865 cmd.status_folders = g_ptr_array_new();
1866 g_ptr_array_add(cmd.status_folders,
1869 p = (i+1 < argc)?argv[i+1]:NULL;
1871 } else if (!strncmp(argv[i], "--search", 8)) {
1872 cmd.search_folder = (i+1 < argc)?argv[i+1]:NULL;
1873 cmd.search_type = (i+2 < argc)?argv[i+2]:NULL;
1874 cmd.search_request = (i+3 < argc)?argv[i+3]:NULL;
1875 const char* rec = (i+4 < argc)?argv[i+4]:NULL;
1876 cmd.search_recursive = TRUE;
1877 if (rec && (tolower(*rec)=='n' || tolower(*rec)=='f' || *rec=='0'))
1878 cmd.search_recursive = FALSE;
1879 if (cmd.search_folder && cmd.search_type && cmd.search_request)
1881 } else if (!strncmp(argv[i], "--online", 8)) {
1882 cmd.online_mode = ONLINE_MODE_ONLINE;
1883 } else if (!strncmp(argv[i], "--offline", 9)) {
1884 cmd.online_mode = ONLINE_MODE_OFFLINE;
1885 } else if (!strncmp(argv[i], "--toggle-debug", 14)) {
1887 } else if (!strncmp(argv[i], "--statistics", 12)) {
1888 cmd.statistics = TRUE;
1889 } else if (!strncmp(argv[i], "--reset-statistics", 18)) {
1890 cmd.reset_statistics = TRUE;
1891 } else if (!strncmp(argv[i], "--help", 6) ||
1892 !strncmp(argv[i], "-h", 2)) {
1893 gchar *base = g_path_get_basename(argv[0]);
1894 g_print(_("Usage: %s [OPTION]...\n"), base);
1896 g_print("%s\n", _(" --compose [address] open composition window"));
1897 g_print("%s\n", _(" --compose-from-file file\n"
1898 " open composition window with data from given file;\n"
1899 " use - as file name for reading from standard input;\n"
1900 " content format: headers first (To: required) until an\n"
1901 " empty line, then mail body until end of file."));
1902 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
1903 g_print("%s\n", _(" --attach file1 [file2]...\n"
1904 " open composition window with specified files\n"
1906 g_print("%s\n", _(" --receive receive new messages"));
1907 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
1908 g_print("%s\n", _(" --cancel-receiving cancel receiving of messages"));
1909 g_print("%s\n", _(" --cancel-sending cancel sending of messages"));
1910 g_print("%s\n", _(" --search folder type request [recursive]\n"
1912 " folder ex.: \"#mh/Mailbox/inbox\" or \"Mail\"\n"
1913 " type: s[ubject],f[rom],t[o],e[xtended],m[ixed] or g: tag\n"
1914 " request: search string\n"
1915 " recursive: false if arg. starts with 0, n, N, f or F"));
1917 g_print("%s\n", _(" --send send all queued messages"));
1918 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
1919 g_print("%s\n", _(" --status-full [folder]...\n"
1920 " show the status of each folder"));
1921 g_print("%s\n", _(" --statistics show session statistics"));
1922 g_print("%s\n", _(" --reset-statistics reset session statistics"));
1923 g_print("%s\n", _(" --select folder[/msg] jumps to the specified folder/message\n"
1924 " folder is a folder id like 'folder/sub_folder'"));
1925 g_print("%s\n", _(" --online switch to online mode"));
1926 g_print("%s\n", _(" --offline switch to offline mode"));
1927 g_print("%s\n", _(" --exit --quit -q exit Claws Mail"));
1928 g_print("%s\n", _(" --debug debug mode"));
1929 g_print("%s\n", _(" --toggle-debug toggle debug mode"));
1930 g_print("%s\n", _(" --help -h display this help and exit"));
1931 g_print("%s\n", _(" --version -v output version information and exit"));
1932 g_print("%s\n", _(" --version-full -V output version and built-in features information and exit"));
1933 g_print("%s\n", _(" --config-dir output configuration directory"));
1934 g_print("%s\n", _(" --alternate-config-dir [dir]\n"
1935 " use specified configuration directory"));
1939 } else if (!strncmp(argv[i], "--crash", 7)) {
1941 cmd.crash_params = g_strdup((i+1 < argc)?argv[i+1]:NULL);
1943 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
1944 g_print(RC_DIR "\n");
1946 } else if (!strncmp(argv[i], "--alternate-config-dir", sizeof "--alternate-config-dir" - 1) && i+1 < argc) {
1947 set_rc_dir(argv[i+1]);
1948 } else if (!strncmp(argv[i], "--exit", 6) ||
1949 !strncmp(argv[i], "--quit", 6) ||
1950 !strncmp(argv[i], "-q", 2)) {
1952 } else if (!strncmp(argv[i], "--select", 8) && i+1 < argc) {
1953 cmd.target = argv[i+1];
1954 } else if (i == 1 && argc == 2) {
1955 /* only one parameter. Do something intelligent about it */
1956 if ((strstr(argv[i], "@")||!strncmp(argv[i], "mailto:", 7)) && !strstr(argv[i], "://")) {
1957 const gchar *p = argv[i];
1960 cmd.compose_mailto = NULL;
1961 if (p && *p != '\0' && *p != '-') {
1962 if (!strncmp(p, "mailto:", 7)) {
1963 cmd.compose_mailto = p + 7;
1965 cmd.compose_mailto = p;
1968 } else if (!strncmp(argv[i], "file://", 7)) {
1969 cmd.target = argv[i];
1970 } else if (!strncmp(argv[i], "?attach=file://", strlen("?attach=file://"))) {
1972 cmd.compose_mailto = argv[i];
1973 } else if (strstr(argv[i], "://")) {
1974 const gchar *p = argv[i];
1975 if (p && *p != '\0' && *p != '-') {
1976 cmd.subscribe = TRUE;
1977 cmd.subscribe_uri = p;
1979 } else if (!strcmp(argv[i], "--sync")) {
1981 } else if (is_dir_exist(argv[i]) || is_file_exist(argv[i])) {
1982 cmd.target = argv[i];
1984 g_print(_("Unknown option\n"));
1990 if (cmd.attach_files && cmd.compose == FALSE) {
1992 cmd.compose_mailto = NULL;
1996 static void initial_processing(FolderItem *item, gpointer data)
1998 MainWindow *mainwin = (MainWindow *)data;
2001 cm_return_if_fail(item);
2002 buf = g_strdup_printf(_("Processing (%s)..."),
2005 : _("top level folder"));
2009 if (item->prefs->enable_processing) {
2010 item->processing_pending = TRUE;
2011 folder_item_apply_processing(item);
2012 item->processing_pending = FALSE;
2015 STATUSBAR_POP(mainwin);
2018 static gboolean draft_all_messages(void)
2020 GList *compose_list = NULL;
2022 compose_clear_exit_drafts();
2023 compose_list = compose_get_compose_list();
2024 while (compose_list != NULL) {
2025 Compose *c = (Compose*)compose_list->data;
2026 if (!compose_draft(c, COMPOSE_DRAFT_FOR_EXIT))
2028 compose_list = compose_get_compose_list();
2032 gboolean clean_quit(gpointer data)
2034 static gboolean firstrun = TRUE;
2041 /*!< Good idea to have the main window stored in a
2042 * static variable so we can check that variable
2043 * to see if we're really allowed to do things
2044 * that actually the spawner is supposed to
2045 * do (like: sending mail, composing messages).
2046 * Because, really, if we're the spawnee, and
2047 * we touch GTK stuff, we're hosed. See the
2050 /* FIXME: Use something else to signal that we're
2051 * in the original spawner, and not in a spawned
2053 if (!static_mainwindow) {
2057 draft_all_messages();
2058 emergency_exit = TRUE;
2059 exit_claws(static_mainwindow);
2065 void app_will_exit(GtkWidget *widget, gpointer data)
2067 MainWindow *mainwin = data;
2069 if (gtk_main_level() == 0) {
2070 debug_print("not even started\n");
2073 if (sc_exiting == TRUE) {
2074 debug_print("exit pending\n");
2078 debug_print("exiting\n");
2079 if (compose_get_compose_list()) {
2080 if (!draft_all_messages()) {
2081 main_window_popup(mainwin);
2087 if (prefs_common.warn_queued_on_exit && procmsg_have_queued_mails_fast()) {
2088 if (alertpanel(_("Queued messages"),
2089 _("Some unsent messages are queued. Exit now?"),
2090 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL)
2091 != G_ALERTALTERNATE) {
2092 main_window_popup(mainwin);
2096 manage_window_focus_in(mainwin->window, NULL, NULL);
2100 #ifdef HAVE_VALGRIND
2101 if (RUNNING_ON_VALGRIND) {
2102 summary_clear_list(mainwin->summaryview);
2105 if (folderview_get_selected_item(mainwin->folderview))
2106 folder_item_close(folderview_get_selected_item(mainwin->folderview));
2110 gboolean claws_is_exiting(void)
2115 gboolean claws_is_starting(void)
2122 * CLAWS: want this public so crash dialog can delete the
2125 gchar *claws_get_socket_name(void)
2127 static gchar *filename = NULL;
2128 const gchar *socket_dir = NULL;
2131 if (filename == NULL) {
2133 socket_dir = g_strdup_printf("%s%cclaws-mail-%d",
2134 g_get_tmp_dir(), G_DIR_SEPARATOR,
2140 if (stat(socket_dir, &st) < 0 && errno != ENOENT) {
2141 g_print("Error stat'ing socket_dir %s: %s\n",
2142 socket_dir, strerror(errno));
2143 } else if (S_ISSOCK(st.st_mode)) {
2144 /* old versions used a sock in $TMPDIR/claws-mail-$UID */
2145 debug_print("Using legacy socket %s\n", socket_dir);
2146 filename = g_strdup(socket_dir);
2150 if (!is_dir_exist(socket_dir) && make_dir(socket_dir) < 0) {
2151 g_print("Error creating socket_dir %s: %s\n",
2152 socket_dir, strerror(errno));
2155 md5_hex_digest(md5sum, get_rc_dir());
2157 filename = g_strdup_printf("%s%c%s", socket_dir, G_DIR_SEPARATOR,
2159 debug_print("Using control socket %s\n", filename);
2166 static gchar *get_crashfile_name(void)
2168 static gchar *filename = NULL;
2170 if (filename == NULL) {
2171 filename = g_strdup_printf("%s%cclaws-crashed",
2172 get_tmp_dir(), G_DIR_SEPARATOR);
2178 static gint prohibit_duplicate_launch(void)
2185 path = claws_get_socket_name();
2186 /* Try to connect to the control socket */
2187 uxsock = fd_connect_unix(path);
2189 if (x_display == NULL)
2190 x_display = g_strdup(g_getenv("DISPLAY"));
2197 /* If connect failed, no other process is running.
2198 * Unlink the potentially existing socket, then
2199 * open it. This has to be done locking a temporary
2200 * file to avoid the race condition where another
2201 * process could have created the socket just in
2204 socket_lock = g_strconcat(path, ".lock",
2206 lock_fd = g_open(socket_lock, O_RDWR|O_CREAT, 0);
2208 debug_print("Couldn't open %s: %s (%d)\n", socket_lock,
2209 strerror(errno), errno);
2210 g_free(socket_lock);
2213 if (flock(lock_fd, LOCK_EX) < 0) {
2214 debug_print("Couldn't lock %s: %s (%d)\n", socket_lock,
2215 strerror(errno), errno);
2217 g_free(socket_lock);
2223 debug_print("Opening socket %s\n", path);
2224 ret = fd_open_unix(path);
2226 flock(lock_fd, LOCK_UN);
2227 claws_unlink(socket_lock);
2228 g_free(socket_lock);
2235 hmutex = CreateMutexA(NULL, FALSE, "ClawsMail");
2237 debug_print("cannot create Mutex\n");
2240 if (GetLastError() != ERROR_ALREADY_EXISTS) {
2241 uxsock = fd_open_inet(50216);
2247 uxsock = fd_connect_inet(50216);
2251 /* remote command mode */
2253 debug_print("another Claws Mail instance is already running.\n");
2255 if (cmd.receive_all) {
2256 fd_write_all(uxsock, "receive_all\n", 12);
2257 } else if (cmd.receive) {
2258 fd_write_all(uxsock, "receive\n", 8);
2259 } else if (cmd.cancel_receiving) {
2260 fd_write_all(uxsock, "cancel_receiving\n", 17);
2261 } else if (cmd.cancel_sending) {
2262 fd_write_all(uxsock, "cancel_sending\n", 15);
2263 } else if (cmd.compose && cmd.attach_files) {
2264 gchar *str, *compose_str;
2266 if (cmd.compose_mailto) {
2267 compose_str = g_strdup_printf("compose_attach %s\n",
2268 cmd.compose_mailto);
2270 compose_str = g_strdup("compose_attach\n");
2273 fd_write_all(uxsock, compose_str, strlen(compose_str));
2274 g_free(compose_str);
2276 for (curr = cmd.attach_files; curr != NULL ; curr = curr->next) {
2277 str = (gchar *) ((AttachInfo *)curr->data)->file;
2278 fd_write_all(uxsock, str, strlen(str));
2279 fd_write_all(uxsock, "\n", 1);
2282 fd_write_all(uxsock, ".\n", 2);
2283 } else if (cmd.compose) {
2286 if (cmd.compose_mailto) {
2287 compose_str = g_strdup_printf
2288 ("compose %s\n", cmd.compose_mailto);
2290 compose_str = g_strdup("compose\n");
2293 fd_write_all(uxsock, compose_str, strlen(compose_str));
2294 g_free(compose_str);
2295 } else if (cmd.subscribe) {
2296 gchar *str = g_strdup_printf("subscribe %s\n", cmd.subscribe_uri);
2297 fd_write_all(uxsock, str, strlen(str));
2299 } else if (cmd.send) {
2300 fd_write_all(uxsock, "send\n", 5);
2301 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
2302 fd_write(uxsock, "online\n", 6);
2303 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
2304 fd_write(uxsock, "offline\n", 7);
2305 } else if (cmd.debug) {
2306 fd_write(uxsock, "debug\n", 7);
2307 } else if (cmd.status || cmd.status_full) {
2308 gchar buf[BUFFSIZE];
2310 const gchar *command;
2314 command = cmd.status_full ? "status-full\n" : "status\n";
2315 folders = cmd.status_full ? cmd.status_full_folders :
2318 fd_write_all(uxsock, command, strlen(command));
2319 for (i = 0; folders && i < folders->len; ++i) {
2320 folder = g_ptr_array_index(folders, i);
2321 fd_write_all(uxsock, folder, strlen(folder));
2322 fd_write_all(uxsock, "\n", 1);
2324 fd_write_all(uxsock, ".\n", 2);
2326 fd_gets(uxsock, buf, sizeof(buf));
2327 if (!strncmp(buf, ".\n", 2)) break;
2330 } else if (cmd.exit) {
2331 fd_write_all(uxsock, "exit\n", 5);
2332 } else if (cmd.statistics) {
2334 fd_write(uxsock, "statistics\n", 11);
2336 fd_gets(uxsock, buf, sizeof(buf));
2337 if (!strncmp(buf, ".\n", 2)) break;
2340 } else if (cmd.reset_statistics) {
2341 fd_write(uxsock, "reset_statistics\n", 17);
2342 } else if (cmd.target) {
2343 gchar *str = g_strdup_printf("select %s\n", cmd.target);
2344 fd_write_all(uxsock, str, strlen(str));
2346 } else if (cmd.search) {
2347 gchar buf[BUFFSIZE];
2349 g_strdup_printf("search %s\n%s\n%s\n%c\n",
2350 cmd.search_folder, cmd.search_type, cmd.search_request,
2351 (cmd.search_recursive==TRUE)?'1':'0');
2352 fd_write_all(uxsock, str, strlen(str));
2355 fd_gets(uxsock, buf, sizeof(buf));
2356 if (!strncmp(buf, ".\n", 2)) break;
2362 fd_write_all(uxsock, "get_display\n", 12);
2363 memset(buf, 0, sizeof(buf));
2364 fd_gets(uxsock, buf, sizeof(buf));
2365 if (strcmp2(buf, x_display)) {
2366 g_print("Claws Mail is already running on display %s.\n",
2370 uxsock = fd_connect_unix(path);
2371 fd_write_all(uxsock, "popup\n", 6);
2374 fd_write_all(uxsock, "popup\n", 6);
2382 static gint lock_socket_remove(void)
2385 gchar *filename, *dirname;
2387 if (lock_socket < 0) {
2391 if (lock_socket_tag > 0) {
2392 g_source_remove(lock_socket_tag);
2394 fd_close(lock_socket);
2397 filename = claws_get_socket_name();
2398 dirname = g_path_get_dirname(filename);
2399 claws_unlink(filename);
2407 static GPtrArray *get_folder_item_list(gint sock)
2409 gchar buf[BUFFSIZE];
2411 GPtrArray *folders = NULL;
2414 fd_gets(sock, buf, sizeof(buf));
2415 if (!strncmp(buf, ".\n", 2)) {
2420 folders = g_ptr_array_new();
2422 item = folder_find_item_from_identifier(buf);
2424 g_ptr_array_add(folders, item);
2426 g_warning("no such folder: %s\n", buf);
2433 static void lock_socket_input_cb(gpointer data,
2435 GIOCondition condition)
2437 MainWindow *mainwin = (MainWindow *)data;
2439 gchar buf[BUFFSIZE];
2441 sock = fd_accept(source);
2442 fd_gets(sock, buf, sizeof(buf));
2444 if (!strncmp(buf, "popup", 5)) {
2445 main_window_popup(mainwin);
2446 } else if (!strncmp(buf, "get_display", 11)) {
2447 fd_write_all(sock, x_display, strlen(x_display));
2448 } else if (!strncmp(buf, "receive_all", 11)) {
2449 inc_all_account_mail(mainwin, FALSE,
2450 prefs_common.newmail_notify_manu);
2451 } else if (!strncmp(buf, "receive", 7)) {
2452 inc_mail(mainwin, prefs_common.newmail_notify_manu);
2453 } else if (!strncmp(buf, "cancel_receiving", 16)) {
2456 } else if (!strncmp(buf, "cancel_sending", 14)) {
2458 } else if (!strncmp(buf, "compose_attach", 14)) {
2459 GList *files = NULL, *curr;
2463 mailto = g_strdup(buf + strlen("compose_attach") + 1);
2464 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
2466 if (!strcmp2(buf, "."))
2469 ainfo = g_new0(AttachInfo, 1);
2470 ainfo->file = g_strdup(buf);
2471 files = g_list_append(files, ainfo);
2473 open_compose_new(mailto, files);
2475 curr = g_list_first(files);
2476 while (curr != NULL) {
2477 ainfo = (AttachInfo *)curr->data;
2478 g_free(ainfo->file);
2484 } else if (!strncmp(buf, "compose", 7)) {
2485 open_compose_new(buf + strlen("compose") + 1, NULL);
2486 } else if (!strncmp(buf, "subscribe", 9)) {
2487 main_window_popup(mainwin);
2488 folder_subscribe(buf + strlen("subscribe") + 1);
2489 } else if (!strncmp(buf, "send", 4)) {
2491 } else if (!strncmp(buf, "online", 6)) {
2492 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
2493 } else if (!strncmp(buf, "offline", 7)) {
2494 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
2495 } else if (!strncmp(buf, "debug", 5)) {
2496 debug_set_mode(debug_get_mode() ? FALSE : TRUE);
2497 } else if (!strncmp(buf, "status-full", 11) ||
2498 !strncmp(buf, "status", 6)) {
2502 folders = get_folder_item_list(sock);
2503 status = folder_get_status
2504 (folders, !strncmp(buf, "status-full", 11));
2505 fd_write_all(sock, status, strlen(status));
2506 fd_write_all(sock, ".\n", 2);
2508 if (folders) g_ptr_array_free(folders, TRUE);
2509 } else if (!strncmp(buf, "statistics", 10)) {
2512 g_snprintf(tmp, sizeof(tmp), _("Session statistics\n"));
2513 fd_write_all(sock, tmp, strlen(tmp));
2515 if (prefs_common.date_format) {
2520 lt = localtime(&session_stats.time_started);
2521 fast_strftime(date, len, prefs_common.date_format, lt);
2522 g_snprintf(tmp, sizeof(tmp), _("Started: %s\n"),
2523 lt ? date : ctime(&session_stats.time_started));
2525 g_snprintf(tmp, sizeof(tmp), _("Started: %s\n"),
2526 ctime(&session_stats.time_started));
2527 fd_write_all(sock, tmp, strlen(tmp));
2529 fd_write_all(sock, "\n", 1);
2531 g_snprintf(tmp, sizeof(tmp), _("Incoming traffic\n"));
2532 fd_write_all(sock, tmp, strlen(tmp));
2534 g_snprintf(tmp, sizeof(tmp), _("Received messages: %d\n"),
2535 session_stats.received);
2536 fd_write_all(sock, tmp, strlen(tmp));
2538 fd_write_all(sock, "\n", 1);
2540 g_snprintf(tmp, sizeof(tmp), _("Outgoing traffic\n"));
2541 fd_write_all(sock, tmp, strlen(tmp));
2543 g_snprintf(tmp, sizeof(tmp), _("New/redirected messages: %d\n"),
2544 session_stats.sent);
2545 fd_write_all(sock, tmp, strlen(tmp));
2547 g_snprintf(tmp, sizeof(tmp), _("Replied messages: %d\n"),
2548 session_stats.replied);
2549 fd_write_all(sock, tmp, strlen(tmp));
2551 g_snprintf(tmp, sizeof(tmp), _("Forwarded messages: %d\n"),
2552 session_stats.forwarded);
2553 fd_write_all(sock, tmp, strlen(tmp));
2555 g_snprintf(tmp, sizeof(tmp), _("Total outgoing messages: %d\n"),
2556 (session_stats.sent + session_stats.replied +
2557 session_stats.forwarded));
2558 fd_write_all(sock, tmp, strlen(tmp));
2560 fd_write_all(sock, ".\n", 2);
2561 } else if (!strncmp(buf, "reset_statistics", 16)) {
2563 } else if (!strncmp(buf, "select ", 7)) {
2564 const gchar *target = buf+7;
2565 mainwindow_jump_to(target, TRUE);
2566 } else if (!strncmp(buf, "search ", 7)) {
2567 FolderItem* folderItem = NULL;
2568 GSList *messages = NULL;
2569 gchar *folder_name = NULL;
2570 gchar *request = NULL;
2571 AdvancedSearch *search;
2573 AdvancedSearchType searchType = ADVANCED_SEARCH_EXTENDED;
2575 search = advsearch_new();
2577 folder_name = g_strdup(buf+7);
2578 strretchomp(folder_name);
2580 if (fd_gets(sock, buf, sizeof(buf)) <= 0)
2583 switch (toupper(buf[0])) {
2584 case 'S': searchType = ADVANCED_SEARCH_SUBJECT; break;
2585 case 'F': searchType = ADVANCED_SEARCH_FROM; break;
2586 case 'T': searchType = ADVANCED_SEARCH_TO; break;
2587 case 'M': searchType = ADVANCED_SEARCH_MIXED; break;
2588 case 'G': searchType = ADVANCED_SEARCH_TAG; break;
2589 case 'E': searchType = ADVANCED_SEARCH_EXTENDED; break;
2592 if (fd_gets(sock, buf, sizeof(buf)) <= 0)
2595 request = g_strdup(buf);
2596 strretchomp(request);
2599 if (fd_gets(sock, buf, sizeof(buf)) > 0)
2600 recursive = buf[0] != '0';
2602 debug_print("search: %s %i %s %i\n", folder_name, searchType, request, recursive);
2605 folderItem = folder_find_item_from_identifier(folder_name);
2607 if (folder_name && folderItem == NULL) {
2608 debug_print("Unknown folder item : '%s', searching folder\n",folder_name);
2609 Folder* folder = folder_find_from_path(folder_name);
2611 folderItem = FOLDER_ITEM(folder->node->data);
2613 debug_print("Unknown folder: '%s'\n",folder_name);
2615 debug_print("%s %s\n",folderItem->name, folderItem->path);
2618 if (folderItem != NULL) {
2619 advsearch_set(search, searchType, request);
2620 advsearch_search_msgs_in_folders(search, &messages, folderItem, recursive);
2622 g_print("Folder '%s' not found.\n'", folder_name);
2626 for (cur = messages; cur != NULL; cur = cur->next) {
2627 MsgInfo* msg = (MsgInfo *)cur->data;
2628 gchar *file = procmsg_get_message_file_path(msg);
2629 fd_write_all(sock, file, strlen(file));
2630 fd_write_all(sock, "\n", 1);
2633 fd_write_all(sock, ".\n", 2);
2636 g_free(folder_name);
2638 advsearch_free(search);
2639 if (messages != NULL)
2640 procmsg_msg_list_free(messages);
2641 } else if (!strncmp(buf, "exit", 4)) {
2642 if (prefs_common.clean_on_exit && !prefs_common.ask_on_clean) {
2643 procmsg_empty_all_trash();
2645 app_will_exit(NULL, mainwin);
2651 static void open_compose_new(const gchar *address, GList *attach_files)
2656 Xstrdup_a(addr, address, return);
2660 compose_new(NULL, addr, attach_files);
2663 static void send_queue(void)
2666 gchar *errstr = NULL;
2667 gboolean error = FALSE;
2668 for (list = folder_get_list(); list != NULL; list = list->next) {
2669 Folder *folder = list->data;
2671 if (folder->queue) {
2672 gint res = procmsg_send_queue
2673 (folder->queue, prefs_common.savemsg,
2677 folder_item_scan(folder->queue);
2685 alertpanel_error_log(_("Some errors occurred "
2686 "while sending queued messages:\n%s"), errstr);
2689 alertpanel_error_log("Some errors occurred "
2690 "while sending queued messages.");
2694 static void quit_signal_handler(int sig)
2696 debug_print("Quitting on signal %d\n", sig);
2698 g_timeout_add(0, clean_quit, NULL);
2701 static void install_basic_sighandlers()
2705 struct sigaction act;
2710 sigaddset(&mask, SIGTERM);
2713 sigaddset(&mask, SIGINT);
2716 sigaddset(&mask, SIGHUP);
2719 act.sa_handler = quit_signal_handler;
2724 sigaction(SIGTERM, &act, 0);
2727 sigaction(SIGINT, &act, 0);
2730 sigaction(SIGHUP, &act, 0);
2733 sigprocmask(SIG_UNBLOCK, &mask, 0);
2734 #endif /* !G_OS_WIN32 */
2737 #if (defined linux && defined SIGIO)
2738 static int mem_notify_fd = 0;
2740 static gboolean clean_caches(gpointer unused)
2742 if (static_mainwindow && static_mainwindow->lock_count > 0)
2744 debug_print("/dev/mem_notify: callback: Freeing some memory now!\n");
2745 folder_clean_cache_memory_force();
2749 static void memory_signal_handler(int sig)
2751 debug_print("/dev/mem_notify: Kernel says we should free up some memory!\n");
2752 g_timeout_add(10, clean_caches, NULL);
2755 static void install_memory_sighandler()
2758 struct sigaction act;
2761 mem_notify_fd = g_open("/dev/mem_notify", O_RDONLY|O_NONBLOCK, 0);
2762 if (mem_notify_fd == -1) {
2763 debug_print("/dev/mem_notify not available (%s)\n",
2768 fcntl(mem_notify_fd, F_SETOWN, getpid());
2769 flags = fcntl(mem_notify_fd, F_GETFL);
2770 fcntl(mem_notify_fd, flags|FASYNC);
2774 sigaddset(&mask, SIGIO);
2776 act.sa_handler = memory_signal_handler;
2780 sigaction(SIGIO, &act, 0);
2782 sigprocmask(SIG_UNBLOCK, &mask, 0);
2784 debug_print("/dev/mem_notify: installed handler\n");
2786 #endif /* linux && SIGIO */
2788 #ifdef HAVE_NETWORKMANAGER_SUPPORT
2789 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
2792 MainWindow *mainWin;
2795 if (static_mainwindow)
2796 mainWin = static_mainwindow;
2798 mainWin = (MainWindow*)data;
2800 if (!prefs_common.use_networkmanager)
2808 online = networkmanager_is_online(&error);
2810 if(online && went_offline_nm) {
2811 went_offline_nm = FALSE;
2812 main_window_toggle_work_offline(mainWin, FALSE, FALSE);
2813 debug_print("NetworkManager: Went online\n");
2814 log_message(LOG_PROTOCOL, _("NetworkManager: network is online.\n"));
2817 went_offline_nm = TRUE;
2818 main_window_toggle_work_offline(mainWin, TRUE, FALSE);
2819 debug_print("NetworkManager: Went offline\n");
2820 log_message(LOG_PROTOCOL, _("NetworkManager: network is offline.\n"));
2824 debug_print("Failed to get online information from NetworkManager: %s\n",
2826 g_error_free(error);
2830 debug_print("NetworkManager: Cannot change connection state because "
2831 "main window does not exist\n");
2834 /* Returns true (and sets error appropriately, if given) in case of error */
2835 gboolean networkmanager_is_online(GError **error)
2837 DBusGConnection *connection;
2839 GError *tmp_error = NULL;
2843 if (!prefs_common.use_networkmanager)
2848 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &tmp_error);
2851 /* If calling code doesn't do error checking, at least print some debug */
2852 if((error == NULL) || (*error == NULL))
2853 debug_print("Failed to open connection to system bus: %s\n",
2854 tmp_error->message);
2855 g_propagate_error(error, tmp_error);
2859 proxy = dbus_g_proxy_new_for_name(connection,
2860 "org.freedesktop.NetworkManager",
2861 "/org/freedesktop/NetworkManager",
2862 "org.freedesktop.NetworkManager");
2864 retVal = dbus_g_proxy_call(proxy,"state",&tmp_error, G_TYPE_INVALID,
2865 G_TYPE_UINT, &state, G_TYPE_INVALID);
2868 g_object_unref(proxy);
2870 dbus_g_connection_unref(connection);
2873 /* If calling code doesn't do error checking, at least print some debug */
2874 if((error == NULL) || (*error == NULL))
2875 debug_print("Failed to get state info from NetworkManager: %s\n",
2876 tmp_error->message);
2877 g_propagate_error(error, tmp_error);
2880 #if NM_CHECK_VERSION(0,8,992)
2881 return (state == NM_STATE_CONNECTED_LOCAL ||
2882 state == NM_STATE_CONNECTED_SITE ||
2883 state == NM_STATE_CONNECTED_GLOBAL ||
2884 state == NM_STATE_UNKNOWN);
2886 return (state == NM_STATE_CONNECTED ||
2887 state == NM_STATE_UNKNOWN);