1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the Nokia Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <modest-cache-mgr.h>
34 /* 'private'/'protected' functions */
35 static void modest_cache_mgr_class_init (ModestCacheMgrClass *klass);
36 static void modest_cache_mgr_init (ModestCacheMgr *obj);
37 static void modest_cache_mgr_finalize (GObject *obj);
45 typedef struct _ModestCacheMgrPrivate ModestCacheMgrPrivate;
46 struct _ModestCacheMgrPrivate {
47 GHashTable *date_str_cache;
48 GHashTable *display_str_cache;
49 GHashTable *pixbuf_cache;
50 GHashTable *send_queue_cache;
52 #define MODEST_CACHE_MGR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
53 MODEST_TYPE_CACHE_MGR, \
54 ModestCacheMgrPrivate))
56 static GObjectClass *parent_class = NULL;
58 /* uncomment the following if you have defined any signals */
59 /* static guint signals[LAST_SIGNAL] = {0}; */
62 modest_cache_mgr_get_type (void)
64 static GType my_type = 0;
66 static const GTypeInfo my_info = {
67 sizeof(ModestCacheMgrClass),
69 NULL, /* base finalize */
70 (GClassInitFunc) modest_cache_mgr_class_init,
71 NULL, /* class finalize */
72 NULL, /* class data */
73 sizeof(ModestCacheMgr),
75 (GInstanceInitFunc) modest_cache_mgr_init,
78 my_type = g_type_register_static (G_TYPE_OBJECT,
86 modest_cache_mgr_class_init (ModestCacheMgrClass *klass)
88 GObjectClass *gobject_class;
89 gobject_class = (GObjectClass*) klass;
91 parent_class = g_type_class_peek_parent (klass);
92 gobject_class->finalize = modest_cache_mgr_finalize;
94 g_type_class_add_private (gobject_class, sizeof(ModestCacheMgrPrivate));
99 void my_object_unref (GObject *obj)
102 g_object_unref (obj);
106 modest_cache_mgr_init (ModestCacheMgr *obj)
108 ModestCacheMgrPrivate *priv;
110 priv = MODEST_CACHE_MGR_GET_PRIVATE(obj);
112 priv->date_str_cache =
113 g_hash_table_new_full (g_int_hash, /* time_t */
115 NULL, /* int -> no need to free */
116 g_free); /* gchar* */
117 priv->display_str_cache =
118 g_hash_table_new_full (g_str_hash, /* gchar* */
121 g_free); /* gchar* */
123 g_hash_table_new_full (g_str_hash, /* gchar* */
126 (GDestroyNotify)my_object_unref);
127 priv->send_queue_cache =
128 g_hash_table_new_full (g_direct_hash, /* ptr */
130 (GDestroyNotify)my_object_unref, /* ref'd GObject */
131 (GDestroyNotify)my_object_unref); /* ref'd GObject */
136 modest_cache_mgr_finalize (GObject *obj)
138 ModestCacheMgr *self;
139 self = MODEST_CACHE_MGR(obj);
141 ModestCacheMgrPrivate *priv;
142 priv = MODEST_CACHE_MGR_GET_PRIVATE(obj);
144 modest_cache_mgr_flush_all (self);
146 priv->date_str_cache = NULL;
147 priv->display_str_cache = NULL;
148 priv->pixbuf_cache = NULL;
149 priv->send_queue_cache = NULL;
151 G_OBJECT_CLASS(parent_class)->finalize (obj);
155 get_cache (ModestCacheMgrPrivate *priv, ModestCacheMgrCacheType type)
158 case MODEST_CACHE_MGR_CACHE_TYPE_DATE_STRING:
159 return priv->date_str_cache;
160 case MODEST_CACHE_MGR_CACHE_TYPE_DISPLAY_STRING:
161 return priv->display_str_cache;
162 case MODEST_CACHE_MGR_CACHE_TYPE_PIXBUF:
163 return priv->pixbuf_cache;
164 case MODEST_CACHE_MGR_CACHE_TYPE_SEND_QUEUE:
165 return priv->send_queue_cache;
167 g_return_val_if_reached(NULL); /* should not happen */
173 modest_cache_mgr_new (void)
175 return MODEST_CACHE_MGR(g_object_new(MODEST_TYPE_CACHE_MGR, NULL));
180 modest_cache_mgr_get_cache (ModestCacheMgr* self, ModestCacheMgrCacheType type)
182 ModestCacheMgrPrivate *priv;
185 g_return_val_if_fail (self, NULL);
187 if (!(type >= 0 && type <= MODEST_CACHE_MGR_CACHE_TYPE_NUM)) {
188 printf ("DEBUG: %s: incorrect type = %d\n", __FUNCTION__, type);
191 g_return_val_if_fail (type >= 0 && type <= MODEST_CACHE_MGR_CACHE_TYPE_NUM, NULL);
193 priv = MODEST_CACHE_MGR_GET_PRIVATE(self);
195 cache = get_cache (priv, type);
201 modest_cache_mgr_flush (ModestCacheMgr *self, ModestCacheMgrCacheType type)
203 ModestCacheMgrPrivate *priv;
206 g_return_if_fail (self);
207 g_return_if_fail (type >= 0 && type <= MODEST_CACHE_MGR_CACHE_TYPE_NUM);
209 priv = MODEST_CACHE_MGR_GET_PRIVATE(self);
211 cache = get_cache (priv, type);
213 g_hash_table_destroy (cache);
218 modest_cache_mgr_flush_all (ModestCacheMgr *self)
221 g_return_if_fail (self);
223 for (i = 0; i != MODEST_CACHE_MGR_CACHE_TYPE_NUM; ++i)
224 modest_cache_mgr_flush (self, i);
229 modest_cache_mgr_get_size (ModestCacheMgr *self, ModestCacheMgrCacheType type)
231 ModestCacheMgrPrivate *priv;
234 priv = MODEST_CACHE_MGR_GET_PRIVATE(self);
236 g_return_val_if_fail (self, 0);
237 g_return_val_if_fail (type >= 0 && type <= MODEST_CACHE_MGR_CACHE_TYPE_NUM, 0);
239 cache = get_cache (priv, type);
240 return cache ? g_hash_table_size (cache) : 0;