Fix help about command-line arguments that do require a parameter.
[claws.git] / src / main.c
index bf3a31eeb379345de9b6134aa10abdec8c8a320c..8e2f2948ca8643ed6f4ee8ee391176edff59d557 100644 (file)
@@ -90,6 +90,7 @@
 #include "prefs_summaries.h"
 #include "prefs_themes.h"
 #include "prefs_other.h"
+#include "prefs_proxy.h"
 #include "prefs_logging.h"
 #include "prefs_send.h"
 #include "prefs_wrapping.h"
 #include "advsearch.h"
 #include "avatars.h"
 #include "passwordstore.h"
+#include "file-utils.h"
 
 #ifdef HAVE_LIBETPAN
 #include "imap-thread.h"
 static gboolean went_offline_nm;
 #endif
 
-#if !defined(NM_CHECK_VERSION)
-#define NM_CHECK_VERSION(x,y,z) 0
-#endif
 
 #ifdef HAVE_DBUS_GLIB
 static DBusGProxy *awn_proxy = NULL;
@@ -172,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,
@@ -318,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);
@@ -444,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;
        }
        
@@ -465,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 (fclose(newfp) == EOF)
+       claws_fclose(oldfp);
+       if (claws_safe_fclose(newfp) == EOF)
                err = TRUE;
        
        return (err ? -1:0);
@@ -636,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);
@@ -732,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)
@@ -762,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;
        }
 }              
@@ -1033,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
@@ -1068,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
@@ -1107,28 +1118,19 @@ int main(int argc, char *argv[])
                        "/org/freedesktop/NetworkManager",
                        "org.freedesktop.NetworkManager");
                if (nm_proxy) {
-#if NM_CHECK_VERSION(0,8,992)
                        dbus_g_proxy_add_signal(nm_proxy, "StateChanged", G_TYPE_UINT, G_TYPE_INVALID);
                        dbus_g_proxy_connect_signal(nm_proxy, "StateChanged",
                                G_CALLBACK(networkmanager_state_change_cb),
                                NULL,NULL);
-#else
-                       dbus_g_proxy_add_signal(nm_proxy, "StateChange", G_TYPE_UINT, G_TYPE_INVALID);
-                       dbus_g_proxy_connect_signal(nm_proxy, "StateChange",
-                               G_CALLBACK(networkmanager_state_change_cb),
-                               NULL,NULL);
-#endif
                }
 #endif
                install_dbus_status_handler();
        }
 #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();
 
@@ -1271,6 +1273,7 @@ int main(int argc, char *argv[])
        prefs_summaries_init();
        prefs_message_init();
        prefs_other_init();
+       prefs_proxy_init();
        prefs_logging_init();
        prefs_receive_init();
        prefs_send_init();
@@ -1281,6 +1284,9 @@ int main(int argc, char *argv[])
        prefs_spelling_init();
 #endif
 
+       codeconv_set_allow_jisx0201_kana(prefs_common.allow_jisx0201_kana);
+       codeconv_set_broken_are_utf8(prefs_common.broken_are_utf8);
+
 #ifdef G_OS_WIN32
        if(prefs_common.gtk_theme && strcmp(prefs_common.gtk_theme, DEFAULT_W32_GTK_THEME))
                gtk_settings_set_string_property(gtk_settings_get_default(),
@@ -1444,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);
 
@@ -1595,8 +1603,7 @@ int main(int argc, char *argv[])
                open_compose_new(cmd.compose_mailto, cmd.attach_files);
        }
        if (cmd.attach_files) {
-               list_free_strings(cmd.attach_files);
-               g_list_free(cmd.attach_files);
+               list_free_strings_full(cmd.attach_files);
                cmd.attach_files = NULL;
        }
        if (cmd.subscribe) {
@@ -1683,7 +1690,10 @@ static void exit_claws(MainWindow *mainwin)
        if((item = folderview_get_opened_item(mainwin->folderview)) != NULL) {
                summary_save_prefs_to_folderitem(
                        mainwin->summaryview, item);
+               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);
        }
 
@@ -1755,6 +1765,7 @@ static void exit_claws(MainWindow *mainwin)
        prefs_summaries_done();
        prefs_message_done();
        prefs_other_done();
+       prefs_proxy_done();
        prefs_receive_done();
        prefs_logging_done();
        prefs_send_done();
@@ -1767,13 +1778,6 @@ static void exit_claws(MainWindow *mainwin)
        claws_done();
 }
 
-#define G_STRING_APPEND_ENCODED_URI(gstring,source)    \
-       {                                               \
-               gchar tmpbuf[BUFFSIZE];                 \
-               encode_uri(tmpbuf, BUFFSIZE, (source)); \
-               g_string_append((gstring), tmpbuf);     \
-       }
-
 #define G_PRINT_EXIT(msg)      \
        {                       \
                g_print(msg);   \
@@ -1797,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')
@@ -1832,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);
@@ -1844,7 +1848,6 @@ static GString * parse_cmd_compose_from_file(const gchar *fn)
        return body;
 }
 
-#undef G_STRING_APPEND_ENCODED_URI
 #undef G_PRINT_EXIT
 
 static void parse_cmd_opt(int argc, char *argv[])
@@ -1982,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"));
@@ -2017,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"));
@@ -2072,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);
                        }
                }
@@ -2398,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;
@@ -2425,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);
@@ -2436,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);
@@ -2456,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 {
@@ -2551,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);
@@ -2572,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);
@@ -2989,14 +3003,9 @@ gboolean networkmanager_is_online(GError **error)
                g_propagate_error(error, tmp_error);
                return TRUE;
        }
-#if NM_CHECK_VERSION(0,8,992)
        return (state == NM_STATE_CONNECTED_LOCAL ||
                state == NM_STATE_CONNECTED_SITE ||
                state == NM_STATE_CONNECTED_GLOBAL ||
                state == NM_STATE_UNKNOWN);
-#else
-       return (state == NM_STATE_CONNECTED ||
-               state == NM_STATE_UNKNOWN);
-#endif
 }
 #endif