33a8ff91713ea226753fd720181d85dbe1d938f1
[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_id_to_file (AtomCtx *ctx, AtomID *id)
28 {
29   gchar *root = atom_config_get_str (ctx, "gio", "root");
30   gchar *path = atom_id_string (id);
31   gchar *filename = g_build_filename (root, path, NULL);
32   GFile *file = g_file_new_for_path (filename);
33   g_free (root);
34   g_free (filename);
35   return file;
36 }
37
38 static AtomEntry *
39 gio_file_to_atom (AtomCtx *ctx, GFile *file)
40 {
41   GError *error = NULL;
42   gchar *data;
43   gsize len;
44   AtomEntry *atom;
45   error = NULL;
46   if (!g_file_load_contents (file, NULL, &data, &len, NULL, &error))
47     {
48       AtomError *aerr = atom_error_new_from_gerror (error);
49       atom_error_set (ctx, aerr);
50       g_error_free (error);
51       return NULL;
52     }
53   atom = atom_entry_new_data_len (data, len);
54   g_free (data);
55   return atom;
56 }
57
58 static AtomEntry *
59 gio_atom_retrieve_entry (AtomCtx *ctx, AtomID *id)
60 {
61   GFile *file;
62   AtomEntry *atom;
63   file = gio_id_to_file (ctx, id);
64   atom = gio_file_to_atom (ctx, file);
65   g_object_unref (file);
66   return atom;
67 }
68
69 static void
70 gio_enumerate_entries (AtomCtx *ctx, char ***reqs, AtomEntry ***entries,
71                        size_t *len)
72 {
73   GFile *dir;
74   GFileEnumerator *enumerator;
75   GFileInfo *info;
76   GFile *file;
77   AtomEntry *entry;
78   GError *error;
79   gchar *root;
80   gchar *name;
81   gchar *filename;
82   GPtrArray *array;
83   GPtrArray *filenames;
84   root = atom_config_get_str (ctx, "gio", "root");
85   dir = g_file_new_for_path (root);
86   error = NULL;
87   enumerator = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME,
88                                           G_FILE_QUERY_INFO_NONE, NULL, &error);
89   if (enumerator == NULL)
90     {
91       AtomError *aerr = atom_error_new_from_gerror (error);
92       atom_error_set (ctx, aerr);
93       g_error_free (error);
94       return;
95     }
96   array = g_ptr_array_new ();
97   filenames = g_ptr_array_new ();
98   while ((info = g_file_enumerator_next_file (enumerator, NULL, NULL)) != NULL)
99     {
100       name = g_file_info_get_name (info);
101       filename = g_build_filename (root, name, NULL);
102       file = g_file_new_for_path (filename);
103       entry = gio_file_to_atom (ctx, file);
104       if (entry)
105         {
106           g_ptr_array_add (array, entry);
107           g_ptr_array_add (filenames, g_strdup (name));
108         }
109       else
110         {
111           atom_error_set (ctx, NULL);
112         }
113       g_object_unref (file);
114       g_free (filename);
115       g_object_unref (info);
116     }
117   g_object_unref (enumerator);
118   g_object_unref (dir);
119   g_free (root);
120   if (reqs)
121     *reqs = filenames->pdata;
122   if (entries)
123     *entries = array->pdata;
124   if (len)
125     *len = array->len;
126   g_ptr_array_free (array, FALSE);
127   g_ptr_array_free (filenames, FALSE);
128 }
129
130 AtomBackend *
131 gio_backend (void)
132 {
133   AtomBackend *backend;
134   backend = atom_backend_new ();
135   atom_backend_retrieve_entry_set (backend, gio_atom_retrieve_entry);
136   atom_backend_enumerate_entries_set (backend, gio_enumerate_entries);
137   return backend;
138 }