2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 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>
49 GCRY_THREAD_OPTION_PTHREAD_IMPL;
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>
68 #include "mainwindow.h"
69 #include "folderview.h"
70 #include "image_viewer.h"
71 #include "summaryview.h"
72 #include "prefs_common.h"
73 #include "prefs_account.h"
74 #include "prefs_actions.h"
75 #include "prefs_ext_prog.h"
76 #include "prefs_fonts.h"
77 #include "prefs_image_viewer.h"
78 #include "prefs_message.h"
79 #include "prefs_receive.h"
80 #include "prefs_msg_colors.h"
81 #include "prefs_quote.h"
82 #include "prefs_spelling.h"
83 #include "prefs_summaries.h"
84 #include "prefs_themes.h"
85 #include "prefs_other.h"
86 #include "prefs_logging.h"
87 #include "prefs_send.h"
88 #include "prefs_wrapping.h"
89 #include "prefs_compose_writing.h"
90 #include "prefs_display_header.h"
95 #include "manage_window.h"
96 #include "alertpanel.h"
97 #include "statusbar.h"
98 #include "addressbook.h"
103 #include "gtkutils.h"
106 #include "prefs_toolbar.h"
109 #include "imap_gtk.h"
110 #include "news_gtk.h"
117 #include "imap-thread.h"
118 #include "nntp-thread.h"
120 #include "stock_pixmap.h"
122 #include "valgrind.h"
124 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
136 #include <hildon/hildon-banner.h>
137 #include <hildon/hildon-program.h>
139 #include <hildon-widgets/hildon-banner.h>
140 #include <hildon-widgets/hildon-program.h>
143 #include <libgnomevfs/gnome-vfs-volume.h>
144 #include <libgnomevfs/gnome-vfs-volume-monitor.h>
145 #include <libgnomevfs/gnome-vfs-utils.h>
147 #define OSSO_NAME "claws_mail"
148 #define OSSO_SERVICE "com.nokia."OSSO_NAME
149 #define OSSO_OBJECT "/com/nokia/"OSSO_NAME
150 #define OSSO_IFACE "com.nokia."OSSO_NAME
152 typedef struct _AppData AppData;
154 HildonProgram *program;
155 HildonWindow *window;
156 osso_context_t *osso_context;
159 static GnomeVFSVolumeMonitor *volmon;
162 #ifdef HAVE_NETWORKMANAGER_SUPPORT
163 /* Went offline due to NetworkManager */
164 static gboolean went_offline_nm;
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 gboolean status_full;
201 GPtrArray *status_folders;
202 GPtrArray *status_full_folders;
209 const gchar *subscribe_uri;
213 static void parse_cmd_opt(int argc, char *argv[]);
215 static gint prohibit_duplicate_launch (void);
216 static gchar * get_crashfile_name (void);
217 static gint lock_socket_remove (void);
218 static void lock_socket_input_cb (gpointer data,
220 GdkInputCondition condition);
222 static void open_compose_new (const gchar *address,
223 GPtrArray *attach_files);
225 static void send_queue (void);
226 static void initial_processing (FolderItem *item, gpointer data);
227 static void quit_signal_handler (int sig);
228 static void install_basic_sighandlers (void);
229 #if (defined linux && defined SIGIO)
230 static void install_memory_sighandler (void);
232 static void exit_claws (MainWindow *mainwin);
234 #ifdef HAVE_NETWORKMANAGER_SUPPORT
235 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
239 #define MAKE_DIR_IF_NOT_EXIST(dir) \
241 if (!is_dir_exist(dir)) { \
242 if (is_file_exist(dir)) { \
244 (_("File '%s' already exists.\n" \
245 "Can't create folder."), \
249 if (make_dir(dir) < 0) \
254 static MainWindow *static_mainwindow;
257 static osso_context_t *static_osso_context;
259 void exit_event_handler(gboolean die_now, gpointer data)
262 appdata = (AppData *) data;
263 /* Do whatever application needs to do before exiting */
264 exit_claws(static_mainwindow);
265 hildon_banner_show_information(GTK_WIDGET(appdata->window), NULL,
270 /* Callback for hardware D-BUS events */
271 void hw_event_handler(osso_hw_state_t *state, gpointer data)
274 appdata = (AppData *) data;
276 if (state->shutdown_ind) {
277 exit_claws(static_mainwindow);
278 hildon_banner_show_information(GTK_WIDGET(appdata->window), NULL,
283 /* Callback for normal D-BUS messages */
284 gint dbus_req_handler(const gchar * interface, const gchar * method,
285 GArray * arguments, gpointer data,
289 appdata = (AppData *) data;
291 if (!strcmp(method, "top_application")) {
292 osso_rpc_free_val(retval);
295 osso_system_note_infoprint(appdata->osso_context, method, retval);
296 osso_rpc_free_val(retval);
301 static gboolean emergency_exit = FALSE;
303 #ifdef HAVE_STARTUP_NOTIFICATION
304 static void sn_error_trap_push(SnDisplay *display, Display *xdisplay)
306 gdk_error_trap_push();
309 static void sn_error_trap_pop(SnDisplay *display, Display *xdisplay)
311 gdk_error_trap_pop();
314 static void startup_notification_complete(gboolean with_window)
317 GtkWidget *hack = NULL;
320 /* this is needed to make the startup notification leave,
321 * if we have been launched from a menu.
322 * We have to display a window, so let it be very little */
323 hack = gtk_window_new(GTK_WINDOW_POPUP);
324 gtk_window_move(GTK_WINDOW(hack), 0, 0);
325 gtk_widget_set_size_request(hack, 1, 1);
326 gtk_widget_show(hack);
329 xdisplay = GDK_DISPLAY();
330 sn_display = sn_display_new(xdisplay,
333 sn_context = sn_launchee_context_new_from_environment(sn_display,
334 DefaultScreen(xdisplay));
336 if (sn_context != NULL) {
337 sn_launchee_context_complete(sn_context);
338 sn_launchee_context_unref(sn_context);
339 sn_display_unref(sn_display);
342 gtk_widget_destroy(hack);
345 #endif /* HAVE_STARTUP_NOTIFICATION */
347 static void claws_gtk_idle(void)
349 while(gtk_events_pending()) {
350 gtk_main_iteration();
355 static gboolean sc_starting = FALSE;
357 static gboolean defer_check_all(void *data)
359 gboolean autochk = GPOINTER_TO_INT(data);
361 inc_all_account_mail(static_mainwindow, autochk,
362 prefs_common.newmail_notify_manu);
366 main_window_set_menu_sensitive(static_mainwindow);
367 toolbar_main_set_sensitive(static_mainwindow);
372 static gboolean defer_check(void *data)
374 inc_mail(static_mainwindow, prefs_common.newmail_notify_manu);
378 main_window_set_menu_sensitive(static_mainwindow);
379 toolbar_main_set_sensitive(static_mainwindow);
384 static gboolean defer_jump(void *data)
386 if (cmd.receive_all) {
387 defer_check_all(GINT_TO_POINTER(FALSE));
388 } else if (prefs_common.chk_on_startup) {
389 defer_check_all(GINT_TO_POINTER(TRUE));
390 } else if (cmd.receive) {
393 mainwindow_jump_to(data, FALSE);
396 main_window_set_menu_sensitive(static_mainwindow);
397 toolbar_main_set_sensitive(static_mainwindow);
402 static void chk_update_val(GtkWidget *widget, gpointer data)
404 gboolean *val = (gboolean *)data;
405 *val = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
408 static gboolean migrate_old_config(const gchar *old_cfg_dir, const gchar *new_cfg_dir, const gchar *oldversion)
410 gchar *message = g_strdup_printf(_("Configuration for %s found.\n"
411 "Do you want to migrate this configuration?"), oldversion);
412 gchar *message2 = g_strdup_printf(_("\n\nYour Sylpheed filtering rules can be converted by a\n"
413 "script available at %s."), TOOLS_URI);
415 if (!strcmp(oldversion, "Sylpheed"))
416 message = g_strconcat(message, message2, NULL);
420 GtkWidget *window = NULL;
421 GtkWidget *keep_backup_chk;
423 gboolean backup = TRUE;
425 keep_backup_chk = gtk_check_button_new_with_label (_("Keep old configuration"));
426 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_backup_chk), TRUE);
427 CLAWS_SET_TIP(keep_backup_chk,
428 _("Keeping a backup will allow you to go back to an "
429 "older version, but may take a while if you have "
430 "cached IMAP or News data, and will take some extra "
431 "room on your disk."));
433 g_signal_connect(G_OBJECT(keep_backup_chk), "toggled",
434 G_CALLBACK(chk_update_val), &backup);
436 if (alertpanel_full(_("Migration of configuration"), message,
437 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL, FALSE,
438 keep_backup_chk, ALERT_QUESTION, G_ALERTDEFAULT) != G_ALERTALTERNATE) {
442 /* we can either do a fast migration requiring not any extra disk
443 * space, or a slow one that copies the old configuration and leaves
447 window = label_window_create(_("Copying configuration... This may take a while..."));
450 r = copy_dir(old_cfg_dir, new_cfg_dir);
451 label_window_destroy(window);
453 /* if copy failed, we'll remove the partially copied
456 alertpanel_error(_("Migration failed!"));
457 remove_dir_recursive(new_cfg_dir);
460 /* fast mode failed, but we don't want backup */
461 remove_dir_recursive(old_cfg_dir);
465 window = label_window_create(_("Migrating configuration..."));
468 r = g_rename(old_cfg_dir, new_cfg_dir);
469 label_window_destroy(window);
471 /* if g_rename failed, we'll try to copy */
473 FILE_OP_ERROR(new_cfg_dir, "g_rename failed, trying copy\n");
480 static int migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
483 gchar *plugin_path, *old_plugin_path, *new_plugin_path;
485 gboolean err = FALSE;
487 oldfp = g_fopen(old_rc, "r");
490 newfp = g_fopen(new_rc, "w");
496 plugin_path = g_strdup(get_plugin_dir());
497 new_plugin_path = g_strdup(plugin_path);
499 if (strstr(plugin_path, "/claws-mail/")) {
500 gchar *end = g_strdup(strstr(plugin_path, "/claws-mail/")+strlen("/claws-mail/"));
501 *(strstr(plugin_path, "/claws-mail/")) = '\0';
502 old_plugin_path = g_strconcat(plugin_path, "/sylpheed-claws/", end, NULL);
505 old_plugin_path = g_strdup(new_plugin_path);
507 debug_print("replacing %s with %s\n", old_plugin_path, new_plugin_path);
508 while (fgets(buf, sizeof(buf), oldfp)) {
509 if (strncmp(buf, old_plugin_path, strlen(old_plugin_path))) {
510 err |= (fputs(buf, newfp) == EOF);
512 debug_print("->replacing %s", buf);
513 debug_print(" with %s%s", new_plugin_path, buf+strlen(old_plugin_path));
514 err |= (fputs(new_plugin_path, newfp) == EOF);
515 err |= (fputs(buf+strlen(old_plugin_path), newfp) == EOF);
519 g_free(new_plugin_path);
520 g_free(old_plugin_path);
522 if (fclose(newfp) == EOF)
530 sc_client_set_value (MainWindow *mainwin,
541 prop.num_vals = num_vals;
545 if (mainwin->smc_conn)
546 SmcSetProperties ((SmcConn) mainwin->smc_conn, 1, proplist);
549 static void sc_die_callback (SmcConn smc_conn, SmPointer client_data)
554 static void sc_save_complete_callback(SmcConn smc_conn, SmPointer client_data)
558 static void sc_shutdown_cancelled_callback (SmcConn smc_conn, SmPointer client_data)
560 MainWindow *mainwin = (MainWindow *)client_data;
561 if (mainwin->smc_conn)
562 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
565 static void sc_save_yourself_callback (SmcConn smc_conn,
566 SmPointer client_data,
572 MainWindow *mainwin = (MainWindow *)client_data;
573 if (mainwin->smc_conn)
574 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
577 static IceIOErrorHandler sc_ice_installed_handler;
579 static void sc_ice_io_error_handler (IceConn connection)
581 if (sc_ice_installed_handler)
582 (*sc_ice_installed_handler) (connection);
584 static gboolean sc_process_ice_messages (GIOChannel *source,
585 GIOCondition condition,
588 IceConn connection = (IceConn) data;
589 IceProcessMessagesStatus status;
591 status = IceProcessMessages (connection, NULL, NULL);
593 if (status == IceProcessMessagesIOError) {
594 IcePointer context = IceGetConnectionContext (connection);
596 if (context && GTK_IS_OBJECT (context)) {
597 guint disconnect_id = g_signal_lookup ("disconnect", G_OBJECT_TYPE (context));
599 if (disconnect_id > 0)
600 g_signal_emit (context, disconnect_id, 0);
602 IceSetShutdownNegotiation (connection, False);
603 IceCloseConnection (connection);
610 static void new_ice_connection (IceConn connection, IcePointer client_data, Bool opening,
611 IcePointer *watch_data)
617 /* Make sure we don't pass on these file descriptors to any
619 fcntl(IceConnectionNumber(connection),F_SETFD,
620 fcntl(IceConnectionNumber(connection),F_GETFD,0) | FD_CLOEXEC);
622 channel = g_io_channel_unix_new (IceConnectionNumber (connection));
623 input_id = g_io_add_watch (channel,
624 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
625 sc_process_ice_messages,
627 g_io_channel_unref (channel);
629 *watch_data = (IcePointer) GUINT_TO_POINTER (input_id);
631 input_id = GPOINTER_TO_UINT ((gpointer) *watch_data);
632 g_source_remove (input_id);
636 static void sc_session_manager_connect(MainWindow *mainwin)
638 static gboolean connected = FALSE;
639 SmcCallbacks callbacks;
641 IceIOErrorHandler default_handler;
648 sc_ice_installed_handler = IceSetIOErrorHandler (NULL);
649 default_handler = IceSetIOErrorHandler (sc_ice_io_error_handler);
651 if (sc_ice_installed_handler == default_handler)
652 sc_ice_installed_handler = NULL;
654 IceAddConnectionWatch (new_ice_connection, NULL);
657 callbacks.save_yourself.callback = sc_save_yourself_callback;
658 callbacks.die.callback = sc_die_callback;
659 callbacks.save_complete.callback = sc_save_complete_callback;
660 callbacks.shutdown_cancelled.callback = sc_shutdown_cancelled_callback;
662 callbacks.save_yourself.client_data =
663 callbacks.die.client_data =
664 callbacks.save_complete.client_data =
665 callbacks.shutdown_cancelled.client_data = (SmPointer) mainwin;
666 if (g_getenv ("SESSION_MANAGER")) {
667 gchar error_string_ret[256] = "";
669 mainwin->smc_conn = (gpointer)
670 SmcOpenConnection (NULL, mainwin,
671 SmProtoMajor, SmProtoMinor,
672 SmcSaveYourselfProcMask | SmcDieProcMask |
673 SmcSaveCompleteProcMask |
674 SmcShutdownCancelledProcMask,
677 256, error_string_ret);
679 if (error_string_ret[0] || mainwin->smc_conn == NULL)
680 g_warning ("While connecting to session manager:\n%s.",
684 vals = g_new (SmPropValue, 1);
685 vals[0].length = strlen(argv0);
686 vals[0].value = argv0;
687 sc_client_set_value (mainwin, SmCloneCommand, SmLISTofARRAY8, 1, vals);
688 sc_client_set_value (mainwin, SmRestartCommand, SmLISTofARRAY8, 1, vals);
689 sc_client_set_value (mainwin, SmProgram, SmARRAY8, 1, vals);
691 vals[0].length = strlen(g_get_user_name()?g_get_user_name():"");
692 vals[0].value = g_strdup(g_get_user_name()?g_get_user_name():"");
693 sc_client_set_value (mainwin, SmUserID, SmARRAY8, 1, vals);
699 static gboolean sc_exiting = FALSE;
700 static gboolean show_at_startup = TRUE;
701 static gboolean claws_crashed_bool = FALSE;
703 gboolean claws_crashed(void) {
704 return claws_crashed_bool;
707 void main_set_show_at_startup(gboolean show)
709 show_at_startup = show;
713 static void main_vol_mount_cb(GnomeVFSVolumeMonitor *vfs, GnomeVFSVolume *vol, MainWindow *mainwin)
715 gchar *uri = gnome_vfs_volume_get_activation_uri (vol);
716 gchar *mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
719 if(!strcmp(mount_path, prefs_common.data_root)) {
720 gtk_widget_set_sensitive(mainwin->window, TRUE);
726 static void main_vol_unmount_cb(GnomeVFSVolumeMonitor *vfs, GnomeVFSVolume *vol, MainWindow *mainwin)
728 gchar *uri = gnome_vfs_volume_get_activation_uri (vol);
729 gchar *mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
732 if(!strcmp(mount_path, prefs_common.data_root)) {
733 gtk_widget_set_sensitive(mainwin->window, FALSE);
742 static FILE* win32_debug_fp=NULL;
743 static guint win32_log_handler_app_id;
744 static guint win32_log_handler_glib_id;
745 static guint win32_log_handler_gtk_id;
747 static void win32_print_stdout(const gchar *string)
749 if (win32_debug_fp) {
750 fprintf(win32_debug_fp, string);
751 fflush(win32_debug_fp);
755 static void win32_print_stderr(const gchar *string)
757 if (win32_debug_fp) {
758 fprintf(win32_debug_fp, string);
759 fflush(win32_debug_fp);
763 static void win32_log(const gchar *log_domain, GLogLevelFlags log_level, const gchar* message, gpointer user_data)
765 if (win32_debug_fp) {
768 switch(log_level & G_LOG_LEVEL_MASK)
770 case G_LOG_LEVEL_ERROR:
773 case G_LOG_LEVEL_CRITICAL:
776 case G_LOG_LEVEL_WARNING:
779 case G_LOG_LEVEL_MESSAGE:
782 case G_LOG_LEVEL_INFO:
785 case G_LOG_LEVEL_DEBUG:
792 fprintf(win32_debug_fp, "%s: %s: %s", log_domain, type, message);
794 fprintf(win32_debug_fp, "%s: %s", type, message);
795 fflush(win32_debug_fp);
799 static void win32_open_log(void)
801 if (is_file_exist("claws-win32.log")) {
802 if (rename_force("claws-win32.log", "claws-win32.log.bak") < 0)
803 FILE_OP_ERROR("claws-win32.log", "rename");
805 win32_debug_fp = fopen("claws-win32.log", "w");
808 g_set_print_handler(win32_print_stdout);
809 g_set_printerr_handler(win32_print_stdout);
810 win32_log_handler_app_id = g_log_set_handler(NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
811 | G_LOG_FLAG_RECURSION, win32_log, NULL);
812 win32_log_handler_glib_id = g_log_set_handler("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
813 | G_LOG_FLAG_RECURSION, win32_log, NULL);
814 win32_log_handler_gtk_id = g_log_set_handler("Gtk", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
815 | G_LOG_FLAG_RECURSION, win32_log, NULL);
819 static void win32_close_log(void)
823 g_log_remove_handler("", win32_log_handler_app_id);
824 g_log_remove_handler("GLib", win32_log_handler_glib_id);
825 g_log_remove_handler("Gtk", win32_log_handler_gtk_id);
826 fclose(win32_debug_fp);
832 static void main_dump_features_list(gboolean show_debug_only)
833 /* display compiled-in features list */
835 if (show_debug_only && !debug_get_mode())
839 debug_print("runtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
840 gtk_major_version, gtk_minor_version, gtk_micro_version,
841 glib_major_version, glib_minor_version, glib_micro_version);
843 g_print("runtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
844 gtk_major_version, gtk_minor_version, gtk_micro_version,
845 glib_major_version, glib_minor_version, glib_micro_version);
847 debug_print("buildtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
848 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
849 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
851 g_print("buildtime GTK+ %d.%d.%d / GLib %d.%d.%d\n",
852 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
853 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
856 debug_print("Compiled-in features:\n");
858 g_print("Compiled-in features:\n");
861 debug_print(" compface\n");
863 g_print(" compface\n");
867 debug_print(" aspell\n");
869 g_print(" aspell\n");
873 debug_print(" gnutls\n");
875 g_print(" gnutls\n");
879 debug_print(" ipv6\n");
885 debug_print(" iconv\n");
891 debug_print(" jpilot\n");
893 g_print(" jpilot\n");
897 debug_print(" ldap\n");
903 debug_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
905 g_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
909 debug_print(" gnomeprint\n");
911 g_print(" gnomeprint\n");
915 debug_print(" libsm\n");
919 #if HAVE_NETWORKMANAGER_SUPPORT
921 debug_print(" NetworkManager\n");
923 g_print(" NetworkManager\n");
927 debug_print(" openssl\n");
929 g_print(" openssl\n");
933 #ifdef HAVE_DBUS_GLIB
934 static guint dbus_item_hook_id = -1;
935 static guint dbus_folder_hook_id = -1;
937 static void uninstall_dbus_status_handler(void)
940 g_object_unref(awn_proxy);
942 if (dbus_item_hook_id != -1)
943 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, dbus_item_hook_id);
944 if (dbus_folder_hook_id != -1)
945 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, dbus_folder_hook_id);
948 static void dbus_update(FolderItem *removed_item)
950 guint new, unread, unreadmarked, marked, total;
951 guint replied, forwarded, locked, ignored, watched;
953 GError *error = NULL;
955 folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total,
956 &replied, &forwarded, &locked, &ignored,
959 total -= removed_item->total_msgs;
960 new -= removed_item->new_msgs;
961 unread -= removed_item->unread_msgs;
965 buf = g_strdup_printf("%d", new);
966 dbus_g_proxy_call(awn_proxy, "SetInfoByName", &error,
967 G_TYPE_STRING, "claws-mail",
969 G_TYPE_INVALID, G_TYPE_INVALID);
973 dbus_g_proxy_call(awn_proxy, "UnsetInfoByName", &error, G_TYPE_STRING,
974 "claws-mail", G_TYPE_INVALID, G_TYPE_INVALID);
977 debug_print("%s", error->message);
982 static gboolean dbus_status_update_folder_hook(gpointer source, gpointer data)
984 FolderUpdateData *hookdata;
986 if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
987 dbus_update(hookdata->item);
994 static gboolean dbus_status_update_item_hook(gpointer source, gpointer data)
1001 static void install_dbus_status_handler(void)
1003 GError *tmp_error = NULL;
1004 DBusGConnection *connection = dbus_g_bus_get(DBUS_BUS_SESSION, &tmp_error);
1007 /* If calling code doesn't do error checking, at least print some debug */
1008 debug_print("Failed to open connection to session bus: %s\n",
1009 tmp_error->message);
1010 g_error_free(tmp_error);
1013 awn_proxy = dbus_g_proxy_new_for_name(connection,
1014 "com.google.code.Awn",
1015 "/com/google/code/Awn",
1016 "com.google.code.Awn");
1017 dbus_item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, dbus_status_update_item_hook, NULL);
1018 if (dbus_item_hook_id == -1) {
1019 g_warning(_("Failed to register folder item update hook"));
1020 uninstall_dbus_status_handler();
1024 dbus_folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, dbus_status_update_folder_hook, NULL);
1025 if (dbus_folder_hook_id == -1) {
1026 g_warning(_("Failed to register folder update hook"));
1027 uninstall_dbus_status_handler();
1033 int main(int argc, char *argv[])
1036 osso_context_t *osso_context;
1037 osso_return_t result;
1039 #ifdef HAVE_DBUS_GLIB
1040 DBusGConnection *connection;
1043 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1044 DBusGProxy *nm_proxy;
1047 MainWindow *mainwin;
1048 FolderView *folderview;
1050 gboolean crash_file_present = FALSE;
1051 gint num_folder_class = 0;
1052 gboolean asked_for_migration = FALSE;
1053 gboolean start_done = TRUE;
1054 GtkUIManager *gui_manager = NULL;
1055 GSList *plug_list = NULL;
1057 START_TIMING("startup");
1064 if (!claws_init(&argc, &argv)) {
1071 main_dump_features_list(TRUE);
1073 prog_version = PROG_VERSION;
1074 argv0 = g_strdup(argv[0]);
1076 parse_cmd_opt(argc, argv);
1078 prefs_prepare_cache();
1083 gtk_init(&argc, &argv);
1084 crash_main(cmd.crash_params);
1090 crash_install_handlers();
1092 install_basic_sighandlers();
1093 #if (defined linux && defined SIGIO)
1094 install_memory_sighandler();
1098 /* check and create unix domain socket for remote operation */
1100 lock_socket = prohibit_duplicate_launch();
1101 if (lock_socket < 0) {
1102 #ifdef HAVE_STARTUP_NOTIFICATION
1103 if(gtk_init_check(&argc, &argv))
1104 startup_notification_complete(TRUE);
1109 if (cmd.status || cmd.status_full) {
1110 puts("0 Claws Mail not running.");
1111 lock_socket_remove();
1118 if (!g_thread_supported())
1119 g_thread_init(NULL);
1122 gtk_init(&argc, &argv);
1124 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1125 went_offline_nm = FALSE;
1128 #ifdef HAVE_DBUS_GLIB
1130 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
1133 debug_print("Failed to open connection to system bus: %s\n", error->message);
1134 g_error_free(error);
1137 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1138 nm_proxy = dbus_g_proxy_new_for_name(connection,
1139 "org.freedesktop.NetworkManager",
1140 "/org/freedesktop/NetworkManager",
1141 "org.freedesktop.NetworkManager");
1143 dbus_g_proxy_add_signal(nm_proxy, "StateChange", G_TYPE_UINT, G_TYPE_INVALID);
1144 dbus_g_proxy_connect_signal(nm_proxy, "StateChange",
1145 G_CALLBACK(networkmanager_state_change_cb),
1149 install_dbus_status_handler();
1155 osso_context = osso_initialize(OSSO_SERVICE, "2.8.1", TRUE, NULL);
1156 if (osso_context == NULL) {
1159 hildon_program = HILDON_PROGRAM(hildon_program_get_instance());
1160 static_osso_context = osso_context;
1162 gtk_widget_set_default_colormap(gdk_rgb_get_colormap());
1164 gui_manager = gtkut_create_ui_manager();
1166 /* Create container for all the menus we will be adding */
1167 MENUITEM_ADDUI("/", "Menus", NULL, GTK_UI_MANAGER_MENUBAR);
1169 if (!g_thread_supported()) {
1170 g_error(_("g_thread is not supported by glib.\n"));
1173 /* check that we're not on a too recent/old gtk+ */
1174 #if GTK_CHECK_VERSION(2, 9, 0)
1175 if (gtk_check_version(2, 9, 0) != NULL) {
1176 alertpanel_error(_("Claws Mail has been compiled with "
1177 "a more recent GTK+ library than is "
1178 "currently available. This will cause "
1179 "crashes. You need to upgrade GTK+ or "
1180 "recompile Claws Mail."));
1187 if (gtk_check_version(2, 9, 0) == NULL) {
1188 alertpanel_error(_("Claws Mail has been compiled with "
1189 "an older GTK+ library than is "
1190 "currently available. This will cause "
1191 "crashes. You need to recompile "
1201 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1203 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1206 /* no config dir exists. See if we can migrate an old config. */
1207 if (!is_dir_exist(RC_DIR)) {
1208 prefs_destroy_cache();
1211 /* if one of the old dirs exist, we'll ask if the user
1212 * want to migrates, and r will be TRUE if he said yes
1213 * and migration succeeded, and FALSE otherwise.
1215 if (is_dir_exist(OLD_GTK2_RC_DIR)) {
1216 r = migrate_old_config(OLD_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 2.6.0 (or older)"));
1217 asked_for_migration = TRUE;
1218 } else if (is_dir_exist(OLDER_GTK2_RC_DIR)) {
1219 r = migrate_old_config(OLDER_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.9.15 (or older)"));
1220 asked_for_migration = TRUE;
1221 } else if (is_dir_exist(OLD_GTK1_RC_DIR)) {
1222 r = migrate_old_config(OLD_GTK1_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.0.5 (or older)"));
1223 asked_for_migration = TRUE;
1224 } else if (is_dir_exist(SYLPHEED_RC_DIR)) {
1225 r = migrate_old_config(SYLPHEED_RC_DIR, RC_DIR, "Sylpheed");
1226 asked_for_migration = TRUE;
1229 /* If migration failed or the user didn't want to do it,
1230 * we create a new one (and we'll hit wizard later).
1232 if (r == FALSE && !is_dir_exist(RC_DIR) && make_dir(RC_DIR) < 0) {
1241 if (!is_file_exist(RC_DIR G_DIR_SEPARATOR_S COMMON_RC) &&
1242 is_file_exist(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC)) {
1243 /* post 2.6 name change */
1244 migrate_common_rc(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC,
1245 RC_DIR G_DIR_SEPARATOR_S COMMON_RC);
1249 plugin_load_all("Common");
1251 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
1252 gtk_rc_parse(userrc);
1255 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1256 gtk_accel_map_load (userrc);
1260 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_rc_dir(), 1, win32_close_log(););
1262 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
1265 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
1266 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
1267 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
1268 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
1269 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
1270 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR);
1272 crash_file_present = is_file_exist(get_crashfile_name());
1273 /* remove temporary files */
1274 remove_all_files(get_tmp_dir());
1275 remove_all_files(get_mime_tmp_dir());
1277 if (!cmd.crash && crash_file_present)
1278 claws_crashed_bool = TRUE;
1280 if (is_file_exist("claws.log")) {
1281 if (rename_force("claws.log", "claws.log.bak") < 0)
1282 FILE_OP_ERROR("claws.log", "rename");
1284 set_log_file(LOG_PROTOCOL, "claws.log");
1286 if (is_file_exist("filtering.log")) {
1287 if (rename_force("filtering.log", "filtering.log.bak") < 0)
1288 FILE_OP_ERROR("filtering.log", "rename");
1290 set_log_file(LOG_DEBUG_FILTERING, "filtering.log");
1293 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1295 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1298 folder_system_init();
1299 prefs_common_read_config();
1301 prefs_themes_init();
1303 prefs_ext_prog_init();
1304 prefs_wrapping_init();
1305 prefs_compose_writing_init();
1306 prefs_msg_colors_init();
1307 image_viewer_init();
1308 prefs_image_viewer_init();
1310 prefs_summaries_init();
1311 prefs_message_init();
1313 prefs_logging_init();
1314 prefs_receive_init();
1318 gtkaspell_checkers_init();
1319 prefs_spelling_init();
1322 sock_set_io_timeout(prefs_common.io_timeout_secs);
1323 prefs_actions_read_config();
1324 prefs_display_header_read_config();
1325 /* prefs_filtering_read_config(); */
1326 addressbook_read_file();
1328 gtkut_widget_init();
1329 stock_pixbuf_gdk(NULL, STOCK_PIXMAP_CLAWS_MAIL_ICON, &icon);
1330 gtk_window_set_default_icon(icon);
1332 folderview_initialize();
1338 mainwin = main_window_create();
1340 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1341 networkmanager_state_change_cb(nm_proxy,NULL,mainwin);
1346 appdata = g_new0(AppData, 1);
1347 appdata->program = hildon_program;
1348 appdata->window = HILDON_WINDOW(mainwin->window);
1349 appdata->osso_context = osso_context;
1350 result = osso_rpc_set_cb_f(appdata->osso_context,
1354 dbus_req_handler, appdata);
1355 if (result != OSSO_OK) {
1360 /* Add handler for Exit D-BUS messages */
1361 result = osso_application_set_exit_cb(appdata->osso_context,
1363 (gpointer) appdata);
1364 if (result != OSSO_OK) {
1368 osso_hw_set_event_cb( appdata->osso_context,
1369 NULL, hw_event_handler, (gpointer) appdata );
1371 manage_window_focus_in(mainwin->window, NULL, NULL);
1372 folderview = mainwin->folderview;
1374 gtk_cmclist_freeze(GTK_CMCLIST(mainwin->folderview->ctree));
1375 folder_item_update_freeze();
1377 /* register the callback of unix domain socket input */
1379 lock_socket_tag = claws_input_add(lock_socket,
1380 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
1381 lock_socket_input_cb,
1386 prefs_account_init();
1387 account_read_config_all();
1389 /* If we can't read a folder list or don't have accounts,
1390 * it means the configuration's not done. Either this is
1391 * a brand new install, either a failed/refused migration.
1392 * So we'll start the wizard.
1394 if (folder_read_list() < 0) {
1395 prefs_destroy_cache();
1397 /* if run_wizard returns FALSE it's because it's
1398 * been cancelled. We can't do much but exit.
1399 * however, if the user was asked for a migration,
1400 * we remove the newly created directory so that
1401 * he's asked again for migration on next launch.*/
1402 if (!run_wizard(mainwin, TRUE)) {
1403 if (asked_for_migration)
1404 remove_dir_recursive(RC_DIR);
1410 main_window_reflect_prefs_all_now();
1411 folder_write_list();
1414 if (!account_get_list()) {
1415 prefs_destroy_cache();
1416 if (!run_wizard(mainwin, FALSE)) {
1417 if (asked_for_migration)
1418 remove_dir_recursive(RC_DIR);
1424 if(!account_get_list()) {
1425 exit_claws(mainwin);
1434 toolbar_main_set_sensitive(mainwin);
1435 main_window_set_menu_sensitive(mainwin);
1437 /* if crashed, show window early so that the user
1438 * sees what's happening */
1439 if (claws_crashed())
1440 main_window_popup(mainwin);
1443 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
1446 #ifdef HAVE_LIBETPAN
1447 imap_main_init(prefs_common.skip_ssl_cert_check);
1448 imap_main_set_timeout(prefs_common.io_timeout_secs);
1449 nntp_main_init(prefs_common.skip_ssl_cert_check);
1451 account_set_missing_folder();
1452 folder_set_missing_folders();
1453 folderview_set(folderview);
1455 prefs_matcher_read_config();
1457 /* make one all-folder processing before using claws */
1458 main_window_cursor_wait(mainwin);
1459 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
1461 /* if claws crashed, rebuild caches */
1462 if (claws_crashed()) {
1464 debug_print("Claws Mail crashed, checking for new messages in local folders\n");
1465 folder_item_update_thaw();
1466 folderview_check_new(NULL);
1467 folder_clean_cache_memory_force();
1468 folder_item_update_freeze();
1470 /* make the crash-indicator file */
1471 str_write_to_file("foo", get_crashfile_name());
1473 inc_autocheck_timer_init(mainwin);
1475 /* ignore SIGPIPE signal for preventing sudden death of program */
1477 signal(SIGPIPE, SIG_IGN);
1479 if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
1480 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1482 if (cmd.online_mode == ONLINE_MODE_ONLINE) {
1483 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1486 if (cmd.status_folders) {
1487 g_ptr_array_free(cmd.status_folders, TRUE);
1488 cmd.status_folders = NULL;
1490 if (cmd.status_full_folders) {
1491 g_ptr_array_free(cmd.status_full_folders, TRUE);
1492 cmd.status_full_folders = NULL;
1495 claws_register_idle_function(claws_gtk_idle);
1497 prefs_toolbar_init();
1499 num_folder_class = g_list_length(folder_get_list());
1501 plugin_load_all("GTK2");
1503 if (g_list_length(folder_get_list()) != num_folder_class) {
1504 debug_print("new folders loaded, reloading processing rules\n");
1505 prefs_matcher_read_config();
1508 if ((plug_list = plugin_get_unloaded_list()) != NULL) {
1511 gint num_plugins = 0;
1512 for (cur = plug_list; cur; cur = cur->next) {
1513 Plugin *plugin = (Plugin *)cur->data;
1514 gchar *tmp = g_strdup_printf("%s\n%s",
1516 plugin_get_name(plugin));
1521 main_window_cursor_normal(mainwin);
1522 alertpanel_warning(ngettext(
1523 "The following plugin failed to load. "
1524 "Check the Plugins configuration "
1525 "for more information:\n%s",
1526 "The following plugins failed to load. "
1527 "Check the Plugins configuration "
1528 "for more information:\n%s",
1531 main_window_cursor_wait(mainwin);
1533 g_slist_free(plug_list);
1536 plugin_load_standard_plugins ();
1538 /* if not crashed, show window now */
1539 if (!claws_crashed()) {
1540 /* apart if something told not to show */
1541 if (show_at_startup)
1542 main_window_popup(mainwin);
1545 if (!folder_have_mailbox()) {
1546 prefs_destroy_cache();
1547 main_window_cursor_normal(mainwin);
1548 if (folder_get_list() != NULL) {
1549 alertpanel_error(_("Claws Mail has detected a configured "
1550 "mailbox, but it is incomplete. It is "
1551 "possibly due to a failing IMAP account. Use "
1552 "\"Rebuild folder tree\" on the mailbox parent "
1553 "folder's context menu to try to fix it."));
1555 alertpanel_error(_("Claws Mail has detected a configured "
1556 "mailbox, but could not load it. It is "
1557 "probably provided by an out-of-date "
1558 "external plugin. Please reinstall the "
1559 "plugin and try again."));
1560 exit_claws(mainwin);
1568 static_mainwindow = mainwin;
1571 if (prefs_common.data_root != NULL && *prefs_common.data_root != '\0') {
1572 GnomeVFSVolume *vol = NULL;
1573 gchar *uri, *mount_path;
1575 volmon = gnome_vfs_get_volume_monitor();
1576 vol = gnome_vfs_volume_monitor_get_volume_for_path(volmon, prefs_common.data_root);
1578 uri = gnome_vfs_volume_get_activation_uri (vol);
1579 mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
1582 if (vol == NULL || !gnome_vfs_volume_is_mounted(vol)
1583 || strcmp(mount_path, prefs_common.data_root)) {
1584 alertpanel_error(_("Claws Mail can not start without its data volume (%s)."),
1585 prefs_common.data_root);
1587 gnome_vfs_volume_unref(vol);
1588 exit_claws(mainwin);
1592 gnome_vfs_volume_unref(vol);
1593 g_signal_connect(G_OBJECT(volmon),
1594 "volume-mounted", G_CALLBACK(main_vol_mount_cb), mainwin);
1595 g_signal_connect(G_OBJECT(volmon),
1596 "volume-unmounted", G_CALLBACK(main_vol_unmount_cb), mainwin);
1600 #ifdef HAVE_STARTUP_NOTIFICATION
1601 startup_notification_complete(FALSE);
1604 sc_session_manager_connect(mainwin);
1607 folder_item_update_thaw();
1608 gtk_cmclist_thaw(GTK_CMCLIST(mainwin->folderview->ctree));
1609 main_window_cursor_normal(mainwin);
1611 if (!cmd.target && prefs_common.goto_last_folder_on_startup &&
1612 folder_find_item_from_identifier(prefs_common.last_opened_folder) != NULL &&
1614 cmd.target = prefs_common.last_opened_folder;
1617 if (cmd.receive_all && !cmd.target) {
1619 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(FALSE));
1620 } else if (prefs_common.chk_on_startup && !cmd.target) {
1622 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(TRUE));
1623 } else if (cmd.receive && !cmd.target) {
1625 g_timeout_add(1000, defer_check, NULL);
1627 gtk_widget_grab_focus(folderview->ctree);
1630 open_compose_new(cmd.compose_mailto, cmd.attach_files);
1632 if (cmd.attach_files) {
1633 ptr_array_free_strings(cmd.attach_files);
1634 g_ptr_array_free(cmd.attach_files, TRUE);
1635 cmd.attach_files = NULL;
1637 if (cmd.subscribe) {
1638 folder_subscribe(cmd.subscribe_uri);
1647 g_timeout_add(500, defer_jump, (gpointer)cmd.target);
1650 prefs_destroy_cache();
1652 compose_reopen_exit_drafts();
1655 sc_starting = FALSE;
1656 main_window_set_menu_sensitive(mainwin);
1657 toolbar_main_set_sensitive(mainwin);
1664 osso_deinitialize(osso_context);
1666 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1668 g_object_unref(nm_proxy);
1670 #ifdef HAVE_DBUS_GLIB
1671 uninstall_dbus_status_handler();
1673 dbus_g_connection_unref(connection);
1679 exit_claws(mainwin);
1684 static void save_all_caches(FolderItem *item, gpointer data)
1691 folder_item_close(item);
1694 folder_item_free_cache(item, TRUE);
1697 static void exit_claws(MainWindow *mainwin)
1703 debug_print("shutting down\n");
1704 inc_autocheck_timer_remove();
1706 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1707 if (prefs_common.work_offline && went_offline_nm)
1708 prefs_common.work_offline = FALSE;
1711 /* save prefs for opened folder */
1712 if(mainwin->folderview->opened) {
1715 item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
1716 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
1717 prefs_common.last_opened_folder = folder_item_get_identifier(item);
1720 /* save all state before exiting */
1721 folder_func_to_all_folders(save_all_caches, NULL);
1722 folder_write_list();
1724 main_window_get_size(mainwin);
1725 main_window_get_position(mainwin);
1727 prefs_common_write_config();
1728 account_write_config_all();
1729 addressbook_export_to_file();
1731 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1732 gtk_accel_map_save(filename);
1735 /* delete temporary files */
1736 remove_all_files(get_tmp_dir());
1737 remove_all_files(get_mime_tmp_dir());
1739 close_log_file(LOG_PROTOCOL);
1740 close_log_file(LOG_DEBUG_FILTERING);
1742 #ifdef HAVE_LIBETPAN
1746 /* delete crashfile */
1748 claws_unlink(get_crashfile_name());
1750 lock_socket_remove();
1753 if (mainwin->smc_conn)
1754 SmcCloseConnection ((SmcConn)mainwin->smc_conn, 0, NULL);
1755 mainwin->smc_conn = NULL;
1758 main_window_destroy_all();
1760 plugin_unload_all("GTK2");
1762 prefs_toolbar_done();
1764 addressbook_destroy();
1766 prefs_themes_done();
1768 prefs_ext_prog_done();
1769 prefs_wrapping_done();
1770 prefs_compose_writing_done();
1771 prefs_msg_colors_done();
1772 prefs_image_viewer_done();
1773 image_viewer_done();
1775 prefs_summaries_done();
1776 prefs_message_done();
1778 prefs_receive_done();
1779 prefs_logging_done();
1783 prefs_spelling_done();
1784 gtkaspell_checkers_quit();
1786 plugin_unload_all("Common");
1790 static void parse_cmd_opt(int argc, char *argv[])
1794 for (i = 1; i < argc; i++) {
1795 if (!strncmp(argv[i], "--receive-all", 13)) {
1796 cmd.receive_all = TRUE;
1797 } else if (!strncmp(argv[i], "--receive", 9)) {
1799 } else if (!strncmp(argv[i], "--compose", 9)) {
1800 const gchar *p = argv[i + 1];
1803 cmd.compose_mailto = NULL;
1804 if (p && *p != '\0' && *p != '-') {
1805 if (!strncmp(p, "mailto:", 7)) {
1806 cmd.compose_mailto = p + 7;
1808 cmd.compose_mailto = p;
1812 } else if (!strncmp(argv[i], "--subscribe", 11)) {
1813 const gchar *p = argv[i + 1];
1814 if (p && *p != '\0' && *p != '-') {
1815 cmd.subscribe = TRUE;
1816 cmd.subscribe_uri = p;
1818 } else if (!strncmp(argv[i], "--attach", 8)) {
1819 const gchar *p = argv[i + 1];
1822 while (p && *p != '\0' && *p != '-') {
1823 if (!cmd.attach_files) {
1824 cmd.attach_files = g_ptr_array_new();
1826 if ((file = g_filename_from_uri(p, NULL, NULL)) != NULL) {
1827 if (!is_file_exist(file)) {
1832 if (file == NULL && *p != G_DIR_SEPARATOR) {
1833 file = g_strconcat(claws_get_startup_dir(),
1836 } else if (file == NULL) {
1839 g_ptr_array_add(cmd.attach_files, file);
1843 } else if (!strncmp(argv[i], "--send", 6)) {
1845 } else if (!strncmp(argv[i], "--version-full", 14) ||
1846 !strncmp(argv[i], "-V", 2)) {
1847 g_print("Claws Mail version " VERSION "\n");
1848 main_dump_features_list(FALSE);
1850 } else if (!strncmp(argv[i], "--version", 9) ||
1851 !strncmp(argv[i], "-v", 2)) {
1852 g_print("Claws Mail version " VERSION "\n");
1854 } else if (!strncmp(argv[i], "--status-full", 13)) {
1855 const gchar *p = argv[i + 1];
1857 cmd.status_full = TRUE;
1858 while (p && *p != '\0' && *p != '-') {
1859 if (!cmd.status_full_folders) {
1860 cmd.status_full_folders =
1863 g_ptr_array_add(cmd.status_full_folders,
1868 } else if (!strncmp(argv[i], "--status", 8)) {
1869 const gchar *p = argv[i + 1];
1872 while (p && *p != '\0' && *p != '-') {
1873 if (!cmd.status_folders)
1874 cmd.status_folders = g_ptr_array_new();
1875 g_ptr_array_add(cmd.status_folders,
1880 } else if (!strncmp(argv[i], "--online", 8)) {
1881 cmd.online_mode = ONLINE_MODE_ONLINE;
1882 } else if (!strncmp(argv[i], "--offline", 9)) {
1883 cmd.online_mode = ONLINE_MODE_OFFLINE;
1884 } else if (!strncmp(argv[i], "--help", 6) ||
1885 !strncmp(argv[i], "-h", 2)) {
1886 gchar *base = g_path_get_basename(argv[0]);
1887 g_print(_("Usage: %s [OPTION]...\n"), base);
1889 g_print("%s\n", _(" --compose [address] open composition window"));
1890 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
1891 g_print("%s\n", _(" --attach file1 [file2]...\n"
1892 " open composition window with specified files\n"
1894 g_print("%s\n", _(" --receive receive new messages"));
1895 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
1896 g_print("%s\n", _(" --send send all queued messages"));
1897 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
1898 g_print("%s\n", _(" --status-full [folder]...\n"
1899 " show the status of each folder"));
1900 g_print("%s\n", _(" --select folder[/msg] jumps to the specified folder/message\n"
1901 " folder is a folder id like 'folder/sub_folder'"));
1902 g_print("%s\n", _(" --online switch to online mode"));
1903 g_print("%s\n", _(" --offline switch to offline mode"));
1904 g_print("%s\n", _(" --exit --quit -q exit Claws Mail"));
1905 g_print("%s\n", _(" --debug debug mode"));
1906 g_print("%s\n", _(" --help -h display this help and exit"));
1907 g_print("%s\n", _(" --version -v output version information and exit"));
1908 g_print("%s\n", _(" --version-full -V output version and built-in features information and exit"));
1909 g_print("%s\n", _(" --config-dir output configuration directory"));
1910 g_print("%s\n", _(" --alternate-config-dir [dir]\n"
1911 " use specified configuration directory"));
1915 } else if (!strncmp(argv[i], "--crash", 7)) {
1917 cmd.crash_params = g_strdup(argv[i + 1]);
1919 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
1920 g_print(RC_DIR "\n");
1922 } else if (!strncmp(argv[i], "--alternate-config-dir", sizeof "--alternate-config-dir" - 1) && i+1 < argc) {
1923 set_rc_dir(argv[i+1]);
1924 } else if (!strncmp(argv[i], "--exit", 6) ||
1925 !strncmp(argv[i], "--quit", 6) ||
1926 !strncmp(argv[i], "-q", 2)) {
1928 } else if (!strncmp(argv[i], "--select", 8) && i+1 < argc) {
1929 cmd.target = argv[i+1];
1930 } else if (i == 1 && argc == 2) {
1931 /* only one parameter. Do something intelligent about it */
1932 if ((strstr(argv[i], "@")||!strncmp(argv[i], "mailto:", 7)) && !strstr(argv[i], "://")) {
1933 const gchar *p = argv[i];
1936 cmd.compose_mailto = NULL;
1937 if (p && *p != '\0' && *p != '-') {
1938 if (!strncmp(p, "mailto:", 7)) {
1939 cmd.compose_mailto = p + 7;
1941 cmd.compose_mailto = p;
1944 } else if (!strncmp(argv[i], "file://", 7)) {
1945 cmd.target = argv[i];
1946 } else if (!strncmp(argv[i], "?attach=file://", strlen("?attach=file://"))) {
1948 cmd.compose_mailto = argv[i];
1949 } else if (strstr(argv[i], "://")) {
1950 const gchar *p = argv[i];
1951 if (p && *p != '\0' && *p != '-') {
1952 cmd.subscribe = TRUE;
1953 cmd.subscribe_uri = p;
1955 } else if (!strcmp(argv[i], "--sync")) {
1957 } else if (is_dir_exist(argv[i]) || is_file_exist(argv[i])) {
1958 cmd.target = argv[i];
1960 g_print(_("Unknown option\n"));
1966 if (cmd.attach_files && cmd.compose == FALSE) {
1968 cmd.compose_mailto = NULL;
1972 static void initial_processing(FolderItem *item, gpointer data)
1974 MainWindow *mainwin = (MainWindow *)data;
1977 g_return_if_fail(item);
1978 buf = g_strdup_printf(_("Processing (%s)..."),
1981 : _("top level folder"));
1985 if (item->prefs->enable_processing) {
1986 item->processing_pending = TRUE;
1987 folder_item_apply_processing(item);
1988 item->processing_pending = FALSE;
1991 STATUSBAR_POP(mainwin);
1994 static gboolean draft_all_messages(void)
1996 GList *compose_list = NULL;
1998 compose_clear_exit_drafts();
1999 compose_list = compose_get_compose_list();
2000 while (compose_list != NULL) {
2001 Compose *c = (Compose*)compose_list->data;
2002 if (!compose_draft(c, COMPOSE_DRAFT_FOR_EXIT))
2004 compose_list = compose_get_compose_list();
2008 gboolean clean_quit(gpointer data)
2010 static gboolean firstrun = TRUE;
2017 /*!< Good idea to have the main window stored in a
2018 * static variable so we can check that variable
2019 * to see if we're really allowed to do things
2020 * that actually the spawner is supposed to
2021 * do (like: sending mail, composing messages).
2022 * Because, really, if we're the spawnee, and
2023 * we touch GTK stuff, we're hosed. See the
2026 /* FIXME: Use something else to signal that we're
2027 * in the original spawner, and not in a spawned
2029 if (!static_mainwindow) {
2033 draft_all_messages();
2034 emergency_exit = TRUE;
2035 exit_claws(static_mainwindow);
2041 void app_will_exit(GtkWidget *widget, gpointer data)
2043 MainWindow *mainwin = data;
2045 if (gtk_main_level() == 0) {
2046 debug_print("not even started\n");
2049 if (sc_exiting == TRUE) {
2050 debug_print("exit pending\n");
2054 debug_print("exiting\n");
2055 if (compose_get_compose_list()) {
2056 if (!draft_all_messages()) {
2057 main_window_popup(mainwin);
2063 if (prefs_common.warn_queued_on_exit && procmsg_have_queued_mails_fast()) {
2064 if (alertpanel(_("Queued messages"),
2065 _("Some unsent messages are queued. Exit now?"),
2066 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL)
2067 != G_ALERTALTERNATE) {
2068 main_window_popup(mainwin);
2072 manage_window_focus_in(mainwin->window, NULL, NULL);
2076 #ifdef HAVE_VALGRIND
2077 if (RUNNING_ON_VALGRIND) {
2078 summary_clear_list(mainwin->summaryview);
2081 if (folderview_get_selected_item(mainwin->folderview))
2082 folder_item_close(folderview_get_selected_item(mainwin->folderview));
2086 gboolean claws_is_exiting(void)
2091 gboolean claws_is_starting(void)
2097 * CLAWS: want this public so crash dialog can delete the
2100 gchar *claws_get_socket_name(void)
2102 static gchar *filename = NULL;
2103 const gchar *socket_dir = NULL;
2105 if (rc_dir_is_alt())
2106 socket_dir = get_rc_dir();
2108 socket_dir = g_get_tmp_dir();
2109 if (filename == NULL) {
2110 filename = g_strdup_printf("%s%cclaws-mail-%d",
2111 socket_dir, G_DIR_SEPARATOR,
2122 static gchar *get_crashfile_name(void)
2124 static gchar *filename = NULL;
2126 if (filename == NULL) {
2127 filename = g_strdup_printf("%s%cclaws-crashed",
2128 get_tmp_dir(), G_DIR_SEPARATOR);
2134 static gint prohibit_duplicate_launch(void)
2139 path = claws_get_socket_name();
2140 uxsock = fd_connect_unix(path);
2142 if (x_display == NULL)
2143 x_display = g_strdup(g_getenv("DISPLAY"));
2147 return fd_open_unix(path);
2150 /* remote command mode */
2152 debug_print("another Claws Mail instance is already running.\n");
2154 if (cmd.receive_all) {
2155 fd_write_all(uxsock, "receive_all\n", 12);
2156 } else if (cmd.receive) {
2157 fd_write_all(uxsock, "receive\n", 8);
2158 } else if (cmd.compose && cmd.attach_files) {
2159 gchar *str, *compose_str;
2162 if (cmd.compose_mailto) {
2163 compose_str = g_strdup_printf("compose_attach %s\n",
2164 cmd.compose_mailto);
2166 compose_str = g_strdup("compose_attach\n");
2169 fd_write_all(uxsock, compose_str, strlen(compose_str));
2170 g_free(compose_str);
2172 for (i = 0; i < cmd.attach_files->len; i++) {
2173 str = g_ptr_array_index(cmd.attach_files, i);
2174 fd_write_all(uxsock, str, strlen(str));
2175 fd_write_all(uxsock, "\n", 1);
2178 fd_write_all(uxsock, ".\n", 2);
2179 } else if (cmd.compose) {
2182 if (cmd.compose_mailto) {
2183 compose_str = g_strdup_printf
2184 ("compose %s\n", cmd.compose_mailto);
2186 compose_str = g_strdup("compose\n");
2189 fd_write_all(uxsock, compose_str, strlen(compose_str));
2190 g_free(compose_str);
2191 } else if (cmd.subscribe) {
2192 gchar *str = g_strdup_printf("subscribe %s\n", cmd.subscribe_uri);
2193 fd_write_all(uxsock, str, strlen(str));
2195 } else if (cmd.send) {
2196 fd_write_all(uxsock, "send\n", 5);
2197 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
2198 fd_write(uxsock, "online\n", 6);
2199 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
2200 fd_write(uxsock, "offline\n", 7);
2201 } else if (cmd.status || cmd.status_full) {
2202 gchar buf[BUFFSIZE];
2204 const gchar *command;
2208 command = cmd.status_full ? "status-full\n" : "status\n";
2209 folders = cmd.status_full ? cmd.status_full_folders :
2212 fd_write_all(uxsock, command, strlen(command));
2213 for (i = 0; folders && i < folders->len; ++i) {
2214 folder = g_ptr_array_index(folders, i);
2215 fd_write_all(uxsock, folder, strlen(folder));
2216 fd_write_all(uxsock, "\n", 1);
2218 fd_write_all(uxsock, ".\n", 2);
2220 fd_gets(uxsock, buf, sizeof(buf));
2221 if (!strncmp(buf, ".\n", 2)) break;
2224 } else if (cmd.exit) {
2225 fd_write_all(uxsock, "exit\n", 5);
2226 } else if (cmd.target) {
2227 gchar *str = g_strdup_printf("select %s\n", cmd.target);
2228 fd_write_all(uxsock, str, strlen(str));
2232 fd_write_all(uxsock, "get_display\n", 12);
2233 memset(buf, 0, sizeof(buf));
2234 fd_gets(uxsock, buf, sizeof(buf));
2235 if (strcmp2(buf, x_display)) {
2236 g_print("Claws Mail is already running on display %s.\n",
2240 uxsock = fd_connect_unix(path);
2241 fd_write_all(uxsock, "popup\n", 6);
2249 static gint lock_socket_remove(void)
2254 if (lock_socket < 0) {
2258 if (lock_socket_tag > 0) {
2259 g_source_remove(lock_socket_tag);
2261 fd_close(lock_socket);
2262 filename = claws_get_socket_name();
2263 claws_unlink(filename);
2269 static GPtrArray *get_folder_item_list(gint sock)
2271 gchar buf[BUFFSIZE];
2273 GPtrArray *folders = NULL;
2276 fd_gets(sock, buf, sizeof(buf));
2277 if (!strncmp(buf, ".\n", 2)) {
2282 folders = g_ptr_array_new();
2284 item = folder_find_item_from_identifier(buf);
2286 g_ptr_array_add(folders, item);
2288 g_warning("no such folder: %s\n", buf);
2295 static void lock_socket_input_cb(gpointer data,
2297 GdkInputCondition condition)
2299 MainWindow *mainwin = (MainWindow *)data;
2301 gchar buf[BUFFSIZE];
2303 sock = fd_accept(source);
2304 fd_gets(sock, buf, sizeof(buf));
2306 if (!strncmp(buf, "popup", 5)) {
2307 main_window_popup(mainwin);
2308 } else if (!strncmp(buf, "get_display", 11)) {
2309 fd_write_all(sock, x_display, strlen(x_display));
2310 } else if (!strncmp(buf, "receive_all", 11)) {
2311 inc_all_account_mail(mainwin, FALSE,
2312 prefs_common.newmail_notify_manu);
2313 } else if (!strncmp(buf, "receive", 7)) {
2314 inc_mail(mainwin, prefs_common.newmail_notify_manu);
2315 } else if (!strncmp(buf, "compose_attach", 14)) {
2319 mailto = g_strdup(buf + strlen("compose_attach") + 1);
2320 files = g_ptr_array_new();
2321 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
2322 if (buf[0] == '.' && buf[1] == '\n') {
2326 g_ptr_array_add(files, g_strdup(buf));
2328 open_compose_new(mailto, files);
2329 ptr_array_free_strings(files);
2330 g_ptr_array_free(files, TRUE);
2332 } else if (!strncmp(buf, "compose", 7)) {
2333 open_compose_new(buf + strlen("compose") + 1, NULL);
2334 } else if (!strncmp(buf, "subscribe", 9)) {
2335 main_window_popup(mainwin);
2336 folder_subscribe(buf + strlen("subscribe") + 1);
2337 } else if (!strncmp(buf, "send", 4)) {
2339 } else if (!strncmp(buf, "online", 6)) {
2340 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
2341 } else if (!strncmp(buf, "offline", 7)) {
2342 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
2343 } else if (!strncmp(buf, "status-full", 11) ||
2344 !strncmp(buf, "status", 6)) {
2348 folders = get_folder_item_list(sock);
2349 status = folder_get_status
2350 (folders, !strncmp(buf, "status-full", 11));
2351 fd_write_all(sock, status, strlen(status));
2352 fd_write_all(sock, ".\n", 2);
2354 if (folders) g_ptr_array_free(folders, TRUE);
2355 } else if (!strncmp(buf, "select ", 7)) {
2356 const gchar *target = buf+7;
2357 mainwindow_jump_to(target, TRUE);
2358 } else if (!strncmp(buf, "exit", 4)) {
2359 app_will_exit(NULL, mainwin);
2365 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
2370 Xstrdup_a(addr, address, return);
2374 compose_new(NULL, addr, attach_files);
2377 static void send_queue(void)
2380 gchar *errstr = NULL;
2381 gboolean error = FALSE;
2382 for (list = folder_get_list(); list != NULL; list = list->next) {
2383 Folder *folder = list->data;
2385 if (folder->queue) {
2386 gint res = procmsg_send_queue
2387 (folder->queue, prefs_common.savemsg,
2391 folder_item_scan(folder->queue);
2399 alertpanel_error_log(_("Some errors occurred "
2400 "while sending queued messages:\n%s"), errstr);
2403 alertpanel_error_log("Some errors occurred "
2404 "while sending queued messages.");
2408 static void quit_signal_handler(int sig)
2410 debug_print("Quitting on signal %d\n", sig);
2412 g_timeout_add(0, clean_quit, NULL);
2415 static void install_basic_sighandlers()
2419 struct sigaction act;
2424 sigaddset(&mask, SIGTERM);
2427 sigaddset(&mask, SIGINT);
2430 sigaddset(&mask, SIGHUP);
2433 act.sa_handler = quit_signal_handler;
2438 sigaction(SIGTERM, &act, 0);
2441 sigaction(SIGINT, &act, 0);
2444 sigaction(SIGHUP, &act, 0);
2447 sigprocmask(SIG_UNBLOCK, &mask, 0);
2448 #endif /* !G_OS_WIN32 */
2451 #if (defined linux && defined SIGIO)
2452 static int mem_notify_fd = 0;
2454 static gboolean clean_caches(gpointer unused)
2456 if (static_mainwindow && static_mainwindow->lock_count > 0)
2458 debug_print("/dev/mem_notify: callback: Freeing some memory now!\n");
2459 folder_clean_cache_memory_force();
2463 static void memory_signal_handler(int sig)
2465 debug_print("/dev/mem_notify: Kernel says we should free up some memory!\n");
2466 g_timeout_add(10, clean_caches, NULL);
2469 static void install_memory_sighandler()
2472 struct sigaction act;
2475 mem_notify_fd = open("/dev/mem_notify", O_RDONLY|O_NONBLOCK);
2476 if (mem_notify_fd == -1) {
2477 debug_print("/dev/mem_notify not available (%s)\n",
2482 fcntl(mem_notify_fd, F_SETOWN, getpid());
2483 flags = fcntl(mem_notify_fd, F_GETFL);
2484 fcntl(mem_notify_fd, flags|FASYNC);
2488 sigaddset(&mask, SIGIO);
2490 act.sa_handler = memory_signal_handler;
2494 sigaction(SIGIO, &act, 0);
2496 sigprocmask(SIG_UNBLOCK, &mask, 0);
2498 debug_print("/dev/mem_notify: installed handler\n");
2500 #endif /* linux && SIGIO */
2503 osso_context_t *get_osso_context(void)
2505 return static_osso_context;
2510 #ifdef HAVE_NETWORKMANAGER_SUPPORT
2511 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
2514 MainWindow *mainWin;
2517 if (static_mainwindow)
2518 mainWin = static_mainwindow;
2520 mainWin = (MainWindow*)data;
2522 if (!prefs_common.use_networkmanager)
2530 online = networkmanager_is_online(&error);
2532 if(online && went_offline_nm) {
2533 went_offline_nm = FALSE;
2534 main_window_toggle_work_offline(mainWin, FALSE, FALSE);
2535 debug_print("NetworkManager: Went online\n");
2536 log_message(LOG_PROTOCOL, _("NetworkManager: network is online.\n"));
2539 went_offline_nm = TRUE;
2540 main_window_toggle_work_offline(mainWin, TRUE, FALSE);
2541 debug_print("NetworkManager: Went offline\n");
2542 log_message(LOG_PROTOCOL, _("NetworkManager: network is offline.\n"));
2546 debug_print("Failed to get online information from NetworkManager: %s\n",
2548 g_error_free(error);
2552 debug_print("NetworkManager: Cannot change connection state because "
2553 "main window does not exist\n");
2556 /* Returns true (and sets error appropriately, if given) in case of error */
2557 gboolean networkmanager_is_online(GError **error)
2559 DBusGConnection *connection;
2561 GError *tmp_error = NULL;
2565 if (!prefs_common.use_networkmanager)
2570 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &tmp_error);
2573 /* If calling code doesn't do error checking, at least print some debug */
2574 if((error == NULL) || (*error == NULL))
2575 debug_print("Failed to open connection to system bus: %s\n",
2576 tmp_error->message);
2577 g_propagate_error(error, tmp_error);
2581 proxy = dbus_g_proxy_new_for_name(connection,
2582 "org.freedesktop.NetworkManager",
2583 "/org/freedesktop/NetworkManager",
2584 "org.freedesktop.NetworkManager");
2586 retVal = dbus_g_proxy_call(proxy,"state",&tmp_error, G_TYPE_INVALID,
2587 G_TYPE_UINT, &state, G_TYPE_INVALID);
2590 g_object_unref(proxy);
2592 dbus_g_connection_unref(connection);
2595 /* If calling code doesn't do error checking, at least print some debug */
2596 if((error == NULL) || (*error == NULL))
2597 debug_print("Failed to get state info from NetworkManager: %s\n",
2598 tmp_error->message);
2599 g_propagate_error(error, tmp_error);
2603 return (state == NM_STATE_CONNECTED || state == NM_STATE_UNKNOWN);