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