Fix help about command-line arguments that do require a parameter.
[claws.git] / src / main.c
index 8a0ce235f950eb41e28730216d3fc96dad62fdb4..8e2f2948ca8643ed6f4ee8ee391176edff59d557 100644 (file)
 #include "advsearch.h"
 #include "avatars.h"
 #include "passwordstore.h"
-#include "safe_fclose.h"
+#include "file-utils.h"
 
 #ifdef HAVE_LIBETPAN
 #include "imap-thread.h"
@@ -171,7 +171,9 @@ static SnDisplay *sn_display = NULL;
 
 static gint lock_socket = -1;
 static gint lock_socket_tag = 0;
+#ifdef G_OS_UNIX
 static gchar *x_display = NULL;
+#endif
 typedef enum 
 {
        ONLINE_MODE_DONT_CHANGE,
@@ -317,10 +319,14 @@ static gboolean defer_check_all(void *data)
 {
        gboolean autochk = GPOINTER_TO_INT(data);
 
-       inc_all_account_mail(static_mainwindow, autochk, 
+       if (!sc_starting) {
+               inc_all_account_mail(static_mainwindow, autochk, FALSE,
                        prefs_common.newmail_notify_manu);
 
-       if (sc_starting) {
+       } else {
+               inc_all_account_mail(static_mainwindow, FALSE,
+                               prefs_common.chk_on_startup,
+                               prefs_common.newmail_notify_manu);
                sc_starting = FALSE;
                main_window_set_menu_sensitive(static_mainwindow);
                toolbar_main_set_sensitive(static_mainwindow);
@@ -443,12 +449,12 @@ static int migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
        gchar buf[BUFFSIZE];
        gboolean err = FALSE;
 
-       oldfp = g_fopen(old_rc, "r");
+       oldfp = claws_fopen(old_rc, "r");
        if (!oldfp)
                return -1;
-       newfp = g_fopen(new_rc, "w");
+       newfp = claws_fopen(new_rc, "w");
        if (!newfp) {
-               fclose(oldfp);
+               claws_fclose(oldfp);
                return -1;
        }
        
@@ -464,21 +470,21 @@ static int migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
                old_plugin_path = g_strdup(new_plugin_path);
        }
        debug_print("replacing %s with %s\n", old_plugin_path, new_plugin_path);
-       while (fgets(buf, sizeof(buf), oldfp)) {
+       while (claws_fgets(buf, sizeof(buf), oldfp)) {
                if (strncmp(buf, old_plugin_path, strlen(old_plugin_path))) {
-                       err |= (fputs(buf, newfp) == EOF);
+                       err |= (claws_fputs(buf, newfp) == EOF);
                } else {
                        debug_print("->replacing %s\n", buf);
                        debug_print("  with %s%s\n", new_plugin_path, buf+strlen(old_plugin_path));
-                       err |= (fputs(new_plugin_path, newfp) == EOF);
-                       err |= (fputs(buf+strlen(old_plugin_path), newfp) == EOF);
+                       err |= (claws_fputs(new_plugin_path, newfp) == EOF);
+                       err |= (claws_fputs(buf+strlen(old_plugin_path), newfp) == EOF);
                }
        }
        g_free(plugin_path);
        g_free(new_plugin_path);
        g_free(old_plugin_path);
-       fclose(oldfp);
-       if (safe_fclose(newfp) == EOF)
+       claws_fclose(oldfp);
+       if (claws_safe_fclose(newfp) == EOF)
                err = TRUE;
        
        return (err ? -1:0);
@@ -635,6 +641,16 @@ static void sc_session_manager_connect(MainWindow *mainwin)
                                NULL, &client_id,
                                256, error_string_ret);
 
+               /* From https://www.x.org/releases/X11R7.7/doc/libSM/SMlib.txt:
+                * If SmcOpenConnection succeeds, it returns an opaque connection
+                * pointer of type SmcConn and the client_id_ret argument contains
+                * the client ID to be used for this session. The client_id_ret
+                * should be freed with a call to free when no longer needed. On
+                * failure, SmcOpenConnection returns NULL, and the reason for
+                * failure is returned in error_string_ret. */
+               if (mainwin->smc_conn != NULL)
+                       g_free(client_id);
+
                if (error_string_ret[0] || mainwin->smc_conn == NULL)
                        g_warning ("While connecting to session manager: %s.",
                                error_string_ret);
@@ -731,14 +747,14 @@ static void win32_log(const gchar *log_domain, GLogLevelFlags log_level, const g
 
 static void win32_open_log(void)
 {
-       gchar *logfile = g_strconcat(g_get_tmp_dir(), G_DIR_SEPARATOR_S, "claws-win32.log", NULL);
-       gchar *oldlogfile = g_strconcat(g_get_tmp_dir(), G_DIR_SEPARATOR_S, "claws-win32.log.bak", NULL);
+       gchar *logfile = win32_debug_log_path();
+       gchar *oldlogfile = g_strconcat(logfile, ".bak", NULL);
 
        if (is_file_exist(logfile)) {
                if (rename_force(logfile, oldlogfile) < 0)
                        FILE_OP_ERROR(logfile, "rename");
        }
-       win32_debug_fp = g_fopen(logfile, "w");
+       win32_debug_fp = claws_fopen(logfile, "w");
        g_free(logfile);
        g_free(oldlogfile);
        if (win32_debug_fp)
@@ -761,7 +777,7 @@ static void win32_close_log(void)
                g_log_remove_handler("", win32_log_handler_app_id);
                g_log_remove_handler("GLib", win32_log_handler_glib_id);
                g_log_remove_handler("Gtk", win32_log_handler_gtk_id);
-               fclose(win32_debug_fp);
+               claws_fclose(win32_debug_fp);
                win32_debug_fp=NULL;
        }
 }              
@@ -1032,9 +1048,7 @@ int main(int argc, char *argv[])
 
 #ifdef CRASH_DIALOG
        if (cmd.crash) {
-#if !GTK_CHECK_VERSION(3, 0, 0)
                gtk_set_locale();
-#endif
                gtk_init(&argc, &argv);
                crash_main(cmd.crash_params);
 #ifdef G_OS_WIN32
@@ -1067,9 +1081,7 @@ int main(int argc, char *argv[])
 
        reset_statistics();
        
-#if !GTK_CHECK_VERSION(3, 0, 0)
        gtk_set_locale();
-#endif
        gtk_init(&argc, &argv);
 
 #ifdef G_OS_WIN32
@@ -1116,11 +1128,9 @@ int main(int argc, char *argv[])
        }
 #endif
 
-#if !GTK_CHECK_VERSION(3, 0, 0)
        gtk_widget_set_default_colormap(
                gdk_screen_get_system_colormap(
                        gdk_screen_get_default()));
-#endif
 
        gtkut_create_ui_manager();
 
@@ -1440,7 +1450,9 @@ int main(int argc, char *argv[])
                folder_item_update_freeze();
        }
        /* make the crash-indicator file */
-       str_write_to_file("foo", get_crashfile_name());
+       if (str_write_to_file("foo", get_crashfile_name(), FALSE) < 0) {
+               g_warning("Can't create the crash-indicator file.");
+       }
 
        inc_autocheck_timer_init(mainwin);
 
@@ -1681,6 +1693,7 @@ static void exit_claws(MainWindow *mainwin)
                if (prefs_common.last_opened_folder != NULL)
                        g_free(prefs_common.last_opened_folder);
                prefs_common.last_opened_folder =
+                       !prefs_common.goto_last_folder_on_startup ? NULL :
                        folder_item_get_identifier(item);
        }
 
@@ -1788,12 +1801,12 @@ static GString * parse_cmd_compose_from_file(const gchar *fn)
        if (isstdin)
                fp = stdin;
        else {
-               fp = g_fopen(fn, "r");
+               fp = claws_fopen(fn, "r");
                if (!fp)
                        G_PRINT_EXIT(_("Cannot open filename for reading\n"));
        }
 
-       while (fgets(fb, sizeof(fb), fp)) {
+       while (claws_fgets(fb, sizeof(fb), fp)) {
                gchar *tmp;     
                strretchomp(fb);
                if (*fb == '\0')
@@ -1823,11 +1836,11 @@ static GString * parse_cmd_compose_from_file(const gchar *fn)
        g_string_append(body, to);
        g_free(to);
        g_string_append(body, "?body=");
-       while (fgets(fb, sizeof(fb), fp)) {
+       while (claws_fgets(fb, sizeof(fb), fp)) {
                g_string_append_uri_escaped(body, fb, NULL, TRUE);
        }
        if (!isstdin)
-               fclose(fp);
+               claws_fclose(fp);
        /* append the remaining headers */
        g_string_append(body, headers->str);
        g_string_free(headers, TRUE);
@@ -1972,7 +1985,7 @@ static void parse_cmd_opt(int argc, char *argv[])
                                          "                         use - as file name for reading from standard input;\n"
                                          "                         content format: headers first (To: required) until an\n"
                                          "                         empty line, then mail body until end of file."));
-                       g_print("%s\n", _("  --subscribe [uri]      subscribe to the given URI if possible"));
+                       g_print("%s\n", _("  --subscribe uri        subscribe to the given URI if possible"));
                        g_print("%s\n", _("  --attach file1 [file2]...\n"
                                          "                         open composition window with specified files\n"
                                          "                         attached"));
@@ -2007,7 +2020,7 @@ static void parse_cmd_opt(int argc, char *argv[])
                        g_print("%s\n", _("  --version -v           output version information and exit"));
                        g_print("%s\n", _("  --version-full -V      output version and built-in features information and exit"));
                        g_print("%s\n", _("  --config-dir           output configuration directory"));
-                       g_print("%s\n", _("  --alternate-config-dir [dir]\n"
+                       g_print("%s\n", _("  --alternate-config-dir directory\n"
                                          "                         use specified configuration directory"));
                        g_print("%s\n", _("  --geometry -geometry WxH+X+Y\n"
                                          "                         set geometry for main window"));
@@ -2062,7 +2075,7 @@ static void parse_cmd_opt(int argc, char *argv[])
                        } else if (is_dir_exist(argv[i]) || is_file_exist(argv[i])) {
                                cmd.target = argv[i];
                        } else {
-                               g_print(_("Unknown option\n"));
+                               g_print(_("Unknown option. Try -h or --help for usage.\n"));
                                exit(1);
                        }
                }
@@ -2388,11 +2401,11 @@ static gint prohibit_duplicate_launch(void)
        } else if (cmd.send) {
                fd_write_all(uxsock, "send\n", 5);
        } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
-               fd_write(uxsock, "online\n", 6);
+               fd_write(uxsock, "online\n", 7);
        } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
-               fd_write(uxsock, "offline\n", 7);
+               fd_write(uxsock, "offline\n", 8);
        } else if (cmd.debug) {
-               fd_write(uxsock, "debug\n", 7);
+               fd_write(uxsock, "debug\n", 6);
        } else if (cmd.status || cmd.status_full) {
                gchar buf[BUFFSIZE];
                gint i;
@@ -2415,7 +2428,10 @@ static gint prohibit_duplicate_launch(void)
                        fd_gets(uxsock, buf, sizeof(buf) - 1);
                        buf[sizeof(buf) - 1] = '\0';
                        if (!strncmp(buf, ".\n", 2)) break;
-                       fputs(buf, stdout);
+                       if (claws_fputs(buf, stdout) == EOF) {
+                               g_warning("writing to stdout failed.");
+                               break;
+                       }
                }
        } else if (cmd.exit) {
                fd_write_all(uxsock, "exit\n", 5);
@@ -2426,7 +2442,10 @@ static gint prohibit_duplicate_launch(void)
                        fd_gets(uxsock, buf, sizeof(buf) - 1);
                        buf[sizeof(buf) - 1] = '\0';
                        if (!strncmp(buf, ".\n", 2)) break;
-                       fputs(buf, stdout);
+                       if (claws_fputs(buf, stdout) == EOF) {
+                               g_warning("writing to stdout failed.");
+                               break;
+                       }
                }
        } else if (cmd.reset_statistics) {
                fd_write(uxsock, "reset_statistics\n", 17);
@@ -2446,16 +2465,19 @@ static gint prohibit_duplicate_launch(void)
                        fd_gets(uxsock, buf, sizeof(buf) - 1);
                        buf[sizeof(buf) - 1] = '\0';
                        if (!strncmp(buf, ".\n", 2)) break;
-                       fputs(buf, stdout);
+                       if (claws_fputs(buf, stdout) == EOF) {
+                               g_warning("writing to stdout failed.");
+                               break;
+                       }
                }
        } else {
-#ifndef G_OS_WIN32
+#ifdef G_OS_UNIX
                gchar buf[BUFSIZ];
                fd_write_all(uxsock, "get_display\n", 12);
                memset(buf, 0, sizeof(buf));
                fd_gets(uxsock, buf, sizeof(buf) - 1);
                buf[sizeof(buf) - 1] = '\0';
-               if (strcmp2(buf, x_display)) {
+               if (g_strcmp0(buf, x_display)) {
                        g_print("Claws Mail is already running on display %s.\n",
                                buf);
                } else {
@@ -2541,10 +2563,12 @@ static void lock_socket_input_cb(gpointer data,
 
        if (!strncmp(buf, "popup", 5)) {
                main_window_popup(mainwin);
+#ifdef G_OS_UNIX
        } else if (!strncmp(buf, "get_display", 11)) {
                fd_write_all(sock, x_display, strlen(x_display));
+#endif
        } else if (!strncmp(buf, "receive_all", 11)) {
-               inc_all_account_mail(mainwin, FALSE,
+               inc_all_account_mail(mainwin, FALSE, FALSE,
                                     prefs_common.newmail_notify_manu);
        } else if (!strncmp(buf, "receive", 7)) {
                inc_mail(mainwin, prefs_common.newmail_notify_manu);
@@ -2562,7 +2586,7 @@ static void lock_socket_input_cb(gpointer data,
                while (fd_gets(sock, buf, sizeof(buf) - 1) > 0) {
                        buf[sizeof(buf) - 1] = '\0';
                        strretchomp(buf);
-                       if (!strcmp2(buf, "."))
+                       if (!g_strcmp0(buf, "."))
                                break;
 
                        ainfo = g_new0(AttachInfo, 1);