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 GIOCondition 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;
427 gboolean backup = TRUE;
429 keep_backup_chk = gtk_check_button_new_with_label (_("Keep old configuration"));
430 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_backup_chk), TRUE);
431 CLAWS_SET_TIP(keep_backup_chk,
432 _("Keeping a backup will allow you to go back to an "
433 "older version, but may take a while if you have "
434 "cached IMAP or News data, and will take some extra "
435 "room on your disk."));
437 g_signal_connect(G_OBJECT(keep_backup_chk), "toggled",
438 G_CALLBACK(chk_update_val), &backup);
440 if (alertpanel_full(_("Migration of configuration"), message,
441 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL, FALSE,
442 keep_backup_chk, ALERT_QUESTION, G_ALERTDEFAULT) != G_ALERTALTERNATE) {
446 /* we can either do a fast migration requiring not any extra disk
447 * space, or a slow one that copies the old configuration and leaves
451 window = label_window_create(_("Copying configuration... This may take a while..."));
454 r = copy_dir(old_cfg_dir, new_cfg_dir);
455 label_window_destroy(window);
457 /* if copy failed, we'll remove the partially copied
460 alertpanel_error(_("Migration failed!"));
461 remove_dir_recursive(new_cfg_dir);
464 /* fast mode failed, but we don't want backup */
465 remove_dir_recursive(old_cfg_dir);
469 window = label_window_create(_("Migrating configuration..."));
472 r = g_rename(old_cfg_dir, new_cfg_dir);
473 label_window_destroy(window);
475 /* if g_rename failed, we'll try to copy */
477 FILE_OP_ERROR(new_cfg_dir, "g_rename failed, trying copy\n");
484 static int migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
487 gchar *plugin_path, *old_plugin_path, *new_plugin_path;
489 gboolean err = FALSE;
491 oldfp = g_fopen(old_rc, "r");
494 newfp = g_fopen(new_rc, "w");
500 plugin_path = g_strdup(get_plugin_dir());
501 new_plugin_path = g_strdup(plugin_path);
503 if (strstr(plugin_path, "/claws-mail/")) {
504 gchar *end = g_strdup(strstr(plugin_path, "/claws-mail/")+strlen("/claws-mail/"));
505 *(strstr(plugin_path, "/claws-mail/")) = '\0';
506 old_plugin_path = g_strconcat(plugin_path, "/sylpheed-claws/", end, NULL);
509 old_plugin_path = g_strdup(new_plugin_path);
511 debug_print("replacing %s with %s\n", old_plugin_path, new_plugin_path);
512 while (fgets(buf, sizeof(buf), oldfp)) {
513 if (strncmp(buf, old_plugin_path, strlen(old_plugin_path))) {
514 err |= (fputs(buf, newfp) == EOF);
516 debug_print("->replacing %s", buf);
517 debug_print(" with %s%s", new_plugin_path, buf+strlen(old_plugin_path));
518 err |= (fputs(new_plugin_path, newfp) == EOF);
519 err |= (fputs(buf+strlen(old_plugin_path), newfp) == EOF);
523 g_free(new_plugin_path);
524 g_free(old_plugin_path);
526 if (fclose(newfp) == EOF)
534 sc_client_set_value (MainWindow *mainwin,
545 prop.num_vals = num_vals;
549 if (mainwin->smc_conn)
550 SmcSetProperties ((SmcConn) mainwin->smc_conn, 1, proplist);
553 static void sc_die_callback (SmcConn smc_conn, SmPointer client_data)
558 static void sc_save_complete_callback(SmcConn smc_conn, SmPointer client_data)
562 static void sc_shutdown_cancelled_callback (SmcConn smc_conn, SmPointer client_data)
564 MainWindow *mainwin = (MainWindow *)client_data;
565 if (mainwin->smc_conn)
566 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
569 static void sc_save_yourself_callback (SmcConn smc_conn,
570 SmPointer client_data,
576 MainWindow *mainwin = (MainWindow *)client_data;
577 if (mainwin->smc_conn)
578 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
581 static IceIOErrorHandler sc_ice_installed_handler;
583 static void sc_ice_io_error_handler (IceConn connection)
585 if (sc_ice_installed_handler)
586 (*sc_ice_installed_handler) (connection);
588 static gboolean sc_process_ice_messages (GIOChannel *source,
589 GIOCondition condition,
592 IceConn connection = (IceConn) data;
593 IceProcessMessagesStatus status;
595 status = IceProcessMessages (connection, NULL, NULL);
597 if (status == IceProcessMessagesIOError) {
598 IcePointer context = IceGetConnectionContext (connection);
600 if (context && GTK_IS_OBJECT (context)) {
601 guint disconnect_id = g_signal_lookup ("disconnect", G_OBJECT_TYPE (context));
603 if (disconnect_id > 0)
604 g_signal_emit (context, disconnect_id, 0);
606 IceSetShutdownNegotiation (connection, False);
607 IceCloseConnection (connection);
614 static void new_ice_connection (IceConn connection, IcePointer client_data, Bool opening,
615 IcePointer *watch_data)
621 /* Make sure we don't pass on these file descriptors to any
623 fcntl(IceConnectionNumber(connection),F_SETFD,
624 fcntl(IceConnectionNumber(connection),F_GETFD,0) | FD_CLOEXEC);
626 channel = g_io_channel_unix_new (IceConnectionNumber (connection));
627 input_id = g_io_add_watch (channel,
628 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
629 sc_process_ice_messages,
631 g_io_channel_unref (channel);
633 *watch_data = (IcePointer) GUINT_TO_POINTER (input_id);
635 input_id = GPOINTER_TO_UINT ((gpointer) *watch_data);
636 g_source_remove (input_id);
640 static void sc_session_manager_connect(MainWindow *mainwin)
642 static gboolean connected = FALSE;
643 SmcCallbacks callbacks;
645 IceIOErrorHandler default_handler;
652 sc_ice_installed_handler = IceSetIOErrorHandler (NULL);
653 default_handler = IceSetIOErrorHandler (sc_ice_io_error_handler);
655 if (sc_ice_installed_handler == default_handler)
656 sc_ice_installed_handler = NULL;
658 IceAddConnectionWatch (new_ice_connection, NULL);
661 callbacks.save_yourself.callback = sc_save_yourself_callback;
662 callbacks.die.callback = sc_die_callback;
663 callbacks.save_complete.callback = sc_save_complete_callback;
664 callbacks.shutdown_cancelled.callback = sc_shutdown_cancelled_callback;
666 callbacks.save_yourself.client_data =
667 callbacks.die.client_data =
668 callbacks.save_complete.client_data =
669 callbacks.shutdown_cancelled.client_data = (SmPointer) mainwin;
670 if (g_getenv ("SESSION_MANAGER")) {
671 gchar error_string_ret[256] = "";
673 mainwin->smc_conn = (gpointer)
674 SmcOpenConnection (NULL, mainwin,
675 SmProtoMajor, SmProtoMinor,
676 SmcSaveYourselfProcMask | SmcDieProcMask |
677 SmcSaveCompleteProcMask |
678 SmcShutdownCancelledProcMask,
681 256, error_string_ret);
683 if (error_string_ret[0] || mainwin->smc_conn == NULL)
684 g_warning ("While connecting to session manager:\n%s.",
688 vals = g_new (SmPropValue, 1);
689 vals[0].length = strlen(argv0);
690 vals[0].value = argv0;
691 sc_client_set_value (mainwin, SmCloneCommand, SmLISTofARRAY8, 1, vals);
692 sc_client_set_value (mainwin, SmRestartCommand, SmLISTofARRAY8, 1, vals);
693 sc_client_set_value (mainwin, SmProgram, SmARRAY8, 1, vals);
695 vals[0].length = strlen(g_get_user_name()?g_get_user_name():"");
696 vals[0].value = g_strdup(g_get_user_name()?g_get_user_name():"");
697 sc_client_set_value (mainwin, SmUserID, SmARRAY8, 1, vals);
703 static gboolean sc_exiting = FALSE;
704 static gboolean show_at_startup = TRUE;
705 static gboolean claws_crashed_bool = FALSE;
707 gboolean claws_crashed(void) {
708 return claws_crashed_bool;
711 void main_set_show_at_startup(gboolean show)
713 show_at_startup = show;
717 static void main_vol_mount_cb(GnomeVFSVolumeMonitor *vfs, GnomeVFSVolume *vol, MainWindow *mainwin)
719 gchar *uri = gnome_vfs_volume_get_activation_uri (vol);
720 gchar *mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
723 if(!strcmp(mount_path, prefs_common.data_root)) {
724 gtk_widget_set_sensitive(mainwin->window, TRUE);
730 static void main_vol_unmount_cb(GnomeVFSVolumeMonitor *vfs, GnomeVFSVolume *vol, MainWindow *mainwin)
732 gchar *uri = gnome_vfs_volume_get_activation_uri (vol);
733 gchar *mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
736 if(!strcmp(mount_path, prefs_common.data_root)) {
737 gtk_widget_set_sensitive(mainwin->window, FALSE);
746 static FILE* win32_debug_fp=NULL;
747 static guint win32_log_handler_app_id;
748 static guint win32_log_handler_glib_id;
749 static guint win32_log_handler_gtk_id;
751 static void win32_print_stdout(const gchar *string)
753 if (win32_debug_fp) {
754 fprintf(win32_debug_fp, string);
755 fflush(win32_debug_fp);
759 static void win32_print_stderr(const gchar *string)
761 if (win32_debug_fp) {
762 fprintf(win32_debug_fp, string);
763 fflush(win32_debug_fp);
767 static void win32_log(const gchar *log_domain, GLogLevelFlags log_level, const gchar* message, gpointer user_data)
769 if (win32_debug_fp) {
772 switch(log_level & G_LOG_LEVEL_MASK)
774 case G_LOG_LEVEL_ERROR:
777 case G_LOG_LEVEL_CRITICAL:
780 case G_LOG_LEVEL_WARNING:
783 case G_LOG_LEVEL_MESSAGE:
786 case G_LOG_LEVEL_INFO:
789 case G_LOG_LEVEL_DEBUG:
796 fprintf(win32_debug_fp, "%s: %s: %s", log_domain, type, message);
798 fprintf(win32_debug_fp, "%s: %s", type, message);
799 fflush(win32_debug_fp);
803 static void win32_open_log(void)
805 gchar *logfile = g_strconcat(g_get_tmp_dir(), G_DIR_SEPARATOR_S, "claws-win32.log", NULL);
806 gchar *oldlogfile = g_strconcat(g_get_tmp_dir(), G_DIR_SEPARATOR_S, "claws-win32.log.bak", NULL);
808 if (is_file_exist(logfile)) {
809 if (rename_force(logfile, oldlogfile) < 0)
810 FILE_OP_ERROR(logfile, "rename");
812 win32_debug_fp = g_fopen(logfile, "w");
817 g_set_print_handler(win32_print_stdout);
818 g_set_printerr_handler(win32_print_stdout);
819 win32_log_handler_app_id = g_log_set_handler(NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
820 | G_LOG_FLAG_RECURSION, win32_log, NULL);
821 win32_log_handler_glib_id = g_log_set_handler("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
822 | G_LOG_FLAG_RECURSION, win32_log, NULL);
823 win32_log_handler_gtk_id = g_log_set_handler("Gtk", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
824 | G_LOG_FLAG_RECURSION, win32_log, NULL);
828 static void win32_close_log(void)
832 g_log_remove_handler("", win32_log_handler_app_id);
833 g_log_remove_handler("GLib", win32_log_handler_glib_id);
834 g_log_remove_handler("Gtk", win32_log_handler_gtk_id);
835 fclose(win32_debug_fp);
841 static void main_dump_features_list(gboolean show_debug_only)
842 /* display compiled-in features list */
844 if (show_debug_only && !debug_get_mode())
848 debug_print("runtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
849 gtk_major_version, gtk_minor_version, gtk_micro_version,
850 glib_major_version, glib_minor_version, glib_micro_version);
852 g_print("runtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
853 gtk_major_version, gtk_minor_version, gtk_micro_version,
854 glib_major_version, glib_minor_version, glib_micro_version);
856 debug_print("buildtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
857 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
858 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
860 g_print("buildtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
861 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
862 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
865 debug_print("Compiled-in features:\n");
867 g_print("Compiled-in features:\n");
870 debug_print(" compface\n");
872 g_print(" compface\n");
876 debug_print(" aspell\n");
878 g_print(" aspell\n");
882 debug_print(" gnutls\n");
884 g_print(" gnutls\n");
888 debug_print(" ipv6\n");
894 debug_print(" iconv\n");
900 debug_print(" jpilot\n");
902 g_print(" jpilot\n");
906 debug_print(" ldap\n");
912 debug_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
914 g_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
918 debug_print(" libsm\n");
922 #if HAVE_NETWORKMANAGER_SUPPORT
924 debug_print(" NetworkManager\n");
926 g_print(" NetworkManager\n");
930 #ifdef HAVE_DBUS_GLIB
931 static guint dbus_item_hook_id = -1;
932 static guint dbus_folder_hook_id = -1;
934 static void uninstall_dbus_status_handler(void)
937 g_object_unref(awn_proxy);
939 if (dbus_item_hook_id != -1)
940 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, dbus_item_hook_id);
941 if (dbus_folder_hook_id != -1)
942 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, dbus_folder_hook_id);
945 static void dbus_update(FolderItem *removed_item)
947 guint new, unread, unreadmarked, marked, total;
948 guint replied, forwarded, locked, ignored, watched;
950 GError *error = NULL;
952 folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total,
953 &replied, &forwarded, &locked, &ignored,
956 total -= removed_item->total_msgs;
957 new -= removed_item->new_msgs;
958 unread -= removed_item->unread_msgs;
962 buf = g_strdup_printf("%d", new);
963 dbus_g_proxy_call(awn_proxy, "SetInfoByName", &error,
964 G_TYPE_STRING, "claws-mail",
966 G_TYPE_INVALID, G_TYPE_INVALID);
970 dbus_g_proxy_call(awn_proxy, "UnsetInfoByName", &error, G_TYPE_STRING,
971 "claws-mail", G_TYPE_INVALID, G_TYPE_INVALID);
974 debug_print("%s\n", error->message);
979 static gboolean dbus_status_update_folder_hook(gpointer source, gpointer data)
981 FolderUpdateData *hookdata;
983 if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
984 dbus_update(hookdata->item);
991 static gboolean dbus_status_update_item_hook(gpointer source, gpointer data)
998 static void install_dbus_status_handler(void)
1000 GError *tmp_error = NULL;
1001 DBusGConnection *connection = dbus_g_bus_get(DBUS_BUS_SESSION, &tmp_error);
1004 /* If calling code doesn't do error checking, at least print some debug */
1005 debug_print("Failed to open connection to session bus: %s\n",
1006 tmp_error->message);
1007 g_error_free(tmp_error);
1010 awn_proxy = dbus_g_proxy_new_for_name(connection,
1011 "com.google.code.Awn",
1012 "/com/google/code/Awn",
1013 "com.google.code.Awn");
1014 dbus_item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, dbus_status_update_item_hook, NULL);
1015 if (dbus_item_hook_id == -1) {
1016 g_warning(_("Failed to register folder item update hook"));
1017 uninstall_dbus_status_handler();
1021 dbus_folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, dbus_status_update_folder_hook, NULL);
1022 if (dbus_folder_hook_id == -1) {
1023 g_warning(_("Failed to register folder update hook"));
1024 uninstall_dbus_status_handler();
1030 int main(int argc, char *argv[])
1033 osso_context_t *osso_context;
1034 osso_return_t result;
1036 #ifdef HAVE_DBUS_GLIB
1037 DBusGConnection *connection;
1040 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1041 DBusGProxy *nm_proxy;
1044 MainWindow *mainwin;
1045 FolderView *folderview;
1047 gboolean crash_file_present = FALSE;
1048 gint num_folder_class = 0;
1049 gboolean asked_for_migration = FALSE;
1050 gboolean start_done = TRUE;
1051 GtkUIManager *gui_manager = NULL;
1052 GSList *plug_list = NULL;
1053 gboolean never_ran = FALSE;
1054 START_TIMING("startup");
1061 if (!claws_init(&argc, &argv)) {
1068 main_dump_features_list(TRUE);
1070 prog_version = PROG_VERSION;
1071 argv0 = g_strdup(argv[0]);
1073 parse_cmd_opt(argc, argv);
1075 prefs_prepare_cache();
1080 gtk_init(&argc, &argv);
1081 crash_main(cmd.crash_params);
1087 crash_install_handlers();
1089 install_basic_sighandlers();
1090 #if (defined linux && defined SIGIO)
1091 install_memory_sighandler();
1095 /* check and create unix domain socket for remote operation */
1096 lock_socket = prohibit_duplicate_launch();
1097 if (lock_socket < 0) {
1098 #ifdef HAVE_STARTUP_NOTIFICATION
1099 if(gtk_init_check(&argc, &argv))
1100 startup_notification_complete(TRUE);
1105 if (cmd.status || cmd.status_full || cmd.search) {
1106 puts("0 Claws Mail not running.");
1107 lock_socket_remove();
1113 if (!g_thread_supported())
1114 g_thread_init(NULL);
1117 gtk_init(&argc, &argv);
1120 gtk_settings_set_string_property(gtk_settings_get_default(),
1126 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1127 went_offline_nm = FALSE;
1130 #ifdef HAVE_DBUS_GLIB
1132 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
1135 debug_print("Failed to open connection to system bus: %s\n", error->message);
1136 g_error_free(error);
1139 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1140 nm_proxy = dbus_g_proxy_new_for_name(connection,
1141 "org.freedesktop.NetworkManager",
1142 "/org/freedesktop/NetworkManager",
1143 "org.freedesktop.NetworkManager");
1145 #if NM_CHECK_VERSION(0,8,992)
1146 dbus_g_proxy_add_signal(nm_proxy, "StateChanged", G_TYPE_UINT, G_TYPE_INVALID);
1147 dbus_g_proxy_connect_signal(nm_proxy, "StateChanged",
1148 G_CALLBACK(networkmanager_state_change_cb),
1151 dbus_g_proxy_add_signal(nm_proxy, "StateChange", G_TYPE_UINT, G_TYPE_INVALID);
1152 dbus_g_proxy_connect_signal(nm_proxy, "StateChange",
1153 G_CALLBACK(networkmanager_state_change_cb),
1158 install_dbus_status_handler();
1164 osso_context = osso_initialize(OSSO_SERVICE, "2.8.1", TRUE, NULL);
1165 if (osso_context == NULL) {
1168 hildon_program = HILDON_PROGRAM(hildon_program_get_instance());
1169 static_osso_context = osso_context;
1171 gtk_widget_set_default_colormap(
1172 gdk_screen_get_system_colormap(
1173 gdk_screen_get_default()));
1175 gui_manager = gtkut_create_ui_manager();
1177 /* Create container for all the menus we will be adding */
1178 MENUITEM_ADDUI("/", "Menus", NULL, GTK_UI_MANAGER_MENUBAR);
1180 if (!g_thread_supported()) {
1181 g_error(_("g_thread is not supported by glib.\n"));
1185 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1187 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1190 /* no config dir exists. See if we can migrate an old config. */
1191 if (!is_dir_exist(RC_DIR)) {
1192 prefs_destroy_cache();
1195 /* if one of the old dirs exist, we'll ask if the user
1196 * want to migrates, and r will be TRUE if he said yes
1197 * and migration succeeded, and FALSE otherwise.
1199 if (is_dir_exist(OLD_GTK2_RC_DIR)) {
1200 r = migrate_old_config(OLD_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 2.6.0 (or older)"));
1201 asked_for_migration = TRUE;
1202 } else if (is_dir_exist(OLDER_GTK2_RC_DIR)) {
1203 r = migrate_old_config(OLDER_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.9.15 (or older)"));
1204 asked_for_migration = TRUE;
1205 } else if (is_dir_exist(OLD_GTK1_RC_DIR)) {
1206 r = migrate_old_config(OLD_GTK1_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.0.5 (or older)"));
1207 asked_for_migration = TRUE;
1208 } else if (is_dir_exist(SYLPHEED_RC_DIR)) {
1209 r = migrate_old_config(SYLPHEED_RC_DIR, RC_DIR, "Sylpheed");
1210 asked_for_migration = TRUE;
1213 /* If migration failed or the user didn't want to do it,
1214 * we create a new one (and we'll hit wizard later).
1216 if (r == FALSE && !is_dir_exist(RC_DIR)) {
1218 if (copy_dir(SYSCONFDIR "/skel/.claws-mail", RC_DIR) < 0) {
1220 if (!is_dir_exist(RC_DIR) && make_dir(RC_DIR) < 0) {
1233 if (!is_file_exist(RC_DIR G_DIR_SEPARATOR_S COMMON_RC) &&
1234 is_file_exist(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC)) {
1235 /* post 2.6 name change */
1236 migrate_common_rc(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC,
1237 RC_DIR G_DIR_SEPARATOR_S COMMON_RC);
1241 plugin_load_all("Common");
1243 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
1244 gtk_rc_parse(userrc);
1247 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1248 gtk_accel_map_load (userrc);
1252 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_rc_dir(), 1, win32_close_log(););
1254 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
1257 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
1258 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
1259 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
1260 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
1261 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
1262 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR);
1264 crash_file_present = is_file_exist(get_crashfile_name());
1265 /* remove temporary files */
1266 remove_all_files(get_tmp_dir());
1267 remove_all_files(get_mime_tmp_dir());
1269 if (!cmd.crash && crash_file_present)
1270 claws_crashed_bool = TRUE;
1272 if (is_file_exist("claws.log")) {
1273 if (rename_force("claws.log", "claws.log.bak") < 0)
1274 FILE_OP_ERROR("claws.log", "rename");
1276 set_log_file(LOG_PROTOCOL, "claws.log");
1278 if (is_file_exist("filtering.log")) {
1279 if (rename_force("filtering.log", "filtering.log.bak") < 0)
1280 FILE_OP_ERROR("filtering.log", "rename");
1282 set_log_file(LOG_DEBUG_FILTERING, "filtering.log");
1285 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1287 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1290 folder_system_init();
1291 prefs_common_read_config();
1293 prefs_themes_init();
1295 prefs_ext_prog_init();
1296 prefs_wrapping_init();
1297 prefs_compose_writing_init();
1298 prefs_msg_colors_init();
1299 image_viewer_init();
1300 prefs_image_viewer_init();
1302 prefs_summaries_init();
1303 prefs_message_init();
1305 prefs_logging_init();
1306 prefs_receive_init();
1310 gtkaspell_checkers_init();
1311 prefs_spelling_init();
1314 sock_set_io_timeout(prefs_common.io_timeout_secs);
1315 prefs_actions_read_config();
1316 prefs_display_header_read_config();
1317 /* prefs_filtering_read_config(); */
1318 addressbook_read_file();
1320 gtkut_widget_init();
1321 stock_pixbuf_gdk(NULL, STOCK_PIXMAP_CLAWS_MAIL_ICON, &icon);
1322 gtk_window_set_default_icon(icon);
1324 folderview_initialize();
1330 mainwin = main_window_create();
1332 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1333 networkmanager_state_change_cb(nm_proxy,NULL,mainwin);
1338 appdata = g_new0(AppData, 1);
1339 appdata->program = hildon_program;
1340 appdata->window = HILDON_WINDOW(mainwin->window);
1341 appdata->osso_context = osso_context;
1342 result = osso_rpc_set_cb_f(appdata->osso_context,
1346 dbus_req_handler, appdata);
1347 if (result != OSSO_OK) {
1352 /* Add handler for Exit D-BUS messages */
1353 result = osso_application_set_exit_cb(appdata->osso_context,
1355 (gpointer) appdata);
1356 if (result != OSSO_OK) {
1360 osso_hw_set_event_cb( appdata->osso_context,
1361 NULL, hw_event_handler, (gpointer) appdata );
1363 manage_window_focus_in(mainwin->window, NULL, NULL);
1364 folderview = mainwin->folderview;
1366 gtk_cmclist_freeze(GTK_CMCLIST(mainwin->folderview->ctree));
1367 folder_item_update_freeze();
1369 /* register the callback of unix domain socket input */
1370 lock_socket_tag = claws_input_add(lock_socket,
1371 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
1372 lock_socket_input_cb,
1376 prefs_account_init();
1377 account_read_config_all();
1379 #ifdef HAVE_LIBETPAN
1380 imap_main_init(prefs_common.skip_ssl_cert_check);
1381 imap_main_set_timeout(prefs_common.io_timeout_secs);
1382 nntp_main_init(prefs_common.skip_ssl_cert_check);
1384 /* If we can't read a folder list or don't have accounts,
1385 * it means the configuration's not done. Either this is
1386 * a brand new install, either a failed/refused migration.
1387 * So we'll start the wizard.
1389 if (folder_read_list() < 0) {
1390 prefs_destroy_cache();
1392 /* if run_wizard returns FALSE it's because it's
1393 * been cancelled. We can't do much but exit.
1394 * however, if the user was asked for a migration,
1395 * we remove the newly created directory so that
1396 * he's asked again for migration on next launch.*/
1397 if (!run_wizard(mainwin, TRUE)) {
1398 if (asked_for_migration)
1399 remove_dir_recursive(RC_DIR);
1405 main_window_reflect_prefs_all_now();
1406 folder_write_list();
1410 if (!account_get_list()) {
1411 prefs_destroy_cache();
1412 if (!run_wizard(mainwin, FALSE)) {
1413 if (asked_for_migration)
1414 remove_dir_recursive(RC_DIR);
1420 if(!account_get_list()) {
1421 exit_claws(mainwin);
1431 toolbar_main_set_sensitive(mainwin);
1432 main_window_set_menu_sensitive(mainwin);
1434 /* if crashed, show window early so that the user
1435 * sees what's happening */
1436 if (claws_crashed())
1437 main_window_popup(mainwin);
1439 account_set_missing_folder();
1440 folder_set_missing_folders();
1441 folderview_set(folderview);
1443 prefs_matcher_read_config();
1444 quicksearch_set_search_strings(mainwin->summaryview->quicksearch);
1446 /* make one all-folder processing before using claws */
1447 main_window_cursor_wait(mainwin);
1448 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
1450 /* if claws crashed, rebuild caches */
1451 if (claws_crashed()) {
1453 debug_print("Claws Mail crashed, checking for new messages in local folders\n");
1454 folder_item_update_thaw();
1455 folderview_check_new(NULL);
1456 folder_clean_cache_memory_force();
1457 folder_item_update_freeze();
1459 /* make the crash-indicator file */
1460 str_write_to_file("foo", get_crashfile_name());
1462 inc_autocheck_timer_init(mainwin);
1464 /* ignore SIGPIPE signal for preventing sudden death of program */
1466 signal(SIGPIPE, SIG_IGN);
1468 if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
1469 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1471 if (cmd.online_mode == ONLINE_MODE_ONLINE) {
1472 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1475 if (cmd.status_folders) {
1476 g_ptr_array_free(cmd.status_folders, TRUE);
1477 cmd.status_folders = NULL;
1479 if (cmd.status_full_folders) {
1480 g_ptr_array_free(cmd.status_full_folders, TRUE);
1481 cmd.status_full_folders = NULL;
1484 claws_register_idle_function(claws_gtk_idle);
1486 prefs_toolbar_init();
1488 num_folder_class = g_list_length(folder_get_list());
1490 plugin_load_all("GTK2");
1492 if (g_list_length(folder_get_list()) != num_folder_class) {
1493 debug_print("new folders loaded, reloading processing rules\n");
1494 prefs_matcher_read_config();
1497 if ((plug_list = plugin_get_unloaded_list()) != NULL) {
1500 gint num_plugins = 0;
1501 for (cur = plug_list; cur; cur = cur->next) {
1502 Plugin *plugin = (Plugin *)cur->data;
1503 gchar *tmp = g_strdup_printf("%s\n%s",
1505 plugin_get_name(plugin));
1510 main_window_cursor_normal(mainwin);
1511 main_window_popup(mainwin);
1512 alertpanel_warning(ngettext(
1513 "The following plugin failed to load. "
1514 "Check the Plugins configuration "
1515 "for more information:\n%s",
1516 "The following plugins failed to load. "
1517 "Check the Plugins configuration "
1518 "for more information:\n%s",
1521 main_window_cursor_wait(mainwin);
1523 g_slist_free(plug_list);
1527 prefs_common_write_config();
1528 plugin_load_standard_plugins ();
1530 /* if not crashed, show window now */
1531 if (!claws_crashed()) {
1532 /* apart if something told not to show */
1533 if (show_at_startup)
1534 main_window_popup(mainwin);
1537 if (!folder_have_mailbox()) {
1538 prefs_destroy_cache();
1539 main_window_cursor_normal(mainwin);
1540 if (folder_get_list() != NULL) {
1541 alertpanel_error(_("Claws Mail has detected a configured "
1542 "mailbox, but it is incomplete. It is "
1543 "possibly due to a failing IMAP account. Use "
1544 "\"Rebuild folder tree\" on the mailbox parent "
1545 "folder's context menu to try to fix it."));
1547 alertpanel_error(_("Claws Mail has detected a configured "
1548 "mailbox, but could not load it. It is "
1549 "probably provided by an out-of-date "
1550 "external plugin. Please reinstall the "
1551 "plugin and try again."));
1552 exit_claws(mainwin);
1560 static_mainwindow = mainwin;
1563 if (prefs_common.data_root != NULL && *prefs_common.data_root != '\0') {
1564 GnomeVFSVolume *vol = NULL;
1565 gchar *uri, *mount_path;
1567 volmon = gnome_vfs_get_volume_monitor();
1568 vol = gnome_vfs_volume_monitor_get_volume_for_path(volmon, prefs_common.data_root);
1570 uri = gnome_vfs_volume_get_activation_uri (vol);
1571 mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
1574 if (vol == NULL || !gnome_vfs_volume_is_mounted(vol)
1575 || strcmp(mount_path, prefs_common.data_root)) {
1576 alertpanel_error(_("Claws Mail can not start without its data volume (%s)."),
1577 prefs_common.data_root);
1579 gnome_vfs_volume_unref(vol);
1580 exit_claws(mainwin);
1584 gnome_vfs_volume_unref(vol);
1585 g_signal_connect(G_OBJECT(volmon),
1586 "volume-mounted", G_CALLBACK(main_vol_mount_cb), mainwin);
1587 g_signal_connect(G_OBJECT(volmon),
1588 "volume-unmounted", G_CALLBACK(main_vol_unmount_cb), mainwin);
1592 #ifdef HAVE_STARTUP_NOTIFICATION
1593 startup_notification_complete(FALSE);
1596 sc_session_manager_connect(mainwin);
1599 folder_item_update_thaw();
1600 gtk_cmclist_thaw(GTK_CMCLIST(mainwin->folderview->ctree));
1601 main_window_cursor_normal(mainwin);
1603 if (!cmd.target && prefs_common.goto_last_folder_on_startup &&
1604 folder_find_item_from_identifier(prefs_common.last_opened_folder) != NULL &&
1606 cmd.target = prefs_common.last_opened_folder;
1609 if (cmd.receive_all && !cmd.target) {
1611 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(FALSE));
1612 } else if (prefs_common.chk_on_startup && !cmd.target) {
1614 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(TRUE));
1615 } else if (cmd.receive && !cmd.target) {
1617 g_timeout_add(1000, defer_check, NULL);
1619 gtk_widget_grab_focus(folderview->ctree);
1622 open_compose_new(cmd.compose_mailto, cmd.attach_files);
1624 if (cmd.attach_files) {
1625 ptr_array_free_strings(cmd.attach_files);
1626 g_ptr_array_free(cmd.attach_files, TRUE);
1627 cmd.attach_files = NULL;
1629 if (cmd.subscribe) {
1630 folder_subscribe(cmd.subscribe_uri);
1639 g_timeout_add(500, defer_jump, (gpointer)cmd.target);
1642 prefs_destroy_cache();
1644 compose_reopen_exit_drafts();
1647 sc_starting = FALSE;
1648 main_window_set_menu_sensitive(mainwin);
1649 toolbar_main_set_sensitive(mainwin);
1656 osso_deinitialize(osso_context);
1658 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1660 g_object_unref(nm_proxy);
1662 #ifdef HAVE_DBUS_GLIB
1663 uninstall_dbus_status_handler();
1665 dbus_g_connection_unref(connection);
1671 exit_claws(mainwin);
1676 static void save_all_caches(FolderItem *item, gpointer data)
1683 folder_item_close(item);
1686 folder_item_free_cache(item, TRUE);
1689 static void exit_claws(MainWindow *mainwin)
1692 gboolean have_connectivity;
1696 debug_print("shutting down\n");
1697 inc_autocheck_timer_remove();
1699 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1700 if (prefs_common.work_offline && went_offline_nm)
1701 prefs_common.work_offline = FALSE;
1704 /* save prefs for opened folder */
1705 if(mainwin->folderview->opened) {
1708 item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
1709 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
1710 prefs_common.last_opened_folder = folder_item_get_identifier(item);
1713 /* save all state before exiting */
1714 folder_func_to_all_folders(save_all_caches, NULL);
1715 folder_write_list();
1717 main_window_get_size(mainwin);
1718 main_window_get_position(mainwin);
1720 prefs_common_write_config();
1721 account_write_config_all();
1722 addressbook_export_to_file();
1724 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1725 gtk_accel_map_save(filename);
1728 /* delete temporary files */
1729 remove_all_files(get_tmp_dir());
1730 remove_all_files(get_mime_tmp_dir());
1732 close_log_file(LOG_PROTOCOL);
1733 close_log_file(LOG_DEBUG_FILTERING);
1735 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1736 have_connectivity = networkmanager_is_online(NULL);
1738 have_connectivity = TRUE;
1740 #ifdef HAVE_LIBETPAN
1741 imap_main_done(have_connectivity);
1742 nntp_main_done(have_connectivity);
1744 /* delete crashfile */
1746 claws_unlink(get_crashfile_name());
1748 lock_socket_remove();
1751 if (mainwin->smc_conn)
1752 SmcCloseConnection ((SmcConn)mainwin->smc_conn, 0, NULL);
1753 mainwin->smc_conn = NULL;
1756 main_window_destroy_all();
1758 plugin_unload_all("GTK2");
1760 prefs_toolbar_done();
1762 addressbook_destroy();
1764 prefs_themes_done();
1766 prefs_ext_prog_done();
1767 prefs_wrapping_done();
1768 prefs_compose_writing_done();
1769 prefs_msg_colors_done();
1770 prefs_image_viewer_done();
1771 image_viewer_done();
1773 prefs_summaries_done();
1774 prefs_message_done();
1776 prefs_receive_done();
1777 prefs_logging_done();
1781 prefs_spelling_done();
1782 gtkaspell_checkers_quit();
1784 plugin_unload_all("Common");
1788 #define G_STRING_APPEND_ENCODED_URI(gstring,source) \
1790 gchar tmpbuf[BUFFSIZE]; \
1791 encode_uri(tmpbuf, BUFFSIZE, (source)); \
1792 g_string_append((gstring), tmpbuf); \
1795 #define G_PRINT_EXIT(msg) \
1801 static GString * parse_cmd_compose_from_file(const gchar *fn)
1803 GString *headers = g_string_new(NULL);
1804 GString *body = g_string_new(NULL);
1812 if (fn == NULL || *fn == '\0')
1813 G_PRINT_EXIT(_("Missing filename\n"));
1814 isstdin = (*fn == '-' && *(fn + 1) == '\0');
1818 fp = g_fopen(fn, "r");
1820 G_PRINT_EXIT(_("Cannot open filename for reading\n"));
1823 while (fgets(fb, sizeof(fb), fp)) {
1829 while (*h && *h != ':') { ++h; } /* search colon */
1831 G_PRINT_EXIT(_("Malformed header\n"));
1833 while (*v && *v == ' ') { ++v; } /* trim value start */
1835 tmp = g_ascii_strdown(fb, -1); /* get header name */
1836 if (!strcmp(tmp, "to")) {
1838 G_PRINT_EXIT(_("Duplicated 'To:' header\n"));
1841 g_string_append_c(headers, '&');
1842 g_string_append(headers, tmp);
1843 g_string_append_c(headers, '=');
1844 #if GLIB_CHECK_VERSION(2,16,0)
1845 g_string_append_uri_escaped(headers, v, NULL, TRUE);
1847 G_STRING_APPEND_ENCODED_URI(headers, v);
1853 G_PRINT_EXIT(_("Missing required 'To:' header\n"));
1854 g_string_append(body, to);
1856 g_string_append(body, "?body=");
1857 while (fgets(fb, sizeof(fb), fp)) {
1858 #if GLIB_CHECK_VERSION(2,16,0)
1859 g_string_append_uri_escaped(body, fb, NULL, TRUE);
1861 G_STRING_APPEND_ENCODED_URI(body, fb);
1866 /* append the remaining headers */
1867 g_string_append(body, headers->str);
1868 g_string_free(headers, TRUE);
1873 #undef G_STRING_APPEND_ENCODED_URI
1876 static void parse_cmd_opt(int argc, char *argv[])
1880 for (i = 1; i < argc; i++) {
1881 if (!strncmp(argv[i], "--receive-all", 13)) {
1882 cmd.receive_all = TRUE;
1883 } else if (!strncmp(argv[i], "--receive", 9)) {
1885 } else if (!strncmp(argv[i], "--compose-from-file", 19)) {
1886 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1888 GString *mailto = parse_cmd_compose_from_file(p);
1890 cmd.compose_mailto = mailto->str;
1892 } else if (!strncmp(argv[i], "--compose", 9)) {
1893 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1896 cmd.compose_mailto = NULL;
1897 if (p && *p != '\0' && *p != '-') {
1898 if (!strncmp(p, "mailto:", 7)) {
1899 cmd.compose_mailto = p + 7;
1901 cmd.compose_mailto = p;
1905 } else if (!strncmp(argv[i], "--subscribe", 11)) {
1906 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1907 if (p && *p != '\0' && *p != '-') {
1908 cmd.subscribe = TRUE;
1909 cmd.subscribe_uri = p;
1911 } else if (!strncmp(argv[i], "--attach", 8)) {
1912 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1915 while (p && *p != '\0' && *p != '-') {
1916 if (!cmd.attach_files) {
1917 cmd.attach_files = g_ptr_array_new();
1919 if ((file = g_filename_from_uri(p, NULL, NULL)) != NULL) {
1920 if (!is_file_exist(file)) {
1925 if (file == NULL && *p != G_DIR_SEPARATOR) {
1926 file = g_strconcat(claws_get_startup_dir(),
1929 } else if (file == NULL) {
1932 g_ptr_array_add(cmd.attach_files, file);
1934 p = (i+1 < argc)?argv[i+1]:NULL;
1936 } else if (!strncmp(argv[i], "--send", 6)) {
1938 } else if (!strncmp(argv[i], "--version-full", 14) ||
1939 !strncmp(argv[i], "-V", 2)) {
1940 g_print("Claws Mail version " VERSION "\n");
1941 main_dump_features_list(FALSE);
1943 } else if (!strncmp(argv[i], "--version", 9) ||
1944 !strncmp(argv[i], "-v", 2)) {
1945 g_print("Claws Mail version " VERSION "\n");
1947 } else if (!strncmp(argv[i], "--status-full", 13)) {
1948 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1950 cmd.status_full = TRUE;
1951 while (p && *p != '\0' && *p != '-') {
1952 if (!cmd.status_full_folders) {
1953 cmd.status_full_folders =
1956 g_ptr_array_add(cmd.status_full_folders,
1959 p = (i+1 < argc)?argv[i+1]:NULL;
1961 } else if (!strncmp(argv[i], "--status", 8)) {
1962 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1965 while (p && *p != '\0' && *p != '-') {
1966 if (!cmd.status_folders)
1967 cmd.status_folders = g_ptr_array_new();
1968 g_ptr_array_add(cmd.status_folders,
1971 p = (i+1 < argc)?argv[i+1]:NULL;
1973 } else if (!strncmp(argv[i], "--search", 8)) {
1974 cmd.search_folder = (i+1 < argc)?argv[i+1]:NULL;
1975 cmd.search_type = (i+2 < argc)?argv[i+2]:NULL;
1976 cmd.search_request = (i+3 < argc)?argv[i+3]:NULL;
1977 const char* rec = (i+4 < argc)?argv[i+4]:NULL;
1978 cmd.search_recursive = TRUE;
1979 if (rec && (tolower(*rec)=='n' || tolower(*rec)=='f' || *rec=='0'))
1980 cmd.search_recursive = FALSE;
1981 if (cmd.search_folder && cmd.search_type && cmd.search_request)
1983 } else if (!strncmp(argv[i], "--online", 8)) {
1984 cmd.online_mode = ONLINE_MODE_ONLINE;
1985 } else if (!strncmp(argv[i], "--offline", 9)) {
1986 cmd.online_mode = ONLINE_MODE_OFFLINE;
1987 } else if (!strncmp(argv[i], "--help", 6) ||
1988 !strncmp(argv[i], "-h", 2)) {
1989 gchar *base = g_path_get_basename(argv[0]);
1990 g_print(_("Usage: %s [OPTION]...\n"), base);
1992 g_print("%s\n", _(" --compose [address] open composition window"));
1993 g_print("%s\n", _(" --compose-from-file file\n"
1994 " open composition window with data from given file;\n"
1995 " use - as file name for reading from standard input;\n"
1996 " content format: headers first (To: required) until an\n"
1997 " empty line, then mail body until end of file."));
1998 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
1999 g_print("%s\n", _(" --attach file1 [file2]...\n"
2000 " open composition window with specified files\n"
2002 g_print("%s\n", _(" --receive receive new messages"));
2003 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
2004 g_print("%s\n", _(" --search folder type request [recursive]\n"
2006 " folder ex.: \"#mh/Mailbox/inbox\" or \"Mail\"\n"
2007 " type: s[ubject],f[rom],t[o],e[xtended],m[ixed] or g: tag\n"
2008 " request: search string\n"
2009 " recursive: false if arg. starts with 0, n, N, f or F"));
2011 g_print("%s\n", _(" --send send all queued messages"));
2012 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
2013 g_print("%s\n", _(" --status-full [folder]...\n"
2014 " show the status of each folder"));
2015 g_print("%s\n", _(" --select folder[/msg] jumps to the specified folder/message\n"
2016 " folder is a folder id like 'folder/sub_folder'"));
2017 g_print("%s\n", _(" --online switch to online mode"));
2018 g_print("%s\n", _(" --offline switch to offline mode"));
2019 g_print("%s\n", _(" --exit --quit -q exit Claws Mail"));
2020 g_print("%s\n", _(" --debug debug mode"));
2021 g_print("%s\n", _(" --help -h display this help and exit"));
2022 g_print("%s\n", _(" --version -v output version information and exit"));
2023 g_print("%s\n", _(" --version-full -V output version and built-in features information and exit"));
2024 g_print("%s\n", _(" --config-dir output configuration directory"));
2025 g_print("%s\n", _(" --alternate-config-dir [dir]\n"
2026 " use specified configuration directory"));
2030 } else if (!strncmp(argv[i], "--crash", 7)) {
2032 cmd.crash_params = g_strdup((i+1 < argc)?argv[i+1]:NULL);
2034 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
2035 g_print(RC_DIR "\n");
2037 } else if (!strncmp(argv[i], "--alternate-config-dir", sizeof "--alternate-config-dir" - 1) && i+1 < argc) {
2038 set_rc_dir(argv[i+1]);
2039 } else if (!strncmp(argv[i], "--exit", 6) ||
2040 !strncmp(argv[i], "--quit", 6) ||
2041 !strncmp(argv[i], "-q", 2)) {
2043 } else if (!strncmp(argv[i], "--select", 8) && i+1 < argc) {
2044 cmd.target = argv[i+1];
2045 } else if (i == 1 && argc == 2) {
2046 /* only one parameter. Do something intelligent about it */
2047 if ((strstr(argv[i], "@")||!strncmp(argv[i], "mailto:", 7)) && !strstr(argv[i], "://")) {
2048 const gchar *p = argv[i];
2051 cmd.compose_mailto = NULL;
2052 if (p && *p != '\0' && *p != '-') {
2053 if (!strncmp(p, "mailto:", 7)) {
2054 cmd.compose_mailto = p + 7;
2056 cmd.compose_mailto = p;
2059 } else if (!strncmp(argv[i], "file://", 7)) {
2060 cmd.target = argv[i];
2061 } else if (!strncmp(argv[i], "?attach=file://", strlen("?attach=file://"))) {
2063 cmd.compose_mailto = argv[i];
2064 } else if (strstr(argv[i], "://")) {
2065 const gchar *p = argv[i];
2066 if (p && *p != '\0' && *p != '-') {
2067 cmd.subscribe = TRUE;
2068 cmd.subscribe_uri = p;
2070 } else if (!strcmp(argv[i], "--sync")) {
2072 } else if (is_dir_exist(argv[i]) || is_file_exist(argv[i])) {
2073 cmd.target = argv[i];
2075 g_print(_("Unknown option\n"));
2081 if (cmd.attach_files && cmd.compose == FALSE) {
2083 cmd.compose_mailto = NULL;
2087 static void initial_processing(FolderItem *item, gpointer data)
2089 MainWindow *mainwin = (MainWindow *)data;
2092 cm_return_if_fail(item);
2093 buf = g_strdup_printf(_("Processing (%s)..."),
2096 : _("top level folder"));
2100 if (item->prefs->enable_processing) {
2101 item->processing_pending = TRUE;
2102 folder_item_apply_processing(item);
2103 item->processing_pending = FALSE;
2106 STATUSBAR_POP(mainwin);
2109 static gboolean draft_all_messages(void)
2111 GList *compose_list = NULL;
2113 compose_clear_exit_drafts();
2114 compose_list = compose_get_compose_list();
2115 while (compose_list != NULL) {
2116 Compose *c = (Compose*)compose_list->data;
2117 if (!compose_draft(c, COMPOSE_DRAFT_FOR_EXIT))
2119 compose_list = compose_get_compose_list();
2123 gboolean clean_quit(gpointer data)
2125 static gboolean firstrun = TRUE;
2132 /*!< Good idea to have the main window stored in a
2133 * static variable so we can check that variable
2134 * to see if we're really allowed to do things
2135 * that actually the spawner is supposed to
2136 * do (like: sending mail, composing messages).
2137 * Because, really, if we're the spawnee, and
2138 * we touch GTK stuff, we're hosed. See the
2141 /* FIXME: Use something else to signal that we're
2142 * in the original spawner, and not in a spawned
2144 if (!static_mainwindow) {
2148 draft_all_messages();
2149 emergency_exit = TRUE;
2150 exit_claws(static_mainwindow);
2156 void app_will_exit(GtkWidget *widget, gpointer data)
2158 MainWindow *mainwin = data;
2160 if (gtk_main_level() == 0) {
2161 debug_print("not even started\n");
2164 if (sc_exiting == TRUE) {
2165 debug_print("exit pending\n");
2169 debug_print("exiting\n");
2170 if (compose_get_compose_list()) {
2171 if (!draft_all_messages()) {
2172 main_window_popup(mainwin);
2178 if (prefs_common.warn_queued_on_exit && procmsg_have_queued_mails_fast()) {
2179 if (alertpanel(_("Queued messages"),
2180 _("Some unsent messages are queued. Exit now?"),
2181 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL)
2182 != G_ALERTALTERNATE) {
2183 main_window_popup(mainwin);
2187 manage_window_focus_in(mainwin->window, NULL, NULL);
2191 #ifdef HAVE_VALGRIND
2192 if (RUNNING_ON_VALGRIND) {
2193 summary_clear_list(mainwin->summaryview);
2196 if (folderview_get_selected_item(mainwin->folderview))
2197 folder_item_close(folderview_get_selected_item(mainwin->folderview));
2201 gboolean claws_is_exiting(void)
2206 gboolean claws_is_starting(void)
2212 * CLAWS: want this public so crash dialog can delete the
2215 gchar *claws_get_socket_name(void)
2217 static gchar *filename = NULL;
2218 const gchar *socket_dir = NULL;
2220 if (rc_dir_is_alt())
2221 socket_dir = get_rc_dir();
2223 socket_dir = g_get_tmp_dir();
2224 if (filename == NULL) {
2225 filename = g_strdup_printf("%s%cclaws-mail-%d",
2226 socket_dir, G_DIR_SEPARATOR,
2237 static gchar *get_crashfile_name(void)
2239 static gchar *filename = NULL;
2241 if (filename == NULL) {
2242 filename = g_strdup_printf("%s%cclaws-crashed",
2243 get_tmp_dir(), G_DIR_SEPARATOR);
2249 static gint prohibit_duplicate_launch(void)
2255 path = claws_get_socket_name();
2256 uxsock = fd_connect_unix(path);
2258 if (x_display == NULL)
2259 x_display = g_strdup(g_getenv("DISPLAY"));
2263 return fd_open_unix(path);
2268 hmutex = CreateMutexA(NULL, FALSE, "ClawsMail");
2270 debug_print("cannot create Mutex\n");
2273 if (GetLastError() != ERROR_ALREADY_EXISTS) {
2274 uxsock = fd_open_inet(50216);
2280 uxsock = fd_connect_inet(50216);
2284 /* remote command mode */
2286 debug_print("another Claws Mail instance is already running.\n");
2288 if (cmd.receive_all) {
2289 fd_write_all(uxsock, "receive_all\n", 12);
2290 } else if (cmd.receive) {
2291 fd_write_all(uxsock, "receive\n", 8);
2292 } else if (cmd.compose && cmd.attach_files) {
2293 gchar *str, *compose_str;
2296 if (cmd.compose_mailto) {
2297 compose_str = g_strdup_printf("compose_attach %s\n",
2298 cmd.compose_mailto);
2300 compose_str = g_strdup("compose_attach\n");
2303 fd_write_all(uxsock, compose_str, strlen(compose_str));
2304 g_free(compose_str);
2306 for (i = 0; i < cmd.attach_files->len; i++) {
2307 str = g_ptr_array_index(cmd.attach_files, i);
2308 fd_write_all(uxsock, str, strlen(str));
2309 fd_write_all(uxsock, "\n", 1);
2312 fd_write_all(uxsock, ".\n", 2);
2313 } else if (cmd.compose) {
2316 if (cmd.compose_mailto) {
2317 compose_str = g_strdup_printf
2318 ("compose %s\n", cmd.compose_mailto);
2320 compose_str = g_strdup("compose\n");
2323 fd_write_all(uxsock, compose_str, strlen(compose_str));
2324 g_free(compose_str);
2325 } else if (cmd.subscribe) {
2326 gchar *str = g_strdup_printf("subscribe %s\n", cmd.subscribe_uri);
2327 fd_write_all(uxsock, str, strlen(str));
2329 } else if (cmd.send) {
2330 fd_write_all(uxsock, "send\n", 5);
2331 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
2332 fd_write(uxsock, "online\n", 6);
2333 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
2334 fd_write(uxsock, "offline\n", 7);
2335 } else if (cmd.status || cmd.status_full) {
2336 gchar buf[BUFFSIZE];
2338 const gchar *command;
2342 command = cmd.status_full ? "status-full\n" : "status\n";
2343 folders = cmd.status_full ? cmd.status_full_folders :
2346 fd_write_all(uxsock, command, strlen(command));
2347 for (i = 0; folders && i < folders->len; ++i) {
2348 folder = g_ptr_array_index(folders, i);
2349 fd_write_all(uxsock, folder, strlen(folder));
2350 fd_write_all(uxsock, "\n", 1);
2352 fd_write_all(uxsock, ".\n", 2);
2354 fd_gets(uxsock, buf, sizeof(buf));
2355 if (!strncmp(buf, ".\n", 2)) break;
2358 } else if (cmd.exit) {
2359 fd_write_all(uxsock, "exit\n", 5);
2360 } else if (cmd.target) {
2361 gchar *str = g_strdup_printf("select %s\n", cmd.target);
2362 fd_write_all(uxsock, str, strlen(str));
2364 } else if (cmd.search) {
2365 gchar buf[BUFFSIZE];
2367 g_strdup_printf("search %s\n%s\n%s\n%c\n",
2368 cmd.search_folder, cmd.search_type, cmd.search_request,
2369 (cmd.search_recursive==TRUE)?'1':'0');
2370 fd_write_all(uxsock, str, strlen(str));
2373 fd_gets(uxsock, buf, sizeof(buf));
2374 if (!strncmp(buf, ".\n", 2)) break;
2380 fd_write_all(uxsock, "get_display\n", 12);
2381 memset(buf, 0, sizeof(buf));
2382 fd_gets(uxsock, buf, sizeof(buf));
2383 if (strcmp2(buf, x_display)) {
2384 g_print("Claws Mail is already running on display %s.\n",
2388 uxsock = fd_connect_unix(path);
2389 fd_write_all(uxsock, "popup\n", 6);
2392 fd_write_all(uxsock, "popup\n", 6);
2400 static gint lock_socket_remove(void)
2405 if (lock_socket < 0) {
2409 if (lock_socket_tag > 0) {
2410 g_source_remove(lock_socket_tag);
2412 fd_close(lock_socket);
2415 filename = claws_get_socket_name();
2416 claws_unlink(filename);
2422 static GPtrArray *get_folder_item_list(gint sock)
2424 gchar buf[BUFFSIZE];
2426 GPtrArray *folders = NULL;
2429 fd_gets(sock, buf, sizeof(buf));
2430 if (!strncmp(buf, ".\n", 2)) {
2435 folders = g_ptr_array_new();
2437 item = folder_find_item_from_identifier(buf);
2439 g_ptr_array_add(folders, item);
2441 g_warning("no such folder: %s\n", buf);
2448 static void lock_socket_input_cb(gpointer data,
2450 GIOCondition condition)
2452 MainWindow *mainwin = (MainWindow *)data;
2454 gchar buf[BUFFSIZE];
2455 /* re-use the same quicksearch (& avoid matcher_list mem.leaks) */
2456 static QuickSearch *quicksearch = NULL;
2458 sock = fd_accept(source);
2459 fd_gets(sock, buf, sizeof(buf));
2461 if (!strncmp(buf, "popup", 5)) {
2462 main_window_popup(mainwin);
2463 } else if (!strncmp(buf, "get_display", 11)) {
2464 fd_write_all(sock, x_display, strlen(x_display));
2465 } else if (!strncmp(buf, "receive_all", 11)) {
2466 inc_all_account_mail(mainwin, FALSE,
2467 prefs_common.newmail_notify_manu);
2468 } else if (!strncmp(buf, "receive", 7)) {
2469 inc_mail(mainwin, prefs_common.newmail_notify_manu);
2470 } else if (!strncmp(buf, "compose_attach", 14)) {
2474 mailto = g_strdup(buf + strlen("compose_attach") + 1);
2475 files = g_ptr_array_new();
2476 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
2478 if (!strcmp2(buf, "."))
2480 g_ptr_array_add(files, g_strdup(buf));
2482 open_compose_new(mailto, files);
2483 ptr_array_free_strings(files);
2484 g_ptr_array_free(files, TRUE);
2486 } else if (!strncmp(buf, "compose", 7)) {
2487 open_compose_new(buf + strlen("compose") + 1, NULL);
2488 } else if (!strncmp(buf, "subscribe", 9)) {
2489 main_window_popup(mainwin);
2490 folder_subscribe(buf + strlen("subscribe") + 1);
2491 } else if (!strncmp(buf, "send", 4)) {
2493 } else if (!strncmp(buf, "online", 6)) {
2494 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
2495 } else if (!strncmp(buf, "offline", 7)) {
2496 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
2497 } else if (!strncmp(buf, "status-full", 11) ||
2498 !strncmp(buf, "status", 6)) {
2502 folders = get_folder_item_list(sock);
2503 status = folder_get_status
2504 (folders, !strncmp(buf, "status-full", 11));
2505 fd_write_all(sock, status, strlen(status));
2506 fd_write_all(sock, ".\n", 2);
2508 if (folders) g_ptr_array_free(folders, TRUE);
2509 } else if (!strncmp(buf, "select ", 7)) {
2510 const gchar *target = buf+7;
2511 mainwindow_jump_to(target, TRUE);
2512 } else if (!strncmp(buf, "search ", 7)) {
2513 FolderItem* folderItem = NULL;
2514 GSList *messages = NULL;
2515 gchar *folder_name, *request;
2516 QuickSearchType searchType = QUICK_SEARCH_EXTENDED;
2519 if (quicksearch==NULL)
2520 quicksearch = quicksearch_new_nogui();
2522 folder_name = g_strdup(buf+7);
2523 strretchomp(folder_name);
2525 if (fd_gets(sock, buf, sizeof(buf)) <= 0) {
2526 g_free(folder_name);
2529 searchType = quicksearch_type(buf);
2530 if (fd_gets(sock, buf, sizeof(buf)) <= 0) {
2531 g_free(folder_name);
2534 request = g_strdup(buf);
2535 strretchomp(request);
2538 if (fd_gets(sock, buf, sizeof(buf)) > 0)
2542 debug_print("search: %s %i %s %i\n",folder_name,searchType,request,recursive);
2545 folderItem = folder_find_item_from_identifier(folder_name);
2546 if (folder_name && folderItem == NULL) {
2547 debug_print("Unknow folder item : '%s', searching folder\n",folder_name);
2548 Folder* folder = folder_find_from_path(folder_name);
2550 folderItem = FOLDER_ITEM(folder->node->data);
2552 debug_print("Unknown folder: '%s'\n",folder_name);
2554 debug_print("%s %s\n",folderItem->name, folderItem->path);
2556 if (folderItem != NULL) {
2557 quicksearch_set(quicksearch, searchType, request);
2558 quicksearch_set_recursive(quicksearch, recursive);
2559 search_msgs_in_folders(&messages, quicksearch, folderItem);
2561 g_print("Folder '%s' not found.\n'", folder_name);
2565 for (cur=messages; cur != NULL; cur = cur->next) {
2566 MsgInfo* msg = (MsgInfo *)cur->data;
2567 gchar *file = procmsg_get_message_file_path(msg);
2568 fd_write_all(sock, file, strlen(file));
2569 fd_write_all(sock, "\n", 1);
2572 fd_write_all(sock, ".\n", 2);
2574 if (messages != NULL)
2575 procmsg_msg_list_free(messages);
2576 g_free(folder_name);
2578 } else if (!strncmp(buf, "exit", 4)) {
2579 if (prefs_common.clean_on_exit && !prefs_common.ask_on_clean) {
2580 procmsg_empty_all_trash();
2582 app_will_exit(NULL, mainwin);
2588 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
2593 Xstrdup_a(addr, address, return);
2597 compose_new(NULL, addr, attach_files);
2600 static void send_queue(void)
2603 gchar *errstr = NULL;
2604 gboolean error = FALSE;
2605 for (list = folder_get_list(); list != NULL; list = list->next) {
2606 Folder *folder = list->data;
2608 if (folder->queue) {
2609 gint res = procmsg_send_queue
2610 (folder->queue, prefs_common.savemsg,
2614 folder_item_scan(folder->queue);
2622 alertpanel_error_log(_("Some errors occurred "
2623 "while sending queued messages:\n%s"), errstr);
2626 alertpanel_error_log("Some errors occurred "
2627 "while sending queued messages.");
2631 static void quit_signal_handler(int sig)
2633 debug_print("Quitting on signal %d\n", sig);
2635 g_timeout_add(0, clean_quit, NULL);
2638 static void install_basic_sighandlers()
2642 struct sigaction act;
2647 sigaddset(&mask, SIGTERM);
2650 sigaddset(&mask, SIGINT);
2653 sigaddset(&mask, SIGHUP);
2656 act.sa_handler = quit_signal_handler;
2661 sigaction(SIGTERM, &act, 0);
2664 sigaction(SIGINT, &act, 0);
2667 sigaction(SIGHUP, &act, 0);
2670 sigprocmask(SIG_UNBLOCK, &mask, 0);
2671 #endif /* !G_OS_WIN32 */
2674 #if (defined linux && defined SIGIO)
2675 static int mem_notify_fd = 0;
2677 static gboolean clean_caches(gpointer unused)
2679 if (static_mainwindow && static_mainwindow->lock_count > 0)
2681 debug_print("/dev/mem_notify: callback: Freeing some memory now!\n");
2682 folder_clean_cache_memory_force();
2686 static void memory_signal_handler(int sig)
2688 debug_print("/dev/mem_notify: Kernel says we should free up some memory!\n");
2689 g_timeout_add(10, clean_caches, NULL);
2692 static void install_memory_sighandler()
2695 struct sigaction act;
2698 mem_notify_fd = g_open("/dev/mem_notify", O_RDONLY|O_NONBLOCK, 0);
2699 if (mem_notify_fd == -1) {
2700 debug_print("/dev/mem_notify not available (%s)\n",
2705 fcntl(mem_notify_fd, F_SETOWN, getpid());
2706 flags = fcntl(mem_notify_fd, F_GETFL);
2707 fcntl(mem_notify_fd, flags|FASYNC);
2711 sigaddset(&mask, SIGIO);
2713 act.sa_handler = memory_signal_handler;
2717 sigaction(SIGIO, &act, 0);
2719 sigprocmask(SIG_UNBLOCK, &mask, 0);
2721 debug_print("/dev/mem_notify: installed handler\n");
2723 #endif /* linux && SIGIO */
2726 osso_context_t *get_osso_context(void)
2728 return static_osso_context;
2733 #ifdef HAVE_NETWORKMANAGER_SUPPORT
2734 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
2737 MainWindow *mainWin;
2740 if (static_mainwindow)
2741 mainWin = static_mainwindow;
2743 mainWin = (MainWindow*)data;
2745 if (!prefs_common.use_networkmanager)
2753 online = networkmanager_is_online(&error);
2755 if(online && went_offline_nm) {
2756 went_offline_nm = FALSE;
2757 main_window_toggle_work_offline(mainWin, FALSE, FALSE);
2758 debug_print("NetworkManager: Went online\n");
2759 log_message(LOG_PROTOCOL, _("NetworkManager: network is online.\n"));
2762 went_offline_nm = TRUE;
2763 main_window_toggle_work_offline(mainWin, TRUE, FALSE);
2764 debug_print("NetworkManager: Went offline\n");
2765 log_message(LOG_PROTOCOL, _("NetworkManager: network is offline.\n"));
2769 debug_print("Failed to get online information from NetworkManager: %s\n",
2771 g_error_free(error);
2775 debug_print("NetworkManager: Cannot change connection state because "
2776 "main window does not exist\n");
2779 /* Returns true (and sets error appropriately, if given) in case of error */
2780 gboolean networkmanager_is_online(GError **error)
2782 DBusGConnection *connection;
2784 GError *tmp_error = NULL;
2788 if (!prefs_common.use_networkmanager)
2793 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &tmp_error);
2796 /* If calling code doesn't do error checking, at least print some debug */
2797 if((error == NULL) || (*error == NULL))
2798 debug_print("Failed to open connection to system bus: %s\n",
2799 tmp_error->message);
2800 g_propagate_error(error, tmp_error);
2804 proxy = dbus_g_proxy_new_for_name(connection,
2805 "org.freedesktop.NetworkManager",
2806 "/org/freedesktop/NetworkManager",
2807 "org.freedesktop.NetworkManager");
2809 retVal = dbus_g_proxy_call(proxy,"state",&tmp_error, G_TYPE_INVALID,
2810 G_TYPE_UINT, &state, G_TYPE_INVALID);
2813 g_object_unref(proxy);
2815 dbus_g_connection_unref(connection);
2818 /* If calling code doesn't do error checking, at least print some debug */
2819 if((error == NULL) || (*error == NULL))
2820 debug_print("Failed to get state info from NetworkManager: %s\n",
2821 tmp_error->message);
2822 g_propagate_error(error, tmp_error);
2825 #if NM_CHECK_VERSION(0,8,992)
2826 return (state == NM_STATE_CONNECTED_LOCAL ||
2827 state == NM_STATE_CONNECTED_SITE ||
2828 state == NM_STATE_CONNECTED_GLOBAL ||
2829 state == NM_STATE_UNKNOWN);
2831 return (state == NM_STATE_CONNECTED ||
2832 state == NM_STATE_UNKNOWN);