2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "claws-features.h"
28 #include <glib/gi18n.h>
38 #include <sys/types.h>
45 #include <X11/SM/SMlib.h>
53 #ifdef HAVE_STARTUP_NOTIFICATION
54 # define SN_API_NOT_YET_FROZEN
55 # include <libsn/sn-launchee.h>
56 # include <gdk/gdkx.h>
60 #include <dbus/dbus-glib.h>
62 #ifdef HAVE_NETWORKMANAGER_SUPPORT
63 #include <NetworkManager.h>
71 #include "mainwindow.h"
72 #include "folderview.h"
73 #include "image_viewer.h"
74 #include "summaryview.h"
75 #include "prefs_common.h"
76 #include "prefs_account.h"
77 #include "prefs_actions.h"
78 #include "prefs_ext_prog.h"
79 #include "prefs_fonts.h"
80 #include "prefs_image_viewer.h"
81 #include "prefs_message.h"
82 #include "prefs_receive.h"
83 #include "prefs_msg_colors.h"
84 #include "prefs_quote.h"
85 #include "prefs_spelling.h"
86 #include "prefs_summaries.h"
87 #include "prefs_themes.h"
88 #include "prefs_other.h"
89 #include "prefs_logging.h"
90 #include "prefs_send.h"
91 #include "prefs_wrapping.h"
92 #include "prefs_compose_writing.h"
93 #include "prefs_display_header.h"
99 #include "manage_window.h"
100 #include "alertpanel.h"
101 #include "statusbar.h"
102 #ifndef USE_NEW_ADDRBOOK
103 #include "addressbook.h"
105 #include "addressbook-dbus.h"
109 #include "folder_item_prefs.h"
112 #include "gtkutils.h"
115 #include "prefs_toolbar.h"
118 #include "imap_gtk.h"
119 #include "news_gtk.h"
124 #include "quicksearch.h"
125 #include "advsearch.h"
128 #include "imap-thread.h"
129 #include "nntp-thread.h"
131 #include "stock_pixmap.h"
144 #include <hildon/hildon-banner.h>
145 #include <hildon/hildon-program.h>
147 #include <hildon-widgets/hildon-banner.h>
148 #include <hildon-widgets/hildon-program.h>
151 #include <libgnomevfs/gnome-vfs-volume.h>
152 #include <libgnomevfs/gnome-vfs-volume-monitor.h>
153 #include <libgnomevfs/gnome-vfs-utils.h>
155 #define OSSO_NAME "claws_mail"
156 #define OSSO_SERVICE "com.nokia."OSSO_NAME
157 #define OSSO_OBJECT "/com/nokia/"OSSO_NAME
158 #define OSSO_IFACE "com.nokia."OSSO_NAME
160 typedef struct _AppData AppData;
162 HildonProgram *program;
163 HildonWindow *window;
164 osso_context_t *osso_context;
167 static GnomeVFSVolumeMonitor *volmon;
170 #ifdef HAVE_NETWORKMANAGER_SUPPORT
171 /* Went offline due to NetworkManager */
172 static gboolean went_offline_nm;
175 #if !defined(NM_CHECK_VERSION)
176 #define NM_CHECK_VERSION(x,y,z) 0
179 #ifdef HAVE_DBUS_GLIB
180 static DBusGProxy *awn_proxy = NULL;
187 HildonProgram *hildon_program;
190 #ifdef HAVE_STARTUP_NOTIFICATION
191 static SnLauncheeContext *sn_context = NULL;
192 static SnDisplay *sn_display = NULL;
195 static gint lock_socket = -1;
196 static gint lock_socket_tag = 0;
197 static gchar *x_display = NULL;
200 ONLINE_MODE_DONT_CHANGE,
205 static struct RemoteCmd {
207 gboolean receive_all;
208 gboolean cancel_receiving;
209 gboolean cancel_sending;
211 const gchar *compose_mailto;
214 const gchar *search_folder;
215 const gchar *search_type;
216 const gchar *search_request;
217 gboolean search_recursive;
219 gboolean status_full;
221 gboolean reset_statistics;
222 GPtrArray *status_folders;
223 GPtrArray *status_full_folders;
230 const gchar *subscribe_uri;
234 SessionStats session_stats;
236 static void reset_statistics(void);
238 static void parse_cmd_opt(int argc, char *argv[]);
240 static gint prohibit_duplicate_launch (void);
241 static gchar * get_crashfile_name (void);
242 static gint lock_socket_remove (void);
243 static void lock_socket_input_cb (gpointer data,
245 GIOCondition condition);
247 static void open_compose_new (const gchar *address,
248 GList *attach_files);
250 static void send_queue (void);
251 static void initial_processing (FolderItem *item, gpointer data);
252 static void quit_signal_handler (int sig);
253 static void install_basic_sighandlers (void);
254 #if (defined linux && defined SIGIO)
255 static void install_memory_sighandler (void);
257 static void exit_claws (MainWindow *mainwin);
259 #ifdef HAVE_NETWORKMANAGER_SUPPORT
260 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
264 #define MAKE_DIR_IF_NOT_EXIST(dir) \
266 if (!is_dir_exist(dir)) { \
267 if (is_file_exist(dir)) { \
269 (_("File '%s' already exists.\n" \
270 "Can't create folder."), \
274 if (make_dir(dir) < 0) \
279 static MainWindow *static_mainwindow;
282 static osso_context_t *static_osso_context;
284 void exit_event_handler(gboolean die_now, gpointer data)
287 appdata = (AppData *) data;
288 /* Do whatever application needs to do before exiting */
289 exit_claws(static_mainwindow);
290 hildon_banner_show_information(GTK_WIDGET(appdata->window), NULL,
295 /* Callback for hardware D-BUS events */
296 void hw_event_handler(osso_hw_state_t *state, gpointer data)
299 appdata = (AppData *) data;
301 if (state->shutdown_ind) {
302 exit_claws(static_mainwindow);
303 hildon_banner_show_information(GTK_WIDGET(appdata->window), NULL,
308 /* Callback for normal D-BUS messages */
309 gint dbus_req_handler(const gchar * interface, const gchar * method,
310 GArray * arguments, gpointer data,
314 appdata = (AppData *) data;
316 if (!strcmp(method, "top_application")) {
317 osso_rpc_free_val(retval);
320 osso_system_note_infoprint(appdata->osso_context, method, retval);
321 osso_rpc_free_val(retval);
326 static gboolean emergency_exit = FALSE;
328 #ifdef HAVE_STARTUP_NOTIFICATION
329 static void sn_error_trap_push(SnDisplay *display, Display *xdisplay)
331 gdk_error_trap_push();
334 static void sn_error_trap_pop(SnDisplay *display, Display *xdisplay)
336 gdk_error_trap_pop();
339 static void startup_notification_complete(gboolean with_window)
342 GtkWidget *hack = NULL;
345 /* this is needed to make the startup notification leave,
346 * if we have been launched from a menu.
347 * We have to display a window, so let it be very little */
348 hack = gtk_window_new(GTK_WINDOW_POPUP);
349 gtk_window_move(GTK_WINDOW(hack), 0, 0);
350 gtk_widget_set_size_request(hack, 1, 1);
351 gtk_widget_show(hack);
354 xdisplay = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
355 sn_display = sn_display_new(xdisplay,
358 sn_context = sn_launchee_context_new_from_environment(sn_display,
359 DefaultScreen(xdisplay));
361 if (sn_context != NULL) {
362 sn_launchee_context_complete(sn_context);
363 sn_launchee_context_unref(sn_context);
364 sn_display_unref(sn_display);
367 gtk_widget_destroy(hack);
370 #endif /* HAVE_STARTUP_NOTIFICATION */
372 static void claws_gtk_idle(void)
374 while(gtk_events_pending()) {
375 gtk_main_iteration();
380 static gboolean sc_starting = FALSE;
382 static gboolean defer_check_all(void *data)
384 gboolean autochk = GPOINTER_TO_INT(data);
386 inc_all_account_mail(static_mainwindow, autochk,
387 prefs_common.newmail_notify_manu);
389 /* Accounts using a specific timer need to be checked separately
392 inc_all_account_spec_timer_check_mail();
396 main_window_set_menu_sensitive(static_mainwindow);
397 toolbar_main_set_sensitive(static_mainwindow);
402 static gboolean defer_check(void *data)
404 inc_mail(static_mainwindow, prefs_common.newmail_notify_manu);
408 main_window_set_menu_sensitive(static_mainwindow);
409 toolbar_main_set_sensitive(static_mainwindow);
414 static gboolean defer_jump(void *data)
416 if (cmd.receive_all) {
417 defer_check_all(GINT_TO_POINTER(FALSE));
418 } else if (prefs_common.chk_on_startup) {
419 defer_check_all(GINT_TO_POINTER(TRUE));
420 } else if (cmd.receive) {
423 mainwindow_jump_to(data, FALSE);
426 main_window_set_menu_sensitive(static_mainwindow);
427 toolbar_main_set_sensitive(static_mainwindow);
432 static void chk_update_val(GtkWidget *widget, gpointer data)
434 gboolean *val = (gboolean *)data;
435 *val = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
438 static gboolean migrate_old_config(const gchar *old_cfg_dir, const gchar *new_cfg_dir, const gchar *oldversion)
440 gchar *message = g_strdup_printf(_("Configuration for %s found.\n"
441 "Do you want to migrate this configuration?"), oldversion);
442 gchar *message2 = g_strdup_printf(_("\n\nYour Sylpheed filtering rules can be converted by a\n"
443 "script available at %s."), TOOLS_URI);
445 if (!strcmp(oldversion, "Sylpheed"))
446 message = g_strconcat(message, message2, NULL);
450 GtkWidget *window = NULL;
451 GtkWidget *keep_backup_chk;
452 gboolean backup = TRUE;
454 keep_backup_chk = gtk_check_button_new_with_label (_("Keep old configuration"));
455 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_backup_chk), TRUE);
456 CLAWS_SET_TIP(keep_backup_chk,
457 _("Keeping a backup will allow you to go back to an "
458 "older version, but may take a while if you have "
459 "cached IMAP or News data, and will take some extra "
460 "room on your disk."));
462 g_signal_connect(G_OBJECT(keep_backup_chk), "toggled",
463 G_CALLBACK(chk_update_val), &backup);
465 if (alertpanel_full(_("Migration of configuration"), message,
466 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL, FALSE,
467 keep_backup_chk, ALERT_QUESTION, G_ALERTDEFAULT) != G_ALERTALTERNATE) {
471 /* we can either do a fast migration requiring not any extra disk
472 * space, or a slow one that copies the old configuration and leaves
476 window = label_window_create(_("Copying configuration... This may take a while..."));
479 r = copy_dir(old_cfg_dir, new_cfg_dir);
480 label_window_destroy(window);
482 /* if copy failed, we'll remove the partially copied
485 alertpanel_error(_("Migration failed!"));
486 remove_dir_recursive(new_cfg_dir);
489 /* fast mode failed, but we don't want backup */
490 remove_dir_recursive(old_cfg_dir);
494 window = label_window_create(_("Migrating configuration..."));
497 r = g_rename(old_cfg_dir, new_cfg_dir);
498 label_window_destroy(window);
500 /* if g_rename failed, we'll try to copy */
502 FILE_OP_ERROR(new_cfg_dir, "g_rename failed, trying copy\n");
509 static int migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
512 gchar *plugin_path, *old_plugin_path, *new_plugin_path;
514 gboolean err = FALSE;
516 oldfp = g_fopen(old_rc, "r");
519 newfp = g_fopen(new_rc, "w");
525 plugin_path = g_strdup(get_plugin_dir());
526 new_plugin_path = g_strdup(plugin_path);
528 if (strstr(plugin_path, "/claws-mail/")) {
529 gchar *end = g_strdup(strstr(plugin_path, "/claws-mail/")+strlen("/claws-mail/"));
530 *(strstr(plugin_path, "/claws-mail/")) = '\0';
531 old_plugin_path = g_strconcat(plugin_path, "/sylpheed-claws/", end, NULL);
534 old_plugin_path = g_strdup(new_plugin_path);
536 debug_print("replacing %s with %s\n", old_plugin_path, new_plugin_path);
537 while (fgets(buf, sizeof(buf), oldfp)) {
538 if (strncmp(buf, old_plugin_path, strlen(old_plugin_path))) {
539 err |= (fputs(buf, newfp) == EOF);
541 debug_print("->replacing %s", buf);
542 debug_print(" with %s%s", new_plugin_path, buf+strlen(old_plugin_path));
543 err |= (fputs(new_plugin_path, newfp) == EOF);
544 err |= (fputs(buf+strlen(old_plugin_path), newfp) == EOF);
548 g_free(new_plugin_path);
549 g_free(old_plugin_path);
551 if (fclose(newfp) == EOF)
559 sc_client_set_value (MainWindow *mainwin,
570 prop.num_vals = num_vals;
574 if (mainwin->smc_conn)
575 SmcSetProperties ((SmcConn) mainwin->smc_conn, 1, proplist);
578 static void sc_die_callback (SmcConn smc_conn, SmPointer client_data)
583 static void sc_save_complete_callback(SmcConn smc_conn, SmPointer client_data)
587 static void sc_shutdown_cancelled_callback (SmcConn smc_conn, SmPointer client_data)
589 MainWindow *mainwin = (MainWindow *)client_data;
590 if (mainwin->smc_conn)
591 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
594 static void sc_save_yourself_callback (SmcConn smc_conn,
595 SmPointer client_data,
601 MainWindow *mainwin = (MainWindow *)client_data;
602 if (mainwin->smc_conn)
603 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
606 static IceIOErrorHandler sc_ice_installed_handler;
608 static void sc_ice_io_error_handler (IceConn connection)
610 if (sc_ice_installed_handler)
611 (*sc_ice_installed_handler) (connection);
613 static gboolean sc_process_ice_messages (GIOChannel *source,
614 GIOCondition condition,
617 IceConn connection = (IceConn) data;
618 IceProcessMessagesStatus status;
620 status = IceProcessMessages (connection, NULL, NULL);
622 if (status == IceProcessMessagesIOError) {
623 IcePointer context = IceGetConnectionContext (connection);
625 if (context && G_IS_OBJECT(context)) {
626 guint disconnect_id = g_signal_lookup ("disconnect", G_OBJECT_TYPE (context));
628 if (disconnect_id > 0)
629 g_signal_emit (context, disconnect_id, 0);
631 IceSetShutdownNegotiation (connection, False);
632 IceCloseConnection (connection);
639 static void new_ice_connection (IceConn connection, IcePointer client_data, Bool opening,
640 IcePointer *watch_data)
646 /* Make sure we don't pass on these file descriptors to any
648 fcntl(IceConnectionNumber(connection),F_SETFD,
649 fcntl(IceConnectionNumber(connection),F_GETFD,0) | FD_CLOEXEC);
651 channel = g_io_channel_unix_new (IceConnectionNumber (connection));
652 input_id = g_io_add_watch (channel,
653 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
654 sc_process_ice_messages,
656 g_io_channel_unref (channel);
658 *watch_data = (IcePointer) GUINT_TO_POINTER (input_id);
660 input_id = GPOINTER_TO_UINT ((gpointer) *watch_data);
661 g_source_remove (input_id);
665 static void sc_session_manager_connect(MainWindow *mainwin)
667 static gboolean connected = FALSE;
668 SmcCallbacks callbacks;
670 IceIOErrorHandler default_handler;
677 sc_ice_installed_handler = IceSetIOErrorHandler (NULL);
678 default_handler = IceSetIOErrorHandler (sc_ice_io_error_handler);
680 if (sc_ice_installed_handler == default_handler)
681 sc_ice_installed_handler = NULL;
683 IceAddConnectionWatch (new_ice_connection, NULL);
686 callbacks.save_yourself.callback = sc_save_yourself_callback;
687 callbacks.die.callback = sc_die_callback;
688 callbacks.save_complete.callback = sc_save_complete_callback;
689 callbacks.shutdown_cancelled.callback = sc_shutdown_cancelled_callback;
691 callbacks.save_yourself.client_data =
692 callbacks.die.client_data =
693 callbacks.save_complete.client_data =
694 callbacks.shutdown_cancelled.client_data = (SmPointer) mainwin;
695 if (g_getenv ("SESSION_MANAGER")) {
696 gchar error_string_ret[256] = "";
698 mainwin->smc_conn = (gpointer)
699 SmcOpenConnection (NULL, mainwin,
700 SmProtoMajor, SmProtoMinor,
701 SmcSaveYourselfProcMask | SmcDieProcMask |
702 SmcSaveCompleteProcMask |
703 SmcShutdownCancelledProcMask,
706 256, error_string_ret);
708 if (error_string_ret[0] || mainwin->smc_conn == NULL)
709 g_warning ("While connecting to session manager:\n%s.",
713 vals = g_new (SmPropValue, 1);
714 vals[0].length = strlen(argv0);
715 vals[0].value = argv0;
716 sc_client_set_value (mainwin, SmCloneCommand, SmLISTofARRAY8, 1, vals);
717 sc_client_set_value (mainwin, SmRestartCommand, SmLISTofARRAY8, 1, vals);
718 sc_client_set_value (mainwin, SmProgram, SmARRAY8, 1, vals);
720 vals[0].length = strlen(g_get_user_name()?g_get_user_name():"");
721 vals[0].value = g_strdup(g_get_user_name()?g_get_user_name():"");
722 sc_client_set_value (mainwin, SmUserID, SmARRAY8, 1, vals);
728 static gboolean sc_exiting = FALSE;
729 static gboolean show_at_startup = TRUE;
730 static gboolean claws_crashed_bool = FALSE;
732 gboolean claws_crashed(void) {
733 return claws_crashed_bool;
736 void main_set_show_at_startup(gboolean show)
738 show_at_startup = show;
742 static void main_vol_mount_cb(GnomeVFSVolumeMonitor *vfs, GnomeVFSVolume *vol, MainWindow *mainwin)
744 gchar *uri = gnome_vfs_volume_get_activation_uri (vol);
745 gchar *mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
748 if(!strcmp(mount_path, prefs_common.data_root)) {
749 gtk_widget_set_sensitive(mainwin->window, TRUE);
755 static void main_vol_unmount_cb(GnomeVFSVolumeMonitor *vfs, GnomeVFSVolume *vol, MainWindow *mainwin)
757 gchar *uri = gnome_vfs_volume_get_activation_uri (vol);
758 gchar *mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
761 if(!strcmp(mount_path, prefs_common.data_root)) {
762 gtk_widget_set_sensitive(mainwin->window, FALSE);
771 static FILE* win32_debug_fp=NULL;
772 static guint win32_log_handler_app_id;
773 static guint win32_log_handler_glib_id;
774 static guint win32_log_handler_gtk_id;
776 static void win32_print_stdout(const gchar *string)
778 if (win32_debug_fp) {
779 fprintf(win32_debug_fp, string);
780 fflush(win32_debug_fp);
784 static void win32_print_stderr(const gchar *string)
786 if (win32_debug_fp) {
787 fprintf(win32_debug_fp, string);
788 fflush(win32_debug_fp);
792 static void win32_log(const gchar *log_domain, GLogLevelFlags log_level, const gchar* message, gpointer user_data)
794 if (win32_debug_fp) {
797 switch(log_level & G_LOG_LEVEL_MASK)
799 case G_LOG_LEVEL_ERROR:
802 case G_LOG_LEVEL_CRITICAL:
805 case G_LOG_LEVEL_WARNING:
808 case G_LOG_LEVEL_MESSAGE:
811 case G_LOG_LEVEL_INFO:
814 case G_LOG_LEVEL_DEBUG:
821 fprintf(win32_debug_fp, "%s: %s: %s", log_domain, type, message);
823 fprintf(win32_debug_fp, "%s: %s", type, message);
824 fflush(win32_debug_fp);
828 static void win32_open_log(void)
830 gchar *logfile = g_strconcat(g_get_tmp_dir(), G_DIR_SEPARATOR_S, "claws-win32.log", NULL);
831 gchar *oldlogfile = g_strconcat(g_get_tmp_dir(), G_DIR_SEPARATOR_S, "claws-win32.log.bak", NULL);
833 if (is_file_exist(logfile)) {
834 if (rename_force(logfile, oldlogfile) < 0)
835 FILE_OP_ERROR(logfile, "rename");
837 win32_debug_fp = g_fopen(logfile, "w");
842 g_set_print_handler(win32_print_stdout);
843 g_set_printerr_handler(win32_print_stdout);
844 win32_log_handler_app_id = g_log_set_handler(NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
845 | G_LOG_FLAG_RECURSION, win32_log, NULL);
846 win32_log_handler_glib_id = g_log_set_handler("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
847 | G_LOG_FLAG_RECURSION, win32_log, NULL);
848 win32_log_handler_gtk_id = g_log_set_handler("Gtk", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
849 | G_LOG_FLAG_RECURSION, win32_log, NULL);
853 static void win32_close_log(void)
857 g_log_remove_handler("", win32_log_handler_app_id);
858 g_log_remove_handler("GLib", win32_log_handler_glib_id);
859 g_log_remove_handler("Gtk", win32_log_handler_gtk_id);
860 fclose(win32_debug_fp);
866 static void main_dump_features_list(gboolean show_debug_only)
867 /* display compiled-in features list */
869 if (show_debug_only && !debug_get_mode())
873 debug_print("runtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
874 gtk_major_version, gtk_minor_version, gtk_micro_version,
875 glib_major_version, glib_minor_version, glib_micro_version);
877 g_print("runtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
878 gtk_major_version, gtk_minor_version, gtk_micro_version,
879 glib_major_version, glib_minor_version, glib_micro_version);
881 debug_print("buildtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
882 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
883 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
885 g_print("buildtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
886 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
887 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
890 debug_print("Compiled-in features:\n");
892 g_print("Compiled-in features:\n");
895 debug_print(" compface\n");
897 g_print(" compface\n");
901 debug_print(" aspell\n");
903 g_print(" aspell\n");
907 debug_print(" gnutls\n");
909 g_print(" gnutls\n");
913 debug_print(" ipv6\n");
919 debug_print(" iconv\n");
925 debug_print(" jpilot\n");
927 g_print(" jpilot\n");
931 debug_print(" ldap\n");
937 debug_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
939 g_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
943 debug_print(" libsm\n");
947 #if HAVE_NETWORKMANAGER_SUPPORT
949 debug_print(" NetworkManager\n");
951 g_print(" NetworkManager\n");
955 #ifdef HAVE_DBUS_GLIB
956 static guint dbus_item_hook_id = -1;
957 static guint dbus_folder_hook_id = -1;
959 static void uninstall_dbus_status_handler(void)
962 g_object_unref(awn_proxy);
964 if (dbus_item_hook_id != -1)
965 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, dbus_item_hook_id);
966 if (dbus_folder_hook_id != -1)
967 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, dbus_folder_hook_id);
970 static void dbus_update(FolderItem *removed_item)
972 guint new, unread, unreadmarked, marked, total;
973 guint replied, forwarded, locked, ignored, watched;
975 GError *error = NULL;
977 folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total,
978 &replied, &forwarded, &locked, &ignored,
981 total -= removed_item->total_msgs;
982 new -= removed_item->new_msgs;
983 unread -= removed_item->unread_msgs;
987 buf = g_strdup_printf("%d", new);
988 dbus_g_proxy_call(awn_proxy, "SetInfoByName", &error,
989 G_TYPE_STRING, "claws-mail",
991 G_TYPE_INVALID, G_TYPE_INVALID);
995 dbus_g_proxy_call(awn_proxy, "UnsetInfoByName", &error, G_TYPE_STRING,
996 "claws-mail", G_TYPE_INVALID, G_TYPE_INVALID);
999 debug_print("%s\n", error->message);
1000 g_error_free(error);
1004 static gboolean dbus_status_update_folder_hook(gpointer source, gpointer data)
1006 FolderUpdateData *hookdata;
1008 if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
1009 dbus_update(hookdata->item);
1016 static gboolean dbus_status_update_item_hook(gpointer source, gpointer data)
1023 static void install_dbus_status_handler(void)
1025 GError *tmp_error = NULL;
1026 DBusGConnection *connection = dbus_g_bus_get(DBUS_BUS_SESSION, &tmp_error);
1029 /* If calling code doesn't do error checking, at least print some debug */
1030 debug_print("Failed to open connection to session bus: %s\n",
1031 tmp_error->message);
1032 g_error_free(tmp_error);
1035 awn_proxy = dbus_g_proxy_new_for_name(connection,
1036 "com.google.code.Awn",
1037 "/com/google/code/Awn",
1038 "com.google.code.Awn");
1039 dbus_item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, dbus_status_update_item_hook, NULL);
1040 if (dbus_item_hook_id == -1) {
1041 g_warning(_("Failed to register folder item update hook"));
1042 uninstall_dbus_status_handler();
1046 dbus_folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, dbus_status_update_folder_hook, NULL);
1047 if (dbus_folder_hook_id == -1) {
1048 g_warning(_("Failed to register folder update hook"));
1049 uninstall_dbus_status_handler();
1055 static void reset_statistics(void)
1057 /* (re-)initialize session statistics */
1058 session_stats.received = 0;
1059 session_stats.sent = 0;
1060 session_stats.replied = 0;
1061 session_stats.forwarded = 0;
1062 session_stats.time_started = time(NULL);
1065 int main(int argc, char *argv[])
1068 osso_context_t *osso_context;
1069 osso_return_t result;
1071 #ifdef HAVE_DBUS_GLIB
1072 DBusGConnection *connection;
1075 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1076 DBusGProxy *nm_proxy;
1079 MainWindow *mainwin;
1080 FolderView *folderview;
1082 gboolean crash_file_present = FALSE;
1083 gint num_folder_class = 0;
1084 gboolean asked_for_migration = FALSE;
1085 gboolean start_done = TRUE;
1086 GSList *plug_list = NULL;
1087 gboolean never_ran = FALSE;
1088 gboolean mainwin_shown = FALSE;
1090 START_TIMING("startup");
1097 if (!claws_init(&argc, &argv)) {
1104 prog_version = PROG_VERSION;
1105 argv0 = g_strdup(argv[0]);
1107 parse_cmd_opt(argc, argv);
1111 /* check and create unix domain socket for remote operation */
1112 lock_socket = prohibit_duplicate_launch();
1113 if (lock_socket < 0) {
1114 #ifdef HAVE_STARTUP_NOTIFICATION
1115 if(gtk_init_check(&argc, &argv))
1116 startup_notification_complete(TRUE);
1121 main_dump_features_list(TRUE);
1122 prefs_prepare_cache();
1126 #if !GTK_CHECK_VERSION(3, 0, 0)
1129 gtk_init(&argc, &argv);
1130 crash_main(cmd.crash_params);
1136 crash_install_handlers();
1138 install_basic_sighandlers();
1139 #if (defined linux && defined SIGIO)
1140 install_memory_sighandler();
1143 if (cmd.status || cmd.status_full || cmd.search ||
1144 cmd.statistics || cmd.reset_statistics ||
1145 cmd.cancel_receiving || cmd.cancel_sending) {
1146 puts("0 Claws Mail not running.");
1147 lock_socket_remove();
1153 if (!g_thread_supported())
1154 g_thread_init(NULL);
1158 #if !GTK_CHECK_VERSION(3, 0, 0)
1161 gtk_init(&argc, &argv);
1164 gtk_settings_set_string_property(gtk_settings_get_default(),
1170 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1171 went_offline_nm = FALSE;
1174 #ifdef HAVE_DBUS_GLIB
1176 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
1179 debug_print("Failed to open connection to system bus: %s\n", error->message);
1180 g_error_free(error);
1183 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1184 nm_proxy = dbus_g_proxy_new_for_name(connection,
1185 "org.freedesktop.NetworkManager",
1186 "/org/freedesktop/NetworkManager",
1187 "org.freedesktop.NetworkManager");
1189 #if NM_CHECK_VERSION(0,8,992)
1190 dbus_g_proxy_add_signal(nm_proxy, "StateChanged", G_TYPE_UINT, G_TYPE_INVALID);
1191 dbus_g_proxy_connect_signal(nm_proxy, "StateChanged",
1192 G_CALLBACK(networkmanager_state_change_cb),
1195 dbus_g_proxy_add_signal(nm_proxy, "StateChange", G_TYPE_UINT, G_TYPE_INVALID);
1196 dbus_g_proxy_connect_signal(nm_proxy, "StateChange",
1197 G_CALLBACK(networkmanager_state_change_cb),
1202 install_dbus_status_handler();
1208 osso_context = osso_initialize(OSSO_SERVICE, "2.8.1", TRUE, NULL);
1209 if (osso_context == NULL) {
1212 hildon_program = HILDON_PROGRAM(hildon_program_get_instance());
1213 static_osso_context = osso_context;
1215 #if !GTK_CHECK_VERSION(3, 0, 0)
1216 gtk_widget_set_default_colormap(
1217 gdk_screen_get_system_colormap(
1218 gdk_screen_get_default()));
1221 gtkut_create_ui_manager();
1223 /* Create container for all the menus we will be adding */
1224 MENUITEM_ADDUI("/", "Menus", NULL, GTK_UI_MANAGER_MENUBAR);
1226 if (!g_thread_supported()) {
1227 g_error(_("g_thread is not supported by glib.\n"));
1231 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1233 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1236 /* no config dir exists. See if we can migrate an old config. */
1237 if (!is_dir_exist(RC_DIR)) {
1238 prefs_destroy_cache();
1241 /* if one of the old dirs exist, we'll ask if the user
1242 * want to migrates, and r will be TRUE if he said yes
1243 * and migration succeeded, and FALSE otherwise.
1245 if (is_dir_exist(OLD_GTK2_RC_DIR)) {
1246 r = migrate_old_config(OLD_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 2.6.0 (or older)"));
1247 asked_for_migration = TRUE;
1248 } else if (is_dir_exist(OLDER_GTK2_RC_DIR)) {
1249 r = migrate_old_config(OLDER_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.9.15 (or older)"));
1250 asked_for_migration = TRUE;
1251 } else if (is_dir_exist(OLD_GTK1_RC_DIR)) {
1252 r = migrate_old_config(OLD_GTK1_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.0.5 (or older)"));
1253 asked_for_migration = TRUE;
1254 } else if (is_dir_exist(SYLPHEED_RC_DIR)) {
1255 r = migrate_old_config(SYLPHEED_RC_DIR, RC_DIR, "Sylpheed");
1256 asked_for_migration = TRUE;
1259 /* If migration failed or the user didn't want to do it,
1260 * we create a new one (and we'll hit wizard later).
1262 if (r == FALSE && !is_dir_exist(RC_DIR)) {
1264 if (copy_dir(SYSCONFDIR "/skel/.claws-mail", RC_DIR) < 0) {
1266 if (!is_dir_exist(RC_DIR) && make_dir(RC_DIR) < 0) {
1279 if (!is_file_exist(RC_DIR G_DIR_SEPARATOR_S COMMON_RC) &&
1280 is_file_exist(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC)) {
1281 /* post 2.6 name change */
1282 migrate_common_rc(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC,
1283 RC_DIR G_DIR_SEPARATOR_S COMMON_RC);
1287 plugin_load_all("Common");
1289 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
1290 gtk_rc_parse(userrc);
1293 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1294 gtk_accel_map_load (userrc);
1298 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_rc_dir(), 1, win32_close_log(););
1300 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
1303 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
1304 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
1305 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
1306 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
1307 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
1308 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR);
1310 crash_file_present = is_file_exist(get_crashfile_name());
1311 /* remove temporary files */
1312 remove_all_files(get_tmp_dir());
1313 remove_all_files(get_mime_tmp_dir());
1315 if (!cmd.crash && crash_file_present)
1316 claws_crashed_bool = TRUE;
1318 if (is_file_exist("claws.log")) {
1319 if (rename_force("claws.log", "claws.log.bak") < 0)
1320 FILE_OP_ERROR("claws.log", "rename");
1322 set_log_file(LOG_PROTOCOL, "claws.log");
1324 if (is_file_exist("filtering.log")) {
1325 if (rename_force("filtering.log", "filtering.log.bak") < 0)
1326 FILE_OP_ERROR("filtering.log", "rename");
1328 set_log_file(LOG_DEBUG_FILTERING, "filtering.log");
1331 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1333 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1336 folder_system_init();
1337 prefs_common_read_config();
1339 prefs_themes_init();
1341 prefs_ext_prog_init();
1342 prefs_wrapping_init();
1343 prefs_compose_writing_init();
1344 prefs_msg_colors_init();
1345 image_viewer_init();
1346 prefs_image_viewer_init();
1348 prefs_summaries_init();
1349 prefs_message_init();
1351 prefs_logging_init();
1352 prefs_receive_init();
1357 gtkaspell_checkers_init();
1358 prefs_spelling_init();
1361 sock_set_io_timeout(prefs_common.io_timeout_secs);
1362 prefs_actions_read_config();
1363 prefs_display_header_read_config();
1364 /* prefs_filtering_read_config(); */
1365 #ifndef USE_NEW_ADDRBOOK
1366 addressbook_read_file();
1368 g_clear_error(&error);
1369 if (! addressbook_start_service(&error)) {
1370 g_warning("%s", error->message);
1371 g_clear_error(&error);
1374 addressbook_install_hooks(&error);
1377 gtkut_widget_init();
1378 stock_pixbuf_gdk(NULL, STOCK_PIXMAP_CLAWS_MAIL_ICON, &icon);
1379 gtk_window_set_default_icon(icon);
1381 folderview_initialize();
1387 mainwin = main_window_create();
1389 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1390 networkmanager_state_change_cb(nm_proxy,NULL,mainwin);
1395 appdata = g_new0(AppData, 1);
1396 appdata->program = hildon_program;
1397 appdata->window = HILDON_WINDOW(mainwin->window);
1398 appdata->osso_context = osso_context;
1399 result = osso_rpc_set_cb_f(appdata->osso_context,
1403 dbus_req_handler, appdata);
1404 if (result != OSSO_OK) {
1409 /* Add handler for Exit D-BUS messages */
1410 result = osso_application_set_exit_cb(appdata->osso_context,
1412 (gpointer) appdata);
1413 if (result != OSSO_OK) {
1417 osso_hw_set_event_cb( appdata->osso_context,
1418 NULL, hw_event_handler, (gpointer) appdata );
1420 manage_window_focus_in(mainwin->window, NULL, NULL);
1421 folderview = mainwin->folderview;
1423 gtk_cmclist_freeze(GTK_CMCLIST(mainwin->folderview->ctree));
1424 folder_item_update_freeze();
1426 prefs_account_init();
1427 account_read_config_all();
1429 #ifdef HAVE_LIBETPAN
1430 imap_main_init(prefs_common.skip_ssl_cert_check);
1431 imap_main_set_timeout(prefs_common.io_timeout_secs);
1432 nntp_main_init(prefs_common.skip_ssl_cert_check);
1434 /* If we can't read a folder list or don't have accounts,
1435 * it means the configuration's not done. Either this is
1436 * a brand new install, either a failed/refused migration.
1437 * So we'll start the wizard.
1439 if (folder_read_list() < 0) {
1440 prefs_destroy_cache();
1442 /* if run_wizard returns FALSE it's because it's
1443 * been cancelled. We can't do much but exit.
1444 * however, if the user was asked for a migration,
1445 * we remove the newly created directory so that
1446 * he's asked again for migration on next launch.*/
1447 if (!run_wizard(mainwin, TRUE)) {
1448 if (asked_for_migration)
1449 remove_dir_recursive(RC_DIR);
1455 main_window_reflect_prefs_all_now();
1456 folder_write_list();
1460 if (!account_get_list()) {
1461 prefs_destroy_cache();
1462 if (!run_wizard(mainwin, FALSE)) {
1463 if (asked_for_migration)
1464 remove_dir_recursive(RC_DIR);
1470 if(!account_get_list()) {
1471 exit_claws(mainwin);
1481 toolbar_main_set_sensitive(mainwin);
1482 main_window_set_menu_sensitive(mainwin);
1484 /* if crashed, show window early so that the user
1485 * sees what's happening */
1486 if (claws_crashed()) {
1487 main_window_popup(mainwin);
1488 mainwin_shown = TRUE;
1491 account_set_missing_folder();
1492 folder_set_missing_folders();
1493 folderview_set(folderview);
1495 prefs_matcher_read_config();
1496 quicksearch_set_search_strings(mainwin->summaryview->quicksearch);
1498 /* make one all-folder processing before using claws */
1499 main_window_cursor_wait(mainwin);
1500 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
1502 /* if claws crashed, rebuild caches */
1503 if (claws_crashed()) {
1505 debug_print("Claws Mail crashed, checking for new messages in local folders\n");
1506 folder_item_update_thaw();
1507 folderview_check_new(NULL);
1508 folder_clean_cache_memory_force();
1509 folder_item_update_freeze();
1511 /* make the crash-indicator file */
1512 str_write_to_file("foo", get_crashfile_name());
1514 inc_autocheck_timer_init(mainwin);
1516 /* ignore SIGPIPE signal for preventing sudden death of program */
1518 signal(SIGPIPE, SIG_IGN);
1520 if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
1521 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1523 if (cmd.online_mode == ONLINE_MODE_ONLINE) {
1524 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1527 if (cmd.status_folders) {
1528 g_ptr_array_free(cmd.status_folders, TRUE);
1529 cmd.status_folders = NULL;
1531 if (cmd.status_full_folders) {
1532 g_ptr_array_free(cmd.status_full_folders, TRUE);
1533 cmd.status_full_folders = NULL;
1536 claws_register_idle_function(claws_gtk_idle);
1538 prefs_toolbar_init();
1540 num_folder_class = g_list_length(folder_get_list());
1542 plugin_load_all("GTK2");
1544 if (g_list_length(folder_get_list()) != num_folder_class) {
1545 debug_print("new folders loaded, reloading processing rules\n");
1546 prefs_matcher_read_config();
1549 if ((plug_list = plugin_get_unloaded_list()) != NULL) {
1552 gint num_plugins = 0;
1553 for (cur = plug_list; cur; cur = cur->next) {
1554 Plugin *plugin = (Plugin *)cur->data;
1555 gchar *tmp = g_strdup_printf("%s\n%s",
1557 plugin_get_name(plugin));
1562 main_window_cursor_normal(mainwin);
1563 main_window_popup(mainwin);
1564 mainwin_shown = TRUE;
1565 alertpanel_warning(ngettext(
1566 "The following plugin failed to load. "
1567 "Check the Plugins configuration "
1568 "for more information:\n%s",
1569 "The following plugins failed to load. "
1570 "Check the Plugins configuration "
1571 "for more information:\n%s",
1574 main_window_cursor_wait(mainwin);
1576 g_slist_free(plug_list);
1580 prefs_common_write_config();
1581 plugin_load_standard_plugins ();
1583 /* if not crashed, show window now */
1584 if (!mainwin_shown) {
1585 /* apart if something told not to show */
1586 if (show_at_startup)
1587 main_window_popup(mainwin);
1590 if (!folder_have_mailbox()) {
1591 prefs_destroy_cache();
1592 main_window_cursor_normal(mainwin);
1593 if (folder_get_list() != NULL) {
1594 alertpanel_error(_("Claws Mail has detected a configured "
1595 "mailbox, but it is incomplete. It is "
1596 "possibly due to a failing IMAP account. Use "
1597 "\"Rebuild folder tree\" on the mailbox parent "
1598 "folder's context menu to try to fix it."));
1600 alertpanel_error(_("Claws Mail has detected a configured "
1601 "mailbox, but could not load it. It is "
1602 "probably provided by an out-of-date "
1603 "external plugin. Please reinstall the "
1604 "plugin and try again."));
1605 exit_claws(mainwin);
1613 static_mainwindow = mainwin;
1616 if (prefs_common.data_root != NULL && *prefs_common.data_root != '\0') {
1617 GnomeVFSVolume *vol = NULL;
1618 gchar *uri, *mount_path;
1620 volmon = gnome_vfs_get_volume_monitor();
1621 vol = gnome_vfs_volume_monitor_get_volume_for_path(volmon, prefs_common.data_root);
1623 uri = gnome_vfs_volume_get_activation_uri (vol);
1624 mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
1627 if (vol == NULL || !gnome_vfs_volume_is_mounted(vol)
1628 || strcmp(mount_path, prefs_common.data_root)) {
1629 alertpanel_error(_("Claws Mail can not start without its data volume (%s)."),
1630 prefs_common.data_root);
1632 gnome_vfs_volume_unref(vol);
1633 exit_claws(mainwin);
1637 gnome_vfs_volume_unref(vol);
1638 g_signal_connect(G_OBJECT(volmon),
1639 "volume-mounted", G_CALLBACK(main_vol_mount_cb), mainwin);
1640 g_signal_connect(G_OBJECT(volmon),
1641 "volume-unmounted", G_CALLBACK(main_vol_unmount_cb), mainwin);
1645 #ifdef HAVE_STARTUP_NOTIFICATION
1646 startup_notification_complete(FALSE);
1649 sc_session_manager_connect(mainwin);
1652 folder_item_update_thaw();
1653 gtk_cmclist_thaw(GTK_CMCLIST(mainwin->folderview->ctree));
1654 main_window_cursor_normal(mainwin);
1656 if (!cmd.target && prefs_common.goto_last_folder_on_startup &&
1657 folder_find_item_from_identifier(prefs_common.last_opened_folder) != NULL &&
1659 cmd.target = prefs_common.last_opened_folder;
1662 if (cmd.receive_all && !cmd.target) {
1664 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(FALSE));
1665 } else if (prefs_common.chk_on_startup && !cmd.target) {
1667 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(TRUE));
1668 } else if (cmd.receive && !cmd.target) {
1670 g_timeout_add(1000, defer_check, NULL);
1672 gtk_widget_grab_focus(folderview->ctree);
1675 open_compose_new(cmd.compose_mailto, cmd.attach_files);
1677 if (cmd.attach_files) {
1678 list_free_strings(cmd.attach_files);
1679 g_list_free(cmd.attach_files);
1680 cmd.attach_files = NULL;
1682 if (cmd.subscribe) {
1683 folder_subscribe(cmd.subscribe_uri);
1692 g_timeout_add(500, defer_jump, (gpointer)cmd.target);
1695 prefs_destroy_cache();
1697 compose_reopen_exit_drafts();
1700 sc_starting = FALSE;
1701 main_window_set_menu_sensitive(mainwin);
1702 toolbar_main_set_sensitive(mainwin);
1705 /* register the callback of unix domain socket input */
1706 lock_socket_tag = claws_input_add(lock_socket,
1707 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
1708 lock_socket_input_cb,
1716 osso_deinitialize(osso_context);
1718 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1720 g_object_unref(nm_proxy);
1722 #ifdef HAVE_DBUS_GLIB
1723 uninstall_dbus_status_handler();
1725 dbus_g_connection_unref(connection);
1731 exit_claws(mainwin);
1736 static void save_all_caches(FolderItem *item, gpointer data)
1743 folder_item_close(item);
1746 folder_item_free_cache(item, TRUE);
1749 static void exit_claws(MainWindow *mainwin)
1752 gboolean have_connectivity;
1756 debug_print("shutting down\n");
1757 inc_autocheck_timer_remove();
1759 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1760 if (prefs_common.work_offline && went_offline_nm)
1761 prefs_common.work_offline = FALSE;
1764 /* save prefs for opened folder */
1765 if(mainwin->folderview->opened) {
1768 item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
1769 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
1770 prefs_common.last_opened_folder = folder_item_get_identifier(item);
1773 /* save all state before exiting */
1774 folder_func_to_all_folders(save_all_caches, NULL);
1775 folder_write_list();
1777 main_window_get_size(mainwin);
1778 main_window_get_position(mainwin);
1780 prefs_common_write_config();
1781 account_write_config_all();
1782 #ifndef USE_NEW_ADDRBOOK
1783 addressbook_export_to_file();
1785 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1786 gtk_accel_map_save(filename);
1789 /* delete temporary files */
1790 remove_all_files(get_tmp_dir());
1791 remove_all_files(get_mime_tmp_dir());
1793 close_log_file(LOG_PROTOCOL);
1794 close_log_file(LOG_DEBUG_FILTERING);
1796 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1797 have_connectivity = networkmanager_is_online(NULL);
1799 have_connectivity = TRUE;
1801 #ifdef HAVE_LIBETPAN
1802 imap_main_done(have_connectivity);
1803 nntp_main_done(have_connectivity);
1805 /* delete crashfile */
1807 claws_unlink(get_crashfile_name());
1809 lock_socket_remove();
1812 if (mainwin->smc_conn)
1813 SmcCloseConnection ((SmcConn)mainwin->smc_conn, 0, NULL);
1814 mainwin->smc_conn = NULL;
1817 main_window_destroy_all();
1819 plugin_unload_all("GTK2");
1822 prefs_toolbar_done();
1824 #ifndef USE_NEW_ADDRBOOK
1825 addressbook_destroy();
1827 prefs_themes_done();
1829 prefs_ext_prog_done();
1830 prefs_wrapping_done();
1831 prefs_compose_writing_done();
1832 prefs_msg_colors_done();
1833 prefs_image_viewer_done();
1834 image_viewer_done();
1836 prefs_summaries_done();
1837 prefs_message_done();
1839 prefs_receive_done();
1840 prefs_logging_done();
1844 prefs_spelling_done();
1845 gtkaspell_checkers_quit();
1847 plugin_unload_all("Common");
1851 #define G_STRING_APPEND_ENCODED_URI(gstring,source) \
1853 gchar tmpbuf[BUFFSIZE]; \
1854 encode_uri(tmpbuf, BUFFSIZE, (source)); \
1855 g_string_append((gstring), tmpbuf); \
1858 #define G_PRINT_EXIT(msg) \
1864 static GString * parse_cmd_compose_from_file(const gchar *fn)
1866 GString *headers = g_string_new(NULL);
1867 GString *body = g_string_new(NULL);
1875 if (fn == NULL || *fn == '\0')
1876 G_PRINT_EXIT(_("Missing filename\n"));
1877 isstdin = (*fn == '-' && *(fn + 1) == '\0');
1881 fp = g_fopen(fn, "r");
1883 G_PRINT_EXIT(_("Cannot open filename for reading\n"));
1886 while (fgets(fb, sizeof(fb), fp)) {
1892 while (*h && *h != ':') { ++h; } /* search colon */
1894 G_PRINT_EXIT(_("Malformed header\n"));
1896 while (*v && *v == ' ') { ++v; } /* trim value start */
1898 tmp = g_ascii_strdown(fb, -1); /* get header name */
1899 if (!strcmp(tmp, "to")) {
1901 G_PRINT_EXIT(_("Duplicated 'To:' header\n"));
1904 g_string_append_c(headers, '&');
1905 g_string_append(headers, tmp);
1906 g_string_append_c(headers, '=');
1907 #if GLIB_CHECK_VERSION(2,16,0)
1908 g_string_append_uri_escaped(headers, v, NULL, TRUE);
1910 G_STRING_APPEND_ENCODED_URI(headers, v);
1916 G_PRINT_EXIT(_("Missing required 'To:' header\n"));
1917 g_string_append(body, to);
1919 g_string_append(body, "?body=");
1920 while (fgets(fb, sizeof(fb), fp)) {
1921 #if GLIB_CHECK_VERSION(2,16,0)
1922 g_string_append_uri_escaped(body, fb, NULL, TRUE);
1924 G_STRING_APPEND_ENCODED_URI(body, fb);
1929 /* append the remaining headers */
1930 g_string_append(body, headers->str);
1931 g_string_free(headers, TRUE);
1936 #undef G_STRING_APPEND_ENCODED_URI
1939 static void parse_cmd_opt(int argc, char *argv[])
1944 for (i = 1; i < argc; i++) {
1945 if (!strncmp(argv[i], "--receive-all", 13)) {
1946 cmd.receive_all = TRUE;
1947 } else if (!strncmp(argv[i], "--receive", 9)) {
1949 } else if (!strncmp(argv[i], "--cancel-receiving", 18)) {
1950 cmd.cancel_receiving = TRUE;
1951 } else if (!strncmp(argv[i], "--cancel-sending", 16)) {
1952 cmd.cancel_sending = TRUE;
1953 } else if (!strncmp(argv[i], "--compose-from-file", 19)) {
1954 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1956 GString *mailto = parse_cmd_compose_from_file(p);
1958 cmd.compose_mailto = mailto->str;
1960 } else if (!strncmp(argv[i], "--compose", 9)) {
1961 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1964 cmd.compose_mailto = NULL;
1965 if (p && *p != '\0' && *p != '-') {
1966 if (!strncmp(p, "mailto:", 7)) {
1967 cmd.compose_mailto = p + 7;
1969 cmd.compose_mailto = p;
1973 } else if (!strncmp(argv[i], "--subscribe", 11)) {
1974 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1975 if (p && *p != '\0' && *p != '-') {
1976 cmd.subscribe = TRUE;
1977 cmd.subscribe_uri = p;
1979 } else if (!strncmp(argv[i], "--attach", 8)) {
1980 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1983 while (p && *p != '\0' && *p != '-') {
1984 if ((file = g_filename_from_uri(p, NULL, NULL)) != NULL) {
1985 if (!is_file_exist(file)) {
1990 if (file == NULL && *p != G_DIR_SEPARATOR) {
1991 file = g_strconcat(claws_get_startup_dir(),
1994 } else if (file == NULL) {
1997 ainfo = g_new0(AttachInfo, 1);
1999 cmd.attach_files = g_list_append(cmd.attach_files, ainfo);
2001 p = (i+1 < argc)?argv[i+1]:NULL;
2003 } else if (!strncmp(argv[i], "--send", 6)) {
2005 } else if (!strncmp(argv[i], "--version-full", 14) ||
2006 !strncmp(argv[i], "-V", 2)) {
2007 g_print("Claws Mail version " VERSION "\n");
2008 main_dump_features_list(FALSE);
2010 } else if (!strncmp(argv[i], "--version", 9) ||
2011 !strncmp(argv[i], "-v", 2)) {
2012 g_print("Claws Mail version " VERSION "\n");
2014 } else if (!strncmp(argv[i], "--status-full", 13)) {
2015 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
2017 cmd.status_full = TRUE;
2018 while (p && *p != '\0' && *p != '-') {
2019 if (!cmd.status_full_folders) {
2020 cmd.status_full_folders =
2023 g_ptr_array_add(cmd.status_full_folders,
2026 p = (i+1 < argc)?argv[i+1]:NULL;
2028 } else if (!strncmp(argv[i], "--status", 8)) {
2029 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
2032 while (p && *p != '\0' && *p != '-') {
2033 if (!cmd.status_folders)
2034 cmd.status_folders = g_ptr_array_new();
2035 g_ptr_array_add(cmd.status_folders,
2038 p = (i+1 < argc)?argv[i+1]:NULL;
2040 } else if (!strncmp(argv[i], "--search", 8)) {
2041 cmd.search_folder = (i+1 < argc)?argv[i+1]:NULL;
2042 cmd.search_type = (i+2 < argc)?argv[i+2]:NULL;
2043 cmd.search_request = (i+3 < argc)?argv[i+3]:NULL;
2044 const char* rec = (i+4 < argc)?argv[i+4]:NULL;
2045 cmd.search_recursive = TRUE;
2046 if (rec && (tolower(*rec)=='n' || tolower(*rec)=='f' || *rec=='0'))
2047 cmd.search_recursive = FALSE;
2048 if (cmd.search_folder && cmd.search_type && cmd.search_request)
2050 } else if (!strncmp(argv[i], "--online", 8)) {
2051 cmd.online_mode = ONLINE_MODE_ONLINE;
2052 } else if (!strncmp(argv[i], "--offline", 9)) {
2053 cmd.online_mode = ONLINE_MODE_OFFLINE;
2054 } else if (!strncmp(argv[i], "--statistics", 12)) {
2055 cmd.statistics = TRUE;
2056 } else if (!strncmp(argv[i], "--reset-statistics", 18)) {
2057 cmd.reset_statistics = TRUE;
2058 } else if (!strncmp(argv[i], "--help", 6) ||
2059 !strncmp(argv[i], "-h", 2)) {
2060 gchar *base = g_path_get_basename(argv[0]);
2061 g_print(_("Usage: %s [OPTION]...\n"), base);
2063 g_print("%s\n", _(" --compose [address] open composition window"));
2064 g_print("%s\n", _(" --compose-from-file file\n"
2065 " open composition window with data from given file;\n"
2066 " use - as file name for reading from standard input;\n"
2067 " content format: headers first (To: required) until an\n"
2068 " empty line, then mail body until end of file."));
2069 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
2070 g_print("%s\n", _(" --attach file1 [file2]...\n"
2071 " open composition window with specified files\n"
2073 g_print("%s\n", _(" --receive receive new messages"));
2074 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
2075 g_print("%s\n", _(" --cancel-receiving cancel receiving of messages"));
2076 g_print("%s\n", _(" --cancel-sending cancel sending of messages"));
2077 g_print("%s\n", _(" --search folder type request [recursive]\n"
2079 " folder ex.: \"#mh/Mailbox/inbox\" or \"Mail\"\n"
2080 " type: s[ubject],f[rom],t[o],e[xtended],m[ixed] or g: tag\n"
2081 " request: search string\n"
2082 " recursive: false if arg. starts with 0, n, N, f or F"));
2084 g_print("%s\n", _(" --send send all queued messages"));
2085 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
2086 g_print("%s\n", _(" --status-full [folder]...\n"
2087 " show the status of each folder"));
2088 g_print("%s\n", _(" --statistics show session statistics"));
2089 g_print("%s\n", _(" --reset-statistics reset session statistics"));
2090 g_print("%s\n", _(" --select folder[/msg] jumps to the specified folder/message\n"
2091 " folder is a folder id like 'folder/sub_folder'"));
2092 g_print("%s\n", _(" --online switch to online mode"));
2093 g_print("%s\n", _(" --offline switch to offline mode"));
2094 g_print("%s\n", _(" --exit --quit -q exit Claws Mail"));
2095 g_print("%s\n", _(" --debug debug mode"));
2096 g_print("%s\n", _(" --help -h display this help and exit"));
2097 g_print("%s\n", _(" --version -v output version information and exit"));
2098 g_print("%s\n", _(" --version-full -V output version and built-in features information and exit"));
2099 g_print("%s\n", _(" --config-dir output configuration directory"));
2100 g_print("%s\n", _(" --alternate-config-dir [dir]\n"
2101 " use specified configuration directory"));
2105 } else if (!strncmp(argv[i], "--crash", 7)) {
2107 cmd.crash_params = g_strdup((i+1 < argc)?argv[i+1]:NULL);
2109 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
2110 g_print(RC_DIR "\n");
2112 } else if (!strncmp(argv[i], "--alternate-config-dir", sizeof "--alternate-config-dir" - 1) && i+1 < argc) {
2113 set_rc_dir(argv[i+1]);
2114 } else if (!strncmp(argv[i], "--exit", 6) ||
2115 !strncmp(argv[i], "--quit", 6) ||
2116 !strncmp(argv[i], "-q", 2)) {
2118 } else if (!strncmp(argv[i], "--select", 8) && i+1 < argc) {
2119 cmd.target = argv[i+1];
2120 } else if (i == 1 && argc == 2) {
2121 /* only one parameter. Do something intelligent about it */
2122 if ((strstr(argv[i], "@")||!strncmp(argv[i], "mailto:", 7)) && !strstr(argv[i], "://")) {
2123 const gchar *p = argv[i];
2126 cmd.compose_mailto = NULL;
2127 if (p && *p != '\0' && *p != '-') {
2128 if (!strncmp(p, "mailto:", 7)) {
2129 cmd.compose_mailto = p + 7;
2131 cmd.compose_mailto = p;
2134 } else if (!strncmp(argv[i], "file://", 7)) {
2135 cmd.target = argv[i];
2136 } else if (!strncmp(argv[i], "?attach=file://", strlen("?attach=file://"))) {
2138 cmd.compose_mailto = argv[i];
2139 } else if (strstr(argv[i], "://")) {
2140 const gchar *p = argv[i];
2141 if (p && *p != '\0' && *p != '-') {
2142 cmd.subscribe = TRUE;
2143 cmd.subscribe_uri = p;
2145 } else if (!strcmp(argv[i], "--sync")) {
2147 } else if (is_dir_exist(argv[i]) || is_file_exist(argv[i])) {
2148 cmd.target = argv[i];
2150 g_print(_("Unknown option\n"));
2156 if (cmd.attach_files && cmd.compose == FALSE) {
2158 cmd.compose_mailto = NULL;
2162 static void initial_processing(FolderItem *item, gpointer data)
2164 MainWindow *mainwin = (MainWindow *)data;
2167 cm_return_if_fail(item);
2168 buf = g_strdup_printf(_("Processing (%s)..."),
2171 : _("top level folder"));
2175 if (item->prefs->enable_processing) {
2176 item->processing_pending = TRUE;
2177 folder_item_apply_processing(item);
2178 item->processing_pending = FALSE;
2181 STATUSBAR_POP(mainwin);
2184 static gboolean draft_all_messages(void)
2186 GList *compose_list = NULL;
2188 compose_clear_exit_drafts();
2189 compose_list = compose_get_compose_list();
2190 while (compose_list != NULL) {
2191 Compose *c = (Compose*)compose_list->data;
2192 if (!compose_draft(c, COMPOSE_DRAFT_FOR_EXIT))
2194 compose_list = compose_get_compose_list();
2198 gboolean clean_quit(gpointer data)
2200 static gboolean firstrun = TRUE;
2207 /*!< Good idea to have the main window stored in a
2208 * static variable so we can check that variable
2209 * to see if we're really allowed to do things
2210 * that actually the spawner is supposed to
2211 * do (like: sending mail, composing messages).
2212 * Because, really, if we're the spawnee, and
2213 * we touch GTK stuff, we're hosed. See the
2216 /* FIXME: Use something else to signal that we're
2217 * in the original spawner, and not in a spawned
2219 if (!static_mainwindow) {
2223 draft_all_messages();
2224 emergency_exit = TRUE;
2225 exit_claws(static_mainwindow);
2231 void app_will_exit(GtkWidget *widget, gpointer data)
2233 MainWindow *mainwin = data;
2235 if (gtk_main_level() == 0) {
2236 debug_print("not even started\n");
2239 if (sc_exiting == TRUE) {
2240 debug_print("exit pending\n");
2244 debug_print("exiting\n");
2245 if (compose_get_compose_list()) {
2246 if (!draft_all_messages()) {
2247 main_window_popup(mainwin);
2253 if (prefs_common.warn_queued_on_exit && procmsg_have_queued_mails_fast()) {
2254 if (alertpanel(_("Queued messages"),
2255 _("Some unsent messages are queued. Exit now?"),
2256 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL)
2257 != G_ALERTALTERNATE) {
2258 main_window_popup(mainwin);
2262 manage_window_focus_in(mainwin->window, NULL, NULL);
2266 #ifdef HAVE_VALGRIND
2267 if (RUNNING_ON_VALGRIND) {
2268 summary_clear_list(mainwin->summaryview);
2271 if (folderview_get_selected_item(mainwin->folderview))
2272 folder_item_close(folderview_get_selected_item(mainwin->folderview));
2276 gboolean claws_is_exiting(void)
2281 gboolean claws_is_starting(void)
2287 * CLAWS: want this public so crash dialog can delete the
2290 gchar *claws_get_socket_name(void)
2292 static gchar *filename = NULL;
2293 const gchar *socket_dir = NULL;
2295 if (rc_dir_is_alt())
2296 socket_dir = get_rc_dir();
2298 socket_dir = g_get_tmp_dir();
2299 if (filename == NULL) {
2300 filename = g_strdup_printf("%s%cclaws-mail-%d",
2301 socket_dir, G_DIR_SEPARATOR,
2312 static gchar *get_crashfile_name(void)
2314 static gchar *filename = NULL;
2316 if (filename == NULL) {
2317 filename = g_strdup_printf("%s%cclaws-crashed",
2318 get_tmp_dir(), G_DIR_SEPARATOR);
2324 static gint prohibit_duplicate_launch(void)
2331 path = claws_get_socket_name();
2332 /* Try to connect to the control socket */
2333 uxsock = fd_connect_unix(path);
2335 if (x_display == NULL)
2336 x_display = g_strdup(g_getenv("DISPLAY"));
2343 /* If connect failed, no other process is running.
2344 * Unlink the potentially existing socket, then
2345 * open it. This has to be done locking a temporary
2346 * file to avoid the race condition where another
2347 * process could have created the socket just in
2350 socket_lock = g_strconcat(path, ".lock",
2352 lock_fd = g_open(socket_lock, O_RDWR|O_CREAT, 0);
2354 debug_print("Couldn't open %s: %s (%d)\n", socket_lock,
2355 strerror(errno), errno);
2356 g_free(socket_lock);
2359 if (flock(lock_fd, LOCK_EX) < 0) {
2360 debug_print("Couldn't lock %s: %s (%d)\n", socket_lock,
2361 strerror(errno), errno);
2363 g_free(socket_lock);
2369 debug_print("Opening socket %s\n", path);
2370 ret = fd_open_unix(path);
2372 flock(lock_fd, LOCK_UN);
2373 claws_unlink(socket_lock);
2374 g_free(socket_lock);
2381 hmutex = CreateMutexA(NULL, FALSE, "ClawsMail");
2383 debug_print("cannot create Mutex\n");
2386 if (GetLastError() != ERROR_ALREADY_EXISTS) {
2387 uxsock = fd_open_inet(50216);
2393 uxsock = fd_connect_inet(50216);
2397 /* remote command mode */
2399 debug_print("another Claws Mail instance is already running.\n");
2401 if (cmd.receive_all) {
2402 fd_write_all(uxsock, "receive_all\n", 12);
2403 } else if (cmd.receive) {
2404 fd_write_all(uxsock, "receive\n", 8);
2405 } else if (cmd.cancel_receiving) {
2406 fd_write_all(uxsock, "cancel_receiving\n", 17);
2407 } else if (cmd.cancel_sending) {
2408 fd_write_all(uxsock, "cancel_sending\n", 15);
2409 } else if (cmd.compose && cmd.attach_files) {
2410 gchar *str, *compose_str;
2412 if (cmd.compose_mailto) {
2413 compose_str = g_strdup_printf("compose_attach %s\n",
2414 cmd.compose_mailto);
2416 compose_str = g_strdup("compose_attach\n");
2419 fd_write_all(uxsock, compose_str, strlen(compose_str));
2420 g_free(compose_str);
2422 for (curr = cmd.attach_files; curr != NULL ; curr = curr->next) {
2423 str = (gchar *) ((AttachInfo *)curr->data)->file;
2424 fd_write_all(uxsock, str, strlen(str));
2425 fd_write_all(uxsock, "\n", 1);
2428 fd_write_all(uxsock, ".\n", 2);
2429 } else if (cmd.compose) {
2432 if (cmd.compose_mailto) {
2433 compose_str = g_strdup_printf
2434 ("compose %s\n", cmd.compose_mailto);
2436 compose_str = g_strdup("compose\n");
2439 fd_write_all(uxsock, compose_str, strlen(compose_str));
2440 g_free(compose_str);
2441 } else if (cmd.subscribe) {
2442 gchar *str = g_strdup_printf("subscribe %s\n", cmd.subscribe_uri);
2443 fd_write_all(uxsock, str, strlen(str));
2445 } else if (cmd.send) {
2446 fd_write_all(uxsock, "send\n", 5);
2447 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
2448 fd_write(uxsock, "online\n", 6);
2449 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
2450 fd_write(uxsock, "offline\n", 7);
2451 } else if (cmd.status || cmd.status_full) {
2452 gchar buf[BUFFSIZE];
2454 const gchar *command;
2458 command = cmd.status_full ? "status-full\n" : "status\n";
2459 folders = cmd.status_full ? cmd.status_full_folders :
2462 fd_write_all(uxsock, command, strlen(command));
2463 for (i = 0; folders && i < folders->len; ++i) {
2464 folder = g_ptr_array_index(folders, i);
2465 fd_write_all(uxsock, folder, strlen(folder));
2466 fd_write_all(uxsock, "\n", 1);
2468 fd_write_all(uxsock, ".\n", 2);
2470 fd_gets(uxsock, buf, sizeof(buf));
2471 if (!strncmp(buf, ".\n", 2)) break;
2474 } else if (cmd.exit) {
2475 fd_write_all(uxsock, "exit\n", 5);
2476 } else if (cmd.statistics) {
2478 fd_write(uxsock, "statistics\n", 11);
2480 fd_gets(uxsock, buf, sizeof(buf));
2481 if (!strncmp(buf, ".\n", 2)) break;
2484 } else if (cmd.reset_statistics) {
2485 fd_write(uxsock, "reset_statistics\n", 17);
2486 } else if (cmd.target) {
2487 gchar *str = g_strdup_printf("select %s\n", cmd.target);
2488 fd_write_all(uxsock, str, strlen(str));
2490 } else if (cmd.search) {
2491 gchar buf[BUFFSIZE];
2493 g_strdup_printf("search %s\n%s\n%s\n%c\n",
2494 cmd.search_folder, cmd.search_type, cmd.search_request,
2495 (cmd.search_recursive==TRUE)?'1':'0');
2496 fd_write_all(uxsock, str, strlen(str));
2499 fd_gets(uxsock, buf, sizeof(buf));
2500 if (!strncmp(buf, ".\n", 2)) break;
2506 fd_write_all(uxsock, "get_display\n", 12);
2507 memset(buf, 0, sizeof(buf));
2508 fd_gets(uxsock, buf, sizeof(buf));
2509 if (strcmp2(buf, x_display)) {
2510 g_print("Claws Mail is already running on display %s.\n",
2514 uxsock = fd_connect_unix(path);
2515 fd_write_all(uxsock, "popup\n", 6);
2518 fd_write_all(uxsock, "popup\n", 6);
2526 static gint lock_socket_remove(void)
2531 if (lock_socket < 0) {
2535 if (lock_socket_tag > 0) {
2536 g_source_remove(lock_socket_tag);
2538 fd_close(lock_socket);
2541 filename = claws_get_socket_name();
2542 claws_unlink(filename);
2548 static GPtrArray *get_folder_item_list(gint sock)
2550 gchar buf[BUFFSIZE];
2552 GPtrArray *folders = NULL;
2555 fd_gets(sock, buf, sizeof(buf));
2556 if (!strncmp(buf, ".\n", 2)) {
2561 folders = g_ptr_array_new();
2563 item = folder_find_item_from_identifier(buf);
2565 g_ptr_array_add(folders, item);
2567 g_warning("no such folder: %s\n", buf);
2574 static void lock_socket_input_cb(gpointer data,
2576 GIOCondition condition)
2578 MainWindow *mainwin = (MainWindow *)data;
2580 gchar buf[BUFFSIZE];
2582 sock = fd_accept(source);
2583 fd_gets(sock, buf, sizeof(buf));
2585 if (!strncmp(buf, "popup", 5)) {
2586 main_window_popup(mainwin);
2587 } else if (!strncmp(buf, "get_display", 11)) {
2588 fd_write_all(sock, x_display, strlen(x_display));
2589 } else if (!strncmp(buf, "receive_all", 11)) {
2590 inc_all_account_mail(mainwin, FALSE,
2591 prefs_common.newmail_notify_manu);
2592 } else if (!strncmp(buf, "receive", 7)) {
2593 inc_mail(mainwin, prefs_common.newmail_notify_manu);
2594 } else if (!strncmp(buf, "cancel_receiving", 16)) {
2597 } else if (!strncmp(buf, "cancel_sending", 14)) {
2599 } else if (!strncmp(buf, "compose_attach", 14)) {
2600 GList *files = NULL, *curr;
2604 mailto = g_strdup(buf + strlen("compose_attach") + 1);
2605 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
2607 if (!strcmp2(buf, "."))
2610 ainfo = g_new0(AttachInfo, 1);
2611 ainfo->file = g_strdup(buf);
2612 files = g_list_append(files, ainfo);
2614 open_compose_new(mailto, files);
2616 curr = g_list_first(files);
2617 while (curr != NULL) {
2618 ainfo = (AttachInfo *)curr->data;
2619 g_free(ainfo->file);
2625 } else if (!strncmp(buf, "compose", 7)) {
2626 open_compose_new(buf + strlen("compose") + 1, NULL);
2627 } else if (!strncmp(buf, "subscribe", 9)) {
2628 main_window_popup(mainwin);
2629 folder_subscribe(buf + strlen("subscribe") + 1);
2630 } else if (!strncmp(buf, "send", 4)) {
2632 } else if (!strncmp(buf, "online", 6)) {
2633 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
2634 } else if (!strncmp(buf, "offline", 7)) {
2635 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
2636 } else if (!strncmp(buf, "status-full", 11) ||
2637 !strncmp(buf, "status", 6)) {
2641 folders = get_folder_item_list(sock);
2642 status = folder_get_status
2643 (folders, !strncmp(buf, "status-full", 11));
2644 fd_write_all(sock, status, strlen(status));
2645 fd_write_all(sock, ".\n", 2);
2647 if (folders) g_ptr_array_free(folders, TRUE);
2648 } else if (!strncmp(buf, "statistics", 10)) {
2651 g_snprintf(tmp, sizeof(tmp), _("Session statistics\n"));
2652 fd_write_all(sock, tmp, strlen(tmp));
2654 if (prefs_common.date_format) {
2659 lt = localtime(&session_stats.time_started);
2660 fast_strftime(date, len, prefs_common.date_format, lt);
2661 g_snprintf(tmp, sizeof(tmp), _("Started: %s\n"),
2662 lt ? date : ctime(&session_stats.time_started));
2664 g_snprintf(tmp, sizeof(tmp), _("Started: %s\n"),
2665 ctime(&session_stats.time_started));
2666 fd_write_all(sock, tmp, strlen(tmp));
2668 fd_write_all(sock, "\n", 1);
2670 g_snprintf(tmp, sizeof(tmp), _("Incoming traffic\n"));
2671 fd_write_all(sock, tmp, strlen(tmp));
2673 g_snprintf(tmp, sizeof(tmp), _("Received messages: %d\n"),
2674 session_stats.received);
2675 fd_write_all(sock, tmp, strlen(tmp));
2677 fd_write_all(sock, "\n", 1);
2679 g_snprintf(tmp, sizeof(tmp), _("Outgoing traffic\n"));
2680 fd_write_all(sock, tmp, strlen(tmp));
2682 g_snprintf(tmp, sizeof(tmp), _("New/redirected messages: %d\n"),
2683 session_stats.sent);
2684 fd_write_all(sock, tmp, strlen(tmp));
2686 g_snprintf(tmp, sizeof(tmp), _("Replied messages: %d\n"),
2687 session_stats.replied);
2688 fd_write_all(sock, tmp, strlen(tmp));
2690 g_snprintf(tmp, sizeof(tmp), _("Forwarded messages: %d\n"),
2691 session_stats.forwarded);
2692 fd_write_all(sock, tmp, strlen(tmp));
2694 g_snprintf(tmp, sizeof(tmp), _("Total outgoing messages: %d\n"),
2695 (session_stats.sent + session_stats.replied +
2696 session_stats.forwarded));
2697 fd_write_all(sock, tmp, strlen(tmp));
2699 fd_write_all(sock, ".\n", 2);
2700 } else if (!strncmp(buf, "reset_statistics", 16)) {
2702 } else if (!strncmp(buf, "select ", 7)) {
2703 const gchar *target = buf+7;
2704 mainwindow_jump_to(target, TRUE);
2705 } else if (!strncmp(buf, "search ", 7)) {
2706 FolderItem* folderItem = NULL;
2707 GSList *messages = NULL;
2708 gchar *folder_name = NULL;
2709 gchar *request = NULL;
2710 AdvancedSearch *search;
2712 AdvancedSearchType searchType = ADVANCED_SEARCH_EXTENDED;
2714 search = advsearch_new();
2716 folder_name = g_strdup(buf+7);
2717 strretchomp(folder_name);
2719 if (fd_gets(sock, buf, sizeof(buf)) <= 0)
2722 switch (toupper(buf[0])) {
2723 case 'S': searchType = ADVANCED_SEARCH_SUBJECT; break;
2724 case 'F': searchType = ADVANCED_SEARCH_FROM; break;
2725 case 'T': searchType = ADVANCED_SEARCH_TO; break;
2726 case 'M': searchType = ADVANCED_SEARCH_MIXED; break;
2727 case 'G': searchType = ADVANCED_SEARCH_TAG; break;
2728 case 'E': searchType = ADVANCED_SEARCH_EXTENDED; break;
2731 if (fd_gets(sock, buf, sizeof(buf)) <= 0)
2734 request = g_strdup(buf);
2735 strretchomp(request);
2738 if (fd_gets(sock, buf, sizeof(buf)) > 0)
2739 recursive = buf[0] != '0';
2741 debug_print("search: %s %i %s %i\n", folder_name, searchType, request, recursive);
2744 folderItem = folder_find_item_from_identifier(folder_name);
2746 if (folder_name && folderItem == NULL) {
2747 debug_print("Unknown folder item : '%s', searching folder\n",folder_name);
2748 Folder* folder = folder_find_from_path(folder_name);
2750 folderItem = FOLDER_ITEM(folder->node->data);
2752 debug_print("Unknown folder: '%s'\n",folder_name);
2754 debug_print("%s %s\n",folderItem->name, folderItem->path);
2757 if (folderItem != NULL) {
2758 advsearch_set(search, searchType, request);
2759 advsearch_search_msgs_in_folders(search, &messages, folderItem, recursive);
2761 g_print("Folder '%s' not found.\n'", folder_name);
2765 for (cur = messages; cur != NULL; cur = cur->next) {
2766 MsgInfo* msg = (MsgInfo *)cur->data;
2767 gchar *file = procmsg_get_message_file_path(msg);
2768 fd_write_all(sock, file, strlen(file));
2769 fd_write_all(sock, "\n", 1);
2772 fd_write_all(sock, ".\n", 2);
2775 g_free(folder_name);
2777 advsearch_free(search);
2778 if (messages != NULL)
2779 procmsg_msg_list_free(messages);
2780 } else if (!strncmp(buf, "exit", 4)) {
2781 if (prefs_common.clean_on_exit && !prefs_common.ask_on_clean) {
2782 procmsg_empty_all_trash();
2784 app_will_exit(NULL, mainwin);
2790 static void open_compose_new(const gchar *address, GList *attach_files)
2795 Xstrdup_a(addr, address, return);
2799 compose_new(NULL, addr, attach_files);
2802 static void send_queue(void)
2805 gchar *errstr = NULL;
2806 gboolean error = FALSE;
2807 for (list = folder_get_list(); list != NULL; list = list->next) {
2808 Folder *folder = list->data;
2810 if (folder->queue) {
2811 gint res = procmsg_send_queue
2812 (folder->queue, prefs_common.savemsg,
2816 folder_item_scan(folder->queue);
2824 alertpanel_error_log(_("Some errors occurred "
2825 "while sending queued messages:\n%s"), errstr);
2828 alertpanel_error_log("Some errors occurred "
2829 "while sending queued messages.");
2833 static void quit_signal_handler(int sig)
2835 debug_print("Quitting on signal %d\n", sig);
2837 g_timeout_add(0, clean_quit, NULL);
2840 static void install_basic_sighandlers()
2844 struct sigaction act;
2849 sigaddset(&mask, SIGTERM);
2852 sigaddset(&mask, SIGINT);
2855 sigaddset(&mask, SIGHUP);
2858 act.sa_handler = quit_signal_handler;
2863 sigaction(SIGTERM, &act, 0);
2866 sigaction(SIGINT, &act, 0);
2869 sigaction(SIGHUP, &act, 0);
2872 sigprocmask(SIG_UNBLOCK, &mask, 0);
2873 #endif /* !G_OS_WIN32 */
2876 #if (defined linux && defined SIGIO)
2877 static int mem_notify_fd = 0;
2879 static gboolean clean_caches(gpointer unused)
2881 if (static_mainwindow && static_mainwindow->lock_count > 0)
2883 debug_print("/dev/mem_notify: callback: Freeing some memory now!\n");
2884 folder_clean_cache_memory_force();
2888 static void memory_signal_handler(int sig)
2890 debug_print("/dev/mem_notify: Kernel says we should free up some memory!\n");
2891 g_timeout_add(10, clean_caches, NULL);
2894 static void install_memory_sighandler()
2897 struct sigaction act;
2900 mem_notify_fd = g_open("/dev/mem_notify", O_RDONLY|O_NONBLOCK, 0);
2901 if (mem_notify_fd == -1) {
2902 debug_print("/dev/mem_notify not available (%s)\n",
2907 fcntl(mem_notify_fd, F_SETOWN, getpid());
2908 flags = fcntl(mem_notify_fd, F_GETFL);
2909 fcntl(mem_notify_fd, flags|FASYNC);
2913 sigaddset(&mask, SIGIO);
2915 act.sa_handler = memory_signal_handler;
2919 sigaction(SIGIO, &act, 0);
2921 sigprocmask(SIG_UNBLOCK, &mask, 0);
2923 debug_print("/dev/mem_notify: installed handler\n");
2925 #endif /* linux && SIGIO */
2928 osso_context_t *get_osso_context(void)
2930 return static_osso_context;
2935 #ifdef HAVE_NETWORKMANAGER_SUPPORT
2936 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
2939 MainWindow *mainWin;
2942 if (static_mainwindow)
2943 mainWin = static_mainwindow;
2945 mainWin = (MainWindow*)data;
2947 if (!prefs_common.use_networkmanager)
2955 online = networkmanager_is_online(&error);
2957 if(online && went_offline_nm) {
2958 went_offline_nm = FALSE;
2959 main_window_toggle_work_offline(mainWin, FALSE, FALSE);
2960 debug_print("NetworkManager: Went online\n");
2961 log_message(LOG_PROTOCOL, _("NetworkManager: network is online.\n"));
2964 went_offline_nm = TRUE;
2965 main_window_toggle_work_offline(mainWin, TRUE, FALSE);
2966 debug_print("NetworkManager: Went offline\n");
2967 log_message(LOG_PROTOCOL, _("NetworkManager: network is offline.\n"));
2971 debug_print("Failed to get online information from NetworkManager: %s\n",
2973 g_error_free(error);
2977 debug_print("NetworkManager: Cannot change connection state because "
2978 "main window does not exist\n");
2981 /* Returns true (and sets error appropriately, if given) in case of error */
2982 gboolean networkmanager_is_online(GError **error)
2984 DBusGConnection *connection;
2986 GError *tmp_error = NULL;
2990 if (!prefs_common.use_networkmanager)
2995 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &tmp_error);
2998 /* If calling code doesn't do error checking, at least print some debug */
2999 if((error == NULL) || (*error == NULL))
3000 debug_print("Failed to open connection to system bus: %s\n",
3001 tmp_error->message);
3002 g_propagate_error(error, tmp_error);
3006 proxy = dbus_g_proxy_new_for_name(connection,
3007 "org.freedesktop.NetworkManager",
3008 "/org/freedesktop/NetworkManager",
3009 "org.freedesktop.NetworkManager");
3011 retVal = dbus_g_proxy_call(proxy,"state",&tmp_error, G_TYPE_INVALID,
3012 G_TYPE_UINT, &state, G_TYPE_INVALID);
3015 g_object_unref(proxy);
3017 dbus_g_connection_unref(connection);
3020 /* If calling code doesn't do error checking, at least print some debug */
3021 if((error == NULL) || (*error == NULL))
3022 debug_print("Failed to get state info from NetworkManager: %s\n",
3023 tmp_error->message);
3024 g_propagate_error(error, tmp_error);
3027 #if NM_CHECK_VERSION(0,8,992)
3028 return (state == NM_STATE_CONNECTED_LOCAL ||
3029 state == NM_STATE_CONNECTED_SITE ||
3030 state == NM_STATE_CONNECTED_GLOBAL ||
3031 state == NM_STATE_UNKNOWN);
3033 return (state == NM_STATE_CONNECTED ||
3034 state == NM_STATE_UNKNOWN);