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>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtkrc.h>
38 #include <sys/types.h>
45 #include <X11/SM/SMlib.h>
50 GCRY_THREAD_OPTION_PTHREAD_IMPL;
54 #ifdef HAVE_STARTUP_NOTIFICATION
55 # define SN_API_NOT_YET_FROZEN
56 # include <libsn/sn-launchee.h>
57 # include <gdk/gdkx.h>
61 #include <dbus/dbus-glib.h>
63 #ifdef HAVE_NETWORKMANAGER_SUPPORT
64 #include <NetworkManager.h>
69 #include "mainwindow.h"
70 #include "folderview.h"
71 #include "image_viewer.h"
72 #include "summaryview.h"
73 #include "prefs_common.h"
74 #include "prefs_account.h"
75 #include "prefs_actions.h"
76 #include "prefs_ext_prog.h"
77 #include "prefs_fonts.h"
78 #include "prefs_image_viewer.h"
79 #include "prefs_message.h"
80 #include "prefs_receive.h"
81 #include "prefs_msg_colors.h"
82 #include "prefs_quote.h"
83 #include "prefs_spelling.h"
84 #include "prefs_summaries.h"
85 #include "prefs_themes.h"
86 #include "prefs_other.h"
87 #include "prefs_logging.h"
88 #include "prefs_send.h"
89 #include "prefs_wrapping.h"
90 #include "prefs_compose_writing.h"
91 #include "prefs_display_header.h"
96 #include "manage_window.h"
97 #include "alertpanel.h"
98 #include "statusbar.h"
99 #include "addressbook.h"
104 #include "gtkutils.h"
107 #include "prefs_toolbar.h"
110 #include "imap_gtk.h"
111 #include "news_gtk.h"
118 #include "imap-thread.h"
119 #include "nntp-thread.h"
121 #include "stock_pixmap.h"
123 #include "valgrind.h"
125 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
137 #include <hildon/hildon-banner.h>
138 #include <hildon/hildon-program.h>
140 #include <hildon-widgets/hildon-banner.h>
141 #include <hildon-widgets/hildon-program.h>
143 #include <gtk/gtkmain.h>
145 #include <libgnomevfs/gnome-vfs-volume.h>
146 #include <libgnomevfs/gnome-vfs-volume-monitor.h>
147 #include <libgnomevfs/gnome-vfs-utils.h>
149 #define OSSO_NAME "claws_mail"
150 #define OSSO_SERVICE "com.nokia."OSSO_NAME
151 #define OSSO_OBJECT "/com/nokia/"OSSO_NAME
152 #define OSSO_IFACE "com.nokia."OSSO_NAME
154 typedef struct _AppData AppData;
156 HildonProgram *program;
157 HildonWindow *window;
158 osso_context_t *osso_context;
161 static GnomeVFSVolumeMonitor *volmon;
164 #ifdef HAVE_NETWORKMANAGER_SUPPORT
165 /* Went offline due to NetworkManager */
166 static gboolean went_offline_nm;
169 #ifdef HAVE_DBUS_GLIB
170 static DBusGProxy *awn_proxy = NULL;
177 HildonProgram *hildon_program;
180 #ifdef HAVE_STARTUP_NOTIFICATION
181 static SnLauncheeContext *sn_context = NULL;
182 static SnDisplay *sn_display = NULL;
185 static gint lock_socket = -1;
186 static gint lock_socket_tag = 0;
187 static gchar *x_display = NULL;
190 ONLINE_MODE_DONT_CHANGE,
195 static struct RemoteCmd {
197 gboolean receive_all;
199 const gchar *compose_mailto;
200 GPtrArray *attach_files;
202 gboolean status_full;
203 GPtrArray *status_folders;
204 GPtrArray *status_full_folders;
211 const gchar *subscribe_uri;
215 static void parse_cmd_opt(int argc, char *argv[]);
217 static gint prohibit_duplicate_launch (void);
218 static gchar * get_crashfile_name (void);
219 static gint lock_socket_remove (void);
220 static void lock_socket_input_cb (gpointer data,
222 GdkInputCondition condition);
224 static void open_compose_new (const gchar *address,
225 GPtrArray *attach_files);
227 static void send_queue (void);
228 static void initial_processing (FolderItem *item, gpointer data);
229 static void quit_signal_handler (int sig);
230 static void install_basic_sighandlers (void);
231 #if (defined linux && defined SIGIO)
232 static void install_memory_sighandler (void);
234 static void exit_claws (MainWindow *mainwin);
236 #ifdef HAVE_NETWORKMANAGER_SUPPORT
237 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
241 #define MAKE_DIR_IF_NOT_EXIST(dir) \
243 if (!is_dir_exist(dir)) { \
244 if (is_file_exist(dir)) { \
246 (_("File '%s' already exists.\n" \
247 "Can't create folder."), \
251 if (make_dir(dir) < 0) \
256 static MainWindow *static_mainwindow;
259 static osso_context_t *static_osso_context;
261 void exit_event_handler(gboolean die_now, gpointer data)
264 appdata = (AppData *) data;
265 /* Do whatever application needs to do before exiting */
266 exit_claws(static_mainwindow);
267 hildon_banner_show_information(GTK_WIDGET(appdata->window), NULL,
272 /* Callback for hardware D-BUS events */
273 void hw_event_handler(osso_hw_state_t *state, gpointer data)
276 appdata = (AppData *) data;
278 if (state->shutdown_ind) {
279 exit_claws(static_mainwindow);
280 hildon_banner_show_information(GTK_WIDGET(appdata->window), NULL,
285 /* Callback for normal D-BUS messages */
286 gint dbus_req_handler(const gchar * interface, const gchar * method,
287 GArray * arguments, gpointer data,
291 appdata = (AppData *) data;
293 if (!strcmp(method, "top_application")) {
294 osso_rpc_free_val(retval);
297 osso_system_note_infoprint(appdata->osso_context, method, retval);
298 osso_rpc_free_val(retval);
303 static gboolean emergency_exit = FALSE;
305 #ifdef HAVE_STARTUP_NOTIFICATION
306 static void sn_error_trap_push(SnDisplay *display, Display *xdisplay)
308 gdk_error_trap_push();
311 static void sn_error_trap_pop(SnDisplay *display, Display *xdisplay)
313 gdk_error_trap_pop();
316 static void startup_notification_complete(gboolean with_window)
319 GtkWidget *hack = NULL;
322 /* this is needed to make the startup notification leave,
323 * if we have been launched from a menu.
324 * We have to display a window, so let it be very little */
325 hack = gtk_window_new(GTK_WINDOW_POPUP);
326 gtk_window_move(GTK_WINDOW(hack), 0, 0);
327 gtk_widget_set_size_request(hack, 1, 1);
328 gtk_widget_show(hack);
331 xdisplay = GDK_DISPLAY();
332 sn_display = sn_display_new(xdisplay,
335 sn_context = sn_launchee_context_new_from_environment(sn_display,
336 DefaultScreen(xdisplay));
338 if (sn_context != NULL) {
339 sn_launchee_context_complete(sn_context);
340 sn_launchee_context_unref(sn_context);
341 sn_display_unref(sn_display);
344 gtk_widget_destroy(hack);
347 #endif /* HAVE_STARTUP_NOTIFICATION */
349 static void claws_gtk_idle(void)
351 while(gtk_events_pending()) {
352 gtk_main_iteration();
357 static gboolean sc_starting = FALSE;
359 static gboolean defer_check_all(void *data)
361 gboolean autochk = GPOINTER_TO_INT(data);
363 inc_all_account_mail(static_mainwindow, autochk,
364 prefs_common.newmail_notify_manu);
368 main_window_set_menu_sensitive(static_mainwindow);
369 toolbar_main_set_sensitive(static_mainwindow);
374 static gboolean defer_check(void *data)
376 inc_mail(static_mainwindow, prefs_common.newmail_notify_manu);
380 main_window_set_menu_sensitive(static_mainwindow);
381 toolbar_main_set_sensitive(static_mainwindow);
386 static gboolean defer_jump(void *data)
388 if (cmd.receive_all) {
389 defer_check_all(GINT_TO_POINTER(FALSE));
390 } else if (prefs_common.chk_on_startup) {
391 defer_check_all(GINT_TO_POINTER(TRUE));
392 } else if (cmd.receive) {
395 mainwindow_jump_to(data, FALSE);
398 main_window_set_menu_sensitive(static_mainwindow);
399 toolbar_main_set_sensitive(static_mainwindow);
404 static void chk_update_val(GtkWidget *widget, gpointer data)
406 gboolean *val = (gboolean *)data;
407 *val = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
410 static gboolean migrate_old_config(const gchar *old_cfg_dir, const gchar *new_cfg_dir, const gchar *oldversion)
412 gchar *message = g_strdup_printf(_("Configuration for %s found.\n"
413 "Do you want to migrate this configuration?"), oldversion);
414 gchar *message2 = g_strdup_printf(_("\n\nYour Sylpheed filtering rules can be converted by a\n"
415 "script available at %s."), TOOLS_URI);
417 if (!strcmp(oldversion, "Sylpheed"))
418 message = g_strconcat(message, message2, NULL);
422 GtkWidget *window = NULL;
423 GtkWidget *keep_backup_chk;
425 gboolean backup = TRUE;
427 keep_backup_chk = gtk_check_button_new_with_label (_("Keep old configuration"));
428 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_backup_chk), TRUE);
429 CLAWS_SET_TIP(keep_backup_chk,
430 _("Keeping a backup will allow you to go back to an "
431 "older version, but may take a while if you have "
432 "cached IMAP or News data, and will take some extra "
433 "room on your disk."));
435 g_signal_connect(G_OBJECT(keep_backup_chk), "toggled",
436 G_CALLBACK(chk_update_val), &backup);
438 if (alertpanel_full(_("Migration of configuration"), message,
439 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL, FALSE,
440 keep_backup_chk, ALERT_QUESTION, G_ALERTDEFAULT) != G_ALERTALTERNATE) {
444 /* we can either do a fast migration requiring not any extra disk
445 * space, or a slow one that copies the old configuration and leaves
449 window = label_window_create(_("Copying configuration... This may take a while..."));
452 r = copy_dir(old_cfg_dir, new_cfg_dir);
453 label_window_destroy(window);
455 /* if copy failed, we'll remove the partially copied
458 alertpanel_error(_("Migration failed!"));
459 remove_dir_recursive(new_cfg_dir);
462 /* fast mode failed, but we don't want backup */
463 remove_dir_recursive(old_cfg_dir);
467 window = label_window_create(_("Migrating configuration..."));
470 r = g_rename(old_cfg_dir, new_cfg_dir);
471 label_window_destroy(window);
473 /* if g_rename failed, we'll try to copy */
475 FILE_OP_ERROR(new_cfg_dir, "g_rename failed, trying copy\n");
482 static int migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
485 gchar *plugin_path, *old_plugin_path, *new_plugin_path;
487 gboolean err = FALSE;
489 oldfp = g_fopen(old_rc, "r");
492 newfp = g_fopen(new_rc, "w");
498 plugin_path = g_strdup(get_plugin_dir());
499 new_plugin_path = g_strdup(plugin_path);
501 if (strstr(plugin_path, "/claws-mail/")) {
502 gchar *end = g_strdup(strstr(plugin_path, "/claws-mail/")+strlen("/claws-mail/"));
503 *(strstr(plugin_path, "/claws-mail/")) = '\0';
504 old_plugin_path = g_strconcat(plugin_path, "/sylpheed-claws/", end, NULL);
507 old_plugin_path = g_strdup(new_plugin_path);
509 debug_print("replacing %s with %s\n", old_plugin_path, new_plugin_path);
510 while (fgets(buf, sizeof(buf), oldfp)) {
511 if (strncmp(buf, old_plugin_path, strlen(old_plugin_path))) {
512 err |= (fputs(buf, newfp) == EOF);
514 debug_print("->replacing %s", buf);
515 debug_print(" with %s%s", new_plugin_path, buf+strlen(old_plugin_path));
516 err |= (fputs(new_plugin_path, newfp) == EOF);
517 err |= (fputs(buf+strlen(old_plugin_path), newfp) == EOF);
521 g_free(new_plugin_path);
522 g_free(old_plugin_path);
524 if (fclose(newfp) == EOF)
532 sc_client_set_value (MainWindow *mainwin,
543 prop.num_vals = num_vals;
547 if (mainwin->smc_conn)
548 SmcSetProperties ((SmcConn) mainwin->smc_conn, 1, proplist);
551 static void sc_die_callback (SmcConn smc_conn, SmPointer client_data)
556 static void sc_save_complete_callback(SmcConn smc_conn, SmPointer client_data)
560 static void sc_shutdown_cancelled_callback (SmcConn smc_conn, SmPointer client_data)
562 MainWindow *mainwin = (MainWindow *)client_data;
563 if (mainwin->smc_conn)
564 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
567 static void sc_save_yourself_callback (SmcConn smc_conn,
568 SmPointer client_data,
574 MainWindow *mainwin = (MainWindow *)client_data;
575 if (mainwin->smc_conn)
576 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
579 static IceIOErrorHandler sc_ice_installed_handler;
581 static void sc_ice_io_error_handler (IceConn connection)
583 if (sc_ice_installed_handler)
584 (*sc_ice_installed_handler) (connection);
586 static gboolean sc_process_ice_messages (GIOChannel *source,
587 GIOCondition condition,
590 IceConn connection = (IceConn) data;
591 IceProcessMessagesStatus status;
593 status = IceProcessMessages (connection, NULL, NULL);
595 if (status == IceProcessMessagesIOError) {
596 IcePointer context = IceGetConnectionContext (connection);
598 if (context && GTK_IS_OBJECT (context)) {
599 guint disconnect_id = g_signal_lookup ("disconnect", G_OBJECT_TYPE (context));
601 if (disconnect_id > 0)
602 g_signal_emit (context, disconnect_id, 0);
604 IceSetShutdownNegotiation (connection, False);
605 IceCloseConnection (connection);
612 static void new_ice_connection (IceConn connection, IcePointer client_data, Bool opening,
613 IcePointer *watch_data)
619 /* Make sure we don't pass on these file descriptors to any
621 fcntl(IceConnectionNumber(connection),F_SETFD,
622 fcntl(IceConnectionNumber(connection),F_GETFD,0) | FD_CLOEXEC);
624 channel = g_io_channel_unix_new (IceConnectionNumber (connection));
625 input_id = g_io_add_watch (channel,
626 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
627 sc_process_ice_messages,
629 g_io_channel_unref (channel);
631 *watch_data = (IcePointer) GUINT_TO_POINTER (input_id);
633 input_id = GPOINTER_TO_UINT ((gpointer) *watch_data);
634 g_source_remove (input_id);
638 static void sc_session_manager_connect(MainWindow *mainwin)
640 static gboolean connected = FALSE;
641 SmcCallbacks callbacks;
643 IceIOErrorHandler default_handler;
650 sc_ice_installed_handler = IceSetIOErrorHandler (NULL);
651 default_handler = IceSetIOErrorHandler (sc_ice_io_error_handler);
653 if (sc_ice_installed_handler == default_handler)
654 sc_ice_installed_handler = NULL;
656 IceAddConnectionWatch (new_ice_connection, NULL);
659 callbacks.save_yourself.callback = sc_save_yourself_callback;
660 callbacks.die.callback = sc_die_callback;
661 callbacks.save_complete.callback = sc_save_complete_callback;
662 callbacks.shutdown_cancelled.callback = sc_shutdown_cancelled_callback;
664 callbacks.save_yourself.client_data =
665 callbacks.die.client_data =
666 callbacks.save_complete.client_data =
667 callbacks.shutdown_cancelled.client_data = (SmPointer) mainwin;
668 if (g_getenv ("SESSION_MANAGER")) {
669 gchar error_string_ret[256] = "";
671 mainwin->smc_conn = (gpointer)
672 SmcOpenConnection (NULL, mainwin,
673 SmProtoMajor, SmProtoMinor,
674 SmcSaveYourselfProcMask | SmcDieProcMask |
675 SmcSaveCompleteProcMask |
676 SmcShutdownCancelledProcMask,
679 256, error_string_ret);
681 if (error_string_ret[0] || mainwin->smc_conn == NULL)
682 g_warning ("While connecting to session manager:\n%s.",
686 vals = g_new (SmPropValue, 1);
687 vals[0].length = strlen(argv0);
688 vals[0].value = argv0;
689 sc_client_set_value (mainwin, SmCloneCommand, SmLISTofARRAY8, 1, vals);
690 sc_client_set_value (mainwin, SmRestartCommand, SmLISTofARRAY8, 1, vals);
691 sc_client_set_value (mainwin, SmProgram, SmARRAY8, 1, vals);
693 vals[0].length = strlen(g_get_user_name()?g_get_user_name():"");
694 vals[0].value = g_strdup(g_get_user_name()?g_get_user_name():"");
695 sc_client_set_value (mainwin, SmUserID, SmARRAY8, 1, vals);
701 static gboolean sc_exiting = FALSE;
702 static gboolean show_at_startup = TRUE;
703 static gboolean claws_crashed_bool = FALSE;
705 gboolean claws_crashed(void) {
706 return claws_crashed_bool;
709 void main_set_show_at_startup(gboolean show)
711 show_at_startup = show;
715 static void main_vol_mount_cb(GnomeVFSVolumeMonitor *vfs, GnomeVFSVolume *vol, MainWindow *mainwin)
717 gchar *uri = gnome_vfs_volume_get_activation_uri (vol);
718 gchar *mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
721 if(!strcmp(mount_path, prefs_common.data_root)) {
722 gtk_widget_set_sensitive(mainwin->window, TRUE);
728 static void main_vol_unmount_cb(GnomeVFSVolumeMonitor *vfs, GnomeVFSVolume *vol, MainWindow *mainwin)
730 gchar *uri = gnome_vfs_volume_get_activation_uri (vol);
731 gchar *mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
734 if(!strcmp(mount_path, prefs_common.data_root)) {
735 gtk_widget_set_sensitive(mainwin->window, FALSE);
744 static FILE* win32_debug_fp=NULL;
745 static guint win32_log_handler_app_id;
746 static guint win32_log_handler_glib_id;
747 static guint win32_log_handler_gtk_id;
749 static void win32_print_stdout(const gchar *string)
751 if (win32_debug_fp) {
752 fprintf(win32_debug_fp, string);
753 fflush(win32_debug_fp);
757 static void win32_print_stderr(const gchar *string)
759 if (win32_debug_fp) {
760 fprintf(win32_debug_fp, string);
761 fflush(win32_debug_fp);
765 static void win32_log(const gchar *log_domain, GLogLevelFlags log_level, const gchar* message, gpointer user_data)
767 if (win32_debug_fp) {
770 switch(log_level & G_LOG_LEVEL_MASK)
772 case G_LOG_LEVEL_ERROR:
775 case G_LOG_LEVEL_CRITICAL:
778 case G_LOG_LEVEL_WARNING:
781 case G_LOG_LEVEL_MESSAGE:
784 case G_LOG_LEVEL_INFO:
787 case G_LOG_LEVEL_DEBUG:
794 fprintf(win32_debug_fp, "%s: %s: %s", log_domain, type, message);
796 fprintf(win32_debug_fp, "%s: %s", type, message);
797 fflush(win32_debug_fp);
801 static void win32_open_log(void)
803 if (is_file_exist("claws-win32.log")) {
804 if (rename_force("claws-win32.log", "claws-win32.log.bak") < 0)
805 FILE_OP_ERROR("claws-win32.log", "rename");
807 win32_debug_fp = fopen("claws-win32.log", "w");
810 g_set_print_handler(win32_print_stdout);
811 g_set_printerr_handler(win32_print_stdout);
812 win32_log_handler_app_id = g_log_set_handler(NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
813 | G_LOG_FLAG_RECURSION, win32_log, NULL);
814 win32_log_handler_glib_id = g_log_set_handler("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
815 | G_LOG_FLAG_RECURSION, win32_log, NULL);
816 win32_log_handler_gtk_id = g_log_set_handler("Gtk", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
817 | G_LOG_FLAG_RECURSION, win32_log, NULL);
821 static void win32_close_log(void)
825 g_log_remove_handler("", win32_log_handler_app_id);
826 g_log_remove_handler("GLib", win32_log_handler_glib_id);
827 g_log_remove_handler("Gtk", win32_log_handler_gtk_id);
828 fclose(win32_debug_fp);
834 static void main_dump_features_list(gboolean show_debug_only)
835 /* display compiled-in features list */
837 if (show_debug_only && !debug_get_mode())
841 debug_print("GTK+ %d.%d.%d / GLib %d.%d.%d\n",
842 gtk_major_version, gtk_minor_version, gtk_micro_version,
843 glib_major_version, glib_minor_version, glib_micro_version);
845 g_print("GTK+ %d.%d.%d / GLib %d.%d.%d\n",
846 gtk_major_version, gtk_minor_version, gtk_micro_version,
847 glib_major_version, glib_minor_version, glib_micro_version);
849 debug_print("Compiled-in features:\n");
851 g_print("Compiled-in features:\n");
854 debug_print(" compface\n");
856 g_print(" compface\n");
860 debug_print(" aspell\n");
862 g_print(" aspell\n");
866 debug_print(" gnutls\n");
868 g_print(" gnutls\n");
872 debug_print(" ipv6\n");
878 debug_print(" iconv\n");
884 debug_print(" jpilot\n");
886 g_print(" jpilot\n");
890 debug_print(" ldap\n");
896 debug_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
898 g_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR, LIBETPAN_VERSION_MINOR);
902 debug_print(" gnomeprint\n");
904 g_print(" gnomeprint\n");
908 debug_print(" libsm\n");
912 #if HAVE_NETWORKMANAGER_SUPPORT
914 debug_print(" NetworkManager\n");
916 g_print(" NetworkManager\n");
920 debug_print(" openssl\n");
922 g_print(" openssl\n");
926 #ifdef HAVE_DBUS_GLIB
927 static guint dbus_item_hook_id = -1;
928 static guint dbus_folder_hook_id = -1;
930 static void uninstall_dbus_status_handler(void)
933 g_object_unref(awn_proxy);
935 if (dbus_item_hook_id != -1)
936 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, dbus_item_hook_id);
937 if (dbus_folder_hook_id != -1)
938 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, dbus_folder_hook_id);
941 static void dbus_update(FolderItem *removed_item)
943 guint new, unread, unreadmarked, marked, total;
944 guint replied, forwarded, locked, ignored, watched;
946 GError *error = NULL;
948 folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total,
949 &replied, &forwarded, &locked, &ignored,
952 total -= removed_item->total_msgs;
953 new -= removed_item->new_msgs;
954 unread -= removed_item->unread_msgs;
958 buf = g_strdup_printf("%d", new);
959 dbus_g_proxy_call(awn_proxy, "SetInfoByName", &error,
960 G_TYPE_STRING, "claws-mail",
962 G_TYPE_INVALID, G_TYPE_INVALID);
966 dbus_g_proxy_call(awn_proxy, "UnsetInfoByName", &error, G_TYPE_STRING,
967 "claws-mail", G_TYPE_INVALID, G_TYPE_INVALID);
970 debug_print(error->message);
975 static gboolean dbus_status_update_folder_hook(gpointer source, gpointer data)
977 FolderUpdateData *hookdata;
979 if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
980 dbus_update(hookdata->item);
987 static gboolean dbus_status_update_item_hook(gpointer source, gpointer data)
994 static void install_dbus_status_handler(void)
996 GError *tmp_error = NULL;
997 DBusGConnection *connection = dbus_g_bus_get(DBUS_BUS_SESSION, &tmp_error);
1000 /* If calling code doesn't do error checking, at least print some debug */
1001 debug_print("Failed to open connection to session bus: %s\n",
1002 tmp_error->message);
1003 g_error_free(tmp_error);
1006 awn_proxy = dbus_g_proxy_new_for_name(connection,
1007 "com.google.code.Awn",
1008 "/com/google/code/Awn",
1009 "com.google.code.Awn");
1010 dbus_item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, dbus_status_update_item_hook, NULL);
1011 if (dbus_item_hook_id == -1) {
1012 g_warning(_("Failed to register folder item update hook"));
1013 uninstall_dbus_status_handler();
1017 dbus_folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, dbus_status_update_folder_hook, NULL);
1018 if (dbus_folder_hook_id == -1) {
1019 g_warning(_("Failed to register folder update hook"));
1020 uninstall_dbus_status_handler();
1026 int main(int argc, char *argv[])
1029 osso_context_t *osso_context;
1030 osso_return_t result;
1032 #ifdef HAVE_DBUS_GLIB
1033 DBusGConnection *connection;
1036 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1037 DBusGProxy *nm_proxy;
1040 MainWindow *mainwin;
1041 FolderView *folderview;
1043 gboolean crash_file_present = FALSE;
1044 gint num_folder_class = 0;
1045 gboolean asked_for_migration = FALSE;
1046 gboolean start_done = TRUE;
1047 GtkUIManager *gui_manager = NULL;
1049 START_TIMING("startup");
1056 if (!claws_init(&argc, &argv)) {
1063 main_dump_features_list(TRUE);
1065 prog_version = PROG_VERSION;
1066 argv0 = g_strdup(argv[0]);
1068 parse_cmd_opt(argc, argv);
1070 prefs_prepare_cache();
1075 gtk_init(&argc, &argv);
1076 crash_main(cmd.crash_params);
1082 crash_install_handlers();
1084 install_basic_sighandlers();
1085 #if (defined linux && defined SIGIO)
1086 install_memory_sighandler();
1090 /* check and create unix domain socket for remote operation */
1092 lock_socket = prohibit_duplicate_launch();
1093 if (lock_socket < 0) {
1094 #ifdef HAVE_STARTUP_NOTIFICATION
1095 if(gtk_init_check(&argc, &argv))
1096 startup_notification_complete(TRUE);
1101 if (cmd.status || cmd.status_full) {
1102 puts("0 Claws Mail not running.");
1103 lock_socket_remove();
1110 if (!g_thread_supported())
1111 g_thread_init(NULL);
1114 gtk_init(&argc, &argv);
1116 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1117 went_offline_nm = FALSE;
1120 #ifdef HAVE_DBUS_GLIB
1122 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
1125 debug_print("Failed to open connection to system bus: %s\n", error->message);
1126 g_error_free(error);
1129 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1130 nm_proxy = dbus_g_proxy_new_for_name(connection,
1131 "org.freedesktop.NetworkManager",
1132 "/org/freedesktop/NetworkManager",
1133 "org.freedesktop.NetworkManager");
1135 dbus_g_proxy_add_signal(nm_proxy, "StateChange", G_TYPE_UINT, G_TYPE_INVALID);
1136 dbus_g_proxy_connect_signal(nm_proxy, "StateChange",
1137 G_CALLBACK(networkmanager_state_change_cb),
1141 install_dbus_status_handler();
1147 osso_context = osso_initialize(OSSO_SERVICE, "2.8.1", TRUE, NULL);
1148 if (osso_context == NULL) {
1151 hildon_program = HILDON_PROGRAM(hildon_program_get_instance());
1152 static_osso_context = osso_context;
1155 gtk_widget_set_default_colormap(gdk_rgb_get_colormap());
1157 gui_manager = gtkut_create_ui_manager();
1159 /* Create container for all the menus we will be adding */
1160 MENUITEM_ADDUI("/", "Menus", NULL, GTK_UI_MANAGER_MENUBAR);
1162 if (!g_thread_supported()) {
1163 g_error(_("g_thread is not supported by glib.\n"));
1166 /* check that we're not on a too recent/old gtk+ */
1167 #if GTK_CHECK_VERSION(2, 9, 0)
1168 if (gtk_check_version(2, 9, 0) != NULL) {
1169 alertpanel_error(_("Claws Mail has been compiled with "
1170 "a more recent GTK+ library than is "
1171 "currently available. This will cause "
1172 "crashes. You need to upgrade GTK+ or "
1173 "recompile Claws Mail."));
1180 if (gtk_check_version(2, 9, 0) == NULL) {
1181 alertpanel_error(_("Claws Mail has been compiled with "
1182 "an older GTK+ library than is "
1183 "currently available. This will cause "
1184 "crashes. You need to recompile "
1194 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1196 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1199 /* no config dir exists. See if we can migrate an old config. */
1200 if (!is_dir_exist(RC_DIR)) {
1201 prefs_destroy_cache();
1204 /* if one of the old dirs exist, we'll ask if the user
1205 * want to migrates, and r will be TRUE if he said yes
1206 * and migration succeeded, and FALSE otherwise.
1208 if (is_dir_exist(OLD_GTK2_RC_DIR)) {
1209 r = migrate_old_config(OLD_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 2.6.0 (or older)"));
1210 asked_for_migration = TRUE;
1211 } else if (is_dir_exist(OLDER_GTK2_RC_DIR)) {
1212 r = migrate_old_config(OLDER_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.9.15 (or older)"));
1213 asked_for_migration = TRUE;
1214 } else if (is_dir_exist(OLD_GTK1_RC_DIR)) {
1215 r = migrate_old_config(OLD_GTK1_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.0.5 (or older)"));
1216 asked_for_migration = TRUE;
1217 } else if (is_dir_exist(SYLPHEED_RC_DIR)) {
1218 r = migrate_old_config(SYLPHEED_RC_DIR, RC_DIR, "Sylpheed");
1219 asked_for_migration = TRUE;
1222 /* If migration failed or the user didn't want to do it,
1223 * we create a new one (and we'll hit wizard later).
1225 if (r == FALSE && !is_dir_exist(RC_DIR) && make_dir(RC_DIR) < 0) {
1234 if (!is_file_exist(RC_DIR G_DIR_SEPARATOR_S COMMON_RC) &&
1235 is_file_exist(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC)) {
1236 /* post 2.6 name change */
1237 migrate_common_rc(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC,
1238 RC_DIR G_DIR_SEPARATOR_S COMMON_RC);
1242 plugin_load_all("Common");
1244 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
1245 gtk_rc_parse(userrc);
1248 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1249 gtk_accel_map_load (userrc);
1253 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_rc_dir(), 1, win32_close_log(););
1255 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
1258 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
1259 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
1260 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
1261 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
1262 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
1263 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR);
1265 crash_file_present = is_file_exist(get_crashfile_name());
1266 /* remove temporary files */
1267 remove_all_files(get_tmp_dir());
1268 remove_all_files(get_mime_tmp_dir());
1270 if (!cmd.crash && crash_file_present)
1271 claws_crashed_bool = TRUE;
1273 if (is_file_exist("claws.log")) {
1274 if (rename_force("claws.log", "claws.log.bak") < 0)
1275 FILE_OP_ERROR("claws.log", "rename");
1277 set_log_file(LOG_PROTOCOL, "claws.log");
1279 if (is_file_exist("filtering.log")) {
1280 if (rename_force("filtering.log", "filtering.log.bak") < 0)
1281 FILE_OP_ERROR("filtering.log", "rename");
1283 set_log_file(LOG_DEBUG_FILTERING, "filtering.log");
1286 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1288 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1291 folder_system_init();
1292 prefs_common_read_config();
1294 prefs_themes_init();
1296 prefs_ext_prog_init();
1297 prefs_wrapping_init();
1298 prefs_compose_writing_init();
1299 prefs_msg_colors_init();
1300 image_viewer_init();
1301 prefs_image_viewer_init();
1303 prefs_summaries_init();
1304 prefs_message_init();
1306 prefs_logging_init();
1307 prefs_receive_init();
1311 gtkaspell_checkers_init();
1312 prefs_spelling_init();
1315 sock_set_io_timeout(prefs_common.io_timeout_secs);
1316 prefs_actions_read_config();
1317 prefs_display_header_read_config();
1318 /* prefs_filtering_read_config(); */
1319 addressbook_read_file();
1321 gtkut_widget_init();
1322 stock_pixbuf_gdk(NULL, STOCK_PIXMAP_CLAWS_MAIL_ICON, &icon);
1323 gtk_window_set_default_icon(icon);
1325 folderview_initialize();
1331 mainwin = main_window_create();
1333 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1334 networkmanager_state_change_cb(nm_proxy,NULL,mainwin);
1339 appdata = g_new0(AppData, 1);
1340 appdata->program = hildon_program;
1341 appdata->window = HILDON_WINDOW(mainwin->window);
1342 appdata->osso_context = osso_context;
1343 result = osso_rpc_set_cb_f(appdata->osso_context,
1347 dbus_req_handler, appdata);
1348 if (result != OSSO_OK) {
1353 /* Add handler for Exit D-BUS messages */
1354 result = osso_application_set_exit_cb(appdata->osso_context,
1356 (gpointer) appdata);
1357 if (result != OSSO_OK) {
1361 osso_hw_set_event_cb( appdata->osso_context,
1362 NULL, hw_event_handler, (gpointer) appdata );
1364 manage_window_focus_in(mainwin->window, NULL, NULL);
1365 folderview = mainwin->folderview;
1367 gtk_cmclist_freeze(GTK_CMCLIST(mainwin->folderview->ctree));
1368 folder_item_update_freeze();
1370 /* register the callback of unix domain socket input */
1372 lock_socket_tag = gdk_input_add(lock_socket,
1373 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
1374 lock_socket_input_cb,
1379 prefs_account_init();
1380 account_read_config_all();
1382 /* If we can't read a folder list or don't have accounts,
1383 * it means the configuration's not done. Either this is
1384 * a brand new install, either a failed/refused migration.
1385 * So we'll start the wizard.
1387 if (folder_read_list() < 0) {
1388 prefs_destroy_cache();
1390 /* if run_wizard returns FALSE it's because it's
1391 * been cancelled. We can't do much but exit.
1392 * however, if the user was asked for a migration,
1393 * we remove the newly created directory so that
1394 * he's asked again for migration on next launch.*/
1395 if (!run_wizard(mainwin, TRUE)) {
1396 if (asked_for_migration)
1397 remove_dir_recursive(RC_DIR);
1403 main_window_reflect_prefs_all_now();
1404 folder_write_list();
1407 if (!account_get_list()) {
1408 prefs_destroy_cache();
1409 if (!run_wizard(mainwin, FALSE)) {
1410 if (asked_for_migration)
1411 remove_dir_recursive(RC_DIR);
1417 if(!account_get_list()) {
1418 exit_claws(mainwin);
1427 toolbar_main_set_sensitive(mainwin);
1428 main_window_set_menu_sensitive(mainwin);
1430 /* if crashed, show window early so that the user
1431 * sees what's happening */
1432 if (claws_crashed())
1433 main_window_popup(mainwin);
1436 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
1439 #ifdef HAVE_LIBETPAN
1440 imap_main_init(prefs_common.skip_ssl_cert_check);
1441 imap_main_set_timeout(prefs_common.io_timeout_secs);
1442 nntp_main_init(prefs_common.skip_ssl_cert_check);
1444 account_set_missing_folder();
1445 folder_set_missing_folders();
1446 folderview_set(folderview);
1448 prefs_matcher_read_config();
1450 /* make one all-folder processing before using claws */
1451 main_window_cursor_wait(mainwin);
1452 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
1454 /* if claws crashed, rebuild caches */
1455 if (claws_crashed()) {
1457 debug_print("Claws Mail crashed, checking for new messages in local folders\n");
1458 folder_item_update_thaw();
1459 folderview_check_new(NULL);
1460 folder_clean_cache_memory_force();
1461 folder_item_update_freeze();
1463 /* make the crash-indicator file */
1464 str_write_to_file("foo", get_crashfile_name());
1466 inc_autocheck_timer_init(mainwin);
1468 /* ignore SIGPIPE signal for preventing sudden death of program */
1470 signal(SIGPIPE, SIG_IGN);
1472 if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
1473 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1475 if (cmd.online_mode == ONLINE_MODE_ONLINE) {
1476 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1479 if (cmd.status_folders) {
1480 g_ptr_array_free(cmd.status_folders, TRUE);
1481 cmd.status_folders = NULL;
1483 if (cmd.status_full_folders) {
1484 g_ptr_array_free(cmd.status_full_folders, TRUE);
1485 cmd.status_full_folders = NULL;
1488 claws_register_idle_function(claws_gtk_idle);
1490 prefs_toolbar_init();
1492 num_folder_class = g_list_length(folder_get_list());
1494 plugin_load_all("GTK2");
1496 if (g_list_length(folder_get_list()) != num_folder_class) {
1497 debug_print("new folders loaded, reloading processing rules\n");
1498 prefs_matcher_read_config();
1501 if (plugin_get_unloaded_list() != NULL) {
1502 main_window_cursor_normal(mainwin);
1503 alertpanel_warning(_("Some plugin(s) failed to load. "
1504 "Check the Plugins configuration "
1505 "for more information."));
1506 main_window_cursor_wait(mainwin);
1509 plugin_load_standard_plugins ();
1511 /* if not crashed, show window now */
1512 if (!claws_crashed()) {
1513 /* apart if something told not to show */
1514 if (show_at_startup)
1515 main_window_popup(mainwin);
1518 if (!folder_have_mailbox()) {
1519 prefs_destroy_cache();
1520 main_window_cursor_normal(mainwin);
1521 if (folder_get_list() != NULL) {
1522 alertpanel_error(_("Claws Mail has detected a configured "
1523 "mailbox, but it is incomplete. It is "
1524 "possibly due to a failing IMAP account. Use "
1525 "\"Rebuild folder tree\" on the mailbox parent "
1526 "folder's context menu to try to fix it."));
1528 alertpanel_error(_("Claws Mail has detected a configured "
1529 "mailbox, but could not load it. It is "
1530 "probably provided by an out-of-date "
1531 "external plugin. Please reinstall the "
1532 "plugin and try again."));
1533 exit_claws(mainwin);
1541 static_mainwindow = mainwin;
1544 if (prefs_common.data_root != NULL && *prefs_common.data_root != '\0') {
1545 GnomeVFSVolume *vol = NULL;
1546 gchar *uri, *mount_path;
1548 volmon = gnome_vfs_get_volume_monitor();
1549 vol = gnome_vfs_volume_monitor_get_volume_for_path(volmon, prefs_common.data_root);
1551 uri = gnome_vfs_volume_get_activation_uri (vol);
1552 mount_path = uri?gnome_vfs_get_local_path_from_uri (uri):NULL;
1555 if (vol == NULL || !gnome_vfs_volume_is_mounted(vol)
1556 || strcmp(mount_path, prefs_common.data_root)) {
1557 alertpanel_error(_("Claws Mail can not start without its data volume (%s)."),
1558 prefs_common.data_root);
1560 gnome_vfs_volume_unref(vol);
1561 exit_claws(mainwin);
1565 gnome_vfs_volume_unref(vol);
1566 g_signal_connect(G_OBJECT(volmon),
1567 "volume-mounted", G_CALLBACK(main_vol_mount_cb), mainwin);
1568 g_signal_connect(G_OBJECT(volmon),
1569 "volume-unmounted", G_CALLBACK(main_vol_unmount_cb), mainwin);
1573 #ifdef HAVE_STARTUP_NOTIFICATION
1574 startup_notification_complete(FALSE);
1577 sc_session_manager_connect(mainwin);
1580 folder_item_update_thaw();
1581 gtk_cmclist_thaw(GTK_CMCLIST(mainwin->folderview->ctree));
1582 main_window_cursor_normal(mainwin);
1584 if (!cmd.target && prefs_common.goto_last_folder_on_startup &&
1585 folder_find_item_from_identifier(prefs_common.last_opened_folder) != NULL &&
1587 cmd.target = prefs_common.last_opened_folder;
1590 if (cmd.receive_all && !cmd.target) {
1592 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(FALSE));
1593 } else if (prefs_common.chk_on_startup && !cmd.target) {
1595 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(TRUE));
1596 } else if (cmd.receive && !cmd.target) {
1598 g_timeout_add(1000, defer_check, NULL);
1600 gtk_widget_grab_focus(folderview->ctree);
1603 open_compose_new(cmd.compose_mailto, cmd.attach_files);
1605 if (cmd.attach_files) {
1606 ptr_array_free_strings(cmd.attach_files);
1607 g_ptr_array_free(cmd.attach_files, TRUE);
1608 cmd.attach_files = NULL;
1610 if (cmd.subscribe) {
1611 folder_subscribe(cmd.subscribe_uri);
1620 g_timeout_add(500, defer_jump, (gpointer)cmd.target);
1623 prefs_destroy_cache();
1625 compose_reopen_exit_drafts();
1628 sc_starting = FALSE;
1629 main_window_set_menu_sensitive(mainwin);
1630 toolbar_main_set_sensitive(mainwin);
1637 osso_deinitialize(osso_context);
1639 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1641 g_object_unref(nm_proxy);
1643 #ifdef HAVE_DBUS_GLIB
1644 uninstall_dbus_status_handler();
1646 dbus_g_connection_unref(connection);
1652 exit_claws(mainwin);
1657 static void save_all_caches(FolderItem *item, gpointer data)
1664 folder_item_close(item);
1667 folder_item_free_cache(item, TRUE);
1670 static void exit_claws(MainWindow *mainwin)
1676 debug_print("shutting down\n");
1677 inc_autocheck_timer_remove();
1679 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1680 if (prefs_common.work_offline && went_offline_nm)
1681 prefs_common.work_offline = FALSE;
1684 /* save prefs for opened folder */
1685 if(mainwin->folderview->opened) {
1688 item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
1689 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
1690 prefs_common.last_opened_folder = folder_item_get_identifier(item);
1693 /* save all state before exiting */
1694 folder_func_to_all_folders(save_all_caches, NULL);
1695 folder_write_list();
1697 main_window_get_size(mainwin);
1698 main_window_get_position(mainwin);
1700 prefs_common_write_config();
1701 account_write_config_all();
1702 addressbook_export_to_file();
1704 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1705 gtk_accel_map_save(filename);
1708 /* delete temporary files */
1709 remove_all_files(get_tmp_dir());
1710 remove_all_files(get_mime_tmp_dir());
1712 close_log_file(LOG_PROTOCOL);
1713 close_log_file(LOG_DEBUG_FILTERING);
1715 #ifdef HAVE_LIBETPAN
1719 /* delete crashfile */
1721 claws_unlink(get_crashfile_name());
1723 lock_socket_remove();
1726 if (mainwin->smc_conn)
1727 SmcCloseConnection ((SmcConn)mainwin->smc_conn, 0, NULL);
1728 mainwin->smc_conn = NULL;
1731 main_window_destroy_all();
1733 plugin_unload_all("GTK2");
1735 prefs_toolbar_done();
1737 addressbook_destroy();
1739 prefs_themes_done();
1741 prefs_ext_prog_done();
1742 prefs_wrapping_done();
1743 prefs_compose_writing_done();
1744 prefs_msg_colors_done();
1745 prefs_image_viewer_done();
1746 image_viewer_done();
1748 prefs_summaries_done();
1749 prefs_message_done();
1751 prefs_receive_done();
1752 prefs_logging_done();
1756 prefs_spelling_done();
1757 gtkaspell_checkers_quit();
1759 plugin_unload_all("Common");
1763 static void parse_cmd_opt(int argc, char *argv[])
1767 for (i = 1; i < argc; i++) {
1768 if (!strncmp(argv[i], "--receive-all", 13)) {
1769 cmd.receive_all = TRUE;
1770 } else if (!strncmp(argv[i], "--receive", 9)) {
1772 } else if (!strncmp(argv[i], "--compose", 9)) {
1773 const gchar *p = argv[i + 1];
1776 cmd.compose_mailto = NULL;
1777 if (p && *p != '\0' && *p != '-') {
1778 if (!strncmp(p, "mailto:", 7)) {
1779 cmd.compose_mailto = p + 7;
1781 cmd.compose_mailto = p;
1785 } else if (!strncmp(argv[i], "--subscribe", 11)) {
1786 const gchar *p = argv[i + 1];
1787 if (p && *p != '\0' && *p != '-') {
1788 cmd.subscribe = TRUE;
1789 cmd.subscribe_uri = p;
1791 } else if (!strncmp(argv[i], "--attach", 8)) {
1792 const gchar *p = argv[i + 1];
1795 while (p && *p != '\0' && *p != '-') {
1796 if (!cmd.attach_files) {
1797 cmd.attach_files = g_ptr_array_new();
1799 if ((file = g_filename_from_uri(p, NULL, NULL)) != NULL) {
1800 if (!is_file_exist(file)) {
1805 if (file == NULL && *p != G_DIR_SEPARATOR) {
1806 file = g_strconcat(claws_get_startup_dir(),
1809 } else if (file == NULL) {
1812 g_ptr_array_add(cmd.attach_files, file);
1816 } else if (!strncmp(argv[i], "--send", 6)) {
1818 } else if (!strncmp(argv[i], "--version-full", 14) ||
1819 !strncmp(argv[i], "-V", 2)) {
1820 g_print("Claws Mail version " VERSION "\n");
1821 main_dump_features_list(FALSE);
1823 } else if (!strncmp(argv[i], "--version", 9) ||
1824 !strncmp(argv[i], "-v", 2)) {
1825 g_print("Claws Mail version " VERSION "\n");
1827 } else if (!strncmp(argv[i], "--status-full", 13)) {
1828 const gchar *p = argv[i + 1];
1830 cmd.status_full = TRUE;
1831 while (p && *p != '\0' && *p != '-') {
1832 if (!cmd.status_full_folders) {
1833 cmd.status_full_folders =
1836 g_ptr_array_add(cmd.status_full_folders,
1841 } else if (!strncmp(argv[i], "--status", 8)) {
1842 const gchar *p = argv[i + 1];
1845 while (p && *p != '\0' && *p != '-') {
1846 if (!cmd.status_folders)
1847 cmd.status_folders = g_ptr_array_new();
1848 g_ptr_array_add(cmd.status_folders,
1853 } else if (!strncmp(argv[i], "--online", 8)) {
1854 cmd.online_mode = ONLINE_MODE_ONLINE;
1855 } else if (!strncmp(argv[i], "--offline", 9)) {
1856 cmd.online_mode = ONLINE_MODE_OFFLINE;
1857 } else if (!strncmp(argv[i], "--help", 6) ||
1858 !strncmp(argv[i], "-h", 2)) {
1859 gchar *base = g_path_get_basename(argv[0]);
1860 g_print(_("Usage: %s [OPTION]...\n"), base);
1862 g_print("%s\n", _(" --compose [address] open composition window"));
1863 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
1864 g_print("%s\n", _(" --attach file1 [file2]...\n"
1865 " open composition window with specified files\n"
1867 g_print("%s\n", _(" --receive receive new messages"));
1868 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
1869 g_print("%s\n", _(" --send send all queued messages"));
1870 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
1871 g_print("%s\n", _(" --status-full [folder]...\n"
1872 " show the status of each folder"));
1873 g_print("%s\n", _(" --select folder[/msg] jumps to the specified folder/message\n"
1874 " folder is a folder id like 'folder/sub_folder'"));
1875 g_print("%s\n", _(" --online switch to online mode"));
1876 g_print("%s\n", _(" --offline switch to offline mode"));
1877 g_print("%s\n", _(" --exit --quit -q exit Claws Mail"));
1878 g_print("%s\n", _(" --debug debug mode"));
1879 g_print("%s\n", _(" --help -h display this help and exit"));
1880 g_print("%s\n", _(" --version -v output version information and exit"));
1881 g_print("%s\n", _(" --version-full -V output version and built-in features information and exit"));
1882 g_print("%s\n", _(" --config-dir output configuration directory"));
1883 g_print("%s\n", _(" --alternate-config-dir [dir]\n"
1884 " use specified configuration directory"));
1888 } else if (!strncmp(argv[i], "--crash", 7)) {
1890 cmd.crash_params = g_strdup(argv[i + 1]);
1892 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
1893 g_print(RC_DIR "\n");
1895 } else if (!strncmp(argv[i], "--alternate-config-dir", sizeof "--alternate-config-dir" - 1) && i+1 < argc) {
1896 set_rc_dir(argv[i+1]);
1897 } else if (!strncmp(argv[i], "--exit", 6) ||
1898 !strncmp(argv[i], "--quit", 6) ||
1899 !strncmp(argv[i], "-q", 2)) {
1901 } else if (!strncmp(argv[i], "--select", 8) && i+1 < argc) {
1902 cmd.target = argv[i+1];
1903 } else if (i == 1 && argc == 2) {
1904 /* only one parameter. Do something intelligent about it */
1905 if ((strstr(argv[i], "@")||!strncmp(argv[i], "mailto:", 7)) && !strstr(argv[i], "://")) {
1906 const gchar *p = argv[i];
1909 cmd.compose_mailto = NULL;
1910 if (p && *p != '\0' && *p != '-') {
1911 if (!strncmp(p, "mailto:", 7)) {
1912 cmd.compose_mailto = p + 7;
1914 cmd.compose_mailto = p;
1917 } else if (!strncmp(argv[i], "file://", 7)) {
1918 cmd.target = argv[i];
1919 } else if (!strncmp(argv[i], "?attach=file://", strlen("?attach=file://"))) {
1921 cmd.compose_mailto = argv[i];
1922 } else if (strstr(argv[i], "://")) {
1923 const gchar *p = argv[i];
1924 if (p && *p != '\0' && *p != '-') {
1925 cmd.subscribe = TRUE;
1926 cmd.subscribe_uri = p;
1928 } else if (!strcmp(argv[i], "--sync")) {
1930 } else if (is_dir_exist(argv[i]) || is_file_exist(argv[i])) {
1931 cmd.target = argv[i];
1933 g_print(_("Unknown option\n"));
1939 if (cmd.attach_files && cmd.compose == FALSE) {
1941 cmd.compose_mailto = NULL;
1945 static void initial_processing(FolderItem *item, gpointer data)
1947 MainWindow *mainwin = (MainWindow *)data;
1950 g_return_if_fail(item);
1951 buf = g_strdup_printf(_("Processing (%s)..."),
1954 : _("top level folder"));
1958 if (item->prefs->enable_processing) {
1959 item->processing_pending = TRUE;
1960 folder_item_apply_processing(item);
1961 item->processing_pending = FALSE;
1964 STATUSBAR_POP(mainwin);
1967 static gboolean draft_all_messages(void)
1969 GList *compose_list = NULL;
1971 compose_clear_exit_drafts();
1972 compose_list = compose_get_compose_list();
1973 while (compose_list != NULL) {
1974 Compose *c = (Compose*)compose_list->data;
1975 if (!compose_draft(c, COMPOSE_DRAFT_FOR_EXIT))
1977 compose_list = compose_get_compose_list();
1981 gboolean clean_quit(gpointer data)
1983 static gboolean firstrun = TRUE;
1990 /*!< Good idea to have the main window stored in a
1991 * static variable so we can check that variable
1992 * to see if we're really allowed to do things
1993 * that actually the spawner is supposed to
1994 * do (like: sending mail, composing messages).
1995 * Because, really, if we're the spawnee, and
1996 * we touch GTK stuff, we're hosed. See the
1999 /* FIXME: Use something else to signal that we're
2000 * in the original spawner, and not in a spawned
2002 if (!static_mainwindow) {
2006 draft_all_messages();
2007 emergency_exit = TRUE;
2008 exit_claws(static_mainwindow);
2014 void app_will_exit(GtkWidget *widget, gpointer data)
2016 MainWindow *mainwin = data;
2018 if (gtk_main_level() == 0) {
2019 debug_print("not even started\n");
2022 if (sc_exiting == TRUE) {
2023 debug_print("exit pending\n");
2027 debug_print("exiting\n");
2028 if (compose_get_compose_list()) {
2029 if (!draft_all_messages()) {
2030 main_window_popup(mainwin);
2036 if (prefs_common.warn_queued_on_exit && procmsg_have_queued_mails_fast()) {
2037 if (alertpanel(_("Queued messages"),
2038 _("Some unsent messages are queued. Exit now?"),
2039 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL)
2040 != G_ALERTALTERNATE) {
2041 main_window_popup(mainwin);
2045 manage_window_focus_in(mainwin->window, NULL, NULL);
2049 #ifdef HAVE_VALGRIND
2050 if (RUNNING_ON_VALGRIND) {
2051 summary_clear_list(mainwin->summaryview);
2054 if (folderview_get_selected_item(mainwin->folderview))
2055 folder_item_close(folderview_get_selected_item(mainwin->folderview));
2059 gboolean claws_is_exiting(void)
2064 gboolean claws_is_starting(void)
2070 * CLAWS: want this public so crash dialog can delete the
2073 gchar *claws_get_socket_name(void)
2075 static gchar *filename = NULL;
2076 const gchar *socket_dir = NULL;
2078 if (rc_dir_is_alt())
2079 socket_dir = get_rc_dir();
2081 socket_dir = g_get_tmp_dir();
2082 if (filename == NULL) {
2083 filename = g_strdup_printf("%s%cclaws-mail-%d",
2084 socket_dir, G_DIR_SEPARATOR,
2095 static gchar *get_crashfile_name(void)
2097 static gchar *filename = NULL;
2099 if (filename == NULL) {
2100 filename = g_strdup_printf("%s%cclaws-crashed",
2101 get_tmp_dir(), G_DIR_SEPARATOR);
2107 static gint prohibit_duplicate_launch(void)
2112 path = claws_get_socket_name();
2113 uxsock = fd_connect_unix(path);
2115 if (x_display == NULL)
2116 x_display = g_strdup(g_getenv("DISPLAY"));
2120 return fd_open_unix(path);
2123 /* remote command mode */
2125 debug_print("another Claws Mail instance is already running.\n");
2127 if (cmd.receive_all) {
2128 fd_write_all(uxsock, "receive_all\n", 12);
2129 } else if (cmd.receive) {
2130 fd_write_all(uxsock, "receive\n", 8);
2131 } else if (cmd.compose && cmd.attach_files) {
2132 gchar *str, *compose_str;
2135 if (cmd.compose_mailto) {
2136 compose_str = g_strdup_printf("compose_attach %s\n",
2137 cmd.compose_mailto);
2139 compose_str = g_strdup("compose_attach\n");
2142 fd_write_all(uxsock, compose_str, strlen(compose_str));
2143 g_free(compose_str);
2145 for (i = 0; i < cmd.attach_files->len; i++) {
2146 str = g_ptr_array_index(cmd.attach_files, i);
2147 fd_write_all(uxsock, str, strlen(str));
2148 fd_write_all(uxsock, "\n", 1);
2151 fd_write_all(uxsock, ".\n", 2);
2152 } else if (cmd.compose) {
2155 if (cmd.compose_mailto) {
2156 compose_str = g_strdup_printf
2157 ("compose %s\n", cmd.compose_mailto);
2159 compose_str = g_strdup("compose\n");
2162 fd_write_all(uxsock, compose_str, strlen(compose_str));
2163 g_free(compose_str);
2164 } else if (cmd.subscribe) {
2165 gchar *str = g_strdup_printf("subscribe %s\n", cmd.subscribe_uri);
2166 fd_write_all(uxsock, str, strlen(str));
2168 } else if (cmd.send) {
2169 fd_write_all(uxsock, "send\n", 5);
2170 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
2171 fd_write(uxsock, "online\n", 6);
2172 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
2173 fd_write(uxsock, "offline\n", 7);
2174 } else if (cmd.status || cmd.status_full) {
2175 gchar buf[BUFFSIZE];
2177 const gchar *command;
2181 command = cmd.status_full ? "status-full\n" : "status\n";
2182 folders = cmd.status_full ? cmd.status_full_folders :
2185 fd_write_all(uxsock, command, strlen(command));
2186 for (i = 0; folders && i < folders->len; ++i) {
2187 folder = g_ptr_array_index(folders, i);
2188 fd_write_all(uxsock, folder, strlen(folder));
2189 fd_write_all(uxsock, "\n", 1);
2191 fd_write_all(uxsock, ".\n", 2);
2193 fd_gets(uxsock, buf, sizeof(buf));
2194 if (!strncmp(buf, ".\n", 2)) break;
2197 } else if (cmd.exit) {
2198 fd_write_all(uxsock, "exit\n", 5);
2199 } else if (cmd.target) {
2200 gchar *str = g_strdup_printf("select %s\n", cmd.target);
2201 fd_write_all(uxsock, str, strlen(str));
2205 fd_write_all(uxsock, "get_display\n", 12);
2206 memset(buf, 0, sizeof(buf));
2207 fd_gets(uxsock, buf, sizeof(buf));
2208 if (strcmp2(buf, x_display)) {
2209 g_print("Claws Mail is already running on display %s.\n",
2213 uxsock = fd_connect_unix(path);
2214 fd_write_all(uxsock, "popup\n", 6);
2222 static gint lock_socket_remove(void)
2227 if (lock_socket < 0) {
2231 if (lock_socket_tag > 0) {
2232 gdk_input_remove(lock_socket_tag);
2234 fd_close(lock_socket);
2235 filename = claws_get_socket_name();
2236 claws_unlink(filename);
2242 static GPtrArray *get_folder_item_list(gint sock)
2244 gchar buf[BUFFSIZE];
2246 GPtrArray *folders = NULL;
2249 fd_gets(sock, buf, sizeof(buf));
2250 if (!strncmp(buf, ".\n", 2)) {
2255 folders = g_ptr_array_new();
2257 item = folder_find_item_from_identifier(buf);
2259 g_ptr_array_add(folders, item);
2261 g_warning("no such folder: %s\n", buf);
2268 static void lock_socket_input_cb(gpointer data,
2270 GdkInputCondition condition)
2272 MainWindow *mainwin = (MainWindow *)data;
2274 gchar buf[BUFFSIZE];
2276 sock = fd_accept(source);
2277 fd_gets(sock, buf, sizeof(buf));
2279 if (!strncmp(buf, "popup", 5)) {
2280 main_window_popup(mainwin);
2281 } else if (!strncmp(buf, "get_display", 11)) {
2282 fd_write_all(sock, x_display, strlen(x_display));
2283 } else if (!strncmp(buf, "receive_all", 11)) {
2284 inc_all_account_mail(mainwin, FALSE,
2285 prefs_common.newmail_notify_manu);
2286 } else if (!strncmp(buf, "receive", 7)) {
2287 inc_mail(mainwin, prefs_common.newmail_notify_manu);
2288 } else if (!strncmp(buf, "compose_attach", 14)) {
2292 mailto = g_strdup(buf + strlen("compose_attach") + 1);
2293 files = g_ptr_array_new();
2294 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
2295 if (buf[0] == '.' && buf[1] == '\n') {
2299 g_ptr_array_add(files, g_strdup(buf));
2301 open_compose_new(mailto, files);
2302 ptr_array_free_strings(files);
2303 g_ptr_array_free(files, TRUE);
2305 } else if (!strncmp(buf, "compose", 7)) {
2306 open_compose_new(buf + strlen("compose") + 1, NULL);
2307 } else if (!strncmp(buf, "subscribe", 9)) {
2308 main_window_popup(mainwin);
2309 folder_subscribe(buf + strlen("subscribe") + 1);
2310 } else if (!strncmp(buf, "send", 4)) {
2312 } else if (!strncmp(buf, "online", 6)) {
2313 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
2314 } else if (!strncmp(buf, "offline", 7)) {
2315 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
2316 } else if (!strncmp(buf, "status-full", 11) ||
2317 !strncmp(buf, "status", 6)) {
2321 folders = get_folder_item_list(sock);
2322 status = folder_get_status
2323 (folders, !strncmp(buf, "status-full", 11));
2324 fd_write_all(sock, status, strlen(status));
2325 fd_write_all(sock, ".\n", 2);
2327 if (folders) g_ptr_array_free(folders, TRUE);
2328 } else if (!strncmp(buf, "select ", 7)) {
2329 const gchar *target = buf+7;
2330 mainwindow_jump_to(target, TRUE);
2331 } else if (!strncmp(buf, "exit", 4)) {
2332 app_will_exit(NULL, mainwin);
2338 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
2343 Xstrdup_a(addr, address, return);
2347 compose_new(NULL, addr, attach_files);
2350 static void send_queue(void)
2353 gchar *errstr = NULL;
2354 gboolean error = FALSE;
2355 for (list = folder_get_list(); list != NULL; list = list->next) {
2356 Folder *folder = list->data;
2358 if (folder->queue) {
2359 gint res = procmsg_send_queue
2360 (folder->queue, prefs_common.savemsg,
2364 folder_item_scan(folder->queue);
2372 alertpanel_error_log(_("Some errors occurred "
2373 "while sending queued messages:\n%s"), errstr);
2376 alertpanel_error_log("Some errors occurred "
2377 "while sending queued messages.");
2381 static void quit_signal_handler(int sig)
2383 debug_print("Quitting on signal %d\n", sig);
2385 g_timeout_add(0, clean_quit, NULL);
2388 static void install_basic_sighandlers()
2392 struct sigaction act;
2397 sigaddset(&mask, SIGTERM);
2400 sigaddset(&mask, SIGINT);
2403 sigaddset(&mask, SIGHUP);
2406 act.sa_handler = quit_signal_handler;
2411 sigaction(SIGTERM, &act, 0);
2414 sigaction(SIGINT, &act, 0);
2417 sigaction(SIGHUP, &act, 0);
2420 sigprocmask(SIG_UNBLOCK, &mask, 0);
2421 #endif /* !G_OS_WIN32 */
2424 #if (defined linux && defined SIGIO)
2425 static int mem_notify_fd = 0;
2427 static gboolean clean_caches(gpointer unused)
2429 if (static_mainwindow && static_mainwindow->lock_count > 0)
2431 debug_print("/dev/mem_notify: callback: Freeing some memory now!\n");
2432 folder_clean_cache_memory_force();
2436 static void memory_signal_handler(int sig)
2438 debug_print("/dev/mem_notify: Kernel says we should free up some memory!\n");
2439 g_timeout_add(10, clean_caches, NULL);
2442 static void install_memory_sighandler()
2445 struct sigaction act;
2448 mem_notify_fd = open("/dev/mem_notify", O_RDONLY|O_NONBLOCK);
2449 if (mem_notify_fd == -1) {
2450 debug_print("/dev/mem_notify not available (%s)\n",
2455 fcntl(mem_notify_fd, F_SETOWN, getpid());
2456 flags = fcntl(mem_notify_fd, F_GETFL);
2457 fcntl(mem_notify_fd, flags|FASYNC);
2461 sigaddset(&mask, SIGIO);
2463 act.sa_handler = memory_signal_handler;
2467 sigaction(SIGIO, &act, 0);
2469 sigprocmask(SIG_UNBLOCK, &mask, 0);
2471 debug_print("/dev/mem_notify: installed handler\n");
2473 #endif /* linux && SIGIO */
2476 osso_context_t *get_osso_context(void)
2478 return static_osso_context;
2483 #ifdef HAVE_NETWORKMANAGER_SUPPORT
2484 static void networkmanager_state_change_cb(DBusGProxy *proxy, gchar *dev,
2487 MainWindow *mainWin;
2490 if (static_mainwindow)
2491 mainWin = static_mainwindow;
2493 mainWin = (MainWindow*)data;
2495 if (!prefs_common.use_networkmanager)
2503 online = networkmanager_is_online(&error);
2505 if(online && went_offline_nm) {
2506 went_offline_nm = FALSE;
2507 main_window_toggle_work_offline(mainWin, FALSE, FALSE);
2508 debug_print("NetworkManager: Went online\n");
2509 log_message(LOG_PROTOCOL, _("NetworkManager: network is online.\n"));
2512 went_offline_nm = TRUE;
2513 main_window_toggle_work_offline(mainWin, TRUE, FALSE);
2514 debug_print("NetworkManager: Went offline\n");
2515 log_message(LOG_PROTOCOL, _("NetworkManager: network is offline.\n"));
2519 debug_print("Failed to get online information from NetworkManager: %s\n",
2521 g_error_free(error);
2525 debug_print("NetworkManager: Cannot change connection state because "
2526 "main window does not exist\n");
2529 /* Returns true (and sets error appropriately, if given) in case of error */
2530 gboolean networkmanager_is_online(GError **error)
2532 DBusGConnection *connection;
2534 GError *tmp_error = NULL;
2538 if (!prefs_common.use_networkmanager)
2543 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &tmp_error);
2546 /* If calling code doesn't do error checking, at least print some debug */
2547 if((error == NULL) || (*error == NULL))
2548 debug_print("Failed to open connection to system bus: %s\n",
2549 tmp_error->message);
2550 g_propagate_error(error, tmp_error);
2554 proxy = dbus_g_proxy_new_for_name(connection,
2555 "org.freedesktop.NetworkManager",
2556 "/org/freedesktop/NetworkManager",
2557 "org.freedesktop.NetworkManager");
2559 retVal = dbus_g_proxy_call(proxy,"state",&tmp_error, G_TYPE_INVALID,
2560 G_TYPE_UINT, &state, G_TYPE_INVALID);
2563 g_object_unref(proxy);
2565 dbus_g_connection_unref(connection);
2568 /* If calling code doesn't do error checking, at least print some debug */
2569 if((error == NULL) || (*error == NULL))
2570 debug_print("Failed to get state info from NetworkManager: %s\n",
2571 tmp_error->message);
2572 g_propagate_error(error, tmp_error);
2576 return (state == NM_STATE_CONNECTED || state == NM_STATE_UNKNOWN);