2007-02-23 [colin] 2.7.2cvs70
[claws.git] / src / main.c
blobdde21c56905eb6159f91b79e666be9a0a7839487
1 /*
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.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
24 #include "defs.h"
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtkrc.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <unistd.h>
36 #include <time.h>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39 #ifdef G_OS_UNIX
40 # include <signal.h>
41 #endif
42 #ifdef HAVE_LIBSM
43 #include <X11/SM/SMlib.h>
44 #include <fcntl.h>
45 #endif
47 #include "wizard.h"
48 #ifdef HAVE_STARTUP_NOTIFICATION
49 # define SN_API_NOT_YET_FROZEN
50 # include <libsn/sn-launchee.h>
51 # include <gdk/gdkx.h>
52 #endif
54 #include "claws.h"
55 #include "main.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"
78 #include "account.h"
79 #include "procmsg.h"
80 #include "inc.h"
81 #include "import.h"
82 #include "manage_window.h"
83 #include "alertpanel.h"
84 #include "statusbar.h"
85 #include "addressbook.h"
86 #include "compose.h"
87 #include "folder.h"
88 #include "setup.h"
89 #include "utils.h"
90 #include "gtkutils.h"
91 #include "socket.h"
92 #include "log.h"
93 #include "prefs_toolbar.h"
94 #include "plugin.h"
95 #include "mh_gtk.h"
96 #include "imap_gtk.h"
97 #include "news_gtk.h"
98 #include "matcher.h"
99 #ifdef HAVE_LIBETPAN
100 #include "imap-thread.h"
101 #endif
102 #include "stock_pixmap.h"
104 #if USE_OPENSSL
105 # include "ssl.h"
106 #endif
108 #include "version.h"
110 #include "crash.h"
112 #include "timing.h"
114 gchar *prog_version;
115 gchar *argv0;
117 #ifdef HAVE_STARTUP_NOTIFICATION
118 static SnLauncheeContext *sn_context = NULL;
119 static SnDisplay *sn_display = NULL;
120 #endif
122 static gint lock_socket = -1;
123 static gint lock_socket_tag = 0;
125 typedef enum
127 ONLINE_MODE_DONT_CHANGE,
128 ONLINE_MODE_ONLINE,
129 ONLINE_MODE_OFFLINE
130 } OnlineMode;
132 static struct RemoteCmd {
133 gboolean receive;
134 gboolean receive_all;
135 gboolean compose;
136 const gchar *compose_mailto;
137 GPtrArray *attach_files;
138 gboolean status;
139 gboolean status_full;
140 GPtrArray *status_folders;
141 GPtrArray *status_full_folders;
142 gboolean send;
143 gboolean crash;
144 int online_mode;
145 gchar *crash_params;
146 gboolean exit;
147 gboolean subscribe;
148 const gchar *subscribe_uri;
149 const gchar *target;
150 } cmd;
152 static void parse_cmd_opt(int argc, char *argv[]);
154 static gint prohibit_duplicate_launch (void);
155 static gchar * get_crashfile_name (void);
156 static gint lock_socket_remove (void);
157 static void lock_socket_input_cb (gpointer data,
158 gint source,
159 GdkInputCondition condition);
161 static void open_compose_new (const gchar *address,
162 GPtrArray *attach_files);
164 static void send_queue (void);
165 static void initial_processing (FolderItem *item, gpointer data);
166 static void quit_signal_handler (int sig);
167 static void install_basic_sighandlers (void);
168 static void exit_claws (MainWindow *mainwin);
170 #define MAKE_DIR_IF_NOT_EXIST(dir) \
172 if (!is_dir_exist(dir)) { \
173 if (is_file_exist(dir)) { \
174 alertpanel_warning \
175 (_("File '%s' already exists.\n" \
176 "Can't create folder."), \
177 dir); \
178 return 1; \
180 if (make_dir(dir) < 0) \
181 return 1; \
185 static MainWindow *static_mainwindow;
186 static gboolean emergency_exit = FALSE;
188 #ifdef HAVE_STARTUP_NOTIFICATION
189 static void sn_error_trap_push(SnDisplay *display, Display *xdisplay)
191 gdk_error_trap_push();
194 static void sn_error_trap_pop(SnDisplay *display, Display *xdisplay)
196 gdk_error_trap_pop();
199 static void startup_notification_complete(gboolean with_window)
201 Display *xdisplay;
202 GtkWidget *hack = NULL;
204 if (with_window) {
205 /* this is needed to make the startup notification leave,
206 * if we have been launched from a menu.
207 * We have to display a window, so let it be very little */
208 hack = gtk_window_new(GTK_WINDOW_POPUP);
209 gtk_widget_set_uposition(hack, 0, 0);
210 gtk_widget_set_size_request(hack, 1, 1);
211 gtk_widget_show(hack);
214 xdisplay = GDK_DISPLAY();
215 sn_display = sn_display_new(xdisplay,
216 sn_error_trap_push,
217 sn_error_trap_pop);
218 sn_context = sn_launchee_context_new_from_environment(sn_display,
219 DefaultScreen(xdisplay));
221 if (sn_context != NULL) {
222 sn_launchee_context_complete(sn_context);
223 sn_launchee_context_unref(sn_context);
224 sn_display_unref(sn_display);
226 if (with_window) {
227 gtk_widget_destroy(hack);
230 #endif /* HAVE_STARTUP_NOTIFICATION */
232 static void claws_gtk_idle(void)
234 while(gtk_events_pending()) {
235 gtk_main_iteration();
237 g_usleep(50000);
240 static gboolean defer_check_all(void *data)
242 gboolean autochk = GPOINTER_TO_INT(data);
244 inc_all_account_mail(static_mainwindow, autochk,
245 prefs_common.newmail_notify_manu);
247 return FALSE;
250 static gboolean defer_check(void *data)
252 inc_mail(static_mainwindow, prefs_common.newmail_notify_manu);
254 return FALSE;
257 static gboolean defer_jump(void *data)
259 mainwindow_jump_to(data);
260 return FALSE;
263 static void chk_update_val(GtkWidget *widget, gpointer data)
265 gboolean *val = (gboolean *)data;
266 *val = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
269 static gboolean migrate_old_config(const gchar *old_cfg_dir, const gchar *new_cfg_dir, const gchar *oldversion)
271 gchar *message = g_strdup_printf(_("Configuration for %s (or previous) found.\n"
272 "Do you want to migrate this configuration?"), oldversion);
273 gint r = 0;
274 GtkWidget *window = NULL;
275 GtkWidget *keep_backup_chk;
276 GtkTooltips *tips = gtk_tooltips_new();
277 gboolean backup = TRUE;
279 keep_backup_chk = gtk_check_button_new_with_label (_("Keep old configuration"));
280 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_backup_chk), TRUE);
281 gtk_tooltips_set_tip(GTK_TOOLTIPS(tips), keep_backup_chk,
282 _("Keeping a backup will allow you to go back to an "
283 "older version, but may take a while if you have "
284 "cached IMAP or News data, and will take some extra "
285 "room on your disk."),
286 NULL);
288 g_signal_connect(G_OBJECT(keep_backup_chk), "toggled",
289 G_CALLBACK(chk_update_val), &backup);
291 if (alertpanel_full(_("Migration of configuration"), message,
292 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL, FALSE,
293 keep_backup_chk, ALERT_QUESTION, G_ALERTDEFAULT) != G_ALERTALTERNATE) {
294 return FALSE;
297 /* we can either do a fast migration requiring not any extra disk
298 * space, or a slow one that copies the old configuration and leaves
299 * it in place. */
300 if (backup) {
301 backup_mode:
302 window = label_window_create(_("Copying configuration... This may take a while..."));
303 GTK_EVENTS_FLUSH();
305 r = copy_dir(old_cfg_dir, new_cfg_dir);
306 gtk_widget_destroy(window);
308 /* if copy failed, we'll remove the partially copied
309 * new directory */
310 if (r != 0) {
311 alertpanel_error(_("Migration failed!"));
312 remove_dir_recursive(new_cfg_dir);
313 } else {
314 if (!backup) {
315 /* fast mode failed, but we don't want backup */
316 remove_dir_recursive(old_cfg_dir);
319 } else {
320 window = label_window_create(_("Migrating configuration..."));
321 GTK_EVENTS_FLUSH();
323 r = g_rename(old_cfg_dir, new_cfg_dir);
324 gtk_widget_destroy(window);
326 /* if g_rename failed, we'll try to copy */
327 if (r != 0) {
328 FILE_OP_ERROR(new_cfg_dir, "g_rename failed, trying copy\n");
329 goto backup_mode;
332 return (r == 0);
335 static void migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
337 FILE *oldfp, *newfp;
338 gchar *plugin_path, *old_plugin_path, *new_plugin_path;
339 gchar buf[BUFFSIZE];
340 oldfp = g_fopen(old_rc, "r");
341 if (!oldfp)
342 return;
343 newfp = g_fopen(new_rc, "w");
344 if (!newfp) {
345 fclose(oldfp);
346 return;
349 plugin_path = g_strdup(get_plugin_dir());
350 new_plugin_path = g_strdup(plugin_path);
352 if (strstr(plugin_path, "/claws-mail/")) {
353 gchar *end = g_strdup(strstr(plugin_path, "/claws-mail/")+strlen("/claws-mail/"));
354 *(strstr(plugin_path, "/claws-mail/")) = '\0';
355 old_plugin_path = g_strconcat(plugin_path, "/sylpheed-claws/", end, NULL);
356 g_free(end);
357 } else {
358 old_plugin_path = g_strdup(new_plugin_path);
360 debug_print("replacing %s with %s\n", old_plugin_path, new_plugin_path);
361 while (fgets(buf, sizeof(buf), oldfp)) {
362 if (strncmp(buf, old_plugin_path, strlen(old_plugin_path))) {
363 fputs(buf, newfp);
364 } else {
365 debug_print("->replacing %s", buf);
366 debug_print(" with %s%s", new_plugin_path, buf+strlen(old_plugin_path));
367 fputs(new_plugin_path, newfp);
368 fputs(buf+strlen(old_plugin_path), newfp);
371 g_free(plugin_path);
372 g_free(new_plugin_path);
373 g_free(old_plugin_path);
374 fclose(oldfp);
375 fclose(newfp);
378 #ifdef HAVE_LIBSM
379 static void
380 sc_client_set_value (MainWindow *mainwin,
381 gchar *name,
382 char *type,
383 int num_vals,
384 SmPropValue *vals)
386 SmProp *proplist[1];
387 SmProp prop;
389 prop.name = name;
390 prop.type = type;
391 prop.num_vals = num_vals;
392 prop.vals = vals;
394 proplist[0]= &prop;
395 if (mainwin->smc_conn)
396 SmcSetProperties ((SmcConn) mainwin->smc_conn, 1, proplist);
399 static void sc_die_callback (SmcConn smc_conn, SmPointer client_data)
401 clean_quit(NULL);
404 static void sc_save_complete_callback(SmcConn smc_conn, SmPointer client_data)
408 static void sc_shutdown_cancelled_callback (SmcConn smc_conn, SmPointer client_data)
410 MainWindow *mainwin = (MainWindow *)client_data;
411 if (mainwin->smc_conn)
412 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
415 static void sc_save_yourself_callback (SmcConn smc_conn,
416 SmPointer client_data,
417 int save_style,
418 gboolean shutdown,
419 int interact_style,
420 gboolean fast) {
422 MainWindow *mainwin = (MainWindow *)client_data;
423 if (mainwin->smc_conn)
424 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
427 static IceIOErrorHandler sc_ice_installed_handler;
429 static void sc_ice_io_error_handler (IceConn connection)
431 if (sc_ice_installed_handler)
432 (*sc_ice_installed_handler) (connection);
434 static gboolean sc_process_ice_messages (GIOChannel *source,
435 GIOCondition condition,
436 gpointer data)
438 IceConn connection = (IceConn) data;
439 IceProcessMessagesStatus status;
441 status = IceProcessMessages (connection, NULL, NULL);
443 if (status == IceProcessMessagesIOError) {
444 IcePointer context = IceGetConnectionContext (connection);
446 if (context && GTK_IS_OBJECT (context)) {
447 guint disconnect_id = g_signal_lookup ("disconnect", G_OBJECT_TYPE (context));
449 if (disconnect_id > 0)
450 g_signal_emit (context, disconnect_id, 0);
451 } else {
452 IceSetShutdownNegotiation (connection, False);
453 IceCloseConnection (connection);
457 return TRUE;
460 static void new_ice_connection (IceConn connection, IcePointer client_data, Bool opening,
461 IcePointer *watch_data)
463 guint input_id;
465 if (opening) {
466 GIOChannel *channel;
467 /* Make sure we don't pass on these file descriptors to any
468 exec'ed children */
469 fcntl(IceConnectionNumber(connection),F_SETFD,
470 fcntl(IceConnectionNumber(connection),F_GETFD,0) | FD_CLOEXEC);
472 channel = g_io_channel_unix_new (IceConnectionNumber (connection));
473 input_id = g_io_add_watch (channel,
474 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
475 sc_process_ice_messages,
476 connection);
477 g_io_channel_unref (channel);
479 *watch_data = (IcePointer) GUINT_TO_POINTER (input_id);
480 } else {
481 input_id = GPOINTER_TO_UINT ((gpointer) *watch_data);
482 g_source_remove (input_id);
486 static void sc_session_manager_connect(MainWindow *mainwin)
488 static gboolean connected = FALSE;
489 SmcCallbacks callbacks;
490 gchar *client_id;
491 IceIOErrorHandler default_handler;
493 if (connected)
494 return;
495 connected = TRUE;
498 sc_ice_installed_handler = IceSetIOErrorHandler (NULL);
499 default_handler = IceSetIOErrorHandler (sc_ice_io_error_handler);
501 if (sc_ice_installed_handler == default_handler)
502 sc_ice_installed_handler = NULL;
504 IceAddConnectionWatch (new_ice_connection, NULL);
507 callbacks.save_yourself.callback = sc_save_yourself_callback;
508 callbacks.die.callback = sc_die_callback;
509 callbacks.save_complete.callback = sc_save_complete_callback;
510 callbacks.shutdown_cancelled.callback = sc_shutdown_cancelled_callback;
512 callbacks.save_yourself.client_data =
513 callbacks.die.client_data =
514 callbacks.save_complete.client_data =
515 callbacks.shutdown_cancelled.client_data = (SmPointer) mainwin;
516 if (g_getenv ("SESSION_MANAGER")) {
517 gchar error_string_ret[256] = "";
519 mainwin->smc_conn = (gpointer)
520 SmcOpenConnection (NULL, mainwin,
521 SmProtoMajor, SmProtoMinor,
522 SmcSaveYourselfProcMask | SmcDieProcMask |
523 SmcSaveCompleteProcMask |
524 SmcShutdownCancelledProcMask,
525 &callbacks,
526 NULL, &client_id,
527 256, error_string_ret);
529 if (error_string_ret[0] || mainwin->smc_conn == NULL)
530 g_warning ("While connecting to session manager:\n%s.",
531 error_string_ret);
532 else {
533 SmPropValue *vals;
534 vals = g_new (SmPropValue, 1);
535 vals[0].length = strlen(argv0);
536 vals[0].value = argv0;
537 sc_client_set_value (mainwin, SmCloneCommand, SmLISTofARRAY8, 1, vals);
538 sc_client_set_value (mainwin, SmRestartCommand, SmLISTofARRAY8, 1, vals);
539 sc_client_set_value (mainwin, SmProgram, SmARRAY8, 1, vals);
541 vals[0].length = strlen(g_get_user_name()?g_get_user_name():"");
542 vals[0].value = g_strdup(g_get_user_name()?g_get_user_name():"");
543 sc_client_set_value (mainwin, SmUserID, SmARRAY8, 1, vals);
547 #endif
549 static gboolean sc_exiting = FALSE;
550 static gboolean sc_starting = FALSE;
551 static gboolean show_at_startup = TRUE;
553 void main_set_show_at_startup(gboolean show)
555 show_at_startup = show;
558 int main(int argc, char *argv[])
560 gchar *userrc;
561 MainWindow *mainwin;
562 FolderView *folderview;
563 GdkPixbuf *icon;
564 gboolean crash_file_present = FALSE;
565 gint num_folder_class = 0;
566 gboolean asked_for_migration = FALSE;
568 START_TIMING("startup");
570 sc_starting = TRUE;
572 if (!claws_init(&argc, &argv)) {
573 return 0;
576 prefs_prepare_cache();
577 prog_version = PROG_VERSION;
578 argv0 = g_strdup(argv[0]);
580 parse_cmd_opt(argc, argv);
582 #ifdef CRASH_DIALOG
583 if (cmd.crash) {
584 gtk_set_locale();
585 gtk_init(&argc, &argv);
586 crash_main(cmd.crash_params);
587 return 0;
589 crash_install_handlers();
590 #endif
591 install_basic_sighandlers();
592 sock_init();
594 /* check and create unix domain socket for remote operation */
595 #ifdef G_OS_UNIX
596 lock_socket = prohibit_duplicate_launch();
597 if (lock_socket < 0) {
598 #ifdef HAVE_STARTUP_NOTIFICATION
599 if(gtk_init_check(&argc, &argv))
600 startup_notification_complete(TRUE);
601 #endif
602 return 0;
605 if (cmd.status || cmd.status_full) {
606 puts("0 Claws Mail not running.");
607 lock_socket_remove();
608 return 0;
611 if (cmd.exit)
612 return 0;
613 #endif
614 if (!g_thread_supported())
615 g_thread_init(NULL);
617 gtk_set_locale();
618 gtk_init(&argc, &argv);
620 gdk_rgb_init();
621 gtk_widget_set_default_colormap(gdk_rgb_get_colormap());
622 gtk_widget_set_default_visual(gdk_rgb_get_visual());
624 if (!g_thread_supported()) {
625 g_error(_("g_thread is not supported by glib.\n"));
628 /* check that we're not on a too recent/old gtk+ */
629 #if GTK_CHECK_VERSION(2, 9, 0)
630 if (gtk_check_version(2, 9, 0) != NULL) {
631 alertpanel_error(_("Claws Mail has been compiled with "
632 "a more recent GTK+ library than is "
633 "currently available. This will cause "
634 "crashes. You need to upgrade GTK+ or "
635 "recompile Claws Mail."));
636 exit(1);
638 #else
639 if (gtk_check_version(2, 9, 0) == NULL) {
640 alertpanel_error(_("Claws Mail has been compiled with "
641 "an older GTK+ library than is "
642 "currently available. This will cause "
643 "crashes. You need to recompile "
644 "Claws Mail."));
645 exit(1);
647 #endif
648 /* parse gtkrc files */
649 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc-2.0",
650 NULL);
651 gtk_rc_parse(userrc);
652 g_free(userrc);
653 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
654 G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
655 gtk_rc_parse(userrc);
656 g_free(userrc);
658 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
660 /* no config dir exists. See if we can migrate an old config. */
661 if (!is_dir_exist(RC_DIR)) {
662 prefs_destroy_cache();
663 gboolean r = FALSE;
665 /* if one of the old dirs exist, we'll ask if the user
666 * want to migrates, and r will be TRUE if he said yes
667 * and migration succeeded, and FALSE otherwise.
669 if (is_dir_exist(OLD_GTK2_RC_DIR)) {
670 r = migrate_old_config(OLD_GTK2_RC_DIR, RC_DIR, "Sylpheed-Claws 2.6.0");
671 asked_for_migration = TRUE;
672 } else if (is_dir_exist(OLDER_GTK2_RC_DIR)) {
673 r = migrate_old_config(OLDER_GTK2_RC_DIR, RC_DIR, "Sylpheed-Claws 1.9.15");
674 asked_for_migration = TRUE;
675 } else if (is_dir_exist(OLD_GTK1_RC_DIR)) {
676 r = migrate_old_config(OLD_GTK1_RC_DIR, RC_DIR, "Sylpheed-Claws 1.0.5");
677 asked_for_migration = TRUE;
680 /* If migration failed or the user didn't want to do it,
681 * we create a new one (and we'll hit wizard later).
683 if (r == FALSE && !is_dir_exist(RC_DIR) && make_dir(RC_DIR) < 0)
684 exit(1);
688 if (!is_file_exist(RC_DIR G_DIR_SEPARATOR_S COMMON_RC) &&
689 is_file_exist(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC)) {
690 /* post 2.6 name change */
691 migrate_common_rc(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC,
692 RC_DIR G_DIR_SEPARATOR_S COMMON_RC);
695 if (!cmd.exit)
696 plugin_load_all("Common");
698 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
699 gtk_rc_parse(userrc);
700 g_free(userrc);
702 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
703 gtk_accel_map_load (userrc);
704 g_free(userrc);
706 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
708 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
709 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
710 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
711 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
712 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
713 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR);
715 crash_file_present = is_file_exist(get_crashfile_name());
716 /* remove temporary files */
717 remove_all_files(get_tmp_dir());
718 remove_all_files(get_mime_tmp_dir());
720 if (is_file_exist("claws.log")) {
721 if (rename_force("claws.log", "claws.log.bak") < 0)
722 FILE_OP_ERROR("claws.log", "rename");
724 set_log_file("claws.log");
726 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
728 folder_system_init();
729 prefs_common_read_config();
731 prefs_themes_init();
732 prefs_fonts_init();
733 prefs_ext_prog_init();
734 prefs_wrapping_init();
735 prefs_compose_writing_init();
736 prefs_msg_colors_init();
737 image_viewer_init();
738 prefs_image_viewer_init();
739 prefs_quote_init();
740 prefs_summaries_init();
741 prefs_message_init();
742 prefs_other_init();
743 prefs_receive_init();
744 prefs_send_init();
745 #ifdef USE_ASPELL
746 gtkaspell_checkers_init();
747 prefs_spelling_init();
748 #endif
750 sock_set_io_timeout(prefs_common.io_timeout_secs);
751 #ifdef HAVE_LIBETPAN
752 imap_main_set_timeout(prefs_common.io_timeout_secs);
753 #endif
754 prefs_actions_read_config();
755 prefs_display_header_read_config();
756 /* prefs_filtering_read_config(); */
757 addressbook_read_file();
758 renderer_read_config();
760 gtkut_widget_init();
761 stock_pixbuf_gdk(NULL, STOCK_PIXMAP_CLAWS_MAIL_ICON, &icon);
762 gtk_window_set_default_icon(icon);
764 folderview_initialize();
766 mh_gtk_init();
767 imap_gtk_init();
768 news_gtk_init();
770 mainwin = main_window_create();
772 manage_window_focus_in(mainwin->window, NULL, NULL);
773 folderview = mainwin->folderview;
775 gtk_clist_freeze(GTK_CLIST(mainwin->folderview->ctree));
776 folder_item_update_freeze();
778 /* register the callback of unix domain socket input */
779 #ifdef G_OS_UNIX
780 lock_socket_tag = gdk_input_add(lock_socket,
781 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
782 lock_socket_input_cb,
783 mainwin);
784 #endif
786 prefs_account_init();
787 account_read_config_all();
789 /* If we can't read a folder list or don't have accounts,
790 * it means the configuration's not done. Either this is
791 * a brand new install, either a failed/refused migration.
792 * So we'll start the wizard.
794 if (folder_read_list() < 0) {
795 prefs_destroy_cache();
797 /* if run_wizard returns FALSE it's because it's
798 * been cancelled. We can't do much but exit.
799 * however, if the user was asked for a migration,
800 * we remove the newly created directory so that
801 * he's asked again for migration on next launch.*/
802 if (!run_wizard(mainwin, TRUE)) {
803 if (asked_for_migration)
804 remove_dir_recursive(RC_DIR);
805 exit(1);
807 main_window_reflect_prefs_all_now();
808 folder_write_list();
811 if (!account_get_list()) {
812 prefs_destroy_cache();
813 if (!run_wizard(mainwin, FALSE)) {
814 if (asked_for_migration)
815 remove_dir_recursive(RC_DIR);
816 exit(1);
818 account_read_config_all();
819 if(!account_get_list()) {
820 exit_claws(mainwin);
821 exit(1);
826 toolbar_main_set_sensitive(mainwin);
827 main_window_set_menu_sensitive(mainwin);
829 /* if crashed, show window early so that the user
830 * sees what's happening */
831 if (!cmd.crash && crash_file_present)
832 main_window_popup(mainwin);
834 #ifdef HAVE_LIBETPAN
835 imap_main_init(prefs_common.skip_ssl_cert_check);
836 #endif
837 account_set_missing_folder();
838 folder_set_missing_folders();
839 folderview_set(folderview);
841 prefs_matcher_read_config();
843 /* make one all-folder processing before using claws */
844 main_window_cursor_wait(mainwin);
845 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
847 /* if claws crashed, rebuild caches */
848 if (!cmd.crash && crash_file_present) {
849 GTK_EVENTS_FLUSH();
850 debug_print("Claws Mail crashed, checking for new messages in local folders\n");
851 folder_item_update_thaw();
852 folderview_check_new(NULL);
853 folder_clean_cache_memory_force();
854 folder_item_update_freeze();
856 /* make the crash-indicator file */
857 str_write_to_file("foo", get_crashfile_name());
859 inc_autocheck_timer_init(mainwin);
861 /* ignore SIGPIPE signal for preventing sudden death of program */
862 #ifdef G_OS_UNIX
863 signal(SIGPIPE, SIG_IGN);
864 #endif
865 if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
866 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
868 if (cmd.online_mode == ONLINE_MODE_ONLINE) {
869 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
872 if (cmd.status_folders) {
873 g_ptr_array_free(cmd.status_folders, TRUE);
874 cmd.status_folders = NULL;
876 if (cmd.status_full_folders) {
877 g_ptr_array_free(cmd.status_full_folders, TRUE);
878 cmd.status_full_folders = NULL;
881 claws_register_idle_function(claws_gtk_idle);
883 prefs_toolbar_init();
885 num_folder_class = g_list_length(folder_get_list());
887 plugin_load_all("GTK2");
889 if (g_list_length(folder_get_list()) != num_folder_class) {
890 debug_print("new folders loaded, reloading processing rules\n");
891 prefs_matcher_read_config();
894 if (plugin_get_unloaded_list() != NULL) {
895 main_window_cursor_normal(mainwin);
896 alertpanel_warning(_("Some plugin(s) failed to load. "
897 "Check the Plugins configuration "
898 "for more information."));
899 main_window_cursor_wait(mainwin);
902 plugin_load_standard_plugins ();
904 /* if not crashed, show window now */
905 if (!(!cmd.crash && crash_file_present)) {
906 /* apart if something told not to show */
907 if (show_at_startup)
908 main_window_popup(mainwin);
911 if (!folder_have_mailbox()) {
912 prefs_destroy_cache();
913 main_window_cursor_normal(mainwin);
914 if (folder_get_list() != NULL) {
915 alertpanel_error(_("Claws Mail has detected a configured "
916 "mailbox, but is it incomplete. It is "
917 "possibly due to a failing IMAP account. Use "
918 "\"Rebuild folder tree\" on the mailbox's folder "
919 "to try to fix it."));
920 } else {
921 alertpanel_error(_("Claws Mail has detected a configured "
922 "mailbox, but could not load it. It is "
923 "probably provided by an out-of-date "
924 "external plugin. Please reinstall the "
925 "plugin and try again."));
926 exit_claws(mainwin);
927 exit(1);
931 static_mainwindow = mainwin;
933 #ifdef HAVE_STARTUP_NOTIFICATION
934 startup_notification_complete(FALSE);
935 #endif
936 #ifdef HAVE_LIBSM
937 sc_session_manager_connect(mainwin);
938 #endif
939 folder_item_update_thaw();
940 gtk_clist_thaw(GTK_CLIST(mainwin->folderview->ctree));
941 main_window_cursor_normal(mainwin);
943 if (cmd.receive_all) {
944 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(FALSE));
945 } else if (prefs_common.chk_on_startup) {
946 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(TRUE));
947 } else if (cmd.receive) {
948 g_timeout_add(1000, defer_check, NULL);
949 } else {
950 gtk_widget_grab_focus(folderview->ctree);
953 if (cmd.compose) {
954 open_compose_new(cmd.compose_mailto, cmd.attach_files);
956 if (cmd.attach_files) {
957 ptr_array_free_strings(cmd.attach_files);
958 g_ptr_array_free(cmd.attach_files, TRUE);
959 cmd.attach_files = NULL;
961 if (cmd.subscribe) {
962 folder_subscribe(cmd.subscribe_uri);
965 if (cmd.send) {
966 send_queue();
969 if (cmd.target) {
970 g_timeout_add(500, defer_jump, (gpointer)cmd.target);
973 prefs_destroy_cache();
975 compose_reopen_exit_drafts();
977 sc_starting = FALSE;
978 END_TIMING();
980 gtk_main();
982 exit_claws(mainwin);
984 return 0;
987 static void save_all_caches(FolderItem *item, gpointer data)
989 if (!item->cache) {
990 return;
993 if (item->opened)
994 folder_item_close(item);
996 folder_item_free_cache(item, TRUE);
999 static void exit_claws(MainWindow *mainwin)
1001 gchar *filename;
1003 sc_exiting = TRUE;
1005 debug_print("shutting down\n");
1006 inc_autocheck_timer_remove();
1008 if (prefs_common.clean_on_exit && !emergency_exit) {
1009 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
1012 /* save prefs for opened folder */
1013 if(mainwin->folderview->opened) {
1014 FolderItem *item;
1016 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
1017 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
1020 /* save all state before exiting */
1021 folder_func_to_all_folders(save_all_caches, NULL);
1022 folder_write_list();
1024 main_window_get_size(mainwin);
1025 main_window_get_position(mainwin);
1027 prefs_common_write_config();
1028 account_write_config_all();
1029 addressbook_export_to_file();
1031 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
1032 gtk_accel_map_save(filename);
1033 g_free(filename);
1035 /* delete temporary files */
1036 remove_all_files(get_tmp_dir());
1037 remove_all_files(get_mime_tmp_dir());
1039 close_log_file();
1041 #ifdef HAVE_LIBETPAN
1042 imap_main_done();
1043 #endif
1044 /* delete crashfile */
1045 if (!cmd.crash)
1046 g_unlink(get_crashfile_name());
1048 lock_socket_remove();
1050 #ifdef HAVE_LIBSM
1051 if (mainwin->smc_conn)
1052 SmcCloseConnection ((SmcConn)mainwin->smc_conn, 0, NULL);
1053 mainwin->smc_conn = NULL;
1054 #endif
1056 main_window_destroy_all();
1058 plugin_unload_all("GTK2");
1060 prefs_toolbar_done();
1062 addressbook_destroy();
1064 prefs_themes_done();
1065 prefs_fonts_done();
1066 prefs_ext_prog_done();
1067 prefs_wrapping_done();
1068 prefs_compose_writing_done();
1069 prefs_msg_colors_done();
1070 prefs_image_viewer_done();
1071 image_viewer_done();
1072 prefs_quote_done();
1073 prefs_summaries_done();
1074 prefs_message_done();
1075 prefs_other_done();
1076 prefs_receive_done();
1077 prefs_send_done();
1078 #ifdef USE_ASPELL
1079 prefs_spelling_done();
1080 gtkaspell_checkers_quit();
1081 #endif
1082 plugin_unload_all("Common");
1083 claws_done();
1086 static void parse_cmd_opt(int argc, char *argv[])
1088 gint i;
1090 for (i = 1; i < argc; i++) {
1091 if (!strncmp(argv[i], "--receive-all", 13)) {
1092 cmd.receive_all = TRUE;
1093 } else if (!strncmp(argv[i], "--receive", 9)) {
1094 cmd.receive = TRUE;
1095 } else if (!strncmp(argv[i], "--compose", 9)) {
1096 const gchar *p = argv[i + 1];
1098 cmd.compose = TRUE;
1099 cmd.compose_mailto = NULL;
1100 if (p && *p != '\0' && *p != '-') {
1101 if (!strncmp(p, "mailto:", 7)) {
1102 cmd.compose_mailto = p + 7;
1103 } else {
1104 cmd.compose_mailto = p;
1106 i++;
1108 } else if (!strncmp(argv[i], "--subscribe", 11)) {
1109 const gchar *p = argv[i + 1];
1110 if (p && *p != '\0' && *p != '-') {
1111 cmd.subscribe = TRUE;
1112 cmd.subscribe_uri = p;
1114 } else if (!strncmp(argv[i], "--attach", 8)) {
1115 const gchar *p = argv[i + 1];
1116 gchar *file = NULL;
1118 while (p && *p != '\0' && *p != '-') {
1119 if (!cmd.attach_files) {
1120 cmd.attach_files = g_ptr_array_new();
1122 if ((file = g_filename_from_uri(p, NULL, NULL)) != NULL) {
1123 if (!is_file_exist(file)) {
1124 g_free(file);
1125 file = NULL;
1128 if (file == NULL && *p != G_DIR_SEPARATOR) {
1129 file = g_strconcat(claws_get_startup_dir(),
1130 G_DIR_SEPARATOR_S,
1131 p, NULL);
1132 } else if (file == NULL) {
1133 file = g_strdup(p);
1135 g_ptr_array_add(cmd.attach_files, file);
1136 i++;
1137 p = argv[i + 1];
1139 } else if (!strncmp(argv[i], "--send", 6)) {
1140 cmd.send = TRUE;
1141 } else if (!strncmp(argv[i], "--version", 9) ||
1142 !strncmp(argv[i], "-v", 2)) {
1143 puts("Claws Mail version " VERSION);
1144 exit(0);
1145 } else if (!strncmp(argv[i], "--status-full", 13)) {
1146 const gchar *p = argv[i + 1];
1148 cmd.status_full = TRUE;
1149 while (p && *p != '\0' && *p != '-') {
1150 if (!cmd.status_full_folders) {
1151 cmd.status_full_folders =
1152 g_ptr_array_new();
1154 g_ptr_array_add(cmd.status_full_folders,
1155 g_strdup(p));
1156 i++;
1157 p = argv[i + 1];
1159 } else if (!strncmp(argv[i], "--status", 8)) {
1160 const gchar *p = argv[i + 1];
1162 cmd.status = TRUE;
1163 while (p && *p != '\0' && *p != '-') {
1164 if (!cmd.status_folders)
1165 cmd.status_folders = g_ptr_array_new();
1166 g_ptr_array_add(cmd.status_folders,
1167 g_strdup(p));
1168 i++;
1169 p = argv[i + 1];
1171 } else if (!strncmp(argv[i], "--online", 8)) {
1172 cmd.online_mode = ONLINE_MODE_ONLINE;
1173 } else if (!strncmp(argv[i], "--offline", 9)) {
1174 cmd.online_mode = ONLINE_MODE_OFFLINE;
1175 } else if (!strncmp(argv[i], "--help", 6) ||
1176 !strncmp(argv[i], "-h", 2)) {
1177 gchar *base = g_path_get_basename(argv[0]);
1178 g_print(_("Usage: %s [OPTION]...\n"), base);
1180 g_print("%s\n", _(" --compose [address] open composition window"));
1181 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
1182 g_print("%s\n", _(" --attach file1 [file2]...\n"
1183 " open composition window with specified files\n"
1184 " attached"));
1185 g_print("%s\n", _(" --receive receive new messages"));
1186 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
1187 g_print("%s\n", _(" --send send all queued messages"));
1188 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
1189 g_print("%s\n", _(" --status-full [folder]...\n"
1190 " show the status of each folder"));
1191 g_print("%s\n", _(" --select folder[/msg] jumps to the specified folder/message\n"
1192 " folder is a folder id like 'folder/sub_folder'"));
1193 g_print("%s\n", _(" --online switch to online mode"));
1194 g_print("%s\n", _(" --offline switch to offline mode"));
1195 g_print("%s\n", _(" --exit --quit -q exit Claws Mail"));
1196 g_print("%s\n", _(" --debug debug mode"));
1197 g_print("%s\n", _(" --help -h display this help and exit"));
1198 g_print("%s\n", _(" --version -v output version information and exit"));
1199 g_print("%s\n", _(" --config-dir output configuration directory"));
1201 g_free(base);
1202 exit(1);
1203 } else if (!strncmp(argv[i], "--crash", 7)) {
1204 cmd.crash = TRUE;
1205 cmd.crash_params = g_strdup(argv[i + 1]);
1206 i++;
1207 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
1208 puts(RC_DIR);
1209 exit(0);
1210 } else if (!strncmp(argv[i], "--exit", 6) ||
1211 !strncmp(argv[i], "--quit", 6) ||
1212 !strncmp(argv[i], "-q", 2)) {
1213 cmd.exit = TRUE;
1214 } else if (!strncmp(argv[i], "--select", 8) && i+1 < argc) {
1215 cmd.target = argv[i+1];
1216 } else if (i == 1 && argc == 2) {
1217 /* only one parameter. Do something intelligent about it */
1218 if (strstr(argv[i], "@") && !strstr(argv[i], "://")) {
1219 const gchar *p = argv[i];
1221 cmd.compose = TRUE;
1222 cmd.compose_mailto = NULL;
1223 if (p && *p != '\0' && *p != '-') {
1224 if (!strncmp(p, "mailto:", 7)) {
1225 cmd.compose_mailto = p + 7;
1226 } else {
1227 cmd.compose_mailto = p;
1230 } else if (strstr(argv[i], "://")) {
1231 const gchar *p = argv[i];
1232 if (p && *p != '\0' && *p != '-') {
1233 cmd.subscribe = TRUE;
1234 cmd.subscribe_uri = p;
1236 } else if (!strcmp(argv[i], "--sync")) {
1237 /* gtk debug */
1238 } else {
1239 g_print(_("Unknown option\n"));
1240 exit(1);
1245 if (cmd.attach_files && cmd.compose == FALSE) {
1246 cmd.compose = TRUE;
1247 cmd.compose_mailto = NULL;
1251 static void initial_processing(FolderItem *item, gpointer data)
1253 MainWindow *mainwin = (MainWindow *)data;
1254 gchar *buf;
1256 g_return_if_fail(item);
1257 buf = g_strdup_printf(_("Processing (%s)..."),
1258 item->path
1259 ? item->path
1260 : _("top level folder"));
1261 g_free(buf);
1264 if (item->prefs->enable_processing) {
1265 folder_item_apply_processing(item);
1268 STATUSBAR_POP(mainwin);
1271 static void draft_all_messages(void)
1273 GList *compose_list = NULL;
1275 compose_clear_exit_drafts();
1276 while ((compose_list = compose_get_compose_list()) != NULL) {
1277 Compose *c = (Compose*)compose_list->data;
1278 compose_draft(c, COMPOSE_DRAFT_FOR_EXIT);
1281 gboolean clean_quit(gpointer data)
1283 static gboolean firstrun = TRUE;
1285 if (!firstrun) {
1286 return FALSE;
1288 firstrun = FALSE;
1290 /*!< Good idea to have the main window stored in a
1291 * static variable so we can check that variable
1292 * to see if we're really allowed to do things
1293 * that actually the spawner is supposed to
1294 * do (like: sending mail, composing messages).
1295 * Because, really, if we're the spawnee, and
1296 * we touch GTK stuff, we're hosed. See the
1297 * next fixme. */
1299 /* FIXME: Use something else to signal that we're
1300 * in the original spawner, and not in a spawned
1301 * child. */
1302 if (!static_mainwindow) {
1303 return FALSE;
1306 draft_all_messages();
1307 emergency_exit = TRUE;
1308 exit_claws(static_mainwindow);
1309 exit(0);
1311 return FALSE;
1314 void app_will_exit(GtkWidget *widget, gpointer data)
1316 MainWindow *mainwin = data;
1318 if (sc_exiting == TRUE) {
1319 debug_print("exit pending\n");
1320 return;
1322 sc_exiting = TRUE;
1323 debug_print("exiting\n");
1324 if (compose_get_compose_list()) {
1325 draft_all_messages();
1328 if (prefs_common.warn_queued_on_exit && procmsg_have_queued_mails_fast()) {
1329 if (alertpanel(_("Queued messages"),
1330 _("Some unsent messages are queued. Exit now?"),
1331 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL)
1332 != G_ALERTALTERNATE) {
1333 main_window_popup(mainwin);
1334 sc_exiting = FALSE;
1335 return;
1337 manage_window_focus_in(mainwin->window, NULL, NULL);
1340 sock_cleanup();
1341 summary_clear_list(mainwin->summaryview);
1342 if (folderview_get_selected_item(mainwin->folderview))
1343 folder_item_close(folderview_get_selected_item(mainwin->folderview));
1344 gtk_main_quit();
1347 gboolean claws_is_exiting(void)
1349 return sc_exiting;
1352 gboolean claws_is_starting(void)
1354 return sc_starting;
1358 * CLAWS: want this public so crash dialog can delete the
1359 * lock file too
1361 gchar *claws_get_socket_name(void)
1363 static gchar *filename = NULL;
1365 if (filename == NULL) {
1366 filename = g_strdup_printf("%s%cclaws-mail-%d",
1367 g_get_tmp_dir(), G_DIR_SEPARATOR,
1368 #if HAVE_GETUID
1369 getuid());
1370 #else
1371 0);
1372 #endif
1375 return filename;
1378 static gchar *get_crashfile_name(void)
1380 static gchar *filename = NULL;
1382 if (filename == NULL) {
1383 filename = g_strdup_printf("%s%cclaws-crashed",
1384 get_tmp_dir(), G_DIR_SEPARATOR);
1387 return filename;
1390 static gint prohibit_duplicate_launch(void)
1392 gint uxsock;
1393 gchar *path;
1395 path = claws_get_socket_name();
1396 uxsock = fd_connect_unix(path);
1397 if (uxsock < 0) {
1398 g_unlink(path);
1399 return fd_open_unix(path);
1402 /* remote command mode */
1404 debug_print("another Claws Mail instance is already running.\n");
1406 if (cmd.receive_all) {
1407 fd_write_all(uxsock, "receive_all\n", 12);
1408 } else if (cmd.receive) {
1409 fd_write_all(uxsock, "receive\n", 8);
1410 } else if (cmd.compose && cmd.attach_files) {
1411 gchar *str, *compose_str;
1412 gint i;
1414 if (cmd.compose_mailto) {
1415 compose_str = g_strdup_printf("compose_attach %s\n",
1416 cmd.compose_mailto);
1417 } else {
1418 compose_str = g_strdup("compose_attach\n");
1421 fd_write_all(uxsock, compose_str, strlen(compose_str));
1422 g_free(compose_str);
1424 for (i = 0; i < cmd.attach_files->len; i++) {
1425 str = g_ptr_array_index(cmd.attach_files, i);
1426 fd_write_all(uxsock, str, strlen(str));
1427 fd_write_all(uxsock, "\n", 1);
1430 fd_write_all(uxsock, ".\n", 2);
1431 } else if (cmd.compose) {
1432 gchar *compose_str;
1434 if (cmd.compose_mailto) {
1435 compose_str = g_strdup_printf
1436 ("compose %s\n", cmd.compose_mailto);
1437 } else {
1438 compose_str = g_strdup("compose\n");
1441 fd_write_all(uxsock, compose_str, strlen(compose_str));
1442 g_free(compose_str);
1443 } else if (cmd.subscribe) {
1444 gchar *str = g_strdup_printf("subscribe %s\n", cmd.subscribe_uri);
1445 fd_write_all(uxsock, str, strlen(str));
1446 g_free(str);
1447 } else if (cmd.send) {
1448 fd_write_all(uxsock, "send\n", 5);
1449 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
1450 fd_write(uxsock, "online\n", 6);
1451 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
1452 fd_write(uxsock, "offline\n", 7);
1453 } else if (cmd.status || cmd.status_full) {
1454 gchar buf[BUFFSIZE];
1455 gint i;
1456 const gchar *command;
1457 GPtrArray *folders;
1458 gchar *folder;
1460 command = cmd.status_full ? "status-full\n" : "status\n";
1461 folders = cmd.status_full ? cmd.status_full_folders :
1462 cmd.status_folders;
1464 fd_write_all(uxsock, command, strlen(command));
1465 for (i = 0; folders && i < folders->len; ++i) {
1466 folder = g_ptr_array_index(folders, i);
1467 fd_write_all(uxsock, folder, strlen(folder));
1468 fd_write_all(uxsock, "\n", 1);
1470 fd_write_all(uxsock, ".\n", 2);
1471 for (;;) {
1472 fd_gets(uxsock, buf, sizeof(buf));
1473 if (!strncmp(buf, ".\n", 2)) break;
1474 fputs(buf, stdout);
1476 } else if (cmd.exit) {
1477 fd_write_all(uxsock, "exit\n", 5);
1478 } else if (cmd.target) {
1479 gchar *str = g_strdup_printf("select %s\n", cmd.target);
1480 fd_write_all(uxsock, str, strlen(str));
1481 g_free(str);
1482 } else
1483 fd_write_all(uxsock, "popup\n", 6);
1485 fd_close(uxsock);
1486 return -1;
1489 static gint lock_socket_remove(void)
1491 #ifdef G_OS_UNIX
1492 gchar *filename;
1494 if (lock_socket < 0) {
1495 return -1;
1498 if (lock_socket_tag > 0) {
1499 gdk_input_remove(lock_socket_tag);
1501 fd_close(lock_socket);
1502 filename = claws_get_socket_name();
1503 g_unlink(filename);
1504 #endif
1506 return 0;
1509 static GPtrArray *get_folder_item_list(gint sock)
1511 gchar buf[BUFFSIZE];
1512 FolderItem *item;
1513 GPtrArray *folders = NULL;
1515 for (;;) {
1516 fd_gets(sock, buf, sizeof(buf));
1517 if (!strncmp(buf, ".\n", 2)) {
1518 break;
1520 strretchomp(buf);
1521 if (!folders) {
1522 folders = g_ptr_array_new();
1524 item = folder_find_item_from_identifier(buf);
1525 if (item) {
1526 g_ptr_array_add(folders, item);
1527 } else {
1528 g_warning("no such folder: %s\n", buf);
1532 return folders;
1535 static void lock_socket_input_cb(gpointer data,
1536 gint source,
1537 GdkInputCondition condition)
1539 MainWindow *mainwin = (MainWindow *)data;
1540 gint sock;
1541 gchar buf[BUFFSIZE];
1543 sock = fd_accept(source);
1544 fd_gets(sock, buf, sizeof(buf));
1546 if (!strncmp(buf, "popup", 5)) {
1547 main_window_popup(mainwin);
1548 } else if (!strncmp(buf, "receive_all", 11)) {
1549 inc_all_account_mail(mainwin, FALSE,
1550 prefs_common.newmail_notify_manu);
1551 } else if (!strncmp(buf, "receive", 7)) {
1552 inc_mail(mainwin, prefs_common.newmail_notify_manu);
1553 } else if (!strncmp(buf, "compose_attach", 14)) {
1554 GPtrArray *files;
1555 gchar *mailto;
1557 mailto = g_strdup(buf + strlen("compose_attach") + 1);
1558 files = g_ptr_array_new();
1559 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
1560 if (buf[0] == '.' && buf[1] == '\n') {
1561 break;
1563 strretchomp(buf);
1564 g_ptr_array_add(files, g_strdup(buf));
1566 open_compose_new(mailto, files);
1567 ptr_array_free_strings(files);
1568 g_ptr_array_free(files, TRUE);
1569 g_free(mailto);
1570 } else if (!strncmp(buf, "compose", 7)) {
1571 open_compose_new(buf + strlen("compose") + 1, NULL);
1572 } else if (!strncmp(buf, "subscribe", 9)) {
1573 main_window_popup(mainwin);
1574 folder_subscribe(buf + strlen("subscribe") + 1);
1575 } else if (!strncmp(buf, "send", 4)) {
1576 send_queue();
1577 } else if (!strncmp(buf, "online", 6)) {
1578 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1579 } else if (!strncmp(buf, "offline", 7)) {
1580 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1581 } else if (!strncmp(buf, "status-full", 11) ||
1582 !strncmp(buf, "status", 6)) {
1583 gchar *status;
1584 GPtrArray *folders;
1586 folders = get_folder_item_list(sock);
1587 status = folder_get_status
1588 (folders, !strncmp(buf, "status-full", 11));
1589 fd_write_all(sock, status, strlen(status));
1590 fd_write_all(sock, ".\n", 2);
1591 g_free(status);
1592 if (folders) g_ptr_array_free(folders, TRUE);
1593 } else if (!strncmp(buf, "select ", 7)) {
1594 const gchar *target = buf+7;
1595 mainwindow_jump_to(target);
1596 } else if (!strncmp(buf, "exit", 4)) {
1597 app_will_exit(NULL, mainwin);
1600 fd_close(sock);
1603 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
1605 gchar *addr = NULL;
1607 if (address) {
1608 Xstrdup_a(addr, address, return);
1609 g_strstrip(addr);
1612 compose_new(NULL, addr, attach_files);
1615 static void send_queue(void)
1617 GList *list;
1618 gchar *errstr = NULL;
1619 for (list = folder_get_list(); list != NULL; list = list->next) {
1620 Folder *folder = list->data;
1622 if (folder->queue) {
1623 gint res = procmsg_send_queue
1624 (folder->queue, prefs_common.savemsg,
1625 &errstr);
1627 if (res) {
1628 folder_item_scan(folder->queue);
1632 if (errstr) {
1633 gchar *tmp = g_strdup_printf(_("Some errors occurred "
1634 "while sending queued messages:\n%s"), errstr);
1635 g_free(errstr);
1636 alertpanel_error_log(tmp);
1637 g_free(tmp);
1638 } else {
1639 alertpanel_error_log("Some errors occurred "
1640 "while sending queued messages.");
1644 static void quit_signal_handler(int sig)
1646 debug_print("Quitting on signal %d\n", sig);
1648 g_timeout_add(0, clean_quit, NULL);
1651 static void install_basic_sighandlers()
1653 #ifndef G_OS_WIN32
1654 sigset_t mask;
1655 struct sigaction act;
1657 sigemptyset(&mask);
1659 #ifdef SIGTERM
1660 sigaddset(&mask, SIGTERM);
1661 #endif
1662 #ifdef SIGINT
1663 sigaddset(&mask, SIGINT);
1664 #endif
1665 #ifdef SIGHUP
1666 sigaddset(&mask, SIGHUP);
1667 #endif
1669 act.sa_handler = quit_signal_handler;
1670 act.sa_mask = mask;
1671 act.sa_flags = 0;
1673 #ifdef SIGTERM
1674 sigaction(SIGTERM, &act, 0);
1675 #endif
1676 #ifdef SIGINT
1677 sigaction(SIGINT, &act, 0);
1678 #endif
1679 #ifdef SIGHUP
1680 sigaction(SIGHUP, &act, 0);
1681 #endif
1683 sigprocmask(SIG_UNBLOCK, &mask, 0);
1684 #endif /* !G_OS_WIN32 */