/*
* Sylpheed templates subsystem
* Copyright (C) 2001 Alexander Barinov
- * Copyright (C) 2001 Hiroyuki Yamamoto
+ * Copyright (C) 2001-2005 Hiroyuki Yamamoto
*
* 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
#include "defs.h"
#include <glib.h>
+#include <glib/gi18n.h>
#include <stdio.h>
-#include <dirent.h>
#include <sys/stat.h>
#include <ctype.h>
-#include "intl.h"
#include "utils.h"
#include "template.h"
#include "../codeconv.h"
gchar buf[BUFFSIZE];
gint bytes_read;
- if ((fp = fopen(filename, "rb")) == NULL) {
+ if ((fp = g_fopen(filename, "rb")) == NULL) {
FILE_OP_ERROR(filename, "fopen");
return NULL;
}
tmpl->value = NULL;
while (fgets(buf, sizeof(buf), fp) != NULL) {
- gchar *tmp = NULL;
-
if (buf[0] == '\n')
break;
else if (!g_ascii_strncasecmp(buf, "Name:", 5))
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));
}
g_slist_free(tmpl_list);
}
+gint tmpl_compare(gconstpointer tmpl1, gconstpointer tmpl2)
+{
+ if ((Template *)tmpl1 == NULL || (Template *)tmpl2 == NULL)
+ return 0;
+
+ if (((Template *)tmpl1)->name == NULL || ((Template *)tmpl2)->name == NULL)
+ return 0;
+
+ return (gint) strcmp((char *)((Template *)tmpl1)->name,
+ (char *)((Template *)tmpl2)->name);
+}
+
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;
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;
}
- while ((de = readdir(dp)) != NULL) {
- if (*de->d_name == '.') continue;
-
+ while ((dir_name = g_dir_read_name(dir)) != NULL) {
filename = g_strconcat(path, G_DIR_SEPARATOR_S,
- de->d_name, NULL);
+ 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;
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);
}
- closedir(dp);
+ g_dir_close(dir);
return tmpl_list;
}
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;
fprintf(fp, "To: %s\n", tmpl->to);
if (tmpl->cc && *tmpl->cc != '\0')
fprintf(fp, "Cc: %s\n", tmpl->cc);
- if (tmpl->cc && *tmpl->bcc != '\0')
- fprintf(fp, "Cc: %s\n", tmpl->bcc);
+ if (tmpl->bcc && *tmpl->bcc != '\0')
+ fprintf(fp, "Bcc: %s\n", tmpl->bcc);
fputs("\n", fp);
- fwrite(tmpl->value, sizeof(gchar) * strlen(tmpl->value), 1, fp);
+ if (tmpl->value && *tmpl->value != '\0')
+ fwrite(tmpl->value, sizeof(gchar), strlen(tmpl->value), fp);
+ else
+ fwrite("", sizeof(gchar), 1, fp);
fclose(fp);
g_free(filename);