2012-10-12 [iwkse] 3.8.1cvs95
[claws.git] / src / common / utils.c
index 826724b438f7f02f08fad003510f16f42fe14ac5..1696267ee1735f8394371bb4fd0d69eea5dd62dc 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2009 Hiroyuki Yamamoto & The Claws Mail Team
+ * Copyright (C) 1999-2012 Hiroyuki Yamamoto & 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
@@ -19,6 +19,7 @@
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #include "defs.h"
 
 #include <glib/gi18n.h>
 
+#ifdef USE_PTHREAD
+#include <pthread.h>
+#endif
+
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
@@ -701,7 +706,7 @@ void eliminate_parenthesis(gchar *str, gchar op, gchar cl)
        register gchar *srcp, *destp;
        gint in_brace;
 
-       srcp = destp = str;
+       destp = str;
 
        while ((destp = strchr(destp, op))) {
                in_brace = 1;
@@ -725,7 +730,7 @@ void extract_parenthesis(gchar *str, gchar op, gchar cl)
        register gchar *srcp, *destp;
        gint in_brace;
 
-       srcp = destp = str;
+       destp = str;
 
        while ((srcp = strchr(destp, op))) {
                if (destp > str)
@@ -754,7 +759,7 @@ static void extract_parenthesis_with_skip_quote(gchar *str, gchar quote_chr,
        gint in_brace;
        gboolean in_quote = FALSE;
 
-       srcp = destp = str;
+       destp = str;
 
        while ((srcp = strchr_with_skip_quote(destp, quote_chr, op))) {
                if (destp > str)
@@ -800,7 +805,7 @@ void eliminate_address_comment(gchar *str)
        register gchar *srcp, *destp;
        gint in_brace;
 
-       srcp = destp = str;
+       destp = str;
 
        while ((destp = strchr(destp, '"'))) {
                if ((srcp = strchr(destp + 1, '"'))) {
@@ -817,7 +822,7 @@ void eliminate_address_comment(gchar *str)
                }
        }
 
-       srcp = destp = str;
+       destp = str;
 
        while ((destp = strchr_with_skip_quote(destp, '"', '('))) {
                in_brace = 1;
@@ -2318,6 +2323,18 @@ gint remove_numbered_files(const gchar *dir, guint first, guint last)
        gchar *prev_dir;
        gint file_no;
 
+       if (first == last) {
+               /* Skip all the dir reading part. */
+               gchar *filename = g_strdup_printf("%s%s%u", dir, G_DIR_SEPARATOR_S, first);
+               if (claws_unlink(filename) < 0) {
+                       FILE_OP_ERROR(filename, "unlink");
+                       g_free(filename);
+                       return -1;
+               }
+               g_free(filename);
+               return 0;
+       }
+
        prev_dir = g_get_current_dir();
 
        if (g_chdir(dir) < 0) {
@@ -2361,6 +2378,10 @@ gint remove_numbered_files_not_in_list(const gchar *dir, GSList *numberlist)
        const gchar *dir_name;
        gchar *prev_dir;
        gint file_no;
+       GHashTable *file_no_tbl;
+
+       if (numberlist == NULL)
+           return 0;
 
        prev_dir = g_get_current_dir();
 
@@ -2376,18 +2397,26 @@ gint remove_numbered_files_not_in_list(const gchar *dir, GSList *numberlist)
                return -1;
        }
 
+       file_no_tbl = g_hash_table_new(g_direct_hash, g_direct_equal);
        while ((dir_name = g_dir_read_name(dp)) != NULL) {
                file_no = to_number(dir_name);
-               if (file_no > 0 && (g_slist_find(numberlist, GINT_TO_POINTER(file_no)) == NULL)) {
-                       debug_print("removing unwanted file %d from %s\n", file_no, dir);
-                       if (is_dir_exist(dir_name))
-                               continue;
+               if (is_dir_exist(dir_name))
+                   continue;
+               if (file_no > 0)
+                   g_hash_table_insert(file_no_tbl, GINT_TO_POINTER(file_no), GINT_TO_POINTER(1));
+       }
+       
+       do {
+               if (g_hash_table_lookup(file_no_tbl, numberlist->data) == NULL) {
+                       debug_print("removing unwanted file %d from %s\n", 
+                                   GPOINTER_TO_INT(numberlist->data), dir);
                        if (claws_unlink(dir_name) < 0)
                                FILE_OP_ERROR(dir_name, "unlink");
                }
-       }
+       } while ((numberlist = g_slist_next(numberlist)));
 
        g_dir_close(dp);
+       g_hash_table_destroy(file_no_tbl);
 
        if (g_chdir(prev_dir) < 0) {
                FILE_OP_ERROR(prev_dir, "chdir");
@@ -2869,7 +2898,7 @@ gint canonicalize_file_replace(const gchar *file)
 
 gchar *normalize_newlines(const gchar *str)
 {
-       const gchar *p = str;
+       const gchar *p;
        gchar *out, *outp;
 
        out = outp = g_malloc(strlen(str) + 1);
@@ -3528,9 +3557,7 @@ time_t tzoffset_sec(time_t *now)
 {
        struct tm gmt, *lt;
        gint off;
-#ifndef G_OS_WIN32
        struct tm buf1, buf2;
-#endif
 #ifdef G_OS_WIN32
        if (now && *now < 0)
                return 0;
@@ -3564,9 +3591,7 @@ gchar *tzoffset(time_t *now)
        struct tm gmt, *lt;
        gint off;
        gchar sign = '+';
-#ifndef G_OS_WIN32
        struct tm buf1, buf2;
-#endif
 #ifdef G_OS_WIN32
        if (now && *now < 0)
                return 0;
@@ -3604,10 +3629,8 @@ void get_rfc822_date(gchar *buf, gint len)
        time_t t;
        gchar day[4], mon[4];
        gint dd, hh, mm, ss, yyyy;
-#ifndef G_OS_WIN32
        struct tm buf1;
        gchar buf2[BUFFSIZE];
-#endif
 
        t = time(NULL);
        lt = localtime_r(&t, &buf1);
@@ -3725,7 +3748,7 @@ int subject_get_prefix_length(const gchar *subject)
                "Vs\\:",                        /* "Vs" (Norwegian) */
                "Ad\\:",                        /* "Ad" (Norwegian) */
                "\347\255\224\345\244\215\\:",  /* "Re" (Chinese, UTF-8) */
-               "R\303\251f\\. \\:",            /* "Réf. :" (French Lotus Notes) */
+               "R\303\251f\\. \\:",            /* "Rf. :" (French Lotus Notes) */
                "Re \\:",                       /* "Re :" (French Yahoo Mail) */
                /* add more */
        };
@@ -3784,7 +3807,7 @@ int subject_get_prefix_length(const gchar *subject)
                "sv:",                  /* "Sv" (Norwegian) */
                "vs:",                  /* "Vs" (Norwegian) */
                "ad:",                  /* "Ad" (Norwegian) */
-               "R\303\251f. :",        /* "Réf. :" (French Lotus Notes) */
+               "R\303\251f. :",        /* "Rf. :" (French Lotus Notes) */
                "Re :",                 /* "Re :" (French Yahoo Mail) */
                /* add more */
        };
@@ -3836,24 +3859,17 @@ gchar *generate_msgid(gchar *buf, gint len, gchar *user_addr)
        struct tm *lt;
        time_t t;
        gchar *addr;
-#ifndef G_OS_WIN32
        struct tm buft;
-#endif
 
        t = time(NULL);
        lt = localtime_r(&t, &buft);
 
-       if (strcmp(buf, "") == 0) {
-               if (user_addr != NULL)
-                       addr = g_strconcat(".", user_addr, "@", get_domain_name(), NULL);
-               else
-                       addr = g_strconcat("@", get_domain_name(), NULL);
-       } else {
-               if (user_addr != NULL)
-                       addr = g_strconcat(".", user_addr, "@", buf, NULL);
-               else
-                       addr = g_strconcat("@", buf, NULL);
-       }
+       if (user_addr != NULL)
+             addr = g_strdup_printf(".%s", user_addr);
+       else if (strlen(buf) != 0)
+             addr = g_strdup_printf("@%s", buf);
+       else
+             addr = g_strdup_printf("@%s", get_domain_name());
 
        /* Replace all @ but the last one in addr, with underscores.
         * RFC 2822 States that msg-id syntax only allows one @.
@@ -3876,7 +3892,7 @@ gchar *generate_msgid(gchar *buf, gint len, gchar *user_addr)
 
    return a quoted string safely usable in argument of a command.
 
-   code is extracted and adapted from etPan! project -- DINH V. Hoà.
+   code is extracted and adapted from etPan! project -- DINH V. Ho.
 */
 
 gint quote_cmd_argument(gchar * result, guint size,
@@ -4017,7 +4033,7 @@ void get_hex_str(gchar *out, guchar ch)
        INT_TO_HEX(hex, ch >> 4);
        *out++ = hex;
        INT_TO_HEX(hex, ch & 0x0f);
-       *out++ = hex;
+       *out   = hex;
 }
 
 #undef REF_DEBUG
@@ -4853,9 +4869,11 @@ gboolean file_is_email (const gchar *filename)
               && fgets(buffer, sizeof (buffer), fp) > 0) {
                if (!strncmp(buffer, "From:", strlen("From:")))
                        score++;
-               if (!strncmp(buffer, "To:", strlen("To:")))
+               else if (!strncmp(buffer, "Date:", strlen("Date:")))
                        score++;
-               if (!strncmp(buffer, "Subject:", strlen("Subject:")))
+               else if (!strncmp(buffer, "Message-ID:", strlen("Message-ID:")))
+                       score++;
+               else if (!strncmp(buffer, "Subject:", strlen("Subject:")))
                        score++;
                i++;
        }
@@ -5244,7 +5262,7 @@ size_t fast_strftime(gchar *buf, gint buflen, const gchar *format, struct tm *lt
                        *curpos++ = *format++; 
                }
        }
-       *curpos++ = '\0';
+       *curpos = '\0';
        return total_done;
 }
 
@@ -5290,3 +5308,22 @@ int claws_unlink(const gchar *filename)
        }
        return g_unlink(filename);
 }
+
+GMutex *cm_mutex_new(void) {
+#if GLIB_CHECK_VERSION(2,32,0)
+       GMutex *m = g_new0(GMutex, 1);
+       g_mutex_init(m);
+       return m;
+#else
+       return g_mutex_new();
+#endif
+}
+
+void cm_mutex_free(GMutex *mutex) {
+#if GLIB_CHECK_VERSION(2,32,0)
+       g_mutex_clear(mutex);
+       g_free(mutex);
+#else
+       g_mutex_free(mutex);
+#endif
+}