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>
49 #ifdef HAVE_STARTUP_NOTIFICATION
50 # define SN_API_NOT_YET_FROZEN
51 # include <libsn/sn-launchee.h>
52 # include <gdk/gdkx.h>
56 #include <dbus/dbus-glib.h>
58 #ifdef HAVE_NETWORKMANAGER_SUPPORT
59 #include <NetworkManager.h>
67 #include "mainwindow.h"
68 #include "folderview.h"
69 #include "image_viewer.h"
70 #include "summaryview.h"
71 #include "prefs_common.h"
72 #include "prefs_account.h"
73 #include "prefs_actions.h"
74 #include "prefs_ext_prog.h"
75 #include "prefs_fonts.h"
76 #include "prefs_image_viewer.h"
77 #include "prefs_message.h"
78 #include "prefs_receive.h"
79 #include "prefs_msg_colors.h"
80 #include "prefs_quote.h"
81 #include "prefs_spelling.h"
82 #include "prefs_summaries.h"
83 #include "prefs_themes.h"
84 #include "prefs_other.h"
85 #include "prefs_logging.h"
86 #include "prefs_send.h"
87 #include "prefs_wrapping.h"
88 #include "prefs_compose_writing.h"
89 #include "prefs_display_header.h"
94 #include "manage_window.h"
95 #include "alertpanel.h"
96 #include "statusbar.h"
97 #ifndef USE_NEW_ADDRBOOK
98 #include "addressbook.h"
100 #include "addressbook-dbus.h"
106 #include "gtkutils.h"
109 #include "prefs_toolbar.h"
112 #include "imap_gtk.h"
113 #include "news_gtk.h"
118 #include "quicksearch.h"
121 #include "imap-thread.h"
122 #include "nntp-thread.h"
124 #include "stock_pixmap.h"
137 #include <hildon/hildon-banner.h>
138 #include <hildon/hildon-program.h>
140 #include <hildon-widgets/hildon-banner.h>
141 #include <hildon-widgets/hildon-program.h>
144 #include <libgnomevfs/gnome-vfs-volume.h>
145 #include <libgnomevfs/gnome-vfs-volume-monitor.h>
146 #include <libgnomevfs/gnome-vfs-utils.h>
148 #define OSSO_NAME "claws_mail"
149 #define OSSO_SERVICE "com.nokia."OSSO_NAME
150 #define OSSO_OBJECT "/com/nokia/"OSSO_NAME
151 #define OSSO_IFACE "com.nokia."OSSO_NAME
153 typedef struct _AppData AppData;
155 HildonProgram *program;
156 HildonWindow *window;
157 osso_context_t *osso_context;
160 static GnomeVFSVolumeMonitor *volmon;
163 #ifdef HAVE_NETWORKMANAGER_SUPPORT
164 /* Went offline due to NetworkManager */
165 static gboolean went_offline_nm;
168 #if !defined(NM_CHECK_VERSION)
169 #define NM_CHECK_VERSION(x,y,z) 0
172 #ifdef HAVE_DBUS_GLIB
173 static DBusGProxy *awn_proxy = NULL;
180 HildonProgram *hildon_program;
183 #ifdef HAVE_STARTUP_NOTIFICATION
184 static SnLauncheeContext *sn_context = NULL;
185 static SnDisplay *sn_display = NULL;
188 static gint lock_socket = -1;
189 static gint lock_socket_tag = 0;
190 static gchar *x_display = NULL;
193 ONLINE_MODE_DONT_CHANGE,
198 static struct RemoteCmd {
200 gboolean receive_all;
202 const gchar *compose_mailto;
205 const gchar *search_folder;
206 const gchar *search_type;
207 const gchar *search_request;
208 gboolean search_recursive;
210 gboolean status_full;
212 gboolean reset_statistics;
213 GPtrArray *status_folders;
214 GPtrArray *status_full_folders;
221 const gchar *subscribe_uri;
225 SessionStats session_stats;
227 static void reset_statistics(void);
229 static void parse_cmd_opt(int argc, char *argv[]);
231 static gint prohibit_duplicate_launch (void);
232 static gchar * get_crashfile_name (void);
233 static gint lock_socket_remove (void);
234 static void lock_socket_input_cb (gpointer data,
236 GIOCondition condition);
238 static void open_compose_new (const gchar *address,
239 GList *attach_files);
241 static void send_queue (void);
242 static void initial_processing (FolderItem *item, gpointer data);
243 static void quit_signal_handler (int sig);
244 static void install_basic_sighandlers (void);
245 #if (defined linux && defined SIGIO)
246 static void install_memory_sighandler (void);
248 static void exit_claws (MainWindow *mainwin);
250 #ifdef HAVE_NETWORKMANAGER_SUPPORT
251 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
255 #define MAKE_DIR_IF_NOT_EXIST(dir) \
257 if (!is_dir_exist(dir)) { \
258 if (is_file_exist(dir)) { \
260 (_("File '%s' already exists.\n" \
261 "Can't create folder."), \
265 if (make_dir(dir) < 0) \
270 static MainWindow *static_mainwindow;
273 static osso_context_t *static_osso_context;
275 void exit_event_handler(gboolean die_now, gpointer data)
278 appdata = (AppData *) data;
279 /* Do whatever application needs to do before exiting */
280 exit_claws(static_mainwindow);
281 hildon_banner_show_information(GTK_WIDGET(appdata->window), NULL,
286 /* Callback for hardware D-BUS events */
287 void hw_event_handler(osso_hw_state_t *state, gpointer data)
290 appdata = (AppData *) data;
292 if (state->shutdown_ind) {
293 exit_claws(static_mainwindow);
294 hildon_banner_show_information(GTK_WIDGET(appdata->window), NULL,
299 /* Callback for normal D-BUS messages */
300 gint dbus_req_handler(const gchar * interface, const gchar * method,
301 GArray * arguments, gpointer data,
305 appdata = (AppData *) data;
307 if (!strcmp(method, "top_application")) {
308 osso_rpc_free_val(retval);
311 osso_system_note_infoprint(appdata->osso_context, method, retval);
312 osso_rpc_free_val(retval);
317 static gboolean emergency_exit = FALSE;
319 #ifdef HAVE_STARTUP_NOTIFICATION
320 static void sn_error_trap_push(SnDisplay *display, Display *xdisplay)
322 gdk_error_trap_push();
325 static void sn_error_trap_pop(SnDisplay *display, Display *xdisplay)
327 gdk_error_trap_pop();
330 static void startup_notification_complete(gboolean with_window)
333 GtkWidget *hack = NULL;
336 /* this is needed to make the startup notification leave,
337 * if we have been launched from a menu.
338 * We have to display a window, so let it be very little */
339 hack = gtk_window_new(GTK_WINDOW_POPUP);
340 gtk_window_move(GTK_WINDOW(hack), 0, 0);
341 gtk_widget_set_size_request(hack, 1, 1);
342 gtk_widget_show(hack);
345 xdisplay = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
346 sn_display = sn_display_new(xdisplay,
349 sn_context = sn_launchee_context_new_from_environment(sn_display,
350 DefaultScreen(xdisplay));
352 if (sn_context != NULL) {
353 sn_launchee_context_complete(sn_context);
354 sn_launchee_context_unref(sn_context);
355 sn_display_unref(sn_display);
358 gtk_widget_destroy(hack);
361 #endif /* HAVE_STARTUP_NOTIFICATION */
363 static void claws_gtk_idle(void)
365 while(gtk_events_pending()) {
366 gtk_main_iteration();
371 static gboolean sc_starting = FALSE;
373 static gboolean defer_check_all(void *data)
375 gboolean autochk = GPOINTER_TO_INT(data);
377 inc_all_account_mail(static_mainwindow, autochk,
378 prefs_common.newmail_notify_manu);
382 main_window_set_menu_sensitive(static_mainwindow);
383 toolbar_main_set_sensitive(static_mainwindow);
388 static gboolean defer_check(void *data)
390 inc_mail(static_mainwindow, prefs_common.newmail_notify_manu);
394 main_window_set_menu_sensitive(static_mainwindow);
395 toolbar_main_set_sensitive(static_mainwindow);
400 static gboolean defer_jump(void *data)
402 if (cmd.receive_all) {
403 defer_check_all(GINT_TO_POINTER(FALSE));
404 } else if (prefs_common.chk_on_startup) {
405 defer_check_all(GINT_TO_POINTER(TRUE));
406 } else if (cmd.receive) {
409 mainwindow_jump_to(data, FALSE);
412 main_window_set_menu_sensitive(static_mainwindow);
413 toolbar_main_set_sensitive(static_mainwindow);
418 static void chk_update_val(GtkWidget *widget, gpointer data)
420 gboolean *val = (gboolean *)data;
421 *val = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
424 static gboolean migrate_old_config(const gchar *old_cfg_dir, const gchar *new_cfg_dir, const gchar *oldversion)
426 gchar *message = g_strdup_printf(_("Configuration for %s found.\n"
427 "Do you want to migrate this configuration?"), oldversion);
428 gchar *message2 = g_strdup_printf(_("\n\nYour Sylpheed filtering rules can be converted by a\n"
429 "script available at %s."), TOOLS_URI);
431 if (!strcmp(oldversion, "Sylpheed"))
432 message = g_strconcat(message, message2, NULL);
436 GtkWidget *window = NULL;
437 GtkWidget *keep_backup_chk;
438 gboolean backup = TRUE;
440 keep_backup_chk = gtk_check_button_new_with_label (_("Keep old configuration"));
441 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_backup_chk), TRUE);
442 CLAWS_SET_TIP(keep_backup_chk,
443 _("Keeping a backup will allow you to go back to an "
444 "older version, but may take a while if you have "
445 "cached IMAP or News data, and will take some extra "
446 "room on your disk."));
448 g_signal_connect(G_OBJECT(keep_backup_chk), "toggled",
449 G_CALLBACK(chk_update_val), &backup);
451 if (alertpanel_full(_("Migration of configuration"), message,
452 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL, FALSE,
453 keep_backup_chk, ALERT_QUESTION, G_ALERTDEFAULT) != G_ALERTALTERNATE) {
457 /* we can either do a fast migration requiring not any extra disk
458 * space, or a slow one that copies the old configuration and leaves
462 window = label_window_create(_("Copying configuration... This may take a while..."));
465 r = copy_dir(old_cfg_dir, new_cfg_dir);
466 label_window_destroy(window);
468 /* if copy failed, we'll remove the partially copied
471 alertpanel_error(_("Migration failed!"));
472 remove_dir_recursive(new_cfg_dir);
475 /* fast mode failed, but we don't want backup */
476 remove_dir_recursive(old_cfg_dir);
480 window = label_window_create(_("Migrating configuration..."));
483 r = g_rename(old_cfg_dir, new_cfg_dir);
484 label_window_destroy(window);
486 /* if g_rename failed, we'll try to copy */
488 FILE_OP_ERROR(new_cfg_dir, "g_rename failed, trying copy\n");
495 static int migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
498 gchar *plugin_path, *old_plugin_path, *new_plugin_path;
500 gboolean err = FALSE;
502 oldfp = g_fopen(old_rc, "r");
505 newfp = g_fopen(new_rc, "w");
511 plugin_path = g_strdup(get_plugin_dir());
512 new_plugin_path = g_strdup(plugin_path);
514 if (strstr(plugin_path, "/claws-mail/")) {
515 gchar *end = g_strdup(strstr(plugin_path, "/claws-mail/")+strlen("/claws-mail/"));
516 *(strstr(plugin_path, "/claws-mail/")) = '\0';
517 old_plugin_path = g_strconcat(plugin_path, "/sylpheed-claws/", end, NULL);
520 old_plugin_path = g_strdup(new_plugin_path);
522 debug_print("replacing %s with %s\n", old_plugin_path, new_plugin_path);
523 while (fgets(buf, sizeof(buf), oldfp)) {
524 if (strncmp(buf, old_plugin_path, strlen(old_plugin_path))) {
525 err |= (fputs(buf, newfp) == EOF);
527 debug_print("->replacing %s", buf);
528 debug_print(" with %s%s", new_plugin_path, buf+strlen(old_plugin_path));
529 err |= (fputs(new_plugin_path, newfp) == EOF);
530 err |= (fputs(buf+strlen(old_plugin_path), newfp) == EOF);
534 g_free(new_plugin_path);
535 g_free(old_plugin_path);
537 if (fclose(newfp) == EOF)
545 sc_client_set_value (MainWindow *mainwin,
556 prop.num_vals = num_vals;
560 if (mainwin->smc_conn)
561 SmcSetProperties ((SmcConn) mainwin->smc_conn, 1, proplist);
564 static void sc_die_callback (SmcConn smc_conn, SmPointer client_data)
569 static void sc_save_complete_callback(SmcConn smc_conn, SmPointer client_data)
573 static void sc_shutdown_cancelled_callback (SmcConn smc_conn, SmPointer client_data)
575 MainWindow *mainwin = (MainWindow *)client_data;
576 if (mainwin->smc_conn)
577 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
580 static void sc_save_yourself_callback (SmcConn smc_conn,
581 SmPointer client_data,
587 MainWindow *mainwin = (MainWindow *)client_data;
588 if (mainwin->smc_conn)
589 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
592 static IceIOErrorHandler sc_ice_installed_handler;
594 static void sc_ice_io_error_handler (IceConn connection)
596 if (sc_ice_installed_handler)
597 (*sc_ice_installed_handler) (connection);
599 static gboolean sc_process_ice_messages (GIOChannel *source,
600 GIOCondition condition,
603 IceConn connection = (IceConn) data;
604 IceProcessMessagesStatus status;
606 status = IceProcessMessages (connection, NULL, NULL);
608 if (status == IceProcessMessagesIOError) {
609 IcePointer context = IceGetConnectionContext (connection);
611 if (context && GTK_IS_OBJECT (context)) {
612 guint disconnect_id = g_signal_lookup ("disconnect", G_OBJECT_TYPE (context));
614 if (disconnect_id > 0)
615 g_signal_emit (context, disconnect_id, 0);
617 IceSetShutdownNegotiation (connection, False);
618 IceCloseConnection (connection);
625 static void new_ice_connection (IceConn connection, IcePointer client_data, Bool opening,
626 IcePointer *watch_data)
632 /* Make sure we don't pass on these file descriptors to any
634 fcntl(IceConnectionNumber(connection),F_SETFD,
635 fcntl(IceConnectionNumber(connection),F_GETFD,0) | FD_CLOEXEC);
637 channel = g_io_channel_unix_new (IceConnectionNumber (connection));
638 input_id = g_io_add_watch (channel,
639 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
640 sc_process_ice_messages,
642 g_io_channel_unref (channel);
644 *watch_data = (IcePointer) GUINT_TO_POINTER (input_id);
646 input_id = GPOINTER_TO_UINT ((gpointer) *watch_data);
647 g_source_remove (input_id);
651 static void sc_session_manager_connect(MainWindow *mainwin)
653 static gboolean connected = FALSE;
654 SmcCallbacks callbacks;
656 IceIOErrorHandler default_handler;
663 sc_ice_installed_handler = IceSetIOErrorHandler (NULL);
664 default_handler = IceSetIOErrorHandler (sc_ice_io_error_handler);
666 if (sc_ice_installed_handler == default_handler)
667 sc_ice_installed_handler = NULL;
669 IceAddConnectionWatch (new_ice_connection, NULL);
672 callbacks.save_yourself.callback = sc_save_yourself_callback;
673 callbacks.die.callback = sc_die_callback;
674 callbacks.save_complete.callback = sc_save_complete_callback;
675 callbacks.shutdown_cancelled.callback = sc_shutdown_cancelled_callback;
677 callbacks.save_yourself.client_data =
678 callbacks.die.client_data =
679 callbacks.save_complete.client_data =
680 callbacks.shutdown_cancelled.client_data = (SmPointer) mainwin;
681 if (g_getenv ("SESSION_MANAGER")) {
682 gchar error_string_ret[256] = "";
684 mainwin->smc_conn = (gpointer)
685 SmcOpenConnection (NULL, mainwin,
686 SmProtoMajor, SmProtoMinor,
687 SmcSaveYourselfProcMask | SmcDieProcMask |
688 SmcSaveCompleteProcMask |
689 SmcShutdownCancelledProcMask,
692 256, error_string_ret);
694 if (error_string_ret[0] || mainwin->smc_conn == NULL)
695 g_warning ("While connecting to session manager:\n%s.",
699 vals = g_new (SmPropValue, 1);
700 vals[0].length = strlen(argv0);
701 vals[0].value = argv0;
702 sc_client_set_value (mainwin, SmCloneCommand, SmLISTofARRAY8, 1, vals);
703 sc_client_set_value (mainwin, SmRestartCommand, SmLISTofARRAY8, 1, vals);
704 sc_client_set_value (mainwin, SmProgram, SmARRAY8, 1, vals);
706 vals[0].length = strlen(g_get_user_name()?g_get_user_name():"");
707 vals[0].value = g_strdup(g_get_user_name()?g_get_user_name():"");
708 sc_client_set_value (mainwin, SmUserID, SmARRAY8, 1, vals);
714 static gboolean sc_exiting = FALSE;
715 static gboolean show_at_startup = TRUE;
716 static gboolean claws_crashed_bool = FALSE;
718 gboolean claws_crashed(void) {
719 return claws_crashed_bool;
722 void main_set_show_at_startup(gboolean show)
724 show_at_startup = show;
728 static void main_vol_mount_cb(GnomeVFSVolumeMonitor *vfs, GnomeVFSVolume *vol, MainWindow *mainwin)
730 gchar *uri = gnome_vfs_volume_get_activation_uri (vol);
731 gchar *mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
734 if(!strcmp(mount_path, prefs_common.data_root)) {
735 gtk_widget_set_sensitive(mainwin->window, TRUE);
741 static void main_vol_unmount_cb(GnomeVFSVolumeMonitor *vfs, GnomeVFSVolume *vol, MainWindow *mainwin)
743 gchar *uri = gnome_vfs_volume_get_activation_uri (vol);
744 gchar *mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
747 if(!strcmp(mount_path, prefs_common.data_root)) {
748 gtk_widget_set_sensitive(mainwin->window, FALSE);
757 static FILE* win32_debug_fp=NULL;
758 static guint win32_log_handler_app_id;
759 static guint win32_log_handler_glib_id;
760 static guint win32_log_handler_gtk_id;
762 static void win32_print_stdout(const gchar *string)
764 if (win32_debug_fp) {
765 fprintf(win32_debug_fp, string);
766 fflush(win32_debug_fp);
770 static void win32_print_stderr(const gchar *string)
772 if (win32_debug_fp) {
773 fprintf(win32_debug_fp, string);
774 fflush(win32_debug_fp);
778 static void win32_log(const gchar *log_domain, GLogLevelFlags log_level, const gchar* message, gpointer user_data)
780 if (win32_debug_fp) {
783 switch(log_level & G_LOG_LEVEL_MASK)
785 case G_LOG_LEVEL_ERROR:
788 case G_LOG_LEVEL_CRITICAL:
791 case G_LOG_LEVEL_WARNING:
794 case G_LOG_LEVEL_MESSAGE:
797 case G_LOG_LEVEL_INFO:
800 case G_LOG_LEVEL_DEBUG:
807 fprintf(win32_debug_fp, "%s: %s: %s", log_domain, type, message);
809 fprintf(win32_debug_fp, "%s: %s", type, message);
810 fflush(win32_debug_fp);
814 static void win32_open_log(void)
816 gchar *logfile = g_strconcat(g_get_tmp_dir(), G_DIR_SEPARATOR_S, "claws-win32.log", NULL);
817 gchar *oldlogfile = g_strconcat(g_get_tmp_dir(), G_DIR_SEPARATOR_S, "claws-win32.log.bak", NULL);
819 if (is_file_exist(logfile)) {
820 if (rename_force(logfile, oldlogfile) < 0)
821 FILE_OP_ERROR(logfile, "rename");
823 win32_debug_fp = g_fopen(logfile, "w");
828 g_set_print_handler(win32_print_stdout);
829 g_set_printerr_handler(win32_print_stdout);
830 win32_log_handler_app_id = g_log_set_handler(NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
831 | G_LOG_FLAG_RECURSION, win32_log, NULL);
832 win32_log_handler_glib_id = g_log_set_handler("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
833 | G_LOG_FLAG_RECURSION, win32_log, NULL);
834 win32_log_handler_gtk_id = g_log_set_handler("Gtk", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
835 | G_LOG_FLAG_RECURSION, win32_log, NULL);
839 static void win32_close_log(void)
843 g_log_remove_handler("", win32_log_handler_app_id);
844 g_log_remove_handler("GLib", win32_log_handler_glib_id);
845 g_log_remove_handler("Gtk", win32_log_handler_gtk_id);
846 fclose(win32_debug_fp);
852 static void main_dump_features_list(gboolean show_debug_only)
853 /* display compiled-in features list */
855 if (show_debug_only && !debug_get_mode())
859 debug_print("runtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
860 gtk_major_version, gtk_minor_version, gtk_micro_version,
861 glib_major_version, glib_minor_version, glib_micro_version);
863 g_print("runtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
864 gtk_major_version, gtk_minor_version, gtk_micro_version,
865 glib_major_version, glib_minor_version, glib_micro_version);
867 debug_print("buildtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
868 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
869 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
871 g_print("buildtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
872 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
873 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
876 debug_print("Compiled-in features:\n");
878 g_print("Compiled-in features:\n");
881 debug_print(" compface\n");
883 g_print(" compface\n");
887 debug_print(" aspell\n");
889 g_print(" aspell\n");
893 debug_print(" gnutls\n");
895 g_print(" gnutls\n");
899 debug_print(" ipv6\n");
905 debug_print(" iconv\n");
911 debug_print(" jpilot\n");
913 g_print(" jpilot\n");
917 debug_print(" ldap\n");
923 debug_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
925 g_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
929 debug_print(" libsm\n");
933 #if HAVE_NETWORKMANAGER_SUPPORT
935 debug_print(" NetworkManager\n");
937 g_print(" NetworkManager\n");
941 #ifdef HAVE_DBUS_GLIB
942 static guint dbus_item_hook_id = -1;
943 static guint dbus_folder_hook_id = -1;
945 static void uninstall_dbus_status_handler(void)
948 g_object_unref(awn_proxy);
950 if (dbus_item_hook_id != -1)
951 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, dbus_item_hook_id);
952 if (dbus_folder_hook_id != -1)
953 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, dbus_folder_hook_id);
956 static void dbus_update(FolderItem *removed_item)
958 guint new, unread, unreadmarked, marked, total;
959 guint replied, forwarded, locked, ignored, watched;
961 GError *error = NULL;
963 folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total,
964 &replied, &forwarded, &locked, &ignored,
967 total -= removed_item->total_msgs;
968 new -= removed_item->new_msgs;
969 unread -= removed_item->unread_msgs;
973 buf = g_strdup_printf("%d", new);
974 dbus_g_proxy_call(awn_proxy, "SetInfoByName", &error,
975 G_TYPE_STRING, "claws-mail",
977 G_TYPE_INVALID, G_TYPE_INVALID);
981 dbus_g_proxy_call(awn_proxy, "UnsetInfoByName", &error, G_TYPE_STRING,
982 "claws-mail", G_TYPE_INVALID, G_TYPE_INVALID);
985 debug_print("%s\n", error->message);
990 static gboolean dbus_status_update_folder_hook(gpointer source, gpointer data)
992 FolderUpdateData *hookdata;
994 if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
995 dbus_update(hookdata->item);
1002 static gboolean dbus_status_update_item_hook(gpointer source, gpointer data)
1009 static void install_dbus_status_handler(void)
1011 GError *tmp_error = NULL;
1012 DBusGConnection *connection = dbus_g_bus_get(DBUS_BUS_SESSION, &tmp_error);
1015 /* If calling code doesn't do error checking, at least print some debug */
1016 debug_print("Failed to open connection to session bus: %s\n",
1017 tmp_error->message);
1018 g_error_free(tmp_error);
1021 awn_proxy = dbus_g_proxy_new_for_name(connection,
1022 "com.google.code.Awn",
1023 "/com/google/code/Awn",
1024 "com.google.code.Awn");
1025 dbus_item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, dbus_status_update_item_hook, NULL);
1026 if (dbus_item_hook_id == -1) {
1027 g_warning(_("Failed to register folder item update hook"));
1028 uninstall_dbus_status_handler();
1032 dbus_folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, dbus_status_update_folder_hook, NULL);
1033 if (dbus_folder_hook_id == -1) {
1034 g_warning(_("Failed to register folder update hook"));
1035 uninstall_dbus_status_handler();
1041 static void reset_statistics(void)
1043 /* (re-)initialize session statistics */
1044 session_stats.received = 0;
1045 session_stats.sent = 0;
1046 session_stats.replied = 0;
1047 session_stats.forwarded = 0;
1048 session_stats.time_started = time(NULL);
1051 int main(int argc, char *argv[])
1054 osso_context_t *osso_context;
1055 osso_return_t result;
1057 #ifdef HAVE_DBUS_GLIB
1058 DBusGConnection *connection;
1061 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1062 DBusGProxy *nm_proxy;
1065 MainWindow *mainwin;
1066 FolderView *folderview;
1068 gboolean crash_file_present = FALSE;
1069 gint num_folder_class = 0;
1070 gboolean asked_for_migration = FALSE;
1071 gboolean start_done = TRUE;
1072 GtkUIManager *gui_manager = NULL;
1073 GSList *plug_list = NULL;
1074 gboolean never_ran = FALSE;
1075 START_TIMING("startup");
1082 if (!claws_init(&argc, &argv)) {
1089 main_dump_features_list(TRUE);
1091 prog_version = PROG_VERSION;
1092 argv0 = g_strdup(argv[0]);
1094 parse_cmd_opt(argc, argv);
1096 prefs_prepare_cache();
1101 gtk_init(&argc, &argv);
1102 crash_main(cmd.crash_params);
1108 crash_install_handlers();
1110 install_basic_sighandlers();
1111 #if (defined linux && defined SIGIO)
1112 install_memory_sighandler();
1116 /* check and create unix domain socket for remote operation */
1117 lock_socket = prohibit_duplicate_launch();
1118 if (lock_socket < 0) {
1119 #ifdef HAVE_STARTUP_NOTIFICATION
1120 if(gtk_init_check(&argc, &argv))
1121 startup_notification_complete(TRUE);
1126 if (cmd.status || cmd.status_full || cmd.search ||
1127 cmd.statistics || cmd.reset_statistics) {
1128 puts("0 Claws Mail not running.");
1129 lock_socket_remove();
1135 if (!g_thread_supported())
1136 g_thread_init(NULL);
1141 gtk_init(&argc, &argv);
1144 gtk_settings_set_string_property(gtk_settings_get_default(),
1150 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1151 went_offline_nm = FALSE;
1154 #ifdef HAVE_DBUS_GLIB
1156 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
1159 debug_print("Failed to open connection to system bus: %s\n", error->message);
1160 g_error_free(error);
1163 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1164 nm_proxy = dbus_g_proxy_new_for_name(connection,
1165 "org.freedesktop.NetworkManager",
1166 "/org/freedesktop/NetworkManager",
1167 "org.freedesktop.NetworkManager");
1169 #if NM_CHECK_VERSION(0,8,992)
1170 dbus_g_proxy_add_signal(nm_proxy, "StateChanged", G_TYPE_UINT, G_TYPE_INVALID);
1171 dbus_g_proxy_connect_signal(nm_proxy, "StateChanged",
1172 G_CALLBACK(networkmanager_state_change_cb),
1175 dbus_g_proxy_add_signal(nm_proxy, "StateChange", G_TYPE_UINT, G_TYPE_INVALID);
1176 dbus_g_proxy_connect_signal(nm_proxy, "StateChange",
1177 G_CALLBACK(networkmanager_state_change_cb),
1182 install_dbus_status_handler();
1188 osso_context = osso_initialize(OSSO_SERVICE, "2.8.1", TRUE, NULL);
1189 if (osso_context == NULL) {
1192 hildon_program = HILDON_PROGRAM(hildon_program_get_instance());
1193 static_osso_context = osso_context;
1195 gtk_widget_set_default_colormap(
1196 gdk_screen_get_system_colormap(
1197 gdk_screen_get_default()));
1199 gui_manager = gtkut_create_ui_manager();
1201 /* Create container for all the menus we will be adding */
1202 MENUITEM_ADDUI("/", "Menus", NULL, GTK_UI_MANAGER_MENUBAR);
1204 if (!g_thread_supported()) {
1205 g_error(_("g_thread is not supported by glib.\n"));
1209 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1211 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1214 /* no config dir exists. See if we can migrate an old config. */
1215 if (!is_dir_exist(RC_DIR)) {
1216 prefs_destroy_cache();
1219 /* if one of the old dirs exist, we'll ask if the user
1220 * want to migrates, and r will be TRUE if he said yes
1221 * and migration succeeded, and FALSE otherwise.
1223 if (is_dir_exist(OLD_GTK2_RC_DIR)) {
1224 r = migrate_old_config(OLD_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 2.6.0 (or older)"));
1225 asked_for_migration = TRUE;
1226 } else if (is_dir_exist(OLDER_GTK2_RC_DIR)) {
1227 r = migrate_old_config(OLDER_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.9.15 (or older)"));
1228 asked_for_migration = TRUE;
1229 } else if (is_dir_exist(OLD_GTK1_RC_DIR)) {
1230 r = migrate_old_config(OLD_GTK1_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.0.5 (or older)"));
1231 asked_for_migration = TRUE;
1232 } else if (is_dir_exist(SYLPHEED_RC_DIR)) {
1233 r = migrate_old_config(SYLPHEED_RC_DIR, RC_DIR, "Sylpheed");
1234 asked_for_migration = TRUE;
1237 /* If migration failed or the user didn't want to do it,
1238 * we create a new one (and we'll hit wizard later).
1240 if (r == FALSE && !is_dir_exist(RC_DIR)) {
1242 if (copy_dir(SYSCONFDIR "/skel/.claws-mail", RC_DIR) < 0) {
1244 if (!is_dir_exist(RC_DIR) && make_dir(RC_DIR) < 0) {
1257 if (!is_file_exist(RC_DIR G_DIR_SEPARATOR_S COMMON_RC) &&
1258 is_file_exist(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC)) {
1259 /* post 2.6 name change */
1260 migrate_common_rc(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC,
1261 RC_DIR G_DIR_SEPARATOR_S COMMON_RC);
1265 plugin_load_all("Common");
1267 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
1268 gtk_rc_parse(userrc);
1271 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1272 gtk_accel_map_load (userrc);
1276 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_rc_dir(), 1, win32_close_log(););
1278 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
1281 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
1282 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
1283 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
1284 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
1285 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
1286 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR);
1288 crash_file_present = is_file_exist(get_crashfile_name());
1289 /* remove temporary files */
1290 remove_all_files(get_tmp_dir());
1291 remove_all_files(get_mime_tmp_dir());
1293 if (!cmd.crash && crash_file_present)
1294 claws_crashed_bool = TRUE;
1296 if (is_file_exist("claws.log")) {
1297 if (rename_force("claws.log", "claws.log.bak") < 0)
1298 FILE_OP_ERROR("claws.log", "rename");
1300 set_log_file(LOG_PROTOCOL, "claws.log");
1302 if (is_file_exist("filtering.log")) {
1303 if (rename_force("filtering.log", "filtering.log.bak") < 0)
1304 FILE_OP_ERROR("filtering.log", "rename");
1306 set_log_file(LOG_DEBUG_FILTERING, "filtering.log");
1309 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1311 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1314 folder_system_init();
1315 prefs_common_read_config();
1317 prefs_themes_init();
1319 prefs_ext_prog_init();
1320 prefs_wrapping_init();
1321 prefs_compose_writing_init();
1322 prefs_msg_colors_init();
1323 image_viewer_init();
1324 prefs_image_viewer_init();
1326 prefs_summaries_init();
1327 prefs_message_init();
1329 prefs_logging_init();
1330 prefs_receive_init();
1334 gtkaspell_checkers_init();
1335 prefs_spelling_init();
1338 sock_set_io_timeout(prefs_common.io_timeout_secs);
1339 prefs_actions_read_config();
1340 prefs_display_header_read_config();
1341 /* prefs_filtering_read_config(); */
1342 #ifndef USE_NEW_ADDRBOOK
1343 addressbook_read_file();
1345 g_clear_error(&error);
1346 if (! addressbook_start_service(&error)) {
1347 g_warning("%s", error->message);
1348 g_clear_error(&error);
1351 addressbook_install_hooks(&error);
1354 gtkut_widget_init();
1355 stock_pixbuf_gdk(NULL, STOCK_PIXMAP_CLAWS_MAIL_ICON, &icon);
1356 gtk_window_set_default_icon(icon);
1358 folderview_initialize();
1364 mainwin = main_window_create();
1366 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1367 networkmanager_state_change_cb(nm_proxy,NULL,mainwin);
1372 appdata = g_new0(AppData, 1);
1373 appdata->program = hildon_program;
1374 appdata->window = HILDON_WINDOW(mainwin->window);
1375 appdata->osso_context = osso_context;
1376 result = osso_rpc_set_cb_f(appdata->osso_context,
1380 dbus_req_handler, appdata);
1381 if (result != OSSO_OK) {
1386 /* Add handler for Exit D-BUS messages */
1387 result = osso_application_set_exit_cb(appdata->osso_context,
1389 (gpointer) appdata);
1390 if (result != OSSO_OK) {
1394 osso_hw_set_event_cb( appdata->osso_context,
1395 NULL, hw_event_handler, (gpointer) appdata );
1397 manage_window_focus_in(mainwin->window, NULL, NULL);
1398 folderview = mainwin->folderview;
1400 gtk_cmclist_freeze(GTK_CMCLIST(mainwin->folderview->ctree));
1401 folder_item_update_freeze();
1403 /* register the callback of unix domain socket input */
1404 lock_socket_tag = claws_input_add(lock_socket,
1405 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
1406 lock_socket_input_cb,
1410 prefs_account_init();
1411 account_read_config_all();
1413 #ifdef HAVE_LIBETPAN
1414 imap_main_init(prefs_common.skip_ssl_cert_check);
1415 imap_main_set_timeout(prefs_common.io_timeout_secs);
1416 nntp_main_init(prefs_common.skip_ssl_cert_check);
1418 /* If we can't read a folder list or don't have accounts,
1419 * it means the configuration's not done. Either this is
1420 * a brand new install, either a failed/refused migration.
1421 * So we'll start the wizard.
1423 if (folder_read_list() < 0) {
1424 prefs_destroy_cache();
1426 /* if run_wizard returns FALSE it's because it's
1427 * been cancelled. We can't do much but exit.
1428 * however, if the user was asked for a migration,
1429 * we remove the newly created directory so that
1430 * he's asked again for migration on next launch.*/
1431 if (!run_wizard(mainwin, TRUE)) {
1432 if (asked_for_migration)
1433 remove_dir_recursive(RC_DIR);
1439 main_window_reflect_prefs_all_now();
1440 folder_write_list();
1444 if (!account_get_list()) {
1445 prefs_destroy_cache();
1446 if (!run_wizard(mainwin, FALSE)) {
1447 if (asked_for_migration)
1448 remove_dir_recursive(RC_DIR);
1454 if(!account_get_list()) {
1455 exit_claws(mainwin);
1465 toolbar_main_set_sensitive(mainwin);
1466 main_window_set_menu_sensitive(mainwin);
1468 /* if crashed, show window early so that the user
1469 * sees what's happening */
1470 if (claws_crashed())
1471 main_window_popup(mainwin);
1473 account_set_missing_folder();
1474 folder_set_missing_folders();
1475 folderview_set(folderview);
1477 prefs_matcher_read_config();
1478 quicksearch_set_search_strings(mainwin->summaryview->quicksearch);
1480 /* make one all-folder processing before using claws */
1481 main_window_cursor_wait(mainwin);
1482 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
1484 /* if claws crashed, rebuild caches */
1485 if (claws_crashed()) {
1487 debug_print("Claws Mail crashed, checking for new messages in local folders\n");
1488 folder_item_update_thaw();
1489 folderview_check_new(NULL);
1490 folder_clean_cache_memory_force();
1491 folder_item_update_freeze();
1493 /* make the crash-indicator file */
1494 str_write_to_file("foo", get_crashfile_name());
1496 inc_autocheck_timer_init(mainwin);
1498 /* ignore SIGPIPE signal for preventing sudden death of program */
1500 signal(SIGPIPE, SIG_IGN);
1502 if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
1503 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1505 if (cmd.online_mode == ONLINE_MODE_ONLINE) {
1506 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1509 if (cmd.status_folders) {
1510 g_ptr_array_free(cmd.status_folders, TRUE);
1511 cmd.status_folders = NULL;
1513 if (cmd.status_full_folders) {
1514 g_ptr_array_free(cmd.status_full_folders, TRUE);
1515 cmd.status_full_folders = NULL;
1518 claws_register_idle_function(claws_gtk_idle);
1520 prefs_toolbar_init();
1522 num_folder_class = g_list_length(folder_get_list());
1524 plugin_load_all("GTK2");
1526 if (g_list_length(folder_get_list()) != num_folder_class) {
1527 debug_print("new folders loaded, reloading processing rules\n");
1528 prefs_matcher_read_config();
1531 if ((plug_list = plugin_get_unloaded_list()) != NULL) {
1534 gint num_plugins = 0;
1535 for (cur = plug_list; cur; cur = cur->next) {
1536 Plugin *plugin = (Plugin *)cur->data;
1537 gchar *tmp = g_strdup_printf("%s\n%s",
1539 plugin_get_name(plugin));
1544 main_window_cursor_normal(mainwin);
1545 main_window_popup(mainwin);
1546 alertpanel_warning(ngettext(
1547 "The following plugin failed to load. "
1548 "Check the Plugins configuration "
1549 "for more information:\n%s",
1550 "The following plugins failed to load. "
1551 "Check the Plugins configuration "
1552 "for more information:\n%s",
1555 main_window_cursor_wait(mainwin);
1557 g_slist_free(plug_list);
1561 prefs_common_write_config();
1562 plugin_load_standard_plugins ();
1564 /* if not crashed, show window now */
1565 if (!claws_crashed()) {
1566 /* apart if something told not to show */
1567 if (show_at_startup)
1568 main_window_popup(mainwin);
1571 if (!folder_have_mailbox()) {
1572 prefs_destroy_cache();
1573 main_window_cursor_normal(mainwin);
1574 if (folder_get_list() != NULL) {
1575 alertpanel_error(_("Claws Mail has detected a configured "
1576 "mailbox, but it is incomplete. It is "
1577 "possibly due to a failing IMAP account. Use "
1578 "\"Rebuild folder tree\" on the mailbox parent "
1579 "folder's context menu to try to fix it."));
1581 alertpanel_error(_("Claws Mail has detected a configured "
1582 "mailbox, but could not load it. It is "
1583 "probably provided by an out-of-date "
1584 "external plugin. Please reinstall the "
1585 "plugin and try again."));
1586 exit_claws(mainwin);
1594 static_mainwindow = mainwin;
1597 if (prefs_common.data_root != NULL && *prefs_common.data_root != '\0') {
1598 GnomeVFSVolume *vol = NULL;
1599 gchar *uri, *mount_path;
1601 volmon = gnome_vfs_get_volume_monitor();
1602 vol = gnome_vfs_volume_monitor_get_volume_for_path(volmon, prefs_common.data_root);
1604 uri = gnome_vfs_volume_get_activation_uri (vol);
1605 mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
1608 if (vol == NULL || !gnome_vfs_volume_is_mounted(vol)
1609 || strcmp(mount_path, prefs_common.data_root)) {
1610 alertpanel_error(_("Claws Mail can not start without its data volume (%s)."),
1611 prefs_common.data_root);
1613 gnome_vfs_volume_unref(vol);
1614 exit_claws(mainwin);
1618 gnome_vfs_volume_unref(vol);
1619 g_signal_connect(G_OBJECT(volmon),
1620 "volume-mounted", G_CALLBACK(main_vol_mount_cb), mainwin);
1621 g_signal_connect(G_OBJECT(volmon),
1622 "volume-unmounted", G_CALLBACK(main_vol_unmount_cb), mainwin);
1626 #ifdef HAVE_STARTUP_NOTIFICATION
1627 startup_notification_complete(FALSE);
1630 sc_session_manager_connect(mainwin);
1633 folder_item_update_thaw();
1634 gtk_cmclist_thaw(GTK_CMCLIST(mainwin->folderview->ctree));
1635 main_window_cursor_normal(mainwin);
1637 if (!cmd.target && prefs_common.goto_last_folder_on_startup &&
1638 folder_find_item_from_identifier(prefs_common.last_opened_folder) != NULL &&
1640 cmd.target = prefs_common.last_opened_folder;
1643 if (cmd.receive_all && !cmd.target) {
1645 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(FALSE));
1646 } else if (prefs_common.chk_on_startup && !cmd.target) {
1648 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(TRUE));
1649 } else if (cmd.receive && !cmd.target) {
1651 g_timeout_add(1000, defer_check, NULL);
1653 gtk_widget_grab_focus(folderview->ctree);
1656 open_compose_new(cmd.compose_mailto, cmd.attach_files);
1658 if (cmd.attach_files) {
1659 list_free_strings(cmd.attach_files);
1660 g_list_free(cmd.attach_files);
1661 cmd.attach_files = NULL;
1663 if (cmd.subscribe) {
1664 folder_subscribe(cmd.subscribe_uri);
1673 g_timeout_add(500, defer_jump, (gpointer)cmd.target);
1676 prefs_destroy_cache();
1678 compose_reopen_exit_drafts();
1681 sc_starting = FALSE;
1682 main_window_set_menu_sensitive(mainwin);
1683 toolbar_main_set_sensitive(mainwin);
1690 osso_deinitialize(osso_context);
1692 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1694 g_object_unref(nm_proxy);
1696 #ifdef HAVE_DBUS_GLIB
1697 uninstall_dbus_status_handler();
1699 dbus_g_connection_unref(connection);
1705 exit_claws(mainwin);
1710 static void save_all_caches(FolderItem *item, gpointer data)
1717 folder_item_close(item);
1720 folder_item_free_cache(item, TRUE);
1723 static void exit_claws(MainWindow *mainwin)
1726 gboolean have_connectivity;
1730 debug_print("shutting down\n");
1731 inc_autocheck_timer_remove();
1733 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1734 if (prefs_common.work_offline && went_offline_nm)
1735 prefs_common.work_offline = FALSE;
1738 /* save prefs for opened folder */
1739 if(mainwin->folderview->opened) {
1742 item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
1743 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
1744 prefs_common.last_opened_folder = folder_item_get_identifier(item);
1747 /* save all state before exiting */
1748 folder_func_to_all_folders(save_all_caches, NULL);
1749 folder_write_list();
1751 main_window_get_size(mainwin);
1752 main_window_get_position(mainwin);
1754 prefs_common_write_config();
1755 account_write_config_all();
1756 #ifndef USE_NEW_ADDRBOOK
1757 addressbook_export_to_file();
1759 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1760 gtk_accel_map_save(filename);
1763 /* delete temporary files */
1764 remove_all_files(get_tmp_dir());
1765 remove_all_files(get_mime_tmp_dir());
1767 close_log_file(LOG_PROTOCOL);
1768 close_log_file(LOG_DEBUG_FILTERING);
1770 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1771 have_connectivity = networkmanager_is_online(NULL);
1773 have_connectivity = TRUE;
1775 #ifdef HAVE_LIBETPAN
1776 imap_main_done(have_connectivity);
1777 nntp_main_done(have_connectivity);
1779 /* delete crashfile */
1781 claws_unlink(get_crashfile_name());
1783 lock_socket_remove();
1786 if (mainwin->smc_conn)
1787 SmcCloseConnection ((SmcConn)mainwin->smc_conn, 0, NULL);
1788 mainwin->smc_conn = NULL;
1791 main_window_destroy_all();
1793 plugin_unload_all("GTK2");
1795 prefs_toolbar_done();
1797 #ifndef USE_NEW_ADDRBOOK
1798 addressbook_destroy();
1800 prefs_themes_done();
1802 prefs_ext_prog_done();
1803 prefs_wrapping_done();
1804 prefs_compose_writing_done();
1805 prefs_msg_colors_done();
1806 prefs_image_viewer_done();
1807 image_viewer_done();
1809 prefs_summaries_done();
1810 prefs_message_done();
1812 prefs_receive_done();
1813 prefs_logging_done();
1817 prefs_spelling_done();
1818 gtkaspell_checkers_quit();
1820 plugin_unload_all("Common");
1824 #define G_STRING_APPEND_ENCODED_URI(gstring,source) \
1826 gchar tmpbuf[BUFFSIZE]; \
1827 encode_uri(tmpbuf, BUFFSIZE, (source)); \
1828 g_string_append((gstring), tmpbuf); \
1831 #define G_PRINT_EXIT(msg) \
1837 static GString * parse_cmd_compose_from_file(const gchar *fn)
1839 GString *headers = g_string_new(NULL);
1840 GString *body = g_string_new(NULL);
1848 if (fn == NULL || *fn == '\0')
1849 G_PRINT_EXIT(_("Missing filename\n"));
1850 isstdin = (*fn == '-' && *(fn + 1) == '\0');
1854 fp = g_fopen(fn, "r");
1856 G_PRINT_EXIT(_("Cannot open filename for reading\n"));
1859 while (fgets(fb, sizeof(fb), fp)) {
1865 while (*h && *h != ':') { ++h; } /* search colon */
1867 G_PRINT_EXIT(_("Malformed header\n"));
1869 while (*v && *v == ' ') { ++v; } /* trim value start */
1871 tmp = g_ascii_strdown(fb, -1); /* get header name */
1872 if (!strcmp(tmp, "to")) {
1874 G_PRINT_EXIT(_("Duplicated 'To:' header\n"));
1877 g_string_append_c(headers, '&');
1878 g_string_append(headers, tmp);
1879 g_string_append_c(headers, '=');
1880 #if GLIB_CHECK_VERSION(2,16,0)
1881 g_string_append_uri_escaped(headers, v, NULL, TRUE);
1883 G_STRING_APPEND_ENCODED_URI(headers, v);
1889 G_PRINT_EXIT(_("Missing required 'To:' header\n"));
1890 g_string_append(body, to);
1892 g_string_append(body, "?body=");
1893 while (fgets(fb, sizeof(fb), fp)) {
1894 #if GLIB_CHECK_VERSION(2,16,0)
1895 g_string_append_uri_escaped(body, fb, NULL, TRUE);
1897 G_STRING_APPEND_ENCODED_URI(body, fb);
1902 /* append the remaining headers */
1903 g_string_append(body, headers->str);
1904 g_string_free(headers, TRUE);
1909 #undef G_STRING_APPEND_ENCODED_URI
1912 static void parse_cmd_opt(int argc, char *argv[])
1917 for (i = 1; i < argc; i++) {
1918 if (!strncmp(argv[i], "--receive-all", 13)) {
1919 cmd.receive_all = TRUE;
1920 } else if (!strncmp(argv[i], "--receive", 9)) {
1922 } else if (!strncmp(argv[i], "--compose-from-file", 19)) {
1923 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1925 GString *mailto = parse_cmd_compose_from_file(p);
1927 cmd.compose_mailto = mailto->str;
1929 } else if (!strncmp(argv[i], "--compose", 9)) {
1930 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1933 cmd.compose_mailto = NULL;
1934 if (p && *p != '\0' && *p != '-') {
1935 if (!strncmp(p, "mailto:", 7)) {
1936 cmd.compose_mailto = p + 7;
1938 cmd.compose_mailto = p;
1942 } else if (!strncmp(argv[i], "--subscribe", 11)) {
1943 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1944 if (p && *p != '\0' && *p != '-') {
1945 cmd.subscribe = TRUE;
1946 cmd.subscribe_uri = p;
1948 } else if (!strncmp(argv[i], "--attach", 8)) {
1949 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1952 while (p && *p != '\0' && *p != '-') {
1953 if ((file = g_filename_from_uri(p, NULL, NULL)) != NULL) {
1954 if (!is_file_exist(file)) {
1959 if (file == NULL && *p != G_DIR_SEPARATOR) {
1960 file = g_strconcat(claws_get_startup_dir(),
1963 } else if (file == NULL) {
1966 ainfo = g_new0(AttachInfo, 1);
1968 cmd.attach_files = g_list_append(cmd.attach_files, ainfo);
1970 p = (i+1 < argc)?argv[i+1]:NULL;
1972 } else if (!strncmp(argv[i], "--send", 6)) {
1974 } else if (!strncmp(argv[i], "--version-full", 14) ||
1975 !strncmp(argv[i], "-V", 2)) {
1976 g_print("Claws Mail version " VERSION "\n");
1977 main_dump_features_list(FALSE);
1979 } else if (!strncmp(argv[i], "--version", 9) ||
1980 !strncmp(argv[i], "-v", 2)) {
1981 g_print("Claws Mail version " VERSION "\n");
1983 } else if (!strncmp(argv[i], "--status-full", 13)) {
1984 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1986 cmd.status_full = TRUE;
1987 while (p && *p != '\0' && *p != '-') {
1988 if (!cmd.status_full_folders) {
1989 cmd.status_full_folders =
1992 g_ptr_array_add(cmd.status_full_folders,
1995 p = (i+1 < argc)?argv[i+1]:NULL;
1997 } else if (!strncmp(argv[i], "--status", 8)) {
1998 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
2001 while (p && *p != '\0' && *p != '-') {
2002 if (!cmd.status_folders)
2003 cmd.status_folders = g_ptr_array_new();
2004 g_ptr_array_add(cmd.status_folders,
2007 p = (i+1 < argc)?argv[i+1]:NULL;
2009 } else if (!strncmp(argv[i], "--search", 8)) {
2010 cmd.search_folder = (i+1 < argc)?argv[i+1]:NULL;
2011 cmd.search_type = (i+2 < argc)?argv[i+2]:NULL;
2012 cmd.search_request = (i+3 < argc)?argv[i+3]:NULL;
2013 const char* rec = (i+4 < argc)?argv[i+4]:NULL;
2014 cmd.search_recursive = TRUE;
2015 if (rec && (tolower(*rec)=='n' || tolower(*rec)=='f' || *rec=='0'))
2016 cmd.search_recursive = FALSE;
2017 if (cmd.search_folder && cmd.search_type && cmd.search_request)
2019 } else if (!strncmp(argv[i], "--online", 8)) {
2020 cmd.online_mode = ONLINE_MODE_ONLINE;
2021 } else if (!strncmp(argv[i], "--offline", 9)) {
2022 cmd.online_mode = ONLINE_MODE_OFFLINE;
2023 } else if (!strncmp(argv[i], "--statistics", 12)) {
2024 cmd.statistics = TRUE;
2025 } else if (!strncmp(argv[i], "--reset-statistics", 18)) {
2026 cmd.reset_statistics = TRUE;
2027 } else if (!strncmp(argv[i], "--help", 6) ||
2028 !strncmp(argv[i], "-h", 2)) {
2029 gchar *base = g_path_get_basename(argv[0]);
2030 g_print(_("Usage: %s [OPTION]...\n"), base);
2032 g_print("%s\n", _(" --compose [address] open composition window"));
2033 g_print("%s\n", _(" --compose-from-file file\n"
2034 " open composition window with data from given file;\n"
2035 " use - as file name for reading from standard input;\n"
2036 " content format: headers first (To: required) until an\n"
2037 " empty line, then mail body until end of file."));
2038 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
2039 g_print("%s\n", _(" --attach file1 [file2]...\n"
2040 " open composition window with specified files\n"
2042 g_print("%s\n", _(" --receive receive new messages"));
2043 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
2044 g_print("%s\n", _(" --search folder type request [recursive]\n"
2046 " folder ex.: \"#mh/Mailbox/inbox\" or \"Mail\"\n"
2047 " type: s[ubject],f[rom],t[o],e[xtended],m[ixed] or g: tag\n"
2048 " request: search string\n"
2049 " recursive: false if arg. starts with 0, n, N, f or F"));
2051 g_print("%s\n", _(" --send send all queued messages"));
2052 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
2053 g_print("%s\n", _(" --status-full [folder]...\n"
2054 " show the status of each folder"));
2055 g_print("%s\n", _(" --statistics show session statistics"));
2056 g_print("%s\n", _(" --reset-statistics reset session statistics"));
2057 g_print("%s\n", _(" --select folder[/msg] jumps to the specified folder/message\n"
2058 " folder is a folder id like 'folder/sub_folder'"));
2059 g_print("%s\n", _(" --online switch to online mode"));
2060 g_print("%s\n", _(" --offline switch to offline mode"));
2061 g_print("%s\n", _(" --exit --quit -q exit Claws Mail"));
2062 g_print("%s\n", _(" --debug debug mode"));
2063 g_print("%s\n", _(" --help -h display this help and exit"));
2064 g_print("%s\n", _(" --version -v output version information and exit"));
2065 g_print("%s\n", _(" --version-full -V output version and built-in features information and exit"));
2066 g_print("%s\n", _(" --config-dir output configuration directory"));
2067 g_print("%s\n", _(" --alternate-config-dir [dir]\n"
2068 " use specified configuration directory"));
2072 } else if (!strncmp(argv[i], "--crash", 7)) {
2074 cmd.crash_params = g_strdup((i+1 < argc)?argv[i+1]:NULL);
2076 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
2077 g_print(RC_DIR "\n");
2079 } else if (!strncmp(argv[i], "--alternate-config-dir", sizeof "--alternate-config-dir" - 1) && i+1 < argc) {
2080 set_rc_dir(argv[i+1]);
2081 } else if (!strncmp(argv[i], "--exit", 6) ||
2082 !strncmp(argv[i], "--quit", 6) ||
2083 !strncmp(argv[i], "-q", 2)) {
2085 } else if (!strncmp(argv[i], "--select", 8) && i+1 < argc) {
2086 cmd.target = argv[i+1];
2087 } else if (i == 1 && argc == 2) {
2088 /* only one parameter. Do something intelligent about it */
2089 if ((strstr(argv[i], "@")||!strncmp(argv[i], "mailto:", 7)) && !strstr(argv[i], "://")) {
2090 const gchar *p = argv[i];
2093 cmd.compose_mailto = NULL;
2094 if (p && *p != '\0' && *p != '-') {
2095 if (!strncmp(p, "mailto:", 7)) {
2096 cmd.compose_mailto = p + 7;
2098 cmd.compose_mailto = p;
2101 } else if (!strncmp(argv[i], "file://", 7)) {
2102 cmd.target = argv[i];
2103 } else if (!strncmp(argv[i], "?attach=file://", strlen("?attach=file://"))) {
2105 cmd.compose_mailto = argv[i];
2106 } else if (strstr(argv[i], "://")) {
2107 const gchar *p = argv[i];
2108 if (p && *p != '\0' && *p != '-') {
2109 cmd.subscribe = TRUE;
2110 cmd.subscribe_uri = p;
2112 } else if (!strcmp(argv[i], "--sync")) {
2114 } else if (is_dir_exist(argv[i]) || is_file_exist(argv[i])) {
2115 cmd.target = argv[i];
2117 g_print(_("Unknown option\n"));
2123 if (cmd.attach_files && cmd.compose == FALSE) {
2125 cmd.compose_mailto = NULL;
2129 static void initial_processing(FolderItem *item, gpointer data)
2131 MainWindow *mainwin = (MainWindow *)data;
2134 cm_return_if_fail(item);
2135 buf = g_strdup_printf(_("Processing (%s)..."),
2138 : _("top level folder"));
2142 if (item->prefs->enable_processing) {
2143 item->processing_pending = TRUE;
2144 folder_item_apply_processing(item);
2145 item->processing_pending = FALSE;
2148 STATUSBAR_POP(mainwin);
2151 static gboolean draft_all_messages(void)
2153 GList *compose_list = NULL;
2155 compose_clear_exit_drafts();
2156 compose_list = compose_get_compose_list();
2157 while (compose_list != NULL) {
2158 Compose *c = (Compose*)compose_list->data;
2159 if (!compose_draft(c, COMPOSE_DRAFT_FOR_EXIT))
2161 compose_list = compose_get_compose_list();
2165 gboolean clean_quit(gpointer data)
2167 static gboolean firstrun = TRUE;
2174 /*!< Good idea to have the main window stored in a
2175 * static variable so we can check that variable
2176 * to see if we're really allowed to do things
2177 * that actually the spawner is supposed to
2178 * do (like: sending mail, composing messages).
2179 * Because, really, if we're the spawnee, and
2180 * we touch GTK stuff, we're hosed. See the
2183 /* FIXME: Use something else to signal that we're
2184 * in the original spawner, and not in a spawned
2186 if (!static_mainwindow) {
2190 draft_all_messages();
2191 emergency_exit = TRUE;
2192 exit_claws(static_mainwindow);
2198 void app_will_exit(GtkWidget *widget, gpointer data)
2200 MainWindow *mainwin = data;
2202 if (gtk_main_level() == 0) {
2203 debug_print("not even started\n");
2206 if (sc_exiting == TRUE) {
2207 debug_print("exit pending\n");
2211 debug_print("exiting\n");
2212 if (compose_get_compose_list()) {
2213 if (!draft_all_messages()) {
2214 main_window_popup(mainwin);
2220 if (prefs_common.warn_queued_on_exit && procmsg_have_queued_mails_fast()) {
2221 if (alertpanel(_("Queued messages"),
2222 _("Some unsent messages are queued. Exit now?"),
2223 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL)
2224 != G_ALERTALTERNATE) {
2225 main_window_popup(mainwin);
2229 manage_window_focus_in(mainwin->window, NULL, NULL);
2233 #ifdef HAVE_VALGRIND
2234 if (RUNNING_ON_VALGRIND) {
2235 summary_clear_list(mainwin->summaryview);
2238 if (folderview_get_selected_item(mainwin->folderview))
2239 folder_item_close(folderview_get_selected_item(mainwin->folderview));
2243 gboolean claws_is_exiting(void)
2248 gboolean claws_is_starting(void)
2254 * CLAWS: want this public so crash dialog can delete the
2257 gchar *claws_get_socket_name(void)
2259 static gchar *filename = NULL;
2260 const gchar *socket_dir = NULL;
2262 if (rc_dir_is_alt())
2263 socket_dir = get_rc_dir();
2265 socket_dir = g_get_tmp_dir();
2266 if (filename == NULL) {
2267 filename = g_strdup_printf("%s%cclaws-mail-%d",
2268 socket_dir, G_DIR_SEPARATOR,
2279 static gchar *get_crashfile_name(void)
2281 static gchar *filename = NULL;
2283 if (filename == NULL) {
2284 filename = g_strdup_printf("%s%cclaws-crashed",
2285 get_tmp_dir(), G_DIR_SEPARATOR);
2291 static gint prohibit_duplicate_launch(void)
2298 path = claws_get_socket_name();
2299 uxsock = fd_connect_unix(path);
2301 if (x_display == NULL)
2302 x_display = g_strdup(g_getenv("DISPLAY"));
2306 return fd_open_unix(path);
2311 hmutex = CreateMutexA(NULL, FALSE, "ClawsMail");
2313 debug_print("cannot create Mutex\n");
2316 if (GetLastError() != ERROR_ALREADY_EXISTS) {
2317 uxsock = fd_open_inet(50216);
2323 uxsock = fd_connect_inet(50216);
2327 /* remote command mode */
2329 debug_print("another Claws Mail instance is already running.\n");
2331 if (cmd.receive_all) {
2332 fd_write_all(uxsock, "receive_all\n", 12);
2333 } else if (cmd.receive) {
2334 fd_write_all(uxsock, "receive\n", 8);
2335 } else if (cmd.compose && cmd.attach_files) {
2336 gchar *str, *compose_str;
2338 if (cmd.compose_mailto) {
2339 compose_str = g_strdup_printf("compose_attach %s\n",
2340 cmd.compose_mailto);
2342 compose_str = g_strdup("compose_attach\n");
2345 fd_write_all(uxsock, compose_str, strlen(compose_str));
2346 g_free(compose_str);
2348 for (curr = cmd.attach_files; curr != NULL ; curr = curr->next) {
2349 str = (gchar *) ((AttachInfo *)curr->data)->file;
2350 fd_write_all(uxsock, str, strlen(str));
2351 fd_write_all(uxsock, "\n", 1);
2354 fd_write_all(uxsock, ".\n", 2);
2355 } else if (cmd.compose) {
2358 if (cmd.compose_mailto) {
2359 compose_str = g_strdup_printf
2360 ("compose %s\n", cmd.compose_mailto);
2362 compose_str = g_strdup("compose\n");
2365 fd_write_all(uxsock, compose_str, strlen(compose_str));
2366 g_free(compose_str);
2367 } else if (cmd.subscribe) {
2368 gchar *str = g_strdup_printf("subscribe %s\n", cmd.subscribe_uri);
2369 fd_write_all(uxsock, str, strlen(str));
2371 } else if (cmd.send) {
2372 fd_write_all(uxsock, "send\n", 5);
2373 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
2374 fd_write(uxsock, "online\n", 6);
2375 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
2376 fd_write(uxsock, "offline\n", 7);
2377 } else if (cmd.status || cmd.status_full) {
2378 gchar buf[BUFFSIZE];
2380 const gchar *command;
2384 command = cmd.status_full ? "status-full\n" : "status\n";
2385 folders = cmd.status_full ? cmd.status_full_folders :
2388 fd_write_all(uxsock, command, strlen(command));
2389 for (i = 0; folders && i < folders->len; ++i) {
2390 folder = g_ptr_array_index(folders, i);
2391 fd_write_all(uxsock, folder, strlen(folder));
2392 fd_write_all(uxsock, "\n", 1);
2394 fd_write_all(uxsock, ".\n", 2);
2396 fd_gets(uxsock, buf, sizeof(buf));
2397 if (!strncmp(buf, ".\n", 2)) break;
2400 } else if (cmd.exit) {
2401 fd_write_all(uxsock, "exit\n", 5);
2402 } else if (cmd.statistics) {
2404 fd_write(uxsock, "statistics\n", 11);
2406 fd_gets(uxsock, buf, sizeof(buf));
2407 if (!strncmp(buf, ".\n", 2)) break;
2410 } else if (cmd.reset_statistics) {
2411 fd_write(uxsock, "reset_statistics\n", 17);
2412 } else if (cmd.target) {
2413 gchar *str = g_strdup_printf("select %s\n", cmd.target);
2414 fd_write_all(uxsock, str, strlen(str));
2416 } else if (cmd.search) {
2417 gchar buf[BUFFSIZE];
2419 g_strdup_printf("search %s\n%s\n%s\n%c\n",
2420 cmd.search_folder, cmd.search_type, cmd.search_request,
2421 (cmd.search_recursive==TRUE)?'1':'0');
2422 fd_write_all(uxsock, str, strlen(str));
2425 fd_gets(uxsock, buf, sizeof(buf));
2426 if (!strncmp(buf, ".\n", 2)) break;
2432 fd_write_all(uxsock, "get_display\n", 12);
2433 memset(buf, 0, sizeof(buf));
2434 fd_gets(uxsock, buf, sizeof(buf));
2435 if (strcmp2(buf, x_display)) {
2436 g_print("Claws Mail is already running on display %s.\n",
2440 uxsock = fd_connect_unix(path);
2441 fd_write_all(uxsock, "popup\n", 6);
2444 fd_write_all(uxsock, "popup\n", 6);
2452 static gint lock_socket_remove(void)
2457 if (lock_socket < 0) {
2461 if (lock_socket_tag > 0) {
2462 g_source_remove(lock_socket_tag);
2464 fd_close(lock_socket);
2467 filename = claws_get_socket_name();
2468 claws_unlink(filename);
2474 static GPtrArray *get_folder_item_list(gint sock)
2476 gchar buf[BUFFSIZE];
2478 GPtrArray *folders = NULL;
2481 fd_gets(sock, buf, sizeof(buf));
2482 if (!strncmp(buf, ".\n", 2)) {
2487 folders = g_ptr_array_new();
2489 item = folder_find_item_from_identifier(buf);
2491 g_ptr_array_add(folders, item);
2493 g_warning("no such folder: %s\n", buf);
2500 static void lock_socket_input_cb(gpointer data,
2502 GIOCondition condition)
2504 MainWindow *mainwin = (MainWindow *)data;
2506 gchar buf[BUFFSIZE];
2507 /* re-use the same quicksearch (& avoid matcher_list mem.leaks) */
2508 static QuickSearch *quicksearch = NULL;
2510 sock = fd_accept(source);
2511 fd_gets(sock, buf, sizeof(buf));
2513 if (!strncmp(buf, "popup", 5)) {
2514 main_window_popup(mainwin);
2515 } else if (!strncmp(buf, "get_display", 11)) {
2516 fd_write_all(sock, x_display, strlen(x_display));
2517 } else if (!strncmp(buf, "receive_all", 11)) {
2518 inc_all_account_mail(mainwin, FALSE,
2519 prefs_common.newmail_notify_manu);
2520 } else if (!strncmp(buf, "receive", 7)) {
2521 inc_mail(mainwin, prefs_common.newmail_notify_manu);
2522 } else if (!strncmp(buf, "compose_attach", 14)) {
2523 GList *files = NULL, *curr;
2527 mailto = g_strdup(buf + strlen("compose_attach") + 1);
2528 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
2530 if (!strcmp2(buf, "."))
2533 ainfo = g_new0(AttachInfo, 1);
2534 ainfo->file = g_strdup(buf);
2535 files = g_list_append(files, ainfo);
2537 open_compose_new(mailto, files);
2539 curr = g_list_first(files);
2540 while (curr != NULL) {
2541 ainfo = (AttachInfo *)curr->data;
2542 g_free(ainfo->file);
2548 } else if (!strncmp(buf, "compose", 7)) {
2549 open_compose_new(buf + strlen("compose") + 1, NULL);
2550 } else if (!strncmp(buf, "subscribe", 9)) {
2551 main_window_popup(mainwin);
2552 folder_subscribe(buf + strlen("subscribe") + 1);
2553 } else if (!strncmp(buf, "send", 4)) {
2555 } else if (!strncmp(buf, "online", 6)) {
2556 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
2557 } else if (!strncmp(buf, "offline", 7)) {
2558 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
2559 } else if (!strncmp(buf, "status-full", 11) ||
2560 !strncmp(buf, "status", 6)) {
2564 folders = get_folder_item_list(sock);
2565 status = folder_get_status
2566 (folders, !strncmp(buf, "status-full", 11));
2567 fd_write_all(sock, status, strlen(status));
2568 fd_write_all(sock, ".\n", 2);
2570 if (folders) g_ptr_array_free(folders, TRUE);
2571 } else if (!strncmp(buf, "statistics", 10)) {
2574 g_snprintf(tmp, sizeof(tmp), _("Session statistics\n"));
2575 fd_write_all(sock, tmp, strlen(tmp));
2577 if (prefs_common.date_format) {
2582 lt = localtime(&session_stats.time_started);
2583 fast_strftime(date, len, prefs_common.date_format, lt);
2584 g_snprintf(tmp, sizeof(tmp), _("Started: %s\n"),
2585 lt ? date : ctime(&session_stats.time_started));
2587 g_snprintf(tmp, sizeof(tmp), _("Started: %s\n"),
2588 ctime(&session_stats.time_started));
2589 fd_write_all(sock, tmp, strlen(tmp));
2591 fd_write_all(sock, "\n", 1);
2593 g_snprintf(tmp, sizeof(tmp), _("Incoming traffic\n"));
2594 fd_write_all(sock, tmp, strlen(tmp));
2596 g_snprintf(tmp, sizeof(tmp), _("Received messages: %d\n"),
2597 session_stats.received);
2598 fd_write_all(sock, tmp, strlen(tmp));
2600 fd_write_all(sock, "\n", 1);
2602 g_snprintf(tmp, sizeof(tmp), _("Outgoing traffic\n"));
2603 fd_write_all(sock, tmp, strlen(tmp));
2605 g_snprintf(tmp, sizeof(tmp), _("New/redirected messages: %d\n"),
2606 session_stats.sent);
2607 fd_write_all(sock, tmp, strlen(tmp));
2609 g_snprintf(tmp, sizeof(tmp), _("Replied messages: %d\n"),
2610 session_stats.replied);
2611 fd_write_all(sock, tmp, strlen(tmp));
2613 g_snprintf(tmp, sizeof(tmp), _("Forwarded messages: %d\n"),
2614 session_stats.forwarded);
2615 fd_write_all(sock, tmp, strlen(tmp));
2617 g_snprintf(tmp, sizeof(tmp), _("Total outgoing messages: %d\n"),
2618 (session_stats.sent + session_stats.replied +
2619 session_stats.forwarded));
2620 fd_write_all(sock, tmp, strlen(tmp));
2622 fd_write_all(sock, ".\n", 2);
2623 } else if (!strncmp(buf, "reset_statistics", 16)) {
2625 } else if (!strncmp(buf, "select ", 7)) {
2626 const gchar *target = buf+7;
2627 mainwindow_jump_to(target, TRUE);
2628 } else if (!strncmp(buf, "search ", 7)) {
2629 FolderItem* folderItem = NULL;
2630 GSList *messages = NULL;
2631 gchar *folder_name, *request;
2632 QuickSearchType searchType = QUICK_SEARCH_EXTENDED;
2635 if (quicksearch==NULL)
2636 quicksearch = quicksearch_new_nogui();
2638 folder_name = g_strdup(buf+7);
2639 strretchomp(folder_name);
2641 if (fd_gets(sock, buf, sizeof(buf)) <= 0) {
2642 g_free(folder_name);
2645 searchType = quicksearch_type(buf);
2646 if (fd_gets(sock, buf, sizeof(buf)) <= 0) {
2647 g_free(folder_name);
2650 request = g_strdup(buf);
2651 strretchomp(request);
2654 if (fd_gets(sock, buf, sizeof(buf)) > 0)
2658 debug_print("search: %s %i %s %i\n",folder_name,searchType,request,recursive);
2661 folderItem = folder_find_item_from_identifier(folder_name);
2662 if (folder_name && folderItem == NULL) {
2663 debug_print("Unknown folder item : '%s', searching folder\n",folder_name);
2664 Folder* folder = folder_find_from_path(folder_name);
2666 folderItem = FOLDER_ITEM(folder->node->data);
2668 debug_print("Unknown folder: '%s'\n",folder_name);
2670 debug_print("%s %s\n",folderItem->name, folderItem->path);
2672 if (folderItem != NULL) {
2673 quicksearch_set(quicksearch, searchType, request);
2674 quicksearch_set_recursive(quicksearch, recursive);
2675 search_msgs_in_folders(&messages, quicksearch, folderItem);
2677 g_print("Folder '%s' not found.\n'", folder_name);
2681 for (cur=messages; cur != NULL; cur = cur->next) {
2682 MsgInfo* msg = (MsgInfo *)cur->data;
2683 gchar *file = procmsg_get_message_file_path(msg);
2684 fd_write_all(sock, file, strlen(file));
2685 fd_write_all(sock, "\n", 1);
2688 fd_write_all(sock, ".\n", 2);
2690 if (messages != NULL)
2691 procmsg_msg_list_free(messages);
2692 g_free(folder_name);
2694 } else if (!strncmp(buf, "exit", 4)) {
2695 if (prefs_common.clean_on_exit && !prefs_common.ask_on_clean) {
2696 procmsg_empty_all_trash();
2698 app_will_exit(NULL, mainwin);
2704 static void open_compose_new(const gchar *address, GList *attach_files)
2709 Xstrdup_a(addr, address, return);
2713 compose_new(NULL, addr, attach_files);
2716 static void send_queue(void)
2719 gchar *errstr = NULL;
2720 gboolean error = FALSE;
2721 for (list = folder_get_list(); list != NULL; list = list->next) {
2722 Folder *folder = list->data;
2724 if (folder->queue) {
2725 gint res = procmsg_send_queue
2726 (folder->queue, prefs_common.savemsg,
2730 folder_item_scan(folder->queue);
2738 alertpanel_error_log(_("Some errors occurred "
2739 "while sending queued messages:\n%s"), errstr);
2742 alertpanel_error_log("Some errors occurred "
2743 "while sending queued messages.");
2747 static void quit_signal_handler(int sig)
2749 debug_print("Quitting on signal %d\n", sig);
2751 g_timeout_add(0, clean_quit, NULL);
2754 static void install_basic_sighandlers()
2758 struct sigaction act;
2763 sigaddset(&mask, SIGTERM);
2766 sigaddset(&mask, SIGINT);
2769 sigaddset(&mask, SIGHUP);
2772 act.sa_handler = quit_signal_handler;
2777 sigaction(SIGTERM, &act, 0);
2780 sigaction(SIGINT, &act, 0);
2783 sigaction(SIGHUP, &act, 0);
2786 sigprocmask(SIG_UNBLOCK, &mask, 0);
2787 #endif /* !G_OS_WIN32 */
2790 #if (defined linux && defined SIGIO)
2791 static int mem_notify_fd = 0;
2793 static gboolean clean_caches(gpointer unused)
2795 if (static_mainwindow && static_mainwindow->lock_count > 0)
2797 debug_print("/dev/mem_notify: callback: Freeing some memory now!\n");
2798 folder_clean_cache_memory_force();
2802 static void memory_signal_handler(int sig)
2804 debug_print("/dev/mem_notify: Kernel says we should free up some memory!\n");
2805 g_timeout_add(10, clean_caches, NULL);
2808 static void install_memory_sighandler()
2811 struct sigaction act;
2814 mem_notify_fd = g_open("/dev/mem_notify", O_RDONLY|O_NONBLOCK, 0);
2815 if (mem_notify_fd == -1) {
2816 debug_print("/dev/mem_notify not available (%s)\n",
2821 fcntl(mem_notify_fd, F_SETOWN, getpid());
2822 flags = fcntl(mem_notify_fd, F_GETFL);
2823 fcntl(mem_notify_fd, flags|FASYNC);
2827 sigaddset(&mask, SIGIO);
2829 act.sa_handler = memory_signal_handler;
2833 sigaction(SIGIO, &act, 0);
2835 sigprocmask(SIG_UNBLOCK, &mask, 0);
2837 debug_print("/dev/mem_notify: installed handler\n");
2839 #endif /* linux && SIGIO */
2842 osso_context_t *get_osso_context(void)
2844 return static_osso_context;
2849 #ifdef HAVE_NETWORKMANAGER_SUPPORT
2850 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
2853 MainWindow *mainWin;
2856 if (static_mainwindow)
2857 mainWin = static_mainwindow;
2859 mainWin = (MainWindow*)data;
2861 if (!prefs_common.use_networkmanager)
2869 online = networkmanager_is_online(&error);
2871 if(online && went_offline_nm) {
2872 went_offline_nm = FALSE;
2873 main_window_toggle_work_offline(mainWin, FALSE, FALSE);
2874 debug_print("NetworkManager: Went online\n");
2875 log_message(LOG_PROTOCOL, _("NetworkManager: network is online.\n"));
2878 went_offline_nm = TRUE;
2879 main_window_toggle_work_offline(mainWin, TRUE, FALSE);
2880 debug_print("NetworkManager: Went offline\n");
2881 log_message(LOG_PROTOCOL, _("NetworkManager: network is offline.\n"));
2885 debug_print("Failed to get online information from NetworkManager: %s\n",
2887 g_error_free(error);
2891 debug_print("NetworkManager: Cannot change connection state because "
2892 "main window does not exist\n");
2895 /* Returns true (and sets error appropriately, if given) in case of error */
2896 gboolean networkmanager_is_online(GError **error)
2898 DBusGConnection *connection;
2900 GError *tmp_error = NULL;
2904 if (!prefs_common.use_networkmanager)
2909 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &tmp_error);
2912 /* If calling code doesn't do error checking, at least print some debug */
2913 if((error == NULL) || (*error == NULL))
2914 debug_print("Failed to open connection to system bus: %s\n",
2915 tmp_error->message);
2916 g_propagate_error(error, tmp_error);
2920 proxy = dbus_g_proxy_new_for_name(connection,
2921 "org.freedesktop.NetworkManager",
2922 "/org/freedesktop/NetworkManager",
2923 "org.freedesktop.NetworkManager");
2925 retVal = dbus_g_proxy_call(proxy,"state",&tmp_error, G_TYPE_INVALID,
2926 G_TYPE_UINT, &state, G_TYPE_INVALID);
2929 g_object_unref(proxy);
2931 dbus_g_connection_unref(connection);
2934 /* If calling code doesn't do error checking, at least print some debug */
2935 if((error == NULL) || (*error == NULL))
2936 debug_print("Failed to get state info from NetworkManager: %s\n",
2937 tmp_error->message);
2938 g_propagate_error(error, tmp_error);
2941 #if NM_CHECK_VERSION(0,8,992)
2942 return (state == NM_STATE_CONNECTED_LOCAL ||
2943 state == NM_STATE_CONNECTED_SITE ||
2944 state == NM_STATE_CONNECTED_GLOBAL ||
2945 state == NM_STATE_UNKNOWN);
2947 return (state == NM_STATE_CONNECTED ||
2948 state == NM_STATE_UNKNOWN);