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 main_window_popup(mainwin);
1540 alertpanel_warning(ngettext(
1541 "The following plugin failed to load. "
1542 "Check the Plugins configuration "
1543 "for more information:\n%s",
1544 "The following plugins failed to load. "
1545 "Check the Plugins configuration "
1546 "for more information:\n%s",
1549 main_window_cursor_wait(mainwin);
1551 g_slist_free(plug_list);
1555 prefs_common_write_config();
1556 plugin_load_standard_plugins ();
1558 /* if not crashed, show window now */
1559 if (!claws_crashed()) {
1560 /* apart if something told not to show */
1561 if (show_at_startup)
1562 main_window_popup(mainwin);
1565 if (!folder_have_mailbox()) {
1566 prefs_destroy_cache();
1567 main_window_cursor_normal(mainwin);
1568 if (folder_get_list() != NULL) {
1569 alertpanel_error(_("Claws Mail has detected a configured "
1570 "mailbox, but it is incomplete. It is "
1571 "possibly due to a failing IMAP account. Use "
1572 "\"Rebuild folder tree\" on the mailbox parent "
1573 "folder's context menu to try to fix it."));
1575 alertpanel_error(_("Claws Mail has detected a configured "
1576 "mailbox, but could not load it. It is "
1577 "probably provided by an out-of-date "
1578 "external plugin. Please reinstall the "
1579 "plugin and try again."));
1580 exit_claws(mainwin);
1588 static_mainwindow = mainwin;
1591 if (prefs_common.data_root != NULL && *prefs_common.data_root != '\0') {
1592 GnomeVFSVolume *vol = NULL;
1593 gchar *uri, *mount_path;
1595 volmon = gnome_vfs_get_volume_monitor();
1596 vol = gnome_vfs_volume_monitor_get_volume_for_path(volmon, prefs_common.data_root);
1598 uri = gnome_vfs_volume_get_activation_uri (vol);
1599 mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
1602 if (vol == NULL || !gnome_vfs_volume_is_mounted(vol)
1603 || strcmp(mount_path, prefs_common.data_root)) {
1604 alertpanel_error(_("Claws Mail can not start without its data volume (%s)."),
1605 prefs_common.data_root);
1607 gnome_vfs_volume_unref(vol);
1608 exit_claws(mainwin);
1612 gnome_vfs_volume_unref(vol);
1613 g_signal_connect(G_OBJECT(volmon),
1614 "volume-mounted", G_CALLBACK(main_vol_mount_cb), mainwin);
1615 g_signal_connect(G_OBJECT(volmon),
1616 "volume-unmounted", G_CALLBACK(main_vol_unmount_cb), mainwin);
1620 #ifdef HAVE_STARTUP_NOTIFICATION
1621 startup_notification_complete(FALSE);
1624 sc_session_manager_connect(mainwin);
1627 folder_item_update_thaw();
1628 gtk_cmclist_thaw(GTK_CMCLIST(mainwin->folderview->ctree));
1629 main_window_cursor_normal(mainwin);
1631 if (!cmd.target && prefs_common.goto_last_folder_on_startup &&
1632 folder_find_item_from_identifier(prefs_common.last_opened_folder) != NULL &&
1634 cmd.target = prefs_common.last_opened_folder;
1637 if (cmd.receive_all && !cmd.target) {
1639 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(FALSE));
1640 } else if (prefs_common.chk_on_startup && !cmd.target) {
1642 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(TRUE));
1643 } else if (cmd.receive && !cmd.target) {
1645 g_timeout_add(1000, defer_check, NULL);
1647 gtk_widget_grab_focus(folderview->ctree);
1650 open_compose_new(cmd.compose_mailto, cmd.attach_files);
1652 if (cmd.attach_files) {
1653 ptr_array_free_strings(cmd.attach_files);
1654 g_ptr_array_free(cmd.attach_files, TRUE);
1655 cmd.attach_files = NULL;
1657 if (cmd.subscribe) {
1658 folder_subscribe(cmd.subscribe_uri);
1667 g_timeout_add(500, defer_jump, (gpointer)cmd.target);
1670 prefs_destroy_cache();
1672 compose_reopen_exit_drafts();
1675 sc_starting = FALSE;
1676 main_window_set_menu_sensitive(mainwin);
1677 toolbar_main_set_sensitive(mainwin);
1684 osso_deinitialize(osso_context);
1686 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1688 g_object_unref(nm_proxy);
1690 #ifdef HAVE_DBUS_GLIB
1691 uninstall_dbus_status_handler();
1693 dbus_g_connection_unref(connection);
1699 exit_claws(mainwin);
1704 static void save_all_caches(FolderItem *item, gpointer data)
1711 folder_item_close(item);
1714 folder_item_free_cache(item, TRUE);
1717 static void exit_claws(MainWindow *mainwin)
1720 gboolean have_connectivity;
1724 debug_print("shutting down\n");
1725 inc_autocheck_timer_remove();
1727 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1728 if (prefs_common.work_offline && went_offline_nm)
1729 prefs_common.work_offline = FALSE;
1732 /* save prefs for opened folder */
1733 if(mainwin->folderview->opened) {
1736 item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
1737 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
1738 prefs_common.last_opened_folder = folder_item_get_identifier(item);
1741 /* save all state before exiting */
1742 folder_func_to_all_folders(save_all_caches, NULL);
1743 folder_write_list();
1745 main_window_get_size(mainwin);
1746 main_window_get_position(mainwin);
1748 prefs_common_write_config();
1749 account_write_config_all();
1750 addressbook_export_to_file();
1752 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1753 gtk_accel_map_save(filename);
1756 /* delete temporary files */
1757 remove_all_files(get_tmp_dir());
1758 remove_all_files(get_mime_tmp_dir());
1760 close_log_file(LOG_PROTOCOL);
1761 close_log_file(LOG_DEBUG_FILTERING);
1763 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1764 have_connectivity = networkmanager_is_online(NULL);
1766 have_connectivity = TRUE;
1768 #ifdef HAVE_LIBETPAN
1769 imap_main_done(have_connectivity);
1770 nntp_main_done(have_connectivity);
1772 /* delete crashfile */
1774 claws_unlink(get_crashfile_name());
1776 lock_socket_remove();
1779 if (mainwin->smc_conn)
1780 SmcCloseConnection ((SmcConn)mainwin->smc_conn, 0, NULL);
1781 mainwin->smc_conn = NULL;
1784 main_window_destroy_all();
1786 plugin_unload_all("GTK2");
1788 prefs_toolbar_done();
1790 addressbook_destroy();
1792 prefs_themes_done();
1794 prefs_ext_prog_done();
1795 prefs_wrapping_done();
1796 prefs_compose_writing_done();
1797 prefs_msg_colors_done();
1798 prefs_image_viewer_done();
1799 image_viewer_done();
1801 prefs_summaries_done();
1802 prefs_message_done();
1804 prefs_receive_done();
1805 prefs_logging_done();
1809 prefs_spelling_done();
1810 gtkaspell_checkers_quit();
1812 plugin_unload_all("Common");
1816 static void parse_cmd_opt(int argc, char *argv[])
1820 for (i = 1; i < argc; i++) {
1821 if (!strncmp(argv[i], "--receive-all", 13)) {
1822 cmd.receive_all = TRUE;
1823 } else if (!strncmp(argv[i], "--receive", 9)) {
1825 } else if (!strncmp(argv[i], "--compose", 9)) {
1826 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1829 cmd.compose_mailto = NULL;
1830 if (p && *p != '\0' && *p != '-') {
1831 if (!strncmp(p, "mailto:", 7)) {
1832 cmd.compose_mailto = p + 7;
1834 cmd.compose_mailto = p;
1838 } else if (!strncmp(argv[i], "--subscribe", 11)) {
1839 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1840 if (p && *p != '\0' && *p != '-') {
1841 cmd.subscribe = TRUE;
1842 cmd.subscribe_uri = p;
1844 } else if (!strncmp(argv[i], "--attach", 8)) {
1845 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1848 while (p && *p != '\0' && *p != '-') {
1849 if (!cmd.attach_files) {
1850 cmd.attach_files = g_ptr_array_new();
1852 if ((file = g_filename_from_uri(p, NULL, NULL)) != NULL) {
1853 if (!is_file_exist(file)) {
1858 if (file == NULL && *p != G_DIR_SEPARATOR) {
1859 file = g_strconcat(claws_get_startup_dir(),
1862 } else if (file == NULL) {
1865 g_ptr_array_add(cmd.attach_files, file);
1867 p = (i+1 < argc)?argv[i+1]:NULL;
1869 } else if (!strncmp(argv[i], "--send", 6)) {
1871 } else if (!strncmp(argv[i], "--version-full", 14) ||
1872 !strncmp(argv[i], "-V", 2)) {
1873 g_print("Claws Mail version " VERSION "\n");
1874 main_dump_features_list(FALSE);
1876 } else if (!strncmp(argv[i], "--version", 9) ||
1877 !strncmp(argv[i], "-v", 2)) {
1878 g_print("Claws Mail version " VERSION "\n");
1880 } else if (!strncmp(argv[i], "--status-full", 13)) {
1881 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1883 cmd.status_full = TRUE;
1884 while (p && *p != '\0' && *p != '-') {
1885 if (!cmd.status_full_folders) {
1886 cmd.status_full_folders =
1889 g_ptr_array_add(cmd.status_full_folders,
1892 p = (i+1 < argc)?argv[i+1]:NULL;
1894 } else if (!strncmp(argv[i], "--status", 8)) {
1895 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1898 while (p && *p != '\0' && *p != '-') {
1899 if (!cmd.status_folders)
1900 cmd.status_folders = g_ptr_array_new();
1901 g_ptr_array_add(cmd.status_folders,
1904 p = (i+1 < argc)?argv[i+1]:NULL;
1906 } else if (!strncmp(argv[i], "--search", 8)) {
1907 cmd.search_folder = (i+1 < argc)?argv[i+1]:NULL;
1908 cmd.search_type = (i+2 < argc)?argv[i+2]:NULL;
1909 cmd.search_request = (i+3 < argc)?argv[i+3]:NULL;
1910 const char* rec = (i+4 < argc)?argv[i+4]:NULL;
1911 cmd.search_recursive = TRUE;
1912 if (rec && (tolower(*rec)=='n' || tolower(*rec)=='f' || *rec=='0'))
1913 cmd.search_recursive = FALSE;
1914 if (cmd.search_folder && cmd.search_type && cmd.search_request)
1916 } else if (!strncmp(argv[i], "--online", 8)) {
1917 cmd.online_mode = ONLINE_MODE_ONLINE;
1918 } else if (!strncmp(argv[i], "--offline", 9)) {
1919 cmd.online_mode = ONLINE_MODE_OFFLINE;
1920 } else if (!strncmp(argv[i], "--help", 6) ||
1921 !strncmp(argv[i], "-h", 2)) {
1922 gchar *base = g_path_get_basename(argv[0]);
1923 g_print(_("Usage: %s [OPTION]...\n"), base);
1925 g_print("%s\n", _(" --compose [address] open composition window"));
1926 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
1927 g_print("%s\n", _(" --attach file1 [file2]...\n"
1928 " open composition window with specified files\n"
1930 g_print("%s\n", _(" --receive receive new messages"));
1931 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
1932 g_print("%s\n", _(" --search folder type request [recursive]"));
1933 g_print("%s\n", _(" searches mail"));
1934 g_print("%s\n", _(" folder ex.: \"#mh/Mailbox/inbox\" or \"Mail\""));
1935 g_print("%s\n", _(" type: s[ubject],f[rom],t[o],e[xtended],m[ixed] or g: tag"));
1936 g_print("%s\n", _(" request: search string"));
1937 g_print("%s\n", _(" recursive: false if arg. starts with 0, n, N, f or F"));
1939 g_print("%s\n", _(" --send send all queued messages"));
1940 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
1941 g_print("%s\n", _(" --status-full [folder]...\n"
1942 " show the status of each folder"));
1943 g_print("%s\n", _(" --select folder[/msg] jumps to the specified folder/message\n"
1944 " folder is a folder id like 'folder/sub_folder'"));
1945 g_print("%s\n", _(" --online switch to online mode"));
1946 g_print("%s\n", _(" --offline switch to offline mode"));
1947 g_print("%s\n", _(" --exit --quit -q exit Claws Mail"));
1948 g_print("%s\n", _(" --debug debug mode"));
1949 g_print("%s\n", _(" --help -h display this help and exit"));
1950 g_print("%s\n", _(" --version -v output version information and exit"));
1951 g_print("%s\n", _(" --version-full -V output version and built-in features information and exit"));
1952 g_print("%s\n", _(" --config-dir output configuration directory"));
1953 g_print("%s\n", _(" --alternate-config-dir [dir]\n"
1954 " use specified configuration directory"));
1958 } else if (!strncmp(argv[i], "--crash", 7)) {
1960 cmd.crash_params = g_strdup((i+1 < argc)?argv[i+1]:NULL);
1962 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
1963 g_print(RC_DIR "\n");
1965 } else if (!strncmp(argv[i], "--alternate-config-dir", sizeof "--alternate-config-dir" - 1) && i+1 < argc) {
1966 set_rc_dir(argv[i+1]);
1967 } else if (!strncmp(argv[i], "--exit", 6) ||
1968 !strncmp(argv[i], "--quit", 6) ||
1969 !strncmp(argv[i], "-q", 2)) {
1971 } else if (!strncmp(argv[i], "--select", 8) && i+1 < argc) {
1972 cmd.target = argv[i+1];
1973 } else if (i == 1 && argc == 2) {
1974 /* only one parameter. Do something intelligent about it */
1975 if ((strstr(argv[i], "@")||!strncmp(argv[i], "mailto:", 7)) && !strstr(argv[i], "://")) {
1976 const gchar *p = argv[i];
1979 cmd.compose_mailto = NULL;
1980 if (p && *p != '\0' && *p != '-') {
1981 if (!strncmp(p, "mailto:", 7)) {
1982 cmd.compose_mailto = p + 7;
1984 cmd.compose_mailto = p;
1987 } else if (!strncmp(argv[i], "file://", 7)) {
1988 cmd.target = argv[i];
1989 } else if (!strncmp(argv[i], "?attach=file://", strlen("?attach=file://"))) {
1991 cmd.compose_mailto = argv[i];
1992 } else if (strstr(argv[i], "://")) {
1993 const gchar *p = argv[i];
1994 if (p && *p != '\0' && *p != '-') {
1995 cmd.subscribe = TRUE;
1996 cmd.subscribe_uri = p;
1998 } else if (!strcmp(argv[i], "--sync")) {
2000 } else if (is_dir_exist(argv[i]) || is_file_exist(argv[i])) {
2001 cmd.target = argv[i];
2003 g_print(_("Unknown option\n"));
2009 if (cmd.attach_files && cmd.compose == FALSE) {
2011 cmd.compose_mailto = NULL;
2015 static void initial_processing(FolderItem *item, gpointer data)
2017 MainWindow *mainwin = (MainWindow *)data;
2020 cm_return_if_fail(item);
2021 buf = g_strdup_printf(_("Processing (%s)..."),
2024 : _("top level folder"));
2028 if (item->prefs->enable_processing) {
2029 item->processing_pending = TRUE;
2030 folder_item_apply_processing(item);
2031 item->processing_pending = FALSE;
2034 STATUSBAR_POP(mainwin);
2037 static gboolean draft_all_messages(void)
2039 GList *compose_list = NULL;
2041 compose_clear_exit_drafts();
2042 compose_list = compose_get_compose_list();
2043 while (compose_list != NULL) {
2044 Compose *c = (Compose*)compose_list->data;
2045 if (!compose_draft(c, COMPOSE_DRAFT_FOR_EXIT))
2047 compose_list = compose_get_compose_list();
2051 gboolean clean_quit(gpointer data)
2053 static gboolean firstrun = TRUE;
2060 /*!< Good idea to have the main window stored in a
2061 * static variable so we can check that variable
2062 * to see if we're really allowed to do things
2063 * that actually the spawner is supposed to
2064 * do (like: sending mail, composing messages).
2065 * Because, really, if we're the spawnee, and
2066 * we touch GTK stuff, we're hosed. See the
2069 /* FIXME: Use something else to signal that we're
2070 * in the original spawner, and not in a spawned
2072 if (!static_mainwindow) {
2076 draft_all_messages();
2077 emergency_exit = TRUE;
2078 exit_claws(static_mainwindow);
2084 void app_will_exit(GtkWidget *widget, gpointer data)
2086 MainWindow *mainwin = data;
2088 if (gtk_main_level() == 0) {
2089 debug_print("not even started\n");
2092 if (sc_exiting == TRUE) {
2093 debug_print("exit pending\n");
2097 debug_print("exiting\n");
2098 if (compose_get_compose_list()) {
2099 if (!draft_all_messages()) {
2100 main_window_popup(mainwin);
2106 if (prefs_common.warn_queued_on_exit && procmsg_have_queued_mails_fast()) {
2107 if (alertpanel(_("Queued messages"),
2108 _("Some unsent messages are queued. Exit now?"),
2109 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL)
2110 != G_ALERTALTERNATE) {
2111 main_window_popup(mainwin);
2115 manage_window_focus_in(mainwin->window, NULL, NULL);
2119 #ifdef HAVE_VALGRIND
2120 if (RUNNING_ON_VALGRIND) {
2121 summary_clear_list(mainwin->summaryview);
2124 if (folderview_get_selected_item(mainwin->folderview))
2125 folder_item_close(folderview_get_selected_item(mainwin->folderview));
2129 gboolean claws_is_exiting(void)
2134 gboolean claws_is_starting(void)
2140 * CLAWS: want this public so crash dialog can delete the
2143 gchar *claws_get_socket_name(void)
2145 static gchar *filename = NULL;
2146 const gchar *socket_dir = NULL;
2148 if (rc_dir_is_alt())
2149 socket_dir = get_rc_dir();
2151 socket_dir = g_get_tmp_dir();
2152 if (filename == NULL) {
2153 filename = g_strdup_printf("%s%cclaws-mail-%d",
2154 socket_dir, G_DIR_SEPARATOR,
2165 static gchar *get_crashfile_name(void)
2167 static gchar *filename = NULL;
2169 if (filename == NULL) {
2170 filename = g_strdup_printf("%s%cclaws-crashed",
2171 get_tmp_dir(), G_DIR_SEPARATOR);
2177 static gint prohibit_duplicate_launch(void)
2183 path = claws_get_socket_name();
2184 uxsock = fd_connect_unix(path);
2186 if (x_display == NULL)
2187 x_display = g_strdup(g_getenv("DISPLAY"));
2191 return fd_open_unix(path);
2196 hmutex = CreateMutexA(NULL, FALSE, "ClawsMail");
2198 debug_print("cannot create Mutex\n");
2201 if (GetLastError() != ERROR_ALREADY_EXISTS) {
2202 uxsock = fd_open_inet(50216);
2208 uxsock = fd_connect_inet(50216);
2212 /* remote command mode */
2214 debug_print("another Claws Mail instance is already running.\n");
2216 if (cmd.receive_all) {
2217 fd_write_all(uxsock, "receive_all\n", 12);
2218 } else if (cmd.receive) {
2219 fd_write_all(uxsock, "receive\n", 8);
2220 } else if (cmd.compose && cmd.attach_files) {
2221 gchar *str, *compose_str;
2224 if (cmd.compose_mailto) {
2225 compose_str = g_strdup_printf("compose_attach %s\n",
2226 cmd.compose_mailto);
2228 compose_str = g_strdup("compose_attach\n");
2231 fd_write_all(uxsock, compose_str, strlen(compose_str));
2232 g_free(compose_str);
2234 for (i = 0; i < cmd.attach_files->len; i++) {
2235 str = g_ptr_array_index(cmd.attach_files, i);
2236 fd_write_all(uxsock, str, strlen(str));
2237 fd_write_all(uxsock, "\n", 1);
2240 fd_write_all(uxsock, ".\n", 2);
2241 } else if (cmd.compose) {
2244 if (cmd.compose_mailto) {
2245 compose_str = g_strdup_printf
2246 ("compose %s\n", cmd.compose_mailto);
2248 compose_str = g_strdup("compose\n");
2251 fd_write_all(uxsock, compose_str, strlen(compose_str));
2252 g_free(compose_str);
2253 } else if (cmd.subscribe) {
2254 gchar *str = g_strdup_printf("subscribe %s\n", cmd.subscribe_uri);
2255 fd_write_all(uxsock, str, strlen(str));
2257 } else if (cmd.send) {
2258 fd_write_all(uxsock, "send\n", 5);
2259 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
2260 fd_write(uxsock, "online\n", 6);
2261 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
2262 fd_write(uxsock, "offline\n", 7);
2263 } else if (cmd.status || cmd.status_full) {
2264 gchar buf[BUFFSIZE];
2266 const gchar *command;
2270 command = cmd.status_full ? "status-full\n" : "status\n";
2271 folders = cmd.status_full ? cmd.status_full_folders :
2274 fd_write_all(uxsock, command, strlen(command));
2275 for (i = 0; folders && i < folders->len; ++i) {
2276 folder = g_ptr_array_index(folders, i);
2277 fd_write_all(uxsock, folder, strlen(folder));
2278 fd_write_all(uxsock, "\n", 1);
2280 fd_write_all(uxsock, ".\n", 2);
2282 fd_gets(uxsock, buf, sizeof(buf));
2283 if (!strncmp(buf, ".\n", 2)) break;
2286 } else if (cmd.exit) {
2287 fd_write_all(uxsock, "exit\n", 5);
2288 } else if (cmd.target) {
2289 gchar *str = g_strdup_printf("select %s\n", cmd.target);
2290 fd_write_all(uxsock, str, strlen(str));
2292 } else if (cmd.search) {
2293 gchar buf[BUFFSIZE];
2295 g_strdup_printf("search %s\n%s\n%s\n%c\n",
2296 cmd.search_folder, cmd.search_type, cmd.search_request,
2297 (cmd.search_recursive==TRUE)?'1':'0');
2298 fd_write_all(uxsock, str, strlen(str));
2301 fd_gets(uxsock, buf, sizeof(buf));
2302 if (!strncmp(buf, ".\n", 2)) break;
2308 fd_write_all(uxsock, "get_display\n", 12);
2309 memset(buf, 0, sizeof(buf));
2310 fd_gets(uxsock, buf, sizeof(buf));
2311 if (strcmp2(buf, x_display)) {
2312 g_print("Claws Mail is already running on display %s.\n",
2316 uxsock = fd_connect_unix(path);
2317 fd_write_all(uxsock, "popup\n", 6);
2320 fd_write_all(uxsock, "popup\n", 6);
2328 static gint lock_socket_remove(void)
2333 if (lock_socket < 0) {
2337 if (lock_socket_tag > 0) {
2338 g_source_remove(lock_socket_tag);
2340 fd_close(lock_socket);
2343 filename = claws_get_socket_name();
2344 claws_unlink(filename);
2350 static GPtrArray *get_folder_item_list(gint sock)
2352 gchar buf[BUFFSIZE];
2354 GPtrArray *folders = NULL;
2357 fd_gets(sock, buf, sizeof(buf));
2358 if (!strncmp(buf, ".\n", 2)) {
2363 folders = g_ptr_array_new();
2365 item = folder_find_item_from_identifier(buf);
2367 g_ptr_array_add(folders, item);
2369 g_warning("no such folder: %s\n", buf);
2376 static void lock_socket_input_cb(gpointer data,
2378 GdkInputCondition condition)
2380 MainWindow *mainwin = (MainWindow *)data;
2382 gchar buf[BUFFSIZE];
2383 /* re-use the same quicksearch (& avoid matcher_list mem.leaks) */
2384 static QuickSearch *quicksearch = NULL;
2386 sock = fd_accept(source);
2387 fd_gets(sock, buf, sizeof(buf));
2389 if (!strncmp(buf, "popup", 5)) {
2390 main_window_popup(mainwin);
2391 } else if (!strncmp(buf, "get_display", 11)) {
2392 fd_write_all(sock, x_display, strlen(x_display));
2393 } else if (!strncmp(buf, "receive_all", 11)) {
2394 inc_all_account_mail(mainwin, FALSE,
2395 prefs_common.newmail_notify_manu);
2396 } else if (!strncmp(buf, "receive", 7)) {
2397 inc_mail(mainwin, prefs_common.newmail_notify_manu);
2398 } else if (!strncmp(buf, "compose_attach", 14)) {
2402 mailto = g_strdup(buf + strlen("compose_attach") + 1);
2403 files = g_ptr_array_new();
2404 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
2406 if (!strcmp2(buf, "."))
2408 g_ptr_array_add(files, g_strdup(buf));
2410 open_compose_new(mailto, files);
2411 ptr_array_free_strings(files);
2412 g_ptr_array_free(files, TRUE);
2414 } else if (!strncmp(buf, "compose", 7)) {
2415 open_compose_new(buf + strlen("compose") + 1, NULL);
2416 } else if (!strncmp(buf, "subscribe", 9)) {
2417 main_window_popup(mainwin);
2418 folder_subscribe(buf + strlen("subscribe") + 1);
2419 } else if (!strncmp(buf, "send", 4)) {
2421 } else if (!strncmp(buf, "online", 6)) {
2422 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
2423 } else if (!strncmp(buf, "offline", 7)) {
2424 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
2425 } else if (!strncmp(buf, "status-full", 11) ||
2426 !strncmp(buf, "status", 6)) {
2430 folders = get_folder_item_list(sock);
2431 status = folder_get_status
2432 (folders, !strncmp(buf, "status-full", 11));
2433 fd_write_all(sock, status, strlen(status));
2434 fd_write_all(sock, ".\n", 2);
2436 if (folders) g_ptr_array_free(folders, TRUE);
2437 } else if (!strncmp(buf, "select ", 7)) {
2438 const gchar *target = buf+7;
2439 mainwindow_jump_to(target, TRUE);
2440 } else if (!strncmp(buf, "search ", 7)) {
2441 FolderItem* folderItem = NULL;
2442 GSList *messages = NULL;
2443 gchar *folder_name, *request;
2444 QuickSearchType searchType = QUICK_SEARCH_EXTENDED;
2447 if (quicksearch==NULL)
2448 quicksearch = quicksearch_new_nogui();
2450 folder_name = g_strdup(buf+7);
2451 strretchomp(folder_name);
2453 if (fd_gets(sock, buf, sizeof(buf)) <= 0) {
2454 g_free(folder_name);
2457 searchType = quicksearch_type(buf);
2458 if (fd_gets(sock, buf, sizeof(buf)) <= 0) {
2459 g_free(folder_name);
2462 request = g_strdup(buf);
2463 strretchomp(request);
2466 if (fd_gets(sock, buf, sizeof(buf)) > 0)
2470 debug_print("search: %s %i %s %i\n",folder_name,searchType,request,recursive);
2473 folderItem = folder_find_item_from_identifier(folder_name);
2474 if (folder_name && folderItem == NULL) {
2475 debug_print("Unknow folder item : '%s', searching folder\n",folder_name);
2476 Folder* folder = folder_find_from_path(folder_name);
2478 folderItem = FOLDER_ITEM(folder->node->data);
2480 debug_print("Unknown folder: '%s'\n",folder_name);
2482 debug_print("%s %s\n",folderItem->name, folderItem->path);
2484 if (folderItem != NULL) {
2485 quicksearch_set(quicksearch, searchType, request);
2486 quicksearch_set_recursive(quicksearch, recursive);
2487 search_msgs_in_folders(&messages, quicksearch, folderItem);
2489 g_print("Folder '%s' not found.\n'", folder_name);
2493 for (cur=messages; cur != NULL; cur = cur->next) {
2494 MsgInfo* msg = (MsgInfo *)cur->data;
2495 gchar *file = procmsg_get_message_file_path(msg);
2496 fd_write_all(sock, file, strlen(file));
2497 fd_write_all(sock, "\n", 1);
2500 fd_write_all(sock, ".\n", 2);
2502 if (messages != NULL)
2503 procmsg_msg_list_free(messages);
2504 g_free(folder_name);
2506 } else if (!strncmp(buf, "exit", 4)) {
2507 if (prefs_common.clean_on_exit && !prefs_common.ask_on_clean) {
2508 procmsg_empty_all_trash();
2510 app_will_exit(NULL, mainwin);
2516 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
2521 Xstrdup_a(addr, address, return);
2525 compose_new(NULL, addr, attach_files);
2528 static void send_queue(void)
2531 gchar *errstr = NULL;
2532 gboolean error = FALSE;
2533 for (list = folder_get_list(); list != NULL; list = list->next) {
2534 Folder *folder = list->data;
2536 if (folder->queue) {
2537 gint res = procmsg_send_queue
2538 (folder->queue, prefs_common.savemsg,
2542 folder_item_scan(folder->queue);
2550 alertpanel_error_log(_("Some errors occurred "
2551 "while sending queued messages:\n%s"), errstr);
2554 alertpanel_error_log("Some errors occurred "
2555 "while sending queued messages.");
2559 static void quit_signal_handler(int sig)
2561 debug_print("Quitting on signal %d\n", sig);
2563 g_timeout_add(0, clean_quit, NULL);
2566 static void install_basic_sighandlers()
2570 struct sigaction act;
2575 sigaddset(&mask, SIGTERM);
2578 sigaddset(&mask, SIGINT);
2581 sigaddset(&mask, SIGHUP);
2584 act.sa_handler = quit_signal_handler;
2589 sigaction(SIGTERM, &act, 0);
2592 sigaction(SIGINT, &act, 0);
2595 sigaction(SIGHUP, &act, 0);
2598 sigprocmask(SIG_UNBLOCK, &mask, 0);
2599 #endif /* !G_OS_WIN32 */
2602 #if (defined linux && defined SIGIO)
2603 static int mem_notify_fd = 0;
2605 static gboolean clean_caches(gpointer unused)
2607 if (static_mainwindow && static_mainwindow->lock_count > 0)
2609 debug_print("/dev/mem_notify: callback: Freeing some memory now!\n");
2610 folder_clean_cache_memory_force();
2614 static void memory_signal_handler(int sig)
2616 debug_print("/dev/mem_notify: Kernel says we should free up some memory!\n");
2617 g_timeout_add(10, clean_caches, NULL);
2620 static void install_memory_sighandler()
2623 struct sigaction act;
2626 mem_notify_fd = g_open("/dev/mem_notify", O_RDONLY|O_NONBLOCK, 0);
2627 if (mem_notify_fd == -1) {
2628 debug_print("/dev/mem_notify not available (%s)\n",
2633 fcntl(mem_notify_fd, F_SETOWN, getpid());
2634 flags = fcntl(mem_notify_fd, F_GETFL);
2635 fcntl(mem_notify_fd, flags|FASYNC);
2639 sigaddset(&mask, SIGIO);
2641 act.sa_handler = memory_signal_handler;
2645 sigaction(SIGIO, &act, 0);
2647 sigprocmask(SIG_UNBLOCK, &mask, 0);
2649 debug_print("/dev/mem_notify: installed handler\n");
2651 #endif /* linux && SIGIO */
2654 osso_context_t *get_osso_context(void)
2656 return static_osso_context;
2661 #ifdef HAVE_NETWORKMANAGER_SUPPORT
2662 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
2665 MainWindow *mainWin;
2668 if (static_mainwindow)
2669 mainWin = static_mainwindow;
2671 mainWin = (MainWindow*)data;
2673 if (!prefs_common.use_networkmanager)
2681 online = networkmanager_is_online(&error);
2683 if(online && went_offline_nm) {
2684 went_offline_nm = FALSE;
2685 main_window_toggle_work_offline(mainWin, FALSE, FALSE);
2686 debug_print("NetworkManager: Went online\n");
2687 log_message(LOG_PROTOCOL, _("NetworkManager: network is online.\n"));
2690 went_offline_nm = TRUE;
2691 main_window_toggle_work_offline(mainWin, TRUE, FALSE);
2692 debug_print("NetworkManager: Went offline\n");
2693 log_message(LOG_PROTOCOL, _("NetworkManager: network is offline.\n"));
2697 debug_print("Failed to get online information from NetworkManager: %s\n",
2699 g_error_free(error);
2703 debug_print("NetworkManager: Cannot change connection state because "
2704 "main window does not exist\n");
2707 /* Returns true (and sets error appropriately, if given) in case of error */
2708 gboolean networkmanager_is_online(GError **error)
2710 DBusGConnection *connection;
2712 GError *tmp_error = NULL;
2716 if (!prefs_common.use_networkmanager)
2721 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &tmp_error);
2724 /* If calling code doesn't do error checking, at least print some debug */
2725 if((error == NULL) || (*error == NULL))
2726 debug_print("Failed to open connection to system bus: %s\n",
2727 tmp_error->message);
2728 g_propagate_error(error, tmp_error);
2732 proxy = dbus_g_proxy_new_for_name(connection,
2733 "org.freedesktop.NetworkManager",
2734 "/org/freedesktop/NetworkManager",
2735 "org.freedesktop.NetworkManager");
2737 retVal = dbus_g_proxy_call(proxy,"state",&tmp_error, G_TYPE_INVALID,
2738 G_TYPE_UINT, &state, G_TYPE_INVALID);
2741 g_object_unref(proxy);
2743 dbus_g_connection_unref(connection);
2746 /* If calling code doesn't do error checking, at least print some debug */
2747 if((error == NULL) || (*error == NULL))
2748 debug_print("Failed to get state info from NetworkManager: %s\n",
2749 tmp_error->message);
2750 g_propagate_error(error, tmp_error);
2753 #if NM_CHECK_VERSION(0,8,992)
2754 return (state == NM_STATE_CONNECTED_LOCAL ||
2755 state == NM_STATE_CONNECTED_SITE ||
2756 state == NM_STATE_CONNECTED_GLOBAL ||
2757 state == NM_STATE_UNKNOWN);
2759 return (state == NM_STATE_CONNECTED ||
2760 state == NM_STATE_UNKNOWN);