/*
* Sylpheed templates subsystem
* Copyright (C) 2001 Alexander Barinov
+ * Copyright (C) 2001 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 <gdk/gdk.h>
+#include <glib.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
+#include <ctype.h>
-#include "utils.h"
+#include "intl.h"
#include "main.h"
#include "template.h"
-#include "intl.h"
+#include "utils.h"
+
+static GSList *template_list;
-static GSList* template_load(GSList *tmpl_list, gchar *filename)
+static Template *template_load(gchar *filename)
{
Template *tmpl;
FILE *fp;
- char buf[32000];
+ gchar buf[BUFFSIZE];
gint bytes_read;
- debug_print(_("%s:%d loading template from %s\n"), __FILE__, __LINE__, filename);
-
- if ((fp = fopen(filename, "r")) == NULL) {
+ if ((fp = fopen(filename, "rb")) == NULL) {
FILE_OP_ERROR(filename, "fopen");
- return tmpl_list;
+ return NULL;
}
tmpl = g_new(Template, 1);
-
- if (fgets(buf, sizeof(buf), fp) == NULL) {
- FILE_OP_ERROR(filename, "fgets");
- g_free(tmpl);
- debug_print(_("%s:%d exiting\n"), __FILE__, __LINE__);
- return tmpl_list;
+ tmpl->name = NULL;
+ tmpl->subject = NULL;
+ tmpl->to = NULL;
+ tmpl->value = NULL;
+
+ while (fgets(buf, sizeof(buf), fp) != NULL) {
+ if (buf[0] == '\n')
+ break;
+ else if (!g_strncasecmp(buf, "Name:", 5))
+ tmpl->name = g_strdup(g_strstrip(buf + 5));
+ else if (!g_strncasecmp(buf, "Subject:", 8))
+ tmpl->subject = g_strdup(g_strstrip(buf + 8));
+ else if (!g_strncasecmp(buf, "To:", 3))
+ tmpl->to = g_strdup(g_strstrip(buf + 3));
}
- tmpl->name = g_strdup(g_strstrip(buf));
-
- memset(buf, 0, sizeof(buf));
- if ((bytes_read = fread(buf, 1, sizeof(buf)-1, fp)) == 0) {
- FILE_OP_ERROR(filename, "fread");
- g_free(tmpl->name);
- g_free(tmpl);
- return tmpl_list;
+
+ if (!tmpl->name) {
+ g_warning("wrong template format\n");
+ template_free(tmpl);
+ return NULL;
}
- tmpl->value = g_strdup(buf);
- tmpl_list = g_slist_append(tmpl_list, tmpl);
+ if ((bytes_read = fread(buf, 1, sizeof(buf), fp)) == 0) {
+ if (ferror(fp)) {
+ FILE_OP_ERROR(filename, "fread");
+ template_free(tmpl);
+ return NULL;
+ }
+ }
fclose(fp);
- return tmpl_list;
+ tmpl->value = g_strndup(buf, bytes_read);
+
+ return tmpl;
}
void template_free(Template *tmpl)
{
g_free(tmpl->name);
+ g_free(tmpl->subject);
+ g_free(tmpl->to);
g_free(tmpl->value);
g_free(tmpl);
}
void template_clear_config(GSList *tmpl_list)
{
+ GSList *cur;
Template *tmpl;
- while (tmpl_list != NULL) {
- tmpl = tmpl_list->data;
+ for (cur = tmpl_list; cur != NULL; cur = cur->next) {
+ tmpl = (Template *)cur->data;
template_free(tmpl);
- tmpl_list = g_slist_remove(tmpl_list, tmpl);
}
+ g_slist_free(tmpl_list);
}
-GSList* template_read_config(void)
+GSList *template_read_config(void)
{
gchar *path;
gchar *filename;
DIR *dp;
struct dirent *de;
struct stat s;
+ Template *tmpl;
GSList *tmpl_list = NULL;
- path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, TEMPLATES_DIR, NULL);
- debug_print(_("%s:%d reading templates dir %s\n"), __FILE__, __LINE__, path);
+ path = get_template_dir();
+ debug_print("%s:%d reading templates dir %s\n",
+ __FILE__, __LINE__, path);
+
+ if (!is_dir_exist(path)) {
+ if (make_dir(path) < 0)
+ return NULL;
+ }
if ((dp = opendir(path)) == NULL) {
FILE_OP_ERROR(path, "opendir");
- return tmpl_list;
+ return NULL;
}
while ((de = readdir(dp)) != NULL) {
+ if (*de->d_name == '.') continue;
+
filename = g_strconcat(path, G_DIR_SEPARATOR_S, de->d_name, NULL);
- debug_print(_("%s:%d found file %s\n"), __FILE__, __LINE__, filename);
if (stat(filename, &s) != 0 || !S_ISREG(s.st_mode) ) {
- debug_print(_("%s:%d %s is not an ordinary file\n"),
- __FILE__, __LINE__, filename);
+ debug_print("%s:%d %s is not an ordinary file\n",
+ __FILE__, __LINE__, filename);
continue;
}
- tmpl_list = template_load(tmpl_list, filename);
+ tmpl = template_load(filename);
+ if (tmpl)
+ tmpl_list = g_slist_append(tmpl_list, tmpl);
g_free(filename);
}
closedir(dp);
- g_free(path);
+
return tmpl_list;
}
void template_write_config(GSList *tmpl_list)
{
gchar *path;
- gchar *filename;
GSList *cur;
Template *tmpl;
FILE *fp;
- gint tmpl_num = 1;
+ gint tmpl_num;
+
+ debug_print("%s:%d writing templates\n", __FILE__, __LINE__);
- path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, TEMPLATES_DIR, NULL);
+ path = get_template_dir();
if (!is_dir_exist(path)) {
if (is_file_exist(path)) {
- debug_print(_("%s:%d file %s allready exists\n"),
- __FILE__, __LINE__, filename);
- g_free(path);
+ g_warning(_("file %s already exists\n"), path);
return;
}
- if (mkdir(path, S_IRWXU) < 0) {
- FILE_OP_ERROR(path, "mkdir");
- g_free(path);
+ if (make_dir(path) < 0)
return;
- }
}
remove_all_files(path);
- for (cur = tmpl_list; cur != NULL; cur = cur->next) {
+ for (cur = tmpl_list, tmpl_num = 1; cur != NULL;
+ cur = cur->next, tmpl_num++) {
+ gchar *filename;
+
tmpl = cur->data;
- 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, "w")) == NULL) {
+ if ((fp = fopen(filename, "wb")) == NULL) {
FILE_OP_ERROR(filename, "fopen");
g_free(filename);
g_free(path);
return;
}
- debug_print(_("%s:%d writing template \"%s\" to %s\n"),
- __FILE__, __LINE__, tmpl->name, filename);
- fputs(tmpl->name, fp);
+ 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);
fputs("\n", fp);
- fwrite(tmpl->value, sizeof(gchar), strlen(tmpl->value), fp);
+ fwrite(tmpl->value, sizeof(gchar) * strlen(tmpl->value), 1, fp);
fclose(fp);
-
- tmpl_num ++;
}
+}
+
+GSList *template_get_config(void)
+{
+ if (!template_list)
+ template_list = template_read_config();
+
+ return template_list;
+}
- g_free(path);
+void template_set_config(GSList *tmpl_list)
+{
+ template_clear_config(template_list);
+ template_write_config(tmpl_list);
+ template_list = tmpl_list;
}