2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2011 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/>.
27 #include <glib/gi18n.h>
37 #include <sys/types.h>
44 #include <X11/SM/SMlib.h>
48 #ifdef HAVE_STARTUP_NOTIFICATION
49 # define SN_API_NOT_YET_FROZEN
50 # include <libsn/sn-launchee.h>
51 # include <gdk/gdkx.h>
55 #include <dbus/dbus-glib.h>
57 #ifdef HAVE_NETWORKMANAGER_SUPPORT
58 #include <NetworkManager.h>
66 #include "mainwindow.h"
67 #include "folderview.h"
68 #include "image_viewer.h"
69 #include "summaryview.h"
70 #include "prefs_common.h"
71 #include "prefs_account.h"
72 #include "prefs_actions.h"
73 #include "prefs_ext_prog.h"
74 #include "prefs_fonts.h"
75 #include "prefs_image_viewer.h"
76 #include "prefs_message.h"
77 #include "prefs_receive.h"
78 #include "prefs_msg_colors.h"
79 #include "prefs_quote.h"
80 #include "prefs_spelling.h"
81 #include "prefs_summaries.h"
82 #include "prefs_themes.h"
83 #include "prefs_other.h"
84 #include "prefs_logging.h"
85 #include "prefs_send.h"
86 #include "prefs_wrapping.h"
87 #include "prefs_compose_writing.h"
88 #include "prefs_display_header.h"
93 #include "manage_window.h"
94 #include "alertpanel.h"
95 #include "statusbar.h"
96 #include "addressbook.h"
101 #include "gtkutils.h"
104 #include "prefs_toolbar.h"
107 #include "imap_gtk.h"
108 #include "news_gtk.h"
113 #include "quicksearch.h"
116 #include "imap-thread.h"
117 #include "nntp-thread.h"
119 #include "stock_pixmap.h"
132 #include <hildon/hildon-banner.h>
133 #include <hildon/hildon-program.h>
135 #include <hildon-widgets/hildon-banner.h>
136 #include <hildon-widgets/hildon-program.h>
139 #include <libgnomevfs/gnome-vfs-volume.h>
140 #include <libgnomevfs/gnome-vfs-volume-monitor.h>
141 #include <libgnomevfs/gnome-vfs-utils.h>
143 #define OSSO_NAME "claws_mail"
144 #define OSSO_SERVICE "com.nokia."OSSO_NAME
145 #define OSSO_OBJECT "/com/nokia/"OSSO_NAME
146 #define OSSO_IFACE "com.nokia."OSSO_NAME
148 typedef struct _AppData AppData;
150 HildonProgram *program;
151 HildonWindow *window;
152 osso_context_t *osso_context;
155 static GnomeVFSVolumeMonitor *volmon;
158 #ifdef HAVE_NETWORKMANAGER_SUPPORT
159 /* Went offline due to NetworkManager */
160 static gboolean went_offline_nm;
163 #if !defined(NM_CHECK_VERSION)
164 #define NM_CHECK_VERSION(x,y,z) 0
167 #ifdef HAVE_DBUS_GLIB
168 static DBusGProxy *awn_proxy = NULL;
175 HildonProgram *hildon_program;
178 #ifdef HAVE_STARTUP_NOTIFICATION
179 static SnLauncheeContext *sn_context = NULL;
180 static SnDisplay *sn_display = NULL;
183 static gint lock_socket = -1;
184 static gint lock_socket_tag = 0;
185 static gchar *x_display = NULL;
188 ONLINE_MODE_DONT_CHANGE,
193 static struct RemoteCmd {
195 gboolean receive_all;
197 const gchar *compose_mailto;
198 GPtrArray *attach_files;
200 const gchar *search_folder;
201 const gchar *search_type;
202 const gchar *search_request;
203 gboolean search_recursive;
205 gboolean status_full;
206 GPtrArray *status_folders;
207 GPtrArray *status_full_folders;
214 const gchar *subscribe_uri;
218 static void parse_cmd_opt(int argc, char *argv[]);
220 static gint prohibit_duplicate_launch (void);
221 static gchar * get_crashfile_name (void);
222 static gint lock_socket_remove (void);
223 static void lock_socket_input_cb (gpointer data,
225 GdkInputCondition condition);
227 static void open_compose_new (const gchar *address,
228 GPtrArray *attach_files);
230 static void send_queue (void);
231 static void initial_processing (FolderItem *item, gpointer data);
232 static void quit_signal_handler (int sig);
233 static void install_basic_sighandlers (void);
234 #if (defined linux && defined SIGIO)
235 static void install_memory_sighandler (void);
237 static void exit_claws (MainWindow *mainwin);
239 #ifdef HAVE_NETWORKMANAGER_SUPPORT
240 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
244 #define MAKE_DIR_IF_NOT_EXIST(dir) \
246 if (!is_dir_exist(dir)) { \
247 if (is_file_exist(dir)) { \
249 (_("File '%s' already exists.\n" \
250 "Can't create folder."), \
254 if (make_dir(dir) < 0) \
259 static MainWindow *static_mainwindow;
262 static osso_context_t *static_osso_context;
264 void exit_event_handler(gboolean die_now, gpointer data)
267 appdata = (AppData *) data;
268 /* Do whatever application needs to do before exiting */
269 exit_claws(static_mainwindow);
270 hildon_banner_show_information(GTK_WIDGET(appdata->window), NULL,
275 /* Callback for hardware D-BUS events */
276 void hw_event_handler(osso_hw_state_t *state, gpointer data)
279 appdata = (AppData *) data;
281 if (state->shutdown_ind) {
282 exit_claws(static_mainwindow);
283 hildon_banner_show_information(GTK_WIDGET(appdata->window), NULL,
288 /* Callback for normal D-BUS messages */
289 gint dbus_req_handler(const gchar * interface, const gchar * method,
290 GArray * arguments, gpointer data,
294 appdata = (AppData *) data;
296 if (!strcmp(method, "top_application")) {
297 osso_rpc_free_val(retval);
300 osso_system_note_infoprint(appdata->osso_context, method, retval);
301 osso_rpc_free_val(retval);
306 static gboolean emergency_exit = FALSE;
308 #ifdef HAVE_STARTUP_NOTIFICATION
309 static void sn_error_trap_push(SnDisplay *display, Display *xdisplay)
311 gdk_error_trap_push();
314 static void sn_error_trap_pop(SnDisplay *display, Display *xdisplay)
316 gdk_error_trap_pop();
319 static void startup_notification_complete(gboolean with_window)
322 GtkWidget *hack = NULL;
325 /* this is needed to make the startup notification leave,
326 * if we have been launched from a menu.
327 * We have to display a window, so let it be very little */
328 hack = gtk_window_new(GTK_WINDOW_POPUP);
329 gtk_window_move(GTK_WINDOW(hack), 0, 0);
330 gtk_widget_set_size_request(hack, 1, 1);
331 gtk_widget_show(hack);
334 xdisplay = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
335 sn_display = sn_display_new(xdisplay,
338 sn_context = sn_launchee_context_new_from_environment(sn_display,
339 DefaultScreen(xdisplay));
341 if (sn_context != NULL) {
342 sn_launchee_context_complete(sn_context);
343 sn_launchee_context_unref(sn_context);
344 sn_display_unref(sn_display);
347 gtk_widget_destroy(hack);
350 #endif /* HAVE_STARTUP_NOTIFICATION */
352 static void claws_gtk_idle(void)
354 while(gtk_events_pending()) {
355 gtk_main_iteration();
360 static gboolean sc_starting = FALSE;
362 static gboolean defer_check_all(void *data)
364 gboolean autochk = GPOINTER_TO_INT(data);
366 inc_all_account_mail(static_mainwindow, autochk,
367 prefs_common.newmail_notify_manu);
371 main_window_set_menu_sensitive(static_mainwindow);
372 toolbar_main_set_sensitive(static_mainwindow);
377 static gboolean defer_check(void *data)
379 inc_mail(static_mainwindow, prefs_common.newmail_notify_manu);
383 main_window_set_menu_sensitive(static_mainwindow);
384 toolbar_main_set_sensitive(static_mainwindow);
389 static gboolean defer_jump(void *data)
391 if (cmd.receive_all) {
392 defer_check_all(GINT_TO_POINTER(FALSE));
393 } else if (prefs_common.chk_on_startup) {
394 defer_check_all(GINT_TO_POINTER(TRUE));
395 } else if (cmd.receive) {
398 mainwindow_jump_to(data, FALSE);
401 main_window_set_menu_sensitive(static_mainwindow);
402 toolbar_main_set_sensitive(static_mainwindow);
407 static void chk_update_val(GtkWidget *widget, gpointer data)
409 gboolean *val = (gboolean *)data;
410 *val = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
413 static gboolean migrate_old_config(const gchar *old_cfg_dir, const gchar *new_cfg_dir, const gchar *oldversion)
415 gchar *message = g_strdup_printf(_("Configuration for %s found.\n"
416 "Do you want to migrate this configuration?"), oldversion);
417 gchar *message2 = g_strdup_printf(_("\n\nYour Sylpheed filtering rules can be converted by a\n"
418 "script available at %s."), TOOLS_URI);
420 if (!strcmp(oldversion, "Sylpheed"))
421 message = g_strconcat(message, message2, NULL);
425 GtkWidget *window = NULL;
426 GtkWidget *keep_backup_chk;
428 gboolean backup = TRUE;
430 keep_backup_chk = gtk_check_button_new_with_label (_("Keep old configuration"));
431 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_backup_chk), TRUE);
432 CLAWS_SET_TIP(keep_backup_chk,
433 _("Keeping a backup will allow you to go back to an "
434 "older version, but may take a while if you have "
435 "cached IMAP or News data, and will take some extra "
436 "room on your disk."));
438 g_signal_connect(G_OBJECT(keep_backup_chk), "toggled",
439 G_CALLBACK(chk_update_val), &backup);
441 if (alertpanel_full(_("Migration of configuration"), message,
442 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL, FALSE,
443 keep_backup_chk, ALERT_QUESTION, G_ALERTDEFAULT) != G_ALERTALTERNATE) {
447 /* we can either do a fast migration requiring not any extra disk
448 * space, or a slow one that copies the old configuration and leaves
452 window = label_window_create(_("Copying configuration... This may take a while..."));
455 r = copy_dir(old_cfg_dir, new_cfg_dir);
456 label_window_destroy(window);
458 /* if copy failed, we'll remove the partially copied
461 alertpanel_error(_("Migration failed!"));
462 remove_dir_recursive(new_cfg_dir);
465 /* fast mode failed, but we don't want backup */
466 remove_dir_recursive(old_cfg_dir);
470 window = label_window_create(_("Migrating configuration..."));
473 r = g_rename(old_cfg_dir, new_cfg_dir);
474 label_window_destroy(window);
476 /* if g_rename failed, we'll try to copy */
478 FILE_OP_ERROR(new_cfg_dir, "g_rename failed, trying copy\n");
485 static int migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
488 gchar *plugin_path, *old_plugin_path, *new_plugin_path;
490 gboolean err = FALSE;
492 oldfp = g_fopen(old_rc, "r");
495 newfp = g_fopen(new_rc, "w");
501 plugin_path = g_strdup(get_plugin_dir());
502 new_plugin_path = g_strdup(plugin_path);
504 if (strstr(plugin_path, "/claws-mail/")) {
505 gchar *end = g_strdup(strstr(plugin_path, "/claws-mail/")+strlen("/claws-mail/"));
506 *(strstr(plugin_path, "/claws-mail/")) = '\0';
507 old_plugin_path = g_strconcat(plugin_path, "/sylpheed-claws/", end, NULL);
510 old_plugin_path = g_strdup(new_plugin_path);
512 debug_print("replacing %s with %s\n", old_plugin_path, new_plugin_path);
513 while (fgets(buf, sizeof(buf), oldfp)) {
514 if (strncmp(buf, old_plugin_path, strlen(old_plugin_path))) {
515 err |= (fputs(buf, newfp) == EOF);
517 debug_print("->replacing %s", buf);
518 debug_print(" with %s%s", new_plugin_path, buf+strlen(old_plugin_path));
519 err |= (fputs(new_plugin_path, newfp) == EOF);
520 err |= (fputs(buf+strlen(old_plugin_path), newfp) == EOF);
524 g_free(new_plugin_path);
525 g_free(old_plugin_path);
527 if (fclose(newfp) == EOF)
535 sc_client_set_value (MainWindow *mainwin,
546 prop.num_vals = num_vals;
550 if (mainwin->smc_conn)
551 SmcSetProperties ((SmcConn) mainwin->smc_conn, 1, proplist);
554 static void sc_die_callback (SmcConn smc_conn, SmPointer client_data)
559 static void sc_save_complete_callback(SmcConn smc_conn, SmPointer client_data)
563 static void sc_shutdown_cancelled_callback (SmcConn smc_conn, SmPointer client_data)
565 MainWindow *mainwin = (MainWindow *)client_data;
566 if (mainwin->smc_conn)
567 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
570 static void sc_save_yourself_callback (SmcConn smc_conn,
571 SmPointer client_data,
577 MainWindow *mainwin = (MainWindow *)client_data;
578 if (mainwin->smc_conn)
579 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
582 static IceIOErrorHandler sc_ice_installed_handler;
584 static void sc_ice_io_error_handler (IceConn connection)
586 if (sc_ice_installed_handler)
587 (*sc_ice_installed_handler) (connection);
589 static gboolean sc_process_ice_messages (GIOChannel *source,
590 GIOCondition condition,
593 IceConn connection = (IceConn) data;
594 IceProcessMessagesStatus status;
596 status = IceProcessMessages (connection, NULL, NULL);
598 if (status == IceProcessMessagesIOError) {
599 IcePointer context = IceGetConnectionContext (connection);
601 if (context && GTK_IS_OBJECT (context)) {
602 guint disconnect_id = g_signal_lookup ("disconnect", G_OBJECT_TYPE (context));
604 if (disconnect_id > 0)
605 g_signal_emit (context, disconnect_id, 0);
607 IceSetShutdownNegotiation (connection, False);
608 IceCloseConnection (connection);
615 static void new_ice_connection (IceConn connection, IcePointer client_data, Bool opening,
616 IcePointer *watch_data)
622 /* Make sure we don't pass on these file descriptors to any
624 fcntl(IceConnectionNumber(connection),F_SETFD,
625 fcntl(IceConnectionNumber(connection),F_GETFD,0) | FD_CLOEXEC);
627 channel = g_io_channel_unix_new (IceConnectionNumber (connection));
628 input_id = g_io_add_watch (channel,
629 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
630 sc_process_ice_messages,
632 g_io_channel_unref (channel);
634 *watch_data = (IcePointer) GUINT_TO_POINTER (input_id);
636 input_id = GPOINTER_TO_UINT ((gpointer) *watch_data);
637 g_source_remove (input_id);
641 static void sc_session_manager_connect(MainWindow *mainwin)
643 static gboolean connected = FALSE;
644 SmcCallbacks callbacks;
646 IceIOErrorHandler default_handler;
653 sc_ice_installed_handler = IceSetIOErrorHandler (NULL);
654 default_handler = IceSetIOErrorHandler (sc_ice_io_error_handler);
656 if (sc_ice_installed_handler == default_handler)
657 sc_ice_installed_handler = NULL;
659 IceAddConnectionWatch (new_ice_connection, NULL);
662 callbacks.save_yourself.callback = sc_save_yourself_callback;
663 callbacks.die.callback = sc_die_callback;
664 callbacks.save_complete.callback = sc_save_complete_callback;
665 callbacks.shutdown_cancelled.callback = sc_shutdown_cancelled_callback;
667 callbacks.save_yourself.client_data =
668 callbacks.die.client_data =
669 callbacks.save_complete.client_data =
670 callbacks.shutdown_cancelled.client_data = (SmPointer) mainwin;
671 if (g_getenv ("SESSION_MANAGER")) {
672 gchar error_string_ret[256] = "";
674 mainwin->smc_conn = (gpointer)
675 SmcOpenConnection (NULL, mainwin,
676 SmProtoMajor, SmProtoMinor,
677 SmcSaveYourselfProcMask | SmcDieProcMask |
678 SmcSaveCompleteProcMask |
679 SmcShutdownCancelledProcMask,
682 256, error_string_ret);
684 if (error_string_ret[0] || mainwin->smc_conn == NULL)
685 g_warning ("While connecting to session manager:\n%s.",
689 vals = g_new (SmPropValue, 1);
690 vals[0].length = strlen(argv0);
691 vals[0].value = argv0;
692 sc_client_set_value (mainwin, SmCloneCommand, SmLISTofARRAY8, 1, vals);
693 sc_client_set_value (mainwin, SmRestartCommand, SmLISTofARRAY8, 1, vals);
694 sc_client_set_value (mainwin, SmProgram, SmARRAY8, 1, vals);
696 vals[0].length = strlen(g_get_user_name()?g_get_user_name():"");
697 vals[0].value = g_strdup(g_get_user_name()?g_get_user_name():"");
698 sc_client_set_value (mainwin, SmUserID, SmARRAY8, 1, vals);
704 static gboolean sc_exiting = FALSE;
705 static gboolean show_at_startup = TRUE;
706 static gboolean claws_crashed_bool = FALSE;
708 gboolean claws_crashed(void) {
709 return claws_crashed_bool;
712 void main_set_show_at_startup(gboolean show)
714 show_at_startup = show;
718 static void main_vol_mount_cb(GnomeVFSVolumeMonitor *vfs, GnomeVFSVolume *vol, MainWindow *mainwin)
720 gchar *uri = gnome_vfs_volume_get_activation_uri (vol);
721 gchar *mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
724 if(!strcmp(mount_path, prefs_common.data_root)) {
725 gtk_widget_set_sensitive(mainwin->window, TRUE);
731 static void main_vol_unmount_cb(GnomeVFSVolumeMonitor *vfs, GnomeVFSVolume *vol, MainWindow *mainwin)
733 gchar *uri = gnome_vfs_volume_get_activation_uri (vol);
734 gchar *mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
737 if(!strcmp(mount_path, prefs_common.data_root)) {
738 gtk_widget_set_sensitive(mainwin->window, FALSE);
747 static FILE* win32_debug_fp=NULL;
748 static guint win32_log_handler_app_id;
749 static guint win32_log_handler_glib_id;
750 static guint win32_log_handler_gtk_id;
752 static void win32_print_stdout(const gchar *string)
754 if (win32_debug_fp) {
755 fprintf(win32_debug_fp, string);
756 fflush(win32_debug_fp);
760 static void win32_print_stderr(const gchar *string)
762 if (win32_debug_fp) {
763 fprintf(win32_debug_fp, string);
764 fflush(win32_debug_fp);
768 static void win32_log(const gchar *log_domain, GLogLevelFlags log_level, const gchar* message, gpointer user_data)
770 if (win32_debug_fp) {
773 switch(log_level & G_LOG_LEVEL_MASK)
775 case G_LOG_LEVEL_ERROR:
778 case G_LOG_LEVEL_CRITICAL:
781 case G_LOG_LEVEL_WARNING:
784 case G_LOG_LEVEL_MESSAGE:
787 case G_LOG_LEVEL_INFO:
790 case G_LOG_LEVEL_DEBUG:
797 fprintf(win32_debug_fp, "%s: %s: %s", log_domain, type, message);
799 fprintf(win32_debug_fp, "%s: %s", type, message);
800 fflush(win32_debug_fp);
804 static void win32_open_log(void)
806 gchar *logfile = g_strconcat(g_get_tmp_dir(), G_DIR_SEPARATOR_S, "claws-win32.log", NULL);
807 gchar *oldlogfile = g_strconcat(g_get_tmp_dir(), G_DIR_SEPARATOR_S, "claws-win32.log.bak", NULL);
809 if (is_file_exist(logfile)) {
810 if (rename_force(logfile, oldlogfile) < 0)
811 FILE_OP_ERROR(logfile, "rename");
813 win32_debug_fp = g_fopen(logfile, "w");
818 g_set_print_handler(win32_print_stdout);
819 g_set_printerr_handler(win32_print_stdout);
820 win32_log_handler_app_id = g_log_set_handler(NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
821 | G_LOG_FLAG_RECURSION, win32_log, NULL);
822 win32_log_handler_glib_id = g_log_set_handler("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
823 | G_LOG_FLAG_RECURSION, win32_log, NULL);
824 win32_log_handler_gtk_id = g_log_set_handler("Gtk", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
825 | G_LOG_FLAG_RECURSION, win32_log, NULL);
829 static void win32_close_log(void)
833 g_log_remove_handler("", win32_log_handler_app_id);
834 g_log_remove_handler("GLib", win32_log_handler_glib_id);
835 g_log_remove_handler("Gtk", win32_log_handler_gtk_id);
836 fclose(win32_debug_fp);
842 static void main_dump_features_list(gboolean show_debug_only)
843 /* display compiled-in features list */
845 if (show_debug_only && !debug_get_mode())
849 debug_print("runtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
850 gtk_major_version, gtk_minor_version, gtk_micro_version,
851 glib_major_version, glib_minor_version, glib_micro_version);
853 g_print("runtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
854 gtk_major_version, gtk_minor_version, gtk_micro_version,
855 glib_major_version, glib_minor_version, glib_micro_version);
857 debug_print("buildtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
858 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
859 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
861 g_print("buildtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
862 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
863 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
866 debug_print("Compiled-in features:\n");
868 g_print("Compiled-in features:\n");
871 debug_print(" compface\n");
873 g_print(" compface\n");
877 debug_print(" aspell\n");
879 g_print(" aspell\n");
883 debug_print(" gnutls\n");
885 g_print(" gnutls\n");
889 debug_print(" ipv6\n");
895 debug_print(" iconv\n");
901 debug_print(" jpilot\n");
903 g_print(" jpilot\n");
907 debug_print(" ldap\n");
913 debug_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
915 g_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
919 debug_print(" libsm\n");
923 #if HAVE_NETWORKMANAGER_SUPPORT
925 debug_print(" NetworkManager\n");
927 g_print(" NetworkManager\n");
931 #ifdef HAVE_DBUS_GLIB
932 static guint dbus_item_hook_id = -1;
933 static guint dbus_folder_hook_id = -1;
935 static void uninstall_dbus_status_handler(void)
938 g_object_unref(awn_proxy);
940 if (dbus_item_hook_id != -1)
941 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, dbus_item_hook_id);
942 if (dbus_folder_hook_id != -1)
943 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, dbus_folder_hook_id);
946 static void dbus_update(FolderItem *removed_item)
948 guint new, unread, unreadmarked, marked, total;
949 guint replied, forwarded, locked, ignored, watched;
951 GError *error = NULL;
953 folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total,
954 &replied, &forwarded, &locked, &ignored,
957 total -= removed_item->total_msgs;
958 new -= removed_item->new_msgs;
959 unread -= removed_item->unread_msgs;
963 buf = g_strdup_printf("%d", new);
964 dbus_g_proxy_call(awn_proxy, "SetInfoByName", &error,
965 G_TYPE_STRING, "claws-mail",
967 G_TYPE_INVALID, G_TYPE_INVALID);
971 dbus_g_proxy_call(awn_proxy, "UnsetInfoByName", &error, G_TYPE_STRING,
972 "claws-mail", G_TYPE_INVALID, G_TYPE_INVALID);
975 debug_print("%s\n", error->message);
980 static gboolean dbus_status_update_folder_hook(gpointer source, gpointer data)
982 FolderUpdateData *hookdata;
984 if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
985 dbus_update(hookdata->item);
992 static gboolean dbus_status_update_item_hook(gpointer source, gpointer data)
999 static void install_dbus_status_handler(void)
1001 GError *tmp_error = NULL;
1002 DBusGConnection *connection = dbus_g_bus_get(DBUS_BUS_SESSION, &tmp_error);
1005 /* If calling code doesn't do error checking, at least print some debug */
1006 debug_print("Failed to open connection to session bus: %s\n",
1007 tmp_error->message);
1008 g_error_free(tmp_error);
1011 awn_proxy = dbus_g_proxy_new_for_name(connection,
1012 "com.google.code.Awn",
1013 "/com/google/code/Awn",
1014 "com.google.code.Awn");
1015 dbus_item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, dbus_status_update_item_hook, NULL);
1016 if (dbus_item_hook_id == -1) {
1017 g_warning(_("Failed to register folder item update hook"));
1018 uninstall_dbus_status_handler();
1022 dbus_folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, dbus_status_update_folder_hook, NULL);
1023 if (dbus_folder_hook_id == -1) {
1024 g_warning(_("Failed to register folder update hook"));
1025 uninstall_dbus_status_handler();
1031 int main(int argc, char *argv[])
1034 osso_context_t *osso_context;
1035 osso_return_t result;
1037 #ifdef HAVE_DBUS_GLIB
1038 DBusGConnection *connection;
1041 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1042 DBusGProxy *nm_proxy;
1045 MainWindow *mainwin;
1046 FolderView *folderview;
1048 gboolean crash_file_present = FALSE;
1049 gint num_folder_class = 0;
1050 gboolean asked_for_migration = FALSE;
1051 gboolean start_done = TRUE;
1052 GtkUIManager *gui_manager = NULL;
1053 GSList *plug_list = NULL;
1054 gboolean never_ran = FALSE;
1055 START_TIMING("startup");
1062 if (!claws_init(&argc, &argv)) {
1069 main_dump_features_list(TRUE);
1071 prog_version = PROG_VERSION;
1072 argv0 = g_strdup(argv[0]);
1074 parse_cmd_opt(argc, argv);
1076 prefs_prepare_cache();
1081 gtk_init(&argc, &argv);
1082 crash_main(cmd.crash_params);
1088 crash_install_handlers();
1090 install_basic_sighandlers();
1091 #if (defined linux && defined SIGIO)
1092 install_memory_sighandler();
1096 /* check and create unix domain socket for remote operation */
1097 lock_socket = prohibit_duplicate_launch();
1098 if (lock_socket < 0) {
1099 #ifdef HAVE_STARTUP_NOTIFICATION
1100 if(gtk_init_check(&argc, &argv))
1101 startup_notification_complete(TRUE);
1106 if (cmd.status || cmd.status_full || cmd.search) {
1107 puts("0 Claws Mail not running.");
1108 lock_socket_remove();
1114 if (!g_thread_supported())
1115 g_thread_init(NULL);
1118 gtk_init(&argc, &argv);
1121 gtk_settings_set_string_property(gtk_settings_get_default(),
1127 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1128 went_offline_nm = FALSE;
1131 #ifdef HAVE_DBUS_GLIB
1133 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
1136 debug_print("Failed to open connection to system bus: %s\n", error->message);
1137 g_error_free(error);
1140 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1141 nm_proxy = dbus_g_proxy_new_for_name(connection,
1142 "org.freedesktop.NetworkManager",
1143 "/org/freedesktop/NetworkManager",
1144 "org.freedesktop.NetworkManager");
1146 #if NM_CHECK_VERSION(0,8,992)
1147 dbus_g_proxy_add_signal(nm_proxy, "StateChanged", G_TYPE_UINT, G_TYPE_INVALID);
1148 dbus_g_proxy_connect_signal(nm_proxy, "StateChanged",
1149 G_CALLBACK(networkmanager_state_change_cb),
1152 dbus_g_proxy_add_signal(nm_proxy, "StateChange", G_TYPE_UINT, G_TYPE_INVALID);
1153 dbus_g_proxy_connect_signal(nm_proxy, "StateChange",
1154 G_CALLBACK(networkmanager_state_change_cb),
1159 install_dbus_status_handler();
1165 osso_context = osso_initialize(OSSO_SERVICE, "2.8.1", TRUE, NULL);
1166 if (osso_context == NULL) {
1169 hildon_program = HILDON_PROGRAM(hildon_program_get_instance());
1170 static_osso_context = osso_context;
1172 gtk_widget_set_default_colormap(
1173 gdk_screen_get_system_colormap(
1174 gdk_screen_get_default()));
1176 gui_manager = gtkut_create_ui_manager();
1178 /* Create container for all the menus we will be adding */
1179 MENUITEM_ADDUI("/", "Menus", NULL, GTK_UI_MANAGER_MENUBAR);
1181 if (!g_thread_supported()) {
1182 g_error(_("g_thread is not supported by glib.\n"));
1185 /* check that we're not on a too recent/old gtk+ */
1186 #if GTK_CHECK_VERSION(2, 9, 0)
1187 if (gtk_check_version(2, 9, 0) != NULL) {
1188 alertpanel_error(_("Claws Mail has been compiled with "
1189 "a more recent GTK+ library than is "
1190 "currently available. This will cause "
1191 "crashes. You need to upgrade GTK+ or "
1192 "recompile Claws Mail."));
1199 if (gtk_check_version(2, 9, 0) == NULL) {
1200 alertpanel_error(_("Claws Mail has been compiled with "
1201 "an older GTK+ library than is "
1202 "currently available. This will cause "
1203 "crashes. You need to recompile "
1213 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1215 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1218 /* no config dir exists. See if we can migrate an old config. */
1219 if (!is_dir_exist(RC_DIR)) {
1220 prefs_destroy_cache();
1223 /* if one of the old dirs exist, we'll ask if the user
1224 * want to migrates, and r will be TRUE if he said yes
1225 * and migration succeeded, and FALSE otherwise.
1227 if (is_dir_exist(OLD_GTK2_RC_DIR)) {
1228 r = migrate_old_config(OLD_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 2.6.0 (or older)"));
1229 asked_for_migration = TRUE;
1230 } else if (is_dir_exist(OLDER_GTK2_RC_DIR)) {
1231 r = migrate_old_config(OLDER_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.9.15 (or older)"));
1232 asked_for_migration = TRUE;
1233 } else if (is_dir_exist(OLD_GTK1_RC_DIR)) {
1234 r = migrate_old_config(OLD_GTK1_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.0.5 (or older)"));
1235 asked_for_migration = TRUE;
1236 } else if (is_dir_exist(SYLPHEED_RC_DIR)) {
1237 r = migrate_old_config(SYLPHEED_RC_DIR, RC_DIR, "Sylpheed");
1238 asked_for_migration = TRUE;
1241 /* If migration failed or the user didn't want to do it,
1242 * we create a new one (and we'll hit wizard later).
1244 if (r == FALSE && !is_dir_exist(RC_DIR)) {
1246 if (copy_dir(SYSCONFDIR "/skel/.claws-mail", RC_DIR) < 0) {
1248 if (!is_dir_exist(RC_DIR) && make_dir(RC_DIR) < 0) {
1261 if (!is_file_exist(RC_DIR G_DIR_SEPARATOR_S COMMON_RC) &&
1262 is_file_exist(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC)) {
1263 /* post 2.6 name change */
1264 migrate_common_rc(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC,
1265 RC_DIR G_DIR_SEPARATOR_S COMMON_RC);
1269 plugin_load_all("Common");
1271 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
1272 gtk_rc_parse(userrc);
1275 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1276 gtk_accel_map_load (userrc);
1280 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_rc_dir(), 1, win32_close_log(););
1282 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
1285 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
1286 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
1287 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
1288 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
1289 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
1290 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR);
1292 crash_file_present = is_file_exist(get_crashfile_name());
1293 /* remove temporary files */
1294 remove_all_files(get_tmp_dir());
1295 remove_all_files(get_mime_tmp_dir());
1297 if (!cmd.crash && crash_file_present)
1298 claws_crashed_bool = TRUE;
1300 if (is_file_exist("claws.log")) {
1301 if (rename_force("claws.log", "claws.log.bak") < 0)
1302 FILE_OP_ERROR("claws.log", "rename");
1304 set_log_file(LOG_PROTOCOL, "claws.log");
1306 if (is_file_exist("filtering.log")) {
1307 if (rename_force("filtering.log", "filtering.log.bak") < 0)
1308 FILE_OP_ERROR("filtering.log", "rename");
1310 set_log_file(LOG_DEBUG_FILTERING, "filtering.log");
1313 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1315 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1318 folder_system_init();
1319 prefs_common_read_config();
1321 prefs_themes_init();
1323 prefs_ext_prog_init();
1324 prefs_wrapping_init();
1325 prefs_compose_writing_init();
1326 prefs_msg_colors_init();
1327 image_viewer_init();
1328 prefs_image_viewer_init();
1330 prefs_summaries_init();
1331 prefs_message_init();
1333 prefs_logging_init();
1334 prefs_receive_init();
1338 gtkaspell_checkers_init();
1339 prefs_spelling_init();
1342 sock_set_io_timeout(prefs_common.io_timeout_secs);
1343 prefs_actions_read_config();
1344 prefs_display_header_read_config();
1345 /* prefs_filtering_read_config(); */
1346 addressbook_read_file();
1348 gtkut_widget_init();
1349 stock_pixbuf_gdk(NULL, STOCK_PIXMAP_CLAWS_MAIL_ICON, &icon);
1350 gtk_window_set_default_icon(icon);
1352 folderview_initialize();
1358 mainwin = main_window_create();
1360 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1361 networkmanager_state_change_cb(nm_proxy,NULL,mainwin);
1366 appdata = g_new0(AppData, 1);
1367 appdata->program = hildon_program;
1368 appdata->window = HILDON_WINDOW(mainwin->window);
1369 appdata->osso_context = osso_context;
1370 result = osso_rpc_set_cb_f(appdata->osso_context,
1374 dbus_req_handler, appdata);
1375 if (result != OSSO_OK) {
1380 /* Add handler for Exit D-BUS messages */
1381 result = osso_application_set_exit_cb(appdata->osso_context,
1383 (gpointer) appdata);
1384 if (result != OSSO_OK) {
1388 osso_hw_set_event_cb( appdata->osso_context,
1389 NULL, hw_event_handler, (gpointer) appdata );
1391 manage_window_focus_in(mainwin->window, NULL, NULL);
1392 folderview = mainwin->folderview;
1394 gtk_cmclist_freeze(GTK_CMCLIST(mainwin->folderview->ctree));
1395 folder_item_update_freeze();
1397 /* register the callback of unix domain socket input */
1398 lock_socket_tag = claws_input_add(lock_socket,
1399 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
1400 lock_socket_input_cb,
1404 prefs_account_init();
1405 account_read_config_all();
1407 #ifdef HAVE_LIBETPAN
1408 imap_main_init(prefs_common.skip_ssl_cert_check);
1409 imap_main_set_timeout(prefs_common.io_timeout_secs);
1410 nntp_main_init(prefs_common.skip_ssl_cert_check);
1412 /* If we can't read a folder list or don't have accounts,
1413 * it means the configuration's not done. Either this is
1414 * a brand new install, either a failed/refused migration.
1415 * So we'll start the wizard.
1417 if (folder_read_list() < 0) {
1418 prefs_destroy_cache();
1420 /* if run_wizard returns FALSE it's because it's
1421 * been cancelled. We can't do much but exit.
1422 * however, if the user was asked for a migration,
1423 * we remove the newly created directory so that
1424 * he's asked again for migration on next launch.*/
1425 if (!run_wizard(mainwin, TRUE)) {
1426 if (asked_for_migration)
1427 remove_dir_recursive(RC_DIR);
1433 main_window_reflect_prefs_all_now();
1434 folder_write_list();
1438 if (!account_get_list()) {
1439 prefs_destroy_cache();
1440 if (!run_wizard(mainwin, FALSE)) {
1441 if (asked_for_migration)
1442 remove_dir_recursive(RC_DIR);
1448 if(!account_get_list()) {
1449 exit_claws(mainwin);
1459 toolbar_main_set_sensitive(mainwin);
1460 main_window_set_menu_sensitive(mainwin);
1462 /* if crashed, show window early so that the user
1463 * sees what's happening */
1464 if (claws_crashed())
1465 main_window_popup(mainwin);
1467 account_set_missing_folder();
1468 folder_set_missing_folders();
1469 folderview_set(folderview);
1471 prefs_matcher_read_config();
1472 quicksearch_set_search_strings(mainwin->summaryview->quicksearch);
1474 /* make one all-folder processing before using claws */
1475 main_window_cursor_wait(mainwin);
1476 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
1478 /* if claws crashed, rebuild caches */
1479 if (claws_crashed()) {
1481 debug_print("Claws Mail crashed, checking for new messages in local folders\n");
1482 folder_item_update_thaw();
1483 folderview_check_new(NULL);
1484 folder_clean_cache_memory_force();
1485 folder_item_update_freeze();
1487 /* make the crash-indicator file */
1488 str_write_to_file("foo", get_crashfile_name());
1490 inc_autocheck_timer_init(mainwin);
1492 /* ignore SIGPIPE signal for preventing sudden death of program */
1494 signal(SIGPIPE, SIG_IGN);
1496 if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
1497 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1499 if (cmd.online_mode == ONLINE_MODE_ONLINE) {
1500 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1503 if (cmd.status_folders) {
1504 g_ptr_array_free(cmd.status_folders, TRUE);
1505 cmd.status_folders = NULL;
1507 if (cmd.status_full_folders) {
1508 g_ptr_array_free(cmd.status_full_folders, TRUE);
1509 cmd.status_full_folders = NULL;
1512 claws_register_idle_function(claws_gtk_idle);
1514 prefs_toolbar_init();
1516 num_folder_class = g_list_length(folder_get_list());
1518 plugin_load_all("GTK2");
1520 if (g_list_length(folder_get_list()) != num_folder_class) {
1521 debug_print("new folders loaded, reloading processing rules\n");
1522 prefs_matcher_read_config();
1525 if ((plug_list = plugin_get_unloaded_list()) != NULL) {
1528 gint num_plugins = 0;
1529 for (cur = plug_list; cur; cur = cur->next) {
1530 Plugin *plugin = (Plugin *)cur->data;
1531 gchar *tmp = g_strdup_printf("%s\n%s",
1533 plugin_get_name(plugin));
1538 main_window_cursor_normal(mainwin);
1539 alertpanel_warning(ngettext(
1540 "The following plugin failed to load. "
1541 "Check the Plugins configuration "
1542 "for more information:\n%s",
1543 "The following plugins failed to load. "
1544 "Check the Plugins configuration "
1545 "for more information:\n%s",
1548 main_window_cursor_wait(mainwin);
1550 g_slist_free(plug_list);
1554 prefs_common_write_config();
1555 plugin_load_standard_plugins ();
1557 /* if not crashed, show window now */
1558 if (!claws_crashed()) {
1559 /* apart if something told not to show */
1560 if (show_at_startup)
1561 main_window_popup(mainwin);
1564 if (!folder_have_mailbox()) {
1565 prefs_destroy_cache();
1566 main_window_cursor_normal(mainwin);
1567 if (folder_get_list() != NULL) {
1568 alertpanel_error(_("Claws Mail has detected a configured "
1569 "mailbox, but it is incomplete. It is "
1570 "possibly due to a failing IMAP account. Use "
1571 "\"Rebuild folder tree\" on the mailbox parent "
1572 "folder's context menu to try to fix it."));
1574 alertpanel_error(_("Claws Mail has detected a configured "
1575 "mailbox, but could not load it. It is "
1576 "probably provided by an out-of-date "
1577 "external plugin. Please reinstall the "
1578 "plugin and try again."));
1579 exit_claws(mainwin);
1587 static_mainwindow = mainwin;
1590 if (prefs_common.data_root != NULL && *prefs_common.data_root != '\0') {
1591 GnomeVFSVolume *vol = NULL;
1592 gchar *uri, *mount_path;
1594 volmon = gnome_vfs_get_volume_monitor();
1595 vol = gnome_vfs_volume_monitor_get_volume_for_path(volmon, prefs_common.data_root);
1597 uri = gnome_vfs_volume_get_activation_uri (vol);
1598 mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
1601 if (vol == NULL || !gnome_vfs_volume_is_mounted(vol)
1602 || strcmp(mount_path, prefs_common.data_root)) {
1603 alertpanel_error(_("Claws Mail can not start without its data volume (%s)."),
1604 prefs_common.data_root);
1606 gnome_vfs_volume_unref(vol);
1607 exit_claws(mainwin);
1611 gnome_vfs_volume_unref(vol);
1612 g_signal_connect(G_OBJECT(volmon),
1613 "volume-mounted", G_CALLBACK(main_vol_mount_cb), mainwin);
1614 g_signal_connect(G_OBJECT(volmon),
1615 "volume-unmounted", G_CALLBACK(main_vol_unmount_cb), mainwin);
1619 #ifdef HAVE_STARTUP_NOTIFICATION
1620 startup_notification_complete(FALSE);
1623 sc_session_manager_connect(mainwin);
1626 folder_item_update_thaw();
1627 gtk_cmclist_thaw(GTK_CMCLIST(mainwin->folderview->ctree));
1628 main_window_cursor_normal(mainwin);
1630 if (!cmd.target && prefs_common.goto_last_folder_on_startup &&
1631 folder_find_item_from_identifier(prefs_common.last_opened_folder) != NULL &&
1633 cmd.target = prefs_common.last_opened_folder;
1636 if (cmd.receive_all && !cmd.target) {
1638 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(FALSE));
1639 } else if (prefs_common.chk_on_startup && !cmd.target) {
1641 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(TRUE));
1642 } else if (cmd.receive && !cmd.target) {
1644 g_timeout_add(1000, defer_check, NULL);
1646 gtk_widget_grab_focus(folderview->ctree);
1649 open_compose_new(cmd.compose_mailto, cmd.attach_files);
1651 if (cmd.attach_files) {
1652 ptr_array_free_strings(cmd.attach_files);
1653 g_ptr_array_free(cmd.attach_files, TRUE);
1654 cmd.attach_files = NULL;
1656 if (cmd.subscribe) {
1657 folder_subscribe(cmd.subscribe_uri);
1666 g_timeout_add(500, defer_jump, (gpointer)cmd.target);
1669 prefs_destroy_cache();
1671 compose_reopen_exit_drafts();
1674 sc_starting = FALSE;
1675 main_window_set_menu_sensitive(mainwin);
1676 toolbar_main_set_sensitive(mainwin);
1683 osso_deinitialize(osso_context);
1685 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1687 g_object_unref(nm_proxy);
1689 #ifdef HAVE_DBUS_GLIB
1690 uninstall_dbus_status_handler();
1692 dbus_g_connection_unref(connection);
1698 exit_claws(mainwin);
1703 static void save_all_caches(FolderItem *item, gpointer data)
1710 folder_item_close(item);
1713 folder_item_free_cache(item, TRUE);
1716 static void exit_claws(MainWindow *mainwin)
1719 gboolean have_connectivity;
1723 debug_print("shutting down\n");
1724 inc_autocheck_timer_remove();
1726 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1727 if (prefs_common.work_offline && went_offline_nm)
1728 prefs_common.work_offline = FALSE;
1731 /* save prefs for opened folder */
1732 if(mainwin->folderview->opened) {
1735 item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
1736 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
1737 prefs_common.last_opened_folder = folder_item_get_identifier(item);
1740 /* save all state before exiting */
1741 folder_func_to_all_folders(save_all_caches, NULL);
1742 folder_write_list();
1744 main_window_get_size(mainwin);
1745 main_window_get_position(mainwin);
1747 prefs_common_write_config();
1748 account_write_config_all();
1749 addressbook_export_to_file();
1751 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1752 gtk_accel_map_save(filename);
1755 /* delete temporary files */
1756 remove_all_files(get_tmp_dir());
1757 remove_all_files(get_mime_tmp_dir());
1759 close_log_file(LOG_PROTOCOL);
1760 close_log_file(LOG_DEBUG_FILTERING);
1762 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1763 have_connectivity = networkmanager_is_online(NULL);
1765 have_connectivity = TRUE;
1767 #ifdef HAVE_LIBETPAN
1768 imap_main_done(have_connectivity);
1769 nntp_main_done(have_connectivity);
1771 /* delete crashfile */
1773 claws_unlink(get_crashfile_name());
1775 lock_socket_remove();
1778 if (mainwin->smc_conn)
1779 SmcCloseConnection ((SmcConn)mainwin->smc_conn, 0, NULL);
1780 mainwin->smc_conn = NULL;
1783 main_window_destroy_all();
1785 plugin_unload_all("GTK2");
1787 prefs_toolbar_done();
1789 addressbook_destroy();
1791 prefs_themes_done();
1793 prefs_ext_prog_done();
1794 prefs_wrapping_done();
1795 prefs_compose_writing_done();
1796 prefs_msg_colors_done();
1797 prefs_image_viewer_done();
1798 image_viewer_done();
1800 prefs_summaries_done();
1801 prefs_message_done();
1803 prefs_receive_done();
1804 prefs_logging_done();
1808 prefs_spelling_done();
1809 gtkaspell_checkers_quit();
1811 plugin_unload_all("Common");
1815 static void parse_cmd_opt(int argc, char *argv[])
1819 for (i = 1; i < argc; i++) {
1820 if (!strncmp(argv[i], "--receive-all", 13)) {
1821 cmd.receive_all = TRUE;
1822 } else if (!strncmp(argv[i], "--receive", 9)) {
1824 } else if (!strncmp(argv[i], "--compose", 9)) {
1825 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1828 cmd.compose_mailto = NULL;
1829 if (p && *p != '\0' && *p != '-') {
1830 if (!strncmp(p, "mailto:", 7)) {
1831 cmd.compose_mailto = p + 7;
1833 cmd.compose_mailto = p;
1837 } else if (!strncmp(argv[i], "--subscribe", 11)) {
1838 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1839 if (p && *p != '\0' && *p != '-') {
1840 cmd.subscribe = TRUE;
1841 cmd.subscribe_uri = p;
1843 } else if (!strncmp(argv[i], "--attach", 8)) {
1844 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1847 while (p && *p != '\0' && *p != '-') {
1848 if (!cmd.attach_files) {
1849 cmd.attach_files = g_ptr_array_new();
1851 if ((file = g_filename_from_uri(p, NULL, NULL)) != NULL) {
1852 if (!is_file_exist(file)) {
1857 if (file == NULL && *p != G_DIR_SEPARATOR) {
1858 file = g_strconcat(claws_get_startup_dir(),
1861 } else if (file == NULL) {
1864 g_ptr_array_add(cmd.attach_files, file);
1866 p = (i+1 < argc)?argv[i+1]:NULL;
1868 } else if (!strncmp(argv[i], "--send", 6)) {
1870 } else if (!strncmp(argv[i], "--version-full", 14) ||
1871 !strncmp(argv[i], "-V", 2)) {
1872 g_print("Claws Mail version " VERSION "\n");
1873 main_dump_features_list(FALSE);
1875 } else if (!strncmp(argv[i], "--version", 9) ||
1876 !strncmp(argv[i], "-v", 2)) {
1877 g_print("Claws Mail version " VERSION "\n");
1879 } else if (!strncmp(argv[i], "--status-full", 13)) {
1880 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1882 cmd.status_full = TRUE;
1883 while (p && *p != '\0' && *p != '-') {
1884 if (!cmd.status_full_folders) {
1885 cmd.status_full_folders =
1888 g_ptr_array_add(cmd.status_full_folders,
1891 p = (i+1 < argc)?argv[i+1]:NULL;
1893 } else if (!strncmp(argv[i], "--status", 8)) {
1894 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1897 while (p && *p != '\0' && *p != '-') {
1898 if (!cmd.status_folders)
1899 cmd.status_folders = g_ptr_array_new();
1900 g_ptr_array_add(cmd.status_folders,
1903 p = (i+1 < argc)?argv[i+1]:NULL;
1905 } else if (!strncmp(argv[i], "--search", 8)) {
1906 cmd.search_folder = (i+1 < argc)?argv[i+1]:NULL;
1907 cmd.search_type = (i+2 < argc)?argv[i+2]:NULL;
1908 cmd.search_request = (i+3 < argc)?argv[i+3]:NULL;
1909 const char* rec = (i+4 < argc)?argv[i+4]:NULL;
1910 cmd.search_recursive = TRUE;
1911 if (rec && (tolower(*rec)=='n' || tolower(*rec)=='f' || *rec=='0'))
1912 cmd.search_recursive = FALSE;
1913 if (cmd.search_folder && cmd.search_type && cmd.search_request)
1915 } else if (!strncmp(argv[i], "--online", 8)) {
1916 cmd.online_mode = ONLINE_MODE_ONLINE;
1917 } else if (!strncmp(argv[i], "--offline", 9)) {
1918 cmd.online_mode = ONLINE_MODE_OFFLINE;
1919 } else if (!strncmp(argv[i], "--help", 6) ||
1920 !strncmp(argv[i], "-h", 2)) {
1921 gchar *base = g_path_get_basename(argv[0]);
1922 g_print(_("Usage: %s [OPTION]...\n"), base);
1924 g_print("%s\n", _(" --compose [address] open composition window"));
1925 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
1926 g_print("%s\n", _(" --attach file1 [file2]...\n"
1927 " open composition window with specified files\n"
1929 g_print("%s\n", _(" --receive receive new messages"));
1930 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
1931 g_print("%s\n", _(" --search folder type request [recursive]"));
1932 g_print("%s\n", _(" searches mail"));
1933 g_print("%s\n", _(" folder ex.: \"#mh/Mailbox/inbox\" or \"Mail\""));
1934 g_print("%s\n", _(" type: s[ubject],f[rom],t[o],e[xtended],m[ixed] or g: tag"));
1935 g_print("%s\n", _(" request: search string"));
1936 g_print("%s\n", _(" recursive: false if arg. starts with 0, n, N, f or F"));
1938 g_print("%s\n", _(" --send send all queued messages"));
1939 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
1940 g_print("%s\n", _(" --status-full [folder]...\n"
1941 " show the status of each folder"));
1942 g_print("%s\n", _(" --select folder[/msg] jumps to the specified folder/message\n"
1943 " folder is a folder id like 'folder/sub_folder'"));
1944 g_print("%s\n", _(" --online switch to online mode"));
1945 g_print("%s\n", _(" --offline switch to offline mode"));
1946 g_print("%s\n", _(" --exit --quit -q exit Claws Mail"));
1947 g_print("%s\n", _(" --debug debug mode"));
1948 g_print("%s\n", _(" --help -h display this help and exit"));
1949 g_print("%s\n", _(" --version -v output version information and exit"));
1950 g_print("%s\n", _(" --version-full -V output version and built-in features information and exit"));
1951 g_print("%s\n", _(" --config-dir output configuration directory"));
1952 g_print("%s\n", _(" --alternate-config-dir [dir]\n"
1953 " use specified configuration directory"));
1957 } else if (!strncmp(argv[i], "--crash", 7)) {
1959 cmd.crash_params = g_strdup((i+1 < argc)?argv[i+1]:NULL);
1961 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
1962 g_print(RC_DIR "\n");
1964 } else if (!strncmp(argv[i], "--alternate-config-dir", sizeof "--alternate-config-dir" - 1) && i+1 < argc) {
1965 set_rc_dir(argv[i+1]);
1966 } else if (!strncmp(argv[i], "--exit", 6) ||
1967 !strncmp(argv[i], "--quit", 6) ||
1968 !strncmp(argv[i], "-q", 2)) {
1970 } else if (!strncmp(argv[i], "--select", 8) && i+1 < argc) {
1971 cmd.target = argv[i+1];
1972 } else if (i == 1 && argc == 2) {
1973 /* only one parameter. Do something intelligent about it */
1974 if ((strstr(argv[i], "@")||!strncmp(argv[i], "mailto:", 7)) && !strstr(argv[i], "://")) {
1975 const gchar *p = argv[i];
1978 cmd.compose_mailto = NULL;
1979 if (p && *p != '\0' && *p != '-') {
1980 if (!strncmp(p, "mailto:", 7)) {
1981 cmd.compose_mailto = p + 7;
1983 cmd.compose_mailto = p;
1986 } else if (!strncmp(argv[i], "file://", 7)) {
1987 cmd.target = argv[i];
1988 } else if (!strncmp(argv[i], "?attach=file://", strlen("?attach=file://"))) {
1990 cmd.compose_mailto = argv[i];
1991 } else if (strstr(argv[i], "://")) {
1992 const gchar *p = argv[i];
1993 if (p && *p != '\0' && *p != '-') {
1994 cmd.subscribe = TRUE;
1995 cmd.subscribe_uri = p;
1997 } else if (!strcmp(argv[i], "--sync")) {
1999 } else if (is_dir_exist(argv[i]) || is_file_exist(argv[i])) {
2000 cmd.target = argv[i];
2002 g_print(_("Unknown option\n"));
2008 if (cmd.attach_files && cmd.compose == FALSE) {
2010 cmd.compose_mailto = NULL;
2014 static void initial_processing(FolderItem *item, gpointer data)
2016 MainWindow *mainwin = (MainWindow *)data;
2019 cm_return_if_fail(item);
2020 buf = g_strdup_printf(_("Processing (%s)..."),
2023 : _("top level folder"));
2027 if (item->prefs->enable_processing) {
2028 item->processing_pending = TRUE;
2029 folder_item_apply_processing(item);
2030 item->processing_pending = FALSE;
2033 STATUSBAR_POP(mainwin);
2036 static gboolean draft_all_messages(void)
2038 GList *compose_list = NULL;
2040 compose_clear_exit_drafts();
2041 compose_list = compose_get_compose_list();
2042 while (compose_list != NULL) {
2043 Compose *c = (Compose*)compose_list->data;
2044 if (!compose_draft(c, COMPOSE_DRAFT_FOR_EXIT))
2046 compose_list = compose_get_compose_list();
2050 gboolean clean_quit(gpointer data)
2052 static gboolean firstrun = TRUE;
2059 /*!< Good idea to have the main window stored in a
2060 * static variable so we can check that variable
2061 * to see if we're really allowed to do things
2062 * that actually the spawner is supposed to
2063 * do (like: sending mail, composing messages).
2064 * Because, really, if we're the spawnee, and
2065 * we touch GTK stuff, we're hosed. See the
2068 /* FIXME: Use something else to signal that we're
2069 * in the original spawner, and not in a spawned
2071 if (!static_mainwindow) {
2075 draft_all_messages();
2076 emergency_exit = TRUE;
2077 exit_claws(static_mainwindow);
2083 void app_will_exit(GtkWidget *widget, gpointer data)
2085 MainWindow *mainwin = data;
2087 if (gtk_main_level() == 0) {
2088 debug_print("not even started\n");
2091 if (sc_exiting == TRUE) {
2092 debug_print("exit pending\n");
2096 debug_print("exiting\n");
2097 if (compose_get_compose_list()) {
2098 if (!draft_all_messages()) {
2099 main_window_popup(mainwin);
2105 if (prefs_common.warn_queued_on_exit && procmsg_have_queued_mails_fast()) {
2106 if (alertpanel(_("Queued messages"),
2107 _("Some unsent messages are queued. Exit now?"),
2108 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL)
2109 != G_ALERTALTERNATE) {
2110 main_window_popup(mainwin);
2114 manage_window_focus_in(mainwin->window, NULL, NULL);
2118 #ifdef HAVE_VALGRIND
2119 if (RUNNING_ON_VALGRIND) {
2120 summary_clear_list(mainwin->summaryview);
2123 if (folderview_get_selected_item(mainwin->folderview))
2124 folder_item_close(folderview_get_selected_item(mainwin->folderview));
2128 gboolean claws_is_exiting(void)
2133 gboolean claws_is_starting(void)
2139 * CLAWS: want this public so crash dialog can delete the
2142 gchar *claws_get_socket_name(void)
2144 static gchar *filename = NULL;
2145 const gchar *socket_dir = NULL;
2147 if (rc_dir_is_alt())
2148 socket_dir = get_rc_dir();
2150 socket_dir = g_get_tmp_dir();
2151 if (filename == NULL) {
2152 filename = g_strdup_printf("%s%cclaws-mail-%d",
2153 socket_dir, G_DIR_SEPARATOR,
2164 static gchar *get_crashfile_name(void)
2166 static gchar *filename = NULL;
2168 if (filename == NULL) {
2169 filename = g_strdup_printf("%s%cclaws-crashed",
2170 get_tmp_dir(), G_DIR_SEPARATOR);
2176 static gint prohibit_duplicate_launch(void)
2182 path = claws_get_socket_name();
2183 uxsock = fd_connect_unix(path);
2185 if (x_display == NULL)
2186 x_display = g_strdup(g_getenv("DISPLAY"));
2190 return fd_open_unix(path);
2195 hmutex = CreateMutexA(NULL, FALSE, "ClawsMail");
2197 debug_print("cannot create Mutex\n");
2200 if (GetLastError() != ERROR_ALREADY_EXISTS) {
2201 uxsock = fd_open_inet(50216);
2207 uxsock = fd_connect_inet(50216);
2211 /* remote command mode */
2213 debug_print("another Claws Mail instance is already running.\n");
2215 if (cmd.receive_all) {
2216 fd_write_all(uxsock, "receive_all\n", 12);
2217 } else if (cmd.receive) {
2218 fd_write_all(uxsock, "receive\n", 8);
2219 } else if (cmd.compose && cmd.attach_files) {
2220 gchar *str, *compose_str;
2223 if (cmd.compose_mailto) {
2224 compose_str = g_strdup_printf("compose_attach %s\n",
2225 cmd.compose_mailto);
2227 compose_str = g_strdup("compose_attach\n");
2230 fd_write_all(uxsock, compose_str, strlen(compose_str));
2231 g_free(compose_str);
2233 for (i = 0; i < cmd.attach_files->len; i++) {
2234 str = g_ptr_array_index(cmd.attach_files, i);
2235 fd_write_all(uxsock, str, strlen(str));
2236 fd_write_all(uxsock, "\n", 1);
2239 fd_write_all(uxsock, ".\n", 2);
2240 } else if (cmd.compose) {
2243 if (cmd.compose_mailto) {
2244 compose_str = g_strdup_printf
2245 ("compose %s\n", cmd.compose_mailto);
2247 compose_str = g_strdup("compose\n");
2250 fd_write_all(uxsock, compose_str, strlen(compose_str));
2251 g_free(compose_str);
2252 } else if (cmd.subscribe) {
2253 gchar *str = g_strdup_printf("subscribe %s\n", cmd.subscribe_uri);
2254 fd_write_all(uxsock, str, strlen(str));
2256 } else if (cmd.send) {
2257 fd_write_all(uxsock, "send\n", 5);
2258 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
2259 fd_write(uxsock, "online\n", 6);
2260 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
2261 fd_write(uxsock, "offline\n", 7);
2262 } else if (cmd.status || cmd.status_full) {
2263 gchar buf[BUFFSIZE];
2265 const gchar *command;
2269 command = cmd.status_full ? "status-full\n" : "status\n";
2270 folders = cmd.status_full ? cmd.status_full_folders :
2273 fd_write_all(uxsock, command, strlen(command));
2274 for (i = 0; folders && i < folders->len; ++i) {
2275 folder = g_ptr_array_index(folders, i);
2276 fd_write_all(uxsock, folder, strlen(folder));
2277 fd_write_all(uxsock, "\n", 1);
2279 fd_write_all(uxsock, ".\n", 2);
2281 fd_gets(uxsock, buf, sizeof(buf));
2282 if (!strncmp(buf, ".\n", 2)) break;
2285 } else if (cmd.exit) {
2286 fd_write_all(uxsock, "exit\n", 5);
2287 } else if (cmd.target) {
2288 gchar *str = g_strdup_printf("select %s\n", cmd.target);
2289 fd_write_all(uxsock, str, strlen(str));
2291 } else if (cmd.search) {
2292 gchar buf[BUFFSIZE];
2294 g_strdup_printf("search %s\n%s\n%s\n%c\n",
2295 cmd.search_folder, cmd.search_type, cmd.search_request,
2296 (cmd.search_recursive==TRUE)?'1':'0');
2297 fd_write_all(uxsock, str, strlen(str));
2300 fd_gets(uxsock, buf, sizeof(buf));
2301 if (!strncmp(buf, ".\n", 2)) break;
2307 fd_write_all(uxsock, "get_display\n", 12);
2308 memset(buf, 0, sizeof(buf));
2309 fd_gets(uxsock, buf, sizeof(buf));
2310 if (strcmp2(buf, x_display)) {
2311 g_print("Claws Mail is already running on display %s.\n",
2315 uxsock = fd_connect_unix(path);
2316 fd_write_all(uxsock, "popup\n", 6);
2319 fd_write_all(uxsock, "popup\n", 6);
2327 static gint lock_socket_remove(void)
2332 if (lock_socket < 0) {
2336 if (lock_socket_tag > 0) {
2337 g_source_remove(lock_socket_tag);
2339 fd_close(lock_socket);
2342 filename = claws_get_socket_name();
2343 claws_unlink(filename);
2349 static GPtrArray *get_folder_item_list(gint sock)
2351 gchar buf[BUFFSIZE];
2353 GPtrArray *folders = NULL;
2356 fd_gets(sock, buf, sizeof(buf));
2357 if (!strncmp(buf, ".\n", 2)) {
2362 folders = g_ptr_array_new();
2364 item = folder_find_item_from_identifier(buf);
2366 g_ptr_array_add(folders, item);
2368 g_warning("no such folder: %s\n", buf);
2375 static void lock_socket_input_cb(gpointer data,
2377 GdkInputCondition condition)
2379 MainWindow *mainwin = (MainWindow *)data;
2381 gchar buf[BUFFSIZE];
2382 /* re-use the same quicksearch (& avoid matcher_list mem.leaks) */
2383 static QuickSearch *quicksearch = NULL;
2385 sock = fd_accept(source);
2386 fd_gets(sock, buf, sizeof(buf));
2388 if (!strncmp(buf, "popup", 5)) {
2389 main_window_popup(mainwin);
2390 } else if (!strncmp(buf, "get_display", 11)) {
2391 fd_write_all(sock, x_display, strlen(x_display));
2392 } else if (!strncmp(buf, "receive_all", 11)) {
2393 inc_all_account_mail(mainwin, FALSE,
2394 prefs_common.newmail_notify_manu);
2395 } else if (!strncmp(buf, "receive", 7)) {
2396 inc_mail(mainwin, prefs_common.newmail_notify_manu);
2397 } else if (!strncmp(buf, "compose_attach", 14)) {
2401 mailto = g_strdup(buf + strlen("compose_attach") + 1);
2402 files = g_ptr_array_new();
2403 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
2405 if (!strcmp2(buf, "."))
2407 g_ptr_array_add(files, g_strdup(buf));
2409 open_compose_new(mailto, files);
2410 ptr_array_free_strings(files);
2411 g_ptr_array_free(files, TRUE);
2413 } else if (!strncmp(buf, "compose", 7)) {
2414 open_compose_new(buf + strlen("compose") + 1, NULL);
2415 } else if (!strncmp(buf, "subscribe", 9)) {
2416 main_window_popup(mainwin);
2417 folder_subscribe(buf + strlen("subscribe") + 1);
2418 } else if (!strncmp(buf, "send", 4)) {
2420 } else if (!strncmp(buf, "online", 6)) {
2421 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
2422 } else if (!strncmp(buf, "offline", 7)) {
2423 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
2424 } else if (!strncmp(buf, "status-full", 11) ||
2425 !strncmp(buf, "status", 6)) {
2429 folders = get_folder_item_list(sock);
2430 status = folder_get_status
2431 (folders, !strncmp(buf, "status-full", 11));
2432 fd_write_all(sock, status, strlen(status));
2433 fd_write_all(sock, ".\n", 2);
2435 if (folders) g_ptr_array_free(folders, TRUE);
2436 } else if (!strncmp(buf, "select ", 7)) {
2437 const gchar *target = buf+7;
2438 mainwindow_jump_to(target, TRUE);
2439 } else if (!strncmp(buf, "search ", 7)) {
2440 FolderItem* folderItem = NULL;
2441 GSList *messages = NULL;
2442 gchar *folder_name, *request;
2443 QuickSearchType searchType = QUICK_SEARCH_EXTENDED;
2446 if (quicksearch==NULL)
2447 quicksearch = quicksearch_new_nogui();
2449 folder_name = g_strdup(buf+7);
2450 strretchomp(folder_name);
2452 if (fd_gets(sock, buf, sizeof(buf)) <= 0) {
2453 g_free(folder_name);
2456 searchType = quicksearch_type(buf);
2457 if (fd_gets(sock, buf, sizeof(buf)) <= 0) {
2458 g_free(folder_name);
2461 request = g_strdup(buf);
2462 strretchomp(request);
2465 if (fd_gets(sock, buf, sizeof(buf)) > 0)
2469 debug_print("search: %s %i %s %i\n",folder_name,searchType,request,recursive);
2472 folderItem = folder_find_item_from_identifier(folder_name);
2473 if (folder_name && folderItem == NULL) {
2474 debug_print("Unknow folder item : '%s', searching folder\n",folder_name);
2475 Folder* folder = folder_find_from_path(folder_name);
2477 folderItem = FOLDER_ITEM(folder->node->data);
2479 debug_print("Unknown folder: '%s'\n",folder_name);
2481 debug_print("%s %s\n",folderItem->name, folderItem->path);
2483 if (folderItem != NULL) {
2484 quicksearch_set(quicksearch, searchType, request);
2485 quicksearch_set_recursive(quicksearch, recursive);
2486 search_msgs_in_folders(&messages, quicksearch, folderItem);
2488 g_print("Folder '%s' not found.\n'", folder_name);
2492 for (cur=messages; cur != NULL; cur = cur->next) {
2493 MsgInfo* msg = (MsgInfo *)cur->data;
2494 gchar *file = procmsg_get_message_file_path(msg);
2495 fd_write_all(sock, file, strlen(file));
2496 fd_write_all(sock, "\n", 1);
2499 fd_write_all(sock, ".\n", 2);
2501 if (messages != NULL)
2502 procmsg_msg_list_free(messages);
2503 g_free(folder_name);
2505 } else if (!strncmp(buf, "exit", 4)) {
2506 if (prefs_common.clean_on_exit && !prefs_common.ask_on_clean) {
2507 procmsg_empty_all_trash();
2509 app_will_exit(NULL, mainwin);
2515 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
2520 Xstrdup_a(addr, address, return);
2524 compose_new(NULL, addr, attach_files);
2527 static void send_queue(void)
2530 gchar *errstr = NULL;
2531 gboolean error = FALSE;
2532 for (list = folder_get_list(); list != NULL; list = list->next) {
2533 Folder *folder = list->data;
2535 if (folder->queue) {
2536 gint res = procmsg_send_queue
2537 (folder->queue, prefs_common.savemsg,
2541 folder_item_scan(folder->queue);
2549 alertpanel_error_log(_("Some errors occurred "
2550 "while sending queued messages:\n%s"), errstr);
2553 alertpanel_error_log("Some errors occurred "
2554 "while sending queued messages.");
2558 static void quit_signal_handler(int sig)
2560 debug_print("Quitting on signal %d\n", sig);
2562 g_timeout_add(0, clean_quit, NULL);
2565 static void install_basic_sighandlers()
2569 struct sigaction act;
2574 sigaddset(&mask, SIGTERM);
2577 sigaddset(&mask, SIGINT);
2580 sigaddset(&mask, SIGHUP);
2583 act.sa_handler = quit_signal_handler;
2588 sigaction(SIGTERM, &act, 0);
2591 sigaction(SIGINT, &act, 0);
2594 sigaction(SIGHUP, &act, 0);
2597 sigprocmask(SIG_UNBLOCK, &mask, 0);
2598 #endif /* !G_OS_WIN32 */
2601 #if (defined linux && defined SIGIO)
2602 static int mem_notify_fd = 0;
2604 static gboolean clean_caches(gpointer unused)
2606 if (static_mainwindow && static_mainwindow->lock_count > 0)
2608 debug_print("/dev/mem_notify: callback: Freeing some memory now!\n");
2609 folder_clean_cache_memory_force();
2613 static void memory_signal_handler(int sig)
2615 debug_print("/dev/mem_notify: Kernel says we should free up some memory!\n");
2616 g_timeout_add(10, clean_caches, NULL);
2619 static void install_memory_sighandler()
2622 struct sigaction act;
2625 mem_notify_fd = g_open("/dev/mem_notify", O_RDONLY|O_NONBLOCK, 0);
2626 if (mem_notify_fd == -1) {
2627 debug_print("/dev/mem_notify not available (%s)\n",
2632 fcntl(mem_notify_fd, F_SETOWN, getpid());
2633 flags = fcntl(mem_notify_fd, F_GETFL);
2634 fcntl(mem_notify_fd, flags|FASYNC);
2638 sigaddset(&mask, SIGIO);
2640 act.sa_handler = memory_signal_handler;
2644 sigaction(SIGIO, &act, 0);
2646 sigprocmask(SIG_UNBLOCK, &mask, 0);
2648 debug_print("/dev/mem_notify: installed handler\n");
2650 #endif /* linux && SIGIO */
2653 osso_context_t *get_osso_context(void)
2655 return static_osso_context;
2660 #ifdef HAVE_NETWORKMANAGER_SUPPORT
2661 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
2664 MainWindow *mainWin;
2667 if (static_mainwindow)
2668 mainWin = static_mainwindow;
2670 mainWin = (MainWindow*)data;
2672 if (!prefs_common.use_networkmanager)
2680 online = networkmanager_is_online(&error);
2682 if(online && went_offline_nm) {
2683 went_offline_nm = FALSE;
2684 main_window_toggle_work_offline(mainWin, FALSE, FALSE);
2685 debug_print("NetworkManager: Went online\n");
2686 log_message(LOG_PROTOCOL, _("NetworkManager: network is online.\n"));
2689 went_offline_nm = TRUE;
2690 main_window_toggle_work_offline(mainWin, TRUE, FALSE);
2691 debug_print("NetworkManager: Went offline\n");
2692 log_message(LOG_PROTOCOL, _("NetworkManager: network is offline.\n"));
2696 debug_print("Failed to get online information from NetworkManager: %s\n",
2698 g_error_free(error);
2702 debug_print("NetworkManager: Cannot change connection state because "
2703 "main window does not exist\n");
2706 /* Returns true (and sets error appropriately, if given) in case of error */
2707 gboolean networkmanager_is_online(GError **error)
2709 DBusGConnection *connection;
2711 GError *tmp_error = NULL;
2715 if (!prefs_common.use_networkmanager)
2720 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &tmp_error);
2723 /* If calling code doesn't do error checking, at least print some debug */
2724 if((error == NULL) || (*error == NULL))
2725 debug_print("Failed to open connection to system bus: %s\n",
2726 tmp_error->message);
2727 g_propagate_error(error, tmp_error);
2731 proxy = dbus_g_proxy_new_for_name(connection,
2732 "org.freedesktop.NetworkManager",
2733 "/org/freedesktop/NetworkManager",
2734 "org.freedesktop.NetworkManager");
2736 retVal = dbus_g_proxy_call(proxy,"state",&tmp_error, G_TYPE_INVALID,
2737 G_TYPE_UINT, &state, G_TYPE_INVALID);
2740 g_object_unref(proxy);
2742 dbus_g_connection_unref(connection);
2745 /* If calling code doesn't do error checking, at least print some debug */
2746 if((error == NULL) || (*error == NULL))
2747 debug_print("Failed to get state info from NetworkManager: %s\n",
2748 tmp_error->message);
2749 g_propagate_error(error, tmp_error);
2752 #if NM_CHECK_VERSION(0,8,992)
2753 return (state == NM_STATE_CONNECTED_LOCAL ||
2754 state == NM_STATE_CONNECTED_SITE ||
2755 state == NM_STATE_CONNECTED_GLOBAL ||
2756 state == NM_STATE_UNKNOWN);
2758 return (state == NM_STATE_CONNECTED ||
2759 state == NM_STATE_UNKNOWN);