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