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_logging.h"
94 #include "prefs_send.h"
95 #include "prefs_wrapping.h"
96 #include "prefs_compose_writing.h"
97 #include "prefs_display_header.h"
102 #include "send_message.h"
105 #include "manage_window.h"
106 #include "alertpanel.h"
107 #include "statusbar.h"
108 #ifndef USE_ALT_ADDRBOOK
109 #include "addressbook.h"
111 #include "addressbook-dbus.h"
115 #include "folder_item_prefs.h"
118 #include "gtkutils.h"
121 #include "prefs_toolbar.h"
124 #include "imap_gtk.h"
125 #include "news_gtk.h"
130 #include "quicksearch.h"
131 #include "advsearch.h"
133 #include "passwordstore.h"
136 #include "imap-thread.h"
137 #include "nntp-thread.h"
139 #include "stock_pixmap.h"
150 #ifdef HAVE_NETWORKMANAGER_SUPPORT
151 /* Went offline due to NetworkManager */
152 static gboolean went_offline_nm;
155 #if !defined(NM_CHECK_VERSION)
156 #define NM_CHECK_VERSION(x,y,z) 0
159 #ifdef HAVE_DBUS_GLIB
160 static DBusGProxy *awn_proxy = NULL;
168 #ifdef HAVE_STARTUP_NOTIFICATION
169 static SnLauncheeContext *sn_context = NULL;
170 static SnDisplay *sn_display = NULL;
173 static gint lock_socket = -1;
174 static gint lock_socket_tag = 0;
175 static gchar *x_display = NULL;
178 ONLINE_MODE_DONT_CHANGE,
183 static struct RemoteCmd {
185 gboolean receive_all;
186 gboolean cancel_receiving;
187 gboolean cancel_sending;
189 const gchar *compose_mailto;
192 const gchar *search_folder;
193 const gchar *search_type;
194 const gchar *search_request;
195 gboolean search_recursive;
197 gboolean status_full;
199 gboolean reset_statistics;
200 GPtrArray *status_folders;
201 GPtrArray *status_full_folders;
208 const gchar *subscribe_uri;
211 const gchar *geometry;
214 SessionStats session_stats;
216 static void reset_statistics(void);
218 static void parse_cmd_opt(int argc, char *argv[]);
220 static gint prohibit_duplicate_launch (void);
221 static gchar * get_crashfile_name (void);
222 static gint lock_socket_remove (void);
223 static void lock_socket_input_cb (gpointer data,
225 GIOCondition condition);
227 static void open_compose_new (const gchar *address,
228 GList *attach_files);
230 static void send_queue (void);
231 static void initial_processing (FolderItem *item, gpointer data);
232 static void quit_signal_handler (int sig);
233 static void install_basic_sighandlers (void);
234 #if (defined linux && defined SIGIO)
235 static void install_memory_sighandler (void);
237 static void exit_claws (MainWindow *mainwin);
239 #ifdef HAVE_NETWORKMANAGER_SUPPORT
240 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
244 #define MAKE_DIR_IF_NOT_EXIST(dir) \
246 if (!is_dir_exist(dir)) { \
247 if (is_file_exist(dir)) { \
249 (_("File '%s' already exists.\n" \
250 "Can't create folder."), \
254 if (make_dir(dir) < 0) \
259 static MainWindow *static_mainwindow;
261 static gboolean emergency_exit = FALSE;
263 #ifdef HAVE_STARTUP_NOTIFICATION
264 static void sn_error_trap_push(SnDisplay *display, Display *xdisplay)
266 gdk_error_trap_push();
269 static void sn_error_trap_pop(SnDisplay *display, Display *xdisplay)
271 gdk_error_trap_pop();
274 static void startup_notification_complete(gboolean with_window)
277 GtkWidget *hack = NULL;
280 /* this is needed to make the startup notification leave,
281 * if we have been launched from a menu.
282 * We have to display a window, so let it be very little */
283 hack = gtk_window_new(GTK_WINDOW_POPUP);
284 gtk_window_move(GTK_WINDOW(hack), 0, 0);
285 gtk_widget_set_size_request(hack, 1, 1);
286 gtk_widget_show(hack);
289 xdisplay = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
290 sn_display = sn_display_new(xdisplay,
293 sn_context = sn_launchee_context_new_from_environment(sn_display,
294 DefaultScreen(xdisplay));
296 if (sn_context != NULL) {
297 sn_launchee_context_complete(sn_context);
298 sn_launchee_context_unref(sn_context);
299 sn_display_unref(sn_display);
302 gtk_widget_destroy(hack);
305 #endif /* HAVE_STARTUP_NOTIFICATION */
307 static void claws_gtk_idle(void)
309 while(gtk_events_pending()) {
310 gtk_main_iteration();
315 static gboolean sc_starting = FALSE;
317 static gboolean defer_check_all(void *data)
319 gboolean autochk = GPOINTER_TO_INT(data);
321 inc_all_account_mail(static_mainwindow, autochk,
322 prefs_common.newmail_notify_manu);
326 main_window_set_menu_sensitive(static_mainwindow);
327 toolbar_main_set_sensitive(static_mainwindow);
332 static gboolean defer_check(void *data)
334 inc_mail(static_mainwindow, prefs_common.newmail_notify_manu);
338 main_window_set_menu_sensitive(static_mainwindow);
339 toolbar_main_set_sensitive(static_mainwindow);
344 static gboolean defer_jump(void *data)
346 if (cmd.receive_all) {
347 defer_check_all(GINT_TO_POINTER(FALSE));
348 } else if (prefs_common.chk_on_startup) {
349 defer_check_all(GINT_TO_POINTER(TRUE));
350 } else if (cmd.receive) {
353 mainwindow_jump_to(data, FALSE);
356 main_window_set_menu_sensitive(static_mainwindow);
357 toolbar_main_set_sensitive(static_mainwindow);
362 static void chk_update_val(GtkWidget *widget, gpointer data)
364 gboolean *val = (gboolean *)data;
365 *val = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
368 static gboolean migrate_old_config(const gchar *old_cfg_dir, const gchar *new_cfg_dir, const gchar *oldversion)
370 gchar *message = g_strdup_printf(_("Configuration for %s found.\n"
371 "Do you want to migrate this configuration?"), oldversion);
372 gchar *message2 = g_strdup_printf(_("\n\nYour Sylpheed filtering rules can be converted by a\n"
373 "script available at %s."), TOOLS_URI);
375 if (!strcmp(oldversion, "Sylpheed"))
376 message = g_strconcat(message, message2, NULL);
380 GtkWidget *window = NULL;
381 GtkWidget *keep_backup_chk;
382 gboolean backup = TRUE;
384 keep_backup_chk = gtk_check_button_new_with_label (_("Keep old configuration"));
385 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_backup_chk), TRUE);
386 CLAWS_SET_TIP(keep_backup_chk,
387 _("Keeping a backup will allow you to go back to an "
388 "older version, but may take a while if you have "
389 "cached IMAP or News data, and will take some extra "
390 "room on your disk."));
392 g_signal_connect(G_OBJECT(keep_backup_chk), "toggled",
393 G_CALLBACK(chk_update_val), &backup);
395 if (alertpanel_full(_("Migration of configuration"), message,
396 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL, FALSE,
397 keep_backup_chk, ALERT_QUESTION, G_ALERTDEFAULT) != G_ALERTALTERNATE) {
401 /* we can either do a fast migration requiring not any extra disk
402 * space, or a slow one that copies the old configuration and leaves
406 window = label_window_create(_("Copying configuration... This may take a while..."));
409 r = copy_dir(old_cfg_dir, new_cfg_dir);
410 label_window_destroy(window);
412 /* if copy failed, we'll remove the partially copied
415 alertpanel_error(_("Migration failed!"));
416 remove_dir_recursive(new_cfg_dir);
419 /* fast mode failed, but we don't want backup */
420 remove_dir_recursive(old_cfg_dir);
424 window = label_window_create(_("Migrating configuration..."));
427 r = g_rename(old_cfg_dir, new_cfg_dir);
428 label_window_destroy(window);
430 /* if g_rename failed, we'll try to copy */
432 FILE_OP_ERROR(new_cfg_dir, "g_rename");
433 debug_print("rename failed, trying copy\n");
440 static int migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
443 gchar *plugin_path, *old_plugin_path, *new_plugin_path;
445 gboolean err = FALSE;
447 oldfp = g_fopen(old_rc, "r");
450 newfp = g_fopen(new_rc, "w");
456 plugin_path = g_strdup(get_plugin_dir());
457 new_plugin_path = g_strdup(plugin_path);
459 if (strstr(plugin_path, "/claws-mail/")) {
460 gchar *end = g_strdup(strstr(plugin_path, "/claws-mail/")+strlen("/claws-mail/"));
461 *(strstr(plugin_path, "/claws-mail/")) = '\0';
462 old_plugin_path = g_strconcat(plugin_path, "/sylpheed-claws/", end, NULL);
465 old_plugin_path = g_strdup(new_plugin_path);
467 debug_print("replacing %s with %s\n", old_plugin_path, new_plugin_path);
468 while (fgets(buf, sizeof(buf), oldfp)) {
469 if (strncmp(buf, old_plugin_path, strlen(old_plugin_path))) {
470 err |= (fputs(buf, newfp) == EOF);
472 debug_print("->replacing %s\n", buf);
473 debug_print(" with %s%s\n", new_plugin_path, buf+strlen(old_plugin_path));
474 err |= (fputs(new_plugin_path, newfp) == EOF);
475 err |= (fputs(buf+strlen(old_plugin_path), newfp) == EOF);
479 g_free(new_plugin_path);
480 g_free(old_plugin_path);
482 if (fclose(newfp) == EOF)
490 sc_client_set_value (MainWindow *mainwin,
501 prop.num_vals = num_vals;
505 if (mainwin->smc_conn)
506 SmcSetProperties ((SmcConn) mainwin->smc_conn, 1, proplist);
509 static void sc_die_callback (SmcConn smc_conn, SmPointer client_data)
514 static void sc_save_complete_callback(SmcConn smc_conn, SmPointer client_data)
518 static void sc_shutdown_cancelled_callback (SmcConn smc_conn, SmPointer client_data)
520 MainWindow *mainwin = (MainWindow *)client_data;
521 if (mainwin->smc_conn)
522 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
525 static void sc_save_yourself_callback (SmcConn smc_conn,
526 SmPointer client_data,
532 MainWindow *mainwin = (MainWindow *)client_data;
533 if (mainwin->smc_conn)
534 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
537 static IceIOErrorHandler sc_ice_installed_handler;
539 static void sc_ice_io_error_handler (IceConn connection)
541 if (sc_ice_installed_handler)
542 (*sc_ice_installed_handler) (connection);
544 static gboolean sc_process_ice_messages (GIOChannel *source,
545 GIOCondition condition,
548 IceConn connection = (IceConn) data;
549 IceProcessMessagesStatus status;
551 status = IceProcessMessages (connection, NULL, NULL);
553 if (status == IceProcessMessagesIOError) {
554 IcePointer context = IceGetConnectionContext (connection);
556 if (context && G_IS_OBJECT(context)) {
557 guint disconnect_id = g_signal_lookup ("disconnect", G_OBJECT_TYPE (context));
559 if (disconnect_id > 0)
560 g_signal_emit (context, disconnect_id, 0);
562 IceSetShutdownNegotiation (connection, False);
563 IceCloseConnection (connection);
570 static void new_ice_connection (IceConn connection, IcePointer client_data, Bool opening,
571 IcePointer *watch_data)
577 /* Make sure we don't pass on these file descriptors to any
579 fcntl(IceConnectionNumber(connection),F_SETFD,
580 fcntl(IceConnectionNumber(connection),F_GETFD,0) | FD_CLOEXEC);
582 channel = g_io_channel_unix_new (IceConnectionNumber (connection));
583 input_id = g_io_add_watch (channel,
584 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
585 sc_process_ice_messages,
587 g_io_channel_unref (channel);
589 *watch_data = (IcePointer) GUINT_TO_POINTER (input_id);
591 input_id = GPOINTER_TO_UINT ((gpointer) *watch_data);
592 g_source_remove (input_id);
596 static void sc_session_manager_connect(MainWindow *mainwin)
598 static gboolean connected = FALSE;
599 SmcCallbacks callbacks;
601 IceIOErrorHandler default_handler;
608 sc_ice_installed_handler = IceSetIOErrorHandler (NULL);
609 default_handler = IceSetIOErrorHandler (sc_ice_io_error_handler);
611 if (sc_ice_installed_handler == default_handler)
612 sc_ice_installed_handler = NULL;
614 IceAddConnectionWatch (new_ice_connection, NULL);
617 callbacks.save_yourself.callback = sc_save_yourself_callback;
618 callbacks.die.callback = sc_die_callback;
619 callbacks.save_complete.callback = sc_save_complete_callback;
620 callbacks.shutdown_cancelled.callback = sc_shutdown_cancelled_callback;
622 callbacks.save_yourself.client_data =
623 callbacks.die.client_data =
624 callbacks.save_complete.client_data =
625 callbacks.shutdown_cancelled.client_data = (SmPointer) mainwin;
626 if (g_getenv ("SESSION_MANAGER")) {
627 gchar error_string_ret[256] = "";
629 mainwin->smc_conn = (gpointer)
630 SmcOpenConnection (NULL, mainwin,
631 SmProtoMajor, SmProtoMinor,
632 SmcSaveYourselfProcMask | SmcDieProcMask |
633 SmcSaveCompleteProcMask |
634 SmcShutdownCancelledProcMask,
637 256, error_string_ret);
639 if (error_string_ret[0] || mainwin->smc_conn == NULL)
640 g_warning ("While connecting to session manager: %s.",
644 vals = g_new (SmPropValue, 1);
645 vals[0].length = strlen(argv0);
646 vals[0].value = argv0;
647 sc_client_set_value (mainwin, SmCloneCommand, SmLISTofARRAY8, 1, vals);
648 sc_client_set_value (mainwin, SmRestartCommand, SmLISTofARRAY8, 1, vals);
649 sc_client_set_value (mainwin, SmProgram, SmARRAY8, 1, vals);
651 vals[0].length = strlen(g_get_user_name()?g_get_user_name():"");
652 vals[0].value = g_strdup(g_get_user_name()?g_get_user_name():"");
653 sc_client_set_value (mainwin, SmUserID, SmARRAY8, 1, vals);
655 g_free(vals[0].value);
662 static gboolean sc_exiting = FALSE;
663 static gboolean show_at_startup = TRUE;
664 static gboolean claws_crashed_bool = FALSE;
666 gboolean claws_crashed(void) {
667 return claws_crashed_bool;
670 void main_set_show_at_startup(gboolean show)
672 show_at_startup = show;
676 static FILE* win32_debug_fp=NULL;
677 static guint win32_log_handler_app_id;
678 static guint win32_log_handler_glib_id;
679 static guint win32_log_handler_gtk_id;
681 static void win32_print_stdout(const gchar *string)
683 if (win32_debug_fp) {
684 fprintf(win32_debug_fp, "%s", string);
685 fflush(win32_debug_fp);
689 static void win32_print_stderr(const gchar *string)
691 if (win32_debug_fp) {
692 fprintf(win32_debug_fp, "%s", string);
693 fflush(win32_debug_fp);
697 static void win32_log(const gchar *log_domain, GLogLevelFlags log_level, const gchar* message, gpointer user_data)
699 if (win32_debug_fp) {
702 switch(log_level & G_LOG_LEVEL_MASK)
704 case G_LOG_LEVEL_ERROR:
707 case G_LOG_LEVEL_CRITICAL:
710 case G_LOG_LEVEL_WARNING:
713 case G_LOG_LEVEL_MESSAGE:
716 case G_LOG_LEVEL_INFO:
719 case G_LOG_LEVEL_DEBUG:
726 fprintf(win32_debug_fp, "%s: %s: %s", log_domain, type, message);
728 fprintf(win32_debug_fp, "%s: %s", type, message);
729 fflush(win32_debug_fp);
733 static void win32_open_log(void)
735 gchar *logfile = g_strconcat(g_get_tmp_dir(), G_DIR_SEPARATOR_S, "claws-win32.log", NULL);
736 gchar *oldlogfile = g_strconcat(g_get_tmp_dir(), G_DIR_SEPARATOR_S, "claws-win32.log.bak", NULL);
738 if (is_file_exist(logfile)) {
739 if (rename_force(logfile, oldlogfile) < 0)
740 FILE_OP_ERROR(logfile, "rename");
742 win32_debug_fp = g_fopen(logfile, "w");
747 g_set_print_handler(win32_print_stdout);
748 g_set_printerr_handler(win32_print_stdout);
749 win32_log_handler_app_id = g_log_set_handler(NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
750 | G_LOG_FLAG_RECURSION, win32_log, NULL);
751 win32_log_handler_glib_id = g_log_set_handler("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
752 | G_LOG_FLAG_RECURSION, win32_log, NULL);
753 win32_log_handler_gtk_id = g_log_set_handler("Gtk", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
754 | G_LOG_FLAG_RECURSION, win32_log, NULL);
758 static void win32_close_log(void)
762 g_log_remove_handler("", win32_log_handler_app_id);
763 g_log_remove_handler("GLib", win32_log_handler_glib_id);
764 g_log_remove_handler("Gtk", win32_log_handler_gtk_id);
765 fclose(win32_debug_fp);
771 static void main_dump_features_list(gboolean show_debug_only)
772 /* display compiled-in features list */
774 if (show_debug_only && !debug_get_mode())
778 debug_print("runtime 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);
782 g_print("runtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
783 gtk_major_version, gtk_minor_version, gtk_micro_version,
784 glib_major_version, glib_minor_version, glib_micro_version);
786 debug_print("buildtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
787 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
788 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
790 g_print("buildtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
791 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
792 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
795 debug_print("Compiled-in features:\n");
797 g_print("Compiled-in features:\n");
800 debug_print(" compface\n");
802 g_print(" compface\n");
806 debug_print(" Enchant\n");
808 g_print(" Enchant\n");
812 debug_print(" GnuTLS\n");
814 g_print(" GnuTLS\n");
818 debug_print(" IPv6\n");
824 debug_print(" iconv\n");
830 debug_print(" JPilot\n");
832 g_print(" JPilot\n");
836 debug_print(" LDAP\n");
842 debug_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
844 g_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
848 debug_print(" libSM\n");
852 #if HAVE_NETWORKMANAGER_SUPPORT
854 debug_print(" NetworkManager\n");
856 g_print(" NetworkManager\n");
860 debug_print(" librSVG " LIBRSVG_VERSION "\n");
862 g_print(" librSVG " LIBRSVG_VERSION "\n");
866 #ifdef HAVE_DBUS_GLIB
867 static guint dbus_item_hook_id = -1;
868 static guint dbus_folder_hook_id = -1;
870 static void uninstall_dbus_status_handler(void)
873 g_object_unref(awn_proxy);
875 if (dbus_item_hook_id != -1)
876 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, dbus_item_hook_id);
877 if (dbus_folder_hook_id != -1)
878 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, dbus_folder_hook_id);
881 static void dbus_update(FolderItem *removed_item)
883 guint new, unread, unreadmarked, marked, total;
884 guint replied, forwarded, locked, ignored, watched;
886 GError *error = NULL;
888 folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total,
889 &replied, &forwarded, &locked, &ignored,
892 total -= removed_item->total_msgs;
893 new -= removed_item->new_msgs;
894 unread -= removed_item->unread_msgs;
898 buf = g_strdup_printf("%d", new);
899 dbus_g_proxy_call(awn_proxy, "SetInfoByName", &error,
900 G_TYPE_STRING, "claws-mail",
902 G_TYPE_INVALID, G_TYPE_INVALID);
906 dbus_g_proxy_call(awn_proxy, "UnsetInfoByName", &error, G_TYPE_STRING,
907 "claws-mail", G_TYPE_INVALID, G_TYPE_INVALID);
910 debug_print("%s\n", error->message);
915 static gboolean dbus_status_update_folder_hook(gpointer source, gpointer data)
917 FolderUpdateData *hookdata;
919 if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
920 dbus_update(hookdata->item);
927 static gboolean dbus_status_update_item_hook(gpointer source, gpointer data)
934 static void install_dbus_status_handler(void)
936 GError *tmp_error = NULL;
937 DBusGConnection *connection = dbus_g_bus_get(DBUS_BUS_SESSION, &tmp_error);
940 /* If calling code doesn't do error checking, at least print some debug */
941 debug_print("Failed to open connection to session bus: %s\n",
943 g_error_free(tmp_error);
946 awn_proxy = dbus_g_proxy_new_for_name(connection,
947 "com.google.code.Awn",
948 "/com/google/code/Awn",
949 "com.google.code.Awn");
950 dbus_item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, dbus_status_update_item_hook, NULL);
951 if (dbus_item_hook_id == -1) {
952 g_warning("Failed to register folder item update hook");
953 uninstall_dbus_status_handler();
957 dbus_folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, dbus_status_update_folder_hook, NULL);
958 if (dbus_folder_hook_id == -1) {
959 g_warning("Failed to register folder update hook");
960 uninstall_dbus_status_handler();
966 static void reset_statistics(void)
968 /* (re-)initialize session statistics */
969 session_stats.received = 0;
970 session_stats.sent = 0;
971 session_stats.replied = 0;
972 session_stats.forwarded = 0;
973 session_stats.time_started = time(NULL);
976 int main(int argc, char *argv[])
978 #ifdef HAVE_DBUS_GLIB
979 DBusGConnection *connection;
982 #ifdef HAVE_NETWORKMANAGER_SUPPORT
983 DBusGProxy *nm_proxy;
987 FolderView *folderview;
989 gboolean crash_file_present = FALSE;
990 gint num_folder_class = 0;
991 gboolean asked_for_migration = FALSE;
992 gboolean start_done = TRUE;
993 GSList *plug_list = NULL;
994 gboolean never_ran = FALSE;
995 gboolean mainwin_shown = FALSE;
997 START_TIMING("startup");
1004 if (!claws_init(&argc, &argv)) {
1011 prog_version = PROG_VERSION;
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 #if NM_CHECK_VERSION(0,8,992)
1110 dbus_g_proxy_add_signal(nm_proxy, "StateChanged", G_TYPE_UINT, G_TYPE_INVALID);
1111 dbus_g_proxy_connect_signal(nm_proxy, "StateChanged",
1112 G_CALLBACK(networkmanager_state_change_cb),
1115 dbus_g_proxy_add_signal(nm_proxy, "StateChange", G_TYPE_UINT, G_TYPE_INVALID);
1116 dbus_g_proxy_connect_signal(nm_proxy, "StateChange",
1117 G_CALLBACK(networkmanager_state_change_cb),
1122 install_dbus_status_handler();
1126 #if !GTK_CHECK_VERSION(3, 0, 0)
1127 gtk_widget_set_default_colormap(
1128 gdk_screen_get_system_colormap(
1129 gdk_screen_get_default()));
1132 gtkut_create_ui_manager();
1134 /* Create container for all the menus we will be adding */
1135 MENUITEM_ADDUI("/", "Menus", NULL, GTK_UI_MANAGER_MENUBAR);
1137 if (!g_thread_supported()) {
1138 g_error(_("g_thread is not supported by glib.\n"));
1142 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1144 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1147 /* no config dir exists. See if we can migrate an old config. */
1148 if (!is_dir_exist(get_rc_dir())) {
1149 prefs_destroy_cache();
1152 /* if one of the old dirs exist, we'll ask if the user
1153 * want to migrates, and r will be TRUE if he said yes
1154 * and migration succeeded, and FALSE otherwise.
1156 if (is_dir_exist(OLD_GTK2_RC_DIR)) {
1157 r = migrate_old_config(OLD_GTK2_RC_DIR, get_rc_dir(),
1158 g_strconcat("Sylpheed-Claws 2.6.0 ", _("(or older)"), NULL));
1159 asked_for_migration = TRUE;
1160 } else if (is_dir_exist(OLDER_GTK2_RC_DIR)) {
1161 r = migrate_old_config(OLDER_GTK2_RC_DIR, get_rc_dir(),
1162 g_strconcat("Sylpheed-Claws 1.9.15 ",_("(or older)"), NULL));
1163 asked_for_migration = TRUE;
1164 } else if (is_dir_exist(OLD_GTK1_RC_DIR)) {
1165 r = migrate_old_config(OLD_GTK1_RC_DIR, get_rc_dir(),
1166 g_strconcat("Sylpheed-Claws 1.0.5 ",_("(or older)"), NULL));
1167 asked_for_migration = TRUE;
1168 } else if (is_dir_exist(SYLPHEED_RC_DIR)) {
1169 r = migrate_old_config(SYLPHEED_RC_DIR, get_rc_dir(), "Sylpheed");
1170 asked_for_migration = TRUE;
1173 /* If migration failed or the user didn't want to do it,
1174 * we create a new one (and we'll hit wizard later).
1176 if (r == FALSE && !is_dir_exist(get_rc_dir())) {
1178 if (copy_dir(SYSCONFDIR "/skel/.claws-mail", get_rc_dir()) < 0) {
1180 if (!is_dir_exist(get_rc_dir()) && make_dir(get_rc_dir()) < 0) {
1193 if (!is_file_exist(RC_DIR G_DIR_SEPARATOR_S COMMON_RC) &&
1194 is_file_exist(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC)) {
1195 /* post 2.6 name change */
1196 migrate_common_rc(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC,
1197 RC_DIR G_DIR_SEPARATOR_S COMMON_RC);
1201 plugin_load_all("Common");
1203 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
1204 gtk_rc_parse(userrc);
1207 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1208 gtk_accel_map_load (userrc);
1212 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_rc_dir(), 1, win32_close_log(););
1214 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
1217 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
1218 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
1219 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
1220 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
1221 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
1222 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR);
1224 crash_file_present = is_file_exist(get_crashfile_name());
1225 /* remove temporary files */
1226 remove_all_files(get_tmp_dir());
1227 remove_all_files(get_mime_tmp_dir());
1229 if (!cmd.crash && crash_file_present)
1230 claws_crashed_bool = TRUE;
1232 if (is_file_exist("claws.log")) {
1233 if (rename_force("claws.log", "claws.log.bak") < 0)
1234 FILE_OP_ERROR("claws.log", "rename");
1236 set_log_file(LOG_PROTOCOL, "claws.log");
1238 if (is_file_exist("filtering.log")) {
1239 if (rename_force("filtering.log", "filtering.log.bak") < 0)
1240 FILE_OP_ERROR("filtering.log", "rename");
1242 set_log_file(LOG_DEBUG_FILTERING, "filtering.log");
1245 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1247 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1250 folder_system_init();
1251 prefs_common_read_config();
1253 prefs_themes_init();
1255 prefs_ext_prog_init();
1256 prefs_wrapping_init();
1257 prefs_compose_writing_init();
1258 prefs_msg_colors_init();
1259 image_viewer_init();
1260 prefs_image_viewer_init();
1262 prefs_summaries_init();
1263 prefs_message_init();
1265 prefs_logging_init();
1266 prefs_receive_init();
1271 gtkaspell_checkers_init();
1272 prefs_spelling_init();
1276 if(prefs_common.gtk_theme && strcmp(prefs_common.gtk_theme, DEFAULT_W32_GTK_THEME))
1277 gtk_settings_set_string_property(gtk_settings_get_default(),
1279 prefs_common.gtk_theme,
1284 sock_set_io_timeout(prefs_common.io_timeout_secs);
1285 prefs_actions_read_config();
1286 prefs_display_header_read_config();
1287 /* prefs_filtering_read_config(); */
1288 #ifndef USE_ALT_ADDRBOOK
1289 addressbook_read_file();
1291 g_clear_error(&error);
1292 if (! addressbook_start_service(&error)) {
1293 g_warning("%s", error->message);
1294 g_clear_error(&error);
1297 addressbook_install_hooks(&error);
1300 gtkut_widget_init();
1301 stock_pixbuf_gdk(STOCK_PIXMAP_CLAWS_MAIL_ICON, &icon);
1302 gtk_window_set_default_icon(icon);
1304 folderview_initialize();
1310 mainwin = main_window_create();
1312 if (!check_file_integrity())
1315 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1316 networkmanager_state_change_cb(nm_proxy,NULL,mainwin);
1319 manage_window_focus_in(mainwin->window, NULL, NULL);
1320 folderview = mainwin->folderview;
1322 folderview_freeze(mainwin->folderview);
1323 folder_item_update_freeze();
1325 passwd_store_read_config();
1326 prefs_account_init();
1327 account_read_config_all();
1329 #ifdef HAVE_LIBETPAN
1330 imap_main_init(prefs_common.skip_ssl_cert_check);
1331 imap_main_set_timeout(prefs_common.io_timeout_secs);
1332 nntp_main_init(prefs_common.skip_ssl_cert_check);
1334 /* If we can't read a folder list or don't have accounts,
1335 * it means the configuration's not done. Either this is
1336 * a brand new install, either a failed/refused migration.
1337 * So we'll start the wizard.
1339 if (folder_read_list() < 0) {
1340 prefs_destroy_cache();
1342 /* if run_wizard returns FALSE it's because it's
1343 * been cancelled. We can't do much but exit.
1344 * however, if the user was asked for a migration,
1345 * we remove the newly created directory so that
1346 * he's asked again for migration on next launch.*/
1347 if (!run_wizard(mainwin, TRUE)) {
1348 if (asked_for_migration)
1349 remove_dir_recursive(RC_DIR);
1355 main_window_reflect_prefs_all_now();
1356 folder_write_list();
1360 if (!account_get_list()) {
1361 prefs_destroy_cache();
1362 if (!run_wizard(mainwin, FALSE)) {
1363 if (asked_for_migration)
1364 remove_dir_recursive(RC_DIR);
1370 if(!account_get_list()) {
1371 exit_claws(mainwin);
1381 toolbar_main_set_sensitive(mainwin);
1382 main_window_set_menu_sensitive(mainwin);
1384 /* if crashed, show window early so that the user
1385 * sees what's happening */
1386 if (claws_crashed()) {
1387 main_window_popup(mainwin);
1388 mainwin_shown = TRUE;
1391 account_set_missing_folder();
1392 folder_set_missing_folders();
1393 folderview_set(folderview);
1395 prefs_matcher_read_config();
1396 quicksearch_set_search_strings(mainwin->summaryview->quicksearch);
1398 /* make one all-folder processing before using claws */
1399 main_window_cursor_wait(mainwin);
1400 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
1402 /* if claws crashed, rebuild caches */
1403 if (claws_crashed()) {
1405 debug_print("Claws Mail crashed, checking for new messages in local folders\n");
1406 folder_item_update_thaw();
1407 folderview_check_new(NULL);
1408 folder_clean_cache_memory_force();
1409 folder_item_update_freeze();
1411 /* make the crash-indicator file */
1412 str_write_to_file("foo", get_crashfile_name());
1414 inc_autocheck_timer_init(mainwin);
1416 /* ignore SIGPIPE signal for preventing sudden death of program */
1418 signal(SIGPIPE, SIG_IGN);
1420 if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
1421 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1423 if (cmd.online_mode == ONLINE_MODE_ONLINE) {
1424 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1427 if (cmd.status_folders) {
1428 g_ptr_array_free(cmd.status_folders, TRUE);
1429 cmd.status_folders = NULL;
1431 if (cmd.status_full_folders) {
1432 g_ptr_array_free(cmd.status_full_folders, TRUE);
1433 cmd.status_full_folders = NULL;
1436 claws_register_idle_function(claws_gtk_idle);
1439 prefs_toolbar_init();
1441 num_folder_class = g_list_length(folder_get_list());
1443 plugin_load_all("GTK2");
1445 if (g_list_length(folder_get_list()) != num_folder_class) {
1446 debug_print("new folders loaded, reloading processing rules\n");
1447 prefs_matcher_read_config();
1450 if ((plug_list = plugin_get_unloaded_list()) != NULL) {
1453 gint num_plugins = 0;
1454 for (cur = plug_list; cur; cur = cur->next) {
1455 Plugin *plugin = (Plugin *)cur->data;
1456 gchar *tmp = g_strdup_printf("%s\n%s",
1458 plugin_get_name(plugin));
1463 main_window_cursor_normal(mainwin);
1464 main_window_popup(mainwin);
1465 mainwin_shown = TRUE;
1466 alertpanel_warning(ngettext(
1467 "The following plugin failed to load. "
1468 "Check the Plugins configuration "
1469 "for more information:\n%s",
1470 "The following plugins failed to load. "
1471 "Check the Plugins configuration "
1472 "for more information:\n%s",
1475 main_window_cursor_wait(mainwin);
1477 g_slist_free(plug_list);
1481 prefs_common_get_prefs()->config_version = CLAWS_CONFIG_VERSION;
1482 prefs_common_write_config();
1483 plugin_load_standard_plugins ();
1485 if (prefs_update_config_version() < 0) {
1486 exit_claws(mainwin);
1494 /* if not crashed, show window now */
1495 if (!mainwin_shown) {
1496 /* apart if something told not to show */
1497 if (show_at_startup)
1498 main_window_popup(mainwin);
1501 if (cmd.geometry != NULL) {
1502 if (!gtk_window_parse_geometry(GTK_WINDOW(mainwin->window), cmd.geometry))
1503 g_warning("failed to parse geometry '%s'", cmd.geometry);
1507 if (sscanf(cmd.geometry, "%ux%u+", &width, &height) == 2)
1508 gtk_window_resize(GTK_WINDOW(mainwin->window), width, height);
1510 g_warning("failed to parse geometry's width/height");
1514 if (!folder_have_mailbox()) {
1515 prefs_destroy_cache();
1516 main_window_cursor_normal(mainwin);
1517 if (folder_get_list() != NULL) {
1518 alertpanel_error(_("Claws Mail has detected a configured "
1519 "mailbox, but it is incomplete. It is "
1520 "possibly due to a failing IMAP account. Use "
1521 "\"Rebuild folder tree\" on the mailbox parent "
1522 "folder's context menu to try to fix it."));
1524 alertpanel_error(_("Claws Mail has detected a configured "
1525 "mailbox, but could not load it. It is "
1526 "probably provided by an out-of-date "
1527 "external plugin. Please reinstall the "
1528 "plugin and try again."));
1529 exit_claws(mainwin);
1537 static_mainwindow = mainwin;
1539 #ifdef HAVE_STARTUP_NOTIFICATION
1540 startup_notification_complete(FALSE);
1543 sc_session_manager_connect(mainwin);
1546 folder_item_update_thaw();
1547 folderview_thaw(mainwin->folderview);
1548 main_window_cursor_normal(mainwin);
1550 if (!cmd.target && prefs_common.goto_last_folder_on_startup &&
1551 folder_find_item_from_identifier(prefs_common.last_opened_folder) != NULL &&
1553 cmd.target = prefs_common.last_opened_folder;
1556 if (cmd.receive_all && !cmd.target) {
1558 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(FALSE));
1559 } else if (prefs_common.chk_on_startup && !cmd.target) {
1561 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(TRUE));
1562 } else if (cmd.receive && !cmd.target) {
1564 g_timeout_add(1000, defer_check, NULL);
1566 folderview_grab_focus(folderview);
1569 open_compose_new(cmd.compose_mailto, cmd.attach_files);
1571 if (cmd.attach_files) {
1572 list_free_strings(cmd.attach_files);
1573 g_list_free(cmd.attach_files);
1574 cmd.attach_files = NULL;
1576 if (cmd.subscribe) {
1577 folder_subscribe(cmd.subscribe_uri);
1586 g_timeout_add(500, defer_jump, (gpointer)cmd.target);
1589 prefs_destroy_cache();
1591 compose_reopen_exit_drafts();
1594 sc_starting = FALSE;
1595 main_window_set_menu_sensitive(mainwin);
1596 toolbar_main_set_sensitive(mainwin);
1599 /* register the callback of unix domain socket input */
1600 lock_socket_tag = claws_input_add(lock_socket,
1601 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
1602 lock_socket_input_cb,
1609 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1611 g_object_unref(nm_proxy);
1613 #ifdef HAVE_DBUS_GLIB
1614 uninstall_dbus_status_handler();
1616 dbus_g_connection_unref(connection);
1622 exit_claws(mainwin);
1627 static void save_all_caches(FolderItem *item, gpointer data)
1634 folder_item_close(item);
1637 folder_item_free_cache(item, TRUE);
1640 static void exit_claws(MainWindow *mainwin)
1643 gboolean have_connectivity;
1648 debug_print("shutting down\n");
1649 inc_autocheck_timer_remove();
1651 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1652 if (prefs_common.work_offline && went_offline_nm)
1653 prefs_common.work_offline = FALSE;
1656 /* save prefs for opened folder */
1657 if((item = folderview_get_opened_item(mainwin->folderview)) != NULL) {
1658 summary_save_prefs_to_folderitem(
1659 mainwin->summaryview, item);
1660 prefs_common.last_opened_folder =
1661 folder_item_get_identifier(item);
1664 /* save all state before exiting */
1665 folder_func_to_all_folders(save_all_caches, NULL);
1666 folder_write_list();
1668 main_window_get_size(mainwin);
1669 main_window_get_position(mainwin);
1671 prefs_common_write_config();
1672 account_write_config_all();
1673 passwd_store_write_config();
1674 #ifndef USE_ALT_ADDRBOOK
1675 addressbook_export_to_file();
1677 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1678 gtk_accel_map_save(filename);
1681 /* delete temporary files */
1682 remove_all_files(get_tmp_dir());
1683 remove_all_files(get_mime_tmp_dir());
1685 close_log_file(LOG_PROTOCOL);
1686 close_log_file(LOG_DEBUG_FILTERING);
1688 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1689 have_connectivity = networkmanager_is_online(NULL);
1691 have_connectivity = TRUE;
1693 #ifdef HAVE_LIBETPAN
1694 imap_main_done(have_connectivity);
1695 nntp_main_done(have_connectivity);
1697 /* delete crashfile */
1699 claws_unlink(get_crashfile_name());
1701 lock_socket_remove();
1704 if (mainwin->smc_conn)
1705 SmcCloseConnection ((SmcConn)mainwin->smc_conn, 0, NULL);
1706 mainwin->smc_conn = NULL;
1709 main_window_destroy_all();
1711 plugin_unload_all("GTK2");
1714 prefs_toolbar_done();
1717 #ifndef USE_ALT_ADDRBOOK
1718 addressbook_destroy();
1720 prefs_themes_done();
1722 prefs_ext_prog_done();
1723 prefs_wrapping_done();
1724 prefs_compose_writing_done();
1725 prefs_msg_colors_done();
1726 prefs_image_viewer_done();
1727 image_viewer_done();
1729 prefs_summaries_done();
1730 prefs_message_done();
1732 prefs_receive_done();
1733 prefs_logging_done();
1737 prefs_spelling_done();
1738 gtkaspell_checkers_quit();
1740 plugin_unload_all("Common");
1744 #define G_STRING_APPEND_ENCODED_URI(gstring,source) \
1746 gchar tmpbuf[BUFFSIZE]; \
1747 encode_uri(tmpbuf, BUFFSIZE, (source)); \
1748 g_string_append((gstring), tmpbuf); \
1751 #define G_PRINT_EXIT(msg) \
1757 static GString * parse_cmd_compose_from_file(const gchar *fn)
1759 GString *headers = g_string_new(NULL);
1760 GString *body = g_string_new(NULL);
1768 if (fn == NULL || *fn == '\0')
1769 G_PRINT_EXIT(_("Missing filename\n"));
1770 isstdin = (*fn == '-' && *(fn + 1) == '\0');
1774 fp = g_fopen(fn, "r");
1776 G_PRINT_EXIT(_("Cannot open filename for reading\n"));
1779 while (fgets(fb, sizeof(fb), fp)) {
1785 while (*h && *h != ':') { ++h; } /* search colon */
1787 G_PRINT_EXIT(_("Malformed header\n"));
1789 while (*v && *v == ' ') { ++v; } /* trim value start */
1791 tmp = g_ascii_strdown(fb, -1); /* get header name */
1792 if (!strcmp(tmp, "to")) {
1794 G_PRINT_EXIT(_("Duplicated 'To:' header\n"));
1797 g_string_append_c(headers, '&');
1798 g_string_append(headers, tmp);
1799 g_string_append_c(headers, '=');
1800 g_string_append_uri_escaped(headers, v, NULL, TRUE);
1805 G_PRINT_EXIT(_("Missing required 'To:' header\n"));
1806 g_string_append(body, to);
1808 g_string_append(body, "?body=");
1809 while (fgets(fb, sizeof(fb), fp)) {
1810 g_string_append_uri_escaped(body, fb, NULL, TRUE);
1814 /* append the remaining headers */
1815 g_string_append(body, headers->str);
1816 g_string_free(headers, TRUE);
1821 #undef G_STRING_APPEND_ENCODED_URI
1824 static void parse_cmd_opt(int argc, char *argv[])
1829 for (i = 1; i < argc; i++) {
1830 if (!strncmp(argv[i], "--receive-all", 13)) {
1831 cmd.receive_all = TRUE;
1832 } else if (!strncmp(argv[i], "--receive", 9)) {
1834 } else if (!strncmp(argv[i], "--cancel-receiving", 18)) {
1835 cmd.cancel_receiving = TRUE;
1836 } else if (!strncmp(argv[i], "--cancel-sending", 16)) {
1837 cmd.cancel_sending = TRUE;
1838 } else if (!strncmp(argv[i], "--compose-from-file", 19)) {
1839 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1841 GString *mailto = parse_cmd_compose_from_file(p);
1843 cmd.compose_mailto = mailto->str;
1845 } else if (!strncmp(argv[i], "--compose", 9)) {
1846 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1849 cmd.compose_mailto = NULL;
1850 if (p && *p != '\0' && *p != '-') {
1851 if (!strncmp(p, "mailto:", 7)) {
1852 cmd.compose_mailto = p + 7;
1854 cmd.compose_mailto = p;
1858 } else if (!strncmp(argv[i], "--subscribe", 11)) {
1859 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1860 if (p && *p != '\0' && *p != '-') {
1861 cmd.subscribe = TRUE;
1862 cmd.subscribe_uri = p;
1864 } else if (!strncmp(argv[i], "--attach", 8)) {
1865 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1868 while (p && *p != '\0' && *p != '-') {
1869 if ((file = g_filename_from_uri(p, NULL, NULL)) != NULL) {
1870 if (!is_file_exist(file)) {
1875 if (file == NULL && *p != G_DIR_SEPARATOR) {
1876 file = g_strconcat(claws_get_startup_dir(),
1879 } else if (file == NULL) {
1882 ainfo = g_new0(AttachInfo, 1);
1884 cmd.attach_files = g_list_append(cmd.attach_files, ainfo);
1886 p = (i+1 < argc)?argv[i+1]:NULL;
1888 } else if (!strncmp(argv[i], "--send", 6)) {
1890 } else if (!strncmp(argv[i], "--version-full", 14) ||
1891 !strncmp(argv[i], "-V", 2)) {
1892 g_print("Claws Mail version " VERSION_GIT_FULL "\n");
1893 main_dump_features_list(FALSE);
1895 } else if (!strncmp(argv[i], "--version", 9) ||
1896 !strncmp(argv[i], "-v", 2)) {
1897 g_print("Claws Mail version " VERSION "\n");
1899 } else if (!strncmp(argv[i], "--status-full", 13)) {
1900 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1902 cmd.status_full = TRUE;
1903 while (p && *p != '\0' && *p != '-') {
1904 if (!cmd.status_full_folders) {
1905 cmd.status_full_folders =
1908 g_ptr_array_add(cmd.status_full_folders,
1911 p = (i+1 < argc)?argv[i+1]:NULL;
1913 } else if (!strncmp(argv[i], "--status", 8)) {
1914 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1917 while (p && *p != '\0' && *p != '-') {
1918 if (!cmd.status_folders)
1919 cmd.status_folders = g_ptr_array_new();
1920 g_ptr_array_add(cmd.status_folders,
1923 p = (i+1 < argc)?argv[i+1]:NULL;
1925 } else if (!strncmp(argv[i], "--search", 8)) {
1926 cmd.search_folder = (i+1 < argc)?argv[i+1]:NULL;
1927 cmd.search_type = (i+2 < argc)?argv[i+2]:NULL;
1928 cmd.search_request = (i+3 < argc)?argv[i+3]:NULL;
1929 const char* rec = (i+4 < argc)?argv[i+4]:NULL;
1930 cmd.search_recursive = TRUE;
1931 if (rec && (tolower(*rec)=='n' || tolower(*rec)=='f' || *rec=='0'))
1932 cmd.search_recursive = FALSE;
1933 if (cmd.search_folder && cmd.search_type && cmd.search_request)
1935 } else if (!strncmp(argv[i], "--online", 8)) {
1936 cmd.online_mode = ONLINE_MODE_ONLINE;
1937 } else if (!strncmp(argv[i], "--offline", 9)) {
1938 cmd.online_mode = ONLINE_MODE_OFFLINE;
1939 } else if (!strncmp(argv[i], "--toggle-debug", 14)) {
1941 } else if (!strncmp(argv[i], "--statistics", 12)) {
1942 cmd.statistics = TRUE;
1943 } else if (!strncmp(argv[i], "--reset-statistics", 18)) {
1944 cmd.reset_statistics = TRUE;
1945 } else if (!strncmp(argv[i], "--help", 6) ||
1946 !strncmp(argv[i], "-h", 2)) {
1947 gchar *base = g_path_get_basename(argv[0]);
1948 g_print(_("Usage: %s [OPTION]...\n"), base);
1950 g_print("%s\n", _(" --compose [address] open composition window"));
1951 g_print("%s\n", _(" --compose-from-file file\n"
1952 " open composition window with data from given file;\n"
1953 " use - as file name for reading from standard input;\n"
1954 " content format: headers first (To: required) until an\n"
1955 " empty line, then mail body until end of file."));
1956 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
1957 g_print("%s\n", _(" --attach file1 [file2]...\n"
1958 " open composition window with specified files\n"
1960 g_print("%s\n", _(" --receive receive new messages"));
1961 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
1962 g_print("%s\n", _(" --cancel-receiving cancel receiving of messages"));
1963 g_print("%s\n", _(" --cancel-sending cancel sending of messages"));
1964 g_print("%s\n", _(" --search folder type request [recursive]\n"
1966 " folder ex.: \"#mh/Mailbox/inbox\" or \"Mail\"\n"
1967 " type: s[ubject],f[rom],t[o],e[xtended],m[ixed] or g: tag\n"
1968 " request: search string\n"
1969 " recursive: false if arg. starts with 0, n, N, f or F"));
1971 g_print("%s\n", _(" --send send all queued messages"));
1972 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
1973 g_print("%s\n", _(" --status-full [folder]...\n"
1974 " show the status of each folder"));
1975 g_print("%s\n", _(" --statistics show session statistics"));
1976 g_print("%s\n", _(" --reset-statistics reset session statistics"));
1977 g_print("%s\n", _(" --select folder[/msg] jumps to the specified folder/message\n"
1978 " folder is a folder id like 'folder/sub_folder'"));
1979 g_print("%s\n", _(" --online switch to online mode"));
1980 g_print("%s\n", _(" --offline switch to offline mode"));
1981 g_print("%s\n", _(" --exit --quit -q exit Claws Mail"));
1982 g_print("%s\n", _(" --debug debug mode"));
1983 g_print("%s\n", _(" --toggle-debug toggle debug mode"));
1984 g_print("%s\n", _(" --help -h display this help and exit"));
1985 g_print("%s\n", _(" --version -v output version information and exit"));
1986 g_print("%s\n", _(" --version-full -V output version and built-in features information and exit"));
1987 g_print("%s\n", _(" --config-dir output configuration directory"));
1988 g_print("%s\n", _(" --alternate-config-dir [dir]\n"
1989 " use specified configuration directory"));
1990 g_print("%s\n", _(" --geometry -geometry WxH+X+Y\n"
1991 " set geometry for main window"));
1995 } else if (!strncmp(argv[i], "--crash", 7)) {
1997 cmd.crash_params = g_strdup((i+1 < argc)?argv[i+1]:NULL);
1999 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
2000 g_print(RC_DIR "\n");
2002 } else if (!strncmp(argv[i], "--alternate-config-dir", sizeof "--alternate-config-dir" - 1) && i+1 < argc) {
2003 set_rc_dir(argv[i+1]);
2004 } else if (!strncmp(argv[i], "--geometry", sizeof "--geometry" - 1)
2005 || !strncmp(argv[i], "-geometry", sizeof "-geometry" - 1)) {
2006 cmd.geometry = (i+1 < argc)? argv[i+1]: NULL;
2007 } else if (!strncmp(argv[i], "--exit", 6) ||
2008 !strncmp(argv[i], "--quit", 6) ||
2009 !strncmp(argv[i], "-q", 2)) {
2011 } else if (!strncmp(argv[i], "--select", 8) && i+1 < argc) {
2012 cmd.target = argv[i+1];
2013 } else if (i == 1 && argc == 2) {
2014 /* only one parameter. Do something intelligent about it */
2015 if ((strstr(argv[i], "@")||!strncmp(argv[i], "mailto:", 7)) && !strstr(argv[i], "://")) {
2016 const gchar *p = argv[i];
2019 cmd.compose_mailto = NULL;
2020 if (p && *p != '\0' && *p != '-') {
2021 if (!strncmp(p, "mailto:", 7)) {
2022 cmd.compose_mailto = p + 7;
2024 cmd.compose_mailto = p;
2027 } else if (!strncmp(argv[i], "file://", 7)) {
2028 cmd.target = argv[i];
2029 } else if (!strncmp(argv[i], "?attach=file://", strlen("?attach=file://"))) {
2031 cmd.compose_mailto = argv[i];
2032 } else if (strstr(argv[i], "://")) {
2033 const gchar *p = argv[i];
2034 if (p && *p != '\0' && *p != '-') {
2035 cmd.subscribe = TRUE;
2036 cmd.subscribe_uri = p;
2038 } else if (!strcmp(argv[i], "--sync")) {
2040 } else if (is_dir_exist(argv[i]) || is_file_exist(argv[i])) {
2041 cmd.target = argv[i];
2043 g_print(_("Unknown option\n"));
2049 if (cmd.attach_files && cmd.compose == FALSE) {
2051 cmd.compose_mailto = NULL;
2055 static void initial_processing(FolderItem *item, gpointer data)
2057 MainWindow *mainwin = (MainWindow *)data;
2060 cm_return_if_fail(item);
2061 buf = g_strdup_printf(_("Processing (%s)..."),
2064 : _("top level folder"));
2068 if (item->prefs->enable_processing) {
2069 item->processing_pending = TRUE;
2070 folder_item_apply_processing(item);
2071 item->processing_pending = FALSE;
2074 STATUSBAR_POP(mainwin);
2077 static gboolean draft_all_messages(void)
2079 const GList *compose_list = NULL;
2081 compose_clear_exit_drafts();
2082 compose_list = compose_get_compose_list();
2083 while (compose_list != NULL) {
2084 Compose *c = (Compose*)compose_list->data;
2085 if (!compose_draft(c, COMPOSE_DRAFT_FOR_EXIT))
2087 compose_list = compose_get_compose_list();
2091 gboolean clean_quit(gpointer data)
2093 static gboolean firstrun = TRUE;
2100 /*!< Good idea to have the main window stored in a
2101 * static variable so we can check that variable
2102 * to see if we're really allowed to do things
2103 * that actually the spawner is supposed to
2104 * do (like: sending mail, composing messages).
2105 * Because, really, if we're the spawnee, and
2106 * we touch GTK stuff, we're hosed. See the
2109 /* FIXME: Use something else to signal that we're
2110 * in the original spawner, and not in a spawned
2112 if (!static_mainwindow) {
2116 draft_all_messages();
2117 emergency_exit = TRUE;
2118 exit_claws(static_mainwindow);
2124 void app_will_exit(GtkWidget *widget, gpointer data)
2126 MainWindow *mainwin = data;
2128 if (gtk_main_level() == 0) {
2129 debug_print("not even started\n");
2132 if (sc_exiting == TRUE) {
2133 debug_print("exit pending\n");
2137 debug_print("exiting\n");
2138 if (compose_get_compose_list()) {
2139 if (!draft_all_messages()) {
2140 main_window_popup(mainwin);
2146 if (prefs_common.warn_queued_on_exit && procmsg_have_queued_mails_fast()) {
2147 if (alertpanel(_("Queued messages"),
2148 _("Some unsent messages are queued. Exit now?"),
2149 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL)
2150 != G_ALERTALTERNATE) {
2151 main_window_popup(mainwin);
2155 manage_window_focus_in(mainwin->window, NULL, NULL);
2159 #ifdef HAVE_VALGRIND
2160 if (RUNNING_ON_VALGRIND) {
2161 summary_clear_list(mainwin->summaryview);
2164 if (folderview_get_selected_item(mainwin->folderview))
2165 folder_item_close(folderview_get_selected_item(mainwin->folderview));
2169 gboolean claws_is_exiting(void)
2174 gboolean claws_is_starting(void)
2181 * CLAWS: want this public so crash dialog can delete the
2184 gchar *claws_get_socket_name(void)
2186 static gchar *filename = NULL;
2187 gchar *socket_dir = NULL;
2190 if (filename == NULL) {
2194 socket_dir = g_strdup_printf("%s%cclaws-mail-%d",
2195 g_get_tmp_dir(), G_DIR_SEPARATOR,
2201 stat_ok = g_stat(socket_dir, &st);
2202 if (stat_ok < 0 && errno != ENOENT) {
2203 g_print("Error stat'ing socket_dir %s: %s\n",
2204 socket_dir, g_strerror(errno));
2205 } else if (stat_ok == 0 && S_ISSOCK(st.st_mode)) {
2206 /* old versions used a sock in $TMPDIR/claws-mail-$UID */
2207 debug_print("Using legacy socket %s\n", socket_dir);
2208 filename = g_strdup(socket_dir);
2212 if (!is_dir_exist(socket_dir) && make_dir(socket_dir) < 0) {
2213 g_print("Error creating socket_dir %s: %s\n",
2214 socket_dir, g_strerror(errno));
2217 md5_hex_digest(md5sum, get_rc_dir());
2219 filename = g_strdup_printf("%s%c%s", socket_dir, G_DIR_SEPARATOR,
2222 debug_print("Using control socket %s\n", filename);
2229 static gchar *get_crashfile_name(void)
2231 static gchar *filename = NULL;
2233 if (filename == NULL) {
2234 filename = g_strdup_printf("%s%cclaws-crashed",
2235 get_tmp_dir(), G_DIR_SEPARATOR);
2241 static gint prohibit_duplicate_launch(void)
2248 path = claws_get_socket_name();
2249 /* Try to connect to the control socket */
2250 uxsock = fd_connect_unix(path);
2252 if (x_display == NULL)
2253 x_display = g_strdup(g_getenv("DISPLAY"));
2260 /* If connect failed, no other process is running.
2261 * Unlink the potentially existing socket, then
2262 * open it. This has to be done locking a temporary
2263 * file to avoid the race condition where another
2264 * process could have created the socket just in
2267 socket_lock = g_strconcat(path, ".lock",
2269 lock_fd = g_open(socket_lock, O_RDWR|O_CREAT, 0);
2271 debug_print("Couldn't open %s: %s (%d)\n", socket_lock,
2272 g_strerror(errno), errno);
2273 g_free(socket_lock);
2276 if (flock(lock_fd, LOCK_EX) < 0) {
2277 debug_print("Couldn't lock %s: %s (%d)\n", socket_lock,
2278 g_strerror(errno), errno);
2280 g_free(socket_lock);
2286 debug_print("Opening socket %s\n", path);
2287 ret = fd_open_unix(path);
2289 flock(lock_fd, LOCK_UN);
2291 claws_unlink(socket_lock);
2292 g_free(socket_lock);
2299 hmutex = CreateMutexA(NULL, FALSE, "ClawsMail");
2301 debug_print("cannot create Mutex\n");
2304 if (GetLastError() != ERROR_ALREADY_EXISTS) {
2305 uxsock = fd_open_inet(50216);
2311 uxsock = fd_connect_inet(50216);
2315 /* remote command mode */
2317 debug_print("another Claws Mail instance is already running.\n");
2319 if (cmd.receive_all) {
2320 fd_write_all(uxsock, "receive_all\n", 12);
2321 } else if (cmd.receive) {
2322 fd_write_all(uxsock, "receive\n", 8);
2323 } else if (cmd.cancel_receiving) {
2324 fd_write_all(uxsock, "cancel_receiving\n", 17);
2325 } else if (cmd.cancel_sending) {
2326 fd_write_all(uxsock, "cancel_sending\n", 15);
2327 } else if (cmd.compose && cmd.attach_files) {
2328 gchar *str, *compose_str;
2330 if (cmd.compose_mailto) {
2331 compose_str = g_strdup_printf("compose_attach %s\n",
2332 cmd.compose_mailto);
2334 compose_str = g_strdup("compose_attach\n");
2337 fd_write_all(uxsock, compose_str, strlen(compose_str));
2338 g_free(compose_str);
2340 for (curr = cmd.attach_files; curr != NULL ; curr = curr->next) {
2341 str = (gchar *) ((AttachInfo *)curr->data)->file;
2342 fd_write_all(uxsock, str, strlen(str));
2343 fd_write_all(uxsock, "\n", 1);
2346 fd_write_all(uxsock, ".\n", 2);
2347 } else if (cmd.compose) {
2350 if (cmd.compose_mailto) {
2351 compose_str = g_strdup_printf
2352 ("compose %s\n", cmd.compose_mailto);
2354 compose_str = g_strdup("compose\n");
2357 fd_write_all(uxsock, compose_str, strlen(compose_str));
2358 g_free(compose_str);
2359 } else if (cmd.subscribe) {
2360 gchar *str = g_strdup_printf("subscribe %s\n", cmd.subscribe_uri);
2361 fd_write_all(uxsock, str, strlen(str));
2363 } else if (cmd.send) {
2364 fd_write_all(uxsock, "send\n", 5);
2365 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
2366 fd_write(uxsock, "online\n", 6);
2367 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
2368 fd_write(uxsock, "offline\n", 7);
2369 } else if (cmd.debug) {
2370 fd_write(uxsock, "debug\n", 7);
2371 } else if (cmd.status || cmd.status_full) {
2372 gchar buf[BUFFSIZE];
2374 const gchar *command;
2378 command = cmd.status_full ? "status-full\n" : "status\n";
2379 folders = cmd.status_full ? cmd.status_full_folders :
2382 fd_write_all(uxsock, command, strlen(command));
2383 for (i = 0; folders && i < folders->len; ++i) {
2384 folder = g_ptr_array_index(folders, i);
2385 fd_write_all(uxsock, folder, strlen(folder));
2386 fd_write_all(uxsock, "\n", 1);
2388 fd_write_all(uxsock, ".\n", 2);
2390 fd_gets(uxsock, buf, sizeof(buf) - 1);
2391 buf[sizeof(buf) - 1] = '\0';
2392 if (!strncmp(buf, ".\n", 2)) break;
2395 } else if (cmd.exit) {
2396 fd_write_all(uxsock, "exit\n", 5);
2397 } else if (cmd.statistics) {
2399 fd_write(uxsock, "statistics\n", 11);
2401 fd_gets(uxsock, buf, sizeof(buf) - 1);
2402 buf[sizeof(buf) - 1] = '\0';
2403 if (!strncmp(buf, ".\n", 2)) break;
2406 } else if (cmd.reset_statistics) {
2407 fd_write(uxsock, "reset_statistics\n", 17);
2408 } else if (cmd.target) {
2409 gchar *str = g_strdup_printf("select %s\n", cmd.target);
2410 fd_write_all(uxsock, str, strlen(str));
2412 } else if (cmd.search) {
2413 gchar buf[BUFFSIZE];
2415 g_strdup_printf("search %s\n%s\n%s\n%c\n",
2416 cmd.search_folder, cmd.search_type, cmd.search_request,
2417 (cmd.search_recursive==TRUE)?'1':'0');
2418 fd_write_all(uxsock, str, strlen(str));
2421 fd_gets(uxsock, buf, sizeof(buf) - 1);
2422 buf[sizeof(buf) - 1] = '\0';
2423 if (!strncmp(buf, ".\n", 2)) break;
2429 fd_write_all(uxsock, "get_display\n", 12);
2430 memset(buf, 0, sizeof(buf));
2431 fd_gets(uxsock, buf, sizeof(buf) - 1);
2432 buf[sizeof(buf) - 1] = '\0';
2433 if (strcmp2(buf, x_display)) {
2434 g_print("Claws Mail is already running on display %s.\n",
2438 uxsock = fd_connect_unix(path);
2439 fd_write_all(uxsock, "popup\n", 6);
2442 fd_write_all(uxsock, "popup\n", 6);
2450 static gint lock_socket_remove(void)
2453 gchar *filename, *dirname;
2455 if (lock_socket < 0) {
2459 if (lock_socket_tag > 0) {
2460 g_source_remove(lock_socket_tag);
2462 fd_close(lock_socket);
2465 filename = claws_get_socket_name();
2466 dirname = g_path_get_dirname(filename);
2467 claws_unlink(filename);
2475 static GPtrArray *get_folder_item_list(gint sock)
2477 gchar buf[BUFFSIZE];
2479 GPtrArray *folders = NULL;
2482 fd_gets(sock, buf, sizeof(buf) - 1);
2483 buf[sizeof(buf) - 1] = '\0';
2484 if (!strncmp(buf, ".\n", 2)) {
2489 folders = g_ptr_array_new();
2491 item = folder_find_item_from_identifier(buf);
2493 g_ptr_array_add(folders, item);
2495 g_warning("no such folder: %s", buf);
2502 static void lock_socket_input_cb(gpointer data,
2504 GIOCondition condition)
2506 MainWindow *mainwin = (MainWindow *)data;
2508 gchar buf[BUFFSIZE];
2510 sock = fd_accept(source);
2514 fd_gets(sock, buf, sizeof(buf) - 1);
2515 buf[sizeof(buf) - 1] = '\0';
2517 if (!strncmp(buf, "popup", 5)) {
2518 main_window_popup(mainwin);
2519 } else if (!strncmp(buf, "get_display", 11)) {
2520 fd_write_all(sock, x_display, strlen(x_display));
2521 } else if (!strncmp(buf, "receive_all", 11)) {
2522 inc_all_account_mail(mainwin, FALSE,
2523 prefs_common.newmail_notify_manu);
2524 } else if (!strncmp(buf, "receive", 7)) {
2525 inc_mail(mainwin, prefs_common.newmail_notify_manu);
2526 } else if (!strncmp(buf, "cancel_receiving", 16)) {
2529 } else if (!strncmp(buf, "cancel_sending", 14)) {
2531 } else if (!strncmp(buf, "compose_attach", 14)) {
2532 GList *files = NULL, *curr;
2536 mailto = g_strdup(buf + strlen("compose_attach") + 1);
2537 while (fd_gets(sock, buf, sizeof(buf) - 1) > 0) {
2538 buf[sizeof(buf) - 1] = '\0';
2540 if (!strcmp2(buf, "."))
2543 ainfo = g_new0(AttachInfo, 1);
2544 ainfo->file = g_strdup(buf);
2545 files = g_list_append(files, ainfo);
2547 open_compose_new(mailto, files);
2549 curr = g_list_first(files);
2550 while (curr != NULL) {
2551 ainfo = (AttachInfo *)curr->data;
2552 g_free(ainfo->file);
2558 } else if (!strncmp(buf, "compose", 7)) {
2559 open_compose_new(buf + strlen("compose") + 1, NULL);
2560 } else if (!strncmp(buf, "subscribe", 9)) {
2561 main_window_popup(mainwin);
2562 folder_subscribe(buf + strlen("subscribe") + 1);
2563 } else if (!strncmp(buf, "send", 4)) {
2565 } else if (!strncmp(buf, "online", 6)) {
2566 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
2567 } else if (!strncmp(buf, "offline", 7)) {
2568 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
2569 } else if (!strncmp(buf, "debug", 5)) {
2570 debug_set_mode(debug_get_mode() ? FALSE : TRUE);
2571 } else if (!strncmp(buf, "status-full", 11) ||
2572 !strncmp(buf, "status", 6)) {
2576 folders = get_folder_item_list(sock);
2577 status = folder_get_status
2578 (folders, !strncmp(buf, "status-full", 11));
2579 fd_write_all(sock, status, strlen(status));
2580 fd_write_all(sock, ".\n", 2);
2582 if (folders) g_ptr_array_free(folders, TRUE);
2583 } else if (!strncmp(buf, "statistics", 10)) {
2586 g_snprintf(tmp, sizeof(tmp), _("Session statistics\n"));
2587 fd_write_all(sock, tmp, strlen(tmp));
2589 if (prefs_common.date_format) {
2594 lt = localtime(&session_stats.time_started);
2595 fast_strftime(date, len, prefs_common.date_format, lt);
2596 g_snprintf(tmp, sizeof(tmp), _("Started: %s\n"),
2597 lt ? date : ctime(&session_stats.time_started));
2599 g_snprintf(tmp, sizeof(tmp), _("Started: %s\n"),
2600 ctime(&session_stats.time_started));
2601 fd_write_all(sock, tmp, strlen(tmp));
2603 fd_write_all(sock, "\n", 1);
2605 g_snprintf(tmp, sizeof(tmp), _("Incoming traffic\n"));
2606 fd_write_all(sock, tmp, strlen(tmp));
2608 g_snprintf(tmp, sizeof(tmp), _("Received messages: %d\n"),
2609 session_stats.received);
2610 fd_write_all(sock, tmp, strlen(tmp));
2612 fd_write_all(sock, "\n", 1);
2614 g_snprintf(tmp, sizeof(tmp), _("Outgoing traffic\n"));
2615 fd_write_all(sock, tmp, strlen(tmp));
2617 g_snprintf(tmp, sizeof(tmp), _("New/redirected messages: %d\n"),
2618 session_stats.sent);
2619 fd_write_all(sock, tmp, strlen(tmp));
2621 g_snprintf(tmp, sizeof(tmp), _("Replied messages: %d\n"),
2622 session_stats.replied);
2623 fd_write_all(sock, tmp, strlen(tmp));
2625 g_snprintf(tmp, sizeof(tmp), _("Forwarded messages: %d\n"),
2626 session_stats.forwarded);
2627 fd_write_all(sock, tmp, strlen(tmp));
2629 g_snprintf(tmp, sizeof(tmp), _("Total outgoing messages: %d\n"),
2630 (session_stats.sent + session_stats.replied +
2631 session_stats.forwarded));
2632 fd_write_all(sock, tmp, strlen(tmp));
2634 fd_write_all(sock, ".\n", 2);
2635 } else if (!strncmp(buf, "reset_statistics", 16)) {
2637 } else if (!strncmp(buf, "select ", 7)) {
2638 const gchar *target = buf+7;
2639 mainwindow_jump_to(target, TRUE);
2640 } else if (!strncmp(buf, "search ", 7)) {
2641 FolderItem* folderItem = NULL;
2642 GSList *messages = NULL;
2643 gchar *folder_name = NULL;
2644 gchar *request = NULL;
2645 AdvancedSearch *search;
2647 AdvancedSearchType searchType = ADVANCED_SEARCH_EXTENDED;
2649 search = advsearch_new();
2651 folder_name = g_strdup(buf+7);
2652 strretchomp(folder_name);
2654 if (fd_gets(sock, buf, sizeof(buf) - 1) <= 0)
2656 buf[sizeof(buf) - 1] = '\0';
2658 switch (toupper(buf[0])) {
2659 case 'S': searchType = ADVANCED_SEARCH_SUBJECT; break;
2660 case 'F': searchType = ADVANCED_SEARCH_FROM; break;
2661 case 'T': searchType = ADVANCED_SEARCH_TO; break;
2662 case 'M': searchType = ADVANCED_SEARCH_MIXED; break;
2663 case 'G': searchType = ADVANCED_SEARCH_TAG; break;
2664 case 'E': searchType = ADVANCED_SEARCH_EXTENDED; break;
2667 if (fd_gets(sock, buf, sizeof(buf) - 1) <= 0)
2670 buf[sizeof(buf) - 1] = '\0';
2671 request = g_strdup(buf);
2672 strretchomp(request);
2675 if (fd_gets(sock, buf, sizeof(buf) - 1) > 0)
2676 recursive = buf[0] != '0';
2678 buf[sizeof(buf) - 1] = '\0';
2680 debug_print("search: %s %i %s %i\n", folder_name, searchType, request, recursive);
2682 folderItem = folder_find_item_from_identifier(folder_name);
2684 if (folderItem == NULL) {
2685 debug_print("Unknown folder item : '%s', searching folder\n",folder_name);
2686 Folder* folder = folder_find_from_path(folder_name);
2688 folderItem = FOLDER_ITEM(folder->node->data);
2690 debug_print("Unknown folder: '%s'\n",folder_name);
2692 debug_print("%s %s\n",folderItem->name, folderItem->path);
2695 if (folderItem != NULL) {
2696 advsearch_set(search, searchType, request);
2697 advsearch_search_msgs_in_folders(search, &messages, folderItem, recursive);
2699 g_print("Folder '%s' not found.\n'", folder_name);
2703 for (cur = messages; cur != NULL; cur = cur->next) {
2704 MsgInfo* msg = (MsgInfo *)cur->data;
2705 gchar *file = procmsg_get_message_file_path(msg);
2706 fd_write_all(sock, file, strlen(file));
2707 fd_write_all(sock, "\n", 1);
2710 fd_write_all(sock, ".\n", 2);
2713 g_free(folder_name);
2715 advsearch_free(search);
2716 if (messages != NULL)
2717 procmsg_msg_list_free(messages);
2718 } else if (!strncmp(buf, "exit", 4)) {
2719 if (prefs_common.clean_on_exit && !prefs_common.ask_on_clean) {
2720 procmsg_empty_all_trash();
2722 app_will_exit(NULL, mainwin);
2728 static void open_compose_new(const gchar *address, GList *attach_files)
2733 Xstrdup_a(addr, address, return);
2737 compose_new(NULL, addr, attach_files);
2740 static void send_queue(void)
2743 gchar *errstr = NULL;
2744 gboolean error = FALSE;
2745 for (list = folder_get_list(); list != NULL; list = list->next) {
2746 Folder *folder = list->data;
2748 if (folder->queue) {
2749 gint res = procmsg_send_queue
2750 (folder->queue, prefs_common.savemsg,
2754 folder_item_scan(folder->queue);
2762 alertpanel_error_log(_("Some errors occurred "
2763 "while sending queued messages:\n%s"), errstr);
2766 alertpanel_error_log("Some errors occurred "
2767 "while sending queued messages.");
2771 static void quit_signal_handler(int sig)
2773 debug_print("Quitting on signal %d\n", sig);
2775 g_timeout_add(0, clean_quit, NULL);
2778 static void install_basic_sighandlers()
2782 struct sigaction act;
2787 sigaddset(&mask, SIGTERM);
2790 sigaddset(&mask, SIGINT);
2793 sigaddset(&mask, SIGHUP);
2796 act.sa_handler = quit_signal_handler;
2801 sigaction(SIGTERM, &act, 0);
2804 sigaction(SIGINT, &act, 0);
2807 sigaction(SIGHUP, &act, 0);
2810 sigprocmask(SIG_UNBLOCK, &mask, 0);
2811 #endif /* !G_OS_WIN32 */
2814 #if (defined linux && defined SIGIO)
2815 static int mem_notify_fd = 0;
2817 static gboolean clean_caches(gpointer unused)
2819 if (static_mainwindow && static_mainwindow->lock_count > 0)
2821 debug_print("/dev/mem_notify: callback: Freeing some memory now!\n");
2822 folder_clean_cache_memory_force();
2826 static void memory_signal_handler(int sig)
2828 debug_print("/dev/mem_notify: Kernel says we should free up some memory!\n");
2829 g_timeout_add(10, clean_caches, NULL);
2832 static void install_memory_sighandler()
2835 struct sigaction act;
2838 mem_notify_fd = g_open("/dev/mem_notify", O_RDONLY|O_NONBLOCK, 0);
2839 if (mem_notify_fd == -1) {
2840 debug_print("/dev/mem_notify not available (%s)\n",
2845 fcntl(mem_notify_fd, F_SETOWN, getpid());
2846 flags = fcntl(mem_notify_fd, F_GETFL);
2847 fcntl(mem_notify_fd, flags|FASYNC);
2851 sigaddset(&mask, SIGIO);
2853 act.sa_handler = memory_signal_handler;
2857 sigaction(SIGIO, &act, 0);
2859 sigprocmask(SIG_UNBLOCK, &mask, 0);
2861 debug_print("/dev/mem_notify: installed handler\n");
2863 #endif /* linux && SIGIO */
2865 #ifdef HAVE_NETWORKMANAGER_SUPPORT
2866 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
2869 MainWindow *mainWin;
2872 if (static_mainwindow)
2873 mainWin = static_mainwindow;
2875 mainWin = (MainWindow*)data;
2877 if (!prefs_common.use_networkmanager)
2881 GError *error = NULL;
2884 online = networkmanager_is_online(&error);
2886 if(online && went_offline_nm) {
2887 went_offline_nm = FALSE;
2888 main_window_toggle_work_offline(mainWin, FALSE, FALSE);
2889 debug_print("NetworkManager: Went online\n");
2890 log_message(LOG_PROTOCOL, _("NetworkManager: network is online.\n"));
2893 went_offline_nm = TRUE;
2894 main_window_toggle_work_offline(mainWin, TRUE, FALSE);
2895 debug_print("NetworkManager: Went offline\n");
2896 log_message(LOG_PROTOCOL, _("NetworkManager: network is offline.\n"));
2900 debug_print("Failed to get online information from NetworkManager: %s\n",
2902 g_error_free(error);
2906 debug_print("NetworkManager: Cannot change connection state because "
2907 "main window does not exist\n");
2910 /* Returns true (and sets error appropriately, if given) in case of error */
2911 gboolean networkmanager_is_online(GError **error)
2913 DBusGConnection *connection;
2915 GError *tmp_error = NULL;
2919 if (!prefs_common.use_networkmanager)
2924 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &tmp_error);
2927 /* If calling code doesn't do error checking, at least print some debug */
2928 if((error == NULL) || (*error == NULL))
2929 debug_print("Failed to open connection to system bus: %s\n",
2930 tmp_error->message);
2931 g_propagate_error(error, tmp_error);
2935 proxy = dbus_g_proxy_new_for_name(connection,
2936 "org.freedesktop.NetworkManager",
2937 "/org/freedesktop/NetworkManager",
2938 "org.freedesktop.NetworkManager");
2940 retVal = dbus_g_proxy_call(proxy,"state",&tmp_error, G_TYPE_INVALID,
2941 G_TYPE_UINT, &state, G_TYPE_INVALID);
2944 g_object_unref(proxy);
2946 dbus_g_connection_unref(connection);
2949 /* If calling code doesn't do error checking, at least print some debug */
2950 if((error == NULL) || (*error == NULL))
2951 debug_print("Failed to get state info from NetworkManager: %s\n",
2952 tmp_error->message);
2953 g_propagate_error(error, tmp_error);
2956 #if NM_CHECK_VERSION(0,8,992)
2957 return (state == NM_STATE_CONNECTED_LOCAL ||
2958 state == NM_STATE_CONNECTED_SITE ||
2959 state == NM_STATE_CONNECTED_GLOBAL ||
2960 state == NM_STATE_UNKNOWN);
2962 return (state == NM_STATE_CONNECTED ||
2963 state == NM_STATE_UNKNOWN);