Added publish handler to frontend
[cascardo/atompub.git] / atom / frontend.c
1 /*
2  *  Copyright (C) 2008  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_frontend
27 {
28   void (*map_entries) (AtomCtx *, char **, AtomEntry **, size_t);
29   int  (*is_feed) (AtomCtx *, char *);
30   AtomRequest * (*get_request) (AtomCtx *);
31   void (*handle_error) (AtomCtx *);
32   void (*handle_entry) (AtomCtx *, AtomEntry *);
33   void (*handle_feed) (AtomCtx *, AtomFeed *);
34   void (*handle_publish) (AtomCtx *, AtomEntry *);
35 };
36
37 AtomFrontend *
38 atom_frontend_new ()
39 {
40   AtomFrontend *frontend;
41   frontend = g_slice_new (AtomFrontend);
42   frontend->map_entries = NULL;
43   frontend->is_feed = NULL;
44   frontend->get_request = NULL;
45   frontend->handle_error = NULL;
46   frontend->handle_entry = NULL;
47   frontend->handle_feed = NULL;
48   frontend->handle_publish = NULL;
49   return frontend;
50 }
51
52 void
53 atom_frontend_delete (AtomFrontend *frontend)
54 {
55   g_slice_free (AtomFrontend, frontend);
56 }
57
58 void
59 atom_frontend_map_entries_set (AtomFrontend *frontend,
60                                void map_entries (AtomCtx *,
61                                                  char **,
62                                                  AtomEntry **,
63                                                  size_t))
64 {
65   frontend->map_entries = map_entries;
66 }
67
68 void
69 atom_frontend_is_feed_set (AtomFrontend *frontend,
70                           int is_feed (AtomCtx *, char *))
71 {
72   frontend->is_feed = is_feed;
73 }
74
75 void
76 atom_frontend_get_request_set (AtomFrontend *frontend,
77                                AtomRequest * get_request (AtomCtx *))
78 {
79   frontend->get_request = get_request;
80 }
81
82 void
83 atom_frontend_handle_error_set (AtomFrontend *frontend,
84                                 void handle_error (AtomCtx *))
85 {
86   frontend->handle_error = handle_error;
87 }
88
89 void
90 atom_frontend_handle_entry_set (AtomFrontend *frontend,
91                                 void handle_entry (AtomCtx *, AtomEntry *))
92 {
93   frontend->handle_entry = handle_entry;
94 }
95
96 void
97 atom_frontend_handle_feed_set (AtomFrontend *frontend,
98                                void handle_feed (AtomCtx *, AtomFeed *))
99 {
100   frontend->handle_feed = handle_feed;
101 }
102
103 void
104 atom_frontend_handle_publish_set (AtomFrontend *frontend,
105                                   void handle_publish (AtomCtx *, AtomEntry *))
106 {
107   frontend->handle_publish = handle_publish;
108 }
109
110 void
111 atom_frontend_map_entries (AtomCtx *ctx, char ** reqs,
112                            AtomEntry ** entries, size_t len)
113 {
114   AtomFrontend *frontend;
115   frontend = atom_frontend (ctx);
116   if (frontend && frontend->map_entries)
117     {
118       frontend->map_entries (ctx, reqs, entries, len);
119     }
120 }
121
122 int
123 atom_is_feed (AtomCtx *ctx, char *req)
124 {
125   AtomFrontend *frontend;
126   AtomError *aerr;
127   frontend = atom_frontend (ctx);
128   if (frontend && frontend->is_feed)
129     {
130       return frontend->is_feed (ctx, req);
131     }
132   /* If frontend cannot decide if a request is a feed, let's tell it's
133    * not. If the request mapping cannot be done, it will return a "Not
134    * Found" error anyway.
135    */
136   return 0;
137 }
138
139 AtomRequest *
140 atom_get_request (AtomCtx *ctx)
141 {
142   AtomFrontend *frontend;
143   frontend = atom_frontend (ctx);
144   if (frontend && frontend->get_request)
145     {
146       return frontend->get_request (ctx);
147     }
148   return NULL;
149 }
150
151 void
152 atom_handle_error (AtomCtx *ctx)
153 {
154   AtomFrontend *frontend;
155   frontend = atom_frontend (ctx);
156   if (frontend && frontend->handle_error)
157     {
158       frontend->handle_error (ctx);
159     }
160 }
161
162 void
163 atom_handle_entry (AtomCtx *ctx, AtomEntry *entry)
164 {
165   AtomFrontend *frontend;
166   frontend = atom_frontend (ctx);
167   if (frontend && frontend->handle_entry)
168     {
169       frontend->handle_entry (ctx, entry);
170     }
171 }
172
173 void
174 atom_handle_feed (AtomCtx *ctx, AtomFeed *feed)
175 {
176   AtomFrontend *frontend;
177   frontend = atom_frontend (ctx);
178   if (frontend && frontend->handle_feed)
179     {
180       frontend->handle_feed (ctx, feed);
181     }
182 }
183
184 void
185 atom_handle_publish (AtomCtx *ctx, AtomEntry *entry)
186 {
187   AtomFrontend *frontend;
188   frontend = atom_frontend (ctx);
189   if (frontend && frontend->handle_publish)
190     {
191       frontend->handle_publish (ctx, entry);
192     }
193 }