1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2003 Evan Martin <evan@livejournal.com>
17 #include "draftstore.h"
26 gboolean
draft_store_each_header (DraftStore
*ds
, LJEntry
*entry
, DraftStoreHeaderFunc func
, gpointer data
) {
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) {
41 e
->time
= *localtime(&statbuf
.st_mtime
);
45 entry
->itemid
= e
->itemid
;
46 entry
->subject
= e
->subject
;
48 entry
->security
= e
->security
;
49 entry
->time
= e
->time
;
51 func(ds
, entry
, data
);
52 g_free(entry
->subject
);
54 entry
->subject
= NULL
;
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
);
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
);
77 gboolean
draft_store_put_entry(DraftStore
*ds
, LJEntry
*entry
, GError
**err
) {
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
);
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
) {
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
) {
109 if (itemid
== 100) return 0;
114 DraftStore
*draft_store_new (JamAccount
*acc
) {
115 DraftStore
*ds
= g_new0(DraftStore
, 1);
116 ds
->path
= conf_make_account_path(acc
, "drafts");
121 void draft_store_free (DraftStore
*ds
) {