Added frontend requests mapping
[cascardo/atompub.git] / atom / ctx.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 struct _atom_ctx
25 {
26   AtomError *error;
27   gpointer config_data;
28   AtomBackend *backend;
29   AtomFrontend *frontend;
30   GHashTable *bemap;
31   GHashTable *femap;
32 };
33
34 AtomCtx *
35 atom_ctx_new ()
36 {
37   AtomCtx *ctx;
38   ctx = g_slice_new (AtomCtx);
39   ctx->error = NULL;
40   ctx->config_data = NULL;
41   ctx->backend = NULL;
42   ctx->frontend = NULL;
43   ctx->bemap = g_hash_table_new_full (g_str_hash, g_str_equal,
44                                       g_free, g_free);
45   ctx->femap = g_hash_table_new_full (g_str_hash, g_str_equal,
46                                       g_free, g_free);
47   return ctx;
48 }
49
50 void
51 atom_ctx_delete (AtomCtx *ctx)
52 {
53   if (ctx->error)
54     atom_error_delete (ctx->error);
55   if (ctx->backend)
56     atom_backend_delete (ctx->backend);
57   if (ctx->frontend)
58     atom_frontend_delete (ctx->frontend);
59   if (ctx->bemap)
60     g_hash_table_destroy (ctx->bemap);
61   if (ctx->femap)
62     g_hash_table_destroy (ctx->femap);
63   g_slice_free (AtomCtx, ctx);
64 }
65
66 void
67 atom_error_set (AtomCtx *ctx, AtomError *error)
68 {
69   if (ctx->error)
70     atom_error_delete (ctx->error);
71   ctx->error = error;
72 }
73
74 AtomError *
75 atom_error_get (AtomCtx *ctx)
76 {
77   return ctx->error;
78 }
79
80 void *
81 atom_config_data (AtomCtx *ctx)
82 {
83   return ctx->config_data;
84 }
85
86 void
87 atom_config_data_set (AtomCtx *ctx, void *data)
88 {
89   ctx->config_data = data;
90 }
91
92 AtomBackend *
93 atom_backend (AtomCtx *ctx)
94 {
95   return ctx->backend;
96 }
97
98 void
99 atom_backend_set (AtomCtx *ctx, AtomBackend *backend)
100 {
101   ctx->backend = backend;
102 }
103
104 AtomFrontend *
105 atom_frontend (AtomCtx *ctx)
106 {
107   return ctx->frontend;
108 }
109
110 void
111 atom_frontend_set (AtomCtx *ctx, AtomFrontend *frontend)
112 {
113   ctx->frontend = frontend;
114 }
115
116 void
117 atom_map_backend_requests (AtomCtx *ctx, char **reqs,
118                            AtomEntry **entries, size_t len)
119 {
120   int i;
121   for (i = 0; i < len; i++)
122     {
123       char *key = g_strdup (atom_entry_id (entries[i]));
124       char *val = g_strdup (reqs[i]);
125       g_hash_table_replace (ctx->bemap, key, val);
126     }
127 }
128
129 void
130 atom_map_frontend_requests (AtomCtx *ctx, char **reqs,
131                             AtomEntry **entries, size_t len)
132 {
133   int i;
134   for (i = 0; i < len; i++)
135     {
136       char *key = g_strdup (reqs[i]);
137       char *val = g_strdup (atom_entry_id (entries[i]));
138       g_hash_table_replace (ctx->femap, key, val);
139     }
140 }