gtk-all.h, util-gtk.h: cosmetix (ignore this headers if built without GTK)
[k8lowj.git] / src / draftstore.c
blob3e0df5f72fffd9ec74605dbec8b3503b1fa52c14
1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2003 Evan Martin <evan@livejournal.com>
4 * vim: tabstop=4 shiftwidth=4 noexpandtab :
5 */
7 #ifdef HAVE_GTK
8 #include "gtk-all.h"
9 #else
10 #include "glib-all.h"
11 #endif
13 #ifndef G_OS_WIN32
14 #include <unistd.h>
15 #else
16 #include <io.h>
17 #endif
18 #include <sys/stat.h>
19 #include <errno.h>
20 #include <string.h>
22 #include "draftstore.h"
23 #include "conf.h"
24 #include "util.h"
26 struct _DraftStore {
27 char *path;
30 gboolean
31 draft_store_each_header(DraftStore *ds, LJEntry *entry,
32 DraftStoreHeaderFunc func, gpointer data) {
33 GDir *dir;
34 const char *filename;
35 char *path;
36 LJEntry *e;
38 dir = g_dir_open(ds->path, 0, NULL);
39 if (!dir)
40 return FALSE;
42 for (filename = g_dir_read_name(dir);
43 filename;
44 filename = g_dir_read_name(dir)) {
45 path = g_build_filename(ds->path, filename, NULL);
46 e = lj_entry_new_from_filename(path, LJ_ENTRY_FILE_XML, NULL, NULL);
47 if (e && e->time.tm_year == 0) {
48 struct stat statbuf;
49 stat(path, &statbuf);
50 e->time = *localtime(&statbuf.st_mtime);
52 g_free(path);
53 if (!e)
54 continue;
55 entry->itemid = e->itemid;
56 entry->subject = e->subject; e->subject = NULL;
57 entry->security = e->security;
58 entry->time = e->time;
59 lj_entry_free(e);
60 func(ds, entry, data);
61 g_free(entry->subject);
63 entry->subject = NULL;
65 g_dir_close(dir);
66 return TRUE;
69 static char*
70 draft_store_make_path(DraftStore *ds, int itemid) {
71 char *filename, *path;
72 filename = g_strdup_printf("%d", -itemid);
73 path = g_build_filename(ds->path, filename, NULL);
74 g_free(filename);
75 return path;
78 LJEntry*
79 draft_store_get_entry(DraftStore *ds, int itemid, GError **err) {
80 char *path;
81 LJEntry *e;
82 path = draft_store_make_path(ds, itemid);
83 e = lj_entry_new_from_filename(path, LJ_ENTRY_FILE_XML, NULL, NULL);
84 g_free(path);
85 return e;
88 gboolean
89 draft_store_put_entry(DraftStore *ds, LJEntry *entry, GError **err) {
90 char *path;
91 gboolean ret;
92 if (!verify_path(ds->path, TRUE, err))
93 return FALSE;
94 path = draft_store_make_path(ds, entry->itemid);
95 ret = lj_entry_to_xml_file(entry, path, NULL);
96 g_free(path);
97 return ret;
100 gboolean
101 draft_store_remove_entry(DraftStore *ds, int itemid, GError **err) {
102 char *path;
103 path = draft_store_make_path(ds, itemid);
104 if (unlink(path) < 0)
105 return FALSE;
106 return TRUE;
110 draft_store_find_itemid(DraftStore *ds) {
111 size_t pathlen;
112 int itemid;
113 char *pathbuf;
114 struct stat statbuf;
116 pathlen = strlen(ds->path);
117 pathbuf = g_new0(char, pathlen+5);
118 strcpy(pathbuf, ds->path);
119 for (itemid = 1; itemid < 100; itemid++) {
120 g_snprintf(pathbuf+pathlen, 5, "/%d", itemid);
121 if (stat(pathbuf, &statbuf) < 0 && errno == ENOENT) {
122 break;
125 g_free(pathbuf);
126 if (itemid == 100)
127 return 0;
128 return -itemid;
131 DraftStore*
132 draft_store_new(JamAccount *acc) {
133 DraftStore *ds;
135 ds = g_new0(DraftStore, 1);
136 ds->path = conf_make_account_path(acc, "drafts");
138 return ds;
141 void
142 draft_store_free(DraftStore *ds) {
143 g_free(ds->path);
144 g_free(ds);