2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "claws-features.h"
28 #include <glib/gi18n.h>
38 #include <sys/types.h>
45 #include <X11/SM/SMlib.h>
53 #ifdef HAVE_STARTUP_NOTIFICATION
54 # define SN_API_NOT_YET_FROZEN
55 # include <libsn/sn-launchee.h>
56 # include <gdk/gdkx.h>
60 #include <dbus/dbus-glib.h>
62 #ifdef HAVE_NETWORKMANAGER_SUPPORT
63 #include <NetworkManager.h>
71 #include "mainwindow.h"
72 #include "folderview.h"
73 #include "image_viewer.h"
74 #include "summaryview.h"
75 #include "prefs_common.h"
76 #include "prefs_account.h"
77 #include "prefs_actions.h"
78 #include "prefs_ext_prog.h"
79 #include "prefs_fonts.h"
80 #include "prefs_image_viewer.h"
81 #include "prefs_message.h"
82 #include "prefs_receive.h"
83 #include "prefs_msg_colors.h"
84 #include "prefs_quote.h"
85 #include "prefs_spelling.h"
86 #include "prefs_summaries.h"
87 #include "prefs_themes.h"
88 #include "prefs_other.h"
89 #include "prefs_logging.h"
90 #include "prefs_send.h"
91 #include "prefs_wrapping.h"
92 #include "prefs_compose_writing.h"
93 #include "prefs_display_header.h"
98 #include "manage_window.h"
99 #include "alertpanel.h"
100 #include "statusbar.h"
101 #ifndef USE_NEW_ADDRBOOK
102 #include "addressbook.h"
104 #include "addressbook-dbus.h"
108 #include "folder_item_prefs.h"
111 #include "gtkutils.h"
114 #include "prefs_toolbar.h"
117 #include "imap_gtk.h"
118 #include "news_gtk.h"
123 #include "quicksearch.h"
124 #include "advsearch.h"
127 #include "imap-thread.h"
128 #include "nntp-thread.h"
130 #include "stock_pixmap.h"
143 #include <hildon/hildon-banner.h>
144 #include <hildon/hildon-program.h>
146 #include <hildon-widgets/hildon-banner.h>
147 #include <hildon-widgets/hildon-program.h>
150 #include <libgnomevfs/gnome-vfs-volume.h>
151 #include <libgnomevfs/gnome-vfs-volume-monitor.h>
152 #include <libgnomevfs/gnome-vfs-utils.h>
154 #define OSSO_NAME "claws_mail"
155 #define OSSO_SERVICE "com.nokia."OSSO_NAME
156 #define OSSO_OBJECT "/com/nokia/"OSSO_NAME
157 #define OSSO_IFACE "com.nokia."OSSO_NAME
159 typedef struct _AppData AppData;
161 HildonProgram *program;
162 HildonWindow *window;
163 osso_context_t *osso_context;
166 static GnomeVFSVolumeMonitor *volmon;
169 #ifdef HAVE_NETWORKMANAGER_SUPPORT
170 /* Went offline due to NetworkManager */
171 static gboolean went_offline_nm;
174 #if !defined(NM_CHECK_VERSION)
175 #define NM_CHECK_VERSION(x,y,z) 0
178 #ifdef HAVE_DBUS_GLIB
179 static DBusGProxy *awn_proxy = NULL;
186 HildonProgram *hildon_program;
189 #ifdef HAVE_STARTUP_NOTIFICATION
190 static SnLauncheeContext *sn_context = NULL;
191 static SnDisplay *sn_display = NULL;
194 static gint lock_socket = -1;
195 static gint lock_socket_tag = 0;
196 static gchar *x_display = NULL;
199 ONLINE_MODE_DONT_CHANGE,
204 static struct RemoteCmd {
206 gboolean receive_all;
208 const gchar *compose_mailto;
211 const gchar *search_folder;
212 const gchar *search_type;
213 const gchar *search_request;
214 gboolean search_recursive;
216 gboolean status_full;
218 gboolean reset_statistics;
219 GPtrArray *status_folders;
220 GPtrArray *status_full_folders;
227 const gchar *subscribe_uri;
231 SessionStats session_stats;
233 static void reset_statistics(void);
235 static void parse_cmd_opt(int argc, char *argv[]);
237 static gint prohibit_duplicate_launch (void);
238 static gchar * get_crashfile_name (void);
239 static gint lock_socket_remove (void);
240 static void lock_socket_input_cb (gpointer data,
242 GIOCondition condition);
244 static void open_compose_new (const gchar *address,
245 GList *attach_files);
247 static void send_queue (void);
248 static void initial_processing (FolderItem *item, gpointer data);
249 static void quit_signal_handler (int sig);
250 static void install_basic_sighandlers (void);
251 #if (defined linux && defined SIGIO)
252 static void install_memory_sighandler (void);
254 static void exit_claws (MainWindow *mainwin);
256 #ifdef HAVE_NETWORKMANAGER_SUPPORT
257 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
261 #define MAKE_DIR_IF_NOT_EXIST(dir) \
263 if (!is_dir_exist(dir)) { \
264 if (is_file_exist(dir)) { \
266 (_("File '%s' already exists.\n" \
267 "Can't create folder."), \
271 if (make_dir(dir) < 0) \
276 static MainWindow *static_mainwindow;
279 static osso_context_t *static_osso_context;
281 void exit_event_handler(gboolean die_now, gpointer data)
284 appdata = (AppData *) data;
285 /* Do whatever application needs to do before exiting */
286 exit_claws(static_mainwindow);
287 hildon_banner_show_information(GTK_WIDGET(appdata->window), NULL,
292 /* Callback for hardware D-BUS events */
293 void hw_event_handler(osso_hw_state_t *state, gpointer data)
296 appdata = (AppData *) data;
298 if (state->shutdown_ind) {
299 exit_claws(static_mainwindow);
300 hildon_banner_show_information(GTK_WIDGET(appdata->window), NULL,
305 /* Callback for normal D-BUS messages */
306 gint dbus_req_handler(const gchar * interface, const gchar * method,
307 GArray * arguments, gpointer data,
311 appdata = (AppData *) data;
313 if (!strcmp(method, "top_application")) {
314 osso_rpc_free_val(retval);
317 osso_system_note_infoprint(appdata->osso_context, method, retval);
318 osso_rpc_free_val(retval);
323 static gboolean emergency_exit = FALSE;
325 #ifdef HAVE_STARTUP_NOTIFICATION
326 static void sn_error_trap_push(SnDisplay *display, Display *xdisplay)
328 gdk_error_trap_push();
331 static void sn_error_trap_pop(SnDisplay *display, Display *xdisplay)
333 gdk_error_trap_pop();
336 static void startup_notification_complete(gboolean with_window)
339 GtkWidget *hack = NULL;
342 /* this is needed to make the startup notification leave,
343 * if we have been launched from a menu.
344 * We have to display a window, so let it be very little */
345 hack = gtk_window_new(GTK_WINDOW_POPUP);
346 gtk_window_move(GTK_WINDOW(hack), 0, 0);
347 gtk_widget_set_size_request(hack, 1, 1);
348 gtk_widget_show(hack);
351 xdisplay = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
352 sn_display = sn_display_new(xdisplay,
355 sn_context = sn_launchee_context_new_from_environment(sn_display,
356 DefaultScreen(xdisplay));
358 if (sn_context != NULL) {
359 sn_launchee_context_complete(sn_context);
360 sn_launchee_context_unref(sn_context);
361 sn_display_unref(sn_display);
364 gtk_widget_destroy(hack);
367 #endif /* HAVE_STARTUP_NOTIFICATION */
369 static void claws_gtk_idle(void)
371 while(gtk_events_pending()) {
372 gtk_main_iteration();
377 static gboolean sc_starting = FALSE;
379 static gboolean defer_check_all(void *data)
381 gboolean autochk = GPOINTER_TO_INT(data);
383 inc_all_account_mail(static_mainwindow, autochk,
384 prefs_common.newmail_notify_manu);
388 main_window_set_menu_sensitive(static_mainwindow);
389 toolbar_main_set_sensitive(static_mainwindow);
394 static gboolean defer_check(void *data)
396 inc_mail(static_mainwindow, prefs_common.newmail_notify_manu);
400 main_window_set_menu_sensitive(static_mainwindow);
401 toolbar_main_set_sensitive(static_mainwindow);
406 static gboolean defer_jump(void *data)
408 if (cmd.receive_all) {
409 defer_check_all(GINT_TO_POINTER(FALSE));
410 } else if (prefs_common.chk_on_startup) {
411 defer_check_all(GINT_TO_POINTER(TRUE));
412 } else if (cmd.receive) {
415 mainwindow_jump_to(data, FALSE);
418 main_window_set_menu_sensitive(static_mainwindow);
419 toolbar_main_set_sensitive(static_mainwindow);
424 static void chk_update_val(GtkWidget *widget, gpointer data)
426 gboolean *val = (gboolean *)data;
427 *val = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
430 static gboolean migrate_old_config(const gchar *old_cfg_dir, const gchar *new_cfg_dir, const gchar *oldversion)
432 gchar *message = g_strdup_printf(_("Configuration for %s found.\n"
433 "Do you want to migrate this configuration?"), oldversion);
434 gchar *message2 = g_strdup_printf(_("\n\nYour Sylpheed filtering rules can be converted by a\n"
435 "script available at %s."), TOOLS_URI);
437 if (!strcmp(oldversion, "Sylpheed"))
438 message = g_strconcat(message, message2, NULL);
442 GtkWidget *window = NULL;
443 GtkWidget *keep_backup_chk;
444 gboolean backup = TRUE;
446 keep_backup_chk = gtk_check_button_new_with_label (_("Keep old configuration"));
447 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_backup_chk), TRUE);
448 CLAWS_SET_TIP(keep_backup_chk,
449 _("Keeping a backup will allow you to go back to an "
450 "older version, but may take a while if you have "
451 "cached IMAP or News data, and will take some extra "
452 "room on your disk."));
454 g_signal_connect(G_OBJECT(keep_backup_chk), "toggled",
455 G_CALLBACK(chk_update_val), &backup);
457 if (alertpanel_full(_("Migration of configuration"), message,
458 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL, FALSE,
459 keep_backup_chk, ALERT_QUESTION, G_ALERTDEFAULT) != G_ALERTALTERNATE) {
463 /* we can either do a fast migration requiring not any extra disk
464 * space, or a slow one that copies the old configuration and leaves
468 window = label_window_create(_("Copying configuration... This may take a while..."));
471 r = copy_dir(old_cfg_dir, new_cfg_dir);
472 label_window_destroy(window);
474 /* if copy failed, we'll remove the partially copied
477 alertpanel_error(_("Migration failed!"));
478 remove_dir_recursive(new_cfg_dir);
481 /* fast mode failed, but we don't want backup */
482 remove_dir_recursive(old_cfg_dir);
486 window = label_window_create(_("Migrating configuration..."));
489 r = g_rename(old_cfg_dir, new_cfg_dir);
490 label_window_destroy(window);
492 /* if g_rename failed, we'll try to copy */
494 FILE_OP_ERROR(new_cfg_dir, "g_rename failed, trying copy\n");
501 static int migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
504 gchar *plugin_path, *old_plugin_path, *new_plugin_path;
506 gboolean err = FALSE;
508 oldfp = g_fopen(old_rc, "r");
511 newfp = g_fopen(new_rc, "w");
517 plugin_path = g_strdup(get_plugin_dir());
518 new_plugin_path = g_strdup(plugin_path);
520 if (strstr(plugin_path, "/claws-mail/")) {
521 gchar *end = g_strdup(strstr(plugin_path, "/claws-mail/")+strlen("/claws-mail/"));
522 *(strstr(plugin_path, "/claws-mail/")) = '\0';
523 old_plugin_path = g_strconcat(plugin_path, "/sylpheed-claws/", end, NULL);
526 old_plugin_path = g_strdup(new_plugin_path);
528 debug_print("replacing %s with %s\n", old_plugin_path, new_plugin_path);
529 while (fgets(buf, sizeof(buf), oldfp)) {
530 if (strncmp(buf, old_plugin_path, strlen(old_plugin_path))) {
531 err |= (fputs(buf, newfp) == EOF);
533 debug_print("->replacing %s", buf);
534 debug_print(" with %s%s", new_plugin_path, buf+strlen(old_plugin_path));
535 err |= (fputs(new_plugin_path, newfp) == EOF);
536 err |= (fputs(buf+strlen(old_plugin_path), newfp) == EOF);
540 g_free(new_plugin_path);
541 g_free(old_plugin_path);
543 if (fclose(newfp) == EOF)
551 sc_client_set_value (MainWindow *mainwin,
562 prop.num_vals = num_vals;
566 if (mainwin->smc_conn)
567 SmcSetProperties ((SmcConn) mainwin->smc_conn, 1, proplist);
570 static void sc_die_callback (SmcConn smc_conn, SmPointer client_data)
575 static void sc_save_complete_callback(SmcConn smc_conn, SmPointer client_data)
579 static void sc_shutdown_cancelled_callback (SmcConn smc_conn, SmPointer client_data)
581 MainWindow *mainwin = (MainWindow *)client_data;
582 if (mainwin->smc_conn)
583 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
586 static void sc_save_yourself_callback (SmcConn smc_conn,
587 SmPointer client_data,
593 MainWindow *mainwin = (MainWindow *)client_data;
594 if (mainwin->smc_conn)
595 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
598 static IceIOErrorHandler sc_ice_installed_handler;
600 static void sc_ice_io_error_handler (IceConn connection)
602 if (sc_ice_installed_handler)
603 (*sc_ice_installed_handler) (connection);
605 static gboolean sc_process_ice_messages (GIOChannel *source,
606 GIOCondition condition,
609 IceConn connection = (IceConn) data;
610 IceProcessMessagesStatus status;
612 status = IceProcessMessages (connection, NULL, NULL);
614 if (status == IceProcessMessagesIOError) {
615 IcePointer context = IceGetConnectionContext (connection);
617 if (context && GTK_IS_OBJECT (context)) {
618 guint disconnect_id = g_signal_lookup ("disconnect", G_OBJECT_TYPE (context));
620 if (disconnect_id > 0)
621 g_signal_emit (context, disconnect_id, 0);
623 IceSetShutdownNegotiation (connection, False);
624 IceCloseConnection (connection);
631 static void new_ice_connection (IceConn connection, IcePointer client_data, Bool opening,
632 IcePointer *watch_data)
638 /* Make sure we don't pass on these file descriptors to any
640 fcntl(IceConnectionNumber(connection),F_SETFD,
641 fcntl(IceConnectionNumber(connection),F_GETFD,0) | FD_CLOEXEC);
643 channel = g_io_channel_unix_new (IceConnectionNumber (connection));
644 input_id = g_io_add_watch (channel,
645 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
646 sc_process_ice_messages,
648 g_io_channel_unref (channel);
650 *watch_data = (IcePointer) GUINT_TO_POINTER (input_id);
652 input_id = GPOINTER_TO_UINT ((gpointer) *watch_data);
653 g_source_remove (input_id);
657 static void sc_session_manager_connect(MainWindow *mainwin)
659 static gboolean connected = FALSE;
660 SmcCallbacks callbacks;
662 IceIOErrorHandler default_handler;
669 sc_ice_installed_handler = IceSetIOErrorHandler (NULL);
670 default_handler = IceSetIOErrorHandler (sc_ice_io_error_handler);
672 if (sc_ice_installed_handler == default_handler)
673 sc_ice_installed_handler = NULL;
675 IceAddConnectionWatch (new_ice_connection, NULL);
678 callbacks.save_yourself.callback = sc_save_yourself_callback;
679 callbacks.die.callback = sc_die_callback;
680 callbacks.save_complete.callback = sc_save_complete_callback;
681 callbacks.shutdown_cancelled.callback = sc_shutdown_cancelled_callback;
683 callbacks.save_yourself.client_data =
684 callbacks.die.client_data =
685 callbacks.save_complete.client_data =
686 callbacks.shutdown_cancelled.client_data = (SmPointer) mainwin;
687 if (g_getenv ("SESSION_MANAGER")) {
688 gchar error_string_ret[256] = "";
690 mainwin->smc_conn = (gpointer)
691 SmcOpenConnection (NULL, mainwin,
692 SmProtoMajor, SmProtoMinor,
693 SmcSaveYourselfProcMask | SmcDieProcMask |
694 SmcSaveCompleteProcMask |
695 SmcShutdownCancelledProcMask,
698 256, error_string_ret);
700 if (error_string_ret[0] || mainwin->smc_conn == NULL)
701 g_warning ("While connecting to session manager:\n%s.",
705 vals = g_new (SmPropValue, 1);
706 vals[0].length = strlen(argv0);
707 vals[0].value = argv0;
708 sc_client_set_value (mainwin, SmCloneCommand, SmLISTofARRAY8, 1, vals);
709 sc_client_set_value (mainwin, SmRestartCommand, SmLISTofARRAY8, 1, vals);
710 sc_client_set_value (mainwin, SmProgram, SmARRAY8, 1, vals);
712 vals[0].length = strlen(g_get_user_name()?g_get_user_name():"");
713 vals[0].value = g_strdup(g_get_user_name()?g_get_user_name():"");
714 sc_client_set_value (mainwin, SmUserID, SmARRAY8, 1, vals);
720 static gboolean sc_exiting = FALSE;
721 static gboolean show_at_startup = TRUE;
722 static gboolean claws_crashed_bool = FALSE;
724 gboolean claws_crashed(void) {
725 return claws_crashed_bool;
728 void main_set_show_at_startup(gboolean show)
730 show_at_startup = show;
734 static void main_vol_mount_cb(GnomeVFSVolumeMonitor *vfs, GnomeVFSVolume *vol, MainWindow *mainwin)
736 gchar *uri = gnome_vfs_volume_get_activation_uri (vol);
737 gchar *mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
740 if(!strcmp(mount_path, prefs_common.data_root)) {
741 gtk_widget_set_sensitive(mainwin->window, TRUE);
747 static void main_vol_unmount_cb(GnomeVFSVolumeMonitor *vfs, GnomeVFSVolume *vol, MainWindow *mainwin)
749 gchar *uri = gnome_vfs_volume_get_activation_uri (vol);
750 gchar *mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
753 if(!strcmp(mount_path, prefs_common.data_root)) {
754 gtk_widget_set_sensitive(mainwin->window, FALSE);
763 static FILE* win32_debug_fp=NULL;
764 static guint win32_log_handler_app_id;
765 static guint win32_log_handler_glib_id;
766 static guint win32_log_handler_gtk_id;
768 static void win32_print_stdout(const gchar *string)
770 if (win32_debug_fp) {
771 fprintf(win32_debug_fp, string);
772 fflush(win32_debug_fp);
776 static void win32_print_stderr(const gchar *string)
778 if (win32_debug_fp) {
779 fprintf(win32_debug_fp, string);
780 fflush(win32_debug_fp);
784 static void win32_log(const gchar *log_domain, GLogLevelFlags log_level, const gchar* message, gpointer user_data)
786 if (win32_debug_fp) {
789 switch(log_level & G_LOG_LEVEL_MASK)
791 case G_LOG_LEVEL_ERROR:
794 case G_LOG_LEVEL_CRITICAL:
797 case G_LOG_LEVEL_WARNING:
800 case G_LOG_LEVEL_MESSAGE:
803 case G_LOG_LEVEL_INFO:
806 case G_LOG_LEVEL_DEBUG:
813 fprintf(win32_debug_fp, "%s: %s: %s", log_domain, type, message);
815 fprintf(win32_debug_fp, "%s: %s", type, message);
816 fflush(win32_debug_fp);
820 static void win32_open_log(void)
822 gchar *logfile = g_strconcat(g_get_tmp_dir(), G_DIR_SEPARATOR_S, "claws-win32.log", NULL);
823 gchar *oldlogfile = g_strconcat(g_get_tmp_dir(), G_DIR_SEPARATOR_S, "claws-win32.log.bak", NULL);
825 if (is_file_exist(logfile)) {
826 if (rename_force(logfile, oldlogfile) < 0)
827 FILE_OP_ERROR(logfile, "rename");
829 win32_debug_fp = g_fopen(logfile, "w");
834 g_set_print_handler(win32_print_stdout);
835 g_set_printerr_handler(win32_print_stdout);
836 win32_log_handler_app_id = g_log_set_handler(NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
837 | G_LOG_FLAG_RECURSION, win32_log, NULL);
838 win32_log_handler_glib_id = g_log_set_handler("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
839 | G_LOG_FLAG_RECURSION, win32_log, NULL);
840 win32_log_handler_gtk_id = g_log_set_handler("Gtk", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
841 | G_LOG_FLAG_RECURSION, win32_log, NULL);
845 static void win32_close_log(void)
849 g_log_remove_handler("", win32_log_handler_app_id);
850 g_log_remove_handler("GLib", win32_log_handler_glib_id);
851 g_log_remove_handler("Gtk", win32_log_handler_gtk_id);
852 fclose(win32_debug_fp);
858 static void main_dump_features_list(gboolean show_debug_only)
859 /* display compiled-in features list */
861 if (show_debug_only && !debug_get_mode())
865 debug_print("runtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
866 gtk_major_version, gtk_minor_version, gtk_micro_version,
867 glib_major_version, glib_minor_version, glib_micro_version);
869 g_print("runtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
870 gtk_major_version, gtk_minor_version, gtk_micro_version,
871 glib_major_version, glib_minor_version, glib_micro_version);
873 debug_print("buildtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
874 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
875 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
877 g_print("buildtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
878 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
879 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
882 debug_print("Compiled-in features:\n");
884 g_print("Compiled-in features:\n");
887 debug_print(" compface\n");
889 g_print(" compface\n");
893 debug_print(" aspell\n");
895 g_print(" aspell\n");
899 debug_print(" gnutls\n");
901 g_print(" gnutls\n");
905 debug_print(" ipv6\n");
911 debug_print(" iconv\n");
917 debug_print(" jpilot\n");
919 g_print(" jpilot\n");
923 debug_print(" ldap\n");
929 debug_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
931 g_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
935 debug_print(" libsm\n");
939 #if HAVE_NETWORKMANAGER_SUPPORT
941 debug_print(" NetworkManager\n");
943 g_print(" NetworkManager\n");
947 #ifdef HAVE_DBUS_GLIB
948 static guint dbus_item_hook_id = -1;
949 static guint dbus_folder_hook_id = -1;
951 static void uninstall_dbus_status_handler(void)
954 g_object_unref(awn_proxy);
956 if (dbus_item_hook_id != -1)
957 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, dbus_item_hook_id);
958 if (dbus_folder_hook_id != -1)
959 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, dbus_folder_hook_id);
962 static void dbus_update(FolderItem *removed_item)
964 guint new, unread, unreadmarked, marked, total;
965 guint replied, forwarded, locked, ignored, watched;
967 GError *error = NULL;
969 folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total,
970 &replied, &forwarded, &locked, &ignored,
973 total -= removed_item->total_msgs;
974 new -= removed_item->new_msgs;
975 unread -= removed_item->unread_msgs;
979 buf = g_strdup_printf("%d", new);
980 dbus_g_proxy_call(awn_proxy, "SetInfoByName", &error,
981 G_TYPE_STRING, "claws-mail",
983 G_TYPE_INVALID, G_TYPE_INVALID);
987 dbus_g_proxy_call(awn_proxy, "UnsetInfoByName", &error, G_TYPE_STRING,
988 "claws-mail", G_TYPE_INVALID, G_TYPE_INVALID);
991 debug_print("%s\n", error->message);
996 static gboolean dbus_status_update_folder_hook(gpointer source, gpointer data)
998 FolderUpdateData *hookdata;
1000 if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
1001 dbus_update(hookdata->item);
1008 static gboolean dbus_status_update_item_hook(gpointer source, gpointer data)
1015 static void install_dbus_status_handler(void)
1017 GError *tmp_error = NULL;
1018 DBusGConnection *connection = dbus_g_bus_get(DBUS_BUS_SESSION, &tmp_error);
1021 /* If calling code doesn't do error checking, at least print some debug */
1022 debug_print("Failed to open connection to session bus: %s\n",
1023 tmp_error->message);
1024 g_error_free(tmp_error);
1027 awn_proxy = dbus_g_proxy_new_for_name(connection,
1028 "com.google.code.Awn",
1029 "/com/google/code/Awn",
1030 "com.google.code.Awn");
1031 dbus_item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, dbus_status_update_item_hook, NULL);
1032 if (dbus_item_hook_id == -1) {
1033 g_warning(_("Failed to register folder item update hook"));
1034 uninstall_dbus_status_handler();
1038 dbus_folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, dbus_status_update_folder_hook, NULL);
1039 if (dbus_folder_hook_id == -1) {
1040 g_warning(_("Failed to register folder update hook"));
1041 uninstall_dbus_status_handler();
1047 static void reset_statistics(void)
1049 /* (re-)initialize session statistics */
1050 session_stats.received = 0;
1051 session_stats.sent = 0;
1052 session_stats.replied = 0;
1053 session_stats.forwarded = 0;
1054 session_stats.time_started = time(NULL);
1057 int main(int argc, char *argv[])
1060 osso_context_t *osso_context;
1061 osso_return_t result;
1063 #ifdef HAVE_DBUS_GLIB
1064 DBusGConnection *connection;
1067 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1068 DBusGProxy *nm_proxy;
1071 MainWindow *mainwin;
1072 FolderView *folderview;
1074 gboolean crash_file_present = FALSE;
1075 gint num_folder_class = 0;
1076 gboolean asked_for_migration = FALSE;
1077 gboolean start_done = TRUE;
1078 GSList *plug_list = NULL;
1079 gboolean never_ran = FALSE;
1080 gboolean mainwin_shown = FALSE;
1082 START_TIMING("startup");
1089 if (!claws_init(&argc, &argv)) {
1096 prog_version = PROG_VERSION;
1097 argv0 = g_strdup(argv[0]);
1099 parse_cmd_opt(argc, argv);
1103 /* check and create unix domain socket for remote operation */
1104 lock_socket = prohibit_duplicate_launch();
1105 if (lock_socket < 0) {
1106 #ifdef HAVE_STARTUP_NOTIFICATION
1107 if(gtk_init_check(&argc, &argv))
1108 startup_notification_complete(TRUE);
1113 main_dump_features_list(TRUE);
1114 prefs_prepare_cache();
1119 gtk_init(&argc, &argv);
1120 crash_main(cmd.crash_params);
1126 crash_install_handlers();
1128 install_basic_sighandlers();
1129 #if (defined linux && defined SIGIO)
1130 install_memory_sighandler();
1133 if (cmd.status || cmd.status_full || cmd.search ||
1134 cmd.statistics || cmd.reset_statistics) {
1135 puts("0 Claws Mail not running.");
1136 lock_socket_remove();
1142 if (!g_thread_supported())
1143 g_thread_init(NULL);
1148 gtk_init(&argc, &argv);
1151 gtk_settings_set_string_property(gtk_settings_get_default(),
1157 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1158 went_offline_nm = FALSE;
1161 #ifdef HAVE_DBUS_GLIB
1163 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
1166 debug_print("Failed to open connection to system bus: %s\n", error->message);
1167 g_error_free(error);
1170 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1171 nm_proxy = dbus_g_proxy_new_for_name(connection,
1172 "org.freedesktop.NetworkManager",
1173 "/org/freedesktop/NetworkManager",
1174 "org.freedesktop.NetworkManager");
1176 #if NM_CHECK_VERSION(0,8,992)
1177 dbus_g_proxy_add_signal(nm_proxy, "StateChanged", G_TYPE_UINT, G_TYPE_INVALID);
1178 dbus_g_proxy_connect_signal(nm_proxy, "StateChanged",
1179 G_CALLBACK(networkmanager_state_change_cb),
1182 dbus_g_proxy_add_signal(nm_proxy, "StateChange", G_TYPE_UINT, G_TYPE_INVALID);
1183 dbus_g_proxy_connect_signal(nm_proxy, "StateChange",
1184 G_CALLBACK(networkmanager_state_change_cb),
1189 install_dbus_status_handler();
1195 osso_context = osso_initialize(OSSO_SERVICE, "2.8.1", TRUE, NULL);
1196 if (osso_context == NULL) {
1199 hildon_program = HILDON_PROGRAM(hildon_program_get_instance());
1200 static_osso_context = osso_context;
1202 gtk_widget_set_default_colormap(
1203 gdk_screen_get_system_colormap(
1204 gdk_screen_get_default()));
1206 gtkut_create_ui_manager();
1208 /* Create container for all the menus we will be adding */
1209 MENUITEM_ADDUI("/", "Menus", NULL, GTK_UI_MANAGER_MENUBAR);
1211 if (!g_thread_supported()) {
1212 g_error(_("g_thread is not supported by glib.\n"));
1216 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1218 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1221 /* no config dir exists. See if we can migrate an old config. */
1222 if (!is_dir_exist(RC_DIR)) {
1223 prefs_destroy_cache();
1226 /* if one of the old dirs exist, we'll ask if the user
1227 * want to migrates, and r will be TRUE if he said yes
1228 * and migration succeeded, and FALSE otherwise.
1230 if (is_dir_exist(OLD_GTK2_RC_DIR)) {
1231 r = migrate_old_config(OLD_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 2.6.0 (or older)"));
1232 asked_for_migration = TRUE;
1233 } else if (is_dir_exist(OLDER_GTK2_RC_DIR)) {
1234 r = migrate_old_config(OLDER_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.9.15 (or older)"));
1235 asked_for_migration = TRUE;
1236 } else if (is_dir_exist(OLD_GTK1_RC_DIR)) {
1237 r = migrate_old_config(OLD_GTK1_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.0.5 (or older)"));
1238 asked_for_migration = TRUE;
1239 } else if (is_dir_exist(SYLPHEED_RC_DIR)) {
1240 r = migrate_old_config(SYLPHEED_RC_DIR, RC_DIR, "Sylpheed");
1241 asked_for_migration = TRUE;
1244 /* If migration failed or the user didn't want to do it,
1245 * we create a new one (and we'll hit wizard later).
1247 if (r == FALSE && !is_dir_exist(RC_DIR)) {
1249 if (copy_dir(SYSCONFDIR "/skel/.claws-mail", RC_DIR) < 0) {
1251 if (!is_dir_exist(RC_DIR) && make_dir(RC_DIR) < 0) {
1264 if (!is_file_exist(RC_DIR G_DIR_SEPARATOR_S COMMON_RC) &&
1265 is_file_exist(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC)) {
1266 /* post 2.6 name change */
1267 migrate_common_rc(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC,
1268 RC_DIR G_DIR_SEPARATOR_S COMMON_RC);
1272 plugin_load_all("Common");
1274 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
1275 gtk_rc_parse(userrc);
1278 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1279 gtk_accel_map_load (userrc);
1283 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_rc_dir(), 1, win32_close_log(););
1285 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
1288 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
1289 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
1290 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
1291 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
1292 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
1293 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR);
1295 crash_file_present = is_file_exist(get_crashfile_name());
1296 /* remove temporary files */
1297 remove_all_files(get_tmp_dir());
1298 remove_all_files(get_mime_tmp_dir());
1300 if (!cmd.crash && crash_file_present)
1301 claws_crashed_bool = TRUE;
1303 if (is_file_exist("claws.log")) {
1304 if (rename_force("claws.log", "claws.log.bak") < 0)
1305 FILE_OP_ERROR("claws.log", "rename");
1307 set_log_file(LOG_PROTOCOL, "claws.log");
1309 if (is_file_exist("filtering.log")) {
1310 if (rename_force("filtering.log", "filtering.log.bak") < 0)
1311 FILE_OP_ERROR("filtering.log", "rename");
1313 set_log_file(LOG_DEBUG_FILTERING, "filtering.log");
1316 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1318 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1321 folder_system_init();
1322 prefs_common_read_config();
1324 prefs_themes_init();
1326 prefs_ext_prog_init();
1327 prefs_wrapping_init();
1328 prefs_compose_writing_init();
1329 prefs_msg_colors_init();
1330 image_viewer_init();
1331 prefs_image_viewer_init();
1333 prefs_summaries_init();
1334 prefs_message_init();
1336 prefs_logging_init();
1337 prefs_receive_init();
1342 gtkaspell_checkers_init();
1343 prefs_spelling_init();
1346 sock_set_io_timeout(prefs_common.io_timeout_secs);
1347 prefs_actions_read_config();
1348 prefs_display_header_read_config();
1349 /* prefs_filtering_read_config(); */
1350 #ifndef USE_NEW_ADDRBOOK
1351 addressbook_read_file();
1353 g_clear_error(&error);
1354 if (! addressbook_start_service(&error)) {
1355 g_warning("%s", error->message);
1356 g_clear_error(&error);
1359 addressbook_install_hooks(&error);
1362 gtkut_widget_init();
1363 stock_pixbuf_gdk(NULL, STOCK_PIXMAP_CLAWS_MAIL_ICON, &icon);
1364 gtk_window_set_default_icon(icon);
1366 folderview_initialize();
1372 mainwin = main_window_create();
1374 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1375 networkmanager_state_change_cb(nm_proxy,NULL,mainwin);
1380 appdata = g_new0(AppData, 1);
1381 appdata->program = hildon_program;
1382 appdata->window = HILDON_WINDOW(mainwin->window);
1383 appdata->osso_context = osso_context;
1384 result = osso_rpc_set_cb_f(appdata->osso_context,
1388 dbus_req_handler, appdata);
1389 if (result != OSSO_OK) {
1394 /* Add handler for Exit D-BUS messages */
1395 result = osso_application_set_exit_cb(appdata->osso_context,
1397 (gpointer) appdata);
1398 if (result != OSSO_OK) {
1402 osso_hw_set_event_cb( appdata->osso_context,
1403 NULL, hw_event_handler, (gpointer) appdata );
1405 manage_window_focus_in(mainwin->window, NULL, NULL);
1406 folderview = mainwin->folderview;
1408 gtk_cmclist_freeze(GTK_CMCLIST(mainwin->folderview->ctree));
1409 folder_item_update_freeze();
1411 prefs_account_init();
1412 account_read_config_all();
1414 #ifdef HAVE_LIBETPAN
1415 imap_main_init(prefs_common.skip_ssl_cert_check);
1416 imap_main_set_timeout(prefs_common.io_timeout_secs);
1417 nntp_main_init(prefs_common.skip_ssl_cert_check);
1419 /* If we can't read a folder list or don't have accounts,
1420 * it means the configuration's not done. Either this is
1421 * a brand new install, either a failed/refused migration.
1422 * So we'll start the wizard.
1424 if (folder_read_list() < 0) {
1425 prefs_destroy_cache();
1427 /* if run_wizard returns FALSE it's because it's
1428 * been cancelled. We can't do much but exit.
1429 * however, if the user was asked for a migration,
1430 * we remove the newly created directory so that
1431 * he's asked again for migration on next launch.*/
1432 if (!run_wizard(mainwin, TRUE)) {
1433 if (asked_for_migration)
1434 remove_dir_recursive(RC_DIR);
1440 main_window_reflect_prefs_all_now();
1441 folder_write_list();
1445 if (!account_get_list()) {
1446 prefs_destroy_cache();
1447 if (!run_wizard(mainwin, FALSE)) {
1448 if (asked_for_migration)
1449 remove_dir_recursive(RC_DIR);
1455 if(!account_get_list()) {
1456 exit_claws(mainwin);
1466 toolbar_main_set_sensitive(mainwin);
1467 main_window_set_menu_sensitive(mainwin);
1469 /* if crashed, show window early so that the user
1470 * sees what's happening */
1471 if (claws_crashed()) {
1472 main_window_popup(mainwin);
1473 mainwin_shown = TRUE;
1476 account_set_missing_folder();
1477 folder_set_missing_folders();
1478 folderview_set(folderview);
1480 prefs_matcher_read_config();
1481 quicksearch_set_search_strings(mainwin->summaryview->quicksearch);
1483 /* make one all-folder processing before using claws */
1484 main_window_cursor_wait(mainwin);
1485 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
1487 /* if claws crashed, rebuild caches */
1488 if (claws_crashed()) {
1490 debug_print("Claws Mail crashed, checking for new messages in local folders\n");
1491 folder_item_update_thaw();
1492 folderview_check_new(NULL);
1493 folder_clean_cache_memory_force();
1494 folder_item_update_freeze();
1496 /* make the crash-indicator file */
1497 str_write_to_file("foo", get_crashfile_name());
1499 inc_autocheck_timer_init(mainwin);
1501 /* ignore SIGPIPE signal for preventing sudden death of program */
1503 signal(SIGPIPE, SIG_IGN);
1505 if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
1506 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1508 if (cmd.online_mode == ONLINE_MODE_ONLINE) {
1509 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1512 if (cmd.status_folders) {
1513 g_ptr_array_free(cmd.status_folders, TRUE);
1514 cmd.status_folders = NULL;
1516 if (cmd.status_full_folders) {
1517 g_ptr_array_free(cmd.status_full_folders, TRUE);
1518 cmd.status_full_folders = NULL;
1521 claws_register_idle_function(claws_gtk_idle);
1523 prefs_toolbar_init();
1525 num_folder_class = g_list_length(folder_get_list());
1527 plugin_load_all("GTK2");
1529 if (g_list_length(folder_get_list()) != num_folder_class) {
1530 debug_print("new folders loaded, reloading processing rules\n");
1531 prefs_matcher_read_config();
1534 if ((plug_list = plugin_get_unloaded_list()) != NULL) {
1537 gint num_plugins = 0;
1538 for (cur = plug_list; cur; cur = cur->next) {
1539 Plugin *plugin = (Plugin *)cur->data;
1540 gchar *tmp = g_strdup_printf("%s\n%s",
1542 plugin_get_name(plugin));
1547 main_window_cursor_normal(mainwin);
1548 main_window_popup(mainwin);
1549 mainwin_shown = TRUE;
1550 alertpanel_warning(ngettext(
1551 "The following plugin failed to load. "
1552 "Check the Plugins configuration "
1553 "for more information:\n%s",
1554 "The following plugins failed to load. "
1555 "Check the Plugins configuration "
1556 "for more information:\n%s",
1559 main_window_cursor_wait(mainwin);
1561 g_slist_free(plug_list);
1565 prefs_common_write_config();
1566 plugin_load_standard_plugins ();
1568 /* if not crashed, show window now */
1569 if (!mainwin_shown) {
1570 /* apart if something told not to show */
1571 if (show_at_startup)
1572 main_window_popup(mainwin);
1575 if (!folder_have_mailbox()) {
1576 prefs_destroy_cache();
1577 main_window_cursor_normal(mainwin);
1578 if (folder_get_list() != NULL) {
1579 alertpanel_error(_("Claws Mail has detected a configured "
1580 "mailbox, but it is incomplete. It is "
1581 "possibly due to a failing IMAP account. Use "
1582 "\"Rebuild folder tree\" on the mailbox parent "
1583 "folder's context menu to try to fix it."));
1585 alertpanel_error(_("Claws Mail has detected a configured "
1586 "mailbox, but could not load it. It is "
1587 "probably provided by an out-of-date "
1588 "external plugin. Please reinstall the "
1589 "plugin and try again."));
1590 exit_claws(mainwin);
1598 static_mainwindow = mainwin;
1601 if (prefs_common.data_root != NULL && *prefs_common.data_root != '\0') {
1602 GnomeVFSVolume *vol = NULL;
1603 gchar *uri, *mount_path;
1605 volmon = gnome_vfs_get_volume_monitor();
1606 vol = gnome_vfs_volume_monitor_get_volume_for_path(volmon, prefs_common.data_root);
1608 uri = gnome_vfs_volume_get_activation_uri (vol);
1609 mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
1612 if (vol == NULL || !gnome_vfs_volume_is_mounted(vol)
1613 || strcmp(mount_path, prefs_common.data_root)) {
1614 alertpanel_error(_("Claws Mail can not start without its data volume (%s)."),
1615 prefs_common.data_root);
1617 gnome_vfs_volume_unref(vol);
1618 exit_claws(mainwin);
1622 gnome_vfs_volume_unref(vol);
1623 g_signal_connect(G_OBJECT(volmon),
1624 "volume-mounted", G_CALLBACK(main_vol_mount_cb), mainwin);
1625 g_signal_connect(G_OBJECT(volmon),
1626 "volume-unmounted", G_CALLBACK(main_vol_unmount_cb), mainwin);
1630 #ifdef HAVE_STARTUP_NOTIFICATION
1631 startup_notification_complete(FALSE);
1634 sc_session_manager_connect(mainwin);
1637 folder_item_update_thaw();
1638 gtk_cmclist_thaw(GTK_CMCLIST(mainwin->folderview->ctree));
1639 main_window_cursor_normal(mainwin);
1641 if (!cmd.target && prefs_common.goto_last_folder_on_startup &&
1642 folder_find_item_from_identifier(prefs_common.last_opened_folder) != NULL &&
1644 cmd.target = prefs_common.last_opened_folder;
1647 if (cmd.receive_all && !cmd.target) {
1649 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(FALSE));
1650 } else if (prefs_common.chk_on_startup && !cmd.target) {
1652 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(TRUE));
1653 } else if (cmd.receive && !cmd.target) {
1655 g_timeout_add(1000, defer_check, NULL);
1657 gtk_widget_grab_focus(folderview->ctree);
1660 open_compose_new(cmd.compose_mailto, cmd.attach_files);
1662 if (cmd.attach_files) {
1663 list_free_strings(cmd.attach_files);
1664 g_list_free(cmd.attach_files);
1665 cmd.attach_files = NULL;
1667 if (cmd.subscribe) {
1668 folder_subscribe(cmd.subscribe_uri);
1677 g_timeout_add(500, defer_jump, (gpointer)cmd.target);
1680 prefs_destroy_cache();
1682 compose_reopen_exit_drafts();
1685 sc_starting = FALSE;
1686 main_window_set_menu_sensitive(mainwin);
1687 toolbar_main_set_sensitive(mainwin);
1690 /* register the callback of unix domain socket input */
1691 lock_socket_tag = claws_input_add(lock_socket,
1692 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
1693 lock_socket_input_cb,
1701 osso_deinitialize(osso_context);
1703 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1705 g_object_unref(nm_proxy);
1707 #ifdef HAVE_DBUS_GLIB
1708 uninstall_dbus_status_handler();
1710 dbus_g_connection_unref(connection);
1716 exit_claws(mainwin);
1721 static void save_all_caches(FolderItem *item, gpointer data)
1728 folder_item_close(item);
1731 folder_item_free_cache(item, TRUE);
1734 static void exit_claws(MainWindow *mainwin)
1737 gboolean have_connectivity;
1741 debug_print("shutting down\n");
1742 inc_autocheck_timer_remove();
1744 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1745 if (prefs_common.work_offline && went_offline_nm)
1746 prefs_common.work_offline = FALSE;
1749 /* save prefs for opened folder */
1750 if(mainwin->folderview->opened) {
1753 item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
1754 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
1755 prefs_common.last_opened_folder = folder_item_get_identifier(item);
1758 /* save all state before exiting */
1759 folder_func_to_all_folders(save_all_caches, NULL);
1760 folder_write_list();
1762 main_window_get_size(mainwin);
1763 main_window_get_position(mainwin);
1765 prefs_common_write_config();
1766 account_write_config_all();
1767 #ifndef USE_NEW_ADDRBOOK
1768 addressbook_export_to_file();
1770 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1771 gtk_accel_map_save(filename);
1774 /* delete temporary files */
1775 remove_all_files(get_tmp_dir());
1776 remove_all_files(get_mime_tmp_dir());
1778 close_log_file(LOG_PROTOCOL);
1779 close_log_file(LOG_DEBUG_FILTERING);
1781 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1782 have_connectivity = networkmanager_is_online(NULL);
1784 have_connectivity = TRUE;
1786 #ifdef HAVE_LIBETPAN
1787 imap_main_done(have_connectivity);
1788 nntp_main_done(have_connectivity);
1790 /* delete crashfile */
1792 claws_unlink(get_crashfile_name());
1794 lock_socket_remove();
1797 if (mainwin->smc_conn)
1798 SmcCloseConnection ((SmcConn)mainwin->smc_conn, 0, NULL);
1799 mainwin->smc_conn = NULL;
1802 main_window_destroy_all();
1804 plugin_unload_all("GTK2");
1807 prefs_toolbar_done();
1809 #ifndef USE_NEW_ADDRBOOK
1810 addressbook_destroy();
1812 prefs_themes_done();
1814 prefs_ext_prog_done();
1815 prefs_wrapping_done();
1816 prefs_compose_writing_done();
1817 prefs_msg_colors_done();
1818 prefs_image_viewer_done();
1819 image_viewer_done();
1821 prefs_summaries_done();
1822 prefs_message_done();
1824 prefs_receive_done();
1825 prefs_logging_done();
1829 prefs_spelling_done();
1830 gtkaspell_checkers_quit();
1832 plugin_unload_all("Common");
1836 #define G_STRING_APPEND_ENCODED_URI(gstring,source) \
1838 gchar tmpbuf[BUFFSIZE]; \
1839 encode_uri(tmpbuf, BUFFSIZE, (source)); \
1840 g_string_append((gstring), tmpbuf); \
1843 #define G_PRINT_EXIT(msg) \
1849 static GString * parse_cmd_compose_from_file(const gchar *fn)
1851 GString *headers = g_string_new(NULL);
1852 GString *body = g_string_new(NULL);
1860 if (fn == NULL || *fn == '\0')
1861 G_PRINT_EXIT(_("Missing filename\n"));
1862 isstdin = (*fn == '-' && *(fn + 1) == '\0');
1866 fp = g_fopen(fn, "r");
1868 G_PRINT_EXIT(_("Cannot open filename for reading\n"));
1871 while (fgets(fb, sizeof(fb), fp)) {
1877 while (*h && *h != ':') { ++h; } /* search colon */
1879 G_PRINT_EXIT(_("Malformed header\n"));
1881 while (*v && *v == ' ') { ++v; } /* trim value start */
1883 tmp = g_ascii_strdown(fb, -1); /* get header name */
1884 if (!strcmp(tmp, "to")) {
1886 G_PRINT_EXIT(_("Duplicated 'To:' header\n"));
1889 g_string_append_c(headers, '&');
1890 g_string_append(headers, tmp);
1891 g_string_append_c(headers, '=');
1892 #if GLIB_CHECK_VERSION(2,16,0)
1893 g_string_append_uri_escaped(headers, v, NULL, TRUE);
1895 G_STRING_APPEND_ENCODED_URI(headers, v);
1901 G_PRINT_EXIT(_("Missing required 'To:' header\n"));
1902 g_string_append(body, to);
1904 g_string_append(body, "?body=");
1905 while (fgets(fb, sizeof(fb), fp)) {
1906 #if GLIB_CHECK_VERSION(2,16,0)
1907 g_string_append_uri_escaped(body, fb, NULL, TRUE);
1909 G_STRING_APPEND_ENCODED_URI(body, fb);
1914 /* append the remaining headers */
1915 g_string_append(body, headers->str);
1916 g_string_free(headers, TRUE);
1921 #undef G_STRING_APPEND_ENCODED_URI
1924 static void parse_cmd_opt(int argc, char *argv[])
1929 for (i = 1; i < argc; i++) {
1930 if (!strncmp(argv[i], "--receive-all", 13)) {
1931 cmd.receive_all = TRUE;
1932 } else if (!strncmp(argv[i], "--receive", 9)) {
1934 } else if (!strncmp(argv[i], "--compose-from-file", 19)) {
1935 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1937 GString *mailto = parse_cmd_compose_from_file(p);
1939 cmd.compose_mailto = mailto->str;
1941 } else if (!strncmp(argv[i], "--compose", 9)) {
1942 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1945 cmd.compose_mailto = NULL;
1946 if (p && *p != '\0' && *p != '-') {
1947 if (!strncmp(p, "mailto:", 7)) {
1948 cmd.compose_mailto = p + 7;
1950 cmd.compose_mailto = p;
1954 } else if (!strncmp(argv[i], "--subscribe", 11)) {
1955 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1956 if (p && *p != '\0' && *p != '-') {
1957 cmd.subscribe = TRUE;
1958 cmd.subscribe_uri = p;
1960 } else if (!strncmp(argv[i], "--attach", 8)) {
1961 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1964 while (p && *p != '\0' && *p != '-') {
1965 if ((file = g_filename_from_uri(p, NULL, NULL)) != NULL) {
1966 if (!is_file_exist(file)) {
1971 if (file == NULL && *p != G_DIR_SEPARATOR) {
1972 file = g_strconcat(claws_get_startup_dir(),
1975 } else if (file == NULL) {
1978 ainfo = g_new0(AttachInfo, 1);
1980 cmd.attach_files = g_list_append(cmd.attach_files, ainfo);
1982 p = (i+1 < argc)?argv[i+1]:NULL;
1984 } else if (!strncmp(argv[i], "--send", 6)) {
1986 } else if (!strncmp(argv[i], "--version-full", 14) ||
1987 !strncmp(argv[i], "-V", 2)) {
1988 g_print("Claws Mail version " VERSION "\n");
1989 main_dump_features_list(FALSE);
1991 } else if (!strncmp(argv[i], "--version", 9) ||
1992 !strncmp(argv[i], "-v", 2)) {
1993 g_print("Claws Mail version " VERSION "\n");
1995 } else if (!strncmp(argv[i], "--status-full", 13)) {
1996 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1998 cmd.status_full = TRUE;
1999 while (p && *p != '\0' && *p != '-') {
2000 if (!cmd.status_full_folders) {
2001 cmd.status_full_folders =
2004 g_ptr_array_add(cmd.status_full_folders,
2007 p = (i+1 < argc)?argv[i+1]:NULL;
2009 } else if (!strncmp(argv[i], "--status", 8)) {
2010 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
2013 while (p && *p != '\0' && *p != '-') {
2014 if (!cmd.status_folders)
2015 cmd.status_folders = g_ptr_array_new();
2016 g_ptr_array_add(cmd.status_folders,
2019 p = (i+1 < argc)?argv[i+1]:NULL;
2021 } else if (!strncmp(argv[i], "--search", 8)) {
2022 cmd.search_folder = (i+1 < argc)?argv[i+1]:NULL;
2023 cmd.search_type = (i+2 < argc)?argv[i+2]:NULL;
2024 cmd.search_request = (i+3 < argc)?argv[i+3]:NULL;
2025 const char* rec = (i+4 < argc)?argv[i+4]:NULL;
2026 cmd.search_recursive = TRUE;
2027 if (rec && (tolower(*rec)=='n' || tolower(*rec)=='f' || *rec=='0'))
2028 cmd.search_recursive = FALSE;
2029 if (cmd.search_folder && cmd.search_type && cmd.search_request)
2031 } else if (!strncmp(argv[i], "--online", 8)) {
2032 cmd.online_mode = ONLINE_MODE_ONLINE;
2033 } else if (!strncmp(argv[i], "--offline", 9)) {
2034 cmd.online_mode = ONLINE_MODE_OFFLINE;
2035 } else if (!strncmp(argv[i], "--statistics", 12)) {
2036 cmd.statistics = TRUE;
2037 } else if (!strncmp(argv[i], "--reset-statistics", 18)) {
2038 cmd.reset_statistics = TRUE;
2039 } else if (!strncmp(argv[i], "--help", 6) ||
2040 !strncmp(argv[i], "-h", 2)) {
2041 gchar *base = g_path_get_basename(argv[0]);
2042 g_print(_("Usage: %s [OPTION]...\n"), base);
2044 g_print("%s\n", _(" --compose [address] open composition window"));
2045 g_print("%s\n", _(" --compose-from-file file\n"
2046 " open composition window with data from given file;\n"
2047 " use - as file name for reading from standard input;\n"
2048 " content format: headers first (To: required) until an\n"
2049 " empty line, then mail body until end of file."));
2050 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
2051 g_print("%s\n", _(" --attach file1 [file2]...\n"
2052 " open composition window with specified files\n"
2054 g_print("%s\n", _(" --receive receive new messages"));
2055 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
2056 g_print("%s\n", _(" --search folder type request [recursive]\n"
2058 " folder ex.: \"#mh/Mailbox/inbox\" or \"Mail\"\n"
2059 " type: s[ubject],f[rom],t[o],e[xtended],m[ixed] or g: tag\n"
2060 " request: search string\n"
2061 " recursive: false if arg. starts with 0, n, N, f or F"));
2063 g_print("%s\n", _(" --send send all queued messages"));
2064 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
2065 g_print("%s\n", _(" --status-full [folder]...\n"
2066 " show the status of each folder"));
2067 g_print("%s\n", _(" --statistics show session statistics"));
2068 g_print("%s\n", _(" --reset-statistics reset session statistics"));
2069 g_print("%s\n", _(" --select folder[/msg] jumps to the specified folder/message\n"
2070 " folder is a folder id like 'folder/sub_folder'"));
2071 g_print("%s\n", _(" --online switch to online mode"));
2072 g_print("%s\n", _(" --offline switch to offline mode"));
2073 g_print("%s\n", _(" --exit --quit -q exit Claws Mail"));
2074 g_print("%s\n", _(" --debug debug mode"));
2075 g_print("%s\n", _(" --help -h display this help and exit"));
2076 g_print("%s\n", _(" --version -v output version information and exit"));
2077 g_print("%s\n", _(" --version-full -V output version and built-in features information and exit"));
2078 g_print("%s\n", _(" --config-dir output configuration directory"));
2079 g_print("%s\n", _(" --alternate-config-dir [dir]\n"
2080 " use specified configuration directory"));
2084 } else if (!strncmp(argv[i], "--crash", 7)) {
2086 cmd.crash_params = g_strdup((i+1 < argc)?argv[i+1]:NULL);
2088 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
2089 g_print(RC_DIR "\n");
2091 } else if (!strncmp(argv[i], "--alternate-config-dir", sizeof "--alternate-config-dir" - 1) && i+1 < argc) {
2092 set_rc_dir(argv[i+1]);
2093 } else if (!strncmp(argv[i], "--exit", 6) ||
2094 !strncmp(argv[i], "--quit", 6) ||
2095 !strncmp(argv[i], "-q", 2)) {
2097 } else if (!strncmp(argv[i], "--select", 8) && i+1 < argc) {
2098 cmd.target = argv[i+1];
2099 } else if (i == 1 && argc == 2) {
2100 /* only one parameter. Do something intelligent about it */
2101 if ((strstr(argv[i], "@")||!strncmp(argv[i], "mailto:", 7)) && !strstr(argv[i], "://")) {
2102 const gchar *p = argv[i];
2105 cmd.compose_mailto = NULL;
2106 if (p && *p != '\0' && *p != '-') {
2107 if (!strncmp(p, "mailto:", 7)) {
2108 cmd.compose_mailto = p + 7;
2110 cmd.compose_mailto = p;
2113 } else if (!strncmp(argv[i], "file://", 7)) {
2114 cmd.target = argv[i];
2115 } else if (!strncmp(argv[i], "?attach=file://", strlen("?attach=file://"))) {
2117 cmd.compose_mailto = argv[i];
2118 } else if (strstr(argv[i], "://")) {
2119 const gchar *p = argv[i];
2120 if (p && *p != '\0' && *p != '-') {
2121 cmd.subscribe = TRUE;
2122 cmd.subscribe_uri = p;
2124 } else if (!strcmp(argv[i], "--sync")) {
2126 } else if (is_dir_exist(argv[i]) || is_file_exist(argv[i])) {
2127 cmd.target = argv[i];
2129 g_print(_("Unknown option\n"));
2135 if (cmd.attach_files && cmd.compose == FALSE) {
2137 cmd.compose_mailto = NULL;
2141 static void initial_processing(FolderItem *item, gpointer data)
2143 MainWindow *mainwin = (MainWindow *)data;
2146 cm_return_if_fail(item);
2147 buf = g_strdup_printf(_("Processing (%s)..."),
2150 : _("top level folder"));
2154 if (item->prefs->enable_processing) {
2155 item->processing_pending = TRUE;
2156 folder_item_apply_processing(item);
2157 item->processing_pending = FALSE;
2160 STATUSBAR_POP(mainwin);
2163 static gboolean draft_all_messages(void)
2165 GList *compose_list = NULL;
2167 compose_clear_exit_drafts();
2168 compose_list = compose_get_compose_list();
2169 while (compose_list != NULL) {
2170 Compose *c = (Compose*)compose_list->data;
2171 if (!compose_draft(c, COMPOSE_DRAFT_FOR_EXIT))
2173 compose_list = compose_get_compose_list();
2177 gboolean clean_quit(gpointer data)
2179 static gboolean firstrun = TRUE;
2186 /*!< Good idea to have the main window stored in a
2187 * static variable so we can check that variable
2188 * to see if we're really allowed to do things
2189 * that actually the spawner is supposed to
2190 * do (like: sending mail, composing messages).
2191 * Because, really, if we're the spawnee, and
2192 * we touch GTK stuff, we're hosed. See the
2195 /* FIXME: Use something else to signal that we're
2196 * in the original spawner, and not in a spawned
2198 if (!static_mainwindow) {
2202 draft_all_messages();
2203 emergency_exit = TRUE;
2204 exit_claws(static_mainwindow);
2210 void app_will_exit(GtkWidget *widget, gpointer data)
2212 MainWindow *mainwin = data;
2214 if (gtk_main_level() == 0) {
2215 debug_print("not even started\n");
2218 if (sc_exiting == TRUE) {
2219 debug_print("exit pending\n");
2223 debug_print("exiting\n");
2224 if (compose_get_compose_list()) {
2225 if (!draft_all_messages()) {
2226 main_window_popup(mainwin);
2232 if (prefs_common.warn_queued_on_exit && procmsg_have_queued_mails_fast()) {
2233 if (alertpanel(_("Queued messages"),
2234 _("Some unsent messages are queued. Exit now?"),
2235 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL)
2236 != G_ALERTALTERNATE) {
2237 main_window_popup(mainwin);
2241 manage_window_focus_in(mainwin->window, NULL, NULL);
2245 #ifdef HAVE_VALGRIND
2246 if (RUNNING_ON_VALGRIND) {
2247 summary_clear_list(mainwin->summaryview);
2250 if (folderview_get_selected_item(mainwin->folderview))
2251 folder_item_close(folderview_get_selected_item(mainwin->folderview));
2255 gboolean claws_is_exiting(void)
2260 gboolean claws_is_starting(void)
2266 * CLAWS: want this public so crash dialog can delete the
2269 gchar *claws_get_socket_name(void)
2271 static gchar *filename = NULL;
2272 const gchar *socket_dir = NULL;
2274 if (rc_dir_is_alt())
2275 socket_dir = get_rc_dir();
2277 socket_dir = g_get_tmp_dir();
2278 if (filename == NULL) {
2279 filename = g_strdup_printf("%s%cclaws-mail-%d",
2280 socket_dir, G_DIR_SEPARATOR,
2291 static gchar *get_crashfile_name(void)
2293 static gchar *filename = NULL;
2295 if (filename == NULL) {
2296 filename = g_strdup_printf("%s%cclaws-crashed",
2297 get_tmp_dir(), G_DIR_SEPARATOR);
2303 static gint prohibit_duplicate_launch(void)
2310 path = claws_get_socket_name();
2311 /* Try to connect to the control socket */
2312 uxsock = fd_connect_unix(path);
2314 if (x_display == NULL)
2315 x_display = g_strdup(g_getenv("DISPLAY"));
2322 /* If connect failed, no other process is running.
2323 * Unlink the potentially existing socket, then
2324 * open it. This has to be done locking a temporary
2325 * file to avoid the race condition where another
2326 * process could have created the socket just in
2329 socket_lock = g_strconcat(path, ".lock",
2331 lock_fd = g_open(socket_lock, O_RDWR|O_CREAT, 0);
2333 debug_print("Couldn't open %s: %s (%d)\n", socket_lock,
2334 strerror(errno), errno);
2335 g_free(socket_lock);
2338 if (flock(lock_fd, LOCK_EX) < 0) {
2339 debug_print("Couldn't lock %s: %s (%d)\n", socket_lock,
2340 strerror(errno), errno);
2342 g_free(socket_lock);
2348 debug_print("Opening socket %s\n", path);
2349 ret = fd_open_unix(path);
2351 flock(lock_fd, LOCK_UN);
2352 claws_unlink(socket_lock);
2353 g_free(socket_lock);
2360 hmutex = CreateMutexA(NULL, FALSE, "ClawsMail");
2362 debug_print("cannot create Mutex\n");
2365 if (GetLastError() != ERROR_ALREADY_EXISTS) {
2366 uxsock = fd_open_inet(50216);
2372 uxsock = fd_connect_inet(50216);
2376 /* remote command mode */
2378 debug_print("another Claws Mail instance is already running.\n");
2380 if (cmd.receive_all) {
2381 fd_write_all(uxsock, "receive_all\n", 12);
2382 } else if (cmd.receive) {
2383 fd_write_all(uxsock, "receive\n", 8);
2384 } else if (cmd.compose && cmd.attach_files) {
2385 gchar *str, *compose_str;
2387 if (cmd.compose_mailto) {
2388 compose_str = g_strdup_printf("compose_attach %s\n",
2389 cmd.compose_mailto);
2391 compose_str = g_strdup("compose_attach\n");
2394 fd_write_all(uxsock, compose_str, strlen(compose_str));
2395 g_free(compose_str);
2397 for (curr = cmd.attach_files; curr != NULL ; curr = curr->next) {
2398 str = (gchar *) ((AttachInfo *)curr->data)->file;
2399 fd_write_all(uxsock, str, strlen(str));
2400 fd_write_all(uxsock, "\n", 1);
2403 fd_write_all(uxsock, ".\n", 2);
2404 } else if (cmd.compose) {
2407 if (cmd.compose_mailto) {
2408 compose_str = g_strdup_printf
2409 ("compose %s\n", cmd.compose_mailto);
2411 compose_str = g_strdup("compose\n");
2414 fd_write_all(uxsock, compose_str, strlen(compose_str));
2415 g_free(compose_str);
2416 } else if (cmd.subscribe) {
2417 gchar *str = g_strdup_printf("subscribe %s\n", cmd.subscribe_uri);
2418 fd_write_all(uxsock, str, strlen(str));
2420 } else if (cmd.send) {
2421 fd_write_all(uxsock, "send\n", 5);
2422 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
2423 fd_write(uxsock, "online\n", 6);
2424 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
2425 fd_write(uxsock, "offline\n", 7);
2426 } else if (cmd.status || cmd.status_full) {
2427 gchar buf[BUFFSIZE];
2429 const gchar *command;
2433 command = cmd.status_full ? "status-full\n" : "status\n";
2434 folders = cmd.status_full ? cmd.status_full_folders :
2437 fd_write_all(uxsock, command, strlen(command));
2438 for (i = 0; folders && i < folders->len; ++i) {
2439 folder = g_ptr_array_index(folders, i);
2440 fd_write_all(uxsock, folder, strlen(folder));
2441 fd_write_all(uxsock, "\n", 1);
2443 fd_write_all(uxsock, ".\n", 2);
2445 fd_gets(uxsock, buf, sizeof(buf));
2446 if (!strncmp(buf, ".\n", 2)) break;
2449 } else if (cmd.exit) {
2450 fd_write_all(uxsock, "exit\n", 5);
2451 } else if (cmd.statistics) {
2453 fd_write(uxsock, "statistics\n", 11);
2455 fd_gets(uxsock, buf, sizeof(buf));
2456 if (!strncmp(buf, ".\n", 2)) break;
2459 } else if (cmd.reset_statistics) {
2460 fd_write(uxsock, "reset_statistics\n", 17);
2461 } else if (cmd.target) {
2462 gchar *str = g_strdup_printf("select %s\n", cmd.target);
2463 fd_write_all(uxsock, str, strlen(str));
2465 } else if (cmd.search) {
2466 gchar buf[BUFFSIZE];
2468 g_strdup_printf("search %s\n%s\n%s\n%c\n",
2469 cmd.search_folder, cmd.search_type, cmd.search_request,
2470 (cmd.search_recursive==TRUE)?'1':'0');
2471 fd_write_all(uxsock, str, strlen(str));
2474 fd_gets(uxsock, buf, sizeof(buf));
2475 if (!strncmp(buf, ".\n", 2)) break;
2481 fd_write_all(uxsock, "get_display\n", 12);
2482 memset(buf, 0, sizeof(buf));
2483 fd_gets(uxsock, buf, sizeof(buf));
2484 if (strcmp2(buf, x_display)) {
2485 g_print("Claws Mail is already running on display %s.\n",
2489 uxsock = fd_connect_unix(path);
2490 fd_write_all(uxsock, "popup\n", 6);
2493 fd_write_all(uxsock, "popup\n", 6);
2501 static gint lock_socket_remove(void)
2506 if (lock_socket < 0) {
2510 if (lock_socket_tag > 0) {
2511 g_source_remove(lock_socket_tag);
2513 fd_close(lock_socket);
2516 filename = claws_get_socket_name();
2517 claws_unlink(filename);
2523 static GPtrArray *get_folder_item_list(gint sock)
2525 gchar buf[BUFFSIZE];
2527 GPtrArray *folders = NULL;
2530 fd_gets(sock, buf, sizeof(buf));
2531 if (!strncmp(buf, ".\n", 2)) {
2536 folders = g_ptr_array_new();
2538 item = folder_find_item_from_identifier(buf);
2540 g_ptr_array_add(folders, item);
2542 g_warning("no such folder: %s\n", buf);
2549 static void lock_socket_input_cb(gpointer data,
2551 GIOCondition condition)
2553 MainWindow *mainwin = (MainWindow *)data;
2555 gchar buf[BUFFSIZE];
2557 sock = fd_accept(source);
2558 fd_gets(sock, buf, sizeof(buf));
2560 if (!strncmp(buf, "popup", 5)) {
2561 main_window_popup(mainwin);
2562 } else if (!strncmp(buf, "get_display", 11)) {
2563 fd_write_all(sock, x_display, strlen(x_display));
2564 } else if (!strncmp(buf, "receive_all", 11)) {
2565 inc_all_account_mail(mainwin, FALSE,
2566 prefs_common.newmail_notify_manu);
2567 } else if (!strncmp(buf, "receive", 7)) {
2568 inc_mail(mainwin, prefs_common.newmail_notify_manu);
2569 } else if (!strncmp(buf, "compose_attach", 14)) {
2570 GList *files = NULL, *curr;
2574 mailto = g_strdup(buf + strlen("compose_attach") + 1);
2575 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
2577 if (!strcmp2(buf, "."))
2580 ainfo = g_new0(AttachInfo, 1);
2581 ainfo->file = g_strdup(buf);
2582 files = g_list_append(files, ainfo);
2584 open_compose_new(mailto, files);
2586 curr = g_list_first(files);
2587 while (curr != NULL) {
2588 ainfo = (AttachInfo *)curr->data;
2589 g_free(ainfo->file);
2595 } else if (!strncmp(buf, "compose", 7)) {
2596 open_compose_new(buf + strlen("compose") + 1, NULL);
2597 } else if (!strncmp(buf, "subscribe", 9)) {
2598 main_window_popup(mainwin);
2599 folder_subscribe(buf + strlen("subscribe") + 1);
2600 } else if (!strncmp(buf, "send", 4)) {
2602 } else if (!strncmp(buf, "online", 6)) {
2603 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
2604 } else if (!strncmp(buf, "offline", 7)) {
2605 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
2606 } else if (!strncmp(buf, "status-full", 11) ||
2607 !strncmp(buf, "status", 6)) {
2611 folders = get_folder_item_list(sock);
2612 status = folder_get_status
2613 (folders, !strncmp(buf, "status-full", 11));
2614 fd_write_all(sock, status, strlen(status));
2615 fd_write_all(sock, ".\n", 2);
2617 if (folders) g_ptr_array_free(folders, TRUE);
2618 } else if (!strncmp(buf, "statistics", 10)) {
2621 g_snprintf(tmp, sizeof(tmp), _("Session statistics\n"));
2622 fd_write_all(sock, tmp, strlen(tmp));
2624 if (prefs_common.date_format) {
2629 lt = localtime(&session_stats.time_started);
2630 fast_strftime(date, len, prefs_common.date_format, lt);
2631 g_snprintf(tmp, sizeof(tmp), _("Started: %s\n"),
2632 lt ? date : ctime(&session_stats.time_started));
2634 g_snprintf(tmp, sizeof(tmp), _("Started: %s\n"),
2635 ctime(&session_stats.time_started));
2636 fd_write_all(sock, tmp, strlen(tmp));
2638 fd_write_all(sock, "\n", 1);
2640 g_snprintf(tmp, sizeof(tmp), _("Incoming traffic\n"));
2641 fd_write_all(sock, tmp, strlen(tmp));
2643 g_snprintf(tmp, sizeof(tmp), _("Received messages: %d\n"),
2644 session_stats.received);
2645 fd_write_all(sock, tmp, strlen(tmp));
2647 fd_write_all(sock, "\n", 1);
2649 g_snprintf(tmp, sizeof(tmp), _("Outgoing traffic\n"));
2650 fd_write_all(sock, tmp, strlen(tmp));
2652 g_snprintf(tmp, sizeof(tmp), _("New/redirected messages: %d\n"),
2653 session_stats.sent);
2654 fd_write_all(sock, tmp, strlen(tmp));
2656 g_snprintf(tmp, sizeof(tmp), _("Replied messages: %d\n"),
2657 session_stats.replied);
2658 fd_write_all(sock, tmp, strlen(tmp));
2660 g_snprintf(tmp, sizeof(tmp), _("Forwarded messages: %d\n"),
2661 session_stats.forwarded);
2662 fd_write_all(sock, tmp, strlen(tmp));
2664 g_snprintf(tmp, sizeof(tmp), _("Total outgoing messages: %d\n"),
2665 (session_stats.sent + session_stats.replied +
2666 session_stats.forwarded));
2667 fd_write_all(sock, tmp, strlen(tmp));
2669 fd_write_all(sock, ".\n", 2);
2670 } else if (!strncmp(buf, "reset_statistics", 16)) {
2672 } else if (!strncmp(buf, "select ", 7)) {
2673 const gchar *target = buf+7;
2674 mainwindow_jump_to(target, TRUE);
2675 } else if (!strncmp(buf, "search ", 7)) {
2676 FolderItem* folderItem = NULL;
2677 GSList *messages = NULL;
2678 gchar *folder_name = NULL;
2679 gchar *request = NULL;
2680 AdvancedSearch *search;
2682 AdvancedSearchType searchType = ADVANCED_SEARCH_EXTENDED;
2684 search = advsearch_new();
2686 folder_name = g_strdup(buf+7);
2687 strretchomp(folder_name);
2689 if (fd_gets(sock, buf, sizeof(buf)) <= 0)
2692 switch (toupper(buf[0])) {
2693 case 'S': searchType = ADVANCED_SEARCH_SUBJECT; break;
2694 case 'F': searchType = ADVANCED_SEARCH_FROM; break;
2695 case 'T': searchType = ADVANCED_SEARCH_TO; break;
2696 case 'M': searchType = ADVANCED_SEARCH_MIXED; break;
2697 case 'G': searchType = ADVANCED_SEARCH_TAG; break;
2698 case 'E': searchType = ADVANCED_SEARCH_EXTENDED; break;
2701 if (fd_gets(sock, buf, sizeof(buf)) <= 0)
2704 request = g_strdup(buf);
2705 strretchomp(request);
2708 if (fd_gets(sock, buf, sizeof(buf)) > 0)
2709 recursive = buf[0] != '0';
2711 debug_print("search: %s %i %s %i\n", folder_name, searchType, request, recursive);
2714 folderItem = folder_find_item_from_identifier(folder_name);
2716 if (folder_name && folderItem == NULL) {
2717 debug_print("Unknown folder item : '%s', searching folder\n",folder_name);
2718 Folder* folder = folder_find_from_path(folder_name);
2720 folderItem = FOLDER_ITEM(folder->node->data);
2722 debug_print("Unknown folder: '%s'\n",folder_name);
2724 debug_print("%s %s\n",folderItem->name, folderItem->path);
2727 if (folderItem != NULL) {
2728 advsearch_set(search, searchType, request);
2729 advsearch_search_msgs_in_folders(search, &messages, folderItem, recursive);
2731 g_print("Folder '%s' not found.\n'", folder_name);
2735 for (cur = messages; cur != NULL; cur = cur->next) {
2736 MsgInfo* msg = (MsgInfo *)cur->data;
2737 gchar *file = procmsg_get_message_file_path(msg);
2738 fd_write_all(sock, file, strlen(file));
2739 fd_write_all(sock, "\n", 1);
2742 fd_write_all(sock, ".\n", 2);
2745 g_free(folder_name);
2747 advsearch_free(search);
2748 if (messages != NULL)
2749 procmsg_msg_list_free(messages);
2750 } else if (!strncmp(buf, "exit", 4)) {
2751 if (prefs_common.clean_on_exit && !prefs_common.ask_on_clean) {
2752 procmsg_empty_all_trash();
2754 app_will_exit(NULL, mainwin);
2760 static void open_compose_new(const gchar *address, GList *attach_files)
2765 Xstrdup_a(addr, address, return);
2769 compose_new(NULL, addr, attach_files);
2772 static void send_queue(void)
2775 gchar *errstr = NULL;
2776 gboolean error = FALSE;
2777 for (list = folder_get_list(); list != NULL; list = list->next) {
2778 Folder *folder = list->data;
2780 if (folder->queue) {
2781 gint res = procmsg_send_queue
2782 (folder->queue, prefs_common.savemsg,
2786 folder_item_scan(folder->queue);
2794 alertpanel_error_log(_("Some errors occurred "
2795 "while sending queued messages:\n%s"), errstr);
2798 alertpanel_error_log("Some errors occurred "
2799 "while sending queued messages.");
2803 static void quit_signal_handler(int sig)
2805 debug_print("Quitting on signal %d\n", sig);
2807 g_timeout_add(0, clean_quit, NULL);
2810 static void install_basic_sighandlers()
2814 struct sigaction act;
2819 sigaddset(&mask, SIGTERM);
2822 sigaddset(&mask, SIGINT);
2825 sigaddset(&mask, SIGHUP);
2828 act.sa_handler = quit_signal_handler;
2833 sigaction(SIGTERM, &act, 0);
2836 sigaction(SIGINT, &act, 0);
2839 sigaction(SIGHUP, &act, 0);
2842 sigprocmask(SIG_UNBLOCK, &mask, 0);
2843 #endif /* !G_OS_WIN32 */
2846 #if (defined linux && defined SIGIO)
2847 static int mem_notify_fd = 0;
2849 static gboolean clean_caches(gpointer unused)
2851 if (static_mainwindow && static_mainwindow->lock_count > 0)
2853 debug_print("/dev/mem_notify: callback: Freeing some memory now!\n");
2854 folder_clean_cache_memory_force();
2858 static void memory_signal_handler(int sig)
2860 debug_print("/dev/mem_notify: Kernel says we should free up some memory!\n");
2861 g_timeout_add(10, clean_caches, NULL);
2864 static void install_memory_sighandler()
2867 struct sigaction act;
2870 mem_notify_fd = g_open("/dev/mem_notify", O_RDONLY|O_NONBLOCK, 0);
2871 if (mem_notify_fd == -1) {
2872 debug_print("/dev/mem_notify not available (%s)\n",
2877 fcntl(mem_notify_fd, F_SETOWN, getpid());
2878 flags = fcntl(mem_notify_fd, F_GETFL);
2879 fcntl(mem_notify_fd, flags|FASYNC);
2883 sigaddset(&mask, SIGIO);
2885 act.sa_handler = memory_signal_handler;
2889 sigaction(SIGIO, &act, 0);
2891 sigprocmask(SIG_UNBLOCK, &mask, 0);
2893 debug_print("/dev/mem_notify: installed handler\n");
2895 #endif /* linux && SIGIO */
2898 osso_context_t *get_osso_context(void)
2900 return static_osso_context;
2905 #ifdef HAVE_NETWORKMANAGER_SUPPORT
2906 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
2909 MainWindow *mainWin;
2912 if (static_mainwindow)
2913 mainWin = static_mainwindow;
2915 mainWin = (MainWindow*)data;
2917 if (!prefs_common.use_networkmanager)
2925 online = networkmanager_is_online(&error);
2927 if(online && went_offline_nm) {
2928 went_offline_nm = FALSE;
2929 main_window_toggle_work_offline(mainWin, FALSE, FALSE);
2930 debug_print("NetworkManager: Went online\n");
2931 log_message(LOG_PROTOCOL, _("NetworkManager: network is online.\n"));
2934 went_offline_nm = TRUE;
2935 main_window_toggle_work_offline(mainWin, TRUE, FALSE);
2936 debug_print("NetworkManager: Went offline\n");
2937 log_message(LOG_PROTOCOL, _("NetworkManager: network is offline.\n"));
2941 debug_print("Failed to get online information from NetworkManager: %s\n",
2943 g_error_free(error);
2947 debug_print("NetworkManager: Cannot change connection state because "
2948 "main window does not exist\n");
2951 /* Returns true (and sets error appropriately, if given) in case of error */
2952 gboolean networkmanager_is_online(GError **error)
2954 DBusGConnection *connection;
2956 GError *tmp_error = NULL;
2960 if (!prefs_common.use_networkmanager)
2965 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &tmp_error);
2968 /* If calling code doesn't do error checking, at least print some debug */
2969 if((error == NULL) || (*error == NULL))
2970 debug_print("Failed to open connection to system bus: %s\n",
2971 tmp_error->message);
2972 g_propagate_error(error, tmp_error);
2976 proxy = dbus_g_proxy_new_for_name(connection,
2977 "org.freedesktop.NetworkManager",
2978 "/org/freedesktop/NetworkManager",
2979 "org.freedesktop.NetworkManager");
2981 retVal = dbus_g_proxy_call(proxy,"state",&tmp_error, G_TYPE_INVALID,
2982 G_TYPE_UINT, &state, G_TYPE_INVALID);
2985 g_object_unref(proxy);
2987 dbus_g_connection_unref(connection);
2990 /* If calling code doesn't do error checking, at least print some debug */
2991 if((error == NULL) || (*error == NULL))
2992 debug_print("Failed to get state info from NetworkManager: %s\n",
2993 tmp_error->message);
2994 g_propagate_error(error, tmp_error);
2997 #if NM_CHECK_VERSION(0,8,992)
2998 return (state == NM_STATE_CONNECTED_LOCAL ||
2999 state == NM_STATE_CONNECTED_SITE ||
3000 state == NM_STATE_CONNECTED_GLOBAL ||
3001 state == NM_STATE_UNKNOWN);
3003 return (state == NM_STATE_CONNECTED ||
3004 state == NM_STATE_UNKNOWN);