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>
52 #include "file_checker.h"
54 #ifdef HAVE_STARTUP_NOTIFICATION
55 # define SN_API_NOT_YET_FROZEN
56 # include <libsn/sn-launchee.h>
57 # include <gdk/gdkx.h>
61 #include <dbus/dbus-glib.h>
63 #ifdef HAVE_NETWORKMANAGER_SUPPORT
64 #include <NetworkManager.h>
72 #include "mainwindow.h"
73 #include "folderview.h"
74 #include "image_viewer.h"
75 #include "summaryview.h"
76 #include "prefs_common.h"
77 #include "prefs_account.h"
78 #include "prefs_actions.h"
79 #include "prefs_ext_prog.h"
80 #include "prefs_fonts.h"
81 #include "prefs_image_viewer.h"
82 #include "prefs_message.h"
83 #include "prefs_receive.h"
84 #include "prefs_msg_colors.h"
85 #include "prefs_quote.h"
86 #include "prefs_spelling.h"
87 #include "prefs_summaries.h"
88 #include "prefs_themes.h"
89 #include "prefs_other.h"
90 #include "prefs_logging.h"
91 #include "prefs_send.h"
92 #include "prefs_wrapping.h"
93 #include "prefs_compose_writing.h"
94 #include "prefs_display_header.h"
99 #include "send_message.h"
102 #include "manage_window.h"
103 #include "alertpanel.h"
104 #include "statusbar.h"
105 #ifndef USE_NEW_ADDRBOOK
106 #include "addressbook.h"
108 #include "addressbook-dbus.h"
112 #include "folder_item_prefs.h"
115 #include "gtkutils.h"
118 #include "prefs_toolbar.h"
121 #include "imap_gtk.h"
122 #include "news_gtk.h"
127 #include "quicksearch.h"
128 #include "advsearch.h"
131 #include "imap-thread.h"
132 #include "nntp-thread.h"
134 #include "stock_pixmap.h"
145 #ifdef HAVE_NETWORKMANAGER_SUPPORT
146 /* Went offline due to NetworkManager */
147 static gboolean went_offline_nm;
150 #if !defined(NM_CHECK_VERSION)
151 #define NM_CHECK_VERSION(x,y,z) 0
154 #ifdef HAVE_DBUS_GLIB
155 static DBusGProxy *awn_proxy = NULL;
161 #ifdef HAVE_STARTUP_NOTIFICATION
162 static SnLauncheeContext *sn_context = NULL;
163 static SnDisplay *sn_display = NULL;
166 static gint lock_socket = -1;
167 static gint lock_socket_tag = 0;
168 static gchar *x_display = NULL;
171 ONLINE_MODE_DONT_CHANGE,
176 static struct RemoteCmd {
178 gboolean receive_all;
179 gboolean cancel_receiving;
180 gboolean cancel_sending;
182 const gchar *compose_mailto;
185 const gchar *search_folder;
186 const gchar *search_type;
187 const gchar *search_request;
188 gboolean search_recursive;
190 gboolean status_full;
192 gboolean reset_statistics;
193 GPtrArray *status_folders;
194 GPtrArray *status_full_folders;
201 const gchar *subscribe_uri;
206 SessionStats session_stats;
208 static void reset_statistics(void);
210 static void parse_cmd_opt(int argc, char *argv[]);
212 static gint prohibit_duplicate_launch (void);
213 static gchar * get_crashfile_name (void);
214 static gint lock_socket_remove (void);
215 static void lock_socket_input_cb (gpointer data,
217 GIOCondition condition);
219 static void open_compose_new (const gchar *address,
220 GList *attach_files);
222 static void send_queue (void);
223 static void initial_processing (FolderItem *item, gpointer data);
224 static void quit_signal_handler (int sig);
225 static void install_basic_sighandlers (void);
226 #if (defined linux && defined SIGIO)
227 static void install_memory_sighandler (void);
229 static void exit_claws (MainWindow *mainwin);
231 #ifdef HAVE_NETWORKMANAGER_SUPPORT
232 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
236 #define MAKE_DIR_IF_NOT_EXIST(dir) \
238 if (!is_dir_exist(dir)) { \
239 if (is_file_exist(dir)) { \
241 (_("File '%s' already exists.\n" \
242 "Can't create folder."), \
246 if (make_dir(dir) < 0) \
251 static MainWindow *static_mainwindow;
253 static gboolean emergency_exit = FALSE;
255 #ifdef HAVE_STARTUP_NOTIFICATION
256 static void sn_error_trap_push(SnDisplay *display, Display *xdisplay)
258 gdk_error_trap_push();
261 static void sn_error_trap_pop(SnDisplay *display, Display *xdisplay)
263 gdk_error_trap_pop();
266 static void startup_notification_complete(gboolean with_window)
269 GtkWidget *hack = NULL;
272 /* this is needed to make the startup notification leave,
273 * if we have been launched from a menu.
274 * We have to display a window, so let it be very little */
275 hack = gtk_window_new(GTK_WINDOW_POPUP);
276 gtk_window_move(GTK_WINDOW(hack), 0, 0);
277 gtk_widget_set_size_request(hack, 1, 1);
278 gtk_widget_show(hack);
281 xdisplay = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
282 sn_display = sn_display_new(xdisplay,
285 sn_context = sn_launchee_context_new_from_environment(sn_display,
286 DefaultScreen(xdisplay));
288 if (sn_context != NULL) {
289 sn_launchee_context_complete(sn_context);
290 sn_launchee_context_unref(sn_context);
291 sn_display_unref(sn_display);
294 gtk_widget_destroy(hack);
297 #endif /* HAVE_STARTUP_NOTIFICATION */
299 static void claws_gtk_idle(void)
301 while(gtk_events_pending()) {
302 gtk_main_iteration();
307 static gboolean sc_starting = FALSE;
309 static gboolean defer_check_all(void *data)
311 gboolean autochk = GPOINTER_TO_INT(data);
313 inc_all_account_mail(static_mainwindow, autochk,
314 prefs_common.newmail_notify_manu);
318 main_window_set_menu_sensitive(static_mainwindow);
319 toolbar_main_set_sensitive(static_mainwindow);
324 static gboolean defer_check(void *data)
326 inc_mail(static_mainwindow, prefs_common.newmail_notify_manu);
330 main_window_set_menu_sensitive(static_mainwindow);
331 toolbar_main_set_sensitive(static_mainwindow);
336 static gboolean defer_jump(void *data)
338 if (cmd.receive_all) {
339 defer_check_all(GINT_TO_POINTER(FALSE));
340 } else if (prefs_common.chk_on_startup) {
341 defer_check_all(GINT_TO_POINTER(TRUE));
342 } else if (cmd.receive) {
345 mainwindow_jump_to(data, FALSE);
348 main_window_set_menu_sensitive(static_mainwindow);
349 toolbar_main_set_sensitive(static_mainwindow);
354 static void chk_update_val(GtkWidget *widget, gpointer data)
356 gboolean *val = (gboolean *)data;
357 *val = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
360 static gboolean migrate_old_config(const gchar *old_cfg_dir, const gchar *new_cfg_dir, const gchar *oldversion)
362 gchar *message = g_strdup_printf(_("Configuration for %s found.\n"
363 "Do you want to migrate this configuration?"), oldversion);
364 gchar *message2 = g_strdup_printf(_("\n\nYour Sylpheed filtering rules can be converted by a\n"
365 "script available at %s."), TOOLS_URI);
367 if (!strcmp(oldversion, "Sylpheed"))
368 message = g_strconcat(message, message2, NULL);
372 GtkWidget *window = NULL;
373 GtkWidget *keep_backup_chk;
374 gboolean backup = TRUE;
376 keep_backup_chk = gtk_check_button_new_with_label (_("Keep old configuration"));
377 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_backup_chk), TRUE);
378 CLAWS_SET_TIP(keep_backup_chk,
379 _("Keeping a backup will allow you to go back to an "
380 "older version, but may take a while if you have "
381 "cached IMAP or News data, and will take some extra "
382 "room on your disk."));
384 g_signal_connect(G_OBJECT(keep_backup_chk), "toggled",
385 G_CALLBACK(chk_update_val), &backup);
387 if (alertpanel_full(_("Migration of configuration"), message,
388 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL, FALSE,
389 keep_backup_chk, ALERT_QUESTION, G_ALERTDEFAULT) != G_ALERTALTERNATE) {
393 /* we can either do a fast migration requiring not any extra disk
394 * space, or a slow one that copies the old configuration and leaves
398 window = label_window_create(_("Copying configuration... This may take a while..."));
401 r = copy_dir(old_cfg_dir, new_cfg_dir);
402 label_window_destroy(window);
404 /* if copy failed, we'll remove the partially copied
407 alertpanel_error(_("Migration failed!"));
408 remove_dir_recursive(new_cfg_dir);
411 /* fast mode failed, but we don't want backup */
412 remove_dir_recursive(old_cfg_dir);
416 window = label_window_create(_("Migrating configuration..."));
419 r = g_rename(old_cfg_dir, new_cfg_dir);
420 label_window_destroy(window);
422 /* if g_rename failed, we'll try to copy */
424 FILE_OP_ERROR(new_cfg_dir, "g_rename failed, trying copy\n");
431 static int migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
434 gchar *plugin_path, *old_plugin_path, *new_plugin_path;
436 gboolean err = FALSE;
438 oldfp = g_fopen(old_rc, "r");
441 newfp = g_fopen(new_rc, "w");
447 plugin_path = g_strdup(get_plugin_dir());
448 new_plugin_path = g_strdup(plugin_path);
450 if (strstr(plugin_path, "/claws-mail/")) {
451 gchar *end = g_strdup(strstr(plugin_path, "/claws-mail/")+strlen("/claws-mail/"));
452 *(strstr(plugin_path, "/claws-mail/")) = '\0';
453 old_plugin_path = g_strconcat(plugin_path, "/sylpheed-claws/", end, NULL);
456 old_plugin_path = g_strdup(new_plugin_path);
458 debug_print("replacing %s with %s\n", old_plugin_path, new_plugin_path);
459 while (fgets(buf, sizeof(buf), oldfp)) {
460 if (strncmp(buf, old_plugin_path, strlen(old_plugin_path))) {
461 err |= (fputs(buf, newfp) == EOF);
463 debug_print("->replacing %s", buf);
464 debug_print(" with %s%s", new_plugin_path, buf+strlen(old_plugin_path));
465 err |= (fputs(new_plugin_path, newfp) == EOF);
466 err |= (fputs(buf+strlen(old_plugin_path), newfp) == EOF);
470 g_free(new_plugin_path);
471 g_free(old_plugin_path);
473 if (fclose(newfp) == EOF)
481 sc_client_set_value (MainWindow *mainwin,
492 prop.num_vals = num_vals;
496 if (mainwin->smc_conn)
497 SmcSetProperties ((SmcConn) mainwin->smc_conn, 1, proplist);
500 static void sc_die_callback (SmcConn smc_conn, SmPointer client_data)
505 static void sc_save_complete_callback(SmcConn smc_conn, SmPointer client_data)
509 static void sc_shutdown_cancelled_callback (SmcConn smc_conn, SmPointer client_data)
511 MainWindow *mainwin = (MainWindow *)client_data;
512 if (mainwin->smc_conn)
513 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
516 static void sc_save_yourself_callback (SmcConn smc_conn,
517 SmPointer client_data,
523 MainWindow *mainwin = (MainWindow *)client_data;
524 if (mainwin->smc_conn)
525 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
528 static IceIOErrorHandler sc_ice_installed_handler;
530 static void sc_ice_io_error_handler (IceConn connection)
532 if (sc_ice_installed_handler)
533 (*sc_ice_installed_handler) (connection);
535 static gboolean sc_process_ice_messages (GIOChannel *source,
536 GIOCondition condition,
539 IceConn connection = (IceConn) data;
540 IceProcessMessagesStatus status;
542 status = IceProcessMessages (connection, NULL, NULL);
544 if (status == IceProcessMessagesIOError) {
545 IcePointer context = IceGetConnectionContext (connection);
547 if (context && G_IS_OBJECT(context)) {
548 guint disconnect_id = g_signal_lookup ("disconnect", G_OBJECT_TYPE (context));
550 if (disconnect_id > 0)
551 g_signal_emit (context, disconnect_id, 0);
553 IceSetShutdownNegotiation (connection, False);
554 IceCloseConnection (connection);
561 static void new_ice_connection (IceConn connection, IcePointer client_data, Bool opening,
562 IcePointer *watch_data)
568 /* Make sure we don't pass on these file descriptors to any
570 fcntl(IceConnectionNumber(connection),F_SETFD,
571 fcntl(IceConnectionNumber(connection),F_GETFD,0) | FD_CLOEXEC);
573 channel = g_io_channel_unix_new (IceConnectionNumber (connection));
574 input_id = g_io_add_watch (channel,
575 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
576 sc_process_ice_messages,
578 g_io_channel_unref (channel);
580 *watch_data = (IcePointer) GUINT_TO_POINTER (input_id);
582 input_id = GPOINTER_TO_UINT ((gpointer) *watch_data);
583 g_source_remove (input_id);
587 static void sc_session_manager_connect(MainWindow *mainwin)
589 static gboolean connected = FALSE;
590 SmcCallbacks callbacks;
592 IceIOErrorHandler default_handler;
599 sc_ice_installed_handler = IceSetIOErrorHandler (NULL);
600 default_handler = IceSetIOErrorHandler (sc_ice_io_error_handler);
602 if (sc_ice_installed_handler == default_handler)
603 sc_ice_installed_handler = NULL;
605 IceAddConnectionWatch (new_ice_connection, NULL);
608 callbacks.save_yourself.callback = sc_save_yourself_callback;
609 callbacks.die.callback = sc_die_callback;
610 callbacks.save_complete.callback = sc_save_complete_callback;
611 callbacks.shutdown_cancelled.callback = sc_shutdown_cancelled_callback;
613 callbacks.save_yourself.client_data =
614 callbacks.die.client_data =
615 callbacks.save_complete.client_data =
616 callbacks.shutdown_cancelled.client_data = (SmPointer) mainwin;
617 if (g_getenv ("SESSION_MANAGER")) {
618 gchar error_string_ret[256] = "";
620 mainwin->smc_conn = (gpointer)
621 SmcOpenConnection (NULL, mainwin,
622 SmProtoMajor, SmProtoMinor,
623 SmcSaveYourselfProcMask | SmcDieProcMask |
624 SmcSaveCompleteProcMask |
625 SmcShutdownCancelledProcMask,
628 256, error_string_ret);
630 if (error_string_ret[0] || mainwin->smc_conn == NULL)
631 g_warning ("While connecting to session manager:\n%s.",
635 vals = g_new (SmPropValue, 1);
636 vals[0].length = strlen(argv0);
637 vals[0].value = argv0;
638 sc_client_set_value (mainwin, SmCloneCommand, SmLISTofARRAY8, 1, vals);
639 sc_client_set_value (mainwin, SmRestartCommand, SmLISTofARRAY8, 1, vals);
640 sc_client_set_value (mainwin, SmProgram, SmARRAY8, 1, vals);
642 vals[0].length = strlen(g_get_user_name()?g_get_user_name():"");
643 vals[0].value = g_strdup(g_get_user_name()?g_get_user_name():"");
644 sc_client_set_value (mainwin, SmUserID, SmARRAY8, 1, vals);
650 static gboolean sc_exiting = FALSE;
651 static gboolean show_at_startup = TRUE;
652 static gboolean claws_crashed_bool = FALSE;
654 gboolean claws_crashed(void) {
655 return claws_crashed_bool;
658 void main_set_show_at_startup(gboolean show)
660 show_at_startup = show;
664 static FILE* win32_debug_fp=NULL;
665 static guint win32_log_handler_app_id;
666 static guint win32_log_handler_glib_id;
667 static guint win32_log_handler_gtk_id;
669 static void win32_print_stdout(const gchar *string)
671 if (win32_debug_fp) {
672 fprintf(win32_debug_fp, "%s", string);
673 fflush(win32_debug_fp);
677 static void win32_print_stderr(const gchar *string)
679 if (win32_debug_fp) {
680 fprintf(win32_debug_fp, "%s", string);
681 fflush(win32_debug_fp);
685 static void win32_log(const gchar *log_domain, GLogLevelFlags log_level, const gchar* message, gpointer user_data)
687 if (win32_debug_fp) {
690 switch(log_level & G_LOG_LEVEL_MASK)
692 case G_LOG_LEVEL_ERROR:
695 case G_LOG_LEVEL_CRITICAL:
698 case G_LOG_LEVEL_WARNING:
701 case G_LOG_LEVEL_MESSAGE:
704 case G_LOG_LEVEL_INFO:
707 case G_LOG_LEVEL_DEBUG:
714 fprintf(win32_debug_fp, "%s: %s: %s", log_domain, type, message);
716 fprintf(win32_debug_fp, "%s: %s", type, message);
717 fflush(win32_debug_fp);
721 static void win32_open_log(void)
723 gchar *logfile = g_strconcat(g_get_tmp_dir(), G_DIR_SEPARATOR_S, "claws-win32.log", NULL);
724 gchar *oldlogfile = g_strconcat(g_get_tmp_dir(), G_DIR_SEPARATOR_S, "claws-win32.log.bak", NULL);
726 if (is_file_exist(logfile)) {
727 if (rename_force(logfile, oldlogfile) < 0)
728 FILE_OP_ERROR(logfile, "rename");
730 win32_debug_fp = g_fopen(logfile, "w");
735 g_set_print_handler(win32_print_stdout);
736 g_set_printerr_handler(win32_print_stdout);
737 win32_log_handler_app_id = g_log_set_handler(NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
738 | G_LOG_FLAG_RECURSION, win32_log, NULL);
739 win32_log_handler_glib_id = g_log_set_handler("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
740 | G_LOG_FLAG_RECURSION, win32_log, NULL);
741 win32_log_handler_gtk_id = g_log_set_handler("Gtk", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
742 | G_LOG_FLAG_RECURSION, win32_log, NULL);
746 static void win32_close_log(void)
750 g_log_remove_handler("", win32_log_handler_app_id);
751 g_log_remove_handler("GLib", win32_log_handler_glib_id);
752 g_log_remove_handler("Gtk", win32_log_handler_gtk_id);
753 fclose(win32_debug_fp);
759 static void main_dump_features_list(gboolean show_debug_only)
760 /* display compiled-in features list */
762 if (show_debug_only && !debug_get_mode())
766 debug_print("runtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
767 gtk_major_version, gtk_minor_version, gtk_micro_version,
768 glib_major_version, glib_minor_version, glib_micro_version);
770 g_print("runtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
771 gtk_major_version, gtk_minor_version, gtk_micro_version,
772 glib_major_version, glib_minor_version, glib_micro_version);
774 debug_print("buildtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
775 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
776 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
778 g_print("buildtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
779 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
780 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
783 debug_print("Compiled-in features:\n");
785 g_print("Compiled-in features:\n");
788 debug_print(" compface\n");
790 g_print(" compface\n");
794 debug_print(" Enchant\n");
796 g_print(" Enchant\n");
800 debug_print(" GnuTLS\n");
802 g_print(" GnuTLS\n");
806 debug_print(" IPv6\n");
812 debug_print(" iconv\n");
818 debug_print(" JPilot\n");
820 g_print(" JPilot\n");
824 debug_print(" LDAP\n");
830 debug_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
832 g_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
836 debug_print(" libSM\n");
840 #if HAVE_NETWORKMANAGER_SUPPORT
842 debug_print(" NetworkManager\n");
844 g_print(" NetworkManager\n");
848 #ifdef HAVE_DBUS_GLIB
849 static guint dbus_item_hook_id = -1;
850 static guint dbus_folder_hook_id = -1;
852 static void uninstall_dbus_status_handler(void)
855 g_object_unref(awn_proxy);
857 if (dbus_item_hook_id != -1)
858 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, dbus_item_hook_id);
859 if (dbus_folder_hook_id != -1)
860 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, dbus_folder_hook_id);
863 static void dbus_update(FolderItem *removed_item)
865 guint new, unread, unreadmarked, marked, total;
866 guint replied, forwarded, locked, ignored, watched;
868 GError *error = NULL;
870 folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total,
871 &replied, &forwarded, &locked, &ignored,
874 total -= removed_item->total_msgs;
875 new -= removed_item->new_msgs;
876 unread -= removed_item->unread_msgs;
880 buf = g_strdup_printf("%d", new);
881 dbus_g_proxy_call(awn_proxy, "SetInfoByName", &error,
882 G_TYPE_STRING, "claws-mail",
884 G_TYPE_INVALID, G_TYPE_INVALID);
888 dbus_g_proxy_call(awn_proxy, "UnsetInfoByName", &error, G_TYPE_STRING,
889 "claws-mail", G_TYPE_INVALID, G_TYPE_INVALID);
892 debug_print("%s\n", error->message);
897 static gboolean dbus_status_update_folder_hook(gpointer source, gpointer data)
899 FolderUpdateData *hookdata;
901 if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
902 dbus_update(hookdata->item);
909 static gboolean dbus_status_update_item_hook(gpointer source, gpointer data)
916 static void install_dbus_status_handler(void)
918 GError *tmp_error = NULL;
919 DBusGConnection *connection = dbus_g_bus_get(DBUS_BUS_SESSION, &tmp_error);
922 /* If calling code doesn't do error checking, at least print some debug */
923 debug_print("Failed to open connection to session bus: %s\n",
925 g_error_free(tmp_error);
928 awn_proxy = dbus_g_proxy_new_for_name(connection,
929 "com.google.code.Awn",
930 "/com/google/code/Awn",
931 "com.google.code.Awn");
932 dbus_item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, dbus_status_update_item_hook, NULL);
933 if (dbus_item_hook_id == -1) {
934 g_warning(_("Failed to register folder item update hook"));
935 uninstall_dbus_status_handler();
939 dbus_folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, dbus_status_update_folder_hook, NULL);
940 if (dbus_folder_hook_id == -1) {
941 g_warning(_("Failed to register folder update hook"));
942 uninstall_dbus_status_handler();
948 static void reset_statistics(void)
950 /* (re-)initialize session statistics */
951 session_stats.received = 0;
952 session_stats.sent = 0;
953 session_stats.replied = 0;
954 session_stats.forwarded = 0;
955 session_stats.time_started = time(NULL);
958 int main(int argc, char *argv[])
960 #ifdef HAVE_DBUS_GLIB
961 DBusGConnection *connection;
964 #ifdef HAVE_NETWORKMANAGER_SUPPORT
965 DBusGProxy *nm_proxy;
969 FolderView *folderview;
971 gboolean crash_file_present = FALSE;
972 gint num_folder_class = 0;
973 gboolean asked_for_migration = FALSE;
974 gboolean start_done = TRUE;
975 GSList *plug_list = NULL;
976 gboolean never_ran = FALSE;
977 gboolean mainwin_shown = FALSE;
979 START_TIMING("startup");
986 if (!claws_init(&argc, &argv)) {
993 prog_version = PROG_VERSION;
994 argv0 = g_strdup(argv[0]);
996 parse_cmd_opt(argc, argv);
1000 /* check and create unix domain socket for remote operation */
1001 lock_socket = prohibit_duplicate_launch();
1002 if (lock_socket < 0) {
1003 #ifdef HAVE_STARTUP_NOTIFICATION
1004 if(gtk_init_check(&argc, &argv))
1005 startup_notification_complete(TRUE);
1010 main_dump_features_list(TRUE);
1011 prefs_prepare_cache();
1015 #if !GTK_CHECK_VERSION(3, 0, 0)
1018 gtk_init(&argc, &argv);
1019 crash_main(cmd.crash_params);
1025 crash_install_handlers();
1027 install_basic_sighandlers();
1028 #if (defined linux && defined SIGIO)
1029 install_memory_sighandler();
1032 if (cmd.status || cmd.status_full || cmd.search ||
1033 cmd.statistics || cmd.reset_statistics ||
1034 cmd.cancel_receiving || cmd.cancel_sending ||
1036 puts("0 Claws Mail not running.");
1037 lock_socket_remove();
1043 #if !GLIB_CHECK_VERSION(2,32,0)
1044 if (!g_thread_supported())
1045 g_thread_init(NULL);
1050 #if !GTK_CHECK_VERSION(3, 0, 0)
1053 gtk_init(&argc, &argv);
1056 gtk_settings_set_string_property(gtk_settings_get_default(),
1062 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1063 went_offline_nm = FALSE;
1066 #ifdef HAVE_DBUS_GLIB
1068 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
1071 debug_print("Failed to open connection to system bus: %s\n", error->message);
1072 g_error_free(error);
1075 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1076 nm_proxy = dbus_g_proxy_new_for_name(connection,
1077 "org.freedesktop.NetworkManager",
1078 "/org/freedesktop/NetworkManager",
1079 "org.freedesktop.NetworkManager");
1081 #if NM_CHECK_VERSION(0,8,992)
1082 dbus_g_proxy_add_signal(nm_proxy, "StateChanged", G_TYPE_UINT, G_TYPE_INVALID);
1083 dbus_g_proxy_connect_signal(nm_proxy, "StateChanged",
1084 G_CALLBACK(networkmanager_state_change_cb),
1087 dbus_g_proxy_add_signal(nm_proxy, "StateChange", G_TYPE_UINT, G_TYPE_INVALID);
1088 dbus_g_proxy_connect_signal(nm_proxy, "StateChange",
1089 G_CALLBACK(networkmanager_state_change_cb),
1094 install_dbus_status_handler();
1098 #if !GTK_CHECK_VERSION(3, 0, 0)
1099 gtk_widget_set_default_colormap(
1100 gdk_screen_get_system_colormap(
1101 gdk_screen_get_default()));
1104 gtkut_create_ui_manager();
1106 /* Create container for all the menus we will be adding */
1107 MENUITEM_ADDUI("/", "Menus", NULL, GTK_UI_MANAGER_MENUBAR);
1109 if (!g_thread_supported()) {
1110 g_error(_("g_thread is not supported by glib.\n"));
1114 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1116 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1119 /* no config dir exists. See if we can migrate an old config. */
1120 if (!is_dir_exist(RC_DIR)) {
1121 prefs_destroy_cache();
1124 /* if one of the old dirs exist, we'll ask if the user
1125 * want to migrates, and r will be TRUE if he said yes
1126 * and migration succeeded, and FALSE otherwise.
1128 if (is_dir_exist(OLD_GTK2_RC_DIR)) {
1129 r = migrate_old_config(OLD_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 2.6.0 (or older)"));
1130 asked_for_migration = TRUE;
1131 } else if (is_dir_exist(OLDER_GTK2_RC_DIR)) {
1132 r = migrate_old_config(OLDER_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.9.15 (or older)"));
1133 asked_for_migration = TRUE;
1134 } else if (is_dir_exist(OLD_GTK1_RC_DIR)) {
1135 r = migrate_old_config(OLD_GTK1_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.0.5 (or older)"));
1136 asked_for_migration = TRUE;
1137 } else if (is_dir_exist(SYLPHEED_RC_DIR)) {
1138 r = migrate_old_config(SYLPHEED_RC_DIR, RC_DIR, "Sylpheed");
1139 asked_for_migration = TRUE;
1142 /* If migration failed or the user didn't want to do it,
1143 * we create a new one (and we'll hit wizard later).
1145 if (r == FALSE && !is_dir_exist(RC_DIR)) {
1147 if (copy_dir(SYSCONFDIR "/skel/.claws-mail", RC_DIR) < 0) {
1149 if (!is_dir_exist(RC_DIR) && make_dir(RC_DIR) < 0) {
1162 if (!is_file_exist(RC_DIR G_DIR_SEPARATOR_S COMMON_RC) &&
1163 is_file_exist(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC)) {
1164 /* post 2.6 name change */
1165 migrate_common_rc(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC,
1166 RC_DIR G_DIR_SEPARATOR_S COMMON_RC);
1170 plugin_load_all("Common");
1172 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
1173 gtk_rc_parse(userrc);
1176 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1177 gtk_accel_map_load (userrc);
1181 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_rc_dir(), 1, win32_close_log(););
1183 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
1186 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
1187 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
1188 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
1189 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
1190 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
1191 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR);
1193 crash_file_present = is_file_exist(get_crashfile_name());
1194 /* remove temporary files */
1195 remove_all_files(get_tmp_dir());
1196 remove_all_files(get_mime_tmp_dir());
1198 if (!cmd.crash && crash_file_present)
1199 claws_crashed_bool = TRUE;
1201 if (is_file_exist("claws.log")) {
1202 if (rename_force("claws.log", "claws.log.bak") < 0)
1203 FILE_OP_ERROR("claws.log", "rename");
1205 set_log_file(LOG_PROTOCOL, "claws.log");
1207 if (is_file_exist("filtering.log")) {
1208 if (rename_force("filtering.log", "filtering.log.bak") < 0)
1209 FILE_OP_ERROR("filtering.log", "rename");
1211 set_log_file(LOG_DEBUG_FILTERING, "filtering.log");
1214 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1216 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1219 folder_system_init();
1220 prefs_common_read_config();
1222 prefs_themes_init();
1224 prefs_ext_prog_init();
1225 prefs_wrapping_init();
1226 prefs_compose_writing_init();
1227 prefs_msg_colors_init();
1228 image_viewer_init();
1229 prefs_image_viewer_init();
1231 prefs_summaries_init();
1232 prefs_message_init();
1234 prefs_logging_init();
1235 prefs_receive_init();
1240 gtkaspell_checkers_init();
1241 prefs_spelling_init();
1245 if(prefs_common.gtk_theme && strcmp(prefs_common.gtk_theme, DEFAULT_W32_GTK_THEME))
1246 gtk_settings_set_string_property(gtk_settings_get_default(),
1248 prefs_common.gtk_theme,
1253 sock_set_io_timeout(prefs_common.io_timeout_secs);
1254 prefs_actions_read_config();
1255 prefs_display_header_read_config();
1256 /* prefs_filtering_read_config(); */
1257 #ifndef USE_NEW_ADDRBOOK
1258 addressbook_read_file();
1260 g_clear_error(&error);
1261 if (! addressbook_start_service(&error)) {
1262 g_warning("%s", error->message);
1263 g_clear_error(&error);
1266 addressbook_install_hooks(&error);
1269 gtkut_widget_init();
1270 stock_pixbuf_gdk(NULL, STOCK_PIXMAP_CLAWS_MAIL_ICON, &icon);
1271 gtk_window_set_default_icon(icon);
1273 folderview_initialize();
1279 mainwin = main_window_create();
1281 if (!check_file_integrity())
1284 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1285 networkmanager_state_change_cb(nm_proxy,NULL,mainwin);
1288 manage_window_focus_in(mainwin->window, NULL, NULL);
1289 folderview = mainwin->folderview;
1291 gtk_cmclist_freeze(GTK_CMCLIST(mainwin->folderview->ctree));
1292 folder_item_update_freeze();
1294 prefs_account_init();
1295 account_read_config_all();
1297 #ifdef HAVE_LIBETPAN
1298 imap_main_init(prefs_common.skip_ssl_cert_check);
1299 imap_main_set_timeout(prefs_common.io_timeout_secs);
1300 nntp_main_init(prefs_common.skip_ssl_cert_check);
1302 /* If we can't read a folder list or don't have accounts,
1303 * it means the configuration's not done. Either this is
1304 * a brand new install, either a failed/refused migration.
1305 * So we'll start the wizard.
1307 if (folder_read_list() < 0) {
1308 prefs_destroy_cache();
1310 /* if run_wizard returns FALSE it's because it's
1311 * been cancelled. We can't do much but exit.
1312 * however, if the user was asked for a migration,
1313 * we remove the newly created directory so that
1314 * he's asked again for migration on next launch.*/
1315 if (!run_wizard(mainwin, TRUE)) {
1316 if (asked_for_migration)
1317 remove_dir_recursive(RC_DIR);
1323 main_window_reflect_prefs_all_now();
1324 folder_write_list();
1328 if (!account_get_list()) {
1329 prefs_destroy_cache();
1330 if (!run_wizard(mainwin, FALSE)) {
1331 if (asked_for_migration)
1332 remove_dir_recursive(RC_DIR);
1338 if(!account_get_list()) {
1339 exit_claws(mainwin);
1349 toolbar_main_set_sensitive(mainwin);
1350 main_window_set_menu_sensitive(mainwin);
1352 /* if crashed, show window early so that the user
1353 * sees what's happening */
1354 if (claws_crashed()) {
1355 main_window_popup(mainwin);
1356 mainwin_shown = TRUE;
1359 account_set_missing_folder();
1360 folder_set_missing_folders();
1361 folderview_set(folderview);
1363 prefs_matcher_read_config();
1364 quicksearch_set_search_strings(mainwin->summaryview->quicksearch);
1366 /* make one all-folder processing before using claws */
1367 main_window_cursor_wait(mainwin);
1368 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
1370 /* if claws crashed, rebuild caches */
1371 if (claws_crashed()) {
1373 debug_print("Claws Mail crashed, checking for new messages in local folders\n");
1374 folder_item_update_thaw();
1375 folderview_check_new(NULL);
1376 folder_clean_cache_memory_force();
1377 folder_item_update_freeze();
1379 /* make the crash-indicator file */
1380 str_write_to_file("foo", get_crashfile_name());
1382 inc_autocheck_timer_init(mainwin);
1384 /* ignore SIGPIPE signal for preventing sudden death of program */
1386 signal(SIGPIPE, SIG_IGN);
1388 if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
1389 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1391 if (cmd.online_mode == ONLINE_MODE_ONLINE) {
1392 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1395 if (cmd.status_folders) {
1396 g_ptr_array_free(cmd.status_folders, TRUE);
1397 cmd.status_folders = NULL;
1399 if (cmd.status_full_folders) {
1400 g_ptr_array_free(cmd.status_full_folders, TRUE);
1401 cmd.status_full_folders = NULL;
1404 claws_register_idle_function(claws_gtk_idle);
1406 prefs_toolbar_init();
1408 num_folder_class = g_list_length(folder_get_list());
1410 plugin_load_all("GTK2");
1412 if (g_list_length(folder_get_list()) != num_folder_class) {
1413 debug_print("new folders loaded, reloading processing rules\n");
1414 prefs_matcher_read_config();
1417 if ((plug_list = plugin_get_unloaded_list()) != NULL) {
1420 gint num_plugins = 0;
1421 for (cur = plug_list; cur; cur = cur->next) {
1422 Plugin *plugin = (Plugin *)cur->data;
1423 gchar *tmp = g_strdup_printf("%s\n%s",
1425 plugin_get_name(plugin));
1430 main_window_cursor_normal(mainwin);
1431 main_window_popup(mainwin);
1432 mainwin_shown = TRUE;
1433 alertpanel_warning(ngettext(
1434 "The following plugin failed to load. "
1435 "Check the Plugins configuration "
1436 "for more information:\n%s",
1437 "The following plugins failed to load. "
1438 "Check the Plugins configuration "
1439 "for more information:\n%s",
1442 main_window_cursor_wait(mainwin);
1444 g_slist_free(plug_list);
1448 prefs_common_write_config();
1449 plugin_load_standard_plugins ();
1451 /* if not crashed, show window now */
1452 if (!mainwin_shown) {
1453 /* apart if something told not to show */
1454 if (show_at_startup)
1455 main_window_popup(mainwin);
1458 if (!folder_have_mailbox()) {
1459 prefs_destroy_cache();
1460 main_window_cursor_normal(mainwin);
1461 if (folder_get_list() != NULL) {
1462 alertpanel_error(_("Claws Mail has detected a configured "
1463 "mailbox, but it is incomplete. It is "
1464 "possibly due to a failing IMAP account. Use "
1465 "\"Rebuild folder tree\" on the mailbox parent "
1466 "folder's context menu to try to fix it."));
1468 alertpanel_error(_("Claws Mail has detected a configured "
1469 "mailbox, but could not load it. It is "
1470 "probably provided by an out-of-date "
1471 "external plugin. Please reinstall the "
1472 "plugin and try again."));
1473 exit_claws(mainwin);
1481 static_mainwindow = mainwin;
1483 #ifdef HAVE_STARTUP_NOTIFICATION
1484 startup_notification_complete(FALSE);
1487 sc_session_manager_connect(mainwin);
1490 folder_item_update_thaw();
1491 gtk_cmclist_thaw(GTK_CMCLIST(mainwin->folderview->ctree));
1492 main_window_cursor_normal(mainwin);
1494 if (!cmd.target && prefs_common.goto_last_folder_on_startup &&
1495 folder_find_item_from_identifier(prefs_common.last_opened_folder) != NULL &&
1497 cmd.target = prefs_common.last_opened_folder;
1500 if (cmd.receive_all && !cmd.target) {
1502 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(FALSE));
1503 } else if (prefs_common.chk_on_startup && !cmd.target) {
1505 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(TRUE));
1506 } else if (cmd.receive && !cmd.target) {
1508 g_timeout_add(1000, defer_check, NULL);
1510 gtk_widget_grab_focus(folderview->ctree);
1513 open_compose_new(cmd.compose_mailto, cmd.attach_files);
1515 if (cmd.attach_files) {
1516 list_free_strings(cmd.attach_files);
1517 g_list_free(cmd.attach_files);
1518 cmd.attach_files = NULL;
1520 if (cmd.subscribe) {
1521 folder_subscribe(cmd.subscribe_uri);
1530 g_timeout_add(500, defer_jump, (gpointer)cmd.target);
1533 prefs_destroy_cache();
1535 compose_reopen_exit_drafts();
1538 sc_starting = FALSE;
1539 main_window_set_menu_sensitive(mainwin);
1540 toolbar_main_set_sensitive(mainwin);
1543 /* register the callback of unix domain socket input */
1544 lock_socket_tag = claws_input_add(lock_socket,
1545 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
1546 lock_socket_input_cb,
1553 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1555 g_object_unref(nm_proxy);
1557 #ifdef HAVE_DBUS_GLIB
1558 uninstall_dbus_status_handler();
1560 dbus_g_connection_unref(connection);
1566 exit_claws(mainwin);
1571 static void save_all_caches(FolderItem *item, gpointer data)
1578 folder_item_close(item);
1581 folder_item_free_cache(item, TRUE);
1584 static void exit_claws(MainWindow *mainwin)
1587 gboolean have_connectivity;
1591 debug_print("shutting down\n");
1592 inc_autocheck_timer_remove();
1594 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1595 if (prefs_common.work_offline && went_offline_nm)
1596 prefs_common.work_offline = FALSE;
1599 /* save prefs for opened folder */
1600 if(mainwin->folderview->opened) {
1603 item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
1604 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
1605 prefs_common.last_opened_folder = folder_item_get_identifier(item);
1608 /* save all state before exiting */
1609 folder_func_to_all_folders(save_all_caches, NULL);
1610 folder_write_list();
1612 main_window_get_size(mainwin);
1613 main_window_get_position(mainwin);
1615 prefs_common_write_config();
1616 account_write_config_all();
1617 #ifndef USE_NEW_ADDRBOOK
1618 addressbook_export_to_file();
1620 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1621 gtk_accel_map_save(filename);
1624 /* delete temporary files */
1625 remove_all_files(get_tmp_dir());
1626 remove_all_files(get_mime_tmp_dir());
1628 close_log_file(LOG_PROTOCOL);
1629 close_log_file(LOG_DEBUG_FILTERING);
1631 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1632 have_connectivity = networkmanager_is_online(NULL);
1634 have_connectivity = TRUE;
1636 #ifdef HAVE_LIBETPAN
1637 imap_main_done(have_connectivity);
1638 nntp_main_done(have_connectivity);
1640 /* delete crashfile */
1642 claws_unlink(get_crashfile_name());
1644 lock_socket_remove();
1647 if (mainwin->smc_conn)
1648 SmcCloseConnection ((SmcConn)mainwin->smc_conn, 0, NULL);
1649 mainwin->smc_conn = NULL;
1652 main_window_destroy_all();
1654 plugin_unload_all("GTK2");
1657 prefs_toolbar_done();
1659 #ifndef USE_NEW_ADDRBOOK
1660 addressbook_destroy();
1662 prefs_themes_done();
1664 prefs_ext_prog_done();
1665 prefs_wrapping_done();
1666 prefs_compose_writing_done();
1667 prefs_msg_colors_done();
1668 prefs_image_viewer_done();
1669 image_viewer_done();
1671 prefs_summaries_done();
1672 prefs_message_done();
1674 prefs_receive_done();
1675 prefs_logging_done();
1679 prefs_spelling_done();
1680 gtkaspell_checkers_quit();
1682 plugin_unload_all("Common");
1686 #define G_STRING_APPEND_ENCODED_URI(gstring,source) \
1688 gchar tmpbuf[BUFFSIZE]; \
1689 encode_uri(tmpbuf, BUFFSIZE, (source)); \
1690 g_string_append((gstring), tmpbuf); \
1693 #define G_PRINT_EXIT(msg) \
1699 static GString * parse_cmd_compose_from_file(const gchar *fn)
1701 GString *headers = g_string_new(NULL);
1702 GString *body = g_string_new(NULL);
1710 if (fn == NULL || *fn == '\0')
1711 G_PRINT_EXIT(_("Missing filename\n"));
1712 isstdin = (*fn == '-' && *(fn + 1) == '\0');
1716 fp = g_fopen(fn, "r");
1718 G_PRINT_EXIT(_("Cannot open filename for reading\n"));
1721 while (fgets(fb, sizeof(fb), fp)) {
1727 while (*h && *h != ':') { ++h; } /* search colon */
1729 G_PRINT_EXIT(_("Malformed header\n"));
1731 while (*v && *v == ' ') { ++v; } /* trim value start */
1733 tmp = g_ascii_strdown(fb, -1); /* get header name */
1734 if (!strcmp(tmp, "to")) {
1736 G_PRINT_EXIT(_("Duplicated 'To:' header\n"));
1739 g_string_append_c(headers, '&');
1740 g_string_append(headers, tmp);
1741 g_string_append_c(headers, '=');
1742 #if GLIB_CHECK_VERSION(2,16,0)
1743 g_string_append_uri_escaped(headers, v, NULL, TRUE);
1745 G_STRING_APPEND_ENCODED_URI(headers, v);
1751 G_PRINT_EXIT(_("Missing required 'To:' header\n"));
1752 g_string_append(body, to);
1754 g_string_append(body, "?body=");
1755 while (fgets(fb, sizeof(fb), fp)) {
1756 #if GLIB_CHECK_VERSION(2,16,0)
1757 g_string_append_uri_escaped(body, fb, NULL, TRUE);
1759 G_STRING_APPEND_ENCODED_URI(body, fb);
1764 /* append the remaining headers */
1765 g_string_append(body, headers->str);
1766 g_string_free(headers, TRUE);
1771 #undef G_STRING_APPEND_ENCODED_URI
1774 static void parse_cmd_opt(int argc, char *argv[])
1779 for (i = 1; i < argc; i++) {
1780 if (!strncmp(argv[i], "--receive-all", 13)) {
1781 cmd.receive_all = TRUE;
1782 } else if (!strncmp(argv[i], "--receive", 9)) {
1784 } else if (!strncmp(argv[i], "--cancel-receiving", 18)) {
1785 cmd.cancel_receiving = TRUE;
1786 } else if (!strncmp(argv[i], "--cancel-sending", 16)) {
1787 cmd.cancel_sending = TRUE;
1788 } else if (!strncmp(argv[i], "--compose-from-file", 19)) {
1789 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1791 GString *mailto = parse_cmd_compose_from_file(p);
1793 cmd.compose_mailto = mailto->str;
1795 } else if (!strncmp(argv[i], "--compose", 9)) {
1796 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1799 cmd.compose_mailto = NULL;
1800 if (p && *p != '\0' && *p != '-') {
1801 if (!strncmp(p, "mailto:", 7)) {
1802 cmd.compose_mailto = p + 7;
1804 cmd.compose_mailto = p;
1808 } else if (!strncmp(argv[i], "--subscribe", 11)) {
1809 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1810 if (p && *p != '\0' && *p != '-') {
1811 cmd.subscribe = TRUE;
1812 cmd.subscribe_uri = p;
1814 } else if (!strncmp(argv[i], "--attach", 8)) {
1815 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1818 while (p && *p != '\0' && *p != '-') {
1819 if ((file = g_filename_from_uri(p, NULL, NULL)) != NULL) {
1820 if (!is_file_exist(file)) {
1825 if (file == NULL && *p != G_DIR_SEPARATOR) {
1826 file = g_strconcat(claws_get_startup_dir(),
1829 } else if (file == NULL) {
1832 ainfo = g_new0(AttachInfo, 1);
1834 cmd.attach_files = g_list_append(cmd.attach_files, ainfo);
1836 p = (i+1 < argc)?argv[i+1]:NULL;
1838 } else if (!strncmp(argv[i], "--send", 6)) {
1840 } else if (!strncmp(argv[i], "--version-full", 14) ||
1841 !strncmp(argv[i], "-V", 2)) {
1842 g_print("Claws Mail version " VERSION "\n");
1843 main_dump_features_list(FALSE);
1845 } else if (!strncmp(argv[i], "--version", 9) ||
1846 !strncmp(argv[i], "-v", 2)) {
1847 g_print("Claws Mail version " VERSION "\n");
1849 } else if (!strncmp(argv[i], "--status-full", 13)) {
1850 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1852 cmd.status_full = TRUE;
1853 while (p && *p != '\0' && *p != '-') {
1854 if (!cmd.status_full_folders) {
1855 cmd.status_full_folders =
1858 g_ptr_array_add(cmd.status_full_folders,
1861 p = (i+1 < argc)?argv[i+1]:NULL;
1863 } else if (!strncmp(argv[i], "--status", 8)) {
1864 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1867 while (p && *p != '\0' && *p != '-') {
1868 if (!cmd.status_folders)
1869 cmd.status_folders = g_ptr_array_new();
1870 g_ptr_array_add(cmd.status_folders,
1873 p = (i+1 < argc)?argv[i+1]:NULL;
1875 } else if (!strncmp(argv[i], "--search", 8)) {
1876 cmd.search_folder = (i+1 < argc)?argv[i+1]:NULL;
1877 cmd.search_type = (i+2 < argc)?argv[i+2]:NULL;
1878 cmd.search_request = (i+3 < argc)?argv[i+3]:NULL;
1879 const char* rec = (i+4 < argc)?argv[i+4]:NULL;
1880 cmd.search_recursive = TRUE;
1881 if (rec && (tolower(*rec)=='n' || tolower(*rec)=='f' || *rec=='0'))
1882 cmd.search_recursive = FALSE;
1883 if (cmd.search_folder && cmd.search_type && cmd.search_request)
1885 } else if (!strncmp(argv[i], "--online", 8)) {
1886 cmd.online_mode = ONLINE_MODE_ONLINE;
1887 } else if (!strncmp(argv[i], "--offline", 9)) {
1888 cmd.online_mode = ONLINE_MODE_OFFLINE;
1889 } else if (!strncmp(argv[i], "--toggle-debug", 14)) {
1891 } else if (!strncmp(argv[i], "--statistics", 12)) {
1892 cmd.statistics = TRUE;
1893 } else if (!strncmp(argv[i], "--reset-statistics", 18)) {
1894 cmd.reset_statistics = TRUE;
1895 } else if (!strncmp(argv[i], "--help", 6) ||
1896 !strncmp(argv[i], "-h", 2)) {
1897 gchar *base = g_path_get_basename(argv[0]);
1898 g_print(_("Usage: %s [OPTION]...\n"), base);
1900 g_print("%s\n", _(" --compose [address] open composition window"));
1901 g_print("%s\n", _(" --compose-from-file file\n"
1902 " open composition window with data from given file;\n"
1903 " use - as file name for reading from standard input;\n"
1904 " content format: headers first (To: required) until an\n"
1905 " empty line, then mail body until end of file."));
1906 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
1907 g_print("%s\n", _(" --attach file1 [file2]...\n"
1908 " open composition window with specified files\n"
1910 g_print("%s\n", _(" --receive receive new messages"));
1911 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
1912 g_print("%s\n", _(" --cancel-receiving cancel receiving of messages"));
1913 g_print("%s\n", _(" --cancel-sending cancel sending of messages"));
1914 g_print("%s\n", _(" --search folder type request [recursive]\n"
1916 " folder ex.: \"#mh/Mailbox/inbox\" or \"Mail\"\n"
1917 " type: s[ubject],f[rom],t[o],e[xtended],m[ixed] or g: tag\n"
1918 " request: search string\n"
1919 " recursive: false if arg. starts with 0, n, N, f or F"));
1921 g_print("%s\n", _(" --send send all queued messages"));
1922 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
1923 g_print("%s\n", _(" --status-full [folder]...\n"
1924 " show the status of each folder"));
1925 g_print("%s\n", _(" --statistics show session statistics"));
1926 g_print("%s\n", _(" --reset-statistics reset session statistics"));
1927 g_print("%s\n", _(" --select folder[/msg] jumps to the specified folder/message\n"
1928 " folder is a folder id like 'folder/sub_folder'"));
1929 g_print("%s\n", _(" --online switch to online mode"));
1930 g_print("%s\n", _(" --offline switch to offline mode"));
1931 g_print("%s\n", _(" --exit --quit -q exit Claws Mail"));
1932 g_print("%s\n", _(" --debug debug mode"));
1933 g_print("%s\n", _(" --toggle-debug toggle debug mode"));
1934 g_print("%s\n", _(" --help -h display this help and exit"));
1935 g_print("%s\n", _(" --version -v output version information and exit"));
1936 g_print("%s\n", _(" --version-full -V output version and built-in features information and exit"));
1937 g_print("%s\n", _(" --config-dir output configuration directory"));
1938 g_print("%s\n", _(" --alternate-config-dir [dir]\n"
1939 " use specified configuration directory"));
1943 } else if (!strncmp(argv[i], "--crash", 7)) {
1945 cmd.crash_params = g_strdup((i+1 < argc)?argv[i+1]:NULL);
1947 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
1948 g_print(RC_DIR "\n");
1950 } else if (!strncmp(argv[i], "--alternate-config-dir", sizeof "--alternate-config-dir" - 1) && i+1 < argc) {
1951 set_rc_dir(argv[i+1]);
1952 } else if (!strncmp(argv[i], "--exit", 6) ||
1953 !strncmp(argv[i], "--quit", 6) ||
1954 !strncmp(argv[i], "-q", 2)) {
1956 } else if (!strncmp(argv[i], "--select", 8) && i+1 < argc) {
1957 cmd.target = argv[i+1];
1958 } else if (i == 1 && argc == 2) {
1959 /* only one parameter. Do something intelligent about it */
1960 if ((strstr(argv[i], "@")||!strncmp(argv[i], "mailto:", 7)) && !strstr(argv[i], "://")) {
1961 const gchar *p = argv[i];
1964 cmd.compose_mailto = NULL;
1965 if (p && *p != '\0' && *p != '-') {
1966 if (!strncmp(p, "mailto:", 7)) {
1967 cmd.compose_mailto = p + 7;
1969 cmd.compose_mailto = p;
1972 } else if (!strncmp(argv[i], "file://", 7)) {
1973 cmd.target = argv[i];
1974 } else if (!strncmp(argv[i], "?attach=file://", strlen("?attach=file://"))) {
1976 cmd.compose_mailto = argv[i];
1977 } else if (strstr(argv[i], "://")) {
1978 const gchar *p = argv[i];
1979 if (p && *p != '\0' && *p != '-') {
1980 cmd.subscribe = TRUE;
1981 cmd.subscribe_uri = p;
1983 } else if (!strcmp(argv[i], "--sync")) {
1985 } else if (is_dir_exist(argv[i]) || is_file_exist(argv[i])) {
1986 cmd.target = argv[i];
1988 g_print(_("Unknown option\n"));
1994 if (cmd.attach_files && cmd.compose == FALSE) {
1996 cmd.compose_mailto = NULL;
2000 static void initial_processing(FolderItem *item, gpointer data)
2002 MainWindow *mainwin = (MainWindow *)data;
2005 cm_return_if_fail(item);
2006 buf = g_strdup_printf(_("Processing (%s)..."),
2009 : _("top level folder"));
2013 if (item->prefs->enable_processing) {
2014 item->processing_pending = TRUE;
2015 folder_item_apply_processing(item);
2016 item->processing_pending = FALSE;
2019 STATUSBAR_POP(mainwin);
2022 static gboolean draft_all_messages(void)
2024 GList *compose_list = NULL;
2026 compose_clear_exit_drafts();
2027 compose_list = compose_get_compose_list();
2028 while (compose_list != NULL) {
2029 Compose *c = (Compose*)compose_list->data;
2030 if (!compose_draft(c, COMPOSE_DRAFT_FOR_EXIT))
2032 compose_list = compose_get_compose_list();
2036 gboolean clean_quit(gpointer data)
2038 static gboolean firstrun = TRUE;
2045 /*!< Good idea to have the main window stored in a
2046 * static variable so we can check that variable
2047 * to see if we're really allowed to do things
2048 * that actually the spawner is supposed to
2049 * do (like: sending mail, composing messages).
2050 * Because, really, if we're the spawnee, and
2051 * we touch GTK stuff, we're hosed. See the
2054 /* FIXME: Use something else to signal that we're
2055 * in the original spawner, and not in a spawned
2057 if (!static_mainwindow) {
2061 draft_all_messages();
2062 emergency_exit = TRUE;
2063 exit_claws(static_mainwindow);
2069 void app_will_exit(GtkWidget *widget, gpointer data)
2071 MainWindow *mainwin = data;
2073 if (gtk_main_level() == 0) {
2074 debug_print("not even started\n");
2077 if (sc_exiting == TRUE) {
2078 debug_print("exit pending\n");
2082 debug_print("exiting\n");
2083 if (compose_get_compose_list()) {
2084 if (!draft_all_messages()) {
2085 main_window_popup(mainwin);
2091 if (prefs_common.warn_queued_on_exit && procmsg_have_queued_mails_fast()) {
2092 if (alertpanel(_("Queued messages"),
2093 _("Some unsent messages are queued. Exit now?"),
2094 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL)
2095 != G_ALERTALTERNATE) {
2096 main_window_popup(mainwin);
2100 manage_window_focus_in(mainwin->window, NULL, NULL);
2104 #ifdef HAVE_VALGRIND
2105 if (RUNNING_ON_VALGRIND) {
2106 summary_clear_list(mainwin->summaryview);
2109 if (folderview_get_selected_item(mainwin->folderview))
2110 folder_item_close(folderview_get_selected_item(mainwin->folderview));
2114 gboolean claws_is_exiting(void)
2119 gboolean claws_is_starting(void)
2126 * CLAWS: want this public so crash dialog can delete the
2129 gchar *claws_get_socket_name(void)
2131 static gchar *filename = NULL;
2132 const gchar *socket_dir = NULL;
2135 if (filename == NULL) {
2137 socket_dir = g_strdup_printf("%s%cclaws-mail-%d",
2138 g_get_tmp_dir(), G_DIR_SEPARATOR,
2144 if (stat(socket_dir, &st) < 0 && errno != ENOENT) {
2145 g_print("Error stat'ing socket_dir %s: %s\n",
2146 socket_dir, strerror(errno));
2147 } else if (S_ISSOCK(st.st_mode)) {
2148 /* old versions used a sock in $TMPDIR/claws-mail-$UID */
2149 debug_print("Using legacy socket %s\n", socket_dir);
2150 filename = g_strdup(socket_dir);
2154 if (!is_dir_exist(socket_dir) && make_dir(socket_dir) < 0) {
2155 g_print("Error creating socket_dir %s: %s\n",
2156 socket_dir, strerror(errno));
2159 md5_hex_digest(md5sum, get_rc_dir());
2161 filename = g_strdup_printf("%s%c%s", socket_dir, G_DIR_SEPARATOR,
2163 debug_print("Using control socket %s\n", filename);
2170 static gchar *get_crashfile_name(void)
2172 static gchar *filename = NULL;
2174 if (filename == NULL) {
2175 filename = g_strdup_printf("%s%cclaws-crashed",
2176 get_tmp_dir(), G_DIR_SEPARATOR);
2182 static gint prohibit_duplicate_launch(void)
2189 path = claws_get_socket_name();
2190 /* Try to connect to the control socket */
2191 uxsock = fd_connect_unix(path);
2193 if (x_display == NULL)
2194 x_display = g_strdup(g_getenv("DISPLAY"));
2201 /* If connect failed, no other process is running.
2202 * Unlink the potentially existing socket, then
2203 * open it. This has to be done locking a temporary
2204 * file to avoid the race condition where another
2205 * process could have created the socket just in
2208 socket_lock = g_strconcat(path, ".lock",
2210 lock_fd = g_open(socket_lock, O_RDWR|O_CREAT, 0);
2212 debug_print("Couldn't open %s: %s (%d)\n", socket_lock,
2213 strerror(errno), errno);
2214 g_free(socket_lock);
2217 if (flock(lock_fd, LOCK_EX) < 0) {
2218 debug_print("Couldn't lock %s: %s (%d)\n", socket_lock,
2219 strerror(errno), errno);
2221 g_free(socket_lock);
2227 debug_print("Opening socket %s\n", path);
2228 ret = fd_open_unix(path);
2230 flock(lock_fd, LOCK_UN);
2231 claws_unlink(socket_lock);
2232 g_free(socket_lock);
2239 hmutex = CreateMutexA(NULL, FALSE, "ClawsMail");
2241 debug_print("cannot create Mutex\n");
2244 if (GetLastError() != ERROR_ALREADY_EXISTS) {
2245 uxsock = fd_open_inet(50216);
2251 uxsock = fd_connect_inet(50216);
2255 /* remote command mode */
2257 debug_print("another Claws Mail instance is already running.\n");
2259 if (cmd.receive_all) {
2260 fd_write_all(uxsock, "receive_all\n", 12);
2261 } else if (cmd.receive) {
2262 fd_write_all(uxsock, "receive\n", 8);
2263 } else if (cmd.cancel_receiving) {
2264 fd_write_all(uxsock, "cancel_receiving\n", 17);
2265 } else if (cmd.cancel_sending) {
2266 fd_write_all(uxsock, "cancel_sending\n", 15);
2267 } else if (cmd.compose && cmd.attach_files) {
2268 gchar *str, *compose_str;
2270 if (cmd.compose_mailto) {
2271 compose_str = g_strdup_printf("compose_attach %s\n",
2272 cmd.compose_mailto);
2274 compose_str = g_strdup("compose_attach\n");
2277 fd_write_all(uxsock, compose_str, strlen(compose_str));
2278 g_free(compose_str);
2280 for (curr = cmd.attach_files; curr != NULL ; curr = curr->next) {
2281 str = (gchar *) ((AttachInfo *)curr->data)->file;
2282 fd_write_all(uxsock, str, strlen(str));
2283 fd_write_all(uxsock, "\n", 1);
2286 fd_write_all(uxsock, ".\n", 2);
2287 } else if (cmd.compose) {
2290 if (cmd.compose_mailto) {
2291 compose_str = g_strdup_printf
2292 ("compose %s\n", cmd.compose_mailto);
2294 compose_str = g_strdup("compose\n");
2297 fd_write_all(uxsock, compose_str, strlen(compose_str));
2298 g_free(compose_str);
2299 } else if (cmd.subscribe) {
2300 gchar *str = g_strdup_printf("subscribe %s\n", cmd.subscribe_uri);
2301 fd_write_all(uxsock, str, strlen(str));
2303 } else if (cmd.send) {
2304 fd_write_all(uxsock, "send\n", 5);
2305 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
2306 fd_write(uxsock, "online\n", 6);
2307 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
2308 fd_write(uxsock, "offline\n", 7);
2309 } else if (cmd.debug) {
2310 fd_write(uxsock, "debug\n", 7);
2311 } else if (cmd.status || cmd.status_full) {
2312 gchar buf[BUFFSIZE];
2314 const gchar *command;
2318 command = cmd.status_full ? "status-full\n" : "status\n";
2319 folders = cmd.status_full ? cmd.status_full_folders :
2322 fd_write_all(uxsock, command, strlen(command));
2323 for (i = 0; folders && i < folders->len; ++i) {
2324 folder = g_ptr_array_index(folders, i);
2325 fd_write_all(uxsock, folder, strlen(folder));
2326 fd_write_all(uxsock, "\n", 1);
2328 fd_write_all(uxsock, ".\n", 2);
2330 fd_gets(uxsock, buf, sizeof(buf));
2331 if (!strncmp(buf, ".\n", 2)) break;
2334 } else if (cmd.exit) {
2335 fd_write_all(uxsock, "exit\n", 5);
2336 } else if (cmd.statistics) {
2338 fd_write(uxsock, "statistics\n", 11);
2340 fd_gets(uxsock, buf, sizeof(buf));
2341 if (!strncmp(buf, ".\n", 2)) break;
2344 } else if (cmd.reset_statistics) {
2345 fd_write(uxsock, "reset_statistics\n", 17);
2346 } else if (cmd.target) {
2347 gchar *str = g_strdup_printf("select %s\n", cmd.target);
2348 fd_write_all(uxsock, str, strlen(str));
2350 } else if (cmd.search) {
2351 gchar buf[BUFFSIZE];
2353 g_strdup_printf("search %s\n%s\n%s\n%c\n",
2354 cmd.search_folder, cmd.search_type, cmd.search_request,
2355 (cmd.search_recursive==TRUE)?'1':'0');
2356 fd_write_all(uxsock, str, strlen(str));
2359 fd_gets(uxsock, buf, sizeof(buf));
2360 if (!strncmp(buf, ".\n", 2)) break;
2366 fd_write_all(uxsock, "get_display\n", 12);
2367 memset(buf, 0, sizeof(buf));
2368 fd_gets(uxsock, buf, sizeof(buf));
2369 if (strcmp2(buf, x_display)) {
2370 g_print("Claws Mail is already running on display %s.\n",
2374 uxsock = fd_connect_unix(path);
2375 fd_write_all(uxsock, "popup\n", 6);
2378 fd_write_all(uxsock, "popup\n", 6);
2386 static gint lock_socket_remove(void)
2389 gchar *filename, *dirname;
2391 if (lock_socket < 0) {
2395 if (lock_socket_tag > 0) {
2396 g_source_remove(lock_socket_tag);
2398 fd_close(lock_socket);
2401 filename = claws_get_socket_name();
2402 dirname = g_path_get_dirname(filename);
2403 claws_unlink(filename);
2411 static GPtrArray *get_folder_item_list(gint sock)
2413 gchar buf[BUFFSIZE];
2415 GPtrArray *folders = NULL;
2418 fd_gets(sock, buf, sizeof(buf));
2419 if (!strncmp(buf, ".\n", 2)) {
2424 folders = g_ptr_array_new();
2426 item = folder_find_item_from_identifier(buf);
2428 g_ptr_array_add(folders, item);
2430 g_warning("no such folder: %s\n", buf);
2437 static void lock_socket_input_cb(gpointer data,
2439 GIOCondition condition)
2441 MainWindow *mainwin = (MainWindow *)data;
2443 gchar buf[BUFFSIZE];
2445 sock = fd_accept(source);
2446 fd_gets(sock, buf, sizeof(buf));
2448 if (!strncmp(buf, "popup", 5)) {
2449 main_window_popup(mainwin);
2450 } else if (!strncmp(buf, "get_display", 11)) {
2451 fd_write_all(sock, x_display, strlen(x_display));
2452 } else if (!strncmp(buf, "receive_all", 11)) {
2453 inc_all_account_mail(mainwin, FALSE,
2454 prefs_common.newmail_notify_manu);
2455 } else if (!strncmp(buf, "receive", 7)) {
2456 inc_mail(mainwin, prefs_common.newmail_notify_manu);
2457 } else if (!strncmp(buf, "cancel_receiving", 16)) {
2460 } else if (!strncmp(buf, "cancel_sending", 14)) {
2462 } else if (!strncmp(buf, "compose_attach", 14)) {
2463 GList *files = NULL, *curr;
2467 mailto = g_strdup(buf + strlen("compose_attach") + 1);
2468 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
2470 if (!strcmp2(buf, "."))
2473 ainfo = g_new0(AttachInfo, 1);
2474 ainfo->file = g_strdup(buf);
2475 files = g_list_append(files, ainfo);
2477 open_compose_new(mailto, files);
2479 curr = g_list_first(files);
2480 while (curr != NULL) {
2481 ainfo = (AttachInfo *)curr->data;
2482 g_free(ainfo->file);
2488 } else if (!strncmp(buf, "compose", 7)) {
2489 open_compose_new(buf + strlen("compose") + 1, NULL);
2490 } else if (!strncmp(buf, "subscribe", 9)) {
2491 main_window_popup(mainwin);
2492 folder_subscribe(buf + strlen("subscribe") + 1);
2493 } else if (!strncmp(buf, "send", 4)) {
2495 } else if (!strncmp(buf, "online", 6)) {
2496 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
2497 } else if (!strncmp(buf, "offline", 7)) {
2498 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
2499 } else if (!strncmp(buf, "debug", 5)) {
2500 debug_set_mode(debug_get_mode() ? FALSE : TRUE);
2501 } else if (!strncmp(buf, "status-full", 11) ||
2502 !strncmp(buf, "status", 6)) {
2506 folders = get_folder_item_list(sock);
2507 status = folder_get_status
2508 (folders, !strncmp(buf, "status-full", 11));
2509 fd_write_all(sock, status, strlen(status));
2510 fd_write_all(sock, ".\n", 2);
2512 if (folders) g_ptr_array_free(folders, TRUE);
2513 } else if (!strncmp(buf, "statistics", 10)) {
2516 g_snprintf(tmp, sizeof(tmp), _("Session statistics\n"));
2517 fd_write_all(sock, tmp, strlen(tmp));
2519 if (prefs_common.date_format) {
2524 lt = localtime(&session_stats.time_started);
2525 fast_strftime(date, len, prefs_common.date_format, lt);
2526 g_snprintf(tmp, sizeof(tmp), _("Started: %s\n"),
2527 lt ? date : ctime(&session_stats.time_started));
2529 g_snprintf(tmp, sizeof(tmp), _("Started: %s\n"),
2530 ctime(&session_stats.time_started));
2531 fd_write_all(sock, tmp, strlen(tmp));
2533 fd_write_all(sock, "\n", 1);
2535 g_snprintf(tmp, sizeof(tmp), _("Incoming traffic\n"));
2536 fd_write_all(sock, tmp, strlen(tmp));
2538 g_snprintf(tmp, sizeof(tmp), _("Received messages: %d\n"),
2539 session_stats.received);
2540 fd_write_all(sock, tmp, strlen(tmp));
2542 fd_write_all(sock, "\n", 1);
2544 g_snprintf(tmp, sizeof(tmp), _("Outgoing traffic\n"));
2545 fd_write_all(sock, tmp, strlen(tmp));
2547 g_snprintf(tmp, sizeof(tmp), _("New/redirected messages: %d\n"),
2548 session_stats.sent);
2549 fd_write_all(sock, tmp, strlen(tmp));
2551 g_snprintf(tmp, sizeof(tmp), _("Replied messages: %d\n"),
2552 session_stats.replied);
2553 fd_write_all(sock, tmp, strlen(tmp));
2555 g_snprintf(tmp, sizeof(tmp), _("Forwarded messages: %d\n"),
2556 session_stats.forwarded);
2557 fd_write_all(sock, tmp, strlen(tmp));
2559 g_snprintf(tmp, sizeof(tmp), _("Total outgoing messages: %d\n"),
2560 (session_stats.sent + session_stats.replied +
2561 session_stats.forwarded));
2562 fd_write_all(sock, tmp, strlen(tmp));
2564 fd_write_all(sock, ".\n", 2);
2565 } else if (!strncmp(buf, "reset_statistics", 16)) {
2567 } else if (!strncmp(buf, "select ", 7)) {
2568 const gchar *target = buf+7;
2569 mainwindow_jump_to(target, TRUE);
2570 } else if (!strncmp(buf, "search ", 7)) {
2571 FolderItem* folderItem = NULL;
2572 GSList *messages = NULL;
2573 gchar *folder_name = NULL;
2574 gchar *request = NULL;
2575 AdvancedSearch *search;
2577 AdvancedSearchType searchType = ADVANCED_SEARCH_EXTENDED;
2579 search = advsearch_new();
2581 folder_name = g_strdup(buf+7);
2582 strretchomp(folder_name);
2584 if (fd_gets(sock, buf, sizeof(buf)) <= 0)
2587 switch (toupper(buf[0])) {
2588 case 'S': searchType = ADVANCED_SEARCH_SUBJECT; break;
2589 case 'F': searchType = ADVANCED_SEARCH_FROM; break;
2590 case 'T': searchType = ADVANCED_SEARCH_TO; break;
2591 case 'M': searchType = ADVANCED_SEARCH_MIXED; break;
2592 case 'G': searchType = ADVANCED_SEARCH_TAG; break;
2593 case 'E': searchType = ADVANCED_SEARCH_EXTENDED; break;
2596 if (fd_gets(sock, buf, sizeof(buf)) <= 0)
2599 request = g_strdup(buf);
2600 strretchomp(request);
2603 if (fd_gets(sock, buf, sizeof(buf)) > 0)
2604 recursive = buf[0] != '0';
2606 debug_print("search: %s %i %s %i\n", folder_name, searchType, request, recursive);
2609 folderItem = folder_find_item_from_identifier(folder_name);
2611 if (folder_name && folderItem == NULL) {
2612 debug_print("Unknown folder item : '%s', searching folder\n",folder_name);
2613 Folder* folder = folder_find_from_path(folder_name);
2615 folderItem = FOLDER_ITEM(folder->node->data);
2617 debug_print("Unknown folder: '%s'\n",folder_name);
2619 debug_print("%s %s\n",folderItem->name, folderItem->path);
2622 if (folderItem != NULL) {
2623 advsearch_set(search, searchType, request);
2624 advsearch_search_msgs_in_folders(search, &messages, folderItem, recursive);
2626 g_print("Folder '%s' not found.\n'", folder_name);
2630 for (cur = messages; cur != NULL; cur = cur->next) {
2631 MsgInfo* msg = (MsgInfo *)cur->data;
2632 gchar *file = procmsg_get_message_file_path(msg);
2633 fd_write_all(sock, file, strlen(file));
2634 fd_write_all(sock, "\n", 1);
2637 fd_write_all(sock, ".\n", 2);
2640 g_free(folder_name);
2642 advsearch_free(search);
2643 if (messages != NULL)
2644 procmsg_msg_list_free(messages);
2645 } else if (!strncmp(buf, "exit", 4)) {
2646 if (prefs_common.clean_on_exit && !prefs_common.ask_on_clean) {
2647 procmsg_empty_all_trash();
2649 app_will_exit(NULL, mainwin);
2655 static void open_compose_new(const gchar *address, GList *attach_files)
2660 Xstrdup_a(addr, address, return);
2664 compose_new(NULL, addr, attach_files);
2667 static void send_queue(void)
2670 gchar *errstr = NULL;
2671 gboolean error = FALSE;
2672 for (list = folder_get_list(); list != NULL; list = list->next) {
2673 Folder *folder = list->data;
2675 if (folder->queue) {
2676 gint res = procmsg_send_queue
2677 (folder->queue, prefs_common.savemsg,
2681 folder_item_scan(folder->queue);
2689 alertpanel_error_log(_("Some errors occurred "
2690 "while sending queued messages:\n%s"), errstr);
2693 alertpanel_error_log("Some errors occurred "
2694 "while sending queued messages.");
2698 static void quit_signal_handler(int sig)
2700 debug_print("Quitting on signal %d\n", sig);
2702 g_timeout_add(0, clean_quit, NULL);
2705 static void install_basic_sighandlers()
2709 struct sigaction act;
2714 sigaddset(&mask, SIGTERM);
2717 sigaddset(&mask, SIGINT);
2720 sigaddset(&mask, SIGHUP);
2723 act.sa_handler = quit_signal_handler;
2728 sigaction(SIGTERM, &act, 0);
2731 sigaction(SIGINT, &act, 0);
2734 sigaction(SIGHUP, &act, 0);
2737 sigprocmask(SIG_UNBLOCK, &mask, 0);
2738 #endif /* !G_OS_WIN32 */
2741 #if (defined linux && defined SIGIO)
2742 static int mem_notify_fd = 0;
2744 static gboolean clean_caches(gpointer unused)
2746 if (static_mainwindow && static_mainwindow->lock_count > 0)
2748 debug_print("/dev/mem_notify: callback: Freeing some memory now!\n");
2749 folder_clean_cache_memory_force();
2753 static void memory_signal_handler(int sig)
2755 debug_print("/dev/mem_notify: Kernel says we should free up some memory!\n");
2756 g_timeout_add(10, clean_caches, NULL);
2759 static void install_memory_sighandler()
2762 struct sigaction act;
2765 mem_notify_fd = g_open("/dev/mem_notify", O_RDONLY|O_NONBLOCK, 0);
2766 if (mem_notify_fd == -1) {
2767 debug_print("/dev/mem_notify not available (%s)\n",
2772 fcntl(mem_notify_fd, F_SETOWN, getpid());
2773 flags = fcntl(mem_notify_fd, F_GETFL);
2774 fcntl(mem_notify_fd, flags|FASYNC);
2778 sigaddset(&mask, SIGIO);
2780 act.sa_handler = memory_signal_handler;
2784 sigaction(SIGIO, &act, 0);
2786 sigprocmask(SIG_UNBLOCK, &mask, 0);
2788 debug_print("/dev/mem_notify: installed handler\n");
2790 #endif /* linux && SIGIO */
2792 #ifdef HAVE_NETWORKMANAGER_SUPPORT
2793 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
2796 MainWindow *mainWin;
2799 if (static_mainwindow)
2800 mainWin = static_mainwindow;
2802 mainWin = (MainWindow*)data;
2804 if (!prefs_common.use_networkmanager)
2812 online = networkmanager_is_online(&error);
2814 if(online && went_offline_nm) {
2815 went_offline_nm = FALSE;
2816 main_window_toggle_work_offline(mainWin, FALSE, FALSE);
2817 debug_print("NetworkManager: Went online\n");
2818 log_message(LOG_PROTOCOL, _("NetworkManager: network is online.\n"));
2821 went_offline_nm = TRUE;
2822 main_window_toggle_work_offline(mainWin, TRUE, FALSE);
2823 debug_print("NetworkManager: Went offline\n");
2824 log_message(LOG_PROTOCOL, _("NetworkManager: network is offline.\n"));
2828 debug_print("Failed to get online information from NetworkManager: %s\n",
2830 g_error_free(error);
2834 debug_print("NetworkManager: Cannot change connection state because "
2835 "main window does not exist\n");
2838 /* Returns true (and sets error appropriately, if given) in case of error */
2839 gboolean networkmanager_is_online(GError **error)
2841 DBusGConnection *connection;
2843 GError *tmp_error = NULL;
2847 if (!prefs_common.use_networkmanager)
2852 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &tmp_error);
2855 /* If calling code doesn't do error checking, at least print some debug */
2856 if((error == NULL) || (*error == NULL))
2857 debug_print("Failed to open connection to system bus: %s\n",
2858 tmp_error->message);
2859 g_propagate_error(error, tmp_error);
2863 proxy = dbus_g_proxy_new_for_name(connection,
2864 "org.freedesktop.NetworkManager",
2865 "/org/freedesktop/NetworkManager",
2866 "org.freedesktop.NetworkManager");
2868 retVal = dbus_g_proxy_call(proxy,"state",&tmp_error, G_TYPE_INVALID,
2869 G_TYPE_UINT, &state, G_TYPE_INVALID);
2872 g_object_unref(proxy);
2874 dbus_g_connection_unref(connection);
2877 /* If calling code doesn't do error checking, at least print some debug */
2878 if((error == NULL) || (*error == NULL))
2879 debug_print("Failed to get state info from NetworkManager: %s\n",
2880 tmp_error->message);
2881 g_propagate_error(error, tmp_error);
2884 #if NM_CHECK_VERSION(0,8,992)
2885 return (state == NM_STATE_CONNECTED_LOCAL ||
2886 state == NM_STATE_CONNECTED_SITE ||
2887 state == NM_STATE_CONNECTED_GLOBAL ||
2888 state == NM_STATE_UNKNOWN);
2890 return (state == NM_STATE_CONNECTED ||
2891 state == NM_STATE_UNKNOWN);