Switch core from strerror to g_strerror
[claws.git] / src / main.c
index e04211553a8b7379a70584d5a977ec9ad2fdb6ec..5f8ddb1d3dacedb2e96d9f6aaa766836dace35e0 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2013 Hiroyuki Yamamoto and the Claws Mail team
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2015 Hiroyuki Yamamoto and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -14,7 +14,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
- * 
+ *
  */
 
 #ifdef HAVE_CONFIG_H
@@ -958,28 +958,6 @@ static void reset_statistics(void)
        session_stats.time_started = time(NULL);
 }
 
-static void test_strftime(void)
-{
-       int i;
-       char buf[64];
-       time_t dummy = time(NULL);
-       struct tm tbuf;
-       struct tm *lt = localtime_r(&dummy, &tbuf);
-
-       {
-       START_TIMING("fast_strftime");
-       for (i = 0; i < 100000; i++)
-               fast_strftime(buf, 64, "%x %X %r", lt);
-       END_TIMING();
-       }
-       {
-       START_TIMING("strftime");
-       for (i = 0; i < 100000; i++)
-               strftime(buf, 64, "%x %X %r", lt);
-       END_TIMING();
-       }
-}
-
 int main(int argc, char *argv[])
 {
 #ifdef HAVE_DBUS_GLIB
@@ -1082,6 +1060,10 @@ int main(int argc, char *argv[])
                        "gtk-theme-name",
                        "MS-Windows",
                        "XProperty");
+       gtk_settings_set_long_property(gtk_settings_get_default(),
+                       "gtk-auto-mnemonics",
+                       TRUE,
+                       "XProperty");
 #endif
 
 #ifdef HAVE_NETWORKMANAGER_SUPPORT
@@ -1574,7 +1556,6 @@ int main(int argc, char *argv[])
 
        END_TIMING();
 
-       test_strftime();
        gtk_main();
 
 #ifdef HAVE_NETWORKMANAGER_SUPPORT
@@ -1627,9 +1608,15 @@ static void exit_claws(MainWindow *mainwin)
        if(mainwin->folderview->opened) {
                FolderItem *item;
 
-               item = gtk_cmctree_node_get_row_data(GTK_CMCTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
-               summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
-               prefs_common.last_opened_folder = folder_item_get_identifier(item);
+               item = gtk_cmctree_node_get_row_data(
+                       GTK_CMCTREE(mainwin->folderview->ctree),
+                       mainwin->folderview->opened);
+               if (item) {
+                       summary_save_prefs_to_folderitem(
+                               mainwin->folderview->summaryview, item);
+                       prefs_common.last_opened_folder = 
+                               folder_item_get_identifier(item);
+               }
        }
 
        /* save all state before exiting */
@@ -1767,11 +1754,7 @@ static GString * parse_cmd_compose_from_file(const gchar *fn)
                        g_string_append_c(headers, '&');
                        g_string_append(headers, tmp);
                        g_string_append_c(headers, '=');
-#if GLIB_CHECK_VERSION(2,16,0)
                        g_string_append_uri_escaped(headers, v, NULL, TRUE);
-#else
-                       G_STRING_APPEND_ENCODED_URI(headers, v);
-#endif 
                }
                g_free(tmp);
        }
@@ -1781,11 +1764,7 @@ static GString * parse_cmd_compose_from_file(const gchar *fn)
        g_free(to);
        g_string_append(body, "?body=");
        while (fgets(fb, sizeof(fb), fp)) {
-#if GLIB_CHECK_VERSION(2,16,0)
                g_string_append_uri_escaped(body, fb, NULL, TRUE);
-#else
-               G_STRING_APPEND_ENCODED_URI(body, fb);
-#endif
        }
        if (!isstdin)
                fclose(fp);
@@ -2049,7 +2028,7 @@ static void initial_processing(FolderItem *item, gpointer data)
 
 static gboolean draft_all_messages(void)
 {
-       GList *compose_list = NULL;
+       const GList *compose_list = NULL;
        
        compose_clear_exit_drafts();
        compose_list = compose_get_compose_list();
@@ -2174,7 +2153,7 @@ gchar *claws_get_socket_name(void)
                stat_ok = stat(socket_dir, &st);
                if (stat_ok < 0 && errno != ENOENT) {
                        g_print("Error stat'ing socket_dir %s: %s\n",
-                               socket_dir, strerror(errno));
+                               socket_dir, g_strerror(errno));
                } else if (stat_ok == 0 && S_ISSOCK(st.st_mode)) {
                        /* old versions used a sock in $TMPDIR/claws-mail-$UID */
                        debug_print("Using legacy socket %s\n", socket_dir);
@@ -2184,7 +2163,7 @@ gchar *claws_get_socket_name(void)
 
                if (!is_dir_exist(socket_dir) && make_dir(socket_dir) < 0) {
                        g_print("Error creating socket_dir %s: %s\n",
-                               socket_dir, strerror(errno));
+                               socket_dir, g_strerror(errno));
                }
 
                md5_hex_digest(md5sum, get_rc_dir());
@@ -2242,13 +2221,13 @@ static gint prohibit_duplicate_launch(void)
                lock_fd = g_open(socket_lock, O_RDWR|O_CREAT, 0);
                if (lock_fd < 0) {
                        debug_print("Couldn't open %s: %s (%d)\n", socket_lock,
-                               strerror(errno), errno);
+                               g_strerror(errno), errno);
                        g_free(socket_lock);
                        return -1;
                }
                if (flock(lock_fd, LOCK_EX) < 0) {
                        debug_print("Couldn't lock %s: %s (%d)\n", socket_lock,
-                               strerror(errno), errno);
+                               g_strerror(errno), errno);
                        close(lock_fd);
                        g_free(socket_lock);
                        return -1;
@@ -2260,6 +2239,7 @@ static gint prohibit_duplicate_launch(void)
                ret = fd_open_unix(path);
 #if HAVE_FLOCK
                flock(lock_fd, LOCK_UN);
+               close(lock_fd);
                claws_unlink(socket_lock);
                g_free(socket_lock);
 #endif
@@ -2359,7 +2339,8 @@ static gint prohibit_duplicate_launch(void)
                }
                fd_write_all(uxsock, ".\n", 2);
                for (;;) {
-                       fd_gets(uxsock, buf, sizeof(buf));
+                       fd_gets(uxsock, buf, sizeof(buf) - 1);
+                       buf[sizeof(buf) - 1] = '\0';
                        if (!strncmp(buf, ".\n", 2)) break;
                        fputs(buf, stdout);
                }
@@ -2369,7 +2350,8 @@ static gint prohibit_duplicate_launch(void)
                gchar buf[BUFSIZ];
                fd_write(uxsock, "statistics\n", 11);
                for (;;) {
-                       fd_gets(uxsock, buf, sizeof(buf));
+                       fd_gets(uxsock, buf, sizeof(buf) - 1);
+                       buf[sizeof(buf) - 1] = '\0';
                        if (!strncmp(buf, ".\n", 2)) break;
                        fputs(buf, stdout);
                }
@@ -2388,7 +2370,8 @@ static gint prohibit_duplicate_launch(void)
                fd_write_all(uxsock, str, strlen(str));
                g_free(str);
                for (;;) {
-                       fd_gets(uxsock, buf, sizeof(buf));
+                       fd_gets(uxsock, buf, sizeof(buf) - 1);
+                       buf[sizeof(buf) - 1] = '\0';
                        if (!strncmp(buf, ".\n", 2)) break;
                        fputs(buf, stdout);
                }
@@ -2397,7 +2380,8 @@ static gint prohibit_duplicate_launch(void)
                gchar buf[BUFSIZ];
                fd_write_all(uxsock, "get_display\n", 12);
                memset(buf, 0, sizeof(buf));
-               fd_gets(uxsock, buf, sizeof(buf));
+               fd_gets(uxsock, buf, sizeof(buf) - 1);
+               buf[sizeof(buf) - 1] = '\0';
                if (strcmp2(buf, x_display)) {
                        g_print("Claws Mail is already running on display %s.\n",
                                buf);
@@ -2447,7 +2431,8 @@ static GPtrArray *get_folder_item_list(gint sock)
        GPtrArray *folders = NULL;
 
        for (;;) {
-               fd_gets(sock, buf, sizeof(buf));
+               fd_gets(sock, buf, sizeof(buf) - 1);
+               buf[sizeof(buf) - 1] = '\0';
                if (!strncmp(buf, ".\n", 2)) {
                        break;
                }
@@ -2475,7 +2460,11 @@ static void lock_socket_input_cb(gpointer data,
        gchar buf[BUFFSIZE];
 
        sock = fd_accept(source);
-       fd_gets(sock, buf, sizeof(buf));
+       if (sock < 0)
+               return;
+
+       fd_gets(sock, buf, sizeof(buf) - 1);
+       buf[sizeof(buf) - 1] = '\0';
 
        if (!strncmp(buf, "popup", 5)) {
                main_window_popup(mainwin);
@@ -2497,7 +2486,8 @@ static void lock_socket_input_cb(gpointer data,
                gchar *mailto;
 
                mailto = g_strdup(buf + strlen("compose_attach") + 1);
-               while (fd_gets(sock, buf, sizeof(buf)) > 0) {
+               while (fd_gets(sock, buf, sizeof(buf) - 1) > 0) {
+                       buf[sizeof(buf) - 1] = '\0';
                        strretchomp(buf);
                        if (!strcmp2(buf, "."))
                                break;
@@ -2613,8 +2603,9 @@ static void lock_socket_input_cb(gpointer data,
                folder_name = g_strdup(buf+7);
                strretchomp(folder_name);
 
-               if (fd_gets(sock, buf, sizeof(buf)) <= 0) 
+               if (fd_gets(sock, buf, sizeof(buf) - 1) <= 0) 
                        goto search_exit;
+               buf[sizeof(buf) - 1] = '\0';
 
                switch (toupper(buf[0])) {
                case 'S': searchType = ADVANCED_SEARCH_SUBJECT; break;
@@ -2625,22 +2616,24 @@ static void lock_socket_input_cb(gpointer data,
                case 'E': searchType = ADVANCED_SEARCH_EXTENDED; break;
                }
 
-               if (fd_gets(sock, buf, sizeof(buf)) <= 0) 
+               if (fd_gets(sock, buf, sizeof(buf) - 1) <= 0) 
                        goto search_exit;
 
+               buf[sizeof(buf) - 1] = '\0';
                request = g_strdup(buf);
                strretchomp(request);
 
                recursive = TRUE;
-               if (fd_gets(sock, buf, sizeof(buf)) > 0)
+               if (fd_gets(sock, buf, sizeof(buf) - 1) > 0)
                        recursive = buf[0] != '0';
 
+               buf[sizeof(buf) - 1] = '\0';
+
                debug_print("search: %s %i %s %i\n", folder_name, searchType, request, recursive);
 
-               if (folder_name)
-                       folderItem = folder_find_item_from_identifier(folder_name);
+               folderItem = folder_find_item_from_identifier(folder_name);
 
-               if (folder_name && folderItem == NULL) {
+               if (folderItem == NULL) {
                        debug_print("Unknown folder item : '%s', searching folder\n",folder_name);
                        Folder* folder = folder_find_from_path(folder_name);
                        if (folder != NULL)
@@ -2797,7 +2790,7 @@ static void install_memory_sighandler()
        mem_notify_fd = g_open("/dev/mem_notify", O_RDONLY|O_NONBLOCK, 0);
        if (mem_notify_fd == -1) {
                debug_print("/dev/mem_notify not available (%s)\n", 
-                       strerror(errno));
+                       g_strerror(errno));
                return;
        }