* Makefile.am, modest-cache-mgr.[ch]:
authorDirk-Jan C. Binnema <dirk-jan.binnema@nokia.com>
Mon, 22 Jan 2007 00:05:21 +0000 (00:05 +0000)
committerDirk-Jan C. Binnema <dirk-jan.binnema@nokia.com>
Mon, 22 Jan 2007 00:05:21 +0000 (00:05 +0000)
- add the modest cache manager

pmo-trunk-r681

src/Makefile.am
src/modest-cache-mgr.c [new file with mode: 0644]
src/modest-cache-mgr.h [new file with mode: 0644]

index a876e01..ab3af92 100644 (file)
@@ -1,6 +1,6 @@
 #
 # Makefile.am
-# Time-stamp: <2007-01-17 09:44:06 (djcb)>
+# Time-stamp: <2007-01-21 21:41:46 (djcb)>
 SUBDIRS=$(MODEST_PLATFORM) widgets
 DIST_SUBDIRS = widgets gtk maemo
 
@@ -32,6 +32,8 @@ bin_PROGRAMS=\
 modest_SOURCES=\
        modest-account-mgr.c\
        modest-account-mgr.h\
+       modest-cache-mgr.c\
+       modest-cache-mgr.h\
        modest-conf.c\
        modest-conf.h \
        modest-debug.c\
diff --git a/src/modest-cache-mgr.c b/src/modest-cache-mgr.c
new file mode 100644 (file)
index 0000000..3097e26
--- /dev/null
@@ -0,0 +1,226 @@
+/* Copyright (c) 2006, Nokia Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * * Neither the name of the Nokia Corporation nor the names of its
+ *   contributors may be used to endorse or promote products derived from
+ *   this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <config.h>
+#include <modest-cache-mgr.h>
+
+/* 'private'/'protected' functions */
+static void modest_cache_mgr_class_init (ModestCacheMgrClass *klass);
+static void modest_cache_mgr_init       (ModestCacheMgr *obj);
+static void modest_cache_mgr_finalize   (GObject *obj);
+/* list my signals  */
+enum {
+       /* MY_SIGNAL_1, */
+       /* MY_SIGNAL_2, */
+       LAST_SIGNAL
+};
+
+typedef struct _ModestCacheMgrPrivate ModestCacheMgrPrivate;
+struct _ModestCacheMgrPrivate {
+       GHashTable *date_str_cache;
+       GHashTable *display_str_cache;
+       GHashTable *pixbuf_cache;
+};
+#define MODEST_CACHE_MGR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
+                                              MODEST_TYPE_CACHE_MGR, \
+                                              ModestCacheMgrPrivate))
+/* globals */
+static GObjectClass *parent_class = NULL;
+
+/* uncomment the following if you have defined any signals */
+/* static guint signals[LAST_SIGNAL] = {0}; */
+
+GType
+modest_cache_mgr_get_type (void)
+{
+       static GType my_type = 0;
+       if (!my_type) {
+               static const GTypeInfo my_info = {
+                       sizeof(ModestCacheMgrClass),
+                       NULL,           /* base init */
+                       NULL,           /* base finalize */
+                       (GClassInitFunc) modest_cache_mgr_class_init,
+                       NULL,           /* class finalize */
+                       NULL,           /* class data */
+                       sizeof(ModestCacheMgr),
+                       1,              /* n_preallocs */
+                       (GInstanceInitFunc) modest_cache_mgr_init,
+                       NULL
+               };
+               my_type = g_type_register_static (G_TYPE_OBJECT,
+                                                 "ModestCacheMgr",
+                                                 &my_info, 0);
+       }
+       return my_type;
+}
+
+static void
+modest_cache_mgr_class_init (ModestCacheMgrClass *klass)
+{
+       GObjectClass *gobject_class;
+       gobject_class = (GObjectClass*) klass;
+
+       parent_class            = g_type_class_peek_parent (klass);
+       gobject_class->finalize = modest_cache_mgr_finalize;
+
+       g_type_class_add_private (gobject_class, sizeof(ModestCacheMgrPrivate));
+}
+
+
+static
+void pixbuf_unref (GObject *pixbuf)
+{
+       if (pixbuf)
+               g_object_unref (pixbuf);
+}
+
+static void
+modest_cache_mgr_init (ModestCacheMgr *obj)
+{
+       ModestCacheMgrPrivate *priv;
+
+       priv = MODEST_CACHE_MGR_GET_PRIVATE(obj);
+       
+       priv->date_str_cache =
+               g_hash_table_new_full (g_int_hash,  /* time_t */
+                                      g_int_equal,
+                                      NULL,        /* int -> no need to free */
+                                      g_free);     /* gchar* */
+       priv->display_str_cache =
+               g_hash_table_new_full (g_str_hash,  /* gchar* */
+                                      g_str_equal,
+                                      g_free,      /* gchar* */
+                                      g_free);     /* gchar* */
+       priv->pixbuf_cache =
+               g_hash_table_new_full (g_str_hash,   /* gchar* */
+                                      g_str_equal,  
+                                      g_free,       /* gchar*/
+                                      (GDestroyNotify)pixbuf_unref);
+}
+
+
+static void
+modest_cache_mgr_finalize (GObject *obj)
+{
+       ModestCacheMgr *self;
+       self = MODEST_CACHE_MGR(obj);
+       
+       ModestCacheMgrPrivate *priv;
+       priv = MODEST_CACHE_MGR_GET_PRIVATE(obj);
+       
+       modest_cache_mgr_flush_all (self);
+       
+       priv->date_str_cache    = NULL;
+       priv->display_str_cache = NULL;
+       priv->pixbuf_cache      = NULL;
+
+       G_OBJECT_CLASS(parent_class)->finalize (obj);
+}
+
+static GHashTable*
+get_cache (ModestCacheMgrPrivate *priv, ModestCacheMgrCacheType type)
+{
+       switch (type) {
+       case MODEST_CACHE_MGR_CACHE_TYPE_DATE_STRING:
+               return priv->date_str_cache;
+       case MODEST_CACHE_MGR_CACHE_TYPE_DISPLAY_STRING:
+               return priv->display_str_cache;
+       case MODEST_CACHE_MGR_CACHE_TYPE_PIXBUF:
+               return priv->pixbuf_cache;
+       default:
+               g_return_val_if_reached(NULL); /* should not happen */
+       }
+}
+
+
+ModestCacheMgr*
+modest_cache_mgr_new (void)
+{
+       return MODEST_CACHE_MGR(g_object_new(MODEST_TYPE_CACHE_MGR, NULL));
+}
+
+
+GHashTable*
+modest_cache_mgr_get_cache   (ModestCacheMgr* self, ModestCacheMgrCacheType type)
+{
+       ModestCacheMgrPrivate *priv;
+       GHashTable *cache;
+       
+       g_return_val_if_fail (self, NULL);
+       g_return_val_if_fail (type >= 0 && type <= MODEST_CACHE_MGR_CACHE_TYPE_NUM, NULL);
+
+       priv  = MODEST_CACHE_MGR_GET_PRIVATE(self);
+       
+       cache = get_cache (priv, type);
+       return cache;
+}
+
+
+void
+modest_cache_mgr_flush (ModestCacheMgr *self, ModestCacheMgrCacheType type)
+{
+       ModestCacheMgrPrivate *priv;
+       GHashTable *cache;
+       
+       g_return_if_fail (self);
+       g_return_if_fail (type >= 0 && type <= MODEST_CACHE_MGR_CACHE_TYPE_NUM);
+
+       priv  = MODEST_CACHE_MGR_GET_PRIVATE(self);
+       cache = get_cache (priv, type);
+       
+       if (cache)
+               g_hash_table_remove_all (cache);
+}
+
+
+void
+modest_cache_mgr_flush_all (ModestCacheMgr *self)
+{
+       int i;
+       g_return_if_fail (self);
+
+       for (i = 0; i != MODEST_CACHE_MGR_CACHE_TYPE_NUM; ++i)
+               modest_cache_mgr_flush (self, i);       
+}
+
+
+guint
+modest_cache_mgr_get_size (ModestCacheMgr *self, ModestCacheMgrCacheType type)
+{
+       ModestCacheMgrPrivate *priv;
+       GHashTable *cache;
+       
+       priv = MODEST_CACHE_MGR_GET_PRIVATE(self);
+
+       g_return_val_if_fail (self, 0);
+       g_return_val_if_fail (type >= 0 && type <= MODEST_CACHE_MGR_CACHE_TYPE_NUM, 0);
+
+       cache = get_cache (priv, type);
+       return cache ? g_hash_table_size (cache) : 0;
+}
diff --git a/src/modest-cache-mgr.h b/src/modest-cache-mgr.h
new file mode 100644 (file)
index 0000000..32205e8
--- /dev/null
@@ -0,0 +1,137 @@
+/* Copyright (c) 2006, Nokia Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * * Neither the name of the Nokia Corporation nor the names of its
+ *   contributors may be used to endorse or promote products derived from
+ *   this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+
+#ifndef __MODEST_CACHE_MGR_H__
+#define __MODEST_CACHE_MGR_H__
+
+#include <glib-object.h>
+G_BEGIN_DECLS
+
+/* convenience macros */
+#define MODEST_TYPE_CACHE_MGR         (modest_cache_mgr_get_type())
+#define MODEST_CACHE_MGR(obj)         (G_TYPE_CHECK_INSTANCE_CAST((obj),MODEST_TYPE_CACHE_MGR,ModestCacheMgr))
+#define MODEST_CACHE_MGR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),MODEST_TYPE_CACHE_MGR,GObject))
+#define MODEST_IS_CACHE_MGR(obj)          (G_TYPE_CHECK_INSTANCE_TYPE((obj),MODEST_TYPE_CACHE_MGR))
+#define MODEST_IS_CACHE_MGR_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE((klass),MODEST_TYPE_CACHE_MGR))
+#define MODEST_CACHE_MGR_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS((obj),MODEST_TYPE_CACHE_MGR,ModestCacheMgrClass))
+
+typedef struct _ModestCacheMgr      ModestCacheMgr;
+typedef struct _ModestCacheMgrClass ModestCacheMgrClass;
+
+struct _ModestCacheMgr {
+        GObject parent;
+       /* insert public members, if any */
+};
+
+struct _ModestCacheMgrClass {
+       GObjectClass parent_class;
+};
+
+/*
+ * the caches managed by this class
+ */
+typedef enum {
+       MODEST_CACHE_MGR_CACHE_TYPE_DATE_STRING,       /* time_t => string */
+       MODEST_CACHE_MGR_CACHE_TYPE_DISPLAY_STRING,    /* gchar* => gchar* */
+       MODEST_CACHE_MGR_CACHE_TYPE_PIXBUF,            /* gchar* => GdkPixbuf */
+
+       MODEST_CACHE_MGR_CACHE_TYPE_NUM
+} ModestCacheMgrCacheType;
+
+
+/**
+ * modest_cache_mgr_get_type:
+ * 
+ * get the GType for ModestCacheMgr
+ *  
+ * Returns: the GType
+ */
+GType        modest_cache_mgr_get_type    (void) G_GNUC_CONST;
+
+
+/**
+ * modest_cache_mgr_new:
+ *
+ * instantiate a new cache_mgr object
+ * 
+ * Returns: a new cache_mgr or NULL in case of error
+ */
+ModestCacheMgr*    modest_cache_mgr_new          (void);
+
+/**
+ * modest_cache_mgr_get_cache:
+ * @self: a valid cache mgr obj
+ * @type: a valid cache mgr cache type
+ * 
+ * get the cache (GHashTable) of the requested type
+ * 
+ * Returns: the requested cache (GHashTable) or NULL in case caching
+ * has been disabled (MODEST_DEBUG_DISABLE_CACHE); clients are supposed
+ * to handle that case
+ * 
+ * the returned  hashtable should NOT be destroyed or unref'd.
+ */
+GHashTable*     modest_cache_mgr_get_cache    (ModestCacheMgr* self, ModestCacheMgrCacheType type);
+
+
+/**
+ * modest_cache_mgr_flush
+ * @self: a valid cache mgr obj
+ * @type: a valid cache mgr cache type
+ *
+ * flush the cache (hashtable) of the given type  
+ */
+void            modest_cache_mgr_flush        (ModestCacheMgr *self, ModestCacheMgrCacheType type);
+
+
+/**
+ * modest_cache_mgr_flush
+ * @self: a valid cache mgr obj
+ *
+ * flush all caches  
+ */
+void            modest_cache_mgr_flush_all    (ModestCacheMgr *self);
+
+
+/**
+ * modest_cache_mgr_get_size
+ * @self: a valid cache mgr obj
+ * @type: a valid cache mgr cache type
+ *
+ * get the size (number of <key,value>-pairs) in the cache
+ * 
+ * Returns: the size of the give cache type 
+ */
+guint           modest_cache_mgr_get_size     (ModestCacheMgr *self, ModestCacheMgrCacheType type);
+
+G_END_DECLS
+
+#endif /* __MODEST_CACHE_MGR_H__ */
+