* src/modest-text-utils.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 #include <stdio.h>
33
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);
38 /* list my signals  */
39 enum {
40         /* MY_SIGNAL_1, */
41         /* MY_SIGNAL_2, */
42         LAST_SIGNAL
43 };
44
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;
51 };
52 #define MODEST_CACHE_MGR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
53                                               MODEST_TYPE_CACHE_MGR, \
54                                               ModestCacheMgrPrivate))
55 /* globals */
56 static GObjectClass *parent_class = NULL;
57
58 /* uncomment the following if you have defined any signals */
59 /* static guint signals[LAST_SIGNAL] = {0}; */
60
61 GType
62 modest_cache_mgr_get_type (void)
63 {
64         static GType my_type = 0;
65         if (!my_type) {
66                 static const GTypeInfo my_info = {
67                         sizeof(ModestCacheMgrClass),
68                         NULL,           /* base init */
69                         NULL,           /* base finalize */
70                         (GClassInitFunc) modest_cache_mgr_class_init,
71                         NULL,           /* class finalize */
72                         NULL,           /* class data */
73                         sizeof(ModestCacheMgr),
74                         0,              /* n_preallocs */
75                         (GInstanceInitFunc) modest_cache_mgr_init,
76                         NULL
77                 };
78                 my_type = g_type_register_static (G_TYPE_OBJECT,
79                                                   "ModestCacheMgr",
80                                                   &my_info, 0);
81         }
82         return my_type;
83 }
84
85 static void
86 modest_cache_mgr_class_init (ModestCacheMgrClass *klass)
87 {
88         GObjectClass *gobject_class;
89         gobject_class = (GObjectClass*) klass;
90
91         parent_class            = g_type_class_peek_parent (klass);
92         gobject_class->finalize = modest_cache_mgr_finalize;
93
94         g_type_class_add_private (gobject_class, sizeof(ModestCacheMgrPrivate));
95 }
96
97
98 static
99 void my_object_unref (GObject *obj)
100 {
101         if (obj)
102                 g_object_unref (obj);
103 }
104
105 static void
106 modest_cache_mgr_init (ModestCacheMgr *obj)
107 {
108         ModestCacheMgrPrivate *priv;
109
110         priv = MODEST_CACHE_MGR_GET_PRIVATE(obj);
111         
112         priv->date_str_cache =
113                 g_hash_table_new_full (g_int_hash,  /* time_t */
114                                        g_int_equal,
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* */
119                                        g_str_equal,
120                                        g_free,      /* gchar* */
121                                        g_free);     /* gchar* */
122         priv->pixbuf_cache =
123                 g_hash_table_new_full (g_str_hash,   /* gchar* */
124                                        g_str_equal,  
125                                        g_free,       /* gchar*/
126                                        (GDestroyNotify)my_object_unref);
127         priv->send_queue_cache =
128                 g_hash_table_new_full (g_direct_hash,   /* ptr */
129                                        g_direct_equal,  
130                                        (GDestroyNotify)my_object_unref,   /* ref'd GObject */
131                                        (GDestroyNotify)my_object_unref);   /* ref'd GObject */  
132
133 }
134
135
136 static void
137 modest_cache_mgr_finalize (GObject *obj)
138 {
139         ModestCacheMgr *self;
140         self = MODEST_CACHE_MGR(obj);
141         
142         ModestCacheMgrPrivate *priv;
143         priv = MODEST_CACHE_MGR_GET_PRIVATE(obj);
144         
145         modest_cache_mgr_flush_all (self);
146         
147         priv->date_str_cache    = NULL;
148         priv->display_str_cache = NULL;
149         priv->pixbuf_cache      = NULL;
150         priv->send_queue_cache  = NULL;
151
152         G_OBJECT_CLASS(parent_class)->finalize (obj);
153 }
154
155 static GHashTable*
156 get_cache (ModestCacheMgrPrivate *priv, ModestCacheMgrCacheType type)
157 {
158         switch (type) {
159         case MODEST_CACHE_MGR_CACHE_TYPE_DATE_STRING:
160                 return priv->date_str_cache;
161         case MODEST_CACHE_MGR_CACHE_TYPE_DISPLAY_STRING:
162                 return priv->display_str_cache;
163         case MODEST_CACHE_MGR_CACHE_TYPE_PIXBUF:
164                 return priv->pixbuf_cache;
165         case MODEST_CACHE_MGR_CACHE_TYPE_SEND_QUEUE:
166                 return priv->send_queue_cache;  
167         default:
168                 g_return_val_if_reached(NULL); /* should not happen */
169         }
170 }
171
172
173 ModestCacheMgr*
174 modest_cache_mgr_new (void)
175 {
176         return MODEST_CACHE_MGR(g_object_new(MODEST_TYPE_CACHE_MGR, NULL));
177 }
178
179
180 GHashTable*
181 modest_cache_mgr_get_cache   (ModestCacheMgr* self, ModestCacheMgrCacheType type)
182 {
183         ModestCacheMgrPrivate *priv;
184         GHashTable *cache;
185         
186         g_return_val_if_fail (self, NULL);
187         
188         if (!(type >= 0 && type <= MODEST_CACHE_MGR_CACHE_TYPE_NUM)) {
189                 printf ("DEBUG: %s: incorrect type = %d\n", __FUNCTION__, type);        
190         }
191         
192         g_return_val_if_fail (type >= 0 && type <= MODEST_CACHE_MGR_CACHE_TYPE_NUM, NULL);
193
194         priv  = MODEST_CACHE_MGR_GET_PRIVATE(self);
195         
196         cache = get_cache (priv, type);
197         return cache;
198 }
199
200
201 static gboolean
202 always_true (gpointer key, gpointer value, gpointer user_data)
203 {
204         return TRUE;
205 }
206
207
208 void
209 modest_cache_mgr_flush (ModestCacheMgr *self, ModestCacheMgrCacheType type)
210 {
211         ModestCacheMgrPrivate *priv;
212         GHashTable *cache;
213         
214         g_return_if_fail (self);
215         g_return_if_fail (type >= 0 && type <= MODEST_CACHE_MGR_CACHE_TYPE_NUM);
216
217         priv  = MODEST_CACHE_MGR_GET_PRIVATE(self);
218
219         cache = get_cache (priv, type);
220         if (cache)
221                 g_hash_table_foreach_remove (cache, always_true, NULL);
222         /*  g_hash_table_remove_all (cache) in only available since GLIB 2.12 */
223 }
224
225
226 void
227 modest_cache_mgr_flush_all (ModestCacheMgr *self)
228 {
229         int i;
230         g_return_if_fail (self);
231
232         for (i = 0; i != MODEST_CACHE_MGR_CACHE_TYPE_NUM; ++i)
233                 modest_cache_mgr_flush (self, i);       
234 }
235
236
237 guint
238 modest_cache_mgr_get_size (ModestCacheMgr *self, ModestCacheMgrCacheType type)
239 {
240         ModestCacheMgrPrivate *priv;
241         GHashTable *cache;
242         
243         priv = MODEST_CACHE_MGR_GET_PRIVATE(self);
244
245         g_return_val_if_fail (self, 0);
246         g_return_val_if_fail (type >= 0 && type <= MODEST_CACHE_MGR_CACHE_TYPE_NUM, 0);
247
248         cache = get_cache (priv, type);
249         return cache ? g_hash_table_size (cache) : 0;
250 }