If backend cannot enumerate entries, return 0 entries with NULL array
[cascardo/atompub.git] / src / backend.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 <atompub/atom.h>
21
22 #include <glib.h>
23
24 #include <string.h>
25
26 struct _atom_backend
27 {
28   AtomEntry * (*retrieve_entry) (AtomCtx *, IRI *);
29   void (*enumerate_entries) (AtomCtx *, AtomEntry ***, size_t *);
30 };
31
32 AtomBackend *
33 atom_backend_new ()
34 {
35   AtomBackend *backend;
36   backend = g_slice_new (AtomBackend);
37   backend->retrieve_entry = NULL;
38   backend->enumerate_entries = NULL;
39   return backend;
40 }
41
42 void
43 atom_backend_delete (AtomBackend *backend)
44 {
45   g_slice_free (AtomBackend, backend);
46 }
47
48 void
49 atom_backend_retrieve_entry_set (AtomBackend *backend,
50                                  AtomEntry *retrieve_entry (AtomCtx *,
51                                                             IRI *))
52 {
53   backend->retrieve_entry = retrieve_entry;
54 }
55
56 void
57 atom_backend_enumerate_entries_set (AtomBackend *backend,
58                                     void enumerate_entries (AtomCtx *,
59                                                             AtomEntry ***, size_t*))
60 {
61   backend->enumerate_entries = enumerate_entries;
62 }
63
64 AtomEntry *
65 atom_retrieve_entry (AtomCtx *ctx, IRI *iri)
66 {
67   AtomBackend *backend;
68   backend = atom_backend (ctx);
69   if (backend && backend->retrieve_entry)
70     return backend->retrieve_entry (ctx, iri);
71   return NULL;
72 }
73
74 void
75 atom_backend_enumerate_entries (AtomCtx *ctx, AtomEntry *** entries, size_t *len)
76 {
77   AtomBackend *backend;
78   backend = atom_backend (ctx);
79   if (backend && backend->enumerate_entries)
80     {
81       backend->enumerate_entries (ctx, entries, len);
82       return;
83     }
84   if (entries)
85     *entries = NULL;
86   if (len)
87     *len = 0;
88 }
89
90 AtomFeed *
91 atom_retrieve_feed (AtomCtx *ctx)
92 {
93   AtomFeed *feed;
94   AtomEntry **entries;
95   size_t len;
96   atom_backend_enumerate_entries (ctx, &entries, &len);
97   feed = atom_feed_new ();
98   atom_feed_entry_append_array (feed, entries, len);
99   return feed;
100 }
101
102 AtomResource *
103 atom_retrieve_resource (AtomCtx *ctx, IRI *iri)
104 {
105   AtomResource *res;
106   char * path;
107   path = iri_get_path (iri);
108   if (!strcmp (path, "/"))
109     {
110       AtomFeed *feed;
111       feed = atom_retrieve_feed (ctx);
112       if (feed == NULL)
113         return NULL;
114       res = atom_resource_new_from_feed (feed);
115       atom_feed_delete (feed);
116     }
117   else
118     {
119       AtomEntry *entry;
120       entry = atom_retrieve_entry (ctx, iri);
121       if (entry == NULL)
122         return NULL;
123       res = atom_resource_new_from_entry (entry);
124       atom_entry_delete (entry);
125     }
126   return res;
127 }