2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2016 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/>.
21 #include "claws-features.h"
27 #include <glib/gi18n.h>
37 #include <sys/types.h>
44 #include <X11/SM/SMlib.h>
51 #include "file_checker.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>
69 #include <librsvg/rsvg.h>
74 #include "mainwindow.h"
75 #include "folderview.h"
76 #include "image_viewer.h"
77 #include "summaryview.h"
78 #include "prefs_common.h"
79 #include "prefs_account.h"
80 #include "prefs_actions.h"
81 #include "prefs_ext_prog.h"
82 #include "prefs_fonts.h"
83 #include "prefs_image_viewer.h"
84 #include "prefs_message.h"
85 #include "prefs_migration.h"
86 #include "prefs_receive.h"
87 #include "prefs_msg_colors.h"
88 #include "prefs_quote.h"
89 #include "prefs_spelling.h"
90 #include "prefs_summaries.h"
91 #include "prefs_themes.h"
92 #include "prefs_other.h"
93 #include "prefs_proxy.h"
94 #include "prefs_logging.h"
95 #include "prefs_send.h"
96 #include "prefs_wrapping.h"
97 #include "prefs_compose_writing.h"
98 #include "prefs_display_header.h"
103 #include "send_message.h"
106 #include "manage_window.h"
107 #include "alertpanel.h"
108 #include "statusbar.h"
109 #ifndef USE_ALT_ADDRBOOK
110 #include "addressbook.h"
112 #include "addressbook-dbus.h"
116 #include "folder_item_prefs.h"
119 #include "gtkutils.h"
122 #include "prefs_toolbar.h"
125 #include "imap_gtk.h"
126 #include "news_gtk.h"
131 #include "quicksearch.h"
132 #include "advsearch.h"
134 #include "passwordstore.h"
135 #include "file-utils.h"
138 #include "imap-thread.h"
139 #include "nntp-thread.h"
141 #include "stock_pixmap.h"
152 #ifdef HAVE_NETWORKMANAGER_SUPPORT
153 /* Went offline due to NetworkManager */
154 static gboolean went_offline_nm;
158 #ifdef HAVE_DBUS_GLIB
159 static DBusGProxy *awn_proxy = NULL;
163 #if (defined HAVE_LIBSM || defined CRASH_DIALOG)
167 #ifdef HAVE_STARTUP_NOTIFICATION
168 static SnLauncheeContext *sn_context = NULL;
169 static SnDisplay *sn_display = NULL;
172 static gint lock_socket = -1;
173 static gint lock_socket_tag = 0;
174 static gchar *x_display = NULL;
177 ONLINE_MODE_DONT_CHANGE,
182 static struct RemoteCmd {
184 gboolean receive_all;
185 gboolean cancel_receiving;
186 gboolean cancel_sending;
188 const gchar *compose_mailto;
191 const gchar *search_folder;
192 const gchar *search_type;
193 const gchar *search_request;
194 gboolean search_recursive;
196 gboolean status_full;
198 gboolean reset_statistics;
199 GPtrArray *status_folders;
200 GPtrArray *status_full_folders;
207 const gchar *subscribe_uri;
210 const gchar *geometry;
213 SessionStats session_stats;
215 static void reset_statistics(void);
217 static void parse_cmd_opt(int argc, char *argv[]);
219 static gint prohibit_duplicate_launch (void);
220 static gchar * get_crashfile_name (void);
221 static gint lock_socket_remove (void);
222 static void lock_socket_input_cb (gpointer data,
224 GIOCondition condition);
226 static void open_compose_new (const gchar *address,
227 GList *attach_files);
229 static void send_queue (void);
230 static void initial_processing (FolderItem *item, gpointer data);
231 static void quit_signal_handler (int sig);
232 static void install_basic_sighandlers (void);
233 #if (defined linux && defined SIGIO)
234 static void install_memory_sighandler (void);
236 static void exit_claws (MainWindow *mainwin);
238 #ifdef HAVE_NETWORKMANAGER_SUPPORT
239 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
243 #define MAKE_DIR_IF_NOT_EXIST(dir) \
245 if (!is_dir_exist(dir)) { \
246 if (is_file_exist(dir)) { \
248 (_("File '%s' already exists.\n" \
249 "Can't create folder."), \
253 if (make_dir(dir) < 0) \
258 static MainWindow *static_mainwindow;
260 static gboolean emergency_exit = FALSE;
262 #ifdef HAVE_STARTUP_NOTIFICATION
263 static void sn_error_trap_push(SnDisplay *display, Display *xdisplay)
265 gdk_error_trap_push();
268 static void sn_error_trap_pop(SnDisplay *display, Display *xdisplay)
270 gdk_error_trap_pop();
273 static void startup_notification_complete(gboolean with_window)
276 GtkWidget *hack = NULL;
279 /* this is needed to make the startup notification leave,
280 * if we have been launched from a menu.
281 * We have to display a window, so let it be very little */
282 hack = gtk_window_new(GTK_WINDOW_POPUP);
283 gtk_window_move(GTK_WINDOW(hack), 0, 0);
284 gtk_widget_set_size_request(hack, 1, 1);
285 gtk_widget_show(hack);
288 xdisplay = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
289 sn_display = sn_display_new(xdisplay,
292 sn_context = sn_launchee_context_new_from_environment(sn_display,
293 DefaultScreen(xdisplay));
295 if (sn_context != NULL) {
296 sn_launchee_context_complete(sn_context);
297 sn_launchee_context_unref(sn_context);
298 sn_display_unref(sn_display);
301 gtk_widget_destroy(hack);
304 #endif /* HAVE_STARTUP_NOTIFICATION */
306 static void claws_gtk_idle(void)
308 while(gtk_events_pending()) {
309 gtk_main_iteration();
314 static gboolean sc_starting = FALSE;
316 static gboolean defer_check_all(void *data)
318 gboolean autochk = GPOINTER_TO_INT(data);
320 inc_all_account_mail(static_mainwindow, autochk,
321 prefs_common.newmail_notify_manu);
325 main_window_set_menu_sensitive(static_mainwindow);
326 toolbar_main_set_sensitive(static_mainwindow);
331 static gboolean defer_check(void *data)
333 inc_mail(static_mainwindow, prefs_common.newmail_notify_manu);
337 main_window_set_menu_sensitive(static_mainwindow);
338 toolbar_main_set_sensitive(static_mainwindow);
343 static gboolean defer_jump(void *data)
345 if (cmd.receive_all) {
346 defer_check_all(GINT_TO_POINTER(FALSE));
347 } else if (prefs_common.chk_on_startup) {
348 defer_check_all(GINT_TO_POINTER(TRUE));
349 } else if (cmd.receive) {
352 mainwindow_jump_to(data, FALSE);
355 main_window_set_menu_sensitive(static_mainwindow);
356 toolbar_main_set_sensitive(static_mainwindow);
361 static void chk_update_val(GtkWidget *widget, gpointer data)
363 gboolean *val = (gboolean *)data;
364 *val = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
367 static gboolean migrate_old_config(const gchar *old_cfg_dir, const gchar *new_cfg_dir, const gchar *oldversion)
369 gchar *message = g_strdup_printf(_("Configuration for %s found.\n"
370 "Do you want to migrate this configuration?"), oldversion);
371 gchar *message2 = g_strdup_printf(_("\n\nYour Sylpheed filtering rules can be converted by a\n"
372 "script available at %s."), TOOLS_URI);
374 if (!strcmp(oldversion, "Sylpheed"))
375 message = g_strconcat(message, message2, NULL);
379 GtkWidget *window = NULL;
380 GtkWidget *keep_backup_chk;
381 gboolean backup = TRUE;
383 keep_backup_chk = gtk_check_button_new_with_label (_("Keep old configuration"));
384 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_backup_chk), TRUE);
385 CLAWS_SET_TIP(keep_backup_chk,
386 _("Keeping a backup will allow you to go back to an "
387 "older version, but may take a while if you have "
388 "cached IMAP or News data, and will take some extra "
389 "room on your disk."));
391 g_signal_connect(G_OBJECT(keep_backup_chk), "toggled",
392 G_CALLBACK(chk_update_val), &backup);
394 if (alertpanel_full(_("Migration of configuration"), message,
395 GTK_STOCK_NO, GTK_STOCK_YES, NULL, ALERTFOCUS_SECOND, FALSE,
396 keep_backup_chk, ALERT_QUESTION) != G_ALERTALTERNATE) {
400 /* we can either do a fast migration requiring not any extra disk
401 * space, or a slow one that copies the old configuration and leaves
405 window = label_window_create(_("Copying configuration... This may take a while..."));
408 r = copy_dir(old_cfg_dir, new_cfg_dir);
409 label_window_destroy(window);
411 /* if copy failed, we'll remove the partially copied
414 alertpanel_error(_("Migration failed!"));
415 remove_dir_recursive(new_cfg_dir);
418 /* fast mode failed, but we don't want backup */
419 remove_dir_recursive(old_cfg_dir);
423 window = label_window_create(_("Migrating configuration..."));
426 r = g_rename(old_cfg_dir, new_cfg_dir);
427 label_window_destroy(window);
429 /* if g_rename failed, we'll try to copy */
431 FILE_OP_ERROR(new_cfg_dir, "g_rename");
432 debug_print("rename failed, trying copy\n");
439 static int migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
442 gchar *plugin_path, *old_plugin_path, *new_plugin_path;
444 gboolean err = FALSE;
446 oldfp = claws_fopen(old_rc, "r");
449 newfp = claws_fopen(new_rc, "w");
455 plugin_path = g_strdup(get_plugin_dir());
456 new_plugin_path = g_strdup(plugin_path);
458 if (strstr(plugin_path, "/claws-mail/")) {
459 gchar *end = g_strdup(strstr(plugin_path, "/claws-mail/")+strlen("/claws-mail/"));
460 *(strstr(plugin_path, "/claws-mail/")) = '\0';
461 old_plugin_path = g_strconcat(plugin_path, "/sylpheed-claws/", end, NULL);
464 old_plugin_path = g_strdup(new_plugin_path);
466 debug_print("replacing %s with %s\n", old_plugin_path, new_plugin_path);
467 while (claws_fgets(buf, sizeof(buf), oldfp)) {
468 if (strncmp(buf, old_plugin_path, strlen(old_plugin_path))) {
469 err |= (claws_fputs(buf, newfp) == EOF);
471 debug_print("->replacing %s\n", buf);
472 debug_print(" with %s%s\n", new_plugin_path, buf+strlen(old_plugin_path));
473 err |= (claws_fputs(new_plugin_path, newfp) == EOF);
474 err |= (claws_fputs(buf+strlen(old_plugin_path), newfp) == EOF);
478 g_free(new_plugin_path);
479 g_free(old_plugin_path);
481 if (claws_safe_fclose(newfp) == EOF)
489 sc_client_set_value (MainWindow *mainwin,
500 prop.num_vals = num_vals;
504 if (mainwin->smc_conn)
505 SmcSetProperties ((SmcConn) mainwin->smc_conn, 1, proplist);
508 static void sc_die_callback (SmcConn smc_conn, SmPointer client_data)
513 static void sc_save_complete_callback(SmcConn smc_conn, SmPointer client_data)
517 static void sc_shutdown_cancelled_callback (SmcConn smc_conn, SmPointer client_data)
519 MainWindow *mainwin = (MainWindow *)client_data;
520 if (mainwin->smc_conn)
521 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
524 static void sc_save_yourself_callback (SmcConn smc_conn,
525 SmPointer client_data,
531 MainWindow *mainwin = (MainWindow *)client_data;
532 if (mainwin->smc_conn)
533 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
536 static IceIOErrorHandler sc_ice_installed_handler;
538 static void sc_ice_io_error_handler (IceConn connection)
540 if (sc_ice_installed_handler)
541 (*sc_ice_installed_handler) (connection);
543 static gboolean sc_process_ice_messages (GIOChannel *source,
544 GIOCondition condition,
547 IceConn connection = (IceConn) data;
548 IceProcessMessagesStatus status;
550 status = IceProcessMessages (connection, NULL, NULL);
552 if (status == IceProcessMessagesIOError) {
553 IcePointer context = IceGetConnectionContext (connection);
555 if (context && G_IS_OBJECT(context)) {
556 guint disconnect_id = g_signal_lookup ("disconnect", G_OBJECT_TYPE (context));
558 if (disconnect_id > 0)
559 g_signal_emit (context, disconnect_id, 0);
561 IceSetShutdownNegotiation (connection, False);
562 IceCloseConnection (connection);
569 static void new_ice_connection (IceConn connection, IcePointer client_data, Bool opening,
570 IcePointer *watch_data)
576 /* Make sure we don't pass on these file descriptors to any
578 fcntl(IceConnectionNumber(connection),F_SETFD,
579 fcntl(IceConnectionNumber(connection),F_GETFD,0) | FD_CLOEXEC);
581 channel = g_io_channel_unix_new (IceConnectionNumber (connection));
582 input_id = g_io_add_watch (channel,
583 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
584 sc_process_ice_messages,
586 g_io_channel_unref (channel);
588 *watch_data = (IcePointer) GUINT_TO_POINTER (input_id);
590 input_id = GPOINTER_TO_UINT ((gpointer) *watch_data);
591 g_source_remove (input_id);
595 static void sc_session_manager_connect(MainWindow *mainwin)
597 static gboolean connected = FALSE;
598 SmcCallbacks callbacks;
600 IceIOErrorHandler default_handler;
607 sc_ice_installed_handler = IceSetIOErrorHandler (NULL);
608 default_handler = IceSetIOErrorHandler (sc_ice_io_error_handler);
610 if (sc_ice_installed_handler == default_handler)
611 sc_ice_installed_handler = NULL;
613 IceAddConnectionWatch (new_ice_connection, NULL);
616 callbacks.save_yourself.callback = sc_save_yourself_callback;
617 callbacks.die.callback = sc_die_callback;
618 callbacks.save_complete.callback = sc_save_complete_callback;
619 callbacks.shutdown_cancelled.callback = sc_shutdown_cancelled_callback;
621 callbacks.save_yourself.client_data =
622 callbacks.die.client_data =
623 callbacks.save_complete.client_data =
624 callbacks.shutdown_cancelled.client_data = (SmPointer) mainwin;
625 if (g_getenv ("SESSION_MANAGER")) {
626 gchar error_string_ret[256] = "";
628 mainwin->smc_conn = (gpointer)
629 SmcOpenConnection (NULL, mainwin,
630 SmProtoMajor, SmProtoMinor,
631 SmcSaveYourselfProcMask | SmcDieProcMask |
632 SmcSaveCompleteProcMask |
633 SmcShutdownCancelledProcMask,
636 256, error_string_ret);
638 if (error_string_ret[0] || mainwin->smc_conn == NULL)
639 g_warning ("While connecting to session manager: %s.",
643 vals = g_new (SmPropValue, 1);
644 vals[0].length = strlen(argv0);
645 vals[0].value = argv0;
646 sc_client_set_value (mainwin, SmCloneCommand, SmLISTofARRAY8, 1, vals);
647 sc_client_set_value (mainwin, SmRestartCommand, SmLISTofARRAY8, 1, vals);
648 sc_client_set_value (mainwin, SmProgram, SmARRAY8, 1, vals);
650 vals[0].length = strlen(g_get_user_name()?g_get_user_name():"");
651 vals[0].value = g_strdup(g_get_user_name()?g_get_user_name():"");
652 sc_client_set_value (mainwin, SmUserID, SmARRAY8, 1, vals);
654 g_free(vals[0].value);
661 static gboolean sc_exiting = FALSE;
662 static gboolean show_at_startup = TRUE;
663 static gboolean claws_crashed_bool = FALSE;
665 gboolean claws_crashed(void) {
666 return claws_crashed_bool;
669 void main_set_show_at_startup(gboolean show)
671 show_at_startup = show;
675 static FILE* win32_debug_fp=NULL;
676 static guint win32_log_handler_app_id;
677 static guint win32_log_handler_glib_id;
678 static guint win32_log_handler_gtk_id;
680 static void win32_print_stdout(const gchar *string)
682 if (win32_debug_fp) {
683 fprintf(win32_debug_fp, "%s", string);
684 fflush(win32_debug_fp);
688 static void win32_print_stderr(const gchar *string)
690 if (win32_debug_fp) {
691 fprintf(win32_debug_fp, "%s", string);
692 fflush(win32_debug_fp);
696 static void win32_log(const gchar *log_domain, GLogLevelFlags log_level, const gchar* message, gpointer user_data)
698 if (win32_debug_fp) {
701 switch(log_level & G_LOG_LEVEL_MASK)
703 case G_LOG_LEVEL_ERROR:
706 case G_LOG_LEVEL_CRITICAL:
709 case G_LOG_LEVEL_WARNING:
712 case G_LOG_LEVEL_MESSAGE:
715 case G_LOG_LEVEL_INFO:
718 case G_LOG_LEVEL_DEBUG:
725 fprintf(win32_debug_fp, "%s: %s: %s", log_domain, type, message);
727 fprintf(win32_debug_fp, "%s: %s", type, message);
728 fflush(win32_debug_fp);
732 static void win32_open_log(void)
734 gchar *logfile = g_strconcat(g_get_tmp_dir(), G_DIR_SEPARATOR_S, "claws-win32.log", NULL);
735 gchar *oldlogfile = g_strconcat(g_get_tmp_dir(), G_DIR_SEPARATOR_S, "claws-win32.log.bak", NULL);
737 if (is_file_exist(logfile)) {
738 if (rename_force(logfile, oldlogfile) < 0)
739 FILE_OP_ERROR(logfile, "rename");
741 win32_debug_fp = claws_fopen(logfile, "w");
746 g_set_print_handler(win32_print_stdout);
747 g_set_printerr_handler(win32_print_stdout);
748 win32_log_handler_app_id = g_log_set_handler(NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
749 | G_LOG_FLAG_RECURSION, win32_log, NULL);
750 win32_log_handler_glib_id = g_log_set_handler("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
751 | G_LOG_FLAG_RECURSION, win32_log, NULL);
752 win32_log_handler_gtk_id = g_log_set_handler("Gtk", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
753 | G_LOG_FLAG_RECURSION, win32_log, NULL);
757 static void win32_close_log(void)
761 g_log_remove_handler("", win32_log_handler_app_id);
762 g_log_remove_handler("GLib", win32_log_handler_glib_id);
763 g_log_remove_handler("Gtk", win32_log_handler_gtk_id);
764 claws_fclose(win32_debug_fp);
770 static void main_dump_features_list(gboolean show_debug_only)
771 /* display compiled-in features list */
773 if (show_debug_only && !debug_get_mode())
777 debug_print("runtime 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);
781 g_print("runtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
782 gtk_major_version, gtk_minor_version, gtk_micro_version,
783 glib_major_version, glib_minor_version, glib_micro_version);
785 debug_print("buildtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
786 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
787 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
789 g_print("buildtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
790 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
791 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
794 debug_print("Compiled-in features:\n");
796 g_print("Compiled-in features:\n");
799 debug_print(" compface\n");
801 g_print(" compface\n");
805 debug_print(" Enchant\n");
807 g_print(" Enchant\n");
811 debug_print(" GnuTLS\n");
813 g_print(" GnuTLS\n");
817 debug_print(" IPv6\n");
823 debug_print(" iconv\n");
829 debug_print(" JPilot\n");
831 g_print(" JPilot\n");
835 debug_print(" LDAP\n");
841 debug_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
843 g_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
847 debug_print(" libSM\n");
851 #if HAVE_NETWORKMANAGER_SUPPORT
853 debug_print(" NetworkManager\n");
855 g_print(" NetworkManager\n");
859 debug_print(" librSVG " LIBRSVG_VERSION "\n");
861 g_print(" librSVG " LIBRSVG_VERSION "\n");
865 #ifdef HAVE_DBUS_GLIB
866 static gulong dbus_item_hook_id = HOOK_NONE;
867 static gulong dbus_folder_hook_id = HOOK_NONE;
869 static void uninstall_dbus_status_handler(void)
872 g_object_unref(awn_proxy);
874 if (dbus_item_hook_id != HOOK_NONE)
875 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, dbus_item_hook_id);
876 if (dbus_folder_hook_id != HOOK_NONE)
877 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, dbus_folder_hook_id);
880 static void dbus_update(FolderItem *removed_item)
882 guint new, unread, unreadmarked, marked, total;
883 guint replied, forwarded, locked, ignored, watched;
885 GError *error = NULL;
887 folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total,
888 &replied, &forwarded, &locked, &ignored,
891 total -= removed_item->total_msgs;
892 new -= removed_item->new_msgs;
893 unread -= removed_item->unread_msgs;
897 buf = g_strdup_printf("%d", new);
898 dbus_g_proxy_call(awn_proxy, "SetInfoByName", &error,
899 G_TYPE_STRING, "claws-mail",
901 G_TYPE_INVALID, G_TYPE_INVALID);
905 dbus_g_proxy_call(awn_proxy, "UnsetInfoByName", &error, G_TYPE_STRING,
906 "claws-mail", G_TYPE_INVALID, G_TYPE_INVALID);
909 debug_print("%s\n", error->message);
914 static gboolean dbus_status_update_folder_hook(gpointer source, gpointer data)
916 FolderUpdateData *hookdata;
918 if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
919 dbus_update(hookdata->item);
926 static gboolean dbus_status_update_item_hook(gpointer source, gpointer data)
933 static void install_dbus_status_handler(void)
935 GError *tmp_error = NULL;
936 DBusGConnection *connection = dbus_g_bus_get(DBUS_BUS_SESSION, &tmp_error);
939 /* If calling code doesn't do error checking, at least print some debug */
940 debug_print("Failed to open connection to session bus: %s\n",
942 g_error_free(tmp_error);
945 awn_proxy = dbus_g_proxy_new_for_name(connection,
946 "com.google.code.Awn",
947 "/com/google/code/Awn",
948 "com.google.code.Awn");
949 dbus_item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, dbus_status_update_item_hook, NULL);
950 if (dbus_item_hook_id == HOOK_NONE) {
951 g_warning("Failed to register folder item update hook");
952 uninstall_dbus_status_handler();
956 dbus_folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, dbus_status_update_folder_hook, NULL);
957 if (dbus_folder_hook_id == HOOK_NONE) {
958 g_warning("Failed to register folder update hook");
959 uninstall_dbus_status_handler();
965 static void reset_statistics(void)
967 /* (re-)initialize session statistics */
968 session_stats.received = 0;
969 session_stats.sent = 0;
970 session_stats.replied = 0;
971 session_stats.forwarded = 0;
972 session_stats.time_started = time(NULL);
975 int main(int argc, char *argv[])
977 #ifdef HAVE_DBUS_GLIB
978 DBusGConnection *connection;
981 #ifdef HAVE_NETWORKMANAGER_SUPPORT
982 DBusGProxy *nm_proxy;
986 FolderView *folderview;
988 gboolean crash_file_present = FALSE;
989 guint num_folder_class = 0;
990 gboolean asked_for_migration = FALSE;
991 gboolean start_done = TRUE;
992 GSList *plug_list = NULL;
993 gboolean never_ran = FALSE;
994 gboolean mainwin_shown = FALSE;
997 START_TIMING("startup");
1004 if (!claws_init(&argc, &argv)) {
1011 prog_version = PROG_VERSION;
1012 #if (defined HAVE_LIBSM || defined CRASH_DIALOG)
1013 argv0 = g_strdup(argv[0]);
1016 parse_cmd_opt(argc, argv);
1020 /* check and create unix domain socket for remote operation */
1021 lock_socket = prohibit_duplicate_launch();
1022 if (lock_socket < 0) {
1023 #ifdef HAVE_STARTUP_NOTIFICATION
1024 if(gtk_init_check(&argc, &argv))
1025 startup_notification_complete(TRUE);
1030 main_dump_features_list(TRUE);
1031 prefs_prepare_cache();
1035 #if !GTK_CHECK_VERSION(3, 0, 0)
1038 gtk_init(&argc, &argv);
1039 crash_main(cmd.crash_params);
1045 crash_install_handlers();
1047 install_basic_sighandlers();
1048 #if (defined linux && defined SIGIO)
1049 install_memory_sighandler();
1052 if (cmd.status || cmd.status_full || cmd.search ||
1053 cmd.statistics || cmd.reset_statistics ||
1054 cmd.cancel_receiving || cmd.cancel_sending ||
1056 puts("0 Claws Mail not running.");
1057 lock_socket_remove();
1063 #if !GLIB_CHECK_VERSION(2,32,0)
1064 if (!g_thread_supported())
1065 g_thread_init(NULL);
1070 #if !GTK_CHECK_VERSION(3, 0, 0)
1073 gtk_init(&argc, &argv);
1076 gtk_settings_set_string_property(gtk_settings_get_default(),
1080 gtk_settings_set_long_property(gtk_settings_get_default(),
1081 "gtk-auto-mnemonics",
1084 gtk_settings_set_long_property(gtk_settings_get_default(),
1085 "gtk-button-images",
1090 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1091 went_offline_nm = FALSE;
1094 #ifdef HAVE_DBUS_GLIB
1096 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
1099 debug_print("Failed to open connection to system bus: %s\n", error->message);
1100 g_error_free(error);
1103 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1104 nm_proxy = dbus_g_proxy_new_for_name(connection,
1105 "org.freedesktop.NetworkManager",
1106 "/org/freedesktop/NetworkManager",
1107 "org.freedesktop.NetworkManager");
1109 dbus_g_proxy_add_signal(nm_proxy, "StateChanged", G_TYPE_UINT, G_TYPE_INVALID);
1110 dbus_g_proxy_connect_signal(nm_proxy, "StateChanged",
1111 G_CALLBACK(networkmanager_state_change_cb),
1115 install_dbus_status_handler();
1119 #if !GTK_CHECK_VERSION(3, 0, 0)
1120 gtk_widget_set_default_colormap(
1121 gdk_screen_get_system_colormap(
1122 gdk_screen_get_default()));
1125 gtkut_create_ui_manager();
1127 /* Create container for all the menus we will be adding */
1128 MENUITEM_ADDUI("/", "Menus", NULL, GTK_UI_MANAGER_MENUBAR);
1130 if (!g_thread_supported()) {
1131 g_error(_("g_thread is not supported by glib.\n"));
1135 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1137 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1140 /* no config dir exists. See if we can migrate an old config. */
1141 if (!is_dir_exist(get_rc_dir())) {
1142 prefs_destroy_cache();
1145 /* if one of the old dirs exist, we'll ask if the user
1146 * want to migrates, and r will be TRUE if he said yes
1147 * and migration succeeded, and FALSE otherwise.
1149 if (is_dir_exist(OLD_GTK2_RC_DIR)) {
1150 r = migrate_old_config(OLD_GTK2_RC_DIR, get_rc_dir(),
1151 g_strconcat("Sylpheed-Claws 2.6.0 ", _("(or older)"), NULL));
1152 asked_for_migration = TRUE;
1153 } else if (is_dir_exist(OLDER_GTK2_RC_DIR)) {
1154 r = migrate_old_config(OLDER_GTK2_RC_DIR, get_rc_dir(),
1155 g_strconcat("Sylpheed-Claws 1.9.15 ",_("(or older)"), NULL));
1156 asked_for_migration = TRUE;
1157 } else if (is_dir_exist(OLD_GTK1_RC_DIR)) {
1158 r = migrate_old_config(OLD_GTK1_RC_DIR, get_rc_dir(),
1159 g_strconcat("Sylpheed-Claws 1.0.5 ",_("(or older)"), NULL));
1160 asked_for_migration = TRUE;
1161 } else if (is_dir_exist(SYLPHEED_RC_DIR)) {
1162 r = migrate_old_config(SYLPHEED_RC_DIR, get_rc_dir(), "Sylpheed");
1163 asked_for_migration = TRUE;
1166 /* If migration failed or the user didn't want to do it,
1167 * we create a new one (and we'll hit wizard later).
1169 if (r == FALSE && !is_dir_exist(get_rc_dir())) {
1171 if (copy_dir(SYSCONFDIR "/skel/.claws-mail", get_rc_dir()) < 0) {
1173 if (!is_dir_exist(get_rc_dir()) && make_dir(get_rc_dir()) < 0) {
1186 if (!is_file_exist(RC_DIR G_DIR_SEPARATOR_S COMMON_RC) &&
1187 is_file_exist(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC)) {
1188 /* post 2.6 name change */
1189 migrate_common_rc(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC,
1190 RC_DIR G_DIR_SEPARATOR_S COMMON_RC);
1194 plugin_load_all("Common");
1196 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
1197 gtk_rc_parse(userrc);
1200 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1201 gtk_accel_map_load (userrc);
1205 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_rc_dir(), 1, win32_close_log(););
1207 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
1210 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
1211 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
1212 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
1213 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
1214 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
1215 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR);
1217 crash_file_present = is_file_exist(get_crashfile_name());
1218 /* remove temporary files */
1219 remove_all_files(get_tmp_dir());
1220 remove_all_files(get_mime_tmp_dir());
1222 if (!cmd.crash && crash_file_present)
1223 claws_crashed_bool = TRUE;
1225 if (is_file_exist("claws.log")) {
1226 if (rename_force("claws.log", "claws.log.bak") < 0)
1227 FILE_OP_ERROR("claws.log", "rename");
1229 set_log_file(LOG_PROTOCOL, "claws.log");
1231 if (is_file_exist("filtering.log")) {
1232 if (rename_force("filtering.log", "filtering.log.bak") < 0)
1233 FILE_OP_ERROR("filtering.log", "rename");
1235 set_log_file(LOG_DEBUG_FILTERING, "filtering.log");
1238 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1240 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1243 folder_system_init();
1244 prefs_common_read_config();
1246 if (prefs_update_config_version_common() < 0) {
1247 debug_print("Main configuration file version upgrade failed, exiting\n");
1254 prefs_themes_init();
1256 prefs_ext_prog_init();
1257 prefs_wrapping_init();
1258 prefs_compose_writing_init();
1259 prefs_msg_colors_init();
1260 image_viewer_init();
1261 prefs_image_viewer_init();
1263 prefs_summaries_init();
1264 prefs_message_init();
1267 prefs_logging_init();
1268 prefs_receive_init();
1273 gtkaspell_checkers_init();
1274 prefs_spelling_init();
1277 codeconv_set_allow_jisx0201_kana(prefs_common.allow_jisx0201_kana);
1278 codeconv_set_broken_are_utf8(prefs_common.broken_are_utf8);
1281 if(prefs_common.gtk_theme && strcmp(prefs_common.gtk_theme, DEFAULT_W32_GTK_THEME))
1282 gtk_settings_set_string_property(gtk_settings_get_default(),
1284 prefs_common.gtk_theme,
1289 sock_set_io_timeout(prefs_common.io_timeout_secs);
1290 prefs_actions_read_config();
1291 prefs_display_header_read_config();
1292 /* prefs_filtering_read_config(); */
1293 #ifndef USE_ALT_ADDRBOOK
1294 addressbook_read_file();
1296 g_clear_error(&error);
1297 if (! addressbook_start_service(&error)) {
1298 g_warning("%s", error->message);
1299 g_clear_error(&error);
1302 addressbook_install_hooks(&error);
1305 gtkut_widget_init();
1306 stock_pixbuf_gdk(STOCK_PIXMAP_CLAWS_MAIL_ICON, &icon);
1307 gtk_window_set_default_icon(icon);
1309 folderview_initialize();
1315 mainwin = main_window_create();
1317 if (!check_file_integrity())
1320 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1321 networkmanager_state_change_cb(nm_proxy,NULL,mainwin);
1324 manage_window_focus_in(mainwin->window, NULL, NULL);
1325 folderview = mainwin->folderview;
1327 folderview_freeze(mainwin->folderview);
1328 folder_item_update_freeze();
1330 if ((ret = passwd_store_read_config()) < 0) {
1331 debug_print("Password store configuration file version upgrade failed (%d), exiting\n", ret);
1338 prefs_account_init();
1339 account_read_config_all();
1341 if (prefs_update_config_version_accounts() < 0) {
1342 debug_print("Accounts configuration file version upgrade failed, exiting\n");
1349 #ifdef HAVE_LIBETPAN
1350 imap_main_init(prefs_common.skip_ssl_cert_check);
1351 imap_main_set_timeout(prefs_common.io_timeout_secs);
1352 nntp_main_init(prefs_common.skip_ssl_cert_check);
1354 /* If we can't read a folder list or don't have accounts,
1355 * it means the configuration's not done. Either this is
1356 * a brand new install, a failed/refused migration,
1357 * or a failed config_version upgrade.
1359 if ((ret = folder_read_list()) < 0) {
1360 debug_print("Folderlist read failed (%d)\n", ret);
1361 prefs_destroy_cache();
1364 /* config_version update failed in folder_read_list(). We
1365 * do not want to run the wizard, just exit. */
1366 debug_print("Folderlist version upgrade failed, exiting\n");
1373 /* if run_wizard returns FALSE it's because it's
1374 * been cancelled. We can't do much but exit.
1375 * however, if the user was asked for a migration,
1376 * we remove the newly created directory so that
1377 * he's asked again for migration on next launch.*/
1378 if (!run_wizard(mainwin, TRUE)) {
1379 if (asked_for_migration)
1380 remove_dir_recursive(RC_DIR);
1386 main_window_reflect_prefs_all_now();
1387 folder_write_list();
1391 if (!account_get_list()) {
1392 prefs_destroy_cache();
1393 if (!run_wizard(mainwin, FALSE)) {
1394 if (asked_for_migration)
1395 remove_dir_recursive(RC_DIR);
1401 if(!account_get_list()) {
1402 exit_claws(mainwin);
1412 toolbar_main_set_sensitive(mainwin);
1413 main_window_set_menu_sensitive(mainwin);
1415 /* if crashed, show window early so that the user
1416 * sees what's happening */
1417 if (claws_crashed()) {
1418 main_window_popup(mainwin);
1419 mainwin_shown = TRUE;
1422 account_set_missing_folder();
1423 folder_set_missing_folders();
1424 folderview_set(folderview);
1426 prefs_matcher_read_config();
1427 quicksearch_set_search_strings(mainwin->summaryview->quicksearch);
1429 /* make one all-folder processing before using claws */
1430 main_window_cursor_wait(mainwin);
1431 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
1433 /* if claws crashed, rebuild caches */
1434 if (claws_crashed()) {
1436 debug_print("Claws Mail crashed, checking for new messages in local folders\n");
1437 folder_item_update_thaw();
1438 folderview_check_new(NULL);
1439 folder_clean_cache_memory_force();
1440 folder_item_update_freeze();
1442 /* make the crash-indicator file */
1443 str_write_to_file("foo", get_crashfile_name());
1445 inc_autocheck_timer_init(mainwin);
1447 /* ignore SIGPIPE signal for preventing sudden death of program */
1449 signal(SIGPIPE, SIG_IGN);
1451 if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
1452 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1454 if (cmd.online_mode == ONLINE_MODE_ONLINE) {
1455 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1458 if (cmd.status_folders) {
1459 g_ptr_array_free(cmd.status_folders, TRUE);
1460 cmd.status_folders = NULL;
1462 if (cmd.status_full_folders) {
1463 g_ptr_array_free(cmd.status_full_folders, TRUE);
1464 cmd.status_full_folders = NULL;
1467 claws_register_idle_function(claws_gtk_idle);
1470 prefs_toolbar_init();
1472 num_folder_class = g_list_length(folder_get_list());
1474 plugin_load_all("GTK2");
1476 if (g_list_length(folder_get_list()) != num_folder_class) {
1477 debug_print("new folders loaded, reloading processing rules\n");
1478 prefs_matcher_read_config();
1481 if ((plug_list = plugin_get_unloaded_list()) != NULL) {
1484 gint num_plugins = 0;
1485 for (cur = plug_list; cur; cur = cur->next) {
1486 Plugin *plugin = (Plugin *)cur->data;
1487 gchar *tmp = g_strdup_printf("%s\n%s",
1489 plugin_get_name(plugin));
1494 main_window_cursor_normal(mainwin);
1495 main_window_popup(mainwin);
1496 mainwin_shown = TRUE;
1497 alertpanel_warning(ngettext(
1498 "The following plugin failed to load. "
1499 "Check the Plugins configuration "
1500 "for more information:\n%s",
1501 "The following plugins failed to load. "
1502 "Check the Plugins configuration "
1503 "for more information:\n%s",
1506 main_window_cursor_wait(mainwin);
1508 g_slist_free(plug_list);
1512 prefs_common_write_config();
1513 plugin_load_standard_plugins ();
1516 /* if not crashed, show window now */
1517 if (!mainwin_shown) {
1518 /* apart if something told not to show */
1519 if (show_at_startup)
1520 main_window_popup(mainwin);
1523 if (cmd.geometry != NULL) {
1524 if (!gtk_window_parse_geometry(GTK_WINDOW(mainwin->window), cmd.geometry))
1525 g_warning("failed to parse geometry '%s'", cmd.geometry);
1529 if (sscanf(cmd.geometry, "%ux%u+", &width, &height) == 2)
1530 gtk_window_resize(GTK_WINDOW(mainwin->window), width, height);
1532 g_warning("failed to parse geometry's width/height");
1536 if (!folder_have_mailbox()) {
1537 prefs_destroy_cache();
1538 main_window_cursor_normal(mainwin);
1539 if (folder_get_list() != NULL) {
1540 alertpanel_error(_("Claws Mail has detected a configured "
1541 "mailbox, but it is incomplete. It is "
1542 "possibly due to a failing IMAP account. Use "
1543 "\"Rebuild folder tree\" on the mailbox parent "
1544 "folder's context menu to try to fix it."));
1546 alertpanel_error(_("Claws Mail has detected a configured "
1547 "mailbox, but could not load it. It is "
1548 "probably provided by an out-of-date "
1549 "external plugin. Please reinstall the "
1550 "plugin and try again."));
1551 exit_claws(mainwin);
1559 static_mainwindow = mainwin;
1561 #ifdef HAVE_STARTUP_NOTIFICATION
1562 startup_notification_complete(FALSE);
1565 sc_session_manager_connect(mainwin);
1568 folder_item_update_thaw();
1569 folderview_thaw(mainwin->folderview);
1570 main_window_cursor_normal(mainwin);
1572 if (!cmd.target && prefs_common.goto_last_folder_on_startup &&
1573 folder_find_item_from_identifier(prefs_common.last_opened_folder) != NULL &&
1575 cmd.target = prefs_common.last_opened_folder;
1578 if (cmd.receive_all && !cmd.target) {
1580 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(FALSE));
1581 } else if (prefs_common.chk_on_startup && !cmd.target) {
1583 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(TRUE));
1584 } else if (cmd.receive && !cmd.target) {
1586 g_timeout_add(1000, defer_check, NULL);
1588 folderview_grab_focus(folderview);
1591 open_compose_new(cmd.compose_mailto, cmd.attach_files);
1593 if (cmd.attach_files) {
1594 list_free_strings_full(cmd.attach_files);
1595 cmd.attach_files = NULL;
1597 if (cmd.subscribe) {
1598 folder_subscribe(cmd.subscribe_uri);
1607 g_timeout_add(500, defer_jump, (gpointer)cmd.target);
1610 prefs_destroy_cache();
1612 compose_reopen_exit_drafts();
1615 sc_starting = FALSE;
1616 main_window_set_menu_sensitive(mainwin);
1617 toolbar_main_set_sensitive(mainwin);
1620 /* register the callback of unix domain socket input */
1621 lock_socket_tag = claws_input_add(lock_socket,
1622 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
1623 lock_socket_input_cb,
1630 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1632 g_object_unref(nm_proxy);
1634 #ifdef HAVE_DBUS_GLIB
1635 uninstall_dbus_status_handler();
1637 dbus_g_connection_unref(connection);
1643 exit_claws(mainwin);
1648 static void save_all_caches(FolderItem *item, gpointer data)
1655 folder_item_close(item);
1658 folder_item_free_cache(item, TRUE);
1661 static void exit_claws(MainWindow *mainwin)
1664 gboolean have_connectivity;
1669 debug_print("shutting down\n");
1670 inc_autocheck_timer_remove();
1672 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1673 if (prefs_common.work_offline && went_offline_nm)
1674 prefs_common.work_offline = FALSE;
1677 /* save prefs for opened folder */
1678 if((item = folderview_get_opened_item(mainwin->folderview)) != NULL) {
1679 summary_save_prefs_to_folderitem(
1680 mainwin->summaryview, item);
1681 if (prefs_common.last_opened_folder != NULL)
1682 g_free(prefs_common.last_opened_folder);
1683 prefs_common.last_opened_folder =
1684 folder_item_get_identifier(item);
1687 /* save all state before exiting */
1688 folder_func_to_all_folders(save_all_caches, NULL);
1689 folder_write_list();
1691 main_window_get_size(mainwin);
1692 main_window_get_position(mainwin);
1694 prefs_common_write_config();
1695 account_write_config_all();
1696 passwd_store_write_config();
1697 #ifndef USE_ALT_ADDRBOOK
1698 addressbook_export_to_file();
1700 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1701 gtk_accel_map_save(filename);
1704 /* delete temporary files */
1705 remove_all_files(get_tmp_dir());
1706 remove_all_files(get_mime_tmp_dir());
1708 close_log_file(LOG_PROTOCOL);
1709 close_log_file(LOG_DEBUG_FILTERING);
1711 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1712 have_connectivity = networkmanager_is_online(NULL);
1714 have_connectivity = TRUE;
1716 #ifdef HAVE_LIBETPAN
1717 imap_main_done(have_connectivity);
1718 nntp_main_done(have_connectivity);
1720 /* delete crashfile */
1722 claws_unlink(get_crashfile_name());
1724 lock_socket_remove();
1727 if (mainwin->smc_conn)
1728 SmcCloseConnection ((SmcConn)mainwin->smc_conn, 0, NULL);
1729 mainwin->smc_conn = NULL;
1732 main_window_destroy_all();
1734 plugin_unload_all("GTK2");
1737 prefs_toolbar_done();
1740 #ifndef USE_ALT_ADDRBOOK
1741 addressbook_destroy();
1743 prefs_themes_done();
1745 prefs_ext_prog_done();
1746 prefs_wrapping_done();
1747 prefs_compose_writing_done();
1748 prefs_msg_colors_done();
1749 prefs_image_viewer_done();
1750 image_viewer_done();
1752 prefs_summaries_done();
1753 prefs_message_done();
1756 prefs_receive_done();
1757 prefs_logging_done();
1761 prefs_spelling_done();
1762 gtkaspell_checkers_quit();
1764 plugin_unload_all("Common");
1768 #define G_PRINT_EXIT(msg) \
1774 static GString * parse_cmd_compose_from_file(const gchar *fn)
1776 GString *headers = g_string_new(NULL);
1777 GString *body = g_string_new(NULL);
1785 if (fn == NULL || *fn == '\0')
1786 G_PRINT_EXIT(_("Missing filename\n"));
1787 isstdin = (*fn == '-' && *(fn + 1) == '\0');
1791 fp = claws_fopen(fn, "r");
1793 G_PRINT_EXIT(_("Cannot open filename for reading\n"));
1796 while (claws_fgets(fb, sizeof(fb), fp)) {
1802 while (*h && *h != ':') { ++h; } /* search colon */
1804 G_PRINT_EXIT(_("Malformed header\n"));
1806 while (*v && *v == ' ') { ++v; } /* trim value start */
1808 tmp = g_ascii_strdown(fb, -1); /* get header name */
1809 if (!strcmp(tmp, "to")) {
1811 G_PRINT_EXIT(_("Duplicated 'To:' header\n"));
1814 g_string_append_c(headers, '&');
1815 g_string_append(headers, tmp);
1816 g_string_append_c(headers, '=');
1817 g_string_append_uri_escaped(headers, v, NULL, TRUE);
1822 G_PRINT_EXIT(_("Missing required 'To:' header\n"));
1823 g_string_append(body, to);
1825 g_string_append(body, "?body=");
1826 while (claws_fgets(fb, sizeof(fb), fp)) {
1827 g_string_append_uri_escaped(body, fb, NULL, TRUE);
1831 /* append the remaining headers */
1832 g_string_append(body, headers->str);
1833 g_string_free(headers, TRUE);
1840 static void parse_cmd_opt(int argc, char *argv[])
1845 for (i = 1; i < argc; i++) {
1846 if (!strncmp(argv[i], "--receive-all", 13)) {
1847 cmd.receive_all = TRUE;
1848 } else if (!strncmp(argv[i], "--receive", 9)) {
1850 } else if (!strncmp(argv[i], "--cancel-receiving", 18)) {
1851 cmd.cancel_receiving = TRUE;
1852 } else if (!strncmp(argv[i], "--cancel-sending", 16)) {
1853 cmd.cancel_sending = TRUE;
1854 } else if (!strncmp(argv[i], "--compose-from-file", 19)) {
1855 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1857 GString *mailto = parse_cmd_compose_from_file(p);
1859 cmd.compose_mailto = mailto->str;
1861 } else if (!strncmp(argv[i], "--compose", 9)) {
1862 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1865 cmd.compose_mailto = NULL;
1866 if (p && *p != '\0' && *p != '-') {
1867 if (!strncmp(p, "mailto:", 7)) {
1868 cmd.compose_mailto = p + 7;
1870 cmd.compose_mailto = p;
1874 } else if (!strncmp(argv[i], "--subscribe", 11)) {
1875 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1876 if (p && *p != '\0' && *p != '-') {
1877 cmd.subscribe = TRUE;
1878 cmd.subscribe_uri = p;
1880 } else if (!strncmp(argv[i], "--attach", 8) || !strncmp(argv[i], "--insert", 8)) {
1881 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1883 gboolean insert = !strncmp(argv[i], "--insert", 8);
1885 while (p && *p != '\0' && *p != '-') {
1886 if ((file = g_filename_from_uri(p, NULL, NULL)) != NULL) {
1887 if (!is_file_exist(file)) {
1892 if (file == NULL && *p != G_DIR_SEPARATOR) {
1893 file = g_strconcat(claws_get_startup_dir(),
1896 } else if (file == NULL) {
1900 ainfo = g_new0(AttachInfo, 1);
1902 ainfo->insert = insert;
1903 cmd.attach_files = g_list_append(cmd.attach_files, ainfo);
1905 p = (i+1 < argc)?argv[i+1]:NULL;
1907 } else if (!strncmp(argv[i], "--send", 6)) {
1909 } else if (!strncmp(argv[i], "--version-full", 14) ||
1910 !strncmp(argv[i], "-V", 2)) {
1911 g_print("Claws Mail version " VERSION_GIT_FULL "\n");
1912 main_dump_features_list(FALSE);
1914 } else if (!strncmp(argv[i], "--version", 9) ||
1915 !strncmp(argv[i], "-v", 2)) {
1916 g_print("Claws Mail version " VERSION "\n");
1918 } else if (!strncmp(argv[i], "--status-full", 13)) {
1919 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1921 cmd.status_full = TRUE;
1922 while (p && *p != '\0' && *p != '-') {
1923 if (!cmd.status_full_folders) {
1924 cmd.status_full_folders =
1927 g_ptr_array_add(cmd.status_full_folders,
1930 p = (i+1 < argc)?argv[i+1]:NULL;
1932 } else if (!strncmp(argv[i], "--status", 8)) {
1933 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1936 while (p && *p != '\0' && *p != '-') {
1937 if (!cmd.status_folders)
1938 cmd.status_folders = g_ptr_array_new();
1939 g_ptr_array_add(cmd.status_folders,
1942 p = (i+1 < argc)?argv[i+1]:NULL;
1944 } else if (!strncmp(argv[i], "--search", 8)) {
1945 cmd.search_folder = (i+1 < argc)?argv[i+1]:NULL;
1946 cmd.search_type = (i+2 < argc)?argv[i+2]:NULL;
1947 cmd.search_request = (i+3 < argc)?argv[i+3]:NULL;
1948 const char* rec = (i+4 < argc)?argv[i+4]:NULL;
1949 cmd.search_recursive = TRUE;
1950 if (rec && (tolower(*rec)=='n' || tolower(*rec)=='f' || *rec=='0'))
1951 cmd.search_recursive = FALSE;
1952 if (cmd.search_folder && cmd.search_type && cmd.search_request)
1954 } else if (!strncmp(argv[i], "--online", 8)) {
1955 cmd.online_mode = ONLINE_MODE_ONLINE;
1956 } else if (!strncmp(argv[i], "--offline", 9)) {
1957 cmd.online_mode = ONLINE_MODE_OFFLINE;
1958 } else if (!strncmp(argv[i], "--toggle-debug", 14)) {
1960 } else if (!strncmp(argv[i], "--statistics", 12)) {
1961 cmd.statistics = TRUE;
1962 } else if (!strncmp(argv[i], "--reset-statistics", 18)) {
1963 cmd.reset_statistics = TRUE;
1964 } else if (!strncmp(argv[i], "--help", 6) ||
1965 !strncmp(argv[i], "-h", 2)) {
1966 gchar *base = g_path_get_basename(argv[0]);
1967 g_print(_("Usage: %s [OPTION]...\n"), base);
1969 g_print("%s\n", _(" --compose [address] open composition window"));
1970 g_print("%s\n", _(" --compose-from-file file\n"
1971 " open composition window with data from given file;\n"
1972 " use - as file name for reading from standard input;\n"
1973 " content format: headers first (To: required) until an\n"
1974 " empty line, then mail body until end of file."));
1975 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
1976 g_print("%s\n", _(" --attach file1 [file2]...\n"
1977 " open composition window with specified files\n"
1979 g_print("%s\n", _(" --insert file1 [file2]...\n"
1980 " open composition window with specified files\n"
1982 g_print("%s\n", _(" --receive receive new messages"));
1983 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
1984 g_print("%s\n", _(" --cancel-receiving cancel receiving of messages"));
1985 g_print("%s\n", _(" --cancel-sending cancel sending of messages"));
1986 g_print("%s\n", _(" --search folder type request [recursive]\n"
1988 " folder ex.: \"#mh/Mailbox/inbox\" or \"Mail\"\n"
1989 " type: s[ubject],f[rom],t[o],e[xtended],m[ixed] or g: tag\n"
1990 " request: search string\n"
1991 " recursive: false if arg. starts with 0, n, N, f or F"));
1993 g_print("%s\n", _(" --send send all queued messages"));
1994 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
1995 g_print("%s\n", _(" --status-full [folder]...\n"
1996 " show the status of each folder"));
1997 g_print("%s\n", _(" --statistics show session statistics"));
1998 g_print("%s\n", _(" --reset-statistics reset session statistics"));
1999 g_print("%s\n", _(" --select folder[/msg] jumps to the specified folder/message\n"
2000 " folder is a folder id like 'folder/sub_folder'"));
2001 g_print("%s\n", _(" --online switch to online mode"));
2002 g_print("%s\n", _(" --offline switch to offline mode"));
2003 g_print("%s\n", _(" --exit --quit -q exit Claws Mail"));
2004 g_print("%s\n", _(" --debug debug mode"));
2005 g_print("%s\n", _(" --toggle-debug toggle debug mode"));
2006 g_print("%s\n", _(" --help -h display this help and exit"));
2007 g_print("%s\n", _(" --version -v output version information and exit"));
2008 g_print("%s\n", _(" --version-full -V output version and built-in features information and exit"));
2009 g_print("%s\n", _(" --config-dir output configuration directory"));
2010 g_print("%s\n", _(" --alternate-config-dir [dir]\n"
2011 " use specified configuration directory"));
2012 g_print("%s\n", _(" --geometry -geometry WxH+X+Y\n"
2013 " set geometry for main window"));
2017 } else if (!strncmp(argv[i], "--crash", 7)) {
2019 cmd.crash_params = g_strdup((i+1 < argc)?argv[i+1]:NULL);
2021 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
2022 g_print(RC_DIR "\n");
2024 } else if (!strncmp(argv[i], "--alternate-config-dir", sizeof "--alternate-config-dir" - 1) && i+1 < argc) {
2025 set_rc_dir(argv[i+1]);
2026 } else if (!strncmp(argv[i], "--geometry", sizeof "--geometry" - 1)
2027 || !strncmp(argv[i], "-geometry", sizeof "-geometry" - 1)) {
2028 cmd.geometry = (i+1 < argc)? argv[i+1]: NULL;
2029 } else if (!strncmp(argv[i], "--exit", 6) ||
2030 !strncmp(argv[i], "--quit", 6) ||
2031 !strncmp(argv[i], "-q", 2)) {
2033 } else if (!strncmp(argv[i], "--select", 8) && i+1 < argc) {
2034 cmd.target = argv[i+1];
2035 } else if (i == 1 && argc == 2) {
2036 /* only one parameter. Do something intelligent about it */
2037 if ((strstr(argv[i], "@")||!strncmp(argv[i], "mailto:", 7)) && !strstr(argv[i], "://")) {
2038 const gchar *p = argv[i];
2041 cmd.compose_mailto = NULL;
2042 if (p && *p != '\0' && *p != '-') {
2043 if (!strncmp(p, "mailto:", 7)) {
2044 cmd.compose_mailto = p + 7;
2046 cmd.compose_mailto = p;
2049 } else if (!strncmp(argv[i], "file://", 7)) {
2050 cmd.target = argv[i];
2051 } else if (!strncmp(argv[i], "?attach=file://", strlen("?attach=file://"))) {
2053 cmd.compose_mailto = argv[i];
2054 } else if (strstr(argv[i], "://")) {
2055 const gchar *p = argv[i];
2056 if (p && *p != '\0' && *p != '-') {
2057 cmd.subscribe = TRUE;
2058 cmd.subscribe_uri = p;
2060 } else if (!strcmp(argv[i], "--sync")) {
2062 } else if (is_dir_exist(argv[i]) || is_file_exist(argv[i])) {
2063 cmd.target = argv[i];
2065 g_print(_("Unknown option\n"));
2071 if (cmd.attach_files && cmd.compose == FALSE) {
2073 cmd.compose_mailto = NULL;
2077 static void initial_processing(FolderItem *item, gpointer data)
2079 MainWindow *mainwin = (MainWindow *)data;
2082 cm_return_if_fail(item);
2083 buf = g_strdup_printf(_("Processing (%s)..."),
2086 : _("top level folder"));
2089 if (folder_item_parent(item) != NULL && item->prefs->enable_processing) {
2090 item->processing_pending = TRUE;
2091 folder_item_apply_processing(item);
2092 item->processing_pending = FALSE;
2095 STATUSBAR_POP(mainwin);
2098 static gboolean draft_all_messages(void)
2100 const GList *compose_list = NULL;
2102 compose_clear_exit_drafts();
2103 compose_list = compose_get_compose_list();
2104 while (compose_list != NULL) {
2105 Compose *c = (Compose*)compose_list->data;
2106 if (!compose_draft(c, COMPOSE_DRAFT_FOR_EXIT))
2108 compose_list = compose_get_compose_list();
2112 gboolean clean_quit(gpointer data)
2114 static gboolean firstrun = TRUE;
2121 /*!< Good idea to have the main window stored in a
2122 * static variable so we can check that variable
2123 * to see if we're really allowed to do things
2124 * that actually the spawner is supposed to
2125 * do (like: sending mail, composing messages).
2126 * Because, really, if we're the spawnee, and
2127 * we touch GTK stuff, we're hosed. See the
2130 /* FIXME: Use something else to signal that we're
2131 * in the original spawner, and not in a spawned
2133 if (!static_mainwindow) {
2137 draft_all_messages();
2138 emergency_exit = TRUE;
2139 exit_claws(static_mainwindow);
2145 void app_will_exit(GtkWidget *widget, gpointer data)
2147 MainWindow *mainwin = data;
2149 if (gtk_main_level() == 0) {
2150 debug_print("not even started\n");
2153 if (sc_exiting == TRUE) {
2154 debug_print("exit pending\n");
2158 debug_print("exiting\n");
2159 if (compose_get_compose_list()) {
2160 if (!draft_all_messages()) {
2161 main_window_popup(mainwin);
2167 if (prefs_common.warn_queued_on_exit && procmsg_have_queued_mails_fast()) {
2168 if (alertpanel(_("Queued messages"),
2169 _("Some unsent messages are queued. Exit now?"),
2170 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL, ALERTFOCUS_FIRST)
2171 != G_ALERTALTERNATE) {
2172 main_window_popup(mainwin);
2176 manage_window_focus_in(mainwin->window, NULL, NULL);
2180 #ifdef HAVE_VALGRIND
2181 if (RUNNING_ON_VALGRIND) {
2182 summary_clear_list(mainwin->summaryview);
2185 if (folderview_get_selected_item(mainwin->folderview))
2186 folder_item_close(folderview_get_selected_item(mainwin->folderview));
2190 gboolean claws_is_exiting(void)
2195 gboolean claws_is_starting(void)
2202 * CLAWS: want this public so crash dialog can delete the
2205 gchar *claws_get_socket_name(void)
2207 static gchar *filename = NULL;
2208 gchar *socket_dir = NULL;
2211 if (filename == NULL) {
2215 socket_dir = g_strdup_printf("%s%cclaws-mail-%d",
2216 g_get_tmp_dir(), G_DIR_SEPARATOR,
2222 stat_ok = g_stat(socket_dir, &st);
2223 if (stat_ok < 0 && errno != ENOENT) {
2224 g_print("Error stat'ing socket_dir %s: %s\n",
2225 socket_dir, g_strerror(errno));
2226 } else if (stat_ok == 0 && S_ISSOCK(st.st_mode)) {
2227 /* old versions used a sock in $TMPDIR/claws-mail-$UID */
2228 debug_print("Using legacy socket %s\n", socket_dir);
2229 filename = g_strdup(socket_dir);
2233 if (!is_dir_exist(socket_dir) && make_dir(socket_dir) < 0) {
2234 g_print("Error creating socket_dir %s: %s\n",
2235 socket_dir, g_strerror(errno));
2238 md5_hex_digest(md5sum, get_rc_dir());
2240 filename = g_strdup_printf("%s%c%s", socket_dir, G_DIR_SEPARATOR,
2243 debug_print("Using control socket %s\n", filename);
2250 static gchar *get_crashfile_name(void)
2252 static gchar *filename = NULL;
2254 if (filename == NULL) {
2255 filename = g_strdup_printf("%s%cclaws-crashed",
2256 get_tmp_dir(), G_DIR_SEPARATOR);
2262 static gint prohibit_duplicate_launch(void)
2269 path = claws_get_socket_name();
2270 /* Try to connect to the control socket */
2271 uxsock = fd_connect_unix(path);
2273 if (x_display == NULL)
2274 x_display = g_strdup(g_getenv("DISPLAY"));
2281 /* If connect failed, no other process is running.
2282 * Unlink the potentially existing socket, then
2283 * open it. This has to be done locking a temporary
2284 * file to avoid the race condition where another
2285 * process could have created the socket just in
2288 socket_lock = g_strconcat(path, ".lock",
2290 lock_fd = g_open(socket_lock, O_RDWR|O_CREAT, 0);
2292 debug_print("Couldn't open %s: %s (%d)\n", socket_lock,
2293 g_strerror(errno), errno);
2294 g_free(socket_lock);
2297 if (flock(lock_fd, LOCK_EX) < 0) {
2298 debug_print("Couldn't lock %s: %s (%d)\n", socket_lock,
2299 g_strerror(errno), errno);
2301 g_free(socket_lock);
2307 debug_print("Opening socket %s\n", path);
2308 ret = fd_open_unix(path);
2310 flock(lock_fd, LOCK_UN);
2312 claws_unlink(socket_lock);
2313 g_free(socket_lock);
2320 hmutex = CreateMutexA(NULL, FALSE, "ClawsMail");
2322 debug_print("cannot create Mutex\n");
2325 if (GetLastError() != ERROR_ALREADY_EXISTS) {
2326 uxsock = fd_open_inet(50216);
2332 uxsock = fd_connect_inet(50216);
2336 /* remote command mode */
2338 debug_print("another Claws Mail instance is already running.\n");
2340 if (cmd.receive_all) {
2341 fd_write_all(uxsock, "receive_all\n", 12);
2342 } else if (cmd.receive) {
2343 fd_write_all(uxsock, "receive\n", 8);
2344 } else if (cmd.cancel_receiving) {
2345 fd_write_all(uxsock, "cancel_receiving\n", 17);
2346 } else if (cmd.cancel_sending) {
2347 fd_write_all(uxsock, "cancel_sending\n", 15);
2348 } else if (cmd.compose && cmd.attach_files) {
2349 gchar *str, *compose_str;
2351 if (cmd.compose_mailto) {
2352 compose_str = g_strdup_printf("compose_attach %s\n",
2353 cmd.compose_mailto);
2355 compose_str = g_strdup("compose_attach\n");
2358 fd_write_all(uxsock, compose_str, strlen(compose_str));
2359 g_free(compose_str);
2361 for (curr = cmd.attach_files; curr != NULL ; curr = curr->next) {
2362 str = (gchar *) ((AttachInfo *)curr->data)->file;
2363 if (((AttachInfo *)curr->data)->insert)
2364 fd_write_all(uxsock, "insert ", strlen("insert "));
2366 fd_write_all(uxsock, "attach ", strlen("attach "));
2367 fd_write_all(uxsock, str, strlen(str));
2368 fd_write_all(uxsock, "\n", 1);
2371 fd_write_all(uxsock, ".\n", 2);
2372 } else if (cmd.compose) {
2375 if (cmd.compose_mailto) {
2376 compose_str = g_strdup_printf
2377 ("compose %s\n", cmd.compose_mailto);
2379 compose_str = g_strdup("compose\n");
2382 fd_write_all(uxsock, compose_str, strlen(compose_str));
2383 g_free(compose_str);
2384 } else if (cmd.subscribe) {
2385 gchar *str = g_strdup_printf("subscribe %s\n", cmd.subscribe_uri);
2386 fd_write_all(uxsock, str, strlen(str));
2388 } else if (cmd.send) {
2389 fd_write_all(uxsock, "send\n", 5);
2390 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
2391 fd_write(uxsock, "online\n", 6);
2392 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
2393 fd_write(uxsock, "offline\n", 7);
2394 } else if (cmd.debug) {
2395 fd_write(uxsock, "debug\n", 7);
2396 } else if (cmd.status || cmd.status_full) {
2397 gchar buf[BUFFSIZE];
2399 const gchar *command;
2403 command = cmd.status_full ? "status-full\n" : "status\n";
2404 folders = cmd.status_full ? cmd.status_full_folders :
2407 fd_write_all(uxsock, command, strlen(command));
2408 for (i = 0; folders && i < folders->len; ++i) {
2409 folder = g_ptr_array_index(folders, i);
2410 fd_write_all(uxsock, folder, strlen(folder));
2411 fd_write_all(uxsock, "\n", 1);
2413 fd_write_all(uxsock, ".\n", 2);
2415 fd_gets(uxsock, buf, sizeof(buf) - 1);
2416 buf[sizeof(buf) - 1] = '\0';
2417 if (!strncmp(buf, ".\n", 2)) break;
2418 claws_fputs(buf, stdout);
2420 } else if (cmd.exit) {
2421 fd_write_all(uxsock, "exit\n", 5);
2422 } else if (cmd.statistics) {
2424 fd_write(uxsock, "statistics\n", 11);
2426 fd_gets(uxsock, buf, sizeof(buf) - 1);
2427 buf[sizeof(buf) - 1] = '\0';
2428 if (!strncmp(buf, ".\n", 2)) break;
2429 claws_fputs(buf, stdout);
2431 } else if (cmd.reset_statistics) {
2432 fd_write(uxsock, "reset_statistics\n", 17);
2433 } else if (cmd.target) {
2434 gchar *str = g_strdup_printf("select %s\n", cmd.target);
2435 fd_write_all(uxsock, str, strlen(str));
2437 } else if (cmd.search) {
2438 gchar buf[BUFFSIZE];
2440 g_strdup_printf("search %s\n%s\n%s\n%c\n",
2441 cmd.search_folder, cmd.search_type, cmd.search_request,
2442 (cmd.search_recursive==TRUE)?'1':'0');
2443 fd_write_all(uxsock, str, strlen(str));
2446 fd_gets(uxsock, buf, sizeof(buf) - 1);
2447 buf[sizeof(buf) - 1] = '\0';
2448 if (!strncmp(buf, ".\n", 2)) break;
2449 claws_fputs(buf, stdout);
2454 fd_write_all(uxsock, "get_display\n", 12);
2455 memset(buf, 0, sizeof(buf));
2456 fd_gets(uxsock, buf, sizeof(buf) - 1);
2457 buf[sizeof(buf) - 1] = '\0';
2458 if (strcmp2(buf, x_display)) {
2459 g_print("Claws Mail is already running on display %s.\n",
2463 uxsock = fd_connect_unix(path);
2464 fd_write_all(uxsock, "popup\n", 6);
2467 fd_write_all(uxsock, "popup\n", 6);
2475 static gint lock_socket_remove(void)
2478 gchar *filename, *dirname;
2480 if (lock_socket < 0) {
2484 if (lock_socket_tag > 0) {
2485 g_source_remove(lock_socket_tag);
2487 fd_close(lock_socket);
2490 filename = claws_get_socket_name();
2491 dirname = g_path_get_dirname(filename);
2492 claws_unlink(filename);
2500 static GPtrArray *get_folder_item_list(gint sock)
2502 gchar buf[BUFFSIZE];
2504 GPtrArray *folders = NULL;
2507 fd_gets(sock, buf, sizeof(buf) - 1);
2508 buf[sizeof(buf) - 1] = '\0';
2509 if (!strncmp(buf, ".\n", 2)) {
2514 folders = g_ptr_array_new();
2516 item = folder_find_item_from_identifier(buf);
2518 g_ptr_array_add(folders, item);
2520 g_warning("no such folder: %s", buf);
2527 static void lock_socket_input_cb(gpointer data,
2529 GIOCondition condition)
2531 MainWindow *mainwin = (MainWindow *)data;
2533 gchar buf[BUFFSIZE];
2535 sock = fd_accept(source);
2539 fd_gets(sock, buf, sizeof(buf) - 1);
2540 buf[sizeof(buf) - 1] = '\0';
2542 if (!strncmp(buf, "popup", 5)) {
2543 main_window_popup(mainwin);
2544 } else if (!strncmp(buf, "get_display", 11)) {
2545 fd_write_all(sock, x_display, strlen(x_display));
2546 } else if (!strncmp(buf, "receive_all", 11)) {
2547 inc_all_account_mail(mainwin, FALSE,
2548 prefs_common.newmail_notify_manu);
2549 } else if (!strncmp(buf, "receive", 7)) {
2550 inc_mail(mainwin, prefs_common.newmail_notify_manu);
2551 } else if (!strncmp(buf, "cancel_receiving", 16)) {
2554 } else if (!strncmp(buf, "cancel_sending", 14)) {
2556 } else if (!strncmp(buf, "compose_attach", 14)) {
2557 GList *files = NULL, *curr;
2561 mailto = g_strdup(buf + strlen("compose_attach") + 1);
2562 while (fd_gets(sock, buf, sizeof(buf) - 1) > 0) {
2563 buf[sizeof(buf) - 1] = '\0';
2565 if (!strcmp2(buf, "."))
2568 ainfo = g_new0(AttachInfo, 1);
2569 ainfo->file = g_strdup(strstr(buf, " ") + 1);
2570 ainfo->insert = !strncmp(buf, "insert ", 7);
2571 files = g_list_append(files, ainfo);
2573 open_compose_new(mailto, files);
2575 curr = g_list_first(files);
2576 while (curr != NULL) {
2577 ainfo = (AttachInfo *)curr->data;
2578 g_free(ainfo->file);
2584 } else if (!strncmp(buf, "compose", 7)) {
2585 open_compose_new(buf + strlen("compose") + 1, NULL);
2586 } else if (!strncmp(buf, "subscribe", 9)) {
2587 main_window_popup(mainwin);
2588 folder_subscribe(buf + strlen("subscribe") + 1);
2589 } else if (!strncmp(buf, "send", 4)) {
2591 } else if (!strncmp(buf, "online", 6)) {
2592 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
2593 } else if (!strncmp(buf, "offline", 7)) {
2594 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
2595 } else if (!strncmp(buf, "debug", 5)) {
2596 debug_set_mode(debug_get_mode() ? FALSE : TRUE);
2597 } else if (!strncmp(buf, "status-full", 11) ||
2598 !strncmp(buf, "status", 6)) {
2602 folders = get_folder_item_list(sock);
2603 status = folder_get_status
2604 (folders, !strncmp(buf, "status-full", 11));
2605 fd_write_all(sock, status, strlen(status));
2606 fd_write_all(sock, ".\n", 2);
2608 if (folders) g_ptr_array_free(folders, TRUE);
2609 } else if (!strncmp(buf, "statistics", 10)) {
2612 g_snprintf(tmp, sizeof(tmp), _("Session statistics\n"));
2613 fd_write_all(sock, tmp, strlen(tmp));
2615 if (prefs_common.date_format) {
2620 lt = localtime(&session_stats.time_started);
2621 fast_strftime(date, len, prefs_common.date_format, lt);
2622 g_snprintf(tmp, sizeof(tmp), _("Started: %s\n"),
2623 lt ? date : ctime(&session_stats.time_started));
2625 g_snprintf(tmp, sizeof(tmp), _("Started: %s\n"),
2626 ctime(&session_stats.time_started));
2627 fd_write_all(sock, tmp, strlen(tmp));
2629 fd_write_all(sock, "\n", 1);
2631 g_snprintf(tmp, sizeof(tmp), _("Incoming traffic\n"));
2632 fd_write_all(sock, tmp, strlen(tmp));
2634 g_snprintf(tmp, sizeof(tmp), _("Received messages: %d\n"),
2635 session_stats.received);
2636 fd_write_all(sock, tmp, strlen(tmp));
2638 fd_write_all(sock, "\n", 1);
2640 g_snprintf(tmp, sizeof(tmp), _("Outgoing traffic\n"));
2641 fd_write_all(sock, tmp, strlen(tmp));
2643 g_snprintf(tmp, sizeof(tmp), _("New/redirected messages: %d\n"),
2644 session_stats.sent);
2645 fd_write_all(sock, tmp, strlen(tmp));
2647 g_snprintf(tmp, sizeof(tmp), _("Replied messages: %d\n"),
2648 session_stats.replied);
2649 fd_write_all(sock, tmp, strlen(tmp));
2651 g_snprintf(tmp, sizeof(tmp), _("Forwarded messages: %d\n"),
2652 session_stats.forwarded);
2653 fd_write_all(sock, tmp, strlen(tmp));
2655 g_snprintf(tmp, sizeof(tmp), _("Total outgoing messages: %d\n"),
2656 (session_stats.sent + session_stats.replied +
2657 session_stats.forwarded));
2658 fd_write_all(sock, tmp, strlen(tmp));
2660 fd_write_all(sock, ".\n", 2);
2661 } else if (!strncmp(buf, "reset_statistics", 16)) {
2663 } else if (!strncmp(buf, "select ", 7)) {
2664 const gchar *target = buf+7;
2665 mainwindow_jump_to(target, TRUE);
2666 } else if (!strncmp(buf, "search ", 7)) {
2667 FolderItem* folderItem = NULL;
2668 GSList *messages = NULL;
2669 gchar *folder_name = NULL;
2670 gchar *request = NULL;
2671 AdvancedSearch *search;
2673 AdvancedSearchType searchType = ADVANCED_SEARCH_EXTENDED;
2675 search = advsearch_new();
2677 folder_name = g_strdup(buf+7);
2678 strretchomp(folder_name);
2680 if (fd_gets(sock, buf, sizeof(buf) - 1) <= 0)
2682 buf[sizeof(buf) - 1] = '\0';
2684 switch (toupper(buf[0])) {
2685 case 'S': searchType = ADVANCED_SEARCH_SUBJECT; break;
2686 case 'F': searchType = ADVANCED_SEARCH_FROM; break;
2687 case 'T': searchType = ADVANCED_SEARCH_TO; break;
2688 case 'M': searchType = ADVANCED_SEARCH_MIXED; break;
2689 case 'G': searchType = ADVANCED_SEARCH_TAG; break;
2690 case 'E': searchType = ADVANCED_SEARCH_EXTENDED; break;
2693 if (fd_gets(sock, buf, sizeof(buf) - 1) <= 0)
2696 buf[sizeof(buf) - 1] = '\0';
2697 request = g_strdup(buf);
2698 strretchomp(request);
2701 if (fd_gets(sock, buf, sizeof(buf) - 1) > 0)
2702 recursive = buf[0] != '0';
2704 buf[sizeof(buf) - 1] = '\0';
2706 debug_print("search: %s %i %s %i\n", folder_name, searchType, request, recursive);
2708 folderItem = folder_find_item_from_identifier(folder_name);
2710 if (folderItem == NULL) {
2711 debug_print("Unknown folder item : '%s', searching folder\n",folder_name);
2712 Folder* folder = folder_find_from_path(folder_name);
2714 folderItem = FOLDER_ITEM(folder->node->data);
2716 debug_print("Unknown folder: '%s'\n",folder_name);
2718 debug_print("%s %s\n",folderItem->name, folderItem->path);
2721 if (folderItem != NULL) {
2722 advsearch_set(search, searchType, request);
2723 advsearch_search_msgs_in_folders(search, &messages, folderItem, recursive);
2725 g_print("Folder '%s' not found.\n'", folder_name);
2729 for (cur = messages; cur != NULL; cur = cur->next) {
2730 MsgInfo* msg = (MsgInfo *)cur->data;
2731 gchar *file = procmsg_get_message_file_path(msg);
2732 fd_write_all(sock, file, strlen(file));
2733 fd_write_all(sock, "\n", 1);
2736 fd_write_all(sock, ".\n", 2);
2739 g_free(folder_name);
2741 advsearch_free(search);
2742 if (messages != NULL)
2743 procmsg_msg_list_free(messages);
2744 } else if (!strncmp(buf, "exit", 4)) {
2745 if (prefs_common.clean_on_exit && !prefs_common.ask_on_clean) {
2746 procmsg_empty_all_trash();
2748 app_will_exit(NULL, mainwin);
2754 static void open_compose_new(const gchar *address, GList *attach_files)
2759 Xstrdup_a(addr, address, return);
2763 compose_new(NULL, addr, attach_files);
2766 static void send_queue(void)
2769 gchar *errstr = NULL;
2770 gboolean error = FALSE;
2771 for (list = folder_get_list(); list != NULL; list = list->next) {
2772 Folder *folder = list->data;
2774 if (folder->queue) {
2775 gint res = procmsg_send_queue
2776 (folder->queue, prefs_common.savemsg,
2780 folder_item_scan(folder->queue);
2788 alertpanel_error_log(_("Some errors occurred "
2789 "while sending queued messages:\n%s"), errstr);
2792 alertpanel_error_log("Some errors occurred "
2793 "while sending queued messages.");
2797 static void quit_signal_handler(int sig)
2799 debug_print("Quitting on signal %d\n", sig);
2801 g_timeout_add(0, clean_quit, NULL);
2804 static void install_basic_sighandlers()
2808 struct sigaction act;
2813 sigaddset(&mask, SIGTERM);
2816 sigaddset(&mask, SIGINT);
2819 sigaddset(&mask, SIGHUP);
2822 act.sa_handler = quit_signal_handler;
2827 sigaction(SIGTERM, &act, 0);
2830 sigaction(SIGINT, &act, 0);
2833 sigaction(SIGHUP, &act, 0);
2836 sigprocmask(SIG_UNBLOCK, &mask, 0);
2837 #endif /* !G_OS_WIN32 */
2840 #if (defined linux && defined SIGIO)
2841 static int mem_notify_fd = 0;
2843 static gboolean clean_caches(gpointer unused)
2845 if (static_mainwindow && static_mainwindow->lock_count > 0)
2847 debug_print("/dev/mem_notify: callback: Freeing some memory now!\n");
2848 folder_clean_cache_memory_force();
2852 static void memory_signal_handler(int sig)
2854 debug_print("/dev/mem_notify: Kernel says we should free up some memory!\n");
2855 g_timeout_add(10, clean_caches, NULL);
2858 static void install_memory_sighandler()
2861 struct sigaction act;
2864 mem_notify_fd = g_open("/dev/mem_notify", O_RDONLY|O_NONBLOCK, 0);
2865 if (mem_notify_fd == -1) {
2866 debug_print("/dev/mem_notify not available (%s)\n",
2871 fcntl(mem_notify_fd, F_SETOWN, getpid());
2872 flags = fcntl(mem_notify_fd, F_GETFL);
2873 fcntl(mem_notify_fd, flags|FASYNC);
2877 sigaddset(&mask, SIGIO);
2879 act.sa_handler = memory_signal_handler;
2883 sigaction(SIGIO, &act, 0);
2885 sigprocmask(SIG_UNBLOCK, &mask, 0);
2887 debug_print("/dev/mem_notify: installed handler\n");
2889 #endif /* linux && SIGIO */
2891 #ifdef HAVE_NETWORKMANAGER_SUPPORT
2892 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
2895 MainWindow *mainWin;
2898 if (static_mainwindow)
2899 mainWin = static_mainwindow;
2901 mainWin = (MainWindow*)data;
2903 if (!prefs_common.use_networkmanager)
2907 GError *error = NULL;
2910 online = networkmanager_is_online(&error);
2912 if(online && went_offline_nm) {
2913 went_offline_nm = FALSE;
2914 main_window_toggle_work_offline(mainWin, FALSE, FALSE);
2915 debug_print("NetworkManager: Went online\n");
2916 log_message(LOG_PROTOCOL, _("NetworkManager: network is online.\n"));
2919 went_offline_nm = TRUE;
2920 main_window_toggle_work_offline(mainWin, TRUE, FALSE);
2921 debug_print("NetworkManager: Went offline\n");
2922 log_message(LOG_PROTOCOL, _("NetworkManager: network is offline.\n"));
2926 debug_print("Failed to get online information from NetworkManager: %s\n",
2928 g_error_free(error);
2932 debug_print("NetworkManager: Cannot change connection state because "
2933 "main window does not exist\n");
2936 /* Returns true (and sets error appropriately, if given) in case of error */
2937 gboolean networkmanager_is_online(GError **error)
2939 DBusGConnection *connection;
2941 GError *tmp_error = NULL;
2945 if (!prefs_common.use_networkmanager)
2950 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &tmp_error);
2953 /* If calling code doesn't do error checking, at least print some debug */
2954 if((error == NULL) || (*error == NULL))
2955 debug_print("Failed to open connection to system bus: %s\n",
2956 tmp_error->message);
2957 g_propagate_error(error, tmp_error);
2961 proxy = dbus_g_proxy_new_for_name(connection,
2962 "org.freedesktop.NetworkManager",
2963 "/org/freedesktop/NetworkManager",
2964 "org.freedesktop.NetworkManager");
2966 retVal = dbus_g_proxy_call(proxy,"state",&tmp_error, G_TYPE_INVALID,
2967 G_TYPE_UINT, &state, G_TYPE_INVALID);
2970 g_object_unref(proxy);
2972 dbus_g_connection_unref(connection);
2975 /* If calling code doesn't do error checking, at least print some debug */
2976 if((error == NULL) || (*error == NULL))
2977 debug_print("Failed to get state info from NetworkManager: %s\n",
2978 tmp_error->message);
2979 g_propagate_error(error, tmp_error);
2982 return (state == NM_STATE_CONNECTED_LOCAL ||
2983 state == NM_STATE_CONNECTED_SITE ||
2984 state == NM_STATE_CONNECTED_GLOBAL ||
2985 state == NM_STATE_UNKNOWN);