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;
207 gboolean cancel_receiving;
209 const gchar *compose_mailto;
212 const gchar *search_folder;
213 const gchar *search_type;
214 const gchar *search_request;
215 gboolean search_recursive;
217 gboolean status_full;
219 gboolean reset_statistics;
220 GPtrArray *status_folders;
221 GPtrArray *status_full_folders;
228 const gchar *subscribe_uri;
232 SessionStats session_stats;
234 static void reset_statistics(void);
236 static void parse_cmd_opt(int argc, char *argv[]);
238 static gint prohibit_duplicate_launch (void);
239 static gchar * get_crashfile_name (void);
240 static gint lock_socket_remove (void);
241 static void lock_socket_input_cb (gpointer data,
243 GIOCondition condition);
245 static void open_compose_new (const gchar *address,
246 GList *attach_files);
248 static void send_queue (void);
249 static void initial_processing (FolderItem *item, gpointer data);
250 static void quit_signal_handler (int sig);
251 static void install_basic_sighandlers (void);
252 #if (defined linux && defined SIGIO)
253 static void install_memory_sighandler (void);
255 static void exit_claws (MainWindow *mainwin);
257 #ifdef HAVE_NETWORKMANAGER_SUPPORT
258 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
262 #define MAKE_DIR_IF_NOT_EXIST(dir) \
264 if (!is_dir_exist(dir)) { \
265 if (is_file_exist(dir)) { \
267 (_("File '%s' already exists.\n" \
268 "Can't create folder."), \
272 if (make_dir(dir) < 0) \
277 static MainWindow *static_mainwindow;
280 static osso_context_t *static_osso_context;
282 void exit_event_handler(gboolean die_now, gpointer data)
285 appdata = (AppData *) data;
286 /* Do whatever application needs to do before exiting */
287 exit_claws(static_mainwindow);
288 hildon_banner_show_information(GTK_WIDGET(appdata->window), NULL,
293 /* Callback for hardware D-BUS events */
294 void hw_event_handler(osso_hw_state_t *state, gpointer data)
297 appdata = (AppData *) data;
299 if (state->shutdown_ind) {
300 exit_claws(static_mainwindow);
301 hildon_banner_show_information(GTK_WIDGET(appdata->window), NULL,
306 /* Callback for normal D-BUS messages */
307 gint dbus_req_handler(const gchar * interface, const gchar * method,
308 GArray * arguments, gpointer data,
312 appdata = (AppData *) data;
314 if (!strcmp(method, "top_application")) {
315 osso_rpc_free_val(retval);
318 osso_system_note_infoprint(appdata->osso_context, method, retval);
319 osso_rpc_free_val(retval);
324 static gboolean emergency_exit = FALSE;
326 #ifdef HAVE_STARTUP_NOTIFICATION
327 static void sn_error_trap_push(SnDisplay *display, Display *xdisplay)
329 gdk_error_trap_push();
332 static void sn_error_trap_pop(SnDisplay *display, Display *xdisplay)
334 gdk_error_trap_pop();
337 static void startup_notification_complete(gboolean with_window)
340 GtkWidget *hack = NULL;
343 /* this is needed to make the startup notification leave,
344 * if we have been launched from a menu.
345 * We have to display a window, so let it be very little */
346 hack = gtk_window_new(GTK_WINDOW_POPUP);
347 gtk_window_move(GTK_WINDOW(hack), 0, 0);
348 gtk_widget_set_size_request(hack, 1, 1);
349 gtk_widget_show(hack);
352 xdisplay = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
353 sn_display = sn_display_new(xdisplay,
356 sn_context = sn_launchee_context_new_from_environment(sn_display,
357 DefaultScreen(xdisplay));
359 if (sn_context != NULL) {
360 sn_launchee_context_complete(sn_context);
361 sn_launchee_context_unref(sn_context);
362 sn_display_unref(sn_display);
365 gtk_widget_destroy(hack);
368 #endif /* HAVE_STARTUP_NOTIFICATION */
370 static void claws_gtk_idle(void)
372 while(gtk_events_pending()) {
373 gtk_main_iteration();
378 static gboolean sc_starting = FALSE;
380 static gboolean defer_check_all(void *data)
382 gboolean autochk = GPOINTER_TO_INT(data);
384 inc_all_account_mail(static_mainwindow, autochk,
385 prefs_common.newmail_notify_manu);
389 main_window_set_menu_sensitive(static_mainwindow);
390 toolbar_main_set_sensitive(static_mainwindow);
395 static gboolean defer_check(void *data)
397 inc_mail(static_mainwindow, prefs_common.newmail_notify_manu);
401 main_window_set_menu_sensitive(static_mainwindow);
402 toolbar_main_set_sensitive(static_mainwindow);
407 static gboolean defer_jump(void *data)
409 if (cmd.receive_all) {
410 defer_check_all(GINT_TO_POINTER(FALSE));
411 } else if (prefs_common.chk_on_startup) {
412 defer_check_all(GINT_TO_POINTER(TRUE));
413 } else if (cmd.receive) {
416 mainwindow_jump_to(data, FALSE);
419 main_window_set_menu_sensitive(static_mainwindow);
420 toolbar_main_set_sensitive(static_mainwindow);
425 static void chk_update_val(GtkWidget *widget, gpointer data)
427 gboolean *val = (gboolean *)data;
428 *val = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
431 static gboolean migrate_old_config(const gchar *old_cfg_dir, const gchar *new_cfg_dir, const gchar *oldversion)
433 gchar *message = g_strdup_printf(_("Configuration for %s found.\n"
434 "Do you want to migrate this configuration?"), oldversion);
435 gchar *message2 = g_strdup_printf(_("\n\nYour Sylpheed filtering rules can be converted by a\n"
436 "script available at %s."), TOOLS_URI);
438 if (!strcmp(oldversion, "Sylpheed"))
439 message = g_strconcat(message, message2, NULL);
443 GtkWidget *window = NULL;
444 GtkWidget *keep_backup_chk;
445 gboolean backup = TRUE;
447 keep_backup_chk = gtk_check_button_new_with_label (_("Keep old configuration"));
448 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_backup_chk), TRUE);
449 CLAWS_SET_TIP(keep_backup_chk,
450 _("Keeping a backup will allow you to go back to an "
451 "older version, but may take a while if you have "
452 "cached IMAP or News data, and will take some extra "
453 "room on your disk."));
455 g_signal_connect(G_OBJECT(keep_backup_chk), "toggled",
456 G_CALLBACK(chk_update_val), &backup);
458 if (alertpanel_full(_("Migration of configuration"), message,
459 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL, FALSE,
460 keep_backup_chk, ALERT_QUESTION, G_ALERTDEFAULT) != G_ALERTALTERNATE) {
464 /* we can either do a fast migration requiring not any extra disk
465 * space, or a slow one that copies the old configuration and leaves
469 window = label_window_create(_("Copying configuration... This may take a while..."));
472 r = copy_dir(old_cfg_dir, new_cfg_dir);
473 label_window_destroy(window);
475 /* if copy failed, we'll remove the partially copied
478 alertpanel_error(_("Migration failed!"));
479 remove_dir_recursive(new_cfg_dir);
482 /* fast mode failed, but we don't want backup */
483 remove_dir_recursive(old_cfg_dir);
487 window = label_window_create(_("Migrating configuration..."));
490 r = g_rename(old_cfg_dir, new_cfg_dir);
491 label_window_destroy(window);
493 /* if g_rename failed, we'll try to copy */
495 FILE_OP_ERROR(new_cfg_dir, "g_rename failed, trying copy\n");
502 static int migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
505 gchar *plugin_path, *old_plugin_path, *new_plugin_path;
507 gboolean err = FALSE;
509 oldfp = g_fopen(old_rc, "r");
512 newfp = g_fopen(new_rc, "w");
518 plugin_path = g_strdup(get_plugin_dir());
519 new_plugin_path = g_strdup(plugin_path);
521 if (strstr(plugin_path, "/claws-mail/")) {
522 gchar *end = g_strdup(strstr(plugin_path, "/claws-mail/")+strlen("/claws-mail/"));
523 *(strstr(plugin_path, "/claws-mail/")) = '\0';
524 old_plugin_path = g_strconcat(plugin_path, "/sylpheed-claws/", end, NULL);
527 old_plugin_path = g_strdup(new_plugin_path);
529 debug_print("replacing %s with %s\n", old_plugin_path, new_plugin_path);
530 while (fgets(buf, sizeof(buf), oldfp)) {
531 if (strncmp(buf, old_plugin_path, strlen(old_plugin_path))) {
532 err |= (fputs(buf, newfp) == EOF);
534 debug_print("->replacing %s", buf);
535 debug_print(" with %s%s", new_plugin_path, buf+strlen(old_plugin_path));
536 err |= (fputs(new_plugin_path, newfp) == EOF);
537 err |= (fputs(buf+strlen(old_plugin_path), newfp) == EOF);
541 g_free(new_plugin_path);
542 g_free(old_plugin_path);
544 if (fclose(newfp) == EOF)
552 sc_client_set_value (MainWindow *mainwin,
563 prop.num_vals = num_vals;
567 if (mainwin->smc_conn)
568 SmcSetProperties ((SmcConn) mainwin->smc_conn, 1, proplist);
571 static void sc_die_callback (SmcConn smc_conn, SmPointer client_data)
576 static void sc_save_complete_callback(SmcConn smc_conn, SmPointer client_data)
580 static void sc_shutdown_cancelled_callback (SmcConn smc_conn, SmPointer client_data)
582 MainWindow *mainwin = (MainWindow *)client_data;
583 if (mainwin->smc_conn)
584 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
587 static void sc_save_yourself_callback (SmcConn smc_conn,
588 SmPointer client_data,
594 MainWindow *mainwin = (MainWindow *)client_data;
595 if (mainwin->smc_conn)
596 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
599 static IceIOErrorHandler sc_ice_installed_handler;
601 static void sc_ice_io_error_handler (IceConn connection)
603 if (sc_ice_installed_handler)
604 (*sc_ice_installed_handler) (connection);
606 static gboolean sc_process_ice_messages (GIOChannel *source,
607 GIOCondition condition,
610 IceConn connection = (IceConn) data;
611 IceProcessMessagesStatus status;
613 status = IceProcessMessages (connection, NULL, NULL);
615 if (status == IceProcessMessagesIOError) {
616 IcePointer context = IceGetConnectionContext (connection);
618 if (context && G_IS_OBJECT(context)) {
619 guint disconnect_id = g_signal_lookup ("disconnect", G_OBJECT_TYPE (context));
621 if (disconnect_id > 0)
622 g_signal_emit (context, disconnect_id, 0);
624 IceSetShutdownNegotiation (connection, False);
625 IceCloseConnection (connection);
632 static void new_ice_connection (IceConn connection, IcePointer client_data, Bool opening,
633 IcePointer *watch_data)
639 /* Make sure we don't pass on these file descriptors to any
641 fcntl(IceConnectionNumber(connection),F_SETFD,
642 fcntl(IceConnectionNumber(connection),F_GETFD,0) | FD_CLOEXEC);
644 channel = g_io_channel_unix_new (IceConnectionNumber (connection));
645 input_id = g_io_add_watch (channel,
646 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
647 sc_process_ice_messages,
649 g_io_channel_unref (channel);
651 *watch_data = (IcePointer) GUINT_TO_POINTER (input_id);
653 input_id = GPOINTER_TO_UINT ((gpointer) *watch_data);
654 g_source_remove (input_id);
658 static void sc_session_manager_connect(MainWindow *mainwin)
660 static gboolean connected = FALSE;
661 SmcCallbacks callbacks;
663 IceIOErrorHandler default_handler;
670 sc_ice_installed_handler = IceSetIOErrorHandler (NULL);
671 default_handler = IceSetIOErrorHandler (sc_ice_io_error_handler);
673 if (sc_ice_installed_handler == default_handler)
674 sc_ice_installed_handler = NULL;
676 IceAddConnectionWatch (new_ice_connection, NULL);
679 callbacks.save_yourself.callback = sc_save_yourself_callback;
680 callbacks.die.callback = sc_die_callback;
681 callbacks.save_complete.callback = sc_save_complete_callback;
682 callbacks.shutdown_cancelled.callback = sc_shutdown_cancelled_callback;
684 callbacks.save_yourself.client_data =
685 callbacks.die.client_data =
686 callbacks.save_complete.client_data =
687 callbacks.shutdown_cancelled.client_data = (SmPointer) mainwin;
688 if (g_getenv ("SESSION_MANAGER")) {
689 gchar error_string_ret[256] = "";
691 mainwin->smc_conn = (gpointer)
692 SmcOpenConnection (NULL, mainwin,
693 SmProtoMajor, SmProtoMinor,
694 SmcSaveYourselfProcMask | SmcDieProcMask |
695 SmcSaveCompleteProcMask |
696 SmcShutdownCancelledProcMask,
699 256, error_string_ret);
701 if (error_string_ret[0] || mainwin->smc_conn == NULL)
702 g_warning ("While connecting to session manager:\n%s.",
706 vals = g_new (SmPropValue, 1);
707 vals[0].length = strlen(argv0);
708 vals[0].value = argv0;
709 sc_client_set_value (mainwin, SmCloneCommand, SmLISTofARRAY8, 1, vals);
710 sc_client_set_value (mainwin, SmRestartCommand, SmLISTofARRAY8, 1, vals);
711 sc_client_set_value (mainwin, SmProgram, SmARRAY8, 1, vals);
713 vals[0].length = strlen(g_get_user_name()?g_get_user_name():"");
714 vals[0].value = g_strdup(g_get_user_name()?g_get_user_name():"");
715 sc_client_set_value (mainwin, SmUserID, SmARRAY8, 1, vals);
721 static gboolean sc_exiting = FALSE;
722 static gboolean show_at_startup = TRUE;
723 static gboolean claws_crashed_bool = FALSE;
725 gboolean claws_crashed(void) {
726 return claws_crashed_bool;
729 void main_set_show_at_startup(gboolean show)
731 show_at_startup = show;
735 static void main_vol_mount_cb(GnomeVFSVolumeMonitor *vfs, GnomeVFSVolume *vol, MainWindow *mainwin)
737 gchar *uri = gnome_vfs_volume_get_activation_uri (vol);
738 gchar *mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
741 if(!strcmp(mount_path, prefs_common.data_root)) {
742 gtk_widget_set_sensitive(mainwin->window, TRUE);
748 static void main_vol_unmount_cb(GnomeVFSVolumeMonitor *vfs, GnomeVFSVolume *vol, MainWindow *mainwin)
750 gchar *uri = gnome_vfs_volume_get_activation_uri (vol);
751 gchar *mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
754 if(!strcmp(mount_path, prefs_common.data_root)) {
755 gtk_widget_set_sensitive(mainwin->window, FALSE);
764 static FILE* win32_debug_fp=NULL;
765 static guint win32_log_handler_app_id;
766 static guint win32_log_handler_glib_id;
767 static guint win32_log_handler_gtk_id;
769 static void win32_print_stdout(const gchar *string)
771 if (win32_debug_fp) {
772 fprintf(win32_debug_fp, string);
773 fflush(win32_debug_fp);
777 static void win32_print_stderr(const gchar *string)
779 if (win32_debug_fp) {
780 fprintf(win32_debug_fp, string);
781 fflush(win32_debug_fp);
785 static void win32_log(const gchar *log_domain, GLogLevelFlags log_level, const gchar* message, gpointer user_data)
787 if (win32_debug_fp) {
790 switch(log_level & G_LOG_LEVEL_MASK)
792 case G_LOG_LEVEL_ERROR:
795 case G_LOG_LEVEL_CRITICAL:
798 case G_LOG_LEVEL_WARNING:
801 case G_LOG_LEVEL_MESSAGE:
804 case G_LOG_LEVEL_INFO:
807 case G_LOG_LEVEL_DEBUG:
814 fprintf(win32_debug_fp, "%s: %s: %s", log_domain, type, message);
816 fprintf(win32_debug_fp, "%s: %s", type, message);
817 fflush(win32_debug_fp);
821 static void win32_open_log(void)
823 gchar *logfile = g_strconcat(g_get_tmp_dir(), G_DIR_SEPARATOR_S, "claws-win32.log", NULL);
824 gchar *oldlogfile = g_strconcat(g_get_tmp_dir(), G_DIR_SEPARATOR_S, "claws-win32.log.bak", NULL);
826 if (is_file_exist(logfile)) {
827 if (rename_force(logfile, oldlogfile) < 0)
828 FILE_OP_ERROR(logfile, "rename");
830 win32_debug_fp = g_fopen(logfile, "w");
835 g_set_print_handler(win32_print_stdout);
836 g_set_printerr_handler(win32_print_stdout);
837 win32_log_handler_app_id = g_log_set_handler(NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
838 | G_LOG_FLAG_RECURSION, win32_log, NULL);
839 win32_log_handler_glib_id = g_log_set_handler("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
840 | G_LOG_FLAG_RECURSION, win32_log, NULL);
841 win32_log_handler_gtk_id = g_log_set_handler("Gtk", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
842 | G_LOG_FLAG_RECURSION, win32_log, NULL);
846 static void win32_close_log(void)
850 g_log_remove_handler("", win32_log_handler_app_id);
851 g_log_remove_handler("GLib", win32_log_handler_glib_id);
852 g_log_remove_handler("Gtk", win32_log_handler_gtk_id);
853 fclose(win32_debug_fp);
859 static void main_dump_features_list(gboolean show_debug_only)
860 /* display compiled-in features list */
862 if (show_debug_only && !debug_get_mode())
866 debug_print("runtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
867 gtk_major_version, gtk_minor_version, gtk_micro_version,
868 glib_major_version, glib_minor_version, glib_micro_version);
870 g_print("runtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
871 gtk_major_version, gtk_minor_version, gtk_micro_version,
872 glib_major_version, glib_minor_version, glib_micro_version);
874 debug_print("buildtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
875 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
876 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
878 g_print("buildtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
879 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
880 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
883 debug_print("Compiled-in features:\n");
885 g_print("Compiled-in features:\n");
888 debug_print(" compface\n");
890 g_print(" compface\n");
894 debug_print(" aspell\n");
896 g_print(" aspell\n");
900 debug_print(" gnutls\n");
902 g_print(" gnutls\n");
906 debug_print(" ipv6\n");
912 debug_print(" iconv\n");
918 debug_print(" jpilot\n");
920 g_print(" jpilot\n");
924 debug_print(" ldap\n");
930 debug_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
932 g_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
936 debug_print(" libsm\n");
940 #if HAVE_NETWORKMANAGER_SUPPORT
942 debug_print(" NetworkManager\n");
944 g_print(" NetworkManager\n");
948 #ifdef HAVE_DBUS_GLIB
949 static guint dbus_item_hook_id = -1;
950 static guint dbus_folder_hook_id = -1;
952 static void uninstall_dbus_status_handler(void)
955 g_object_unref(awn_proxy);
957 if (dbus_item_hook_id != -1)
958 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, dbus_item_hook_id);
959 if (dbus_folder_hook_id != -1)
960 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, dbus_folder_hook_id);
963 static void dbus_update(FolderItem *removed_item)
965 guint new, unread, unreadmarked, marked, total;
966 guint replied, forwarded, locked, ignored, watched;
968 GError *error = NULL;
970 folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total,
971 &replied, &forwarded, &locked, &ignored,
974 total -= removed_item->total_msgs;
975 new -= removed_item->new_msgs;
976 unread -= removed_item->unread_msgs;
980 buf = g_strdup_printf("%d", new);
981 dbus_g_proxy_call(awn_proxy, "SetInfoByName", &error,
982 G_TYPE_STRING, "claws-mail",
984 G_TYPE_INVALID, G_TYPE_INVALID);
988 dbus_g_proxy_call(awn_proxy, "UnsetInfoByName", &error, G_TYPE_STRING,
989 "claws-mail", G_TYPE_INVALID, G_TYPE_INVALID);
992 debug_print("%s\n", error->message);
997 static gboolean dbus_status_update_folder_hook(gpointer source, gpointer data)
999 FolderUpdateData *hookdata;
1001 if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
1002 dbus_update(hookdata->item);
1009 static gboolean dbus_status_update_item_hook(gpointer source, gpointer data)
1016 static void install_dbus_status_handler(void)
1018 GError *tmp_error = NULL;
1019 DBusGConnection *connection = dbus_g_bus_get(DBUS_BUS_SESSION, &tmp_error);
1022 /* If calling code doesn't do error checking, at least print some debug */
1023 debug_print("Failed to open connection to session bus: %s\n",
1024 tmp_error->message);
1025 g_error_free(tmp_error);
1028 awn_proxy = dbus_g_proxy_new_for_name(connection,
1029 "com.google.code.Awn",
1030 "/com/google/code/Awn",
1031 "com.google.code.Awn");
1032 dbus_item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, dbus_status_update_item_hook, NULL);
1033 if (dbus_item_hook_id == -1) {
1034 g_warning(_("Failed to register folder item update hook"));
1035 uninstall_dbus_status_handler();
1039 dbus_folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, dbus_status_update_folder_hook, NULL);
1040 if (dbus_folder_hook_id == -1) {
1041 g_warning(_("Failed to register folder update hook"));
1042 uninstall_dbus_status_handler();
1048 static void reset_statistics(void)
1050 /* (re-)initialize session statistics */
1051 session_stats.received = 0;
1052 session_stats.sent = 0;
1053 session_stats.replied = 0;
1054 session_stats.forwarded = 0;
1055 session_stats.time_started = time(NULL);
1058 int main(int argc, char *argv[])
1061 osso_context_t *osso_context;
1062 osso_return_t result;
1064 #ifdef HAVE_DBUS_GLIB
1065 DBusGConnection *connection;
1068 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1069 DBusGProxy *nm_proxy;
1072 MainWindow *mainwin;
1073 FolderView *folderview;
1075 gboolean crash_file_present = FALSE;
1076 gint num_folder_class = 0;
1077 gboolean asked_for_migration = FALSE;
1078 gboolean start_done = TRUE;
1079 GSList *plug_list = NULL;
1080 gboolean never_ran = FALSE;
1081 gboolean mainwin_shown = FALSE;
1083 START_TIMING("startup");
1090 if (!claws_init(&argc, &argv)) {
1097 prog_version = PROG_VERSION;
1098 argv0 = g_strdup(argv[0]);
1100 parse_cmd_opt(argc, argv);
1104 /* check and create unix domain socket for remote operation */
1105 lock_socket = prohibit_duplicate_launch();
1106 if (lock_socket < 0) {
1107 #ifdef HAVE_STARTUP_NOTIFICATION
1108 if(gtk_init_check(&argc, &argv))
1109 startup_notification_complete(TRUE);
1114 main_dump_features_list(TRUE);
1115 prefs_prepare_cache();
1119 #if !GTK_CHECK_VERSION(3, 0, 0)
1122 gtk_init(&argc, &argv);
1123 crash_main(cmd.crash_params);
1129 crash_install_handlers();
1131 install_basic_sighandlers();
1132 #if (defined linux && defined SIGIO)
1133 install_memory_sighandler();
1136 if (cmd.status || cmd.status_full || cmd.search ||
1137 cmd.statistics || cmd.reset_statistics || cmd.cancel_receiving) {
1138 puts("0 Claws Mail not running.");
1139 lock_socket_remove();
1145 if (!g_thread_supported())
1146 g_thread_init(NULL);
1150 #if !GTK_CHECK_VERSION(3, 0, 0)
1153 gtk_init(&argc, &argv);
1156 gtk_settings_set_string_property(gtk_settings_get_default(),
1162 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1163 went_offline_nm = FALSE;
1166 #ifdef HAVE_DBUS_GLIB
1168 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
1171 debug_print("Failed to open connection to system bus: %s\n", error->message);
1172 g_error_free(error);
1175 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1176 nm_proxy = dbus_g_proxy_new_for_name(connection,
1177 "org.freedesktop.NetworkManager",
1178 "/org/freedesktop/NetworkManager",
1179 "org.freedesktop.NetworkManager");
1181 #if NM_CHECK_VERSION(0,8,992)
1182 dbus_g_proxy_add_signal(nm_proxy, "StateChanged", G_TYPE_UINT, G_TYPE_INVALID);
1183 dbus_g_proxy_connect_signal(nm_proxy, "StateChanged",
1184 G_CALLBACK(networkmanager_state_change_cb),
1187 dbus_g_proxy_add_signal(nm_proxy, "StateChange", G_TYPE_UINT, G_TYPE_INVALID);
1188 dbus_g_proxy_connect_signal(nm_proxy, "StateChange",
1189 G_CALLBACK(networkmanager_state_change_cb),
1194 install_dbus_status_handler();
1200 osso_context = osso_initialize(OSSO_SERVICE, "2.8.1", TRUE, NULL);
1201 if (osso_context == NULL) {
1204 hildon_program = HILDON_PROGRAM(hildon_program_get_instance());
1205 static_osso_context = osso_context;
1207 #if !GTK_CHECK_VERSION(3, 0, 0)
1208 gtk_widget_set_default_colormap(
1209 gdk_screen_get_system_colormap(
1210 gdk_screen_get_default()));
1213 gtkut_create_ui_manager();
1215 /* Create container for all the menus we will be adding */
1216 MENUITEM_ADDUI("/", "Menus", NULL, GTK_UI_MANAGER_MENUBAR);
1218 if (!g_thread_supported()) {
1219 g_error(_("g_thread is not supported by glib.\n"));
1223 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1225 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1228 /* no config dir exists. See if we can migrate an old config. */
1229 if (!is_dir_exist(RC_DIR)) {
1230 prefs_destroy_cache();
1233 /* if one of the old dirs exist, we'll ask if the user
1234 * want to migrates, and r will be TRUE if he said yes
1235 * and migration succeeded, and FALSE otherwise.
1237 if (is_dir_exist(OLD_GTK2_RC_DIR)) {
1238 r = migrate_old_config(OLD_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 2.6.0 (or older)"));
1239 asked_for_migration = TRUE;
1240 } else if (is_dir_exist(OLDER_GTK2_RC_DIR)) {
1241 r = migrate_old_config(OLDER_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.9.15 (or older)"));
1242 asked_for_migration = TRUE;
1243 } else if (is_dir_exist(OLD_GTK1_RC_DIR)) {
1244 r = migrate_old_config(OLD_GTK1_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.0.5 (or older)"));
1245 asked_for_migration = TRUE;
1246 } else if (is_dir_exist(SYLPHEED_RC_DIR)) {
1247 r = migrate_old_config(SYLPHEED_RC_DIR, RC_DIR, "Sylpheed");
1248 asked_for_migration = TRUE;
1251 /* If migration failed or the user didn't want to do it,
1252 * we create a new one (and we'll hit wizard later).
1254 if (r == FALSE && !is_dir_exist(RC_DIR)) {
1256 if (copy_dir(SYSCONFDIR "/skel/.claws-mail", RC_DIR) < 0) {
1258 if (!is_dir_exist(RC_DIR) && make_dir(RC_DIR) < 0) {
1271 if (!is_file_exist(RC_DIR G_DIR_SEPARATOR_S COMMON_RC) &&
1272 is_file_exist(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC)) {
1273 /* post 2.6 name change */
1274 migrate_common_rc(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC,
1275 RC_DIR G_DIR_SEPARATOR_S COMMON_RC);
1279 plugin_load_all("Common");
1281 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
1282 gtk_rc_parse(userrc);
1285 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1286 gtk_accel_map_load (userrc);
1290 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_rc_dir(), 1, win32_close_log(););
1292 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
1295 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
1296 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
1297 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
1298 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
1299 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
1300 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR);
1302 crash_file_present = is_file_exist(get_crashfile_name());
1303 /* remove temporary files */
1304 remove_all_files(get_tmp_dir());
1305 remove_all_files(get_mime_tmp_dir());
1307 if (!cmd.crash && crash_file_present)
1308 claws_crashed_bool = TRUE;
1310 if (is_file_exist("claws.log")) {
1311 if (rename_force("claws.log", "claws.log.bak") < 0)
1312 FILE_OP_ERROR("claws.log", "rename");
1314 set_log_file(LOG_PROTOCOL, "claws.log");
1316 if (is_file_exist("filtering.log")) {
1317 if (rename_force("filtering.log", "filtering.log.bak") < 0)
1318 FILE_OP_ERROR("filtering.log", "rename");
1320 set_log_file(LOG_DEBUG_FILTERING, "filtering.log");
1323 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1325 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1328 folder_system_init();
1329 prefs_common_read_config();
1331 prefs_themes_init();
1333 prefs_ext_prog_init();
1334 prefs_wrapping_init();
1335 prefs_compose_writing_init();
1336 prefs_msg_colors_init();
1337 image_viewer_init();
1338 prefs_image_viewer_init();
1340 prefs_summaries_init();
1341 prefs_message_init();
1343 prefs_logging_init();
1344 prefs_receive_init();
1349 gtkaspell_checkers_init();
1350 prefs_spelling_init();
1353 sock_set_io_timeout(prefs_common.io_timeout_secs);
1354 prefs_actions_read_config();
1355 prefs_display_header_read_config();
1356 /* prefs_filtering_read_config(); */
1357 #ifndef USE_NEW_ADDRBOOK
1358 addressbook_read_file();
1360 g_clear_error(&error);
1361 if (! addressbook_start_service(&error)) {
1362 g_warning("%s", error->message);
1363 g_clear_error(&error);
1366 addressbook_install_hooks(&error);
1369 gtkut_widget_init();
1370 stock_pixbuf_gdk(NULL, STOCK_PIXMAP_CLAWS_MAIL_ICON, &icon);
1371 gtk_window_set_default_icon(icon);
1373 folderview_initialize();
1379 mainwin = main_window_create();
1381 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1382 networkmanager_state_change_cb(nm_proxy,NULL,mainwin);
1387 appdata = g_new0(AppData, 1);
1388 appdata->program = hildon_program;
1389 appdata->window = HILDON_WINDOW(mainwin->window);
1390 appdata->osso_context = osso_context;
1391 result = osso_rpc_set_cb_f(appdata->osso_context,
1395 dbus_req_handler, appdata);
1396 if (result != OSSO_OK) {
1401 /* Add handler for Exit D-BUS messages */
1402 result = osso_application_set_exit_cb(appdata->osso_context,
1404 (gpointer) appdata);
1405 if (result != OSSO_OK) {
1409 osso_hw_set_event_cb( appdata->osso_context,
1410 NULL, hw_event_handler, (gpointer) appdata );
1412 manage_window_focus_in(mainwin->window, NULL, NULL);
1413 folderview = mainwin->folderview;
1415 gtk_cmclist_freeze(GTK_CMCLIST(mainwin->folderview->ctree));
1416 folder_item_update_freeze();
1418 prefs_account_init();
1419 account_read_config_all();
1421 #ifdef HAVE_LIBETPAN
1422 imap_main_init(prefs_common.skip_ssl_cert_check);
1423 imap_main_set_timeout(prefs_common.io_timeout_secs);
1424 nntp_main_init(prefs_common.skip_ssl_cert_check);
1426 /* If we can't read a folder list or don't have accounts,
1427 * it means the configuration's not done. Either this is
1428 * a brand new install, either a failed/refused migration.
1429 * So we'll start the wizard.
1431 if (folder_read_list() < 0) {
1432 prefs_destroy_cache();
1434 /* if run_wizard returns FALSE it's because it's
1435 * been cancelled. We can't do much but exit.
1436 * however, if the user was asked for a migration,
1437 * we remove the newly created directory so that
1438 * he's asked again for migration on next launch.*/
1439 if (!run_wizard(mainwin, TRUE)) {
1440 if (asked_for_migration)
1441 remove_dir_recursive(RC_DIR);
1447 main_window_reflect_prefs_all_now();
1448 folder_write_list();
1452 if (!account_get_list()) {
1453 prefs_destroy_cache();
1454 if (!run_wizard(mainwin, FALSE)) {
1455 if (asked_for_migration)
1456 remove_dir_recursive(RC_DIR);
1462 if(!account_get_list()) {
1463 exit_claws(mainwin);
1473 toolbar_main_set_sensitive(mainwin);
1474 main_window_set_menu_sensitive(mainwin);
1476 /* if crashed, show window early so that the user
1477 * sees what's happening */
1478 if (claws_crashed()) {
1479 main_window_popup(mainwin);
1480 mainwin_shown = TRUE;
1483 account_set_missing_folder();
1484 folder_set_missing_folders();
1485 folderview_set(folderview);
1487 prefs_matcher_read_config();
1488 quicksearch_set_search_strings(mainwin->summaryview->quicksearch);
1490 /* make one all-folder processing before using claws */
1491 main_window_cursor_wait(mainwin);
1492 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
1494 /* if claws crashed, rebuild caches */
1495 if (claws_crashed()) {
1497 debug_print("Claws Mail crashed, checking for new messages in local folders\n");
1498 folder_item_update_thaw();
1499 folderview_check_new(NULL);
1500 folder_clean_cache_memory_force();
1501 folder_item_update_freeze();
1503 /* make the crash-indicator file */
1504 str_write_to_file("foo", get_crashfile_name());
1506 inc_autocheck_timer_init(mainwin);
1508 /* ignore SIGPIPE signal for preventing sudden death of program */
1510 signal(SIGPIPE, SIG_IGN);
1512 if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
1513 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1515 if (cmd.online_mode == ONLINE_MODE_ONLINE) {
1516 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1519 if (cmd.status_folders) {
1520 g_ptr_array_free(cmd.status_folders, TRUE);
1521 cmd.status_folders = NULL;
1523 if (cmd.status_full_folders) {
1524 g_ptr_array_free(cmd.status_full_folders, TRUE);
1525 cmd.status_full_folders = NULL;
1528 claws_register_idle_function(claws_gtk_idle);
1530 prefs_toolbar_init();
1532 num_folder_class = g_list_length(folder_get_list());
1534 plugin_load_all("GTK2");
1536 if (g_list_length(folder_get_list()) != num_folder_class) {
1537 debug_print("new folders loaded, reloading processing rules\n");
1538 prefs_matcher_read_config();
1541 if ((plug_list = plugin_get_unloaded_list()) != NULL) {
1544 gint num_plugins = 0;
1545 for (cur = plug_list; cur; cur = cur->next) {
1546 Plugin *plugin = (Plugin *)cur->data;
1547 gchar *tmp = g_strdup_printf("%s\n%s",
1549 plugin_get_name(plugin));
1554 main_window_cursor_normal(mainwin);
1555 main_window_popup(mainwin);
1556 mainwin_shown = TRUE;
1557 alertpanel_warning(ngettext(
1558 "The following plugin failed to load. "
1559 "Check the Plugins configuration "
1560 "for more information:\n%s",
1561 "The following plugins failed to load. "
1562 "Check the Plugins configuration "
1563 "for more information:\n%s",
1566 main_window_cursor_wait(mainwin);
1568 g_slist_free(plug_list);
1572 prefs_common_write_config();
1573 plugin_load_standard_plugins ();
1575 /* if not crashed, show window now */
1576 if (!mainwin_shown) {
1577 /* apart if something told not to show */
1578 if (show_at_startup)
1579 main_window_popup(mainwin);
1582 if (!folder_have_mailbox()) {
1583 prefs_destroy_cache();
1584 main_window_cursor_normal(mainwin);
1585 if (folder_get_list() != NULL) {
1586 alertpanel_error(_("Claws Mail has detected a configured "
1587 "mailbox, but it is incomplete. It is "
1588 "possibly due to a failing IMAP account. Use "
1589 "\"Rebuild folder tree\" on the mailbox parent "
1590 "folder's context menu to try to fix it."));
1592 alertpanel_error(_("Claws Mail has detected a configured "
1593 "mailbox, but could not load it. It is "
1594 "probably provided by an out-of-date "
1595 "external plugin. Please reinstall the "
1596 "plugin and try again."));
1597 exit_claws(mainwin);
1605 static_mainwindow = mainwin;
1608 if (prefs_common.data_root != NULL && *prefs_common.data_root != '\0') {
1609 GnomeVFSVolume *vol = NULL;
1610 gchar *uri, *mount_path;
1612 volmon = gnome_vfs_get_volume_monitor();
1613 vol = gnome_vfs_volume_monitor_get_volume_for_path(volmon, prefs_common.data_root);
1615 uri = gnome_vfs_volume_get_activation_uri (vol);
1616 mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
1619 if (vol == NULL || !gnome_vfs_volume_is_mounted(vol)
1620 || strcmp(mount_path, prefs_common.data_root)) {
1621 alertpanel_error(_("Claws Mail can not start without its data volume (%s)."),
1622 prefs_common.data_root);
1624 gnome_vfs_volume_unref(vol);
1625 exit_claws(mainwin);
1629 gnome_vfs_volume_unref(vol);
1630 g_signal_connect(G_OBJECT(volmon),
1631 "volume-mounted", G_CALLBACK(main_vol_mount_cb), mainwin);
1632 g_signal_connect(G_OBJECT(volmon),
1633 "volume-unmounted", G_CALLBACK(main_vol_unmount_cb), mainwin);
1637 #ifdef HAVE_STARTUP_NOTIFICATION
1638 startup_notification_complete(FALSE);
1641 sc_session_manager_connect(mainwin);
1644 folder_item_update_thaw();
1645 gtk_cmclist_thaw(GTK_CMCLIST(mainwin->folderview->ctree));
1646 main_window_cursor_normal(mainwin);
1648 if (!cmd.target && prefs_common.goto_last_folder_on_startup &&
1649 folder_find_item_from_identifier(prefs_common.last_opened_folder) != NULL &&
1651 cmd.target = prefs_common.last_opened_folder;
1654 if (cmd.receive_all && !cmd.target) {
1656 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(FALSE));
1657 } else if (prefs_common.chk_on_startup && !cmd.target) {
1659 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(TRUE));
1660 } else if (cmd.receive && !cmd.target) {
1662 g_timeout_add(1000, defer_check, NULL);
1664 gtk_widget_grab_focus(folderview->ctree);
1667 open_compose_new(cmd.compose_mailto, cmd.attach_files);
1669 if (cmd.attach_files) {
1670 list_free_strings(cmd.attach_files);
1671 g_list_free(cmd.attach_files);
1672 cmd.attach_files = NULL;
1674 if (cmd.subscribe) {
1675 folder_subscribe(cmd.subscribe_uri);
1684 g_timeout_add(500, defer_jump, (gpointer)cmd.target);
1687 prefs_destroy_cache();
1689 compose_reopen_exit_drafts();
1692 sc_starting = FALSE;
1693 main_window_set_menu_sensitive(mainwin);
1694 toolbar_main_set_sensitive(mainwin);
1697 /* register the callback of unix domain socket input */
1698 lock_socket_tag = claws_input_add(lock_socket,
1699 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
1700 lock_socket_input_cb,
1708 osso_deinitialize(osso_context);
1710 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1712 g_object_unref(nm_proxy);
1714 #ifdef HAVE_DBUS_GLIB
1715 uninstall_dbus_status_handler();
1717 dbus_g_connection_unref(connection);
1723 exit_claws(mainwin);
1728 static void save_all_caches(FolderItem *item, gpointer data)
1735 folder_item_close(item);
1738 folder_item_free_cache(item, TRUE);
1741 static void exit_claws(MainWindow *mainwin)
1744 gboolean have_connectivity;
1748 debug_print("shutting down\n");
1749 inc_autocheck_timer_remove();
1751 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1752 if (prefs_common.work_offline && went_offline_nm)
1753 prefs_common.work_offline = FALSE;
1756 /* save prefs for opened folder */
1757 if(mainwin->folderview->opened) {
1760 item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
1761 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
1762 prefs_common.last_opened_folder = folder_item_get_identifier(item);
1765 /* save all state before exiting */
1766 folder_func_to_all_folders(save_all_caches, NULL);
1767 folder_write_list();
1769 main_window_get_size(mainwin);
1770 main_window_get_position(mainwin);
1772 prefs_common_write_config();
1773 account_write_config_all();
1774 #ifndef USE_NEW_ADDRBOOK
1775 addressbook_export_to_file();
1777 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1778 gtk_accel_map_save(filename);
1781 /* delete temporary files */
1782 remove_all_files(get_tmp_dir());
1783 remove_all_files(get_mime_tmp_dir());
1785 close_log_file(LOG_PROTOCOL);
1786 close_log_file(LOG_DEBUG_FILTERING);
1788 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1789 have_connectivity = networkmanager_is_online(NULL);
1791 have_connectivity = TRUE;
1793 #ifdef HAVE_LIBETPAN
1794 imap_main_done(have_connectivity);
1795 nntp_main_done(have_connectivity);
1797 /* delete crashfile */
1799 claws_unlink(get_crashfile_name());
1801 lock_socket_remove();
1804 if (mainwin->smc_conn)
1805 SmcCloseConnection ((SmcConn)mainwin->smc_conn, 0, NULL);
1806 mainwin->smc_conn = NULL;
1809 main_window_destroy_all();
1811 plugin_unload_all("GTK2");
1814 prefs_toolbar_done();
1816 #ifndef USE_NEW_ADDRBOOK
1817 addressbook_destroy();
1819 prefs_themes_done();
1821 prefs_ext_prog_done();
1822 prefs_wrapping_done();
1823 prefs_compose_writing_done();
1824 prefs_msg_colors_done();
1825 prefs_image_viewer_done();
1826 image_viewer_done();
1828 prefs_summaries_done();
1829 prefs_message_done();
1831 prefs_receive_done();
1832 prefs_logging_done();
1836 prefs_spelling_done();
1837 gtkaspell_checkers_quit();
1839 plugin_unload_all("Common");
1843 #define G_STRING_APPEND_ENCODED_URI(gstring,source) \
1845 gchar tmpbuf[BUFFSIZE]; \
1846 encode_uri(tmpbuf, BUFFSIZE, (source)); \
1847 g_string_append((gstring), tmpbuf); \
1850 #define G_PRINT_EXIT(msg) \
1856 static GString * parse_cmd_compose_from_file(const gchar *fn)
1858 GString *headers = g_string_new(NULL);
1859 GString *body = g_string_new(NULL);
1867 if (fn == NULL || *fn == '\0')
1868 G_PRINT_EXIT(_("Missing filename\n"));
1869 isstdin = (*fn == '-' && *(fn + 1) == '\0');
1873 fp = g_fopen(fn, "r");
1875 G_PRINT_EXIT(_("Cannot open filename for reading\n"));
1878 while (fgets(fb, sizeof(fb), fp)) {
1884 while (*h && *h != ':') { ++h; } /* search colon */
1886 G_PRINT_EXIT(_("Malformed header\n"));
1888 while (*v && *v == ' ') { ++v; } /* trim value start */
1890 tmp = g_ascii_strdown(fb, -1); /* get header name */
1891 if (!strcmp(tmp, "to")) {
1893 G_PRINT_EXIT(_("Duplicated 'To:' header\n"));
1896 g_string_append_c(headers, '&');
1897 g_string_append(headers, tmp);
1898 g_string_append_c(headers, '=');
1899 #if GLIB_CHECK_VERSION(2,16,0)
1900 g_string_append_uri_escaped(headers, v, NULL, TRUE);
1902 G_STRING_APPEND_ENCODED_URI(headers, v);
1908 G_PRINT_EXIT(_("Missing required 'To:' header\n"));
1909 g_string_append(body, to);
1911 g_string_append(body, "?body=");
1912 while (fgets(fb, sizeof(fb), fp)) {
1913 #if GLIB_CHECK_VERSION(2,16,0)
1914 g_string_append_uri_escaped(body, fb, NULL, TRUE);
1916 G_STRING_APPEND_ENCODED_URI(body, fb);
1921 /* append the remaining headers */
1922 g_string_append(body, headers->str);
1923 g_string_free(headers, TRUE);
1928 #undef G_STRING_APPEND_ENCODED_URI
1931 static void parse_cmd_opt(int argc, char *argv[])
1936 for (i = 1; i < argc; i++) {
1937 if (!strncmp(argv[i], "--receive-all", 13)) {
1938 cmd.receive_all = TRUE;
1939 } else if (!strncmp(argv[i], "--receive", 9)) {
1941 } else if (!strncmp(argv[i], "--cancel-receiving", 18)) {
1942 cmd.cancel_receiving = TRUE;
1943 } else if (!strncmp(argv[i], "--compose-from-file", 19)) {
1944 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1946 GString *mailto = parse_cmd_compose_from_file(p);
1948 cmd.compose_mailto = mailto->str;
1950 } else if (!strncmp(argv[i], "--compose", 9)) {
1951 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1954 cmd.compose_mailto = NULL;
1955 if (p && *p != '\0' && *p != '-') {
1956 if (!strncmp(p, "mailto:", 7)) {
1957 cmd.compose_mailto = p + 7;
1959 cmd.compose_mailto = p;
1963 } else if (!strncmp(argv[i], "--subscribe", 11)) {
1964 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1965 if (p && *p != '\0' && *p != '-') {
1966 cmd.subscribe = TRUE;
1967 cmd.subscribe_uri = p;
1969 } else if (!strncmp(argv[i], "--attach", 8)) {
1970 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1973 while (p && *p != '\0' && *p != '-') {
1974 if ((file = g_filename_from_uri(p, NULL, NULL)) != NULL) {
1975 if (!is_file_exist(file)) {
1980 if (file == NULL && *p != G_DIR_SEPARATOR) {
1981 file = g_strconcat(claws_get_startup_dir(),
1984 } else if (file == NULL) {
1987 ainfo = g_new0(AttachInfo, 1);
1989 cmd.attach_files = g_list_append(cmd.attach_files, ainfo);
1991 p = (i+1 < argc)?argv[i+1]:NULL;
1993 } else if (!strncmp(argv[i], "--send", 6)) {
1995 } else if (!strncmp(argv[i], "--version-full", 14) ||
1996 !strncmp(argv[i], "-V", 2)) {
1997 g_print("Claws Mail version " VERSION "\n");
1998 main_dump_features_list(FALSE);
2000 } else if (!strncmp(argv[i], "--version", 9) ||
2001 !strncmp(argv[i], "-v", 2)) {
2002 g_print("Claws Mail version " VERSION "\n");
2004 } else if (!strncmp(argv[i], "--status-full", 13)) {
2005 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
2007 cmd.status_full = TRUE;
2008 while (p && *p != '\0' && *p != '-') {
2009 if (!cmd.status_full_folders) {
2010 cmd.status_full_folders =
2013 g_ptr_array_add(cmd.status_full_folders,
2016 p = (i+1 < argc)?argv[i+1]:NULL;
2018 } else if (!strncmp(argv[i], "--status", 8)) {
2019 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
2022 while (p && *p != '\0' && *p != '-') {
2023 if (!cmd.status_folders)
2024 cmd.status_folders = g_ptr_array_new();
2025 g_ptr_array_add(cmd.status_folders,
2028 p = (i+1 < argc)?argv[i+1]:NULL;
2030 } else if (!strncmp(argv[i], "--search", 8)) {
2031 cmd.search_folder = (i+1 < argc)?argv[i+1]:NULL;
2032 cmd.search_type = (i+2 < argc)?argv[i+2]:NULL;
2033 cmd.search_request = (i+3 < argc)?argv[i+3]:NULL;
2034 const char* rec = (i+4 < argc)?argv[i+4]:NULL;
2035 cmd.search_recursive = TRUE;
2036 if (rec && (tolower(*rec)=='n' || tolower(*rec)=='f' || *rec=='0'))
2037 cmd.search_recursive = FALSE;
2038 if (cmd.search_folder && cmd.search_type && cmd.search_request)
2040 } else if (!strncmp(argv[i], "--online", 8)) {
2041 cmd.online_mode = ONLINE_MODE_ONLINE;
2042 } else if (!strncmp(argv[i], "--offline", 9)) {
2043 cmd.online_mode = ONLINE_MODE_OFFLINE;
2044 } else if (!strncmp(argv[i], "--statistics", 12)) {
2045 cmd.statistics = TRUE;
2046 } else if (!strncmp(argv[i], "--reset-statistics", 18)) {
2047 cmd.reset_statistics = TRUE;
2048 } else if (!strncmp(argv[i], "--help", 6) ||
2049 !strncmp(argv[i], "-h", 2)) {
2050 gchar *base = g_path_get_basename(argv[0]);
2051 g_print(_("Usage: %s [OPTION]...\n"), base);
2053 g_print("%s\n", _(" --compose [address] open composition window"));
2054 g_print("%s\n", _(" --compose-from-file file\n"
2055 " open composition window with data from given file;\n"
2056 " use - as file name for reading from standard input;\n"
2057 " content format: headers first (To: required) until an\n"
2058 " empty line, then mail body until end of file."));
2059 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
2060 g_print("%s\n", _(" --attach file1 [file2]...\n"
2061 " open composition window with specified files\n"
2063 g_print("%s\n", _(" --receive receive new messages"));
2064 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
2065 g_print("%s\n", _(" --cancel-receiving cancel receiving of messages"));
2066 g_print("%s\n", _(" --search folder type request [recursive]\n"
2068 " folder ex.: \"#mh/Mailbox/inbox\" or \"Mail\"\n"
2069 " type: s[ubject],f[rom],t[o],e[xtended],m[ixed] or g: tag\n"
2070 " request: search string\n"
2071 " recursive: false if arg. starts with 0, n, N, f or F"));
2073 g_print("%s\n", _(" --send send all queued messages"));
2074 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
2075 g_print("%s\n", _(" --status-full [folder]...\n"
2076 " show the status of each folder"));
2077 g_print("%s\n", _(" --statistics show session statistics"));
2078 g_print("%s\n", _(" --reset-statistics reset session statistics"));
2079 g_print("%s\n", _(" --select folder[/msg] jumps to the specified folder/message\n"
2080 " folder is a folder id like 'folder/sub_folder'"));
2081 g_print("%s\n", _(" --online switch to online mode"));
2082 g_print("%s\n", _(" --offline switch to offline mode"));
2083 g_print("%s\n", _(" --exit --quit -q exit Claws Mail"));
2084 g_print("%s\n", _(" --debug debug mode"));
2085 g_print("%s\n", _(" --help -h display this help and exit"));
2086 g_print("%s\n", _(" --version -v output version information and exit"));
2087 g_print("%s\n", _(" --version-full -V output version and built-in features information and exit"));
2088 g_print("%s\n", _(" --config-dir output configuration directory"));
2089 g_print("%s\n", _(" --alternate-config-dir [dir]\n"
2090 " use specified configuration directory"));
2094 } else if (!strncmp(argv[i], "--crash", 7)) {
2096 cmd.crash_params = g_strdup((i+1 < argc)?argv[i+1]:NULL);
2098 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
2099 g_print(RC_DIR "\n");
2101 } else if (!strncmp(argv[i], "--alternate-config-dir", sizeof "--alternate-config-dir" - 1) && i+1 < argc) {
2102 set_rc_dir(argv[i+1]);
2103 } else if (!strncmp(argv[i], "--exit", 6) ||
2104 !strncmp(argv[i], "--quit", 6) ||
2105 !strncmp(argv[i], "-q", 2)) {
2107 } else if (!strncmp(argv[i], "--select", 8) && i+1 < argc) {
2108 cmd.target = argv[i+1];
2109 } else if (i == 1 && argc == 2) {
2110 /* only one parameter. Do something intelligent about it */
2111 if ((strstr(argv[i], "@")||!strncmp(argv[i], "mailto:", 7)) && !strstr(argv[i], "://")) {
2112 const gchar *p = argv[i];
2115 cmd.compose_mailto = NULL;
2116 if (p && *p != '\0' && *p != '-') {
2117 if (!strncmp(p, "mailto:", 7)) {
2118 cmd.compose_mailto = p + 7;
2120 cmd.compose_mailto = p;
2123 } else if (!strncmp(argv[i], "file://", 7)) {
2124 cmd.target = argv[i];
2125 } else if (!strncmp(argv[i], "?attach=file://", strlen("?attach=file://"))) {
2127 cmd.compose_mailto = argv[i];
2128 } else if (strstr(argv[i], "://")) {
2129 const gchar *p = argv[i];
2130 if (p && *p != '\0' && *p != '-') {
2131 cmd.subscribe = TRUE;
2132 cmd.subscribe_uri = p;
2134 } else if (!strcmp(argv[i], "--sync")) {
2136 } else if (is_dir_exist(argv[i]) || is_file_exist(argv[i])) {
2137 cmd.target = argv[i];
2139 g_print(_("Unknown option\n"));
2145 if (cmd.attach_files && cmd.compose == FALSE) {
2147 cmd.compose_mailto = NULL;
2151 static void initial_processing(FolderItem *item, gpointer data)
2153 MainWindow *mainwin = (MainWindow *)data;
2156 cm_return_if_fail(item);
2157 buf = g_strdup_printf(_("Processing (%s)..."),
2160 : _("top level folder"));
2164 if (item->prefs->enable_processing) {
2165 item->processing_pending = TRUE;
2166 folder_item_apply_processing(item);
2167 item->processing_pending = FALSE;
2170 STATUSBAR_POP(mainwin);
2173 static gboolean draft_all_messages(void)
2175 GList *compose_list = NULL;
2177 compose_clear_exit_drafts();
2178 compose_list = compose_get_compose_list();
2179 while (compose_list != NULL) {
2180 Compose *c = (Compose*)compose_list->data;
2181 if (!compose_draft(c, COMPOSE_DRAFT_FOR_EXIT))
2183 compose_list = compose_get_compose_list();
2187 gboolean clean_quit(gpointer data)
2189 static gboolean firstrun = TRUE;
2196 /*!< Good idea to have the main window stored in a
2197 * static variable so we can check that variable
2198 * to see if we're really allowed to do things
2199 * that actually the spawner is supposed to
2200 * do (like: sending mail, composing messages).
2201 * Because, really, if we're the spawnee, and
2202 * we touch GTK stuff, we're hosed. See the
2205 /* FIXME: Use something else to signal that we're
2206 * in the original spawner, and not in a spawned
2208 if (!static_mainwindow) {
2212 draft_all_messages();
2213 emergency_exit = TRUE;
2214 exit_claws(static_mainwindow);
2220 void app_will_exit(GtkWidget *widget, gpointer data)
2222 MainWindow *mainwin = data;
2224 if (gtk_main_level() == 0) {
2225 debug_print("not even started\n");
2228 if (sc_exiting == TRUE) {
2229 debug_print("exit pending\n");
2233 debug_print("exiting\n");
2234 if (compose_get_compose_list()) {
2235 if (!draft_all_messages()) {
2236 main_window_popup(mainwin);
2242 if (prefs_common.warn_queued_on_exit && procmsg_have_queued_mails_fast()) {
2243 if (alertpanel(_("Queued messages"),
2244 _("Some unsent messages are queued. Exit now?"),
2245 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL)
2246 != G_ALERTALTERNATE) {
2247 main_window_popup(mainwin);
2251 manage_window_focus_in(mainwin->window, NULL, NULL);
2255 #ifdef HAVE_VALGRIND
2256 if (RUNNING_ON_VALGRIND) {
2257 summary_clear_list(mainwin->summaryview);
2260 if (folderview_get_selected_item(mainwin->folderview))
2261 folder_item_close(folderview_get_selected_item(mainwin->folderview));
2265 gboolean claws_is_exiting(void)
2270 gboolean claws_is_starting(void)
2276 * CLAWS: want this public so crash dialog can delete the
2279 gchar *claws_get_socket_name(void)
2281 static gchar *filename = NULL;
2282 const gchar *socket_dir = NULL;
2284 if (rc_dir_is_alt())
2285 socket_dir = get_rc_dir();
2287 socket_dir = g_get_tmp_dir();
2288 if (filename == NULL) {
2289 filename = g_strdup_printf("%s%cclaws-mail-%d",
2290 socket_dir, G_DIR_SEPARATOR,
2301 static gchar *get_crashfile_name(void)
2303 static gchar *filename = NULL;
2305 if (filename == NULL) {
2306 filename = g_strdup_printf("%s%cclaws-crashed",
2307 get_tmp_dir(), G_DIR_SEPARATOR);
2313 static gint prohibit_duplicate_launch(void)
2320 path = claws_get_socket_name();
2321 /* Try to connect to the control socket */
2322 uxsock = fd_connect_unix(path);
2324 if (x_display == NULL)
2325 x_display = g_strdup(g_getenv("DISPLAY"));
2332 /* If connect failed, no other process is running.
2333 * Unlink the potentially existing socket, then
2334 * open it. This has to be done locking a temporary
2335 * file to avoid the race condition where another
2336 * process could have created the socket just in
2339 socket_lock = g_strconcat(path, ".lock",
2341 lock_fd = g_open(socket_lock, O_RDWR|O_CREAT, 0);
2343 debug_print("Couldn't open %s: %s (%d)\n", socket_lock,
2344 strerror(errno), errno);
2345 g_free(socket_lock);
2348 if (flock(lock_fd, LOCK_EX) < 0) {
2349 debug_print("Couldn't lock %s: %s (%d)\n", socket_lock,
2350 strerror(errno), errno);
2352 g_free(socket_lock);
2358 debug_print("Opening socket %s\n", path);
2359 ret = fd_open_unix(path);
2361 flock(lock_fd, LOCK_UN);
2362 claws_unlink(socket_lock);
2363 g_free(socket_lock);
2370 hmutex = CreateMutexA(NULL, FALSE, "ClawsMail");
2372 debug_print("cannot create Mutex\n");
2375 if (GetLastError() != ERROR_ALREADY_EXISTS) {
2376 uxsock = fd_open_inet(50216);
2382 uxsock = fd_connect_inet(50216);
2386 /* remote command mode */
2388 debug_print("another Claws Mail instance is already running.\n");
2390 if (cmd.receive_all) {
2391 fd_write_all(uxsock, "receive_all\n", 12);
2392 } else if (cmd.receive) {
2393 fd_write_all(uxsock, "receive\n", 8);
2394 } else if (cmd.cancel_receiving) {
2395 fd_write_all(uxsock, "cancel_receiving\n", 17);
2396 } else if (cmd.compose && cmd.attach_files) {
2397 gchar *str, *compose_str;
2399 if (cmd.compose_mailto) {
2400 compose_str = g_strdup_printf("compose_attach %s\n",
2401 cmd.compose_mailto);
2403 compose_str = g_strdup("compose_attach\n");
2406 fd_write_all(uxsock, compose_str, strlen(compose_str));
2407 g_free(compose_str);
2409 for (curr = cmd.attach_files; curr != NULL ; curr = curr->next) {
2410 str = (gchar *) ((AttachInfo *)curr->data)->file;
2411 fd_write_all(uxsock, str, strlen(str));
2412 fd_write_all(uxsock, "\n", 1);
2415 fd_write_all(uxsock, ".\n", 2);
2416 } else if (cmd.compose) {
2419 if (cmd.compose_mailto) {
2420 compose_str = g_strdup_printf
2421 ("compose %s\n", cmd.compose_mailto);
2423 compose_str = g_strdup("compose\n");
2426 fd_write_all(uxsock, compose_str, strlen(compose_str));
2427 g_free(compose_str);
2428 } else if (cmd.subscribe) {
2429 gchar *str = g_strdup_printf("subscribe %s\n", cmd.subscribe_uri);
2430 fd_write_all(uxsock, str, strlen(str));
2432 } else if (cmd.send) {
2433 fd_write_all(uxsock, "send\n", 5);
2434 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
2435 fd_write(uxsock, "online\n", 6);
2436 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
2437 fd_write(uxsock, "offline\n", 7);
2438 } else if (cmd.status || cmd.status_full) {
2439 gchar buf[BUFFSIZE];
2441 const gchar *command;
2445 command = cmd.status_full ? "status-full\n" : "status\n";
2446 folders = cmd.status_full ? cmd.status_full_folders :
2449 fd_write_all(uxsock, command, strlen(command));
2450 for (i = 0; folders && i < folders->len; ++i) {
2451 folder = g_ptr_array_index(folders, i);
2452 fd_write_all(uxsock, folder, strlen(folder));
2453 fd_write_all(uxsock, "\n", 1);
2455 fd_write_all(uxsock, ".\n", 2);
2457 fd_gets(uxsock, buf, sizeof(buf));
2458 if (!strncmp(buf, ".\n", 2)) break;
2461 } else if (cmd.exit) {
2462 fd_write_all(uxsock, "exit\n", 5);
2463 } else if (cmd.statistics) {
2465 fd_write(uxsock, "statistics\n", 11);
2467 fd_gets(uxsock, buf, sizeof(buf));
2468 if (!strncmp(buf, ".\n", 2)) break;
2471 } else if (cmd.reset_statistics) {
2472 fd_write(uxsock, "reset_statistics\n", 17);
2473 } else if (cmd.target) {
2474 gchar *str = g_strdup_printf("select %s\n", cmd.target);
2475 fd_write_all(uxsock, str, strlen(str));
2477 } else if (cmd.search) {
2478 gchar buf[BUFFSIZE];
2480 g_strdup_printf("search %s\n%s\n%s\n%c\n",
2481 cmd.search_folder, cmd.search_type, cmd.search_request,
2482 (cmd.search_recursive==TRUE)?'1':'0');
2483 fd_write_all(uxsock, str, strlen(str));
2486 fd_gets(uxsock, buf, sizeof(buf));
2487 if (!strncmp(buf, ".\n", 2)) break;
2493 fd_write_all(uxsock, "get_display\n", 12);
2494 memset(buf, 0, sizeof(buf));
2495 fd_gets(uxsock, buf, sizeof(buf));
2496 if (strcmp2(buf, x_display)) {
2497 g_print("Claws Mail is already running on display %s.\n",
2501 uxsock = fd_connect_unix(path);
2502 fd_write_all(uxsock, "popup\n", 6);
2505 fd_write_all(uxsock, "popup\n", 6);
2513 static gint lock_socket_remove(void)
2518 if (lock_socket < 0) {
2522 if (lock_socket_tag > 0) {
2523 g_source_remove(lock_socket_tag);
2525 fd_close(lock_socket);
2528 filename = claws_get_socket_name();
2529 claws_unlink(filename);
2535 static GPtrArray *get_folder_item_list(gint sock)
2537 gchar buf[BUFFSIZE];
2539 GPtrArray *folders = NULL;
2542 fd_gets(sock, buf, sizeof(buf));
2543 if (!strncmp(buf, ".\n", 2)) {
2548 folders = g_ptr_array_new();
2550 item = folder_find_item_from_identifier(buf);
2552 g_ptr_array_add(folders, item);
2554 g_warning("no such folder: %s\n", buf);
2561 static void lock_socket_input_cb(gpointer data,
2563 GIOCondition condition)
2565 MainWindow *mainwin = (MainWindow *)data;
2567 gchar buf[BUFFSIZE];
2569 sock = fd_accept(source);
2570 fd_gets(sock, buf, sizeof(buf));
2572 if (!strncmp(buf, "popup", 5)) {
2573 main_window_popup(mainwin);
2574 } else if (!strncmp(buf, "get_display", 11)) {
2575 fd_write_all(sock, x_display, strlen(x_display));
2576 } else if (!strncmp(buf, "receive_all", 11)) {
2577 inc_all_account_mail(mainwin, FALSE,
2578 prefs_common.newmail_notify_manu);
2579 } else if (!strncmp(buf, "receive", 7)) {
2580 inc_mail(mainwin, prefs_common.newmail_notify_manu);
2581 } else if (!strncmp(buf, "cancel_receiving", 16)) {
2584 } else if (!strncmp(buf, "compose_attach", 14)) {
2585 GList *files = NULL, *curr;
2589 mailto = g_strdup(buf + strlen("compose_attach") + 1);
2590 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
2592 if (!strcmp2(buf, "."))
2595 ainfo = g_new0(AttachInfo, 1);
2596 ainfo->file = g_strdup(buf);
2597 files = g_list_append(files, ainfo);
2599 open_compose_new(mailto, files);
2601 curr = g_list_first(files);
2602 while (curr != NULL) {
2603 ainfo = (AttachInfo *)curr->data;
2604 g_free(ainfo->file);
2610 } else if (!strncmp(buf, "compose", 7)) {
2611 open_compose_new(buf + strlen("compose") + 1, NULL);
2612 } else if (!strncmp(buf, "subscribe", 9)) {
2613 main_window_popup(mainwin);
2614 folder_subscribe(buf + strlen("subscribe") + 1);
2615 } else if (!strncmp(buf, "send", 4)) {
2617 } else if (!strncmp(buf, "online", 6)) {
2618 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
2619 } else if (!strncmp(buf, "offline", 7)) {
2620 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
2621 } else if (!strncmp(buf, "status-full", 11) ||
2622 !strncmp(buf, "status", 6)) {
2626 folders = get_folder_item_list(sock);
2627 status = folder_get_status
2628 (folders, !strncmp(buf, "status-full", 11));
2629 fd_write_all(sock, status, strlen(status));
2630 fd_write_all(sock, ".\n", 2);
2632 if (folders) g_ptr_array_free(folders, TRUE);
2633 } else if (!strncmp(buf, "statistics", 10)) {
2636 g_snprintf(tmp, sizeof(tmp), _("Session statistics\n"));
2637 fd_write_all(sock, tmp, strlen(tmp));
2639 if (prefs_common.date_format) {
2644 lt = localtime(&session_stats.time_started);
2645 fast_strftime(date, len, prefs_common.date_format, lt);
2646 g_snprintf(tmp, sizeof(tmp), _("Started: %s\n"),
2647 lt ? date : ctime(&session_stats.time_started));
2649 g_snprintf(tmp, sizeof(tmp), _("Started: %s\n"),
2650 ctime(&session_stats.time_started));
2651 fd_write_all(sock, tmp, strlen(tmp));
2653 fd_write_all(sock, "\n", 1);
2655 g_snprintf(tmp, sizeof(tmp), _("Incoming traffic\n"));
2656 fd_write_all(sock, tmp, strlen(tmp));
2658 g_snprintf(tmp, sizeof(tmp), _("Received messages: %d\n"),
2659 session_stats.received);
2660 fd_write_all(sock, tmp, strlen(tmp));
2662 fd_write_all(sock, "\n", 1);
2664 g_snprintf(tmp, sizeof(tmp), _("Outgoing traffic\n"));
2665 fd_write_all(sock, tmp, strlen(tmp));
2667 g_snprintf(tmp, sizeof(tmp), _("New/redirected messages: %d\n"),
2668 session_stats.sent);
2669 fd_write_all(sock, tmp, strlen(tmp));
2671 g_snprintf(tmp, sizeof(tmp), _("Replied messages: %d\n"),
2672 session_stats.replied);
2673 fd_write_all(sock, tmp, strlen(tmp));
2675 g_snprintf(tmp, sizeof(tmp), _("Forwarded messages: %d\n"),
2676 session_stats.forwarded);
2677 fd_write_all(sock, tmp, strlen(tmp));
2679 g_snprintf(tmp, sizeof(tmp), _("Total outgoing messages: %d\n"),
2680 (session_stats.sent + session_stats.replied +
2681 session_stats.forwarded));
2682 fd_write_all(sock, tmp, strlen(tmp));
2684 fd_write_all(sock, ".\n", 2);
2685 } else if (!strncmp(buf, "reset_statistics", 16)) {
2687 } else if (!strncmp(buf, "select ", 7)) {
2688 const gchar *target = buf+7;
2689 mainwindow_jump_to(target, TRUE);
2690 } else if (!strncmp(buf, "search ", 7)) {
2691 FolderItem* folderItem = NULL;
2692 GSList *messages = NULL;
2693 gchar *folder_name = NULL;
2694 gchar *request = NULL;
2695 AdvancedSearch *search;
2697 AdvancedSearchType searchType = ADVANCED_SEARCH_EXTENDED;
2699 search = advsearch_new();
2701 folder_name = g_strdup(buf+7);
2702 strretchomp(folder_name);
2704 if (fd_gets(sock, buf, sizeof(buf)) <= 0)
2707 switch (toupper(buf[0])) {
2708 case 'S': searchType = ADVANCED_SEARCH_SUBJECT; break;
2709 case 'F': searchType = ADVANCED_SEARCH_FROM; break;
2710 case 'T': searchType = ADVANCED_SEARCH_TO; break;
2711 case 'M': searchType = ADVANCED_SEARCH_MIXED; break;
2712 case 'G': searchType = ADVANCED_SEARCH_TAG; break;
2713 case 'E': searchType = ADVANCED_SEARCH_EXTENDED; break;
2716 if (fd_gets(sock, buf, sizeof(buf)) <= 0)
2719 request = g_strdup(buf);
2720 strretchomp(request);
2723 if (fd_gets(sock, buf, sizeof(buf)) > 0)
2724 recursive = buf[0] != '0';
2726 debug_print("search: %s %i %s %i\n", folder_name, searchType, request, recursive);
2729 folderItem = folder_find_item_from_identifier(folder_name);
2731 if (folder_name && folderItem == NULL) {
2732 debug_print("Unknown folder item : '%s', searching folder\n",folder_name);
2733 Folder* folder = folder_find_from_path(folder_name);
2735 folderItem = FOLDER_ITEM(folder->node->data);
2737 debug_print("Unknown folder: '%s'\n",folder_name);
2739 debug_print("%s %s\n",folderItem->name, folderItem->path);
2742 if (folderItem != NULL) {
2743 advsearch_set(search, searchType, request);
2744 advsearch_search_msgs_in_folders(search, &messages, folderItem, recursive);
2746 g_print("Folder '%s' not found.\n'", folder_name);
2750 for (cur = messages; cur != NULL; cur = cur->next) {
2751 MsgInfo* msg = (MsgInfo *)cur->data;
2752 gchar *file = procmsg_get_message_file_path(msg);
2753 fd_write_all(sock, file, strlen(file));
2754 fd_write_all(sock, "\n", 1);
2757 fd_write_all(sock, ".\n", 2);
2760 g_free(folder_name);
2762 advsearch_free(search);
2763 if (messages != NULL)
2764 procmsg_msg_list_free(messages);
2765 } else if (!strncmp(buf, "exit", 4)) {
2766 if (prefs_common.clean_on_exit && !prefs_common.ask_on_clean) {
2767 procmsg_empty_all_trash();
2769 app_will_exit(NULL, mainwin);
2775 static void open_compose_new(const gchar *address, GList *attach_files)
2780 Xstrdup_a(addr, address, return);
2784 compose_new(NULL, addr, attach_files);
2787 static void send_queue(void)
2790 gchar *errstr = NULL;
2791 gboolean error = FALSE;
2792 for (list = folder_get_list(); list != NULL; list = list->next) {
2793 Folder *folder = list->data;
2795 if (folder->queue) {
2796 gint res = procmsg_send_queue
2797 (folder->queue, prefs_common.savemsg,
2801 folder_item_scan(folder->queue);
2809 alertpanel_error_log(_("Some errors occurred "
2810 "while sending queued messages:\n%s"), errstr);
2813 alertpanel_error_log("Some errors occurred "
2814 "while sending queued messages.");
2818 static void quit_signal_handler(int sig)
2820 debug_print("Quitting on signal %d\n", sig);
2822 g_timeout_add(0, clean_quit, NULL);
2825 static void install_basic_sighandlers()
2829 struct sigaction act;
2834 sigaddset(&mask, SIGTERM);
2837 sigaddset(&mask, SIGINT);
2840 sigaddset(&mask, SIGHUP);
2843 act.sa_handler = quit_signal_handler;
2848 sigaction(SIGTERM, &act, 0);
2851 sigaction(SIGINT, &act, 0);
2854 sigaction(SIGHUP, &act, 0);
2857 sigprocmask(SIG_UNBLOCK, &mask, 0);
2858 #endif /* !G_OS_WIN32 */
2861 #if (defined linux && defined SIGIO)
2862 static int mem_notify_fd = 0;
2864 static gboolean clean_caches(gpointer unused)
2866 if (static_mainwindow && static_mainwindow->lock_count > 0)
2868 debug_print("/dev/mem_notify: callback: Freeing some memory now!\n");
2869 folder_clean_cache_memory_force();
2873 static void memory_signal_handler(int sig)
2875 debug_print("/dev/mem_notify: Kernel says we should free up some memory!\n");
2876 g_timeout_add(10, clean_caches, NULL);
2879 static void install_memory_sighandler()
2882 struct sigaction act;
2885 mem_notify_fd = g_open("/dev/mem_notify", O_RDONLY|O_NONBLOCK, 0);
2886 if (mem_notify_fd == -1) {
2887 debug_print("/dev/mem_notify not available (%s)\n",
2892 fcntl(mem_notify_fd, F_SETOWN, getpid());
2893 flags = fcntl(mem_notify_fd, F_GETFL);
2894 fcntl(mem_notify_fd, flags|FASYNC);
2898 sigaddset(&mask, SIGIO);
2900 act.sa_handler = memory_signal_handler;
2904 sigaction(SIGIO, &act, 0);
2906 sigprocmask(SIG_UNBLOCK, &mask, 0);
2908 debug_print("/dev/mem_notify: installed handler\n");
2910 #endif /* linux && SIGIO */
2913 osso_context_t *get_osso_context(void)
2915 return static_osso_context;
2920 #ifdef HAVE_NETWORKMANAGER_SUPPORT
2921 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
2924 MainWindow *mainWin;
2927 if (static_mainwindow)
2928 mainWin = static_mainwindow;
2930 mainWin = (MainWindow*)data;
2932 if (!prefs_common.use_networkmanager)
2940 online = networkmanager_is_online(&error);
2942 if(online && went_offline_nm) {
2943 went_offline_nm = FALSE;
2944 main_window_toggle_work_offline(mainWin, FALSE, FALSE);
2945 debug_print("NetworkManager: Went online\n");
2946 log_message(LOG_PROTOCOL, _("NetworkManager: network is online.\n"));
2949 went_offline_nm = TRUE;
2950 main_window_toggle_work_offline(mainWin, TRUE, FALSE);
2951 debug_print("NetworkManager: Went offline\n");
2952 log_message(LOG_PROTOCOL, _("NetworkManager: network is offline.\n"));
2956 debug_print("Failed to get online information from NetworkManager: %s\n",
2958 g_error_free(error);
2962 debug_print("NetworkManager: Cannot change connection state because "
2963 "main window does not exist\n");
2966 /* Returns true (and sets error appropriately, if given) in case of error */
2967 gboolean networkmanager_is_online(GError **error)
2969 DBusGConnection *connection;
2971 GError *tmp_error = NULL;
2975 if (!prefs_common.use_networkmanager)
2980 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &tmp_error);
2983 /* If calling code doesn't do error checking, at least print some debug */
2984 if((error == NULL) || (*error == NULL))
2985 debug_print("Failed to open connection to system bus: %s\n",
2986 tmp_error->message);
2987 g_propagate_error(error, tmp_error);
2991 proxy = dbus_g_proxy_new_for_name(connection,
2992 "org.freedesktop.NetworkManager",
2993 "/org/freedesktop/NetworkManager",
2994 "org.freedesktop.NetworkManager");
2996 retVal = dbus_g_proxy_call(proxy,"state",&tmp_error, G_TYPE_INVALID,
2997 G_TYPE_UINT, &state, G_TYPE_INVALID);
3000 g_object_unref(proxy);
3002 dbus_g_connection_unref(connection);
3005 /* If calling code doesn't do error checking, at least print some debug */
3006 if((error == NULL) || (*error == NULL))
3007 debug_print("Failed to get state info from NetworkManager: %s\n",
3008 tmp_error->message);
3009 g_propagate_error(error, tmp_error);
3012 #if NM_CHECK_VERSION(0,8,992)
3013 return (state == NM_STATE_CONNECTED_LOCAL ||
3014 state == NM_STATE_CONNECTED_SITE ||
3015 state == NM_STATE_CONNECTED_GLOBAL ||
3016 state == NM_STATE_UNKNOWN);
3018 return (state == NM_STATE_CONNECTED ||
3019 state == NM_STATE_UNKNOWN);