Implement publish_entry function in GIO backend
[cascardo/atompub.git] / backend / gio / gio.c
1 /*
2  *  Copyright (C) 2007  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19
20 #include <glib.h>
21 #include <gio/gio.h>
22 #include <atompub/atom.h>
23 #include <atompub/atom-glib.h>
24 #include <string.h>
25
26 static GFile *
27 gio_req_to_file (AtomCtx *ctx, char *req)
28 {
29   gchar *root = atom_config_get_str (ctx, "gio", "root");
30   gchar *filename = g_build_filename (root, req, NULL);
31   GFile *file = g_file_new_for_path (filename);
32   g_free (root);
33   g_free (filename);
34   return file;
35 }
36
37 static AtomEntry *
38 gio_file_to_atom (AtomCtx *ctx, GFile *file)
39 {
40   GError *error = NULL;
41   gchar *data;
42   gsize len;
43   AtomEntry *atom;
44   error = NULL;
45   if (!g_file_load_contents (file, NULL, &data, &len, NULL, &error))
46     {
47       AtomError *aerr = atom_error_new_from_gerror (error);
48       atom_error_set (ctx, aerr);
49       g_error_free (error);
50       return NULL;
51     }
52   atom = atom_entry_new_data_len (data, len);
53   g_free (data);
54   return atom;
55 }
56
57 static void
58 gio_atom_to_file (AtomCtx *ctx, AtomEntry *entry, GFile *file)
59 {
60   GError *error = NULL;
61   gchar *data = NULL;
62   gsize len = 0;
63   atom_entry_string (entry, &data, &len);
64   if (!g_file_replace_contents (file, data, len, NULL, FALSE, 0, NULL,
65                                 NULL, &error))
66     {
67       AtomError *aerr = atom_error_new_from_gerror (error);
68       atom_error_set (ctx, aerr);
69       g_error_free (error);
70       return NULL;
71     }
72   g_free (data);
73 }
74
75 static AtomEntry *
76 gio_atom_retrieve_entry (AtomCtx *ctx, char *req)
77 {
78   GFile *file;
79   AtomEntry *atom;
80   file = gio_req_to_file (ctx, req);
81   atom = gio_file_to_atom (ctx, file);
82   g_object_unref (file);
83   return atom;
84 }
85
86 static void
87 gio_atom_publish_entry (AtomCtx *ctx, char *req, AtomEntry *entry)
88 {
89   GFile *file;
90   /* TODO: Create a function to map from an Entry ID to a new filename */
91   if (req == NULL)
92     req = atom_entry_id (entry);
93   file = gio_req_to_file (ctx, req);
94   gio_atom_to_file (ctx, entry, file);
95   g_object_unref (file);
96 }
97
98 static void
99 gio_enumerate_entries (AtomCtx *ctx, char ***reqs, AtomEntry ***entries,
100                        size_t *len)
101 {
102   GFile *dir;
103   GFileEnumerator *enumerator;
104   GFileInfo *info;
105   GFile *file;
106   AtomEntry *entry;
107   GError *error;
108   gchar *root;
109   gchar *name;
110   gchar *filename;
111   GPtrArray *array;
112   GPtrArray *filenames;
113   root = atom_config_get_str (ctx, "gio", "root");
114   dir = g_file_new_for_path (root);
115   error = NULL;
116   enumerator = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME,
117                                           G_FILE_QUERY_INFO_NONE, NULL, &error);
118   if (enumerator == NULL)
119     {
120       AtomError *aerr = atom_error_new_from_gerror (error);
121       atom_error_set (ctx, aerr);
122       g_error_free (error);
123       return;
124     }
125   array = g_ptr_array_new ();
126   filenames = g_ptr_array_new ();
127   while ((info = g_file_enumerator_next_file (enumerator, NULL, NULL)) != NULL)
128     {
129       name = g_file_info_get_name (info);
130       filename = g_build_filename (root, name, NULL);
131       file = g_file_new_for_path (filename);
132       entry = gio_file_to_atom (ctx, file);
133       if (entry)
134         {
135           g_ptr_array_add (array, entry);
136           g_ptr_array_add (filenames, g_strdup (name));
137         }
138       else
139         {
140           atom_error_set (ctx, NULL);
141         }
142       g_object_unref (file);
143       g_free (filename);
144       g_object_unref (info);
145     }
146   g_object_unref (enumerator);
147   g_object_unref (dir);
148   g_free (root);
149   if (reqs)
150     *reqs = filenames->pdata;
151   if (entries)
152     *entries = array->pdata;
153   if (len)
154     *len = array->len;
155   g_ptr_array_free (array, FALSE);
156   g_ptr_array_free (filenames, FALSE);
157 }
158
159 AtomBackend *
160 gio_backend (void)
161 {
162   AtomBackend *backend;
163   backend = atom_backend_new ();
164   atom_backend_retrieve_entry_set (backend, gio_atom_retrieve_entry);
165   atom_backend_enumerate_entries_set (backend, gio_enumerate_entries);
166   atom_backend_publish_entry_set (backend, gio_atom_publish_entry);
167   return backend;
168 }