2007-05-03 [wwp] 2.9.1cvs41
[claws.git] / src / common / template.c
index fdafcf8fb052762a8ce62b02820c3670ce9a493c..225c1d83a923ab247ff786045f74386d41773df5 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Sylpheed templates subsystem 
  * Copyright (C) 2001 Alexander Barinov
 /*
  * Sylpheed templates subsystem 
  * Copyright (C) 2001 Alexander Barinov
- * Copyright (C) 2001 Hiroyuki Yamamoto
+ * Copyright (C) 2001-2007 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
  *
  * 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
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 #include "defs.h"
 
 #include <glib.h>
  */
 
 #include "defs.h"
 
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <stdio.h>
 #include <stdio.h>
-#include <dirent.h>
 #include <sys/stat.h>
 #include <ctype.h>
 
 #include <sys/stat.h>
 #include <ctype.h>
 
-#include "intl.h"
 #include "utils.h"
 #include "template.h"
 #include "../codeconv.h"
 #include "utils.h"
 #include "template.h"
 #include "../codeconv.h"
@@ -39,16 +38,14 @@ static Template *template_load(gchar *filename)
        FILE *fp;
        gchar buf[BUFFSIZE];
        gint bytes_read;
        FILE *fp;
        gchar buf[BUFFSIZE];
        gint bytes_read;
-#warning FIXME_GTK2
-       const gchar *src_codeset = conv_get_current_charset_str();
-       const gchar *dest_codeset = CS_UTF_8;
 
 
-       if ((fp = fopen(filename, "rb")) == NULL) {
+       if ((fp = g_fopen(filename, "rb")) == NULL) {
                FILE_OP_ERROR(filename, "fopen");
                return NULL;
        }
 
        tmpl = g_new(Template, 1);
                FILE_OP_ERROR(filename, "fopen");
                return NULL;
        }
 
        tmpl = g_new(Template, 1);
+       tmpl->load_filename = g_strdup(filename);;
        tmpl->name = NULL;
        tmpl->subject = NULL;
        tmpl->to = NULL;
        tmpl->name = NULL;
        tmpl->subject = NULL;
        tmpl->to = NULL;
@@ -57,36 +54,18 @@ static Template *template_load(gchar *filename)
        tmpl->value = NULL;
 
        while (fgets(buf, sizeof(buf), fp) != NULL) {
        tmpl->value = NULL;
 
        while (fgets(buf, sizeof(buf), fp) != NULL) {
-               gchar *tmp = NULL;
-
                if (buf[0] == '\n')
                        break;
                if (buf[0] == '\n')
                        break;
-               else if (!g_strncasecmp(buf, "Name:", 5)) {
-                       tmp = conv_codeset_strdup(buf + 5,
-                                                 src_codeset,
-                                                 dest_codeset);
-                       tmpl->name = tmp ? g_strstrip(tmp) : g_strdup(buf + 5);
-               } else if (!g_strncasecmp(buf, "Subject:", 8)) {
-                       tmp = conv_codeset_strdup(buf + 8,
-                                                 src_codeset,
-                                                 dest_codeset);
-                       tmpl->subject = tmp ? g_strstrip(tmp) : g_strdup(buf + 8);
-               } else if (!g_strncasecmp(buf, "To:", 3)) {
-                       tmp = conv_codeset_strdup(buf + 3,
-                                                 src_codeset,
-                                                 dest_codeset);
-                       tmpl->to = tmp ? g_strstrip(tmp) : g_strdup(buf + 3);
-               } else if (!g_strncasecmp(buf, "Cc:", 3)) {
-                       tmp = conv_codeset_strdup(buf + 3,
-                                                 src_codeset,
-                                                 dest_codeset);
-                       tmpl->cc = tmp ? g_strstrip(tmp) : g_strdup(buf + 3);
-               } else if (!g_strncasecmp(buf, "Bcc:", 4)) {
-                       tmp = conv_codeset_strdup(buf + 4,
-                                                 src_codeset,
-                                                 dest_codeset);
-                       tmpl->bcc = tmp ? g_strstrip(tmp) : g_strdup(buf + 4);
-               }
+               else if (!g_ascii_strncasecmp(buf, "Name:", 5))
+                       tmpl->name = g_strdup(g_strstrip(buf + 5));
+               else if (!g_ascii_strncasecmp(buf, "To:", 3))
+                       tmpl->to = g_strdup(g_strstrip(buf + 3));
+               else if (!g_ascii_strncasecmp(buf, "Cc:", 3))
+                       tmpl->cc = g_strdup(g_strstrip(buf + 3));
+               else if (!g_ascii_strncasecmp(buf, "Bcc:", 4))
+                       tmpl->bcc = g_strdup(g_strstrip(buf + 4));                                              
+               else if (!g_ascii_strncasecmp(buf, "Subject:", 8))
+                       tmpl->subject = g_strdup(g_strstrip(buf + 8));
        }
 
        if (!tmpl->name) {
        }
 
        if (!tmpl->name) {
@@ -103,16 +82,14 @@ static Template *template_load(gchar *filename)
                }
        }
        fclose(fp);
                }
        }
        fclose(fp);
-       buf[bytes_read] = '\0';
-       tmpl->value = conv_codeset_strdup(buf, src_codeset, dest_codeset);
-       if (!tmpl->value)
-               tmpl->value = g_strdup(buf);
+       tmpl->value = g_strndup(buf, bytes_read);
 
        return tmpl;
 }
 
 void template_free(Template *tmpl)
 {
 
        return tmpl;
 }
 
 void template_free(Template *tmpl)
 {
+       g_free(tmpl->load_filename);
        g_free(tmpl->name);
        g_free(tmpl->subject);
        g_free(tmpl->to);
        g_free(tmpl->name);
        g_free(tmpl->subject);
        g_free(tmpl->to);
@@ -134,12 +111,43 @@ void template_clear_config(GSList *tmpl_list)
        g_slist_free(tmpl_list);
 }
 
        g_slist_free(tmpl_list);
 }
 
+static gint tmpl_compare(gconstpointer tmpl1, gconstpointer tmpl2)
+{
+       gchar *basename1, *basename2;
+       long filenum1, filenum2;
+       gint ret = 0;
+
+       if ((Template *)tmpl1 == NULL || (Template *)tmpl2 == NULL)
+               return 0;
+
+       if (((Template *)tmpl1)->load_filename == NULL || ((Template *)tmpl2)->load_filename == NULL)
+               return 0;
+
+       basename1 = g_path_get_basename(((Template *)tmpl1)->load_filename);
+       basename2 = g_path_get_basename(((Template *)tmpl2)->load_filename);
+       filenum1 = atol(basename1);
+       filenum2 = atol(basename2);
+       g_free(basename1);
+       g_free(basename2);
+
+       if (filenum1 == 0 || filenum2 == 0)
+               return 0;
+
+       if (filenum1 < filenum2)
+               ret = -1;
+       else
+               if (filenum1 > filenum2)
+                       ret = 1;
+                       
+       return ret;
+}
+
 GSList *template_read_config(void)
 {
        const gchar *path;
        gchar *filename;
 GSList *template_read_config(void)
 {
        const gchar *path;
        gchar *filename;
-       DIR *dp;
-       struct dirent *de;
+       GDir *dir;
+       const gchar *dir_name;
        struct stat s;
        Template *tmpl;
        GSList *tmpl_list = NULL;
        struct stat s;
        Template *tmpl;
        GSList *tmpl_list = NULL;
@@ -153,17 +161,16 @@ GSList *template_read_config(void)
                        return NULL;
        }
 
                        return NULL;
        }
 
-       if ((dp = opendir(path)) == NULL) {
-               FILE_OP_ERROR(path, "opendir");
+       if ((dir = g_dir_open(path, 0, NULL)) == NULL) {
+               g_warning("failed to open directory: %s\n", path);
                return NULL;
        }
 
                return NULL;
        }
 
-       while ((de = readdir(dp)) != NULL) {
-               if (*de->d_name == '.') continue;
-
-               filename = g_strconcat(path, G_DIR_SEPARATOR_S, de->d_name, NULL);
+       while ((dir_name = g_dir_read_name(dir)) != NULL) {
+               filename = g_strconcat(path, G_DIR_SEPARATOR_S,
+                                      dir_name, NULL);
 
 
-               if (stat(filename, &s) != 0 || !S_ISREG(s.st_mode) ) {
+               if (g_stat(filename, &s) != 0 || !S_ISREG(s.st_mode) ) {
                        debug_print("%s:%d %s is not an ordinary file\n",
                                    __FILE__, __LINE__, filename);
                        continue;
                        debug_print("%s:%d %s is not an ordinary file\n",
                                    __FILE__, __LINE__, filename);
                        continue;
@@ -171,11 +178,12 @@ GSList *template_read_config(void)
 
                tmpl = template_load(filename);
                if (tmpl)
 
                tmpl = template_load(filename);
                if (tmpl)
-                       tmpl_list = g_slist_append(tmpl_list, tmpl);
+                       tmpl_list = g_slist_insert_sorted(tmpl_list, tmpl, tmpl_compare);
+
                g_free(filename);
        }
 
                g_free(filename);
        }
 
-       closedir(dp);
+       g_dir_close(dir);
 
        return tmpl_list;
 }
 
        return tmpl_list;
 }
@@ -205,10 +213,6 @@ void template_write_config(GSList *tmpl_list)
 
        for (cur = tmpl_list, tmpl_num = 1; cur != NULL;
             cur = cur->next, tmpl_num++) {
 
        for (cur = tmpl_list, tmpl_num = 1; cur != NULL;
             cur = cur->next, tmpl_num++) {
-#warning FIXME_GTK2
-               const gchar *src_codeset = CS_UTF_8;
-               const gchar *dest_codeset = conv_get_current_charset_str();
-               gchar *tmp = NULL;
                gchar *filename;
 
                tmpl = cur->data;
                gchar *filename;
 
                tmpl = cur->data;
@@ -216,62 +220,29 @@ void template_write_config(GSList *tmpl_list)
                filename = g_strconcat(path, G_DIR_SEPARATOR_S,
                                       itos(tmpl_num), NULL);
 
                filename = g_strconcat(path, G_DIR_SEPARATOR_S,
                                       itos(tmpl_num), NULL);
 
-               if ((fp = fopen(filename, "wb")) == NULL) {
+               if ((fp = g_fopen(filename, "wb")) == NULL) {
                        FILE_OP_ERROR(filename, "fopen");
                        g_free(filename);
                        return;
                }
 
                        FILE_OP_ERROR(filename, "fopen");
                        g_free(filename);
                        return;
                }
 
-               tmp = conv_codeset_strdup(tmpl->name, src_codeset, dest_codeset);
-               if (!tmp)
-                       tmp = g_strdup(tmpl->name);
-               fprintf(fp, "Name: %s\n", tmp ? tmp : "");
-               g_free(tmp);
-
-               if (tmpl->subject && *tmpl->subject != '\0') {
-                       tmp = conv_codeset_strdup(tmpl->subject,
-                                                 src_codeset, dest_codeset);
-                       if (!tmp)
-                               tmp = g_strdup(tmpl->subject);
-                       fprintf(fp, "Subject: %s\n", tmp);
-                       g_free(tmp);
-               }
-
-               if (tmpl->to && *tmpl->to != '\0') {
-                       tmp = conv_codeset_strdup(tmpl->to,
-                                                 src_codeset, dest_codeset);
-                       if (!tmp)
-                               tmp = g_strdup(tmpl->to);
-                       fprintf(fp, "To: %s\n", tmp);
-                       g_free(tmp);
-               }
-
-               if (tmpl->cc && *tmpl->cc != '\0') {
-                       tmp = conv_codeset_strdup(tmpl->cc,
-                                                 src_codeset, dest_codeset);
-                       if (!tmp)
-                               tmp = g_strdup(tmpl->cc);
-                       fprintf(fp, "Cc: %s\n", tmp);
-                       g_free(tmp);
-               }
-
-               if (tmpl->bcc && *tmpl->bcc != '\0') {
-                       tmp = conv_codeset_strdup(tmpl->bcc,
-                                                 src_codeset, dest_codeset);
-                       if (!tmp)
-                               tmp = g_strdup(tmpl->bcc);
-                       fprintf(fp, "Bcc: %s\n", tmp);
-                       g_free(tmp);
-               }
-
+               fprintf(fp, "Name: %s\n", tmpl->name);
+               if (tmpl->subject && *tmpl->subject != '\0')
+                       fprintf(fp, "Subject: %s\n", tmpl->subject);
+               if (tmpl->to && *tmpl->to != '\0')
+                       fprintf(fp, "To: %s\n", tmpl->to);
+               if (tmpl->cc && *tmpl->cc != '\0')
+                       fprintf(fp, "Cc: %s\n", tmpl->cc);
+               if (tmpl->bcc && *tmpl->bcc != '\0')
+                       fprintf(fp, "Bcc: %s\n", tmpl->bcc);                                            
                fputs("\n", fp);
                fputs("\n", fp);
-               tmp = conv_codeset_strdup(tmpl->value,
-                                         src_codeset, dest_codeset);
-               if (!tmp)
-                       tmp = g_strdup(tmpl->value);
-               fwrite(tmp, sizeof(gchar) * strlen(tmp), 1, fp);
-               g_free(tmp);
+               if (tmpl->value && *tmpl->value != '\0')
+                       fwrite(tmpl->value, sizeof(gchar), strlen(tmpl->value), fp);
+               else
+                       fwrite("", sizeof(gchar), 1, fp);
+
                fclose(fp);
                fclose(fp);
+               g_free(filename);
        }
 }
 
        }
 }