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>
33 /* 'private'/'protected' functions */
34 static void modest_cache_mgr_class_init (ModestCacheMgrClass *klass);
35 static void modest_cache_mgr_init (ModestCacheMgr *obj);
36 static void modest_cache_mgr_finalize (GObject *obj);
44 typedef struct _ModestCacheMgrPrivate ModestCacheMgrPrivate;
45 struct _ModestCacheMgrPrivate {
46 GHashTable *date_str_cache;
47 GHashTable *display_str_cache;
48 GHashTable *pixbuf_cache;
50 #define MODEST_CACHE_MGR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
51 MODEST_TYPE_CACHE_MGR, \
52 ModestCacheMgrPrivate))
54 static GObjectClass *parent_class = NULL;
56 /* uncomment the following if you have defined any signals */
57 /* static guint signals[LAST_SIGNAL] = {0}; */
60 modest_cache_mgr_get_type (void)
62 static GType my_type = 0;
64 static const GTypeInfo my_info = {
65 sizeof(ModestCacheMgrClass),
67 NULL, /* base finalize */
68 (GClassInitFunc) modest_cache_mgr_class_init,
69 NULL, /* class finalize */
70 NULL, /* class data */
71 sizeof(ModestCacheMgr),
73 (GInstanceInitFunc) modest_cache_mgr_init,
76 my_type = g_type_register_static (G_TYPE_OBJECT,
84 modest_cache_mgr_class_init (ModestCacheMgrClass *klass)
86 GObjectClass *gobject_class;
87 gobject_class = (GObjectClass*) klass;
89 parent_class = g_type_class_peek_parent (klass);
90 gobject_class->finalize = modest_cache_mgr_finalize;
92 g_type_class_add_private (gobject_class, sizeof(ModestCacheMgrPrivate));
97 void pixbuf_unref (GObject *pixbuf)
100 g_object_unref (pixbuf);
104 modest_cache_mgr_init (ModestCacheMgr *obj)
106 ModestCacheMgrPrivate *priv;
108 priv = MODEST_CACHE_MGR_GET_PRIVATE(obj);
110 priv->date_str_cache =
111 g_hash_table_new_full (g_int_hash, /* time_t */
113 NULL, /* int -> no need to free */
114 g_free); /* gchar* */
115 priv->display_str_cache =
116 g_hash_table_new_full (g_str_hash, /* gchar* */
119 g_free); /* gchar* */
121 g_hash_table_new_full (g_str_hash, /* gchar* */
124 (GDestroyNotify)pixbuf_unref);
129 modest_cache_mgr_finalize (GObject *obj)
131 ModestCacheMgr *self;
132 self = MODEST_CACHE_MGR(obj);
134 ModestCacheMgrPrivate *priv;
135 priv = MODEST_CACHE_MGR_GET_PRIVATE(obj);
137 modest_cache_mgr_flush_all (self);
139 priv->date_str_cache = NULL;
140 priv->display_str_cache = NULL;
141 priv->pixbuf_cache = NULL;
143 G_OBJECT_CLASS(parent_class)->finalize (obj);
147 get_cache (ModestCacheMgrPrivate *priv, ModestCacheMgrCacheType type)
150 case MODEST_CACHE_MGR_CACHE_TYPE_DATE_STRING:
151 return priv->date_str_cache;
152 case MODEST_CACHE_MGR_CACHE_TYPE_DISPLAY_STRING:
153 return priv->display_str_cache;
154 case MODEST_CACHE_MGR_CACHE_TYPE_PIXBUF:
155 return priv->pixbuf_cache;
157 g_return_val_if_reached(NULL); /* should not happen */
163 modest_cache_mgr_new (void)
165 return MODEST_CACHE_MGR(g_object_new(MODEST_TYPE_CACHE_MGR, NULL));
170 modest_cache_mgr_get_cache (ModestCacheMgr* self, ModestCacheMgrCacheType type)
172 ModestCacheMgrPrivate *priv;
175 g_return_val_if_fail (self, NULL);
176 g_return_val_if_fail (type >= 0 && type <= MODEST_CACHE_MGR_CACHE_TYPE_NUM, NULL);
178 priv = MODEST_CACHE_MGR_GET_PRIVATE(self);
180 cache = get_cache (priv, type);
186 always_true (gpointer key, gpointer value, gpointer user_data)
193 modest_cache_mgr_flush (ModestCacheMgr *self, ModestCacheMgrCacheType type)
195 ModestCacheMgrPrivate *priv;
198 g_return_if_fail (self);
199 g_return_if_fail (type >= 0 && type <= MODEST_CACHE_MGR_CACHE_TYPE_NUM);
201 priv = MODEST_CACHE_MGR_GET_PRIVATE(self);
203 cache = get_cache (priv, type);
205 g_hash_table_foreach_remove (cache, always_true, NULL);
206 /* g_hash_table_remove_all (cache) in only available since GLIB 2.12 */
211 modest_cache_mgr_flush_all (ModestCacheMgr *self)
214 g_return_if_fail (self);
216 for (i = 0; i != MODEST_CACHE_MGR_CACHE_TYPE_NUM; ++i)
217 modest_cache_mgr_flush (self, i);
222 modest_cache_mgr_get_size (ModestCacheMgr *self, ModestCacheMgrCacheType type)
224 ModestCacheMgrPrivate *priv;
227 priv = MODEST_CACHE_MGR_GET_PRIVATE(self);
229 g_return_val_if_fail (self, 0);
230 g_return_val_if_fail (type >= 0 && type <= MODEST_CACHE_MGR_CACHE_TYPE_NUM, 0);
232 cache = get_cache (priv, type);
233 return cache ? g_hash_table_size (cache) : 0;