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