8519b31fd89c25650bc73834e30900785237d661
[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 };
32
33 AtomCtx *
34 atom_ctx_new ()
35 {
36   AtomCtx *ctx;
37   ctx = g_slice_new (AtomCtx);
38   ctx->error = NULL;
39   ctx->config_data = NULL;
40   ctx->backend = NULL;
41   ctx->frontend = NULL;
42   ctx->bemap = g_hash_table_new_full (g_str_hash, g_str_equal,
43                                       g_free, g_free);
44   return ctx;
45 }
46
47 void
48 atom_ctx_delete (AtomCtx *ctx)
49 {
50   if (ctx->error)
51     atom_error_delete (ctx->error);
52   if (ctx->backend)
53     atom_backend_delete (ctx->backend);
54   if (ctx->frontend)
55     atom_frontend_delete (ctx->frontend);
56   if (ctx->bemap)
57     g_hash_table_destroy (ctx->bemap);
58   g_slice_free (AtomCtx, ctx);
59 }
60
61 void
62 atom_error_set (AtomCtx *ctx, AtomError *error)
63 {
64   if (ctx->error)
65     atom_error_delete (ctx->error);
66   ctx->error = error;
67 }
68
69 AtomError *
70 atom_error_get (AtomCtx *ctx)
71 {
72   return ctx->error;
73 }
74
75 void *
76 atom_config_data (AtomCtx *ctx)
77 {
78   return ctx->config_data;
79 }
80
81 void
82 atom_config_data_set (AtomCtx *ctx, void *data)
83 {
84   ctx->config_data = data;
85 }
86
87 AtomBackend *
88 atom_backend (AtomCtx *ctx)
89 {
90   return ctx->backend;
91 }
92
93 void
94 atom_backend_set (AtomCtx *ctx, AtomBackend *backend)
95 {
96   ctx->backend = backend;
97 }
98
99 AtomFrontend *
100 atom_frontend (AtomCtx *ctx)
101 {
102   return ctx->frontend;
103 }
104
105 void
106 atom_frontend_set (AtomCtx *ctx, AtomFrontend *frontend)
107 {
108   ctx->frontend = frontend;
109 }
110
111 void
112 atom_map_backend_requests (AtomCtx *ctx, char **reqs,
113                            AtomEntry **entries, size_t len)
114 {
115   int i;
116   for (i = 0; i < len; i++)
117     {
118       char *key = g_strdup (atom_entry_id (entries[i]));
119       char *val = g_strdup (reqs[i]);
120       g_hash_table_replace (ctx->bemap, key, val);
121     }
122 }