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 2 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <glib/gi18n.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtkrc.h>
38 #include <sys/types.h>
43 #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>
56 #include "mainwindow.h"
57 #include "folderview.h"
58 #include "image_viewer.h"
59 #include "summaryview.h"
60 #include "prefs_common.h"
61 #include "prefs_account.h"
62 #include "prefs_actions.h"
63 #include "prefs_ext_prog.h"
64 #include "prefs_fonts.h"
65 #include "prefs_image_viewer.h"
66 #include "prefs_message.h"
67 #include "prefs_receive.h"
68 #include "prefs_msg_colors.h"
69 #include "prefs_quote.h"
70 #include "prefs_spelling.h"
71 #include "prefs_summaries.h"
72 #include "prefs_themes.h"
73 #include "prefs_other.h"
74 #include "prefs_send.h"
75 #include "prefs_wrapping.h"
76 #include "prefs_compose_writing.h"
77 #include "prefs_display_header.h"
82 #include "manage_window.h"
83 #include "alertpanel.h"
84 #include "statusbar.h"
85 #include "addressbook.h"
93 #include "prefs_toolbar.h"
100 #include "imap-thread.h"
102 #include "stock_pixmap.h"
103 #include "valgrind.h"
118 #ifdef HAVE_STARTUP_NOTIFICATION
119 static SnLauncheeContext
*sn_context
= NULL
;
120 static SnDisplay
*sn_display
= NULL
;
123 static gint lock_socket
= -1;
124 static gint lock_socket_tag
= 0;
128 ONLINE_MODE_DONT_CHANGE
,
133 static struct RemoteCmd
{
135 gboolean receive_all
;
137 const gchar
*compose_mailto
;
138 GPtrArray
*attach_files
;
140 gboolean status_full
;
141 GPtrArray
*status_folders
;
142 GPtrArray
*status_full_folders
;
149 const gchar
*subscribe_uri
;
153 static void parse_cmd_opt(int argc
, char *argv
[]);
155 static gint
prohibit_duplicate_launch (void);
156 static gchar
* get_crashfile_name (void);
157 static gint
lock_socket_remove (void);
158 static void lock_socket_input_cb (gpointer data
,
160 GdkInputCondition condition
);
162 static void open_compose_new (const gchar
*address
,
163 GPtrArray
*attach_files
);
165 static void send_queue (void);
166 static void initial_processing (FolderItem
*item
, gpointer data
);
167 static void quit_signal_handler (int sig
);
168 static void install_basic_sighandlers (void);
169 static void exit_claws (MainWindow
*mainwin
);
171 #define MAKE_DIR_IF_NOT_EXIST(dir) \
173 if (!is_dir_exist(dir)) { \
174 if (is_file_exist(dir)) { \
176 (_("File '%s' already exists.\n" \
177 "Can't create folder."), \
181 if (make_dir(dir) < 0) \
186 static MainWindow
*static_mainwindow
;
187 static gboolean emergency_exit
= FALSE
;
189 #ifdef HAVE_STARTUP_NOTIFICATION
190 static void sn_error_trap_push(SnDisplay
*display
, Display
*xdisplay
)
192 gdk_error_trap_push();
195 static void sn_error_trap_pop(SnDisplay
*display
, Display
*xdisplay
)
197 gdk_error_trap_pop();
200 static void startup_notification_complete(gboolean with_window
)
203 GtkWidget
*hack
= NULL
;
206 /* this is needed to make the startup notification leave,
207 * if we have been launched from a menu.
208 * We have to display a window, so let it be very little */
209 hack
= gtk_window_new(GTK_WINDOW_POPUP
);
210 gtk_widget_set_uposition(hack
, 0, 0);
211 gtk_widget_set_size_request(hack
, 1, 1);
212 gtk_widget_show(hack
);
215 xdisplay
= GDK_DISPLAY();
216 sn_display
= sn_display_new(xdisplay
,
219 sn_context
= sn_launchee_context_new_from_environment(sn_display
,
220 DefaultScreen(xdisplay
));
222 if (sn_context
!= NULL
) {
223 sn_launchee_context_complete(sn_context
);
224 sn_launchee_context_unref(sn_context
);
225 sn_display_unref(sn_display
);
228 gtk_widget_destroy(hack
);
231 #endif /* HAVE_STARTUP_NOTIFICATION */
233 static void claws_gtk_idle(void)
235 while(gtk_events_pending()) {
236 gtk_main_iteration();
241 static gboolean
defer_check_all(void *data
)
243 gboolean autochk
= GPOINTER_TO_INT(data
);
245 inc_all_account_mail(static_mainwindow
, autochk
,
246 prefs_common
.newmail_notify_manu
);
251 static gboolean
defer_check(void *data
)
253 inc_mail(static_mainwindow
, prefs_common
.newmail_notify_manu
);
258 static gboolean
defer_jump(void *data
)
260 mainwindow_jump_to(data
);
264 static void chk_update_val(GtkWidget
*widget
, gpointer data
)
266 gboolean
*val
= (gboolean
*)data
;
267 *val
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget
));
270 static gboolean
migrate_old_config(const gchar
*old_cfg_dir
, const gchar
*new_cfg_dir
, const gchar
*oldversion
)
272 gchar
*message
= g_strdup_printf(_("Configuration for %s (or previous) found.\n"
273 "Do you want to migrate this configuration?"), oldversion
);
275 GtkWidget
*window
= NULL
;
276 GtkWidget
*keep_backup_chk
;
277 GtkTooltips
*tips
= gtk_tooltips_new();
278 gboolean backup
= TRUE
;
280 keep_backup_chk
= gtk_check_button_new_with_label (_("Keep old configuration"));
281 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_backup_chk
), TRUE
);
282 gtk_tooltips_set_tip(GTK_TOOLTIPS(tips
), keep_backup_chk
,
283 _("Keeping a backup will allow you to go back to an "
284 "older version, but may take a while if you have "
285 "cached IMAP or News data, and will take some extra "
286 "room on your disk."),
289 g_signal_connect(G_OBJECT(keep_backup_chk
), "toggled",
290 G_CALLBACK(chk_update_val
), &backup
);
292 if (alertpanel_full(_("Migration of configuration"), message
,
293 GTK_STOCK_NO
, "+" GTK_STOCK_YES
, NULL
, FALSE
,
294 keep_backup_chk
, ALERT_QUESTION
, G_ALERTDEFAULT
) != G_ALERTALTERNATE
) {
298 /* we can either do a fast migration requiring not any extra disk
299 * space, or a slow one that copies the old configuration and leaves
303 window
= label_window_create(_("Copying configuration... This may take a while..."));
306 r
= copy_dir(old_cfg_dir
, new_cfg_dir
);
307 gtk_widget_destroy(window
);
309 /* if copy failed, we'll remove the partially copied
312 alertpanel_error(_("Migration failed!"));
313 remove_dir_recursive(new_cfg_dir
);
316 /* fast mode failed, but we don't want backup */
317 remove_dir_recursive(old_cfg_dir
);
321 window
= label_window_create(_("Migrating configuration..."));
324 r
= g_rename(old_cfg_dir
, new_cfg_dir
);
325 gtk_widget_destroy(window
);
327 /* if g_rename failed, we'll try to copy */
329 FILE_OP_ERROR(new_cfg_dir
, "g_rename failed, trying copy\n");
336 static void migrate_common_rc(const gchar
*old_rc
, const gchar
*new_rc
)
339 gchar
*plugin_path
, *old_plugin_path
, *new_plugin_path
;
341 oldfp
= g_fopen(old_rc
, "r");
344 newfp
= g_fopen(new_rc
, "w");
350 plugin_path
= g_strdup(get_plugin_dir());
351 new_plugin_path
= g_strdup(plugin_path
);
353 if (strstr(plugin_path
, "/claws-mail/")) {
354 gchar
*end
= g_strdup(strstr(plugin_path
, "/claws-mail/")+strlen("/claws-mail/"));
355 *(strstr(plugin_path
, "/claws-mail/")) = '\0';
356 old_plugin_path
= g_strconcat(plugin_path
, "/sylpheed-claws/", end
, NULL
);
359 old_plugin_path
= g_strdup(new_plugin_path
);
361 debug_print("replacing %s with %s\n", old_plugin_path
, new_plugin_path
);
362 while (fgets(buf
, sizeof(buf
), oldfp
)) {
363 if (strncmp(buf
, old_plugin_path
, strlen(old_plugin_path
))) {
366 debug_print("->replacing %s", buf
);
367 debug_print(" with %s%s", new_plugin_path
, buf
+strlen(old_plugin_path
));
368 fputs(new_plugin_path
, newfp
);
369 fputs(buf
+strlen(old_plugin_path
), newfp
);
373 g_free(new_plugin_path
);
374 g_free(old_plugin_path
);
381 sc_client_set_value (MainWindow
*mainwin
,
392 prop
.num_vals
= num_vals
;
396 if (mainwin
->smc_conn
)
397 SmcSetProperties ((SmcConn
) mainwin
->smc_conn
, 1, proplist
);
400 static void sc_die_callback (SmcConn smc_conn
, SmPointer client_data
)
405 static void sc_save_complete_callback(SmcConn smc_conn
, SmPointer client_data
)
409 static void sc_shutdown_cancelled_callback (SmcConn smc_conn
, SmPointer client_data
)
411 MainWindow
*mainwin
= (MainWindow
*)client_data
;
412 if (mainwin
->smc_conn
)
413 SmcSaveYourselfDone ((SmcConn
) mainwin
->smc_conn
, TRUE
);
416 static void sc_save_yourself_callback (SmcConn smc_conn
,
417 SmPointer client_data
,
423 MainWindow
*mainwin
= (MainWindow
*)client_data
;
424 if (mainwin
->smc_conn
)
425 SmcSaveYourselfDone ((SmcConn
) mainwin
->smc_conn
, TRUE
);
428 static IceIOErrorHandler sc_ice_installed_handler
;
430 static void sc_ice_io_error_handler (IceConn connection
)
432 if (sc_ice_installed_handler
)
433 (*sc_ice_installed_handler
) (connection
);
435 static gboolean
sc_process_ice_messages (GIOChannel
*source
,
436 GIOCondition condition
,
439 IceConn connection
= (IceConn
) data
;
440 IceProcessMessagesStatus status
;
442 status
= IceProcessMessages (connection
, NULL
, NULL
);
444 if (status
== IceProcessMessagesIOError
) {
445 IcePointer context
= IceGetConnectionContext (connection
);
447 if (context
&& GTK_IS_OBJECT (context
)) {
448 guint disconnect_id
= g_signal_lookup ("disconnect", G_OBJECT_TYPE (context
));
450 if (disconnect_id
> 0)
451 g_signal_emit (context
, disconnect_id
, 0);
453 IceSetShutdownNegotiation (connection
, False
);
454 IceCloseConnection (connection
);
461 static void new_ice_connection (IceConn connection
, IcePointer client_data
, Bool opening
,
462 IcePointer
*watch_data
)
468 /* Make sure we don't pass on these file descriptors to any
470 fcntl(IceConnectionNumber(connection
),F_SETFD
,
471 fcntl(IceConnectionNumber(connection
),F_GETFD
,0) | FD_CLOEXEC
);
473 channel
= g_io_channel_unix_new (IceConnectionNumber (connection
));
474 input_id
= g_io_add_watch (channel
,
475 G_IO_IN
| G_IO_HUP
| G_IO_ERR
| G_IO_PRI
,
476 sc_process_ice_messages
,
478 g_io_channel_unref (channel
);
480 *watch_data
= (IcePointer
) GUINT_TO_POINTER (input_id
);
482 input_id
= GPOINTER_TO_UINT ((gpointer
) *watch_data
);
483 g_source_remove (input_id
);
487 static void sc_session_manager_connect(MainWindow
*mainwin
)
489 static gboolean connected
= FALSE
;
490 SmcCallbacks callbacks
;
492 IceIOErrorHandler default_handler
;
499 sc_ice_installed_handler
= IceSetIOErrorHandler (NULL
);
500 default_handler
= IceSetIOErrorHandler (sc_ice_io_error_handler
);
502 if (sc_ice_installed_handler
== default_handler
)
503 sc_ice_installed_handler
= NULL
;
505 IceAddConnectionWatch (new_ice_connection
, NULL
);
508 callbacks
.save_yourself
.callback
= sc_save_yourself_callback
;
509 callbacks
.die
.callback
= sc_die_callback
;
510 callbacks
.save_complete
.callback
= sc_save_complete_callback
;
511 callbacks
.shutdown_cancelled
.callback
= sc_shutdown_cancelled_callback
;
513 callbacks
.save_yourself
.client_data
=
514 callbacks
.die
.client_data
=
515 callbacks
.save_complete
.client_data
=
516 callbacks
.shutdown_cancelled
.client_data
= (SmPointer
) mainwin
;
517 if (g_getenv ("SESSION_MANAGER")) {
518 gchar error_string_ret
[256] = "";
520 mainwin
->smc_conn
= (gpointer
)
521 SmcOpenConnection (NULL
, mainwin
,
522 SmProtoMajor
, SmProtoMinor
,
523 SmcSaveYourselfProcMask
| SmcDieProcMask
|
524 SmcSaveCompleteProcMask
|
525 SmcShutdownCancelledProcMask
,
528 256, error_string_ret
);
530 if (error_string_ret
[0] || mainwin
->smc_conn
== NULL
)
531 g_warning ("While connecting to session manager:\n%s.",
535 vals
= g_new (SmPropValue
, 1);
536 vals
[0].length
= strlen(argv0
);
537 vals
[0].value
= argv0
;
538 sc_client_set_value (mainwin
, SmCloneCommand
, SmLISTofARRAY8
, 1, vals
);
539 sc_client_set_value (mainwin
, SmRestartCommand
, SmLISTofARRAY8
, 1, vals
);
540 sc_client_set_value (mainwin
, SmProgram
, SmARRAY8
, 1, vals
);
542 vals
[0].length
= strlen(g_get_user_name()?g_get_user_name():"");
543 vals
[0].value
= g_strdup(g_get_user_name()?g_get_user_name():"");
544 sc_client_set_value (mainwin
, SmUserID
, SmARRAY8
, 1, vals
);
550 static gboolean sc_exiting
= FALSE
;
551 static gboolean sc_starting
= FALSE
;
552 static gboolean show_at_startup
= TRUE
;
554 void main_set_show_at_startup(gboolean show
)
556 show_at_startup
= show
;
559 int main(int argc
, char *argv
[])
563 FolderView
*folderview
;
565 gboolean crash_file_present
= FALSE
;
566 gint num_folder_class
= 0;
567 gboolean asked_for_migration
= FALSE
;
569 START_TIMING("startup");
573 if (!claws_init(&argc
, &argv
)) {
577 prefs_prepare_cache();
578 prog_version
= PROG_VERSION
;
579 argv0
= g_strdup(argv
[0]);
581 parse_cmd_opt(argc
, argv
);
586 gtk_init(&argc
, &argv
);
587 crash_main(cmd
.crash_params
);
590 crash_install_handlers();
592 install_basic_sighandlers();
595 /* check and create unix domain socket for remote operation */
597 lock_socket
= prohibit_duplicate_launch();
598 if (lock_socket
< 0) {
599 #ifdef HAVE_STARTUP_NOTIFICATION
600 if(gtk_init_check(&argc
, &argv
))
601 startup_notification_complete(TRUE
);
606 if (cmd
.status
|| cmd
.status_full
) {
607 puts("0 Claws Mail not running.");
608 lock_socket_remove();
615 if (!g_thread_supported())
619 gtk_init(&argc
, &argv
);
622 gtk_widget_set_default_colormap(gdk_rgb_get_colormap());
623 gtk_widget_set_default_visual(gdk_rgb_get_visual());
625 if (!g_thread_supported()) {
626 g_error(_("g_thread is not supported by glib.\n"));
629 /* check that we're not on a too recent/old gtk+ */
630 #if GTK_CHECK_VERSION(2, 9, 0)
631 if (gtk_check_version(2, 9, 0) != NULL
) {
632 alertpanel_error(_("Claws Mail has been compiled with "
633 "a more recent GTK+ library than is "
634 "currently available. This will cause "
635 "crashes. You need to upgrade GTK+ or "
636 "recompile Claws Mail."));
640 if (gtk_check_version(2, 9, 0) == NULL
) {
641 alertpanel_error(_("Claws Mail has been compiled with "
642 "an older GTK+ library than is "
643 "currently available. This will cause "
644 "crashes. You need to recompile "
649 /* parse gtkrc files */
650 userrc
= g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S
, ".gtkrc-2.0",
652 gtk_rc_parse(userrc
);
654 userrc
= g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S
, ".gtk",
655 G_DIR_SEPARATOR_S
, "gtkrc-2.0", NULL
);
656 gtk_rc_parse(userrc
);
659 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
661 /* no config dir exists. See if we can migrate an old config. */
662 if (!is_dir_exist(RC_DIR
)) {
663 prefs_destroy_cache();
666 /* if one of the old dirs exist, we'll ask if the user
667 * want to migrates, and r will be TRUE if he said yes
668 * and migration succeeded, and FALSE otherwise.
670 if (is_dir_exist(OLD_GTK2_RC_DIR
)) {
671 r
= migrate_old_config(OLD_GTK2_RC_DIR
, RC_DIR
, "Sylpheed-Claws 2.6.0");
672 asked_for_migration
= TRUE
;
673 } else if (is_dir_exist(OLDER_GTK2_RC_DIR
)) {
674 r
= migrate_old_config(OLDER_GTK2_RC_DIR
, RC_DIR
, "Sylpheed-Claws 1.9.15");
675 asked_for_migration
= TRUE
;
676 } else if (is_dir_exist(OLD_GTK1_RC_DIR
)) {
677 r
= migrate_old_config(OLD_GTK1_RC_DIR
, RC_DIR
, "Sylpheed-Claws 1.0.5");
678 asked_for_migration
= TRUE
;
681 /* If migration failed or the user didn't want to do it,
682 * we create a new one (and we'll hit wizard later).
684 if (r
== FALSE
&& !is_dir_exist(RC_DIR
) && make_dir(RC_DIR
) < 0)
689 if (!is_file_exist(RC_DIR G_DIR_SEPARATOR_S COMMON_RC
) &&
690 is_file_exist(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC
)) {
691 /* post 2.6 name change */
692 migrate_common_rc(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC
,
693 RC_DIR G_DIR_SEPARATOR_S COMMON_RC
);
697 plugin_load_all("Common");
699 userrc
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, "gtkrc-2.0", NULL
);
700 gtk_rc_parse(userrc
);
703 userrc
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, MENU_RC
, NULL
);
704 gtk_accel_map_load (userrc
);
707 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
709 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
710 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
711 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
712 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
713 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
714 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR
);
716 crash_file_present
= is_file_exist(get_crashfile_name());
717 /* remove temporary files */
718 remove_all_files(get_tmp_dir());
719 remove_all_files(get_mime_tmp_dir());
721 if (is_file_exist("claws.log")) {
722 if (rename_force("claws.log", "claws.log.bak") < 0)
723 FILE_OP_ERROR("claws.log", "rename");
725 set_log_file("claws.log");
727 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
729 folder_system_init();
730 prefs_common_read_config();
734 prefs_ext_prog_init();
735 prefs_wrapping_init();
736 prefs_compose_writing_init();
737 prefs_msg_colors_init();
739 prefs_image_viewer_init();
741 prefs_summaries_init();
742 prefs_message_init();
744 prefs_receive_init();
747 gtkaspell_checkers_init();
748 prefs_spelling_init();
751 sock_set_io_timeout(prefs_common
.io_timeout_secs
);
753 imap_main_set_timeout(prefs_common
.io_timeout_secs
);
755 prefs_actions_read_config();
756 prefs_display_header_read_config();
757 /* prefs_filtering_read_config(); */
758 addressbook_read_file();
759 renderer_read_config();
762 stock_pixbuf_gdk(NULL
, STOCK_PIXMAP_CLAWS_MAIL_ICON
, &icon
);
763 gtk_window_set_default_icon(icon
);
765 folderview_initialize();
771 mainwin
= main_window_create();
773 manage_window_focus_in(mainwin
->window
, NULL
, NULL
);
774 folderview
= mainwin
->folderview
;
776 gtk_clist_freeze(GTK_CLIST(mainwin
->folderview
->ctree
));
777 folder_item_update_freeze();
779 /* register the callback of unix domain socket input */
781 lock_socket_tag
= gdk_input_add(lock_socket
,
782 GDK_INPUT_READ
| GDK_INPUT_EXCEPTION
,
783 lock_socket_input_cb
,
787 prefs_account_init();
788 account_read_config_all();
790 /* If we can't read a folder list or don't have accounts,
791 * it means the configuration's not done. Either this is
792 * a brand new install, either a failed/refused migration.
793 * So we'll start the wizard.
795 if (folder_read_list() < 0) {
796 prefs_destroy_cache();
798 /* if run_wizard returns FALSE it's because it's
799 * been cancelled. We can't do much but exit.
800 * however, if the user was asked for a migration,
801 * we remove the newly created directory so that
802 * he's asked again for migration on next launch.*/
803 if (!run_wizard(mainwin
, TRUE
)) {
804 if (asked_for_migration
)
805 remove_dir_recursive(RC_DIR
);
808 main_window_reflect_prefs_all_now();
812 if (!account_get_list()) {
813 prefs_destroy_cache();
814 if (!run_wizard(mainwin
, FALSE
)) {
815 if (asked_for_migration
)
816 remove_dir_recursive(RC_DIR
);
819 account_read_config_all();
820 if(!account_get_list()) {
827 toolbar_main_set_sensitive(mainwin
);
828 main_window_set_menu_sensitive(mainwin
);
830 /* if crashed, show window early so that the user
831 * sees what's happening */
832 if (!cmd
.crash
&& crash_file_present
)
833 main_window_popup(mainwin
);
836 imap_main_init(prefs_common
.skip_ssl_cert_check
);
838 account_set_missing_folder();
839 folder_set_missing_folders();
840 folderview_set(folderview
);
842 prefs_matcher_read_config();
844 /* make one all-folder processing before using claws */
845 main_window_cursor_wait(mainwin
);
846 folder_func_to_all_folders(initial_processing
, (gpointer
*)mainwin
);
848 /* if claws crashed, rebuild caches */
849 if (!cmd
.crash
&& crash_file_present
) {
851 debug_print("Claws Mail crashed, checking for new messages in local folders\n");
852 folder_item_update_thaw();
853 folderview_check_new(NULL
);
854 folder_clean_cache_memory_force();
855 folder_item_update_freeze();
857 /* make the crash-indicator file */
858 str_write_to_file("foo", get_crashfile_name());
860 inc_autocheck_timer_init(mainwin
);
862 /* ignore SIGPIPE signal for preventing sudden death of program */
864 signal(SIGPIPE
, SIG_IGN
);
866 if (cmd
.online_mode
== ONLINE_MODE_OFFLINE
) {
867 main_window_toggle_work_offline(mainwin
, TRUE
, FALSE
);
869 if (cmd
.online_mode
== ONLINE_MODE_ONLINE
) {
870 main_window_toggle_work_offline(mainwin
, FALSE
, FALSE
);
873 if (cmd
.status_folders
) {
874 g_ptr_array_free(cmd
.status_folders
, TRUE
);
875 cmd
.status_folders
= NULL
;
877 if (cmd
.status_full_folders
) {
878 g_ptr_array_free(cmd
.status_full_folders
, TRUE
);
879 cmd
.status_full_folders
= NULL
;
882 claws_register_idle_function(claws_gtk_idle
);
884 prefs_toolbar_init();
886 num_folder_class
= g_list_length(folder_get_list());
888 plugin_load_all("GTK2");
890 if (g_list_length(folder_get_list()) != num_folder_class
) {
891 debug_print("new folders loaded, reloading processing rules\n");
892 prefs_matcher_read_config();
895 if (plugin_get_unloaded_list() != NULL
) {
896 main_window_cursor_normal(mainwin
);
897 alertpanel_warning(_("Some plugin(s) failed to load. "
898 "Check the Plugins configuration "
899 "for more information."));
900 main_window_cursor_wait(mainwin
);
903 plugin_load_standard_plugins ();
905 /* if not crashed, show window now */
906 if (!(!cmd
.crash
&& crash_file_present
)) {
907 /* apart if something told not to show */
909 main_window_popup(mainwin
);
912 if (!folder_have_mailbox()) {
913 prefs_destroy_cache();
914 main_window_cursor_normal(mainwin
);
915 if (folder_get_list() != NULL
) {
916 alertpanel_error(_("Claws Mail has detected a configured "
917 "mailbox, but is it incomplete. It is "
918 "possibly due to a failing IMAP account. Use "
919 "\"Rebuild folder tree\" on the mailbox's folder "
920 "to try to fix it."));
922 alertpanel_error(_("Claws Mail has detected a configured "
923 "mailbox, but could not load it. It is "
924 "probably provided by an out-of-date "
925 "external plugin. Please reinstall the "
926 "plugin and try again."));
932 static_mainwindow
= mainwin
;
934 #ifdef HAVE_STARTUP_NOTIFICATION
935 startup_notification_complete(FALSE
);
938 sc_session_manager_connect(mainwin
);
940 folder_item_update_thaw();
941 gtk_clist_thaw(GTK_CLIST(mainwin
->folderview
->ctree
));
942 main_window_cursor_normal(mainwin
);
944 if (cmd
.receive_all
) {
945 g_timeout_add(1000, defer_check_all
, GINT_TO_POINTER(FALSE
));
946 } else if (prefs_common
.chk_on_startup
) {
947 g_timeout_add(1000, defer_check_all
, GINT_TO_POINTER(TRUE
));
948 } else if (cmd
.receive
) {
949 g_timeout_add(1000, defer_check
, NULL
);
951 gtk_widget_grab_focus(folderview
->ctree
);
955 open_compose_new(cmd
.compose_mailto
, cmd
.attach_files
);
957 if (cmd
.attach_files
) {
958 ptr_array_free_strings(cmd
.attach_files
);
959 g_ptr_array_free(cmd
.attach_files
, TRUE
);
960 cmd
.attach_files
= NULL
;
963 folder_subscribe(cmd
.subscribe_uri
);
971 g_timeout_add(500, defer_jump
, (gpointer
)cmd
.target
);
974 prefs_destroy_cache();
976 compose_reopen_exit_drafts();
988 static void save_all_caches(FolderItem
*item
, gpointer data
)
995 folder_item_close(item
);
997 folder_item_free_cache(item
, TRUE
);
1000 static void exit_claws(MainWindow
*mainwin
)
1006 debug_print("shutting down\n");
1007 inc_autocheck_timer_remove();
1009 if (prefs_common
.clean_on_exit
&& !emergency_exit
) {
1010 main_window_empty_trash(mainwin
, prefs_common
.ask_on_clean
);
1013 /* save prefs for opened folder */
1014 if(mainwin
->folderview
->opened
) {
1017 item
= gtk_ctree_node_get_row_data(GTK_CTREE(mainwin
->folderview
->ctree
), mainwin
->folderview
->opened
);
1018 summary_save_prefs_to_folderitem(mainwin
->folderview
->summaryview
, item
);
1021 /* save all state before exiting */
1022 folder_func_to_all_folders(save_all_caches
, NULL
);
1023 folder_write_list();
1025 main_window_get_size(mainwin
);
1026 main_window_get_position(mainwin
);
1028 prefs_common_write_config();
1029 account_write_config_all();
1030 addressbook_export_to_file();
1032 filename
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, MENU_RC
, NULL
);
1033 gtk_accel_map_save(filename
);
1036 /* delete temporary files */
1037 remove_all_files(get_tmp_dir());
1038 remove_all_files(get_mime_tmp_dir());
1042 #ifdef HAVE_LIBETPAN
1045 /* delete crashfile */
1047 g_unlink(get_crashfile_name());
1049 lock_socket_remove();
1052 if (mainwin
->smc_conn
)
1053 SmcCloseConnection ((SmcConn
)mainwin
->smc_conn
, 0, NULL
);
1054 mainwin
->smc_conn
= NULL
;
1057 main_window_destroy_all();
1059 plugin_unload_all("GTK2");
1061 prefs_toolbar_done();
1063 addressbook_destroy();
1065 prefs_themes_done();
1067 prefs_ext_prog_done();
1068 prefs_wrapping_done();
1069 prefs_compose_writing_done();
1070 prefs_msg_colors_done();
1071 prefs_image_viewer_done();
1072 image_viewer_done();
1074 prefs_summaries_done();
1075 prefs_message_done();
1077 prefs_receive_done();
1080 prefs_spelling_done();
1081 gtkaspell_checkers_quit();
1083 plugin_unload_all("Common");
1087 static void parse_cmd_opt(int argc
, char *argv
[])
1091 for (i
= 1; i
< argc
; i
++) {
1092 if (!strncmp(argv
[i
], "--receive-all", 13)) {
1093 cmd
.receive_all
= TRUE
;
1094 } else if (!strncmp(argv
[i
], "--receive", 9)) {
1096 } else if (!strncmp(argv
[i
], "--compose", 9)) {
1097 const gchar
*p
= argv
[i
+ 1];
1100 cmd
.compose_mailto
= NULL
;
1101 if (p
&& *p
!= '\0' && *p
!= '-') {
1102 if (!strncmp(p
, "mailto:", 7)) {
1103 cmd
.compose_mailto
= p
+ 7;
1105 cmd
.compose_mailto
= p
;
1109 } else if (!strncmp(argv
[i
], "--subscribe", 11)) {
1110 const gchar
*p
= argv
[i
+ 1];
1111 if (p
&& *p
!= '\0' && *p
!= '-') {
1112 cmd
.subscribe
= TRUE
;
1113 cmd
.subscribe_uri
= p
;
1115 } else if (!strncmp(argv
[i
], "--attach", 8)) {
1116 const gchar
*p
= argv
[i
+ 1];
1119 while (p
&& *p
!= '\0' && *p
!= '-') {
1120 if (!cmd
.attach_files
) {
1121 cmd
.attach_files
= g_ptr_array_new();
1123 if ((file
= g_filename_from_uri(p
, NULL
, NULL
)) != NULL
) {
1124 if (!is_file_exist(file
)) {
1129 if (file
== NULL
&& *p
!= G_DIR_SEPARATOR
) {
1130 file
= g_strconcat(claws_get_startup_dir(),
1133 } else if (file
== NULL
) {
1136 g_ptr_array_add(cmd
.attach_files
, file
);
1140 } else if (!strncmp(argv
[i
], "--send", 6)) {
1142 } else if (!strncmp(argv
[i
], "--version", 9) ||
1143 !strncmp(argv
[i
], "-v", 2)) {
1144 puts("Claws Mail version " VERSION
);
1146 } else if (!strncmp(argv
[i
], "--status-full", 13)) {
1147 const gchar
*p
= argv
[i
+ 1];
1149 cmd
.status_full
= TRUE
;
1150 while (p
&& *p
!= '\0' && *p
!= '-') {
1151 if (!cmd
.status_full_folders
) {
1152 cmd
.status_full_folders
=
1155 g_ptr_array_add(cmd
.status_full_folders
,
1160 } else if (!strncmp(argv
[i
], "--status", 8)) {
1161 const gchar
*p
= argv
[i
+ 1];
1164 while (p
&& *p
!= '\0' && *p
!= '-') {
1165 if (!cmd
.status_folders
)
1166 cmd
.status_folders
= g_ptr_array_new();
1167 g_ptr_array_add(cmd
.status_folders
,
1172 } else if (!strncmp(argv
[i
], "--online", 8)) {
1173 cmd
.online_mode
= ONLINE_MODE_ONLINE
;
1174 } else if (!strncmp(argv
[i
], "--offline", 9)) {
1175 cmd
.online_mode
= ONLINE_MODE_OFFLINE
;
1176 } else if (!strncmp(argv
[i
], "--help", 6) ||
1177 !strncmp(argv
[i
], "-h", 2)) {
1178 gchar
*base
= g_path_get_basename(argv
[0]);
1179 g_print(_("Usage: %s [OPTION]...\n"), base
);
1181 g_print("%s\n", _(" --compose [address] open composition window"));
1182 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
1183 g_print("%s\n", _(" --attach file1 [file2]...\n"
1184 " open composition window with specified files\n"
1186 g_print("%s\n", _(" --receive receive new messages"));
1187 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
1188 g_print("%s\n", _(" --send send all queued messages"));
1189 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
1190 g_print("%s\n", _(" --status-full [folder]...\n"
1191 " show the status of each folder"));
1192 g_print("%s\n", _(" --select folder[/msg] jumps to the specified folder/message\n"
1193 " folder is a folder id like 'folder/sub_folder'"));
1194 g_print("%s\n", _(" --online switch to online mode"));
1195 g_print("%s\n", _(" --offline switch to offline mode"));
1196 g_print("%s\n", _(" --exit --quit -q exit Claws Mail"));
1197 g_print("%s\n", _(" --debug debug mode"));
1198 g_print("%s\n", _(" --help -h display this help and exit"));
1199 g_print("%s\n", _(" --version -v output version information and exit"));
1200 g_print("%s\n", _(" --config-dir output configuration directory"));
1204 } else if (!strncmp(argv
[i
], "--crash", 7)) {
1206 cmd
.crash_params
= g_strdup(argv
[i
+ 1]);
1208 } else if (!strncmp(argv
[i
], "--config-dir", sizeof "--config-dir" - 1)) {
1211 } else if (!strncmp(argv
[i
], "--exit", 6) ||
1212 !strncmp(argv
[i
], "--quit", 6) ||
1213 !strncmp(argv
[i
], "-q", 2)) {
1215 } else if (!strncmp(argv
[i
], "--select", 8) && i
+1 < argc
) {
1216 cmd
.target
= argv
[i
+1];
1217 } else if (i
== 1 && argc
== 2) {
1218 /* only one parameter. Do something intelligent about it */
1219 if (strstr(argv
[i
], "@") && !strstr(argv
[i
], "://")) {
1220 const gchar
*p
= argv
[i
];
1223 cmd
.compose_mailto
= NULL
;
1224 if (p
&& *p
!= '\0' && *p
!= '-') {
1225 if (!strncmp(p
, "mailto:", 7)) {
1226 cmd
.compose_mailto
= p
+ 7;
1228 cmd
.compose_mailto
= p
;
1231 } else if (strstr(argv
[i
], "://")) {
1232 const gchar
*p
= argv
[i
];
1233 if (p
&& *p
!= '\0' && *p
!= '-') {
1234 cmd
.subscribe
= TRUE
;
1235 cmd
.subscribe_uri
= p
;
1237 } else if (!strcmp(argv
[i
], "--sync")) {
1240 g_print(_("Unknown option\n"));
1246 if (cmd
.attach_files
&& cmd
.compose
== FALSE
) {
1248 cmd
.compose_mailto
= NULL
;
1252 static void initial_processing(FolderItem
*item
, gpointer data
)
1254 MainWindow
*mainwin
= (MainWindow
*)data
;
1257 g_return_if_fail(item
);
1258 buf
= g_strdup_printf(_("Processing (%s)..."),
1261 : _("top level folder"));
1265 if (item
->prefs
->enable_processing
) {
1266 folder_item_apply_processing(item
);
1269 STATUSBAR_POP(mainwin
);
1272 static void draft_all_messages(void)
1274 GList
*compose_list
= NULL
;
1276 compose_clear_exit_drafts();
1277 while ((compose_list
= compose_get_compose_list()) != NULL
) {
1278 Compose
*c
= (Compose
*)compose_list
->data
;
1279 compose_draft(c
, COMPOSE_DRAFT_FOR_EXIT
);
1282 gboolean
clean_quit(gpointer data
)
1284 static gboolean firstrun
= TRUE
;
1291 /*!< Good idea to have the main window stored in a
1292 * static variable so we can check that variable
1293 * to see if we're really allowed to do things
1294 * that actually the spawner is supposed to
1295 * do (like: sending mail, composing messages).
1296 * Because, really, if we're the spawnee, and
1297 * we touch GTK stuff, we're hosed. See the
1300 /* FIXME: Use something else to signal that we're
1301 * in the original spawner, and not in a spawned
1303 if (!static_mainwindow
) {
1307 draft_all_messages();
1308 emergency_exit
= TRUE
;
1309 exit_claws(static_mainwindow
);
1315 void app_will_exit(GtkWidget
*widget
, gpointer data
)
1317 MainWindow
*mainwin
= data
;
1319 if (sc_exiting
== TRUE
) {
1320 debug_print("exit pending\n");
1324 debug_print("exiting\n");
1325 if (compose_get_compose_list()) {
1326 draft_all_messages();
1329 if (prefs_common
.warn_queued_on_exit
&& procmsg_have_queued_mails_fast()) {
1330 if (alertpanel(_("Queued messages"),
1331 _("Some unsent messages are queued. Exit now?"),
1332 GTK_STOCK_CANCEL
, GTK_STOCK_OK
, NULL
)
1333 != G_ALERTALTERNATE
) {
1334 main_window_popup(mainwin
);
1338 manage_window_focus_in(mainwin
->window
, NULL
, NULL
);
1342 if (RUNNING_ON_VALGRIND
) {
1343 summary_clear_list(mainwin
->summaryview
);
1345 if (folderview_get_selected_item(mainwin
->folderview
))
1346 folder_item_close(folderview_get_selected_item(mainwin
->folderview
));
1350 gboolean
claws_is_exiting(void)
1355 gboolean
claws_is_starting(void)
1361 * CLAWS: want this public so crash dialog can delete the
1364 gchar
*claws_get_socket_name(void)
1366 static gchar
*filename
= NULL
;
1368 if (filename
== NULL
) {
1369 filename
= g_strdup_printf("%s%cclaws-mail-%d",
1370 g_get_tmp_dir(), G_DIR_SEPARATOR
,
1381 static gchar
*get_crashfile_name(void)
1383 static gchar
*filename
= NULL
;
1385 if (filename
== NULL
) {
1386 filename
= g_strdup_printf("%s%cclaws-crashed",
1387 get_tmp_dir(), G_DIR_SEPARATOR
);
1393 static gint
prohibit_duplicate_launch(void)
1398 path
= claws_get_socket_name();
1399 uxsock
= fd_connect_unix(path
);
1402 return fd_open_unix(path
);
1405 /* remote command mode */
1407 debug_print("another Claws Mail instance is already running.\n");
1409 if (cmd
.receive_all
) {
1410 fd_write_all(uxsock
, "receive_all\n", 12);
1411 } else if (cmd
.receive
) {
1412 fd_write_all(uxsock
, "receive\n", 8);
1413 } else if (cmd
.compose
&& cmd
.attach_files
) {
1414 gchar
*str
, *compose_str
;
1417 if (cmd
.compose_mailto
) {
1418 compose_str
= g_strdup_printf("compose_attach %s\n",
1419 cmd
.compose_mailto
);
1421 compose_str
= g_strdup("compose_attach\n");
1424 fd_write_all(uxsock
, compose_str
, strlen(compose_str
));
1425 g_free(compose_str
);
1427 for (i
= 0; i
< cmd
.attach_files
->len
; i
++) {
1428 str
= g_ptr_array_index(cmd
.attach_files
, i
);
1429 fd_write_all(uxsock
, str
, strlen(str
));
1430 fd_write_all(uxsock
, "\n", 1);
1433 fd_write_all(uxsock
, ".\n", 2);
1434 } else if (cmd
.compose
) {
1437 if (cmd
.compose_mailto
) {
1438 compose_str
= g_strdup_printf
1439 ("compose %s\n", cmd
.compose_mailto
);
1441 compose_str
= g_strdup("compose\n");
1444 fd_write_all(uxsock
, compose_str
, strlen(compose_str
));
1445 g_free(compose_str
);
1446 } else if (cmd
.subscribe
) {
1447 gchar
*str
= g_strdup_printf("subscribe %s\n", cmd
.subscribe_uri
);
1448 fd_write_all(uxsock
, str
, strlen(str
));
1450 } else if (cmd
.send
) {
1451 fd_write_all(uxsock
, "send\n", 5);
1452 } else if (cmd
.online_mode
== ONLINE_MODE_ONLINE
) {
1453 fd_write(uxsock
, "online\n", 6);
1454 } else if (cmd
.online_mode
== ONLINE_MODE_OFFLINE
) {
1455 fd_write(uxsock
, "offline\n", 7);
1456 } else if (cmd
.status
|| cmd
.status_full
) {
1457 gchar buf
[BUFFSIZE
];
1459 const gchar
*command
;
1463 command
= cmd
.status_full
? "status-full\n" : "status\n";
1464 folders
= cmd
.status_full
? cmd
.status_full_folders
:
1467 fd_write_all(uxsock
, command
, strlen(command
));
1468 for (i
= 0; folders
&& i
< folders
->len
; ++i
) {
1469 folder
= g_ptr_array_index(folders
, i
);
1470 fd_write_all(uxsock
, folder
, strlen(folder
));
1471 fd_write_all(uxsock
, "\n", 1);
1473 fd_write_all(uxsock
, ".\n", 2);
1475 fd_gets(uxsock
, buf
, sizeof(buf
));
1476 if (!strncmp(buf
, ".\n", 2)) break;
1479 } else if (cmd
.exit
) {
1480 fd_write_all(uxsock
, "exit\n", 5);
1481 } else if (cmd
.target
) {
1482 gchar
*str
= g_strdup_printf("select %s\n", cmd
.target
);
1483 fd_write_all(uxsock
, str
, strlen(str
));
1486 fd_write_all(uxsock
, "popup\n", 6);
1492 static gint
lock_socket_remove(void)
1497 if (lock_socket
< 0) {
1501 if (lock_socket_tag
> 0) {
1502 gdk_input_remove(lock_socket_tag
);
1504 fd_close(lock_socket
);
1505 filename
= claws_get_socket_name();
1512 static GPtrArray
*get_folder_item_list(gint sock
)
1514 gchar buf
[BUFFSIZE
];
1516 GPtrArray
*folders
= NULL
;
1519 fd_gets(sock
, buf
, sizeof(buf
));
1520 if (!strncmp(buf
, ".\n", 2)) {
1525 folders
= g_ptr_array_new();
1527 item
= folder_find_item_from_identifier(buf
);
1529 g_ptr_array_add(folders
, item
);
1531 g_warning("no such folder: %s\n", buf
);
1538 static void lock_socket_input_cb(gpointer data
,
1540 GdkInputCondition condition
)
1542 MainWindow
*mainwin
= (MainWindow
*)data
;
1544 gchar buf
[BUFFSIZE
];
1546 sock
= fd_accept(source
);
1547 fd_gets(sock
, buf
, sizeof(buf
));
1549 if (!strncmp(buf
, "popup", 5)) {
1550 main_window_popup(mainwin
);
1551 } else if (!strncmp(buf
, "receive_all", 11)) {
1552 inc_all_account_mail(mainwin
, FALSE
,
1553 prefs_common
.newmail_notify_manu
);
1554 } else if (!strncmp(buf
, "receive", 7)) {
1555 inc_mail(mainwin
, prefs_common
.newmail_notify_manu
);
1556 } else if (!strncmp(buf
, "compose_attach", 14)) {
1560 mailto
= g_strdup(buf
+ strlen("compose_attach") + 1);
1561 files
= g_ptr_array_new();
1562 while (fd_gets(sock
, buf
, sizeof(buf
)) > 0) {
1563 if (buf
[0] == '.' && buf
[1] == '\n') {
1567 g_ptr_array_add(files
, g_strdup(buf
));
1569 open_compose_new(mailto
, files
);
1570 ptr_array_free_strings(files
);
1571 g_ptr_array_free(files
, TRUE
);
1573 } else if (!strncmp(buf
, "compose", 7)) {
1574 open_compose_new(buf
+ strlen("compose") + 1, NULL
);
1575 } else if (!strncmp(buf
, "subscribe", 9)) {
1576 main_window_popup(mainwin
);
1577 folder_subscribe(buf
+ strlen("subscribe") + 1);
1578 } else if (!strncmp(buf
, "send", 4)) {
1580 } else if (!strncmp(buf
, "online", 6)) {
1581 main_window_toggle_work_offline(mainwin
, FALSE
, FALSE
);
1582 } else if (!strncmp(buf
, "offline", 7)) {
1583 main_window_toggle_work_offline(mainwin
, TRUE
, FALSE
);
1584 } else if (!strncmp(buf
, "status-full", 11) ||
1585 !strncmp(buf
, "status", 6)) {
1589 folders
= get_folder_item_list(sock
);
1590 status
= folder_get_status
1591 (folders
, !strncmp(buf
, "status-full", 11));
1592 fd_write_all(sock
, status
, strlen(status
));
1593 fd_write_all(sock
, ".\n", 2);
1595 if (folders
) g_ptr_array_free(folders
, TRUE
);
1596 } else if (!strncmp(buf
, "select ", 7)) {
1597 const gchar
*target
= buf
+7;
1598 mainwindow_jump_to(target
);
1599 } else if (!strncmp(buf
, "exit", 4)) {
1600 app_will_exit(NULL
, mainwin
);
1606 static void open_compose_new(const gchar
*address
, GPtrArray
*attach_files
)
1611 Xstrdup_a(addr
, address
, return);
1615 compose_new(NULL
, addr
, attach_files
);
1618 static void send_queue(void)
1621 gchar
*errstr
= NULL
;
1622 for (list
= folder_get_list(); list
!= NULL
; list
= list
->next
) {
1623 Folder
*folder
= list
->data
;
1625 if (folder
->queue
) {
1626 gint res
= procmsg_send_queue
1627 (folder
->queue
, prefs_common
.savemsg
,
1631 folder_item_scan(folder
->queue
);
1636 gchar
*tmp
= g_strdup_printf(_("Some errors occurred "
1637 "while sending queued messages:\n%s"), errstr
);
1639 alertpanel_error_log(tmp
);
1642 alertpanel_error_log("Some errors occurred "
1643 "while sending queued messages.");
1647 static void quit_signal_handler(int sig
)
1649 debug_print("Quitting on signal %d\n", sig
);
1651 g_timeout_add(0, clean_quit
, NULL
);
1654 static void install_basic_sighandlers()
1658 struct sigaction act
;
1663 sigaddset(&mask
, SIGTERM
);
1666 sigaddset(&mask
, SIGINT
);
1669 sigaddset(&mask
, SIGHUP
);
1672 act
.sa_handler
= quit_signal_handler
;
1677 sigaction(SIGTERM
, &act
, 0);
1680 sigaction(SIGINT
, &act
, 0);
1683 sigaction(SIGHUP
, &act
, 0);
1686 sigprocmask(SIG_UNBLOCK
, &mask
, 0);
1687 #endif /* !G_OS_WIN32 */