Backend now retrieves a request, not an Atom ID
[cascardo/atompub.git] / atom / 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 *, char *);
29   void (*enumerate_entries) (AtomCtx *, char ***, 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                                                             char *))
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                                                             char ***,
60                                                             AtomEntry ***,
61                                                             size_t *))
62 {
63   backend->enumerate_entries = enumerate_entries;
64 }
65
66 AtomEntry *
67 atom_retrieve_entry (AtomCtx *ctx, char *req)
68 {
69   AtomBackend *backend;
70   backend = atom_backend (ctx);
71   if (backend && backend->retrieve_entry)
72     return backend->retrieve_entry (ctx, req);
73   return NULL;
74 }
75
76 void
77 atom_backend_enumerate_entries (AtomCtx *ctx, char *** reqs,
78                                 AtomEntry *** entries, size_t *len)
79 {
80   AtomBackend *backend;
81   char **rreqs = NULL;
82   AtomEntry **rentries = NULL;
83   size_t rlen = 0;
84   int i;
85   backend = atom_backend (ctx);
86   if (backend && backend->enumerate_entries)
87     {
88       backend->enumerate_entries (ctx, &rreqs, &rentries, &rlen);
89       atom_map_backend_requests (ctx, rreqs, rentries, rlen);
90       atom_frontend_map_entries (ctx, rreqs, rentries, rlen);
91     }
92   if (reqs)
93     {
94       *reqs = rreqs;
95     }
96   else
97     {
98       for (i = 0; i < rlen; i++)
99         g_free (rreqs[i]);
100       g_free (rreqs);
101     }
102   if (entries)
103     {
104       *entries = rentries;
105     }
106   else
107     {
108       for (i = 0; i < rlen; i++)
109         atom_entry_delete (rentries[i]);
110       g_free (rentries);
111     }
112   if (len)
113     *len = rlen;
114 }
115
116 AtomFeed *
117 atom_retrieve_feed (AtomCtx *ctx)
118 {
119   AtomFeed *feed;
120   AtomEntry **entries;
121   size_t len;
122   atom_backend_enumerate_entries (ctx, NULL, &entries, &len);
123   if (atom_error_get (ctx) != NULL)
124     return NULL;
125   feed = atom_feed_new ();
126   atom_feed_entry_append_array (feed, entries, len);
127   g_free (entries);
128   return feed;
129 }