poll.h: cosmetix
[k8lowj.git] / src / draftstore.c
blob06e165f0f9b248e2612074ea1764040feae1269b
1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2003 Evan Martin <evan@livejournal.com>
3 */
4 #ifdef HAVE_GTK
5 # include "gtk-all.h"
6 #else
7 # include "glib-all.h"
8 #endif
10 #include <errno.h>
11 #include <string.h>
12 #include <unistd.h>
14 #include <sys/stat.h>
16 #include "conf.h"
17 #include "draftstore.h"
18 #include "util.h"
21 struct _DraftStore {
22 char *path;
26 gboolean draft_store_each_header (DraftStore *ds, LJEntry *entry, DraftStoreHeaderFunc func, gpointer data) {
27 GDir *dir;
28 const char *filename;
29 char *path;
30 LJEntry *e;
32 dir = g_dir_open(ds->path, 0, NULL);
33 if (!dir) return FALSE;
35 for (filename = g_dir_read_name(dir); filename; filename = g_dir_read_name(dir)) {
36 path = g_build_filename(ds->path, filename, NULL);
37 e = lj_entry_new_from_filename(path, LJ_ENTRY_FILE_XML, NULL, NULL);
38 if (e && e->time.tm_year == 0) {
39 struct stat statbuf;
40 stat(path, &statbuf);
41 e->time = *localtime(&statbuf.st_mtime);
43 g_free(path);
44 if (!e) continue;
45 entry->itemid = e->itemid;
46 entry->subject = e->subject;
47 e->subject = NULL;
48 entry->security = e->security;
49 entry->time = e->time;
50 lj_entry_free(e);
51 func(ds, entry, data);
52 g_free(entry->subject);
54 entry->subject = NULL;
56 g_dir_close(dir);
57 return TRUE;
61 static char *draft_store_make_path (DraftStore *ds, int itemid) {
62 char *filename = g_strdup_printf("%d", -itemid);
63 char *path = g_build_filename(ds->path, filename, NULL);
64 g_free(filename);
65 return path;
69 LJEntry *draft_store_get_entry (DraftStore *ds, int itemid, GError **err) {
70 char *path = draft_store_make_path(ds, itemid);
71 LJEntry *e = lj_entry_new_from_filename(path, LJ_ENTRY_FILE_XML, NULL, NULL);
72 g_free(path);
73 return e;
77 gboolean draft_store_put_entry(DraftStore *ds, LJEntry *entry, GError **err) {
78 char *path;
79 gboolean ret;
80 if (!verify_path(ds->path, TRUE, err)) return FALSE;
81 path = draft_store_make_path(ds, entry->itemid);
82 ret = lj_entry_to_xml_file(entry, path, NULL);
83 g_free(path);
84 return ret;
88 gboolean draft_store_remove_entry(DraftStore *ds, int itemid, GError **err) {
89 char *path = draft_store_make_path(ds, itemid);
90 return (unlink(path) < 0 ? FALSE : TRUE);
94 int draft_store_find_itemid (DraftStore *ds) {
95 size_t pathlen;
96 int itemid;
97 char *pathbuf;
98 struct stat statbuf;
99 pathlen = strlen(ds->path);
100 pathbuf = g_new0(char, pathlen+5);
101 strcpy(pathbuf, ds->path);
102 for (itemid = 1; itemid < 100; ++itemid) {
103 g_snprintf(pathbuf + pathlen, 5, "/%d", itemid);
104 if (stat(pathbuf, &statbuf) < 0 && errno == ENOENT) {
105 break;
108 g_free(pathbuf);
109 if (itemid == 100) return 0;
110 return -itemid;
114 DraftStore *draft_store_new (JamAccount *acc) {
115 DraftStore *ds = g_new0(DraftStore, 1);
116 ds->path = conf_make_account_path(acc, "drafts");
117 return ds;
121 void draft_store_free (DraftStore *ds) {
122 g_free(ds->path);
123 g_free(ds);