* modest-cache-mgr.c:
[modest] / src / modest-cache-mgr.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29
30 #include <config.h>
31 #include <modest-cache-mgr.h>
32
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);
37 /* list my signals  */
38 enum {
39         /* MY_SIGNAL_1, */
40         /* MY_SIGNAL_2, */
41         LAST_SIGNAL
42 };
43
44 typedef struct _ModestCacheMgrPrivate ModestCacheMgrPrivate;
45 struct _ModestCacheMgrPrivate {
46         GHashTable *date_str_cache;
47         GHashTable *display_str_cache;
48         GHashTable *pixbuf_cache;
49 };
50 #define MODEST_CACHE_MGR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
51                                               MODEST_TYPE_CACHE_MGR, \
52                                               ModestCacheMgrPrivate))
53 /* globals */
54 static GObjectClass *parent_class = NULL;
55
56 /* uncomment the following if you have defined any signals */
57 /* static guint signals[LAST_SIGNAL] = {0}; */
58
59 GType
60 modest_cache_mgr_get_type (void)
61 {
62         static GType my_type = 0;
63         if (!my_type) {
64                 static const GTypeInfo my_info = {
65                         sizeof(ModestCacheMgrClass),
66                         NULL,           /* base init */
67                         NULL,           /* base finalize */
68                         (GClassInitFunc) modest_cache_mgr_class_init,
69                         NULL,           /* class finalize */
70                         NULL,           /* class data */
71                         sizeof(ModestCacheMgr),
72                         1,              /* n_preallocs */
73                         (GInstanceInitFunc) modest_cache_mgr_init,
74                         NULL
75                 };
76                 my_type = g_type_register_static (G_TYPE_OBJECT,
77                                                   "ModestCacheMgr",
78                                                   &my_info, 0);
79         }
80         return my_type;
81 }
82
83 static void
84 modest_cache_mgr_class_init (ModestCacheMgrClass *klass)
85 {
86         GObjectClass *gobject_class;
87         gobject_class = (GObjectClass*) klass;
88
89         parent_class            = g_type_class_peek_parent (klass);
90         gobject_class->finalize = modest_cache_mgr_finalize;
91
92         g_type_class_add_private (gobject_class, sizeof(ModestCacheMgrPrivate));
93 }
94
95
96 static
97 void pixbuf_unref (GObject *pixbuf)
98 {
99         if (pixbuf)
100                 g_object_unref (pixbuf);
101 }
102
103 static void
104 modest_cache_mgr_init (ModestCacheMgr *obj)
105 {
106         ModestCacheMgrPrivate *priv;
107
108         priv = MODEST_CACHE_MGR_GET_PRIVATE(obj);
109         
110         priv->date_str_cache =
111                 g_hash_table_new_full (g_int_hash,  /* time_t */
112                                        g_int_equal,
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* */
117                                        g_str_equal,
118                                        g_free,      /* gchar* */
119                                        g_free);     /* gchar* */
120         priv->pixbuf_cache =
121                 g_hash_table_new_full (g_str_hash,   /* gchar* */
122                                        g_str_equal,  
123                                        g_free,       /* gchar*/
124                                        (GDestroyNotify)pixbuf_unref);
125 }
126
127
128 static void
129 modest_cache_mgr_finalize (GObject *obj)
130 {
131         ModestCacheMgr *self;
132         self = MODEST_CACHE_MGR(obj);
133         
134         ModestCacheMgrPrivate *priv;
135         priv = MODEST_CACHE_MGR_GET_PRIVATE(obj);
136         
137         modest_cache_mgr_flush_all (self);
138         
139         priv->date_str_cache    = NULL;
140         priv->display_str_cache = NULL;
141         priv->pixbuf_cache      = NULL;
142
143         G_OBJECT_CLASS(parent_class)->finalize (obj);
144 }
145
146 static GHashTable*
147 get_cache (ModestCacheMgrPrivate *priv, ModestCacheMgrCacheType type)
148 {
149         switch (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;
156         default:
157                 g_return_val_if_reached(NULL); /* should not happen */
158         }
159 }
160
161
162 ModestCacheMgr*
163 modest_cache_mgr_new (void)
164 {
165         return MODEST_CACHE_MGR(g_object_new(MODEST_TYPE_CACHE_MGR, NULL));
166 }
167
168
169 GHashTable*
170 modest_cache_mgr_get_cache   (ModestCacheMgr* self, ModestCacheMgrCacheType type)
171 {
172         ModestCacheMgrPrivate *priv;
173         GHashTable *cache;
174         
175         g_return_val_if_fail (self, NULL);
176         g_return_val_if_fail (type >= 0 && type <= MODEST_CACHE_MGR_CACHE_TYPE_NUM, NULL);
177
178         priv  = MODEST_CACHE_MGR_GET_PRIVATE(self);
179         
180         cache = get_cache (priv, type);
181         return cache;
182 }
183
184
185 static gboolean
186 always_true (gpointer key, gpointer value, gpointer user_data)
187 {
188         return TRUE;
189 }
190
191
192 void
193 modest_cache_mgr_flush (ModestCacheMgr *self, ModestCacheMgrCacheType type)
194 {
195         ModestCacheMgrPrivate *priv;
196         GHashTable *cache;
197         
198         g_return_if_fail (self);
199         g_return_if_fail (type >= 0 && type <= MODEST_CACHE_MGR_CACHE_TYPE_NUM);
200
201         priv  = MODEST_CACHE_MGR_GET_PRIVATE(self);
202
203         cache = get_cache (priv, type);
204         if (cache)
205                 g_hash_table_foreach_remove (cache, always_true, NULL);
206         /*  g_hash_table_remove_all (cache) in only available since GLIB 2.12 */
207 }
208
209
210 void
211 modest_cache_mgr_flush_all (ModestCacheMgr *self)
212 {
213         int i;
214         g_return_if_fail (self);
215
216         for (i = 0; i != MODEST_CACHE_MGR_CACHE_TYPE_NUM; ++i)
217                 modest_cache_mgr_flush (self, i);       
218 }
219
220
221 guint
222 modest_cache_mgr_get_size (ModestCacheMgr *self, ModestCacheMgrCacheType type)
223 {
224         ModestCacheMgrPrivate *priv;
225         GHashTable *cache;
226         
227         priv = MODEST_CACHE_MGR_GET_PRIVATE(self);
228
229         g_return_val_if_fail (self, 0);
230         g_return_val_if_fail (type >= 0 && type <= MODEST_CACHE_MGR_CACHE_TYPE_NUM, 0);
231
232         cache = get_cache (priv, type);
233         return cache ? g_hash_table_size (cache) : 0;
234 }