2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2009 Hiroyuki Yamamoto and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include <glib/gi18n.h>
37 #include <sys/types.h>
44 #include <X11/SM/SMlib.h>
48 #ifdef HAVE_STARTUP_NOTIFICATION
49 # define SN_API_NOT_YET_FROZEN
50 # include <libsn/sn-launchee.h>
51 # include <gdk/gdkx.h>
55 #include <dbus/dbus-glib.h>
57 #ifdef HAVE_NETWORKMANAGER_SUPPORT
58 #include <NetworkManager.h>
63 #include "mainwindow.h"
64 #include "folderview.h"
65 #include "image_viewer.h"
66 #include "summaryview.h"
67 #include "prefs_common.h"
68 #include "prefs_account.h"
69 #include "prefs_actions.h"
70 #include "prefs_ext_prog.h"
71 #include "prefs_fonts.h"
72 #include "prefs_image_viewer.h"
73 #include "prefs_message.h"
74 #include "prefs_receive.h"
75 #include "prefs_msg_colors.h"
76 #include "prefs_quote.h"
77 #include "prefs_spelling.h"
78 #include "prefs_summaries.h"
79 #include "prefs_themes.h"
80 #include "prefs_other.h"
81 #include "prefs_logging.h"
82 #include "prefs_send.h"
83 #include "prefs_wrapping.h"
84 #include "prefs_compose_writing.h"
85 #include "prefs_display_header.h"
90 #include "manage_window.h"
91 #include "alertpanel.h"
92 #include "statusbar.h"
93 #include "addressbook.h"
101 #include "prefs_toolbar.h"
104 #include "imap_gtk.h"
105 #include "news_gtk.h"
112 #include "imap-thread.h"
113 #include "nntp-thread.h"
115 #include "stock_pixmap.h"
117 #include "valgrind.h"
131 #include <hildon/hildon-banner.h>
132 #include <hildon/hildon-program.h>
134 #include <hildon-widgets/hildon-banner.h>
135 #include <hildon-widgets/hildon-program.h>
138 #include <libgnomevfs/gnome-vfs-volume.h>
139 #include <libgnomevfs/gnome-vfs-volume-monitor.h>
140 #include <libgnomevfs/gnome-vfs-utils.h>
142 #define OSSO_NAME "claws_mail"
143 #define OSSO_SERVICE "com.nokia."OSSO_NAME
144 #define OSSO_OBJECT "/com/nokia/"OSSO_NAME
145 #define OSSO_IFACE "com.nokia."OSSO_NAME
147 typedef struct _AppData AppData;
149 HildonProgram *program;
150 HildonWindow *window;
151 osso_context_t *osso_context;
154 static GnomeVFSVolumeMonitor *volmon;
157 #ifdef HAVE_NETWORKMANAGER_SUPPORT
158 /* Went offline due to NetworkManager */
159 static gboolean went_offline_nm;
162 #ifdef HAVE_DBUS_GLIB
163 static DBusGProxy *awn_proxy = NULL;
170 HildonProgram *hildon_program;
173 #ifdef HAVE_STARTUP_NOTIFICATION
174 static SnLauncheeContext *sn_context = NULL;
175 static SnDisplay *sn_display = NULL;
178 static gint lock_socket = -1;
179 static gint lock_socket_tag = 0;
180 static gchar *x_display = NULL;
183 ONLINE_MODE_DONT_CHANGE,
188 static struct RemoteCmd {
190 gboolean receive_all;
192 const gchar *compose_mailto;
193 GPtrArray *attach_files;
195 gboolean status_full;
196 GPtrArray *status_folders;
197 GPtrArray *status_full_folders;
204 const gchar *subscribe_uri;
208 static void parse_cmd_opt(int argc, char *argv[]);
210 static gint prohibit_duplicate_launch (void);
211 static gchar * get_crashfile_name (void);
212 static gint lock_socket_remove (void);
213 static void lock_socket_input_cb (gpointer data,
215 GdkInputCondition condition);
217 static void open_compose_new (const gchar *address,
218 GPtrArray *attach_files);
220 static void send_queue (void);
221 static void initial_processing (FolderItem *item, gpointer data);
222 static void quit_signal_handler (int sig);
223 static void install_basic_sighandlers (void);
224 #if (defined linux && defined SIGIO)
225 static void install_memory_sighandler (void);
227 static void exit_claws (MainWindow *mainwin);
229 #ifdef HAVE_NETWORKMANAGER_SUPPORT
230 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
234 #define MAKE_DIR_IF_NOT_EXIST(dir) \
236 if (!is_dir_exist(dir)) { \
237 if (is_file_exist(dir)) { \
239 (_("File '%s' already exists.\n" \
240 "Can't create folder."), \
244 if (make_dir(dir) < 0) \
249 static MainWindow *static_mainwindow;
252 static osso_context_t *static_osso_context;
254 void exit_event_handler(gboolean die_now, gpointer data)
257 appdata = (AppData *) data;
258 /* Do whatever application needs to do before exiting */
259 exit_claws(static_mainwindow);
260 hildon_banner_show_information(GTK_WIDGET(appdata->window), NULL,
265 /* Callback for hardware D-BUS events */
266 void hw_event_handler(osso_hw_state_t *state, gpointer data)
269 appdata = (AppData *) data;
271 if (state->shutdown_ind) {
272 exit_claws(static_mainwindow);
273 hildon_banner_show_information(GTK_WIDGET(appdata->window), NULL,
278 /* Callback for normal D-BUS messages */
279 gint dbus_req_handler(const gchar * interface, const gchar * method,
280 GArray * arguments, gpointer data,
284 appdata = (AppData *) data;
286 if (!strcmp(method, "top_application")) {
287 osso_rpc_free_val(retval);
290 osso_system_note_infoprint(appdata->osso_context, method, retval);
291 osso_rpc_free_val(retval);
296 static gboolean emergency_exit = FALSE;
298 #ifdef HAVE_STARTUP_NOTIFICATION
299 static void sn_error_trap_push(SnDisplay *display, Display *xdisplay)
301 gdk_error_trap_push();
304 static void sn_error_trap_pop(SnDisplay *display, Display *xdisplay)
306 gdk_error_trap_pop();
309 static void startup_notification_complete(gboolean with_window)
312 GtkWidget *hack = NULL;
315 /* this is needed to make the startup notification leave,
316 * if we have been launched from a menu.
317 * We have to display a window, so let it be very little */
318 hack = gtk_window_new(GTK_WINDOW_POPUP);
319 gtk_window_move(GTK_WINDOW(hack), 0, 0);
320 gtk_widget_set_size_request(hack, 1, 1);
321 gtk_widget_show(hack);
324 xdisplay = GDK_DISPLAY();
325 sn_display = sn_display_new(xdisplay,
328 sn_context = sn_launchee_context_new_from_environment(sn_display,
329 DefaultScreen(xdisplay));
331 if (sn_context != NULL) {
332 sn_launchee_context_complete(sn_context);
333 sn_launchee_context_unref(sn_context);
334 sn_display_unref(sn_display);
337 gtk_widget_destroy(hack);
340 #endif /* HAVE_STARTUP_NOTIFICATION */
342 static void claws_gtk_idle(void)
344 while(gtk_events_pending()) {
345 gtk_main_iteration();
350 static gboolean sc_starting = FALSE;
352 static gboolean defer_check_all(void *data)
354 gboolean autochk = GPOINTER_TO_INT(data);
356 inc_all_account_mail(static_mainwindow, autochk,
357 prefs_common.newmail_notify_manu);
361 main_window_set_menu_sensitive(static_mainwindow);
362 toolbar_main_set_sensitive(static_mainwindow);
367 static gboolean defer_check(void *data)
369 inc_mail(static_mainwindow, prefs_common.newmail_notify_manu);
373 main_window_set_menu_sensitive(static_mainwindow);
374 toolbar_main_set_sensitive(static_mainwindow);
379 static gboolean defer_jump(void *data)
381 if (cmd.receive_all) {
382 defer_check_all(GINT_TO_POINTER(FALSE));
383 } else if (prefs_common.chk_on_startup) {
384 defer_check_all(GINT_TO_POINTER(TRUE));
385 } else if (cmd.receive) {
388 mainwindow_jump_to(data, FALSE);
391 main_window_set_menu_sensitive(static_mainwindow);
392 toolbar_main_set_sensitive(static_mainwindow);
397 static void chk_update_val(GtkWidget *widget, gpointer data)
399 gboolean *val = (gboolean *)data;
400 *val = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
403 static gboolean migrate_old_config(const gchar *old_cfg_dir, const gchar *new_cfg_dir, const gchar *oldversion)
405 gchar *message = g_strdup_printf(_("Configuration for %s found.\n"
406 "Do you want to migrate this configuration?"), oldversion);
407 gchar *message2 = g_strdup_printf(_("\n\nYour Sylpheed filtering rules can be converted by a\n"
408 "script available at %s."), TOOLS_URI);
410 if (!strcmp(oldversion, "Sylpheed"))
411 message = g_strconcat(message, message2, NULL);
415 GtkWidget *window = NULL;
416 GtkWidget *keep_backup_chk;
418 gboolean backup = TRUE;
420 keep_backup_chk = gtk_check_button_new_with_label (_("Keep old configuration"));
421 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_backup_chk), TRUE);
422 CLAWS_SET_TIP(keep_backup_chk,
423 _("Keeping a backup will allow you to go back to an "
424 "older version, but may take a while if you have "
425 "cached IMAP or News data, and will take some extra "
426 "room on your disk."));
428 g_signal_connect(G_OBJECT(keep_backup_chk), "toggled",
429 G_CALLBACK(chk_update_val), &backup);
431 if (alertpanel_full(_("Migration of configuration"), message,
432 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL, FALSE,
433 keep_backup_chk, ALERT_QUESTION, G_ALERTDEFAULT) != G_ALERTALTERNATE) {
437 /* we can either do a fast migration requiring not any extra disk
438 * space, or a slow one that copies the old configuration and leaves
442 window = label_window_create(_("Copying configuration... This may take a while..."));
445 r = copy_dir(old_cfg_dir, new_cfg_dir);
446 label_window_destroy(window);
448 /* if copy failed, we'll remove the partially copied
451 alertpanel_error(_("Migration failed!"));
452 remove_dir_recursive(new_cfg_dir);
455 /* fast mode failed, but we don't want backup */
456 remove_dir_recursive(old_cfg_dir);
460 window = label_window_create(_("Migrating configuration..."));
463 r = g_rename(old_cfg_dir, new_cfg_dir);
464 label_window_destroy(window);
466 /* if g_rename failed, we'll try to copy */
468 FILE_OP_ERROR(new_cfg_dir, "g_rename failed, trying copy\n");
475 static int migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
478 gchar *plugin_path, *old_plugin_path, *new_plugin_path;
480 gboolean err = FALSE;
482 oldfp = g_fopen(old_rc, "r");
485 newfp = g_fopen(new_rc, "w");
491 plugin_path = g_strdup(get_plugin_dir());
492 new_plugin_path = g_strdup(plugin_path);
494 if (strstr(plugin_path, "/claws-mail/")) {
495 gchar *end = g_strdup(strstr(plugin_path, "/claws-mail/")+strlen("/claws-mail/"));
496 *(strstr(plugin_path, "/claws-mail/")) = '\0';
497 old_plugin_path = g_strconcat(plugin_path, "/sylpheed-claws/", end, NULL);
500 old_plugin_path = g_strdup(new_plugin_path);
502 debug_print("replacing %s with %s\n", old_plugin_path, new_plugin_path);
503 while (fgets(buf, sizeof(buf), oldfp)) {
504 if (strncmp(buf, old_plugin_path, strlen(old_plugin_path))) {
505 err |= (fputs(buf, newfp) == EOF);
507 debug_print("->replacing %s", buf);
508 debug_print(" with %s%s", new_plugin_path, buf+strlen(old_plugin_path));
509 err |= (fputs(new_plugin_path, newfp) == EOF);
510 err |= (fputs(buf+strlen(old_plugin_path), newfp) == EOF);
514 g_free(new_plugin_path);
515 g_free(old_plugin_path);
517 if (fclose(newfp) == EOF)
525 sc_client_set_value (MainWindow *mainwin,
536 prop.num_vals = num_vals;
540 if (mainwin->smc_conn)
541 SmcSetProperties ((SmcConn) mainwin->smc_conn, 1, proplist);
544 static void sc_die_callback (SmcConn smc_conn, SmPointer client_data)
549 static void sc_save_complete_callback(SmcConn smc_conn, SmPointer client_data)
553 static void sc_shutdown_cancelled_callback (SmcConn smc_conn, SmPointer client_data)
555 MainWindow *mainwin = (MainWindow *)client_data;
556 if (mainwin->smc_conn)
557 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
560 static void sc_save_yourself_callback (SmcConn smc_conn,
561 SmPointer client_data,
567 MainWindow *mainwin = (MainWindow *)client_data;
568 if (mainwin->smc_conn)
569 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
572 static IceIOErrorHandler sc_ice_installed_handler;
574 static void sc_ice_io_error_handler (IceConn connection)
576 if (sc_ice_installed_handler)
577 (*sc_ice_installed_handler) (connection);
579 static gboolean sc_process_ice_messages (GIOChannel *source,
580 GIOCondition condition,
583 IceConn connection = (IceConn) data;
584 IceProcessMessagesStatus status;
586 status = IceProcessMessages (connection, NULL, NULL);
588 if (status == IceProcessMessagesIOError) {
589 IcePointer context = IceGetConnectionContext (connection);
591 if (context && GTK_IS_OBJECT (context)) {
592 guint disconnect_id = g_signal_lookup ("disconnect", G_OBJECT_TYPE (context));
594 if (disconnect_id > 0)
595 g_signal_emit (context, disconnect_id, 0);
597 IceSetShutdownNegotiation (connection, False);
598 IceCloseConnection (connection);
605 static void new_ice_connection (IceConn connection, IcePointer client_data, Bool opening,
606 IcePointer *watch_data)
612 /* Make sure we don't pass on these file descriptors to any
614 fcntl(IceConnectionNumber(connection),F_SETFD,
615 fcntl(IceConnectionNumber(connection),F_GETFD,0) | FD_CLOEXEC);
617 channel = g_io_channel_unix_new (IceConnectionNumber (connection));
618 input_id = g_io_add_watch (channel,
619 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
620 sc_process_ice_messages,
622 g_io_channel_unref (channel);
624 *watch_data = (IcePointer) GUINT_TO_POINTER (input_id);
626 input_id = GPOINTER_TO_UINT ((gpointer) *watch_data);
627 g_source_remove (input_id);
631 static void sc_session_manager_connect(MainWindow *mainwin)
633 static gboolean connected = FALSE;
634 SmcCallbacks callbacks;
636 IceIOErrorHandler default_handler;
643 sc_ice_installed_handler = IceSetIOErrorHandler (NULL);
644 default_handler = IceSetIOErrorHandler (sc_ice_io_error_handler);
646 if (sc_ice_installed_handler == default_handler)
647 sc_ice_installed_handler = NULL;
649 IceAddConnectionWatch (new_ice_connection, NULL);
652 callbacks.save_yourself.callback = sc_save_yourself_callback;
653 callbacks.die.callback = sc_die_callback;
654 callbacks.save_complete.callback = sc_save_complete_callback;
655 callbacks.shutdown_cancelled.callback = sc_shutdown_cancelled_callback;
657 callbacks.save_yourself.client_data =
658 callbacks.die.client_data =
659 callbacks.save_complete.client_data =
660 callbacks.shutdown_cancelled.client_data = (SmPointer) mainwin;
661 if (g_getenv ("SESSION_MANAGER")) {
662 gchar error_string_ret[256] = "";
664 mainwin->smc_conn = (gpointer)
665 SmcOpenConnection (NULL, mainwin,
666 SmProtoMajor, SmProtoMinor,
667 SmcSaveYourselfProcMask | SmcDieProcMask |
668 SmcSaveCompleteProcMask |
669 SmcShutdownCancelledProcMask,
672 256, error_string_ret);
674 if (error_string_ret[0] || mainwin->smc_conn == NULL)
675 g_warning ("While connecting to session manager:\n%s.",
679 vals = g_new (SmPropValue, 1);
680 vals[0].length = strlen(argv0);
681 vals[0].value = argv0;
682 sc_client_set_value (mainwin, SmCloneCommand, SmLISTofARRAY8, 1, vals);
683 sc_client_set_value (mainwin, SmRestartCommand, SmLISTofARRAY8, 1, vals);
684 sc_client_set_value (mainwin, SmProgram, SmARRAY8, 1, vals);
686 vals[0].length = strlen(g_get_user_name()?g_get_user_name():"");
687 vals[0].value = g_strdup(g_get_user_name()?g_get_user_name():"");
688 sc_client_set_value (mainwin, SmUserID, SmARRAY8, 1, vals);
694 static gboolean sc_exiting = FALSE;
695 static gboolean show_at_startup = TRUE;
696 static gboolean claws_crashed_bool = FALSE;
698 gboolean claws_crashed(void) {
699 return claws_crashed_bool;
702 void main_set_show_at_startup(gboolean show)
704 show_at_startup = show;
708 static void main_vol_mount_cb(GnomeVFSVolumeMonitor *vfs, GnomeVFSVolume *vol, MainWindow *mainwin)
710 gchar *uri = gnome_vfs_volume_get_activation_uri (vol);
711 gchar *mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
714 if(!strcmp(mount_path, prefs_common.data_root)) {
715 gtk_widget_set_sensitive(mainwin->window, TRUE);
721 static void main_vol_unmount_cb(GnomeVFSVolumeMonitor *vfs, GnomeVFSVolume *vol, MainWindow *mainwin)
723 gchar *uri = gnome_vfs_volume_get_activation_uri (vol);
724 gchar *mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
727 if(!strcmp(mount_path, prefs_common.data_root)) {
728 gtk_widget_set_sensitive(mainwin->window, FALSE);
737 static FILE* win32_debug_fp=NULL;
738 static guint win32_log_handler_app_id;
739 static guint win32_log_handler_glib_id;
740 static guint win32_log_handler_gtk_id;
742 static void win32_print_stdout(const gchar *string)
744 if (win32_debug_fp) {
745 fprintf(win32_debug_fp, string);
746 fflush(win32_debug_fp);
750 static void win32_print_stderr(const gchar *string)
752 if (win32_debug_fp) {
753 fprintf(win32_debug_fp, string);
754 fflush(win32_debug_fp);
758 static void win32_log(const gchar *log_domain, GLogLevelFlags log_level, const gchar* message, gpointer user_data)
760 if (win32_debug_fp) {
763 switch(log_level & G_LOG_LEVEL_MASK)
765 case G_LOG_LEVEL_ERROR:
768 case G_LOG_LEVEL_CRITICAL:
771 case G_LOG_LEVEL_WARNING:
774 case G_LOG_LEVEL_MESSAGE:
777 case G_LOG_LEVEL_INFO:
780 case G_LOG_LEVEL_DEBUG:
787 fprintf(win32_debug_fp, "%s: %s: %s", log_domain, type, message);
789 fprintf(win32_debug_fp, "%s: %s", type, message);
790 fflush(win32_debug_fp);
794 static void win32_open_log(void)
796 gchar *logfile = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "claws-win32.log", NULL);
797 gchar *oldlogfile = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "claws-win32.log.bak", NULL);
799 if (is_file_exist(logfile)) {
800 if (rename_force(logfile, oldlogfile) < 0)
801 FILE_OP_ERROR(logfile, "rename");
803 win32_debug_fp = g_fopen(logfile, "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 #ifdef HAVE_DBUS_GLIB
928 static guint dbus_item_hook_id = -1;
929 static guint dbus_folder_hook_id = -1;
931 static void uninstall_dbus_status_handler(void)
934 g_object_unref(awn_proxy);
936 if (dbus_item_hook_id != -1)
937 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, dbus_item_hook_id);
938 if (dbus_folder_hook_id != -1)
939 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, dbus_folder_hook_id);
942 static void dbus_update(FolderItem *removed_item)
944 guint new, unread, unreadmarked, marked, total;
945 guint replied, forwarded, locked, ignored, watched;
947 GError *error = NULL;
949 folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total,
950 &replied, &forwarded, &locked, &ignored,
953 total -= removed_item->total_msgs;
954 new -= removed_item->new_msgs;
955 unread -= removed_item->unread_msgs;
959 buf = g_strdup_printf("%d", new);
960 dbus_g_proxy_call(awn_proxy, "SetInfoByName", &error,
961 G_TYPE_STRING, "claws-mail",
963 G_TYPE_INVALID, G_TYPE_INVALID);
967 dbus_g_proxy_call(awn_proxy, "UnsetInfoByName", &error, G_TYPE_STRING,
968 "claws-mail", G_TYPE_INVALID, G_TYPE_INVALID);
971 debug_print("%s\n", error->message);
976 static gboolean dbus_status_update_folder_hook(gpointer source, gpointer data)
978 FolderUpdateData *hookdata;
980 if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
981 dbus_update(hookdata->item);
988 static gboolean dbus_status_update_item_hook(gpointer source, gpointer data)
995 static void install_dbus_status_handler(void)
997 GError *tmp_error = NULL;
998 DBusGConnection *connection = dbus_g_bus_get(DBUS_BUS_SESSION, &tmp_error);
1001 /* If calling code doesn't do error checking, at least print some debug */
1002 debug_print("Failed to open connection to session bus: %s\n",
1003 tmp_error->message);
1004 g_error_free(tmp_error);
1007 awn_proxy = dbus_g_proxy_new_for_name(connection,
1008 "com.google.code.Awn",
1009 "/com/google/code/Awn",
1010 "com.google.code.Awn");
1011 dbus_item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, dbus_status_update_item_hook, NULL);
1012 if (dbus_item_hook_id == -1) {
1013 g_warning(_("Failed to register folder item update hook"));
1014 uninstall_dbus_status_handler();
1018 dbus_folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, dbus_status_update_folder_hook, NULL);
1019 if (dbus_folder_hook_id == -1) {
1020 g_warning(_("Failed to register folder update hook"));
1021 uninstall_dbus_status_handler();
1027 int main(int argc, char *argv[])
1030 osso_context_t *osso_context;
1031 osso_return_t result;
1033 #ifdef HAVE_DBUS_GLIB
1034 DBusGConnection *connection;
1037 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1038 DBusGProxy *nm_proxy;
1041 MainWindow *mainwin;
1042 FolderView *folderview;
1044 gboolean crash_file_present = FALSE;
1045 gint num_folder_class = 0;
1046 gboolean asked_for_migration = FALSE;
1047 gboolean start_done = TRUE;
1048 GtkUIManager *gui_manager = NULL;
1049 GSList *plug_list = NULL;
1050 gboolean never_ran = FALSE;
1051 START_TIMING("startup");
1058 if (!claws_init(&argc, &argv)) {
1065 main_dump_features_list(TRUE);
1067 prog_version = PROG_VERSION;
1068 argv0 = g_strdup(argv[0]);
1070 parse_cmd_opt(argc, argv);
1072 prefs_prepare_cache();
1077 gtk_init(&argc, &argv);
1078 crash_main(cmd.crash_params);
1084 crash_install_handlers();
1086 install_basic_sighandlers();
1087 #if (defined linux && defined SIGIO)
1088 install_memory_sighandler();
1092 /* check and create unix domain socket for remote operation */
1093 lock_socket = prohibit_duplicate_launch();
1094 if (lock_socket < 0) {
1095 #ifdef HAVE_STARTUP_NOTIFICATION
1096 if(gtk_init_check(&argc, &argv))
1097 startup_notification_complete(TRUE);
1102 if (cmd.status || cmd.status_full) {
1103 puts("0 Claws Mail not running.");
1104 lock_socket_remove();
1110 if (!g_thread_supported())
1111 g_thread_init(NULL);
1114 gtk_init(&argc, &argv);
1117 gtk_settings_set_string_property(gtk_settings_get_default(),
1123 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1124 went_offline_nm = FALSE;
1127 #ifdef HAVE_DBUS_GLIB
1129 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
1132 debug_print("Failed to open connection to system bus: %s\n", error->message);
1133 g_error_free(error);
1136 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1137 nm_proxy = dbus_g_proxy_new_for_name(connection,
1138 "org.freedesktop.NetworkManager",
1139 "/org/freedesktop/NetworkManager",
1140 "org.freedesktop.NetworkManager");
1142 dbus_g_proxy_add_signal(nm_proxy, "StateChange", G_TYPE_UINT, G_TYPE_INVALID);
1143 dbus_g_proxy_connect_signal(nm_proxy, "StateChange",
1144 G_CALLBACK(networkmanager_state_change_cb),
1148 install_dbus_status_handler();
1154 osso_context = osso_initialize(OSSO_SERVICE, "2.8.1", TRUE, NULL);
1155 if (osso_context == NULL) {
1158 hildon_program = HILDON_PROGRAM(hildon_program_get_instance());
1159 static_osso_context = osso_context;
1161 gtk_widget_set_default_colormap(gdk_rgb_get_colormap());
1163 gui_manager = gtkut_create_ui_manager();
1165 /* Create container for all the menus we will be adding */
1166 MENUITEM_ADDUI("/", "Menus", NULL, GTK_UI_MANAGER_MENUBAR);
1168 if (!g_thread_supported()) {
1169 g_error(_("g_thread is not supported by glib.\n"));
1172 /* check that we're not on a too recent/old gtk+ */
1173 #if GTK_CHECK_VERSION(2, 9, 0)
1174 if (gtk_check_version(2, 9, 0) != NULL) {
1175 alertpanel_error(_("Claws Mail has been compiled with "
1176 "a more recent GTK+ library than is "
1177 "currently available. This will cause "
1178 "crashes. You need to upgrade GTK+ or "
1179 "recompile Claws Mail."));
1186 if (gtk_check_version(2, 9, 0) == NULL) {
1187 alertpanel_error(_("Claws Mail has been compiled with "
1188 "an older GTK+ library than is "
1189 "currently available. This will cause "
1190 "crashes. You need to recompile "
1200 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1202 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1205 /* no config dir exists. See if we can migrate an old config. */
1206 if (!is_dir_exist(RC_DIR)) {
1207 prefs_destroy_cache();
1210 /* if one of the old dirs exist, we'll ask if the user
1211 * want to migrates, and r will be TRUE if he said yes
1212 * and migration succeeded, and FALSE otherwise.
1214 if (is_dir_exist(OLD_GTK2_RC_DIR)) {
1215 r = migrate_old_config(OLD_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 2.6.0 (or older)"));
1216 asked_for_migration = TRUE;
1217 } else if (is_dir_exist(OLDER_GTK2_RC_DIR)) {
1218 r = migrate_old_config(OLDER_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.9.15 (or older)"));
1219 asked_for_migration = TRUE;
1220 } else if (is_dir_exist(OLD_GTK1_RC_DIR)) {
1221 r = migrate_old_config(OLD_GTK1_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.0.5 (or older)"));
1222 asked_for_migration = TRUE;
1223 } else if (is_dir_exist(SYLPHEED_RC_DIR)) {
1224 r = migrate_old_config(SYLPHEED_RC_DIR, RC_DIR, "Sylpheed");
1225 asked_for_migration = TRUE;
1228 /* If migration failed or the user didn't want to do it,
1229 * we create a new one (and we'll hit wizard later).
1231 if (r == FALSE && !is_dir_exist(RC_DIR)) {
1233 if (copy_dir(SYSCONFDIR "/skel/.claws-mail", RC_DIR) < 0) {
1235 if (!is_dir_exist(RC_DIR) && make_dir(RC_DIR) < 0) {
1248 if (!is_file_exist(RC_DIR G_DIR_SEPARATOR_S COMMON_RC) &&
1249 is_file_exist(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC)) {
1250 /* post 2.6 name change */
1251 migrate_common_rc(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC,
1252 RC_DIR G_DIR_SEPARATOR_S COMMON_RC);
1256 plugin_load_all("Common");
1258 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
1259 gtk_rc_parse(userrc);
1262 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1263 gtk_accel_map_load (userrc);
1267 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_rc_dir(), 1, win32_close_log(););
1269 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
1272 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
1273 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
1274 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
1275 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
1276 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
1277 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR);
1279 crash_file_present = is_file_exist(get_crashfile_name());
1280 /* remove temporary files */
1281 remove_all_files(get_tmp_dir());
1282 remove_all_files(get_mime_tmp_dir());
1284 if (!cmd.crash && crash_file_present)
1285 claws_crashed_bool = TRUE;
1287 if (is_file_exist("claws.log")) {
1288 if (rename_force("claws.log", "claws.log.bak") < 0)
1289 FILE_OP_ERROR("claws.log", "rename");
1291 set_log_file(LOG_PROTOCOL, "claws.log");
1293 if (is_file_exist("filtering.log")) {
1294 if (rename_force("filtering.log", "filtering.log.bak") < 0)
1295 FILE_OP_ERROR("filtering.log", "rename");
1297 set_log_file(LOG_DEBUG_FILTERING, "filtering.log");
1300 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1302 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1305 folder_system_init();
1306 prefs_common_read_config();
1308 prefs_themes_init();
1310 prefs_ext_prog_init();
1311 prefs_wrapping_init();
1312 prefs_compose_writing_init();
1313 prefs_msg_colors_init();
1314 image_viewer_init();
1315 prefs_image_viewer_init();
1317 prefs_summaries_init();
1318 prefs_message_init();
1320 prefs_logging_init();
1321 prefs_receive_init();
1325 gtkaspell_checkers_init();
1326 prefs_spelling_init();
1329 sock_set_io_timeout(prefs_common.io_timeout_secs);
1330 prefs_actions_read_config();
1331 prefs_display_header_read_config();
1332 /* prefs_filtering_read_config(); */
1333 addressbook_read_file();
1335 gtkut_widget_init();
1336 stock_pixbuf_gdk(NULL, STOCK_PIXMAP_CLAWS_MAIL_ICON, &icon);
1337 gtk_window_set_default_icon(icon);
1339 folderview_initialize();
1345 mainwin = main_window_create();
1347 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1348 networkmanager_state_change_cb(nm_proxy,NULL,mainwin);
1353 appdata = g_new0(AppData, 1);
1354 appdata->program = hildon_program;
1355 appdata->window = HILDON_WINDOW(mainwin->window);
1356 appdata->osso_context = osso_context;
1357 result = osso_rpc_set_cb_f(appdata->osso_context,
1361 dbus_req_handler, appdata);
1362 if (result != OSSO_OK) {
1367 /* Add handler for Exit D-BUS messages */
1368 result = osso_application_set_exit_cb(appdata->osso_context,
1370 (gpointer) appdata);
1371 if (result != OSSO_OK) {
1375 osso_hw_set_event_cb( appdata->osso_context,
1376 NULL, hw_event_handler, (gpointer) appdata );
1378 manage_window_focus_in(mainwin->window, NULL, NULL);
1379 folderview = mainwin->folderview;
1381 gtk_cmclist_freeze(GTK_CMCLIST(mainwin->folderview->ctree));
1382 folder_item_update_freeze();
1384 /* register the callback of unix domain socket input */
1385 lock_socket_tag = claws_input_add(lock_socket,
1386 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
1387 lock_socket_input_cb,
1391 prefs_account_init();
1392 account_read_config_all();
1394 #ifdef HAVE_LIBETPAN
1395 imap_main_init(prefs_common.skip_ssl_cert_check);
1396 imap_main_set_timeout(prefs_common.io_timeout_secs);
1397 nntp_main_init(prefs_common.skip_ssl_cert_check);
1399 /* If we can't read a folder list or don't have accounts,
1400 * it means the configuration's not done. Either this is
1401 * a brand new install, either a failed/refused migration.
1402 * So we'll start the wizard.
1404 if (folder_read_list() < 0) {
1405 prefs_destroy_cache();
1407 /* if run_wizard returns FALSE it's because it's
1408 * been cancelled. We can't do much but exit.
1409 * however, if the user was asked for a migration,
1410 * we remove the newly created directory so that
1411 * he's asked again for migration on next launch.*/
1412 if (!run_wizard(mainwin, TRUE)) {
1413 if (asked_for_migration)
1414 remove_dir_recursive(RC_DIR);
1420 main_window_reflect_prefs_all_now();
1421 folder_write_list();
1425 if (!account_get_list()) {
1426 prefs_destroy_cache();
1427 if (!run_wizard(mainwin, FALSE)) {
1428 if (asked_for_migration)
1429 remove_dir_recursive(RC_DIR);
1435 if(!account_get_list()) {
1436 exit_claws(mainwin);
1446 toolbar_main_set_sensitive(mainwin);
1447 main_window_set_menu_sensitive(mainwin);
1449 /* if crashed, show window early so that the user
1450 * sees what's happening */
1451 if (claws_crashed())
1452 main_window_popup(mainwin);
1454 account_set_missing_folder();
1455 folder_set_missing_folders();
1456 folderview_set(folderview);
1458 prefs_matcher_read_config();
1460 /* make one all-folder processing before using claws */
1461 main_window_cursor_wait(mainwin);
1462 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
1464 /* if claws crashed, rebuild caches */
1465 if (claws_crashed()) {
1467 debug_print("Claws Mail crashed, checking for new messages in local folders\n");
1468 folder_item_update_thaw();
1469 folderview_check_new(NULL);
1470 folder_clean_cache_memory_force();
1471 folder_item_update_freeze();
1473 /* make the crash-indicator file */
1474 str_write_to_file("foo", get_crashfile_name());
1476 inc_autocheck_timer_init(mainwin);
1478 /* ignore SIGPIPE signal for preventing sudden death of program */
1480 signal(SIGPIPE, SIG_IGN);
1482 if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
1483 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1485 if (cmd.online_mode == ONLINE_MODE_ONLINE) {
1486 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1489 if (cmd.status_folders) {
1490 g_ptr_array_free(cmd.status_folders, TRUE);
1491 cmd.status_folders = NULL;
1493 if (cmd.status_full_folders) {
1494 g_ptr_array_free(cmd.status_full_folders, TRUE);
1495 cmd.status_full_folders = NULL;
1498 claws_register_idle_function(claws_gtk_idle);
1500 prefs_toolbar_init();
1502 num_folder_class = g_list_length(folder_get_list());
1504 plugin_load_all("GTK2");
1506 if (g_list_length(folder_get_list()) != num_folder_class) {
1507 debug_print("new folders loaded, reloading processing rules\n");
1508 prefs_matcher_read_config();
1511 if ((plug_list = plugin_get_unloaded_list()) != NULL) {
1514 gint num_plugins = 0;
1515 for (cur = plug_list; cur; cur = cur->next) {
1516 Plugin *plugin = (Plugin *)cur->data;
1517 gchar *tmp = g_strdup_printf("%s\n%s",
1519 plugin_get_name(plugin));
1524 main_window_cursor_normal(mainwin);
1525 alertpanel_warning(ngettext(
1526 "The following plugin failed to load. "
1527 "Check the Plugins configuration "
1528 "for more information:\n%s",
1529 "The following plugins failed to load. "
1530 "Check the Plugins configuration "
1531 "for more information:\n%s",
1534 main_window_cursor_wait(mainwin);
1536 g_slist_free(plug_list);
1540 prefs_common_write_config();
1541 plugin_load_standard_plugins ();
1543 /* if not crashed, show window now */
1544 if (!claws_crashed()) {
1545 /* apart if something told not to show */
1546 if (show_at_startup)
1547 main_window_popup(mainwin);
1550 if (!folder_have_mailbox()) {
1551 prefs_destroy_cache();
1552 main_window_cursor_normal(mainwin);
1553 if (folder_get_list() != NULL) {
1554 alertpanel_error(_("Claws Mail has detected a configured "
1555 "mailbox, but it is incomplete. It is "
1556 "possibly due to a failing IMAP account. Use "
1557 "\"Rebuild folder tree\" on the mailbox parent "
1558 "folder's context menu to try to fix it."));
1560 alertpanel_error(_("Claws Mail has detected a configured "
1561 "mailbox, but could not load it. It is "
1562 "probably provided by an out-of-date "
1563 "external plugin. Please reinstall the "
1564 "plugin and try again."));
1565 exit_claws(mainwin);
1573 static_mainwindow = mainwin;
1576 if (prefs_common.data_root != NULL && *prefs_common.data_root != '\0') {
1577 GnomeVFSVolume *vol = NULL;
1578 gchar *uri, *mount_path;
1580 volmon = gnome_vfs_get_volume_monitor();
1581 vol = gnome_vfs_volume_monitor_get_volume_for_path(volmon, prefs_common.data_root);
1583 uri = gnome_vfs_volume_get_activation_uri (vol);
1584 mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
1587 if (vol == NULL || !gnome_vfs_volume_is_mounted(vol)
1588 || strcmp(mount_path, prefs_common.data_root)) {
1589 alertpanel_error(_("Claws Mail can not start without its data volume (%s)."),
1590 prefs_common.data_root);
1592 gnome_vfs_volume_unref(vol);
1593 exit_claws(mainwin);
1597 gnome_vfs_volume_unref(vol);
1598 g_signal_connect(G_OBJECT(volmon),
1599 "volume-mounted", G_CALLBACK(main_vol_mount_cb), mainwin);
1600 g_signal_connect(G_OBJECT(volmon),
1601 "volume-unmounted", G_CALLBACK(main_vol_unmount_cb), mainwin);
1605 #ifdef HAVE_STARTUP_NOTIFICATION
1606 startup_notification_complete(FALSE);
1609 sc_session_manager_connect(mainwin);
1612 folder_item_update_thaw();
1613 gtk_cmclist_thaw(GTK_CMCLIST(mainwin->folderview->ctree));
1614 main_window_cursor_normal(mainwin);
1616 if (!cmd.target && prefs_common.goto_last_folder_on_startup &&
1617 folder_find_item_from_identifier(prefs_common.last_opened_folder) != NULL &&
1619 cmd.target = prefs_common.last_opened_folder;
1622 if (cmd.receive_all && !cmd.target) {
1624 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(FALSE));
1625 } else if (prefs_common.chk_on_startup && !cmd.target) {
1627 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(TRUE));
1628 } else if (cmd.receive && !cmd.target) {
1630 g_timeout_add(1000, defer_check, NULL);
1632 gtk_widget_grab_focus(folderview->ctree);
1635 open_compose_new(cmd.compose_mailto, cmd.attach_files);
1637 if (cmd.attach_files) {
1638 ptr_array_free_strings(cmd.attach_files);
1639 g_ptr_array_free(cmd.attach_files, TRUE);
1640 cmd.attach_files = NULL;
1642 if (cmd.subscribe) {
1643 folder_subscribe(cmd.subscribe_uri);
1652 g_timeout_add(500, defer_jump, (gpointer)cmd.target);
1655 prefs_destroy_cache();
1657 compose_reopen_exit_drafts();
1660 sc_starting = FALSE;
1661 main_window_set_menu_sensitive(mainwin);
1662 toolbar_main_set_sensitive(mainwin);
1669 osso_deinitialize(osso_context);
1671 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1673 g_object_unref(nm_proxy);
1675 #ifdef HAVE_DBUS_GLIB
1676 uninstall_dbus_status_handler();
1678 dbus_g_connection_unref(connection);
1684 exit_claws(mainwin);
1689 static void save_all_caches(FolderItem *item, gpointer data)
1696 folder_item_close(item);
1699 folder_item_free_cache(item, TRUE);
1702 static void exit_claws(MainWindow *mainwin)
1708 debug_print("shutting down\n");
1709 inc_autocheck_timer_remove();
1711 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1712 if (prefs_common.work_offline && went_offline_nm)
1713 prefs_common.work_offline = FALSE;
1716 /* save prefs for opened folder */
1717 if(mainwin->folderview->opened) {
1720 item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
1721 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
1722 prefs_common.last_opened_folder = folder_item_get_identifier(item);
1725 /* save all state before exiting */
1726 folder_func_to_all_folders(save_all_caches, NULL);
1727 folder_write_list();
1729 main_window_get_size(mainwin);
1730 main_window_get_position(mainwin);
1732 prefs_common_write_config();
1733 account_write_config_all();
1734 addressbook_export_to_file();
1736 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1737 gtk_accel_map_save(filename);
1740 /* delete temporary files */
1741 remove_all_files(get_tmp_dir());
1742 remove_all_files(get_mime_tmp_dir());
1744 close_log_file(LOG_PROTOCOL);
1745 close_log_file(LOG_DEBUG_FILTERING);
1747 #ifdef HAVE_LIBETPAN
1751 /* delete crashfile */
1753 claws_unlink(get_crashfile_name());
1755 lock_socket_remove();
1758 if (mainwin->smc_conn)
1759 SmcCloseConnection ((SmcConn)mainwin->smc_conn, 0, NULL);
1760 mainwin->smc_conn = NULL;
1763 main_window_destroy_all();
1765 plugin_unload_all("GTK2");
1767 prefs_toolbar_done();
1769 addressbook_destroy();
1771 prefs_themes_done();
1773 prefs_ext_prog_done();
1774 prefs_wrapping_done();
1775 prefs_compose_writing_done();
1776 prefs_msg_colors_done();
1777 prefs_image_viewer_done();
1778 image_viewer_done();
1780 prefs_summaries_done();
1781 prefs_message_done();
1783 prefs_receive_done();
1784 prefs_logging_done();
1788 prefs_spelling_done();
1789 gtkaspell_checkers_quit();
1791 plugin_unload_all("Common");
1795 static void parse_cmd_opt(int argc, char *argv[])
1799 for (i = 1; i < argc; i++) {
1800 if (!strncmp(argv[i], "--receive-all", 13)) {
1801 cmd.receive_all = TRUE;
1802 } else if (!strncmp(argv[i], "--receive", 9)) {
1804 } else if (!strncmp(argv[i], "--compose", 9)) {
1805 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1808 cmd.compose_mailto = NULL;
1809 if (p && *p != '\0' && *p != '-') {
1810 if (!strncmp(p, "mailto:", 7)) {
1811 cmd.compose_mailto = p + 7;
1813 cmd.compose_mailto = p;
1817 } else if (!strncmp(argv[i], "--subscribe", 11)) {
1818 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1819 if (p && *p != '\0' && *p != '-') {
1820 cmd.subscribe = TRUE;
1821 cmd.subscribe_uri = p;
1823 } else if (!strncmp(argv[i], "--attach", 8)) {
1824 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1827 while (p && *p != '\0' && *p != '-') {
1828 if (!cmd.attach_files) {
1829 cmd.attach_files = g_ptr_array_new();
1831 if ((file = g_filename_from_uri(p, NULL, NULL)) != NULL) {
1832 if (!is_file_exist(file)) {
1837 if (file == NULL && *p != G_DIR_SEPARATOR) {
1838 file = g_strconcat(claws_get_startup_dir(),
1841 } else if (file == NULL) {
1844 g_ptr_array_add(cmd.attach_files, file);
1846 p = (i+1 < argc)?argv[i+1]:NULL;
1848 } else if (!strncmp(argv[i], "--send", 6)) {
1850 } else if (!strncmp(argv[i], "--version-full", 14) ||
1851 !strncmp(argv[i], "-V", 2)) {
1852 g_print("Claws Mail version " VERSION "\n");
1853 main_dump_features_list(FALSE);
1855 } else if (!strncmp(argv[i], "--version", 9) ||
1856 !strncmp(argv[i], "-v", 2)) {
1857 g_print("Claws Mail version " VERSION "\n");
1859 } else if (!strncmp(argv[i], "--status-full", 13)) {
1860 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1862 cmd.status_full = TRUE;
1863 while (p && *p != '\0' && *p != '-') {
1864 if (!cmd.status_full_folders) {
1865 cmd.status_full_folders =
1868 g_ptr_array_add(cmd.status_full_folders,
1871 p = (i+1 < argc)?argv[i+1]:NULL;
1873 } else if (!strncmp(argv[i], "--status", 8)) {
1874 const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
1877 while (p && *p != '\0' && *p != '-') {
1878 if (!cmd.status_folders)
1879 cmd.status_folders = g_ptr_array_new();
1880 g_ptr_array_add(cmd.status_folders,
1883 p = (i+1 < argc)?argv[i+1]:NULL;
1885 } else if (!strncmp(argv[i], "--online", 8)) {
1886 cmd.online_mode = ONLINE_MODE_ONLINE;
1887 } else if (!strncmp(argv[i], "--offline", 9)) {
1888 cmd.online_mode = ONLINE_MODE_OFFLINE;
1889 } else if (!strncmp(argv[i], "--help", 6) ||
1890 !strncmp(argv[i], "-h", 2)) {
1891 gchar *base = g_path_get_basename(argv[0]);
1892 g_print(_("Usage: %s [OPTION]...\n"), base);
1894 g_print("%s\n", _(" --compose [address] open composition window"));
1895 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
1896 g_print("%s\n", _(" --attach file1 [file2]...\n"
1897 " open composition window with specified files\n"
1899 g_print("%s\n", _(" --receive receive new messages"));
1900 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
1901 g_print("%s\n", _(" --send send all queued messages"));
1902 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
1903 g_print("%s\n", _(" --status-full [folder]...\n"
1904 " show the status of each folder"));
1905 g_print("%s\n", _(" --select folder[/msg] jumps to the specified folder/message\n"
1906 " folder is a folder id like 'folder/sub_folder'"));
1907 g_print("%s\n", _(" --online switch to online mode"));
1908 g_print("%s\n", _(" --offline switch to offline mode"));
1909 g_print("%s\n", _(" --exit --quit -q exit Claws Mail"));
1910 g_print("%s\n", _(" --debug debug mode"));
1911 g_print("%s\n", _(" --help -h display this help and exit"));
1912 g_print("%s\n", _(" --version -v output version information and exit"));
1913 g_print("%s\n", _(" --version-full -V output version and built-in features information and exit"));
1914 g_print("%s\n", _(" --config-dir output configuration directory"));
1915 g_print("%s\n", _(" --alternate-config-dir [dir]\n"
1916 " use specified configuration directory"));
1920 } else if (!strncmp(argv[i], "--crash", 7)) {
1922 cmd.crash_params = g_strdup((i+1 < argc)?argv[i+1]:NULL);
1924 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
1925 g_print(RC_DIR "\n");
1927 } else if (!strncmp(argv[i], "--alternate-config-dir", sizeof "--alternate-config-dir" - 1) && i+1 < argc) {
1928 set_rc_dir(argv[i+1]);
1929 } else if (!strncmp(argv[i], "--exit", 6) ||
1930 !strncmp(argv[i], "--quit", 6) ||
1931 !strncmp(argv[i], "-q", 2)) {
1933 } else if (!strncmp(argv[i], "--select", 8) && i+1 < argc) {
1934 cmd.target = argv[i+1];
1935 } else if (i == 1 && argc == 2) {
1936 /* only one parameter. Do something intelligent about it */
1937 if ((strstr(argv[i], "@")||!strncmp(argv[i], "mailto:", 7)) && !strstr(argv[i], "://")) {
1938 const gchar *p = argv[i];
1941 cmd.compose_mailto = NULL;
1942 if (p && *p != '\0' && *p != '-') {
1943 if (!strncmp(p, "mailto:", 7)) {
1944 cmd.compose_mailto = p + 7;
1946 cmd.compose_mailto = p;
1949 } else if (!strncmp(argv[i], "file://", 7)) {
1950 cmd.target = argv[i];
1951 } else if (!strncmp(argv[i], "?attach=file://", strlen("?attach=file://"))) {
1953 cmd.compose_mailto = argv[i];
1954 } else if (strstr(argv[i], "://")) {
1955 const gchar *p = argv[i];
1956 if (p && *p != '\0' && *p != '-') {
1957 cmd.subscribe = TRUE;
1958 cmd.subscribe_uri = p;
1960 } else if (!strcmp(argv[i], "--sync")) {
1962 } else if (is_dir_exist(argv[i]) || is_file_exist(argv[i])) {
1963 cmd.target = argv[i];
1965 g_print(_("Unknown option\n"));
1971 if (cmd.attach_files && cmd.compose == FALSE) {
1973 cmd.compose_mailto = NULL;
1977 static void initial_processing(FolderItem *item, gpointer data)
1979 MainWindow *mainwin = (MainWindow *)data;
1982 cm_return_if_fail(item);
1983 buf = g_strdup_printf(_("Processing (%s)..."),
1986 : _("top level folder"));
1990 if (item->prefs->enable_processing) {
1991 item->processing_pending = TRUE;
1992 folder_item_apply_processing(item);
1993 item->processing_pending = FALSE;
1996 STATUSBAR_POP(mainwin);
1999 static gboolean draft_all_messages(void)
2001 GList *compose_list = NULL;
2003 compose_clear_exit_drafts();
2004 compose_list = compose_get_compose_list();
2005 while (compose_list != NULL) {
2006 Compose *c = (Compose*)compose_list->data;
2007 if (!compose_draft(c, COMPOSE_DRAFT_FOR_EXIT))
2009 compose_list = compose_get_compose_list();
2013 gboolean clean_quit(gpointer data)
2015 static gboolean firstrun = TRUE;
2022 /*!< Good idea to have the main window stored in a
2023 * static variable so we can check that variable
2024 * to see if we're really allowed to do things
2025 * that actually the spawner is supposed to
2026 * do (like: sending mail, composing messages).
2027 * Because, really, if we're the spawnee, and
2028 * we touch GTK stuff, we're hosed. See the
2031 /* FIXME: Use something else to signal that we're
2032 * in the original spawner, and not in a spawned
2034 if (!static_mainwindow) {
2038 draft_all_messages();
2039 emergency_exit = TRUE;
2040 exit_claws(static_mainwindow);
2046 void app_will_exit(GtkWidget *widget, gpointer data)
2048 MainWindow *mainwin = data;
2050 if (gtk_main_level() == 0) {
2051 debug_print("not even started\n");
2054 if (sc_exiting == TRUE) {
2055 debug_print("exit pending\n");
2059 debug_print("exiting\n");
2060 if (compose_get_compose_list()) {
2061 if (!draft_all_messages()) {
2062 main_window_popup(mainwin);
2068 if (prefs_common.warn_queued_on_exit && procmsg_have_queued_mails_fast()) {
2069 if (alertpanel(_("Queued messages"),
2070 _("Some unsent messages are queued. Exit now?"),
2071 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL)
2072 != G_ALERTALTERNATE) {
2073 main_window_popup(mainwin);
2077 manage_window_focus_in(mainwin->window, NULL, NULL);
2081 #ifdef HAVE_VALGRIND
2082 if (RUNNING_ON_VALGRIND) {
2083 summary_clear_list(mainwin->summaryview);
2086 if (folderview_get_selected_item(mainwin->folderview))
2087 folder_item_close(folderview_get_selected_item(mainwin->folderview));
2091 gboolean claws_is_exiting(void)
2096 gboolean claws_is_starting(void)
2102 * CLAWS: want this public so crash dialog can delete the
2105 gchar *claws_get_socket_name(void)
2107 static gchar *filename = NULL;
2108 const gchar *socket_dir = NULL;
2110 if (rc_dir_is_alt())
2111 socket_dir = get_rc_dir();
2113 socket_dir = g_get_tmp_dir();
2114 if (filename == NULL) {
2115 filename = g_strdup_printf("%s%cclaws-mail-%d",
2116 socket_dir, G_DIR_SEPARATOR,
2127 static gchar *get_crashfile_name(void)
2129 static gchar *filename = NULL;
2131 if (filename == NULL) {
2132 filename = g_strdup_printf("%s%cclaws-crashed",
2133 get_tmp_dir(), G_DIR_SEPARATOR);
2139 static gint prohibit_duplicate_launch(void)
2145 path = claws_get_socket_name();
2146 uxsock = fd_connect_unix(path);
2148 if (x_display == NULL)
2149 x_display = g_strdup(g_getenv("DISPLAY"));
2153 return fd_open_unix(path);
2158 hmutex = CreateMutexA(NULL, FALSE, "ClawsMail");
2160 debug_print("cannot create Mutex\n");
2163 if (GetLastError() != ERROR_ALREADY_EXISTS) {
2164 uxsock = fd_open_inet(50216);
2170 uxsock = fd_connect_inet(50216);
2174 /* remote command mode */
2176 debug_print("another Claws Mail instance is already running.\n");
2178 if (cmd.receive_all) {
2179 fd_write_all(uxsock, "receive_all\n", 12);
2180 } else if (cmd.receive) {
2181 fd_write_all(uxsock, "receive\n", 8);
2182 } else if (cmd.compose && cmd.attach_files) {
2183 gchar *str, *compose_str;
2186 if (cmd.compose_mailto) {
2187 compose_str = g_strdup_printf("compose_attach %s\n",
2188 cmd.compose_mailto);
2190 compose_str = g_strdup("compose_attach\n");
2193 fd_write_all(uxsock, compose_str, strlen(compose_str));
2194 g_free(compose_str);
2196 for (i = 0; i < cmd.attach_files->len; i++) {
2197 str = g_ptr_array_index(cmd.attach_files, i);
2198 fd_write_all(uxsock, str, strlen(str));
2199 fd_write_all(uxsock, "\n", 1);
2202 fd_write_all(uxsock, ".\n", 2);
2203 } else if (cmd.compose) {
2206 if (cmd.compose_mailto) {
2207 compose_str = g_strdup_printf
2208 ("compose %s\n", cmd.compose_mailto);
2210 compose_str = g_strdup("compose\n");
2213 fd_write_all(uxsock, compose_str, strlen(compose_str));
2214 g_free(compose_str);
2215 } else if (cmd.subscribe) {
2216 gchar *str = g_strdup_printf("subscribe %s\n", cmd.subscribe_uri);
2217 fd_write_all(uxsock, str, strlen(str));
2219 } else if (cmd.send) {
2220 fd_write_all(uxsock, "send\n", 5);
2221 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
2222 fd_write(uxsock, "online\n", 6);
2223 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
2224 fd_write(uxsock, "offline\n", 7);
2225 } else if (cmd.status || cmd.status_full) {
2226 gchar buf[BUFFSIZE];
2228 const gchar *command;
2232 command = cmd.status_full ? "status-full\n" : "status\n";
2233 folders = cmd.status_full ? cmd.status_full_folders :
2236 fd_write_all(uxsock, command, strlen(command));
2237 for (i = 0; folders && i < folders->len; ++i) {
2238 folder = g_ptr_array_index(folders, i);
2239 fd_write_all(uxsock, folder, strlen(folder));
2240 fd_write_all(uxsock, "\n", 1);
2242 fd_write_all(uxsock, ".\n", 2);
2244 fd_gets(uxsock, buf, sizeof(buf));
2245 if (!strncmp(buf, ".\n", 2)) break;
2248 } else if (cmd.exit) {
2249 fd_write_all(uxsock, "exit\n", 5);
2250 } else if (cmd.target) {
2251 gchar *str = g_strdup_printf("select %s\n", cmd.target);
2252 fd_write_all(uxsock, str, strlen(str));
2257 fd_write_all(uxsock, "get_display\n", 12);
2258 memset(buf, 0, sizeof(buf));
2259 fd_gets(uxsock, buf, sizeof(buf));
2260 if (strcmp2(buf, x_display)) {
2261 g_print("Claws Mail is already running on display %s.\n",
2265 uxsock = fd_connect_unix(path);
2266 fd_write_all(uxsock, "popup\n", 6);
2269 fd_write_all(uxsock, "popup\n", 6);
2277 static gint lock_socket_remove(void)
2282 if (lock_socket < 0) {
2286 if (lock_socket_tag > 0) {
2287 g_source_remove(lock_socket_tag);
2289 fd_close(lock_socket);
2292 filename = claws_get_socket_name();
2293 claws_unlink(filename);
2299 static GPtrArray *get_folder_item_list(gint sock)
2301 gchar buf[BUFFSIZE];
2303 GPtrArray *folders = NULL;
2306 fd_gets(sock, buf, sizeof(buf));
2307 if (!strncmp(buf, ".\n", 2)) {
2312 folders = g_ptr_array_new();
2314 item = folder_find_item_from_identifier(buf);
2316 g_ptr_array_add(folders, item);
2318 g_warning("no such folder: %s\n", buf);
2325 static void lock_socket_input_cb(gpointer data,
2327 GdkInputCondition condition)
2329 MainWindow *mainwin = (MainWindow *)data;
2331 gchar buf[BUFFSIZE];
2333 sock = fd_accept(source);
2334 fd_gets(sock, buf, sizeof(buf));
2336 if (!strncmp(buf, "popup", 5)) {
2337 main_window_popup(mainwin);
2338 } else if (!strncmp(buf, "get_display", 11)) {
2339 fd_write_all(sock, x_display, strlen(x_display));
2340 } else if (!strncmp(buf, "receive_all", 11)) {
2341 inc_all_account_mail(mainwin, FALSE,
2342 prefs_common.newmail_notify_manu);
2343 } else if (!strncmp(buf, "receive", 7)) {
2344 inc_mail(mainwin, prefs_common.newmail_notify_manu);
2345 } else if (!strncmp(buf, "compose_attach", 14)) {
2349 mailto = g_strdup(buf + strlen("compose_attach") + 1);
2350 files = g_ptr_array_new();
2351 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
2353 if (!strcmp2(buf, "."))
2355 g_ptr_array_add(files, g_strdup(buf));
2357 open_compose_new(mailto, files);
2358 ptr_array_free_strings(files);
2359 g_ptr_array_free(files, TRUE);
2361 } else if (!strncmp(buf, "compose", 7)) {
2362 open_compose_new(buf + strlen("compose") + 1, NULL);
2363 } else if (!strncmp(buf, "subscribe", 9)) {
2364 main_window_popup(mainwin);
2365 folder_subscribe(buf + strlen("subscribe") + 1);
2366 } else if (!strncmp(buf, "send", 4)) {
2368 } else if (!strncmp(buf, "online", 6)) {
2369 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
2370 } else if (!strncmp(buf, "offline", 7)) {
2371 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
2372 } else if (!strncmp(buf, "status-full", 11) ||
2373 !strncmp(buf, "status", 6)) {
2377 folders = get_folder_item_list(sock);
2378 status = folder_get_status
2379 (folders, !strncmp(buf, "status-full", 11));
2380 fd_write_all(sock, status, strlen(status));
2381 fd_write_all(sock, ".\n", 2);
2383 if (folders) g_ptr_array_free(folders, TRUE);
2384 } else if (!strncmp(buf, "select ", 7)) {
2385 const gchar *target = buf+7;
2386 mainwindow_jump_to(target, TRUE);
2387 } else if (!strncmp(buf, "exit", 4)) {
2388 app_will_exit(NULL, mainwin);
2394 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
2399 Xstrdup_a(addr, address, return);
2403 compose_new(NULL, addr, attach_files);
2406 static void send_queue(void)
2409 gchar *errstr = NULL;
2410 gboolean error = FALSE;
2411 for (list = folder_get_list(); list != NULL; list = list->next) {
2412 Folder *folder = list->data;
2414 if (folder->queue) {
2415 gint res = procmsg_send_queue
2416 (folder->queue, prefs_common.savemsg,
2420 folder_item_scan(folder->queue);
2428 alertpanel_error_log(_("Some errors occurred "
2429 "while sending queued messages:\n%s"), errstr);
2432 alertpanel_error_log("Some errors occurred "
2433 "while sending queued messages.");
2437 static void quit_signal_handler(int sig)
2439 debug_print("Quitting on signal %d\n", sig);
2441 g_timeout_add(0, clean_quit, NULL);
2444 static void install_basic_sighandlers()
2448 struct sigaction act;
2453 sigaddset(&mask, SIGTERM);
2456 sigaddset(&mask, SIGINT);
2459 sigaddset(&mask, SIGHUP);
2462 act.sa_handler = quit_signal_handler;
2467 sigaction(SIGTERM, &act, 0);
2470 sigaction(SIGINT, &act, 0);
2473 sigaction(SIGHUP, &act, 0);
2476 sigprocmask(SIG_UNBLOCK, &mask, 0);
2477 #endif /* !G_OS_WIN32 */
2480 #if (defined linux && defined SIGIO)
2481 static int mem_notify_fd = 0;
2483 static gboolean clean_caches(gpointer unused)
2485 if (static_mainwindow && static_mainwindow->lock_count > 0)
2487 debug_print("/dev/mem_notify: callback: Freeing some memory now!\n");
2488 folder_clean_cache_memory_force();
2492 static void memory_signal_handler(int sig)
2494 debug_print("/dev/mem_notify: Kernel says we should free up some memory!\n");
2495 g_timeout_add(10, clean_caches, NULL);
2498 static void install_memory_sighandler()
2501 struct sigaction act;
2504 mem_notify_fd = g_open("/dev/mem_notify", O_RDONLY|O_NONBLOCK, 0);
2505 if (mem_notify_fd == -1) {
2506 debug_print("/dev/mem_notify not available (%s)\n",
2511 fcntl(mem_notify_fd, F_SETOWN, getpid());
2512 flags = fcntl(mem_notify_fd, F_GETFL);
2513 fcntl(mem_notify_fd, flags|FASYNC);
2517 sigaddset(&mask, SIGIO);
2519 act.sa_handler = memory_signal_handler;
2523 sigaction(SIGIO, &act, 0);
2525 sigprocmask(SIG_UNBLOCK, &mask, 0);
2527 debug_print("/dev/mem_notify: installed handler\n");
2529 #endif /* linux && SIGIO */
2532 osso_context_t *get_osso_context(void)
2534 return static_osso_context;
2539 #ifdef HAVE_NETWORKMANAGER_SUPPORT
2540 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
2543 MainWindow *mainWin;
2546 if (static_mainwindow)
2547 mainWin = static_mainwindow;
2549 mainWin = (MainWindow*)data;
2551 if (!prefs_common.use_networkmanager)
2559 online = networkmanager_is_online(&error);
2561 if(online && went_offline_nm) {
2562 went_offline_nm = FALSE;
2563 main_window_toggle_work_offline(mainWin, FALSE, FALSE);
2564 debug_print("NetworkManager: Went online\n");
2565 log_message(LOG_PROTOCOL, _("NetworkManager: network is online.\n"));
2568 went_offline_nm = TRUE;
2569 main_window_toggle_work_offline(mainWin, TRUE, FALSE);
2570 debug_print("NetworkManager: Went offline\n");
2571 log_message(LOG_PROTOCOL, _("NetworkManager: network is offline.\n"));
2575 debug_print("Failed to get online information from NetworkManager: %s\n",
2577 g_error_free(error);
2581 debug_print("NetworkManager: Cannot change connection state because "
2582 "main window does not exist\n");
2585 /* Returns true (and sets error appropriately, if given) in case of error */
2586 gboolean networkmanager_is_online(GError **error)
2588 DBusGConnection *connection;
2590 GError *tmp_error = NULL;
2594 if (!prefs_common.use_networkmanager)
2599 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &tmp_error);
2602 /* If calling code doesn't do error checking, at least print some debug */
2603 if((error == NULL) || (*error == NULL))
2604 debug_print("Failed to open connection to system bus: %s\n",
2605 tmp_error->message);
2606 g_propagate_error(error, tmp_error);
2610 proxy = dbus_g_proxy_new_for_name(connection,
2611 "org.freedesktop.NetworkManager",
2612 "/org/freedesktop/NetworkManager",
2613 "org.freedesktop.NetworkManager");
2615 retVal = dbus_g_proxy_call(proxy,"state",&tmp_error, G_TYPE_INVALID,
2616 G_TYPE_UINT, &state, G_TYPE_INVALID);
2619 g_object_unref(proxy);
2621 dbus_g_connection_unref(connection);
2624 /* If calling code doesn't do error checking, at least print some debug */
2625 if((error == NULL) || (*error == NULL))
2626 debug_print("Failed to get state info from NetworkManager: %s\n",
2627 tmp_error->message);
2628 g_propagate_error(error, tmp_error);
2632 return (state == NM_STATE_CONNECTED || state == NM_STATE_UNKNOWN);