2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2023 the Claws Mail team and Hiroyuki Yamamoto
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/>.
21 #include "claws-features.h"
27 #include <glib/gi18n.h>
37 #include <sys/types.h>
44 #include <X11/SM/SMlib.h>
51 #include "file_checker.h"
53 #ifdef HAVE_STARTUP_NOTIFICATION
54 #ifdef GDK_WINDOWING_X11
55 # define SN_API_NOT_YET_FROZEN
56 # include <libsn/sn-launchee.h>
57 # include <gdk/gdkx.h>
62 #include <dbus/dbus-glib.h>
64 #ifdef HAVE_NETWORKMANAGER_SUPPORT
65 #include <NetworkManager.h>
71 #include <librsvg/rsvg.h>
76 #include "mainwindow.h"
77 #include "folderview.h"
78 #include "image_viewer.h"
79 #include "summaryview.h"
80 #include "prefs_common.h"
81 #include "prefs_account.h"
82 #include "prefs_actions.h"
83 #include "prefs_ext_prog.h"
84 #include "prefs_fonts.h"
85 #include "prefs_image_viewer.h"
86 #include "prefs_message.h"
87 #include "prefs_migration.h"
88 #include "prefs_receive.h"
89 #include "prefs_msg_colors.h"
90 #include "prefs_quote.h"
91 #include "prefs_spelling.h"
92 #include "prefs_summaries.h"
93 #include "prefs_themes.h"
94 #include "prefs_other.h"
95 #include "prefs_proxy.h"
96 #include "prefs_logging.h"
97 #include "prefs_send.h"
98 #include "prefs_wrapping.h"
99 #include "prefs_compose_writing.h"
100 #include "prefs_display_header.h"
105 #include "send_message.h"
108 #include "manage_window.h"
109 #include "alertpanel.h"
110 #include "statusbar.h"
111 #ifndef USE_ALT_ADDRBOOK
112 #include "addressbook.h"
114 #include "addressbook-dbus.h"
118 #include "folder_item_prefs.h"
121 #include "gtkutils.h"
124 #include "prefs_toolbar.h"
127 #include "imap_gtk.h"
128 #include "news_gtk.h"
133 #include "quicksearch.h"
134 #include "advsearch.h"
136 #include "passwordstore.h"
137 #include "file-utils.h"
140 #include "imap-thread.h"
141 #include "nntp-thread.h"
143 #include "stock_pixmap.h"
154 #ifdef HAVE_NETWORKMANAGER_SUPPORT
155 /* Went offline due to NetworkManager */
156 static gboolean went_offline_nm
;
160 #ifdef HAVE_DBUS_GLIB
161 static DBusGProxy
*awn_proxy
= NULL
;
165 #if (defined HAVE_LIBSM || defined CRASH_DIALOG)
169 #ifdef HAVE_STARTUP_NOTIFICATION
170 static SnLauncheeContext
*sn_context
= NULL
;
171 static SnDisplay
*sn_display
= NULL
;
174 static gint lock_socket
= -1;
175 static gint lock_socket_tag
= 0;
179 ONLINE_MODE_DONT_CHANGE
,
184 static struct RemoteCmd
{
186 gboolean receive_all
;
187 gboolean cancel_receiving
;
188 gboolean cancel_sending
;
190 const gchar
*compose_mailto
;
193 const gchar
*search_folder
;
194 const gchar
*search_type
;
195 const gchar
*search_request
;
196 gboolean search_recursive
;
198 gboolean status_full
;
200 gboolean reset_statistics
;
201 GPtrArray
*status_folders
;
202 GPtrArray
*status_full_folders
;
209 const gchar
*subscribe_uri
;
212 const gchar
*geometry
;
215 SessionStats session_stats
;
217 static void reset_statistics(void);
219 static void parse_cmd_opt(int argc
, char *argv
[]);
221 static gint
prohibit_duplicate_launch (int *argc
,
223 static gchar
* get_crashfile_name (void);
224 static gint
lock_socket_remove (void);
225 static void lock_socket_input_cb (gpointer data
,
227 GIOCondition condition
);
229 static void open_compose_new (const gchar
*address
,
230 GList
*attach_files
);
232 static void send_queue (void);
233 static void initial_processing (FolderItem
*item
, gpointer data
);
235 static void quit_signal_handler (int sig
);
237 static void install_basic_sighandlers (void);
238 static void exit_claws (MainWindow
*mainwin
);
240 #ifdef HAVE_NETWORKMANAGER_SUPPORT
241 static void networkmanager_state_change_cb(DBusGProxy
*proxy
, gchar
*dev
,
245 #define MAKE_DIR_IF_NOT_EXIST(dir) \
247 if (!is_dir_exist(dir)) { \
248 if (is_file_exist(dir)) { \
250 (_("File '%s' already exists.\n" \
251 "Can't create folder."), \
255 if (make_dir(dir) < 0) \
260 #define STRNCMP(S1, S2) (strncmp((S1), (S2), sizeof((S2)) - 1))
262 #define CM_FD_WRITE(S) fd_write(sock, (S), strlen((S)))
263 #define CM_FD_WRITE_ALL(S) fd_write_all(sock, (S), strlen((S)))
265 static MainWindow
*static_mainwindow
;
267 static gboolean emergency_exit
= FALSE
;
269 #ifdef HAVE_STARTUP_NOTIFICATION
270 static void sn_error_trap_push(SnDisplay
*display
, Display
*xdisplay
)
272 gdk_error_trap_push();
275 static void sn_error_trap_pop(SnDisplay
*display
, Display
*xdisplay
)
277 gdk_error_trap_pop();
280 static void startup_notification_complete(gboolean with_window
)
283 GtkWidget
*hack
= NULL
;
286 /* this is needed to make the startup notification leave,
287 * if we have been launched from a menu.
288 * We have to display a window, so let it be very little */
289 hack
= gtk_window_new(GTK_WINDOW_POPUP
);
290 gtk_window_move(GTK_WINDOW(hack
), 0, 0);
291 gtk_widget_set_size_request(hack
, 1, 1);
292 gtk_widget_show(hack
);
295 xdisplay
= GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
296 sn_display
= sn_display_new(xdisplay
,
299 sn_context
= sn_launchee_context_new_from_environment(sn_display
,
300 DefaultScreen(xdisplay
));
302 if (sn_context
!= NULL
) {
303 sn_launchee_context_complete(sn_context
);
304 sn_launchee_context_unref(sn_context
);
305 sn_display_unref(sn_display
);
308 gtk_widget_destroy(hack
);
311 #endif /* HAVE_STARTUP_NOTIFICATION */
313 static void claws_gtk_idle(void)
315 while(gtk_events_pending()) {
316 gtk_main_iteration();
321 static gboolean sc_starting
= FALSE
;
323 static gboolean
defer_check_all(void *data
)
325 gboolean autochk
= GPOINTER_TO_INT(data
);
328 inc_all_account_mail(static_mainwindow
, autochk
, FALSE
,
329 prefs_common
.newmail_notify_manu
);
332 inc_all_account_mail(static_mainwindow
, FALSE
,
333 prefs_common
.chk_on_startup
,
334 prefs_common
.newmail_notify_manu
);
336 main_window_set_menu_sensitive(static_mainwindow
);
337 toolbar_main_set_sensitive(static_mainwindow
);
342 static gboolean
defer_check(void *data
)
344 inc_mail(static_mainwindow
, prefs_common
.newmail_notify_manu
);
348 main_window_set_menu_sensitive(static_mainwindow
);
349 toolbar_main_set_sensitive(static_mainwindow
);
354 static gboolean
defer_jump(void *data
)
356 if (cmd
.receive_all
) {
357 defer_check_all(GINT_TO_POINTER(FALSE
));
358 } else if (prefs_common
.chk_on_startup
) {
359 defer_check_all(GINT_TO_POINTER(TRUE
));
360 } else if (cmd
.receive
) {
363 mainwindow_jump_to(data
, FALSE
);
366 main_window_set_menu_sensitive(static_mainwindow
);
367 toolbar_main_set_sensitive(static_mainwindow
);
372 static void chk_update_val(GtkWidget
*widget
, gpointer data
)
374 gboolean
*val
= (gboolean
*)data
;
375 *val
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget
));
378 static gboolean
migrate_old_config(const gchar
*old_cfg_dir
, const gchar
*new_cfg_dir
, const gchar
*oldversion
)
380 gchar
*message
= g_strdup_printf(_("Configuration for %s found.\n"
381 "Do you want to migrate this configuration?"), oldversion
);
383 if (!strcmp(oldversion
, "Sylpheed")) {
384 gchar
*message2
= g_strdup_printf(_("\n\nYour Sylpheed filtering rules can be converted by a\n"
385 "script available at %s."), TOOLS_URI
);
386 gchar
*tmp
= g_strconcat(message
, message2
, NULL
);
393 GtkWidget
*window
= NULL
;
394 GtkWidget
*keep_backup_chk
;
395 gboolean backup
= TRUE
;
397 keep_backup_chk
= gtk_check_button_new_with_label (_("Keep old configuration"));
398 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_backup_chk
), TRUE
);
399 CLAWS_SET_TIP(keep_backup_chk
,
400 _("Keeping a backup will allow you to go back to an "
401 "older version, but may take a while if you have "
402 "cached IMAP or News data, and will take some extra "
403 "room on your disk."));
405 g_signal_connect(G_OBJECT(keep_backup_chk
), "toggled",
406 G_CALLBACK(chk_update_val
), &backup
);
408 if (alertpanel_full(_("Migration of configuration"), message
,
409 NULL
, _("_No"), NULL
, _("_Yes"), NULL
, NULL
, ALERTFOCUS_SECOND
,
410 FALSE
, keep_backup_chk
, ALERT_QUESTION
) != G_ALERTALTERNATE
) {
414 /* we can either do a fast migration requiring not any extra disk
415 * space, or a slow one that copies the old configuration and leaves
419 window
= label_window_create(_("Copying configuration... This may take a while..."));
422 r
= copy_dir(old_cfg_dir
, new_cfg_dir
);
423 label_window_destroy(window
);
425 /* if copy failed, we'll remove the partially copied
428 alertpanel_error(_("Migration failed!"));
429 remove_dir_recursive(new_cfg_dir
);
432 /* fast mode failed, but we don't want backup */
433 remove_dir_recursive(old_cfg_dir
);
437 window
= label_window_create(_("Migrating configuration..."));
440 r
= g_rename(old_cfg_dir
, new_cfg_dir
);
441 label_window_destroy(window
);
443 /* if g_rename failed, we'll try to copy */
445 FILE_OP_ERROR(new_cfg_dir
, "g_rename");
446 debug_print("rename failed, trying copy\n");
453 static int migrate_common_rc(const gchar
*old_rc
, const gchar
*new_rc
)
456 gchar
*plugin_path
, *old_plugin_path
, *new_plugin_path
;
458 gboolean err
= FALSE
;
460 oldfp
= claws_fopen(old_rc
, "r");
463 newfp
= claws_fopen(new_rc
, "w");
469 plugin_path
= g_strdup(get_plugin_dir());
470 new_plugin_path
= g_strdup(plugin_path
);
472 if (strstr(plugin_path
, "/claws-mail/")) {
473 gchar
*end
= g_strdup(strstr(plugin_path
, "/claws-mail/")+strlen("/claws-mail/"));
474 *(strstr(plugin_path
, "/claws-mail/")) = '\0';
475 old_plugin_path
= g_strconcat(plugin_path
, "/sylpheed-claws/", end
, NULL
);
478 old_plugin_path
= g_strdup(new_plugin_path
);
480 debug_print("replacing %s with %s\n", old_plugin_path
, new_plugin_path
);
481 while (claws_fgets(buf
, sizeof(buf
), oldfp
)) {
482 if (STRNCMP(buf
, old_plugin_path
)) {
483 err
|= (claws_fputs(buf
, newfp
) == EOF
);
485 debug_print("->replacing %s\n", buf
);
486 debug_print(" with %s%s\n", new_plugin_path
, buf
+strlen(old_plugin_path
));
487 err
|= (claws_fputs(new_plugin_path
, newfp
) == EOF
);
488 err
|= (claws_fputs(buf
+strlen(old_plugin_path
), newfp
) == EOF
);
492 g_free(new_plugin_path
);
493 g_free(old_plugin_path
);
495 if (claws_safe_fclose(newfp
) == EOF
)
503 sc_client_set_value (MainWindow
*mainwin
,
514 prop
.num_vals
= num_vals
;
518 if (mainwin
->smc_conn
)
519 SmcSetProperties ((SmcConn
) mainwin
->smc_conn
, 1, proplist
);
522 static void sc_die_callback (SmcConn smc_conn
, SmPointer client_data
)
527 static void sc_save_complete_callback(SmcConn smc_conn
, SmPointer client_data
)
531 static void sc_shutdown_cancelled_callback (SmcConn smc_conn
, SmPointer client_data
)
533 MainWindow
*mainwin
= (MainWindow
*)client_data
;
534 if (mainwin
->smc_conn
)
535 SmcSaveYourselfDone ((SmcConn
) mainwin
->smc_conn
, TRUE
);
538 static void sc_save_yourself_callback (SmcConn smc_conn
,
539 SmPointer client_data
,
545 MainWindow
*mainwin
= (MainWindow
*)client_data
;
546 if (mainwin
->smc_conn
)
547 SmcSaveYourselfDone ((SmcConn
) mainwin
->smc_conn
, TRUE
);
550 static IceIOErrorHandler sc_ice_installed_handler
;
552 static void sc_ice_io_error_handler (IceConn connection
)
554 if (sc_ice_installed_handler
)
555 (*sc_ice_installed_handler
) (connection
);
557 static gboolean
sc_process_ice_messages (GIOChannel
*source
,
558 GIOCondition condition
,
561 IceConn connection
= (IceConn
) data
;
562 IceProcessMessagesStatus status
;
564 status
= IceProcessMessages (connection
, NULL
, NULL
);
566 if (status
== IceProcessMessagesIOError
) {
567 IcePointer context
= IceGetConnectionContext (connection
);
569 if (context
&& G_IS_OBJECT(context
)) {
570 guint disconnect_id
= g_signal_lookup ("disconnect", G_OBJECT_TYPE (context
));
572 if (disconnect_id
> 0)
573 g_signal_emit (context
, disconnect_id
, 0);
575 IceSetShutdownNegotiation (connection
, False
);
576 IceCloseConnection (connection
);
583 static void new_ice_connection (IceConn connection
, IcePointer client_data
, Bool opening
,
584 IcePointer
*watch_data
)
590 /* Make sure we don't pass on these file descriptors to any
592 fcntl(IceConnectionNumber(connection
),F_SETFD
,
593 fcntl(IceConnectionNumber(connection
),F_GETFD
,0) | FD_CLOEXEC
);
595 channel
= g_io_channel_unix_new (IceConnectionNumber (connection
));
596 input_id
= g_io_add_watch (channel
,
597 G_IO_IN
| G_IO_HUP
| G_IO_ERR
| G_IO_PRI
,
598 sc_process_ice_messages
,
600 g_io_channel_unref (channel
);
602 *watch_data
= (IcePointer
) GUINT_TO_POINTER (input_id
);
604 input_id
= GPOINTER_TO_UINT ((gpointer
) *watch_data
);
605 g_source_remove (input_id
);
609 static void sc_session_manager_connect(MainWindow
*mainwin
)
611 static gboolean connected
= FALSE
;
612 SmcCallbacks callbacks
;
614 IceIOErrorHandler default_handler
;
621 sc_ice_installed_handler
= IceSetIOErrorHandler (NULL
);
622 default_handler
= IceSetIOErrorHandler (sc_ice_io_error_handler
);
624 if (sc_ice_installed_handler
== default_handler
)
625 sc_ice_installed_handler
= NULL
;
627 IceAddConnectionWatch (new_ice_connection
, NULL
);
630 callbacks
.save_yourself
.callback
= sc_save_yourself_callback
;
631 callbacks
.die
.callback
= sc_die_callback
;
632 callbacks
.save_complete
.callback
= sc_save_complete_callback
;
633 callbacks
.shutdown_cancelled
.callback
= sc_shutdown_cancelled_callback
;
635 callbacks
.save_yourself
.client_data
=
636 callbacks
.die
.client_data
=
637 callbacks
.save_complete
.client_data
=
638 callbacks
.shutdown_cancelled
.client_data
= (SmPointer
) mainwin
;
639 if (g_getenv ("SESSION_MANAGER")) {
640 gchar error_string_ret
[256] = "";
642 mainwin
->smc_conn
= (gpointer
)
643 SmcOpenConnection (NULL
, mainwin
,
644 SmProtoMajor
, SmProtoMinor
,
645 SmcSaveYourselfProcMask
| SmcDieProcMask
|
646 SmcSaveCompleteProcMask
|
647 SmcShutdownCancelledProcMask
,
650 256, error_string_ret
);
652 /* From https://www.x.org/releases/X11R7.7/doc/libSM/SMlib.txt:
653 * If SmcOpenConnection succeeds, it returns an opaque connection
654 * pointer of type SmcConn and the client_id_ret argument contains
655 * the client ID to be used for this session. The client_id_ret
656 * should be freed with a call to free when no longer needed. On
657 * failure, SmcOpenConnection returns NULL, and the reason for
658 * failure is returned in error_string_ret. */
659 if (mainwin
->smc_conn
!= NULL
)
662 if (error_string_ret
[0] || mainwin
->smc_conn
== NULL
)
663 g_warning("while connecting to session manager: %s",
667 vals
= g_new (SmPropValue
, 1);
668 vals
[0].length
= strlen(argv0
);
669 vals
[0].value
= argv0
;
670 sc_client_set_value (mainwin
, SmCloneCommand
, SmLISTofARRAY8
, 1, vals
);
671 sc_client_set_value (mainwin
, SmRestartCommand
, SmLISTofARRAY8
, 1, vals
);
672 sc_client_set_value (mainwin
, SmProgram
, SmARRAY8
, 1, vals
);
674 vals
[0].length
= strlen(g_get_user_name()?g_get_user_name():"");
675 vals
[0].value
= g_strdup(g_get_user_name()?g_get_user_name():"");
676 sc_client_set_value (mainwin
, SmUserID
, SmARRAY8
, 1, vals
);
678 g_free(vals
[0].value
);
685 static gboolean sc_exiting
= FALSE
;
686 static gboolean show_at_startup
= TRUE
;
687 static gboolean claws_crashed_bool
= FALSE
;
689 gboolean
claws_crashed(void) {
690 return claws_crashed_bool
;
693 void main_set_show_at_startup(gboolean show
)
695 show_at_startup
= show
;
699 static HANDLE win32_debug_log
= NULL
;
700 static guint win32_log_handler_app_id
;
701 static guint win32_log_handler_glib_id
;
702 static guint win32_log_handler_gtk_id
;
704 static void win32_log_WriteFile(const gchar
*string
)
709 ret
= WriteFile(win32_debug_log
, string
, strlen(string
), &bytes_written
, NULL
);
711 DWORD err
= GetLastError();
714 tmp
= g_strdup_printf("Error: WriteFile in failed with error 0x%lx. Buffer contents:\n%s", err
, string
);
715 OutputDebugString(tmp
);
720 static void win32_print_stdout(const gchar
*string
)
722 if (win32_debug_log
) {
723 win32_log_WriteFile(string
);
727 GLogWriterOutput
win32_log_writer(GLogLevelFlags log_level
, const GLogField
*fields
, gsize n_fields
, gpointer user_data
)
732 g_return_val_if_fail(win32_debug_log
!= NULL
, G_LOG_WRITER_UNHANDLED
);
733 g_return_val_if_fail(fields
!= NULL
, G_LOG_WRITER_UNHANDLED
);
734 g_return_val_if_fail(n_fields
> 0, G_LOG_WRITER_UNHANDLED
);
736 formatted
= g_log_writer_format_fields(log_level
, fields
, n_fields
, FALSE
);
737 out
= g_strdup_printf("%s\n", formatted
);
739 win32_log_WriteFile(out
);
744 return G_LOG_WRITER_HANDLED
;
747 static void win32_log(const gchar
*log_domain
, GLogLevelFlags log_level
, const gchar
* message
, gpointer user_data
)
751 if (win32_debug_log
) {
754 switch(log_level
& G_LOG_LEVEL_MASK
)
756 case G_LOG_LEVEL_ERROR
:
759 case G_LOG_LEVEL_CRITICAL
:
762 case G_LOG_LEVEL_WARNING
:
765 case G_LOG_LEVEL_MESSAGE
:
768 case G_LOG_LEVEL_INFO
:
771 case G_LOG_LEVEL_DEBUG
:
779 out
= g_strdup_printf("%s: %s: %s", log_domain
, type
, message
);
781 out
= g_strdup_printf("%s: %s", type
, message
);
783 win32_log_WriteFile(out
);
789 static void win32_open_log(void)
791 gchar
*logfile
= win32_debug_log_path();
792 gchar
*oldlogfile
= g_strconcat(logfile
, ".bak", NULL
);
794 if (is_file_exist(logfile
)) {
795 if (rename_force(logfile
, oldlogfile
) < 0)
796 FILE_OP_ERROR(logfile
, "rename");
799 win32_debug_log
= CreateFile(logfile
,
804 FILE_ATTRIBUTE_NORMAL
,
807 if (win32_debug_log
== INVALID_HANDLE_VALUE
) {
808 win32_debug_log
= NULL
;
814 if (win32_debug_log
) {
815 g_set_print_handler(win32_print_stdout
);
816 g_set_printerr_handler(win32_print_stdout
);
818 win32_log_handler_app_id
= g_log_set_handler(NULL
, G_LOG_LEVEL_MASK
| G_LOG_FLAG_FATAL
819 | G_LOG_FLAG_RECURSION
, win32_log
, NULL
);
820 win32_log_handler_glib_id
= g_log_set_handler("GLib", G_LOG_LEVEL_MASK
| G_LOG_FLAG_FATAL
821 | G_LOG_FLAG_RECURSION
, win32_log
, NULL
);
822 win32_log_handler_gtk_id
= g_log_set_handler("Gtk", G_LOG_LEVEL_MASK
| G_LOG_FLAG_FATAL
823 | G_LOG_FLAG_RECURSION
, win32_log
, NULL
);
825 g_log_set_writer_func(&win32_log_writer
, NULL
, NULL
);
829 static void win32_close_log(void)
831 if (win32_debug_log
) {
832 g_log_remove_handler("", win32_log_handler_app_id
);
833 g_log_remove_handler("GLib", win32_log_handler_glib_id
);
834 g_log_remove_handler("Gtk", win32_log_handler_gtk_id
);
835 CloseHandle(win32_debug_log
);
836 win32_debug_log
= NULL
;
841 static void main_dump_features_list(gboolean show_debug_only
)
842 /* display compiled-in features list */
844 if (show_debug_only
&& !debug_get_mode())
848 debug_print("runtime GTK %d.%d.%d / GLib %d.%d.%d\n",
849 gtk_major_version
, gtk_minor_version
, gtk_micro_version
,
850 glib_major_version
, glib_minor_version
, glib_micro_version
);
852 g_print("runtime GTK %d.%d.%d / GLib %d.%d.%d\n",
853 gtk_major_version
, gtk_minor_version
, gtk_micro_version
,
854 glib_major_version
, glib_minor_version
, glib_micro_version
);
856 debug_print("buildtime GTK %d.%d.%d / GLib %d.%d.%d\n",
857 GTK_MAJOR_VERSION
, GTK_MINOR_VERSION
, GTK_MICRO_VERSION
,
858 GLIB_MAJOR_VERSION
, GLIB_MINOR_VERSION
, GLIB_MICRO_VERSION
);
860 g_print("buildtime GTK %d.%d.%d / GLib %d.%d.%d\n",
861 GTK_MAJOR_VERSION
, GTK_MINOR_VERSION
, GTK_MICRO_VERSION
,
862 GLIB_MAJOR_VERSION
, GLIB_MINOR_VERSION
, GLIB_MICRO_VERSION
);
865 debug_print("Compiled-in features:\n");
867 g_print("Compiled-in features:\n");
870 debug_print(" compface\n");
872 g_print(" compface\n");
876 debug_print(" Enchant\n");
878 g_print(" Enchant\n");
882 debug_print(" GnuTLS\n");
884 g_print(" GnuTLS\n");
888 debug_print(" IPv6\n");
894 debug_print(" iconv\n");
900 debug_print(" JPilot\n");
902 g_print(" JPilot\n");
906 debug_print(" LDAP\n");
912 debug_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR
, LIBETPAN_VERSION_MINOR
);
914 g_print(" libetpan %d.%d\n", LIBETPAN_VERSION_MAJOR
, LIBETPAN_VERSION_MINOR
);
918 debug_print(" libSM\n");
922 #if HAVE_NETWORKMANAGER_SUPPORT
924 debug_print(" NetworkManager\n");
926 g_print(" NetworkManager\n");
930 debug_print(" librSVG " LIBRSVG_VERSION
"\n");
932 g_print(" librSVG " LIBRSVG_VERSION
"\n");
936 #ifdef HAVE_DBUS_GLIB
937 static gulong dbus_item_hook_id
= HOOK_NONE
;
938 static gulong dbus_folder_hook_id
= HOOK_NONE
;
940 static void uninstall_dbus_status_handler(void)
943 g_object_unref(awn_proxy
);
945 if (dbus_item_hook_id
!= HOOK_NONE
)
946 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST
, dbus_item_hook_id
);
947 if (dbus_folder_hook_id
!= HOOK_NONE
)
948 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST
, dbus_folder_hook_id
);
951 static void dbus_update(FolderItem
*removed_item
)
953 guint
new, unread
, unreadmarked
, marked
, total
;
954 guint replied
, forwarded
, locked
, ignored
, watched
;
956 GError
*error
= NULL
;
958 folder_count_total_msgs(&new, &unread
, &unreadmarked
, &marked
, &total
,
959 &replied
, &forwarded
, &locked
, &ignored
,
962 total
-= removed_item
->total_msgs
;
963 new -= removed_item
->new_msgs
;
964 unread
-= removed_item
->unread_msgs
;
968 buf
= g_strdup_printf("%d", new);
969 dbus_g_proxy_call(awn_proxy
, "SetInfoByName", &error
,
970 G_TYPE_STRING
, "claws-mail",
972 G_TYPE_INVALID
, G_TYPE_INVALID
);
976 dbus_g_proxy_call(awn_proxy
, "UnsetInfoByName", &error
, G_TYPE_STRING
,
977 "claws-mail", G_TYPE_INVALID
, G_TYPE_INVALID
);
980 debug_print("%s\n", error
->message
);
985 static gboolean
dbus_status_update_folder_hook(gpointer source
, gpointer data
)
987 FolderUpdateData
*hookdata
;
989 if (hookdata
->update_flags
& FOLDER_REMOVE_FOLDERITEM
)
990 dbus_update(hookdata
->item
);
997 static gboolean
dbus_status_update_item_hook(gpointer source
, gpointer data
)
1004 static void install_dbus_status_handler(void)
1006 GError
*tmp_error
= NULL
;
1007 DBusGConnection
*connection
= dbus_g_bus_get(DBUS_BUS_SESSION
, &tmp_error
);
1010 /* If calling code doesn't do error checking, at least print some debug */
1011 debug_print("Failed to open connection to session bus: %s\n",
1012 tmp_error
->message
);
1013 g_error_free(tmp_error
);
1016 awn_proxy
= dbus_g_proxy_new_for_name(connection
,
1017 "com.google.code.Awn",
1018 "/com/google/code/Awn",
1019 "com.google.code.Awn");
1020 dbus_item_hook_id
= hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST
, dbus_status_update_item_hook
, NULL
);
1021 if (dbus_item_hook_id
== HOOK_NONE
) {
1022 g_warning("failed to register folder item update hook");
1023 uninstall_dbus_status_handler();
1027 dbus_folder_hook_id
= hooks_register_hook (FOLDER_UPDATE_HOOKLIST
, dbus_status_update_folder_hook
, NULL
);
1028 if (dbus_folder_hook_id
== HOOK_NONE
) {
1029 g_warning("failed to register folder update hook");
1030 uninstall_dbus_status_handler();
1036 static void reset_statistics(void)
1038 /* (re-)initialize session statistics */
1039 session_stats
.received
= 0;
1040 session_stats
.sent
= 0;
1041 session_stats
.replied
= 0;
1042 session_stats
.forwarded
= 0;
1043 session_stats
.spam
= 0;
1044 session_stats
.time_started
= time(NULL
);
1047 int main(int argc
, char *argv
[])
1049 #ifdef HAVE_DBUS_GLIB
1050 DBusGConnection
*connection
;
1053 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1054 DBusGProxy
*nm_proxy
;
1057 MainWindow
*mainwin
;
1058 FolderView
*folderview
;
1060 gboolean crash_file_present
= FALSE
;
1061 guint num_folder_class
= 0;
1062 gboolean asked_for_migration
= FALSE
;
1063 gboolean start_done
= TRUE
;
1064 GSList
*plug_list
= NULL
;
1065 gboolean never_ran
= FALSE
;
1066 gboolean mainwin_shown
= FALSE
;
1069 START_TIMING("startup");
1076 if (!claws_init(&argc
, &argv
)) {
1083 prog_version
= PROG_VERSION
;
1084 #if (defined HAVE_LIBSM || defined CRASH_DIALOG)
1085 argv0
= g_strdup(argv
[0]);
1088 parse_cmd_opt(argc
, argv
);
1092 /* check and create unix domain socket for remote operation */
1093 lock_socket
= prohibit_duplicate_launch(&argc
, &argv
);
1094 if (lock_socket
< 0) {
1095 #ifdef HAVE_STARTUP_NOTIFICATION
1096 #ifdef GDK_WINDOWING_X11
1097 if (GDK_IS_X11_DISPLAY(gdk_display_get_default())) {
1098 if (gtk_init_check(&argc
, &argv
))
1099 startup_notification_complete(TRUE
);
1106 main_dump_features_list(TRUE
);
1107 prefs_prepare_cache();
1111 gtk_init(&argc
, &argv
);
1112 crash_main(cmd
.crash_params
);
1118 crash_install_handlers();
1120 install_basic_sighandlers();
1122 if (cmd
.status
|| cmd
.status_full
|| cmd
.search
||
1123 cmd
.statistics
|| cmd
.reset_statistics
||
1124 cmd
.cancel_receiving
|| cmd
.cancel_sending
||
1126 puts("0 Claws Mail not running.");
1127 lock_socket_remove();
1136 gtk_init(&argc
, &argv
);
1138 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1139 went_offline_nm
= FALSE
;
1142 #ifdef HAVE_DBUS_GLIB
1144 connection
= dbus_g_bus_get(DBUS_BUS_SYSTEM
, &error
);
1147 debug_print("Failed to open connection to system bus: %s\n", error
->message
);
1148 g_error_free(error
);
1151 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1152 nm_proxy
= dbus_g_proxy_new_for_name(connection
,
1153 "org.freedesktop.NetworkManager",
1154 "/org/freedesktop/NetworkManager",
1155 "org.freedesktop.NetworkManager");
1157 dbus_g_proxy_add_signal(nm_proxy
, "StateChanged", G_TYPE_UINT
, G_TYPE_INVALID
);
1158 dbus_g_proxy_connect_signal(nm_proxy
, "StateChanged",
1159 G_CALLBACK(networkmanager_state_change_cb
),
1163 install_dbus_status_handler();
1167 gtkut_create_ui_manager();
1169 /* Create container for all the menus we will be adding */
1170 MENUITEM_ADDUI("/", "Menus", NULL
, GTK_UI_MANAGER_MENUBAR
);
1173 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1175 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1178 /* no config dir exists. See if we can migrate an old config. */
1179 if (!is_dir_exist(get_rc_dir())) {
1180 prefs_destroy_cache();
1183 /* if one of the old dirs exist, we'll ask if the user
1184 * want to migrates, and r will be TRUE if he said yes
1185 * and migration succeeded, and FALSE otherwise.
1187 if (is_dir_exist(OLD_GTK2_RC_DIR
)) {
1188 r
= migrate_old_config(OLD_GTK2_RC_DIR
, get_rc_dir(),
1189 g_strconcat("Sylpheed-Claws 2.6.0 ", _("(or older)"), NULL
));
1190 asked_for_migration
= TRUE
;
1191 } else if (is_dir_exist(OLDER_GTK2_RC_DIR
)) {
1192 r
= migrate_old_config(OLDER_GTK2_RC_DIR
, get_rc_dir(),
1193 g_strconcat("Sylpheed-Claws 1.9.15 ",_("(or older)"), NULL
));
1194 asked_for_migration
= TRUE
;
1195 } else if (is_dir_exist(OLD_GTK1_RC_DIR
)) {
1196 r
= migrate_old_config(OLD_GTK1_RC_DIR
, get_rc_dir(),
1197 g_strconcat("Sylpheed-Claws 1.0.5 ",_("(or older)"), NULL
));
1198 asked_for_migration
= TRUE
;
1199 } else if (is_dir_exist(SYLPHEED_RC_DIR
)) {
1200 r
= migrate_old_config(SYLPHEED_RC_DIR
, get_rc_dir(), "Sylpheed");
1201 asked_for_migration
= TRUE
;
1204 /* If migration failed or the user didn't want to do it,
1205 * we create a new one (and we'll hit wizard later).
1207 if (r
== FALSE
&& !is_dir_exist(get_rc_dir())) {
1209 if (copy_dir(SYSCONFDIR
"/skel/.claws-mail", get_rc_dir()) < 0) {
1211 if (!is_dir_exist(get_rc_dir()) && make_dir(get_rc_dir()) < 0) {
1224 if (!is_file_exist(RC_DIR G_DIR_SEPARATOR_S COMMON_RC
) &&
1225 is_file_exist(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC
)) {
1226 /* post 2.6 name change */
1227 migrate_common_rc(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC
,
1228 RC_DIR G_DIR_SEPARATOR_S COMMON_RC
);
1232 plugin_load_all("Common");
1234 userrc
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, "gtkrc-2.0", NULL
);
1235 gtk_rc_parse(userrc
);
1238 userrc
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, MENU_RC
, NULL
);
1239 gtk_accel_map_load (userrc
);
1243 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_rc_dir(), 1, win32_close_log(););
1245 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
1248 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
1249 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
1250 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
1251 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
1252 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
1253 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR
);
1255 crash_file_present
= is_file_exist(get_crashfile_name());
1256 /* remove temporary files */
1257 remove_all_files(get_tmp_dir());
1258 remove_all_files(get_mime_tmp_dir());
1260 if (!cmd
.crash
&& crash_file_present
)
1261 claws_crashed_bool
= TRUE
;
1263 if (is_file_exist("claws.log")) {
1264 if (rename_force("claws.log", "claws.log.bak") < 0)
1265 FILE_OP_ERROR("claws.log", "rename");
1267 set_log_file(LOG_PROTOCOL
, "claws.log");
1269 if (is_file_exist("filtering.log")) {
1270 if (rename_force("filtering.log", "filtering.log.bak") < 0)
1271 FILE_OP_ERROR("filtering.log", "rename");
1273 set_log_file(LOG_DEBUG_FILTERING
, "filtering.log");
1276 CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(get_home_dir(), 1, win32_close_log(););
1278 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
1281 folder_system_init();
1282 prefs_common_read_config();
1284 if (prefs_update_config_version_common() < 0) {
1285 debug_print("Main configuration file version upgrade failed, exiting\n");
1292 prefs_themes_init();
1294 prefs_ext_prog_init();
1295 prefs_wrapping_init();
1296 prefs_compose_writing_init();
1297 prefs_msg_colors_init();
1298 image_viewer_init();
1299 prefs_image_viewer_init();
1301 prefs_summaries_init();
1302 prefs_message_init();
1305 prefs_logging_init();
1306 prefs_receive_init();
1311 gtkaspell_checkers_init();
1312 prefs_spelling_init();
1315 codeconv_set_allow_jisx0201_kana(prefs_common
.allow_jisx0201_kana
);
1316 codeconv_set_broken_are_utf8(prefs_common
.broken_are_utf8
);
1319 if(prefs_common
.gtk_theme
&& strcmp(prefs_common
.gtk_theme
, DEFAULT_W32_GTK_THEME
))
1320 gtk_settings_set_string_property(gtk_settings_get_default(),
1322 prefs_common
.gtk_theme
,
1327 sock_set_io_timeout(prefs_common
.io_timeout_secs
);
1328 prefs_actions_read_config();
1329 prefs_display_header_read_config();
1330 /* prefs_filtering_read_config(); */
1331 #ifndef USE_ALT_ADDRBOOK
1332 addressbook_read_file();
1334 g_clear_error(&error
);
1335 if (! addressbook_start_service(&error
)) {
1336 g_warning("%s", error
->message
);
1337 g_clear_error(&error
);
1340 addressbook_install_hooks(&error
);
1343 gtkut_widget_init();
1344 priv_pixbuf_gdk(PRIV_PIXMAP_CLAWS_MAIL_ICON
, &icon
);
1345 gtk_window_set_default_icon(icon
);
1347 folderview_initialize();
1353 mainwin
= main_window_create();
1355 if (!check_file_integrity())
1358 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1359 networkmanager_state_change_cb(nm_proxy
,NULL
,mainwin
);
1362 manage_window_focus_in(mainwin
->window
, NULL
, NULL
);
1363 folderview
= mainwin
->folderview
;
1365 folderview_freeze(mainwin
->folderview
);
1366 folder_item_update_freeze();
1368 if ((ret
= passwd_store_read_config()) < 0) {
1369 debug_print("Password store configuration file version upgrade failed (%d), exiting\n", ret
);
1376 prefs_account_init();
1377 account_read_config_all();
1379 if (prefs_update_config_version_accounts() < 0) {
1380 debug_print("Accounts configuration file version upgrade failed, exiting\n");
1387 #ifdef HAVE_LIBETPAN
1388 imap_main_init(prefs_common
.skip_ssl_cert_check
);
1389 imap_main_set_timeout(prefs_common
.io_timeout_secs
);
1390 nntp_main_init(prefs_common
.skip_ssl_cert_check
);
1392 /* If we can't read a folder list or don't have accounts,
1393 * it means the configuration's not done. Either this is
1394 * a brand new install, a failed/refused migration,
1395 * or a failed config_version upgrade.
1397 if ((ret
= folder_read_list()) < 0) {
1398 debug_print("Folderlist read failed (%d)\n", ret
);
1399 prefs_destroy_cache();
1402 /* config_version update failed in folder_read_list(). We
1403 * do not want to run the wizard, just exit. */
1404 debug_print("Folderlist version upgrade failed, exiting\n");
1411 /* if run_wizard returns FALSE it's because it's
1412 * been cancelled. We can't do much but exit.
1413 * however, if the user was asked for a migration,
1414 * we remove the newly created directory so that
1415 * he's asked again for migration on next launch.*/
1416 if (!run_wizard(mainwin
, TRUE
)) {
1417 if (asked_for_migration
)
1418 remove_dir_recursive(RC_DIR
);
1424 main_window_reflect_prefs_all_now();
1425 folder_write_list();
1429 if (!account_get_list()) {
1430 prefs_destroy_cache();
1431 if (!run_wizard(mainwin
, FALSE
)) {
1432 if (asked_for_migration
)
1433 remove_dir_recursive(RC_DIR
);
1439 if(!account_get_list()) {
1440 exit_claws(mainwin
);
1447 toolbar_main_set_sensitive(mainwin
);
1448 main_window_set_menu_sensitive(mainwin
);
1450 /* if crashed, show window early so that the user
1451 * sees what's happening */
1452 if (claws_crashed()) {
1453 main_window_popup(mainwin
);
1454 mainwin_shown
= TRUE
;
1457 account_set_missing_folder();
1458 folder_set_missing_folders();
1459 folderview_set(folderview
);
1461 prefs_matcher_read_config();
1462 quicksearch_set_search_strings(mainwin
->summaryview
->quicksearch
);
1464 /* make one all-folder processing before using claws */
1465 main_window_cursor_wait(mainwin
);
1466 folder_func_to_all_folders(initial_processing
, (gpointer
*)mainwin
);
1468 /* if claws crashed, rebuild caches */
1469 if (claws_crashed()) {
1471 debug_print("Claws Mail crashed, checking for new messages in local folders\n");
1472 folder_item_update_thaw();
1473 folderview_check_new(NULL
);
1474 folder_clean_cache_memory_force();
1475 folder_item_update_freeze();
1477 /* make the crash-indicator file */
1478 if (str_write_to_file("foo", get_crashfile_name(), FALSE
) < 0) {
1479 g_warning("can't create the crash-indicator file");
1482 inc_autocheck_timer_init(mainwin
);
1484 /* ignore SIGPIPE signal for preventing sudden death of program */
1486 signal(SIGPIPE
, SIG_IGN
);
1488 if (cmd
.online_mode
== ONLINE_MODE_OFFLINE
) {
1489 main_window_toggle_work_offline(mainwin
, TRUE
, FALSE
);
1491 if (cmd
.online_mode
== ONLINE_MODE_ONLINE
) {
1492 main_window_toggle_work_offline(mainwin
, FALSE
, FALSE
);
1495 if (cmd
.status_folders
) {
1496 g_ptr_array_free(cmd
.status_folders
, TRUE
);
1497 cmd
.status_folders
= NULL
;
1499 if (cmd
.status_full_folders
) {
1500 g_ptr_array_free(cmd
.status_full_folders
, TRUE
);
1501 cmd
.status_full_folders
= NULL
;
1504 claws_register_idle_function(claws_gtk_idle
);
1507 prefs_toolbar_init();
1509 num_folder_class
= g_list_length(folder_get_list());
1511 plugin_load_all("GTK3");
1513 if (g_list_length(folder_get_list()) != num_folder_class
) {
1514 debug_print("new folders loaded, reloading processing rules\n");
1515 prefs_matcher_read_config();
1518 if ((plug_list
= plugin_get_unloaded_list()) != NULL
) {
1521 gint num_plugins
= 0;
1522 for (cur
= plug_list
; cur
; cur
= cur
->next
) {
1523 Plugin
*plugin
= (Plugin
*)cur
->data
;
1524 gchar
*tmp
= g_strdup_printf("%s\n%s",
1526 plugin_get_name(plugin
));
1531 main_window_cursor_normal(mainwin
);
1532 main_window_popup(mainwin
);
1533 mainwin_shown
= TRUE
;
1534 alertpanel_warning(ngettext(
1535 "The following plugin failed to load. "
1536 "Check the Plugins configuration "
1537 "for more information:\n%s",
1538 "The following plugins failed to load. "
1539 "Check the Plugins configuration "
1540 "for more information:\n%s",
1543 main_window_cursor_wait(mainwin
);
1545 g_slist_free(plug_list
);
1549 prefs_common_write_config();
1550 plugin_load_standard_plugins ();
1553 /* if not crashed, show window now */
1554 if (!mainwin_shown
) {
1555 /* apart if something told not to show */
1556 if (show_at_startup
)
1557 main_window_popup(mainwin
);
1560 if (cmd
.geometry
!= NULL
) {
1561 if (!gtk_window_parse_geometry(GTK_WINDOW(mainwin
->window
), cmd
.geometry
))
1562 g_warning("failed to parse geometry '%s'", cmd
.geometry
);
1566 if (sscanf(cmd
.geometry
, "%ux%u+", &width
, &height
) == 2)
1567 gtk_window_resize(GTK_WINDOW(mainwin
->window
), width
, height
);
1569 g_warning("failed to parse geometry's width/height");
1573 if (!folder_have_mailbox()) {
1574 prefs_destroy_cache();
1575 main_window_cursor_normal(mainwin
);
1576 if (folder_get_list() != NULL
) {
1577 alertpanel_error(_("Claws Mail has detected a configured "
1578 "mailbox, but it is incomplete. It is "
1579 "possibly due to a failing IMAP account. Use "
1580 "\"Rebuild folder tree\" on the mailbox parent "
1581 "folder's context menu to try to fix it."));
1583 alertpanel_error(_("Claws Mail has detected a configured "
1584 "mailbox, but could not load it. It is "
1585 "probably provided by an out-of-date "
1586 "external plugin. Please reinstall the "
1587 "plugin and try again."));
1588 exit_claws(mainwin
);
1593 static_mainwindow
= mainwin
;
1595 #ifdef HAVE_STARTUP_NOTIFICATION
1596 #ifdef GDK_WINDOWING_X11
1597 if (GDK_IS_X11_DISPLAY(gdk_display_get_default()))
1598 startup_notification_complete(FALSE
);
1602 sc_session_manager_connect(mainwin
);
1605 folder_item_update_thaw();
1606 folderview_thaw(mainwin
->folderview
);
1607 main_window_cursor_normal(mainwin
);
1609 if (!cmd
.target
&& prefs_common
.goto_folder_on_startup
&&
1610 folder_find_item_from_identifier(prefs_common
.startup_folder
) != NULL
&&
1612 cmd
.target
= prefs_common
.startup_folder
;
1613 } else if (!cmd
.target
&& prefs_common
.goto_last_folder_on_startup
&&
1614 folder_find_item_from_identifier(prefs_common
.last_opened_folder
) != NULL
&&
1616 cmd
.target
= prefs_common
.last_opened_folder
;
1619 if (cmd
.receive_all
&& !cmd
.target
) {
1621 g_timeout_add(1000, defer_check_all
, GINT_TO_POINTER(FALSE
));
1622 } else if (prefs_common
.chk_on_startup
&& !cmd
.target
) {
1624 g_timeout_add(1000, defer_check_all
, GINT_TO_POINTER(TRUE
));
1625 } else if (cmd
.receive
&& !cmd
.target
) {
1627 g_timeout_add(1000, defer_check
, NULL
);
1629 folderview_grab_focus(folderview
);
1632 open_compose_new(cmd
.compose_mailto
, cmd
.attach_files
);
1634 if (cmd
.attach_files
) {
1635 list_free_strings_full(cmd
.attach_files
);
1636 cmd
.attach_files
= NULL
;
1638 if (cmd
.subscribe
) {
1639 folder_subscribe(cmd
.subscribe_uri
);
1648 g_timeout_add(500, defer_jump
, (gpointer
)cmd
.target
);
1651 prefs_destroy_cache();
1653 compose_reopen_exit_drafts();
1656 sc_starting
= FALSE
;
1657 main_window_set_menu_sensitive(mainwin
);
1658 toolbar_main_set_sensitive(mainwin
);
1661 /* register the callback of unix domain socket input */
1662 lock_socket_tag
= claws_input_add(lock_socket
,
1663 G_IO_IN
| G_IO_HUP
| G_IO_ERR
| G_IO_PRI
,
1664 lock_socket_input_cb
,
1671 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1673 g_object_unref(nm_proxy
);
1675 #ifdef HAVE_DBUS_GLIB
1676 uninstall_dbus_status_handler();
1678 dbus_g_connection_unref(connection
);
1681 exit_claws(mainwin
);
1686 static void save_all_caches(FolderItem
*item
, gpointer data
)
1693 folder_item_close(item
);
1696 folder_item_free_cache(item
, TRUE
);
1699 static void exit_claws(MainWindow
*mainwin
)
1702 gboolean have_connectivity
;
1707 debug_print("shutting down\n");
1708 inc_autocheck_timer_remove();
1710 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1711 if (prefs_common
.work_offline
&& went_offline_nm
)
1712 prefs_common
.work_offline
= FALSE
;
1715 /* save prefs for opened folder */
1716 if((item
= folderview_get_opened_item(mainwin
->folderview
)) != NULL
) {
1717 summary_save_prefs_to_folderitem(
1718 mainwin
->summaryview
, item
);
1719 if (prefs_common
.last_opened_folder
!= NULL
)
1720 g_free(prefs_common
.last_opened_folder
);
1721 prefs_common
.last_opened_folder
=
1722 !prefs_common
.goto_last_folder_on_startup
? NULL
:
1723 folder_item_get_identifier(item
);
1726 /* save all state before exiting */
1727 folder_func_to_all_folders(save_all_caches
, NULL
);
1728 folder_write_list();
1730 main_window_get_size(mainwin
);
1731 main_window_get_position(mainwin
);
1733 prefs_common_write_config();
1734 account_write_config_all();
1735 passwd_store_write_config();
1736 #ifndef USE_ALT_ADDRBOOK
1737 addressbook_export_to_file();
1739 filename
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, MENU_RC
, NULL
);
1740 gtk_accel_map_save(filename
);
1743 /* delete temporary files */
1744 remove_all_files(get_tmp_dir());
1745 remove_all_files(get_mime_tmp_dir());
1747 close_log_file(LOG_PROTOCOL
);
1748 close_log_file(LOG_DEBUG_FILTERING
);
1750 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1751 have_connectivity
= networkmanager_is_online(NULL
);
1753 have_connectivity
= TRUE
;
1755 #ifdef HAVE_LIBETPAN
1756 imap_main_done(have_connectivity
);
1757 nntp_main_done(have_connectivity
);
1759 /* delete crashfile */
1761 claws_unlink(get_crashfile_name());
1763 lock_socket_remove();
1766 if (mainwin
->smc_conn
)
1767 SmcCloseConnection ((SmcConn
)mainwin
->smc_conn
, 0, NULL
);
1768 mainwin
->smc_conn
= NULL
;
1771 main_window_destroy_all();
1773 plugin_unload_all("GTK3");
1776 prefs_toolbar_done();
1779 #ifndef USE_ALT_ADDRBOOK
1780 addressbook_destroy();
1782 prefs_themes_done();
1784 prefs_ext_prog_done();
1785 prefs_wrapping_done();
1786 prefs_compose_writing_done();
1787 prefs_msg_colors_done();
1788 prefs_image_viewer_done();
1789 image_viewer_done();
1791 prefs_summaries_done();
1792 prefs_message_done();
1795 prefs_receive_done();
1796 prefs_logging_done();
1800 prefs_spelling_done();
1801 gtkaspell_checkers_quit();
1803 plugin_unload_all("Common");
1810 #define G_PRINT_EXIT(msg) \
1816 static void parse_cmd_compose_from_file(const gchar
*fn
, GString
*body
)
1818 GString
*headers
= g_string_new(NULL
);
1826 if (fn
== NULL
|| *fn
== '\0')
1827 G_PRINT_EXIT(_("Missing filename\n"));
1828 isstdin
= (*fn
== '-' && *(fn
+ 1) == '\0');
1832 fp
= claws_fopen(fn
, "r");
1834 G_PRINT_EXIT(_("Cannot open filename for reading\n"));
1837 while (claws_fgets(fb
, sizeof(fb
), fp
)) {
1843 while (*h
&& *h
!= ':') { ++h
; } /* search colon */
1845 G_PRINT_EXIT(_("Malformed header\n"));
1847 while (*v
&& *v
== ' ') { ++v
; } /* trim value start */
1849 tmp
= g_ascii_strdown(fb
, -1); /* get header name */
1850 if (!strcmp(tmp
, "to")) {
1852 G_PRINT_EXIT(_("Duplicated 'To:' header\n"));
1855 g_string_append_c(headers
, '&');
1856 g_string_append(headers
, tmp
);
1857 g_string_append_c(headers
, '=');
1858 g_string_append_uri_escaped(headers
, v
, NULL
, TRUE
);
1863 G_PRINT_EXIT(_("Missing required 'To:' header\n"));
1864 g_string_append(body
, to
);
1866 g_string_append(body
, "?body=");
1867 while (claws_fgets(fb
, sizeof(fb
), fp
)) {
1868 g_string_append_uri_escaped(body
, fb
, NULL
, TRUE
);
1872 /* append the remaining headers */
1873 g_string_append(body
, headers
->str
);
1874 g_string_free(headers
, TRUE
);
1879 static void parse_cmd_opt_error(char *errstr
, char* optstr
)
1883 cm_return_if_fail(errstr
!= NULL
);
1884 cm_return_if_fail(optstr
!= NULL
);
1886 g_snprintf(tmp
, sizeof(tmp
), errstr
, optstr
);
1887 g_print(_("%s. Try -h or --help for usage.\n"), tmp
);
1891 static GString mailto
; /* used to feed cmd.compose_mailto when --compose-from-file is used */
1893 static void parse_cmd_opt(int argc
, char *argv
[])
1898 for (i
= 1; i
< argc
; i
++) {
1899 if (!strcmp(argv
[i
], "--receive-all")) {
1900 cmd
.receive_all
= TRUE
;
1901 } else if (!strcmp(argv
[i
], "--receive")) {
1903 } else if (!strcmp(argv
[i
], "--cancel-receiving")) {
1904 cmd
.cancel_receiving
= TRUE
;
1905 } else if (!strcmp(argv
[i
], "--cancel-sending")) {
1906 cmd
.cancel_sending
= TRUE
;
1907 } else if (!strcmp(argv
[i
], "--compose-from-file")) {
1909 const gchar
*p
= argv
[i
+1];
1911 parse_cmd_compose_from_file(p
, &mailto
);
1913 cmd
.compose_mailto
= mailto
.str
;
1916 parse_cmd_opt_error(_("Missing file argument for option %s"), argv
[i
]);
1918 } else if (!strcmp(argv
[i
], "--compose")) {
1919 const gchar
*p
= (i
+1 < argc
)?argv
[i
+1]:NULL
;
1922 cmd
.compose_mailto
= NULL
;
1923 if (p
&& *p
!= '\0' && *p
!= '-') {
1924 if (!STRNCMP(p
, "mailto:")) {
1925 cmd
.compose_mailto
= p
+ 7;
1927 cmd
.compose_mailto
= p
;
1931 } else if (!strcmp(argv
[i
], "--subscribe")) {
1933 const gchar
*p
= argv
[i
+1];
1934 if (p
&& *p
!= '\0' && *p
!= '-') {
1935 cmd
.subscribe
= TRUE
;
1936 cmd
.subscribe_uri
= p
;
1938 parse_cmd_opt_error(_("Missing or empty uri argument for option %s"), argv
[i
]);
1941 parse_cmd_opt_error(_("Missing uri argument for option %s"), argv
[i
]);
1943 } else if (!strcmp(argv
[i
], "--attach") ||
1944 !strcmp(argv
[i
], "--insert")) {
1946 const gchar
*p
= argv
[i
+1];
1949 gboolean insert
= !strcmp(argv
[i
], "--insert");
1951 while (p
&& *p
!= '\0' && *p
!= '-') {
1952 if ((file
= g_filename_from_uri(p
, NULL
, NULL
)) != NULL
) {
1953 if (!is_file_exist(file
)) {
1958 if (file
== NULL
&& *p
!= G_DIR_SEPARATOR
) {
1959 file
= g_strconcat(claws_get_startup_dir(),
1962 } else if (file
== NULL
) {
1966 ainfo
= g_new0(AttachInfo
, 1);
1968 ainfo
->insert
= insert
;
1969 cmd
.attach_files
= g_list_append(cmd
.attach_files
, ainfo
);
1971 p
= (ii
+1 < argc
)?argv
[ii
+1]:NULL
;
1974 parse_cmd_opt_error(_("Missing at least one non-empty file argument for option %s"), argv
[i
]);
1979 parse_cmd_opt_error(_("Missing file argument for option %s"), argv
[i
]);
1981 } else if (!strcmp(argv
[i
], "--send")) {
1983 } else if (!strcmp(argv
[i
], "--version-full") ||
1984 !strcmp(argv
[i
], "-V")) {
1985 g_print("Claws Mail version " VERSION_GIT_FULL
"\n");
1986 main_dump_features_list(FALSE
);
1988 } else if (!strcmp(argv
[i
], "--version") ||
1989 !strcmp(argv
[i
], "-v")) {
1990 g_print("Claws Mail version " VERSION
"\n");
1992 } else if (!strcmp(argv
[i
], "--status-full")) {
1993 const gchar
*p
= (i
+1 < argc
)?argv
[i
+1]:NULL
;
1995 cmd
.status_full
= TRUE
;
1996 while (p
&& *p
!= '\0' && *p
!= '-') {
1997 if (!cmd
.status_full_folders
) {
1998 cmd
.status_full_folders
=
2001 g_ptr_array_add(cmd
.status_full_folders
,
2004 p
= (i
+1 < argc
)?argv
[i
+1]:NULL
;
2006 } else if (!strcmp(argv
[i
], "--status")) {
2007 const gchar
*p
= (i
+1 < argc
)?argv
[i
+1]:NULL
;
2010 while (p
&& *p
!= '\0' && *p
!= '-') {
2011 if (!cmd
.status_folders
)
2012 cmd
.status_folders
= g_ptr_array_new();
2013 g_ptr_array_add(cmd
.status_folders
,
2016 p
= (i
+1 < argc
)?argv
[i
+1]:NULL
;
2018 } else if (!strcmp(argv
[i
], "--search")) {
2019 if (i
+3 < argc
) { /* 3 first arguments are mandatory */
2021 /* only set search parameters if they are valid */
2023 cmd
.search_folder
= (p
&& *p
!= '\0' && *p
!= '-')?p
:NULL
;
2025 cmd
.search_type
= (p
&& *p
!= '\0' && *p
!= '-')?p
:NULL
;
2027 cmd
.search_request
= (p
&& *p
!= '\0' && *p
!= '-')?p
:NULL
;
2028 p
= (i
+4 < argc
)?argv
[i
+4]:NULL
;
2029 const char* rec
= (p
&& *p
!= '\0' && *p
!= '-')?p
:NULL
;
2030 cmd
.search_recursive
= TRUE
;
2033 if (tolower(*rec
)=='n' || tolower(*rec
)=='f' || *rec
=='0')
2034 cmd
.search_recursive
= FALSE
;
2036 if (cmd
.search_folder
&& cmd
.search_type
&& cmd
.search_request
) {
2043 parse_cmd_opt_error(_("Missing folder, type and request arguments for option %s"), argv
[i
]);
2046 parse_cmd_opt_error(_("Missing type and request arguments for option %s"), argv
[i
]);
2049 parse_cmd_opt_error(_("Missing request argument for option %s"), argv
[i
]);
2052 } else if (!strcmp(argv
[i
], "--online")) {
2053 cmd
.online_mode
= ONLINE_MODE_ONLINE
;
2054 } else if (!strcmp(argv
[i
], "--offline")) {
2055 cmd
.online_mode
= ONLINE_MODE_OFFLINE
;
2056 } else if (!strcmp(argv
[i
], "--toggle-debug")) {
2058 } else if (!strcmp(argv
[i
], "--statistics")) {
2059 cmd
.statistics
= TRUE
;
2060 } else if (!strcmp(argv
[i
], "--reset-statistics")) {
2061 cmd
.reset_statistics
= TRUE
;
2062 } else if (!strcmp(argv
[i
], "--help") ||
2063 !strcmp(argv
[i
], "-h")) {
2064 gchar
*base
= g_path_get_basename(argv
[0]);
2065 g_print(_("Usage: %s [OPTION]...\n"), base
);
2067 g_print("%s\n", _(" --compose [address] open composition window"));
2068 g_print("%s\n", _(" --compose-from-file file\n"
2069 " open composition window with data from given file;\n"
2070 " use - as file name for reading from standard input;\n"
2071 " content format: headers first (To: required) until an\n"
2072 " empty line, then mail body until end of file."));
2073 g_print("%s\n", _(" --subscribe uri subscribe to the given URI if possible"));
2074 g_print("%s\n", _(" --attach file1 [file2]...\n"
2075 " open composition window with specified files\n"
2077 g_print("%s\n", _(" --insert file1 [file2]...\n"
2078 " open composition window with specified files\n"
2080 g_print("%s\n", _(" --receive receive new messages"));
2081 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
2082 g_print("%s\n", _(" --cancel-receiving cancel receiving of messages"));
2083 g_print("%s\n", _(" --cancel-sending cancel sending of messages"));
2084 g_print("%s\n", _(" --search folder type request [recursive]\n"
2086 " folder ex.: \"#mh/Mailbox/inbox\" or \"Mail\"\n"
2087 " type: s[ubject],f[rom],t[o],e[xtended],m[ixed] or g: tag\n"
2088 " request: search string\n"
2089 " recursive: false if arg. starts with 0, n, N, f or F"));
2091 g_print("%s\n", _(" --send send all queued messages"));
2092 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
2093 g_print("%s\n", _(" --status-full [folder]...\n"
2094 " show the status of each folder"));
2095 g_print("%s\n", _(" --statistics show session statistics"));
2096 g_print("%s\n", _(" --reset-statistics reset session statistics"));
2097 g_print("%s\n", _(" --select folder[/msg] jump to the specified folder/message\n"
2098 " folder is a folder id like 'folder/sub_folder', a file:// uri or an absolute path"));
2099 g_print("%s\n", _(" --online switch to online mode"));
2100 g_print("%s\n", _(" --offline switch to offline mode"));
2101 g_print("%s\n", _(" --exit --quit -q exit Claws Mail"));
2102 g_print("%s\n", _(" --debug -d debug mode"));
2103 g_print("%s\n", _(" --toggle-debug toggle debug mode"));
2104 g_print("%s\n", _(" --help -h display this help"));
2105 g_print("%s\n", _(" --version -v output version information"));
2106 g_print("%s\n", _(" --version-full -V output version and built-in features information"));
2107 g_print("%s\n", _(" --config-dir output configuration directory"));
2108 g_print("%s\n", _(" --alternate-config-dir directory\n"
2109 " use specified configuration directory"));
2110 g_print("%s\n", _(" --geometry -geometry [WxH][+X+Y]\n"
2111 " set geometry for main window"));
2115 } else if (!strcmp(argv
[i
], "--crash")) {
2117 cmd
.crash_params
= g_strdup((i
+1 < argc
)?argv
[i
+1]:NULL
);
2119 } else if (!strcmp(argv
[i
], "--config-dir")) {
2120 g_print(RC_DIR
"\n");
2122 } else if (!strcmp(argv
[i
], "--alternate-config-dir")) {
2124 set_rc_dir(argv
[i
+1]);
2127 parse_cmd_opt_error(_("Missing directory argument for option %s"), argv
[i
]);
2129 } else if (!strcmp(argv
[i
], "--geometry") ||
2130 !strcmp(argv
[i
], "-geometry")) {
2132 cmd
.geometry
= argv
[i
+1];
2135 parse_cmd_opt_error(_("Missing geometry argument for option %s"), argv
[i
]);
2137 } else if (!strcmp(argv
[i
], "--exit") ||
2138 !strcmp(argv
[i
], "--quit") ||
2139 !strcmp(argv
[i
], "-q")) {
2141 } else if (!strcmp(argv
[i
], "--select")) {
2143 cmd
.target
= argv
[i
+1];
2146 parse_cmd_opt_error(_("Missing folder argument for option %s"), argv
[i
]);
2148 } else if (i
== 1 && argc
== 2) {
2149 /* only one parameter. Do something intelligent about it */
2150 if ((strstr(argv
[i
], "@") || !STRNCMP(argv
[i
], "mailto:")) && !strstr(argv
[i
], "://")) {
2151 const gchar
*p
= argv
[i
];
2154 cmd
.compose_mailto
= NULL
;
2155 if (p
&& *p
!= '\0' && *p
!= '-') {
2156 if (!STRNCMP(p
, "mailto:")) {
2157 cmd
.compose_mailto
= p
+ 7;
2159 cmd
.compose_mailto
= p
;
2162 } else if (!STRNCMP(argv
[i
], "file://")) {
2163 cmd
.target
= argv
[i
];
2164 } else if (!STRNCMP(argv
[i
], "?attach=file://")) {
2165 /* Thunar support as per 3.3.0cvs19 */
2167 cmd
.compose_mailto
= argv
[i
];
2168 } else if (strstr(argv
[i
], "://")) {
2169 const gchar
*p
= argv
[i
];
2170 if (p
&& *p
!= '\0' && *p
!= '-') {
2171 cmd
.subscribe
= TRUE
;
2172 cmd
.subscribe_uri
= p
;
2174 } else if (!strcmp(argv
[i
], "--sync")) {
2176 } else if (is_dir_exist(argv
[i
]) || is_file_exist(argv
[i
])) {
2177 cmd
.target
= argv
[i
];
2179 parse_cmd_opt_error(_("Unknown option %s"), argv
[i
]);
2182 parse_cmd_opt_error(_("Unknown option %s"), argv
[i
]);
2186 if (cmd
.attach_files
&& cmd
.compose
== FALSE
) {
2188 cmd
.compose_mailto
= NULL
;
2192 static void initial_processing(FolderItem
*item
, gpointer data
)
2194 MainWindow
*mainwin
= (MainWindow
*)data
;
2197 cm_return_if_fail(item
);
2198 buf
= g_strdup_printf(_("Processing (%s)..."),
2201 : _("top level folder"));
2204 if (folder_item_parent(item
) != NULL
&& item
->prefs
->enable_processing
) {
2205 item
->processing_pending
= TRUE
;
2206 folder_item_apply_processing(item
);
2207 item
->processing_pending
= FALSE
;
2210 STATUSBAR_POP(mainwin
);
2213 static gboolean
draft_all_messages(void)
2215 const GList
*compose_list
= NULL
;
2217 compose_clear_exit_drafts();
2218 compose_list
= compose_get_compose_list();
2219 while (compose_list
!= NULL
) {
2220 Compose
*c
= (Compose
*)compose_list
->data
;
2221 if (!compose_draft(c
, COMPOSE_DRAFT_FOR_EXIT
))
2223 compose_list
= compose_get_compose_list();
2227 gboolean
clean_quit(gpointer data
)
2229 static gboolean firstrun
= TRUE
;
2236 /*!< Good idea to have the main window stored in a
2237 * static variable so we can check that variable
2238 * to see if we're really allowed to do things
2239 * that actually the spawner is supposed to
2240 * do (like: sending mail, composing messages).
2241 * Because, really, if we're the spawnee, and
2242 * we touch GTK stuff, we're hosed. See the
2245 /* FIXME: Use something else to signal that we're
2246 * in the original spawner, and not in a spawned
2248 if (!static_mainwindow
) {
2252 draft_all_messages();
2253 emergency_exit
= TRUE
;
2254 exit_claws(static_mainwindow
);
2260 void app_will_exit(GtkWidget
*widget
, gpointer data
)
2262 MainWindow
*mainwin
= data
;
2264 if (gtk_main_level() == 0) {
2265 debug_print("not even started\n");
2268 if (sc_exiting
== TRUE
) {
2269 debug_print("exit pending\n");
2273 debug_print("exiting\n");
2274 if (compose_get_compose_list()) {
2275 if (!draft_all_messages()) {
2276 main_window_popup(mainwin
);
2282 if (prefs_common
.warn_queued_on_exit
&& procmsg_have_queued_mails_fast()) {
2283 if (alertpanel(_("Queued messages"),
2284 _("Some unsent messages are queued. Exit now?"),
2285 NULL
, _("_Cancel"), NULL
, _("_OK"), NULL
, NULL
,
2287 != G_ALERTALTERNATE
) {
2288 main_window_popup(mainwin
);
2292 manage_window_focus_in(mainwin
->window
, NULL
, NULL
);
2296 #ifdef HAVE_VALGRIND
2297 if (RUNNING_ON_VALGRIND
) {
2298 summary_clear_list(mainwin
->summaryview
);
2301 if (folderview_get_selected_item(mainwin
->folderview
))
2302 folder_item_close(folderview_get_selected_item(mainwin
->folderview
));
2306 gboolean
claws_is_exiting(void)
2311 gboolean
claws_is_starting(void)
2318 * CLAWS: want this public so crash dialog can delete the
2321 gchar
*claws_get_socket_name(void)
2323 static gchar
*filename
= NULL
;
2324 gchar
*socket_dir
= NULL
;
2327 if (filename
== NULL
) {
2331 socket_dir
= g_strdup_printf("%s%cclaws-mail",
2332 g_get_user_runtime_dir(), G_DIR_SEPARATOR
);
2333 stat_ok
= g_stat(socket_dir
, &st
);
2334 if (stat_ok
< 0 && errno
!= ENOENT
) {
2335 g_print("Error stat'ing socket_dir %s: %s\n",
2336 socket_dir
, g_strerror(errno
));
2337 } else if (stat_ok
== 0 && S_ISSOCK(st
.st_mode
)) {
2338 /* old versions used a sock in $TMPDIR/claws-mail-$UID */
2339 debug_print("Using legacy socket %s\n", socket_dir
);
2340 filename
= g_strdup(socket_dir
);
2345 if (!is_dir_exist(socket_dir
) && make_dir(socket_dir
) < 0) {
2346 g_print("Error creating socket_dir %s: %s\n",
2347 socket_dir
, g_strerror(errno
));
2350 md5_hex_digest(md5sum
, get_rc_dir());
2352 filename
= g_strdup_printf("%s%c%s", socket_dir
, G_DIR_SEPARATOR
,
2355 debug_print("Using control socket %s\n", filename
);
2362 static gchar
*get_crashfile_name(void)
2364 static gchar
*filename
= NULL
;
2366 if (filename
== NULL
) {
2367 filename
= g_strdup_printf("%s%cclaws-crashed",
2368 get_tmp_dir(), G_DIR_SEPARATOR
);
2374 static gint
prohibit_duplicate_launch(int *argc
, char ***argv
)
2381 path
= claws_get_socket_name();
2382 /* Try to connect to the control socket */
2383 sock
= fd_connect_unix(path
);
2390 /* If connect failed, no other process is running.
2391 * Unlink the potentially existing socket, then
2392 * open it. This has to be done locking a temporary
2393 * file to avoid the race condition where another
2394 * process could have created the socket just in
2397 socket_lock
= g_strconcat(path
, ".lock",
2399 lock_fd
= g_open(socket_lock
, O_RDWR
|O_CREAT
, 0);
2401 debug_print("Couldn't open %s: %s (%d)\n", socket_lock
,
2402 g_strerror(errno
), errno
);
2403 g_free(socket_lock
);
2406 if (flock(lock_fd
, LOCK_EX
) < 0) {
2407 debug_print("Couldn't lock %s: %s (%d)\n", socket_lock
,
2408 g_strerror(errno
), errno
);
2410 g_free(socket_lock
);
2416 debug_print("Opening socket %s\n", path
);
2417 ret
= fd_open_unix(path
);
2419 flock(lock_fd
, LOCK_UN
);
2421 claws_unlink(socket_lock
);
2422 g_free(socket_lock
);
2429 hmutex
= CreateMutexA(NULL
, FALSE
, "ClawsMail");
2431 debug_print("cannot create Mutex\n");
2434 if (GetLastError() != ERROR_ALREADY_EXISTS
) {
2435 sock
= fd_open_inet(50216);
2441 sock
= fd_connect_inet(50216);
2445 /* remote command mode */
2447 debug_print("another Claws Mail instance is already running.\n");
2449 if (cmd
.receive_all
) {
2450 CM_FD_WRITE_ALL("receive_all\n");
2451 } else if (cmd
.receive
) {
2452 CM_FD_WRITE_ALL("receive\n");
2453 } else if (cmd
.cancel_receiving
) {
2454 CM_FD_WRITE_ALL("cancel_receiving\n");
2455 } else if (cmd
.cancel_sending
) {
2456 CM_FD_WRITE_ALL("cancel_sending\n");
2457 } else if (cmd
.compose
&& cmd
.attach_files
) {
2458 gchar
*str
, *compose_str
;
2460 if (cmd
.compose_mailto
) {
2461 compose_str
= g_strdup_printf("compose_attach %s\n",
2462 cmd
.compose_mailto
);
2464 compose_str
= g_strdup("compose_attach\n");
2467 CM_FD_WRITE_ALL(compose_str
);
2468 g_free(compose_str
);
2470 for (curr
= cmd
.attach_files
; curr
!= NULL
; curr
= curr
->next
) {
2471 str
= (gchar
*) ((AttachInfo
*)curr
->data
)->file
;
2472 if (((AttachInfo
*)curr
->data
)->insert
)
2473 CM_FD_WRITE_ALL("insert ");
2475 CM_FD_WRITE_ALL("attach ");
2476 CM_FD_WRITE_ALL(str
);
2477 CM_FD_WRITE_ALL("\n");
2480 CM_FD_WRITE_ALL(".\n");
2481 } else if (cmd
.compose
) {
2484 if (cmd
.compose_mailto
) {
2485 compose_str
= g_strdup_printf
2486 ("compose %s\n", cmd
.compose_mailto
);
2488 compose_str
= g_strdup("compose\n");
2491 CM_FD_WRITE_ALL(compose_str
);
2492 g_free(compose_str
);
2493 } else if (cmd
.subscribe
) {
2494 gchar
*str
= g_strdup_printf("subscribe %s\n", cmd
.subscribe_uri
);
2495 CM_FD_WRITE_ALL(str
);
2497 } else if (cmd
.send
) {
2498 CM_FD_WRITE_ALL("send\n");
2499 } else if (cmd
.online_mode
== ONLINE_MODE_ONLINE
) {
2500 CM_FD_WRITE("online\n");
2501 } else if (cmd
.online_mode
== ONLINE_MODE_OFFLINE
) {
2502 CM_FD_WRITE("offline\n");
2503 } else if (cmd
.debug
) {
2504 CM_FD_WRITE("debug\n");
2505 } else if (cmd
.status
|| cmd
.status_full
) {
2506 gchar buf
[BUFFSIZE
];
2508 const gchar
*command
;
2512 command
= cmd
.status_full
? "status-full\n" : "status\n";
2513 folders
= cmd
.status_full
? cmd
.status_full_folders
:
2516 CM_FD_WRITE_ALL(command
);
2517 for (i
= 0; folders
&& i
< folders
->len
; ++i
) {
2518 folder
= g_ptr_array_index(folders
, i
);
2519 CM_FD_WRITE_ALL(folder
);
2520 CM_FD_WRITE_ALL("\n");
2522 CM_FD_WRITE_ALL(".\n");
2524 fd_gets(sock
, buf
, sizeof(buf
) - 1);
2525 buf
[sizeof(buf
) - 1] = '\0';
2526 if (!STRNCMP(buf
, ".\n")) break;
2527 if (claws_fputs(buf
, stdout
) == EOF
) {
2528 g_warning("writing to stdout failed");
2532 } else if (cmd
.exit
) {
2533 CM_FD_WRITE_ALL("exit\n");
2534 } else if (cmd
.statistics
) {
2536 CM_FD_WRITE("statistics\n");
2538 fd_gets(sock
, buf
, sizeof(buf
) - 1);
2539 buf
[sizeof(buf
) - 1] = '\0';
2540 if (!STRNCMP(buf
, ".\n")) break;
2541 if (claws_fputs(buf
, stdout
) == EOF
) {
2542 g_warning("writing to stdout failed");
2546 } else if (cmd
.reset_statistics
) {
2547 CM_FD_WRITE("reset_statistics\n");
2548 } else if (cmd
.target
) {
2549 gchar
*str
= g_strdup_printf("select %s\n", cmd
.target
);
2550 CM_FD_WRITE_ALL(str
);
2552 } else if (cmd
.search
) {
2553 gchar buf
[BUFFSIZE
];
2555 g_strdup_printf("search %s\n%s\n%s\n%c\n",
2556 cmd
.search_folder
, cmd
.search_type
, cmd
.search_request
,
2557 (cmd
.search_recursive
==TRUE
)?'1':'0');
2558 CM_FD_WRITE_ALL(str
);
2561 fd_gets(sock
, buf
, sizeof(buf
) - 1);
2562 buf
[sizeof(buf
) - 1] = '\0';
2563 if (!STRNCMP(buf
, ".\n")) break;
2564 if (claws_fputs(buf
, stdout
) == EOF
) {
2565 g_warning("writing to stdout failed");
2572 CM_FD_WRITE_ALL("get_display\n");
2573 memset(buf
, 0, sizeof(buf
));
2574 fd_gets(sock
, buf
, sizeof(buf
) - 1);
2575 buf
[sizeof(buf
) - 1] = '\0';
2577 /* Try to connect to a display; if it is the same one as
2578 * the other Claws instance, then ask it to pop up. */
2579 int diff_display
= 1;
2580 if (gtk_init_check(argc
, argv
)) {
2581 GdkDisplay
*display
= gdk_display_get_default();
2582 diff_display
= g_strcmp0(buf
, gdk_display_get_name(display
));
2585 g_print("Claws Mail is already running on display %s.\n",
2588 g_print("Claws Mail is already running on this display (%s).\n",
2591 sock
= fd_connect_unix(path
);
2592 CM_FD_WRITE_ALL("popup\n");
2595 CM_FD_WRITE_ALL("popup\n");
2603 static gint
lock_socket_remove(void)
2606 gchar
*filename
, *dirname
;
2608 if (lock_socket
< 0) {
2612 if (lock_socket_tag
> 0) {
2613 g_source_remove(lock_socket_tag
);
2615 fd_close(lock_socket
);
2618 filename
= claws_get_socket_name();
2619 dirname
= g_path_get_dirname(filename
);
2620 if (claws_unlink(filename
) < 0)
2621 FILE_OP_ERROR(filename
, "claws_unlink");
2629 static GPtrArray
*get_folder_item_list(gint sock
)
2631 gchar buf
[BUFFSIZE
];
2633 GPtrArray
*folders
= NULL
;
2636 fd_gets(sock
, buf
, sizeof(buf
) - 1);
2637 buf
[sizeof(buf
) - 1] = '\0';
2638 if (!STRNCMP(buf
, ".\n")) {
2643 folders
= g_ptr_array_new();
2645 item
= folder_find_item_from_identifier(buf
);
2647 g_ptr_array_add(folders
, item
);
2649 g_warning("no such folder: %s", buf
);
2656 static void lock_socket_input_cb(gpointer data
,
2658 GIOCondition condition
)
2660 MainWindow
*mainwin
= (MainWindow
*)data
;
2662 gchar buf
[BUFFSIZE
];
2664 sock
= fd_accept(source
);
2668 fd_gets(sock
, buf
, sizeof(buf
) - 1);
2669 buf
[sizeof(buf
) - 1] = '\0';
2671 if (!STRNCMP(buf
, "popup")) {
2672 main_window_popup(mainwin
);
2674 } else if (!STRNCMP(buf
, "get_display")) {
2675 GdkDisplay
* display
= gtk_widget_get_display(mainwin
->window
);
2676 const gchar
*display_name
= gdk_display_get_name(display
);
2677 CM_FD_WRITE_ALL(display_name
);
2679 } else if (!STRNCMP(buf
, "receive_all")) {
2680 inc_all_account_mail(mainwin
, FALSE
, FALSE
,
2681 prefs_common
.newmail_notify_manu
);
2682 } else if (!STRNCMP(buf
, "receive")) {
2683 inc_mail(mainwin
, prefs_common
.newmail_notify_manu
);
2684 } else if (!STRNCMP(buf
, "cancel_receiving")) {
2687 } else if (!STRNCMP(buf
, "cancel_sending")) {
2689 } else if (!STRNCMP(buf
, "compose_attach")) {
2690 GList
*files
= NULL
, *curr
;
2694 mailto
= g_strdup(buf
+ strlen("compose_attach") + 1);
2695 while (fd_gets(sock
, buf
, sizeof(buf
) - 1) > 0) {
2696 buf
[sizeof(buf
) - 1] = '\0';
2698 if (!g_strcmp0(buf
, "."))
2701 ainfo
= g_new0(AttachInfo
, 1);
2702 ainfo
->file
= g_strdup(strstr(buf
, " ") + 1);
2703 ainfo
->insert
= !STRNCMP(buf
, "insert ");
2704 files
= g_list_append(files
, ainfo
);
2706 open_compose_new(mailto
, files
);
2708 curr
= g_list_first(files
);
2709 while (curr
!= NULL
) {
2710 ainfo
= (AttachInfo
*)curr
->data
;
2711 g_free(ainfo
->file
);
2717 } else if (!STRNCMP(buf
, "compose")) {
2718 open_compose_new(buf
+ strlen("compose") + 1, NULL
);
2719 } else if (!STRNCMP(buf
, "subscribe")) {
2720 main_window_popup(mainwin
);
2721 folder_subscribe(buf
+ strlen("subscribe") + 1);
2722 } else if (!STRNCMP(buf
, "send")) {
2724 } else if (!STRNCMP(buf
, "online")) {
2725 main_window_toggle_work_offline(mainwin
, FALSE
, FALSE
);
2726 } else if (!STRNCMP(buf
, "offline")) {
2727 main_window_toggle_work_offline(mainwin
, TRUE
, FALSE
);
2728 } else if (!STRNCMP(buf
, "debug")) {
2729 debug_set_mode(debug_get_mode() ? FALSE
: TRUE
);
2730 } else if (!STRNCMP(buf
, "status-full") ||
2731 !STRNCMP(buf
, "status")) {
2735 folders
= get_folder_item_list(sock
);
2736 status
= folder_get_status
2737 (folders
, !STRNCMP(buf
, "status-full"));
2738 CM_FD_WRITE_ALL(status
);
2739 CM_FD_WRITE_ALL(".\n");
2741 if (folders
) g_ptr_array_free(folders
, TRUE
);
2742 } else if (!STRNCMP(buf
, "statistics")) {
2745 g_snprintf(tmp
, sizeof(tmp
), _("Session statistics\n"));
2746 CM_FD_WRITE_ALL(tmp
);
2748 if (prefs_common
.date_format
) {
2753 lt
= localtime(&session_stats
.time_started
);
2754 fast_strftime(date
, len
, prefs_common
.date_format
, lt
);
2755 g_snprintf(tmp
, sizeof(tmp
), _("Started: %s\n"),
2756 lt
? date
: ctime(&session_stats
.time_started
));
2758 g_snprintf(tmp
, sizeof(tmp
), _("Started: %s\n"),
2759 ctime(&session_stats
.time_started
));
2760 CM_FD_WRITE_ALL(tmp
);
2762 CM_FD_WRITE_ALL("\n");
2764 g_snprintf(tmp
, sizeof(tmp
), _("Incoming traffic\n"));
2765 CM_FD_WRITE_ALL(tmp
);
2767 g_snprintf(tmp
, sizeof(tmp
), _("Received messages: %d\n"),
2768 session_stats
.received
);
2769 CM_FD_WRITE_ALL(tmp
);
2771 if (session_stats
.spam
> 0) {
2772 g_snprintf(tmp
, sizeof(tmp
), _("Spam messages: %d\n"),
2773 session_stats
.spam
);
2774 CM_FD_WRITE_ALL(tmp
);
2777 CM_FD_WRITE_ALL("\n");
2779 g_snprintf(tmp
, sizeof(tmp
), _("Outgoing traffic\n"));
2780 CM_FD_WRITE_ALL(tmp
);
2782 g_snprintf(tmp
, sizeof(tmp
), _("New/redirected messages: %d\n"),
2783 session_stats
.sent
);
2784 CM_FD_WRITE_ALL(tmp
);
2786 g_snprintf(tmp
, sizeof(tmp
), _("Replied messages: %d\n"),
2787 session_stats
.replied
);
2788 CM_FD_WRITE_ALL(tmp
);
2790 g_snprintf(tmp
, sizeof(tmp
), _("Forwarded messages: %d\n"),
2791 session_stats
.forwarded
);
2792 CM_FD_WRITE_ALL(tmp
);
2794 g_snprintf(tmp
, sizeof(tmp
), _("Total outgoing messages: %d\n"),
2795 (session_stats
.sent
+ session_stats
.replied
+
2796 session_stats
.forwarded
));
2797 CM_FD_WRITE_ALL(tmp
);
2799 CM_FD_WRITE_ALL(".\n");
2800 } else if (!STRNCMP(buf
, "reset_statistics")) {
2802 } else if (!STRNCMP(buf
, "select ")) {
2803 const gchar
*target
= buf
+7;
2804 mainwindow_jump_to(target
, TRUE
);
2805 } else if (!STRNCMP(buf
, "search ")) {
2806 FolderItem
* folderItem
= NULL
;
2807 GSList
*messages
= NULL
;
2808 gchar
*folder_name
= NULL
;
2809 gchar
*request
= NULL
;
2810 AdvancedSearch
*search
;
2812 AdvancedSearchType searchType
= ADVANCED_SEARCH_EXTENDED
;
2814 search
= advsearch_new();
2816 folder_name
= g_strdup(buf
+7);
2817 strretchomp(folder_name
);
2819 if (fd_gets(sock
, buf
, sizeof(buf
) - 1) <= 0)
2821 buf
[sizeof(buf
) - 1] = '\0';
2823 switch (toupper(buf
[0])) {
2824 case 'S': searchType
= ADVANCED_SEARCH_SUBJECT
; break;
2825 case 'F': searchType
= ADVANCED_SEARCH_FROM
; break;
2826 case 'T': searchType
= ADVANCED_SEARCH_TO
; break;
2827 case 'M': searchType
= ADVANCED_SEARCH_MIXED
; break;
2828 case 'G': searchType
= ADVANCED_SEARCH_TAG
; break;
2829 case 'E': searchType
= ADVANCED_SEARCH_EXTENDED
; break;
2832 if (fd_gets(sock
, buf
, sizeof(buf
) - 1) <= 0)
2835 buf
[sizeof(buf
) - 1] = '\0';
2836 request
= g_strdup(buf
);
2837 strretchomp(request
);
2840 if (fd_gets(sock
, buf
, sizeof(buf
) - 1) > 0)
2841 recursive
= buf
[0] != '0';
2843 buf
[sizeof(buf
) - 1] = '\0';
2845 debug_print("search: %s %i %s %i\n", folder_name
, searchType
, request
, recursive
);
2847 folderItem
= folder_find_item_from_identifier(folder_name
);
2849 if (folderItem
== NULL
) {
2850 debug_print("Unknown folder item : '%s', searching folder\n",folder_name
);
2851 Folder
* folder
= folder_find_from_path(folder_name
);
2853 folderItem
= FOLDER_ITEM(folder
->node
->data
);
2855 debug_print("Unknown folder: '%s'\n",folder_name
);
2857 debug_print("%s %s\n",folderItem
->name
, folderItem
->path
);
2860 if (folderItem
!= NULL
) {
2861 advsearch_set(search
, searchType
, request
);
2862 advsearch_search_msgs_in_folders(search
, &messages
, folderItem
, recursive
);
2864 g_print("Folder '%s' not found.\n'", folder_name
);
2868 for (cur
= messages
; cur
!= NULL
; cur
= cur
->next
) {
2869 MsgInfo
* msg
= (MsgInfo
*)cur
->data
;
2870 gchar
*file
= procmsg_get_message_file_path(msg
);
2871 CM_FD_WRITE_ALL(file
);
2872 CM_FD_WRITE_ALL("\n");
2875 CM_FD_WRITE_ALL(".\n");
2878 g_free(folder_name
);
2880 advsearch_free(search
);
2881 if (messages
!= NULL
)
2882 procmsg_msg_list_free(messages
);
2883 } else if (!STRNCMP(buf
, "exit")) {
2884 if (prefs_common
.clean_on_exit
&& !prefs_common
.ask_on_clean
) {
2885 procmsg_empty_all_trash();
2887 app_will_exit(NULL
, mainwin
);
2893 static void open_compose_new(const gchar
*address
, GList
*attach_files
)
2898 Xstrdup_a(addr
, address
, return);
2902 compose_new(NULL
, addr
, attach_files
);
2905 static void send_queue(void)
2908 gchar
*errstr
= NULL
;
2909 gboolean error
= FALSE
;
2910 for (list
= folder_get_list(); list
!= NULL
; list
= list
->next
) {
2911 Folder
*folder
= list
->data
;
2913 if (folder
->queue
) {
2914 gint res
= procmsg_send_queue
2915 (folder
->queue
, prefs_common
.savemsg
,
2919 folder_item_scan(folder
->queue
);
2927 alertpanel_error_log(_("Some errors occurred "
2928 "while sending queued messages:\n%s"), errstr
);
2931 alertpanel_error_log("Some errors occurred "
2932 "while sending queued messages.");
2937 static void quit_signal_handler(int sig
)
2939 debug_print("Quitting on signal %d\n", sig
);
2941 g_timeout_add(0, clean_quit
, NULL
);
2945 static void install_basic_sighandlers()
2949 struct sigaction act
;
2954 sigaddset(&mask
, SIGTERM
);
2957 sigaddset(&mask
, SIGINT
);
2960 sigaddset(&mask
, SIGHUP
);
2963 act
.sa_handler
= quit_signal_handler
;
2968 sigaction(SIGTERM
, &act
, 0);
2971 sigaction(SIGINT
, &act
, 0);
2974 sigaction(SIGHUP
, &act
, 0);
2977 sigprocmask(SIG_UNBLOCK
, &mask
, 0);
2978 #endif /* !G_OS_WIN32 */
2981 #ifdef HAVE_NETWORKMANAGER_SUPPORT
2982 static void networkmanager_state_change_cb(DBusGProxy
*proxy
, gchar
*dev
,
2985 MainWindow
*mainWin
;
2988 if (static_mainwindow
)
2989 mainWin
= static_mainwindow
;
2991 mainWin
= (MainWindow
*)data
;
2993 if (!prefs_common
.use_networkmanager
)
2997 GError
*error
= NULL
;
3000 online
= networkmanager_is_online(&error
);
3002 if(online
&& went_offline_nm
) {
3003 went_offline_nm
= FALSE
;
3004 main_window_toggle_work_offline(mainWin
, FALSE
, FALSE
);
3005 debug_print("NetworkManager: Went online\n");
3006 log_message(LOG_PROTOCOL
, _("NetworkManager: network is online.\n"));
3009 went_offline_nm
= TRUE
;
3010 main_window_toggle_work_offline(mainWin
, TRUE
, FALSE
);
3011 debug_print("NetworkManager: Went offline\n");
3012 log_message(LOG_PROTOCOL
, _("NetworkManager: network is offline.\n"));
3016 debug_print("Failed to get online information from NetworkManager: %s\n",
3018 g_error_free(error
);
3022 debug_print("NetworkManager: Cannot change connection state because "
3023 "main window does not exist\n");
3026 /* Returns true (and sets error appropriately, if given) in case of error */
3027 gboolean
networkmanager_is_online(GError
**error
)
3029 DBusGConnection
*connection
;
3031 GError
*tmp_error
= NULL
;
3035 if (!prefs_common
.use_networkmanager
)
3040 connection
= dbus_g_bus_get(DBUS_BUS_SYSTEM
, &tmp_error
);
3043 /* If calling code doesn't do error checking, at least print some debug */
3044 if((error
== NULL
) || (*error
== NULL
))
3045 debug_print("Failed to open connection to system bus: %s\n",
3046 tmp_error
->message
);
3047 g_propagate_error(error
, tmp_error
);
3051 proxy
= dbus_g_proxy_new_for_name(connection
,
3052 "org.freedesktop.NetworkManager",
3053 "/org/freedesktop/NetworkManager",
3054 "org.freedesktop.NetworkManager");
3056 retVal
= dbus_g_proxy_call(proxy
,"state",&tmp_error
, G_TYPE_INVALID
,
3057 G_TYPE_UINT
, &state
, G_TYPE_INVALID
);
3060 g_object_unref(proxy
);
3062 dbus_g_connection_unref(connection
);
3065 /* If calling code doesn't do error checking, at least print some debug */
3066 if((error
== NULL
) || (*error
== NULL
))
3067 debug_print("Failed to get state info from NetworkManager: %s\n",
3068 tmp_error
->message
);
3069 g_propagate_error(error
, tmp_error
);
3072 return (state
== NM_STATE_CONNECTED_LOCAL
||
3073 state
== NM_STATE_CONNECTED_SITE
||
3074 state
== NM_STATE_CONNECTED_GLOBAL
||
3075 state
== NM_STATE_UNKNOWN
);