[evolution] Add mail_folder_cache_ref_folder().



commit c36aa6b7498ac73a77624533516b010f5701f08b
Author: Matthew Barnes <mbarnes redhat com>
Date:   Mon Jul 15 10:38:11 2013 -0400

    Add mail_folder_cache_ref_folder().
    
    Replaces mail_folder_cache_get_folder_from_uri().
    
    Returns the CamelFolder for the CamelStore and folder name if available,
    or else NULL if a CamelFolder instance is not yet cached.  This function
    does not block.

 libemail-engine/mail-folder-cache.c |   50 +++++++++++++++++++++--------------
 libemail-engine/mail-folder-cache.h |    7 ++---
 mail/e-mail-backend.c               |   29 ++++++++------------
 mail/em-folder-tree-model.c         |   17 ++++++-----
 4 files changed, 54 insertions(+), 49 deletions(-)
---
diff --git a/libemail-engine/mail-folder-cache.c b/libemail-engine/mail-folder-cache.c
index bf4e8b3..5b99f34 100644
--- a/libemail-engine/mail-folder-cache.c
+++ b/libemail-engine/mail-folder-cache.c
@@ -1666,36 +1666,46 @@ mail_folder_cache_has_folder_info (MailFolderCache *cache,
 }
 
 /**
- * mail_folder_cache_get_folder_from_uri:
+ * mail_folder_cache_ref_folder:
+ * @cache: a #MailFolderCache
+ * @store: a #CamelStore
+ * @folder_name: a folder name
  *
- * Gets the #CamelFolder for the supplied @uri.
+ * Returns the #CamelFolder for @store and @folder_name if available, or
+ * else %NULL if a #CamelFolder instance is not yet cached.  This function
+ * does not block.
  *
- * Returns: %TRUE if the URI is available, folderp is set to a reffed
- *          folder if the folder has also already been opened
- */
-gboolean
-mail_folder_cache_get_folder_from_uri (MailFolderCache *cache,
-                                       const gchar *uri,
-                                       CamelFolder **folderp)
+ * The returned #CamelFolder is referenced for thread-safety and must be
+ * unreferenced with g_object_unref() when finished with it.
+ *
+ * Returns: a #CamelFolder, or %NULL
+ **/
+CamelFolder *
+mail_folder_cache_ref_folder (MailFolderCache *cache,
+                              CamelStore *store,
+                              const gchar *folder_name)
 {
-       struct _find_info fi = { uri, NULL };
+       StoreInfo *si;
+       struct _folder_info *fi;
+       CamelFolder *folder = NULL;
+
+       g_return_val_if_fail (MAIL_IS_FOLDER_CACHE (cache), NULL);
+       g_return_val_if_fail (CAMEL_IS_STORE (store), NULL);
+       g_return_val_if_fail (folder_name != NULL, NULL);
 
        if (cache->priv->stores == NULL)
-               return FALSE;
+               return NULL;
 
        g_rec_mutex_lock (&cache->priv->stores_mutex);
-       g_hash_table_foreach (
-               cache->priv->stores, (GHFunc)
-               storeinfo_find_folder_info, &fi);
-       if (folderp) {
-               if (fi.fi && fi.fi->folder)
-                       *folderp = g_object_ref (fi.fi->folder);
-               else
-                       *folderp = NULL;
+       si = g_hash_table_lookup (cache->priv->stores, store);
+       if (si != NULL) {
+               fi = g_hash_table_lookup (si->folders, folder_name);
+               if (fi != NULL && fi->folder != NULL)
+                       folder = g_object_ref (fi->folder);
        }
        g_rec_mutex_unlock (&cache->priv->stores_mutex);
 
-       return fi.fi != NULL;
+       return folder;
 }
 
 gboolean
diff --git a/libemail-engine/mail-folder-cache.h b/libemail-engine/mail-folder-cache.h
index 02c305e..2805021 100644
--- a/libemail-engine/mail-folder-cache.h
+++ b/libemail-engine/mail-folder-cache.h
@@ -121,10 +121,9 @@ gboolean   mail_folder_cache_has_folder_info
                                                (MailFolderCache *cache,
                                                 CamelStore *store,
                                                 const gchar *folder_name);
-gboolean       mail_folder_cache_get_folder_from_uri
-                                               (MailFolderCache *cache,
-                                                const gchar *uri,
-                                                CamelFolder **folderp);
+CamelFolder *  mail_folder_cache_ref_folder    (MailFolderCache *cache,
+                                                CamelStore *store,
+                                                const gchar *folder_name);
 gboolean       mail_folder_cache_get_folder_info_flags
                                                (MailFolderCache *cache,
                                                 CamelFolder *folder,
diff --git a/mail/e-mail-backend.c b/mail/e-mail-backend.c
index d1ca660..c2952da 100644
--- a/mail/e-mail-backend.c
+++ b/mail/e-mail-backend.c
@@ -673,35 +673,30 @@ mail_backend_folder_changed_cb (MailFolderCache *folder_cache,
        EMEvent *event = em_event_peek ();
        EMEventTargetFolder *target;
        EMFolderTreeModel *model;
+       CamelFolder *folder;
        gchar *folder_uri;
        gint folder_type;
        CamelFolderInfoFlags flags = 0;
 
        folder_uri = e_mail_folder_uri_build (store, folder_name);
 
-       if (folder_uri != NULL) {
-               CamelFolder *folder = NULL;
-
-               if (mail_folder_cache_get_folder_from_uri (
-                               folder_cache, folder_uri, &folder)) {
-                       if (folder != NULL &&
-                               !mail_folder_cache_get_folder_info_flags (
-                               folder_cache, folder, &flags)) {
-                               g_free (folder_uri);
-                               g_return_if_reached ();
-                       }
-               }
-
-               if (folder != NULL)
-                       g_object_unref (folder);
+       folder = mail_folder_cache_ref_folder (
+               folder_cache, store, folder_name);
+       if (folder != NULL) {
+               /* XXX Need to rethink this API.  Why should we
+                *     need the CamelFolder instance just to get
+                *     folder flags?  The flags are more readily
+                *     available than the CamelFolder instance. */
+               mail_folder_cache_get_folder_info_flags (
+                       folder_cache, folder, &flags);
+               g_object_unref (folder);
        }
 
        target = em_event_target_new_folder (
                event, store, folder_uri, new_messages,
                msg_uid, msg_sender, msg_subject);
 
-       if (folder_uri)
-               g_free (folder_uri);
+       g_free (folder_uri);
 
        folder_type = (flags & CAMEL_FOLDER_TYPE_MASK);
        target->is_inbox = (folder_type == CAMEL_FOLDER_TYPE_INBOX);
diff --git a/mail/em-folder-tree-model.c b/mail/em-folder-tree-model.c
index eaa6b92..66f2926 100644
--- a/mail/em-folder-tree-model.c
+++ b/mail/em-folder-tree-model.c
@@ -748,22 +748,23 @@ em_folder_tree_model_set_folder_info (EMFolderTreeModel *model,
        /* XXX This is duplicated in mail-folder-cache too, should perhaps
         *     be functionised. */
        unread = fi->unread;
-       if (mail_folder_cache_get_folder_from_uri (
-               folder_cache, uri, &folder) && folder) {
+       folder = mail_folder_cache_ref_folder (
+               folder_cache, si->store, fi->full_name);
+       if (folder != NULL) {
                folder_is_drafts = em_utils_folder_is_drafts (registry, folder);
                folder_is_outbox = em_utils_folder_is_outbox (registry, folder);
 
                if (folder_is_drafts || folder_is_outbox) {
                        gint total;
+                       gint deleted;
 
-                       if ((total = camel_folder_get_message_count (folder)) > 0) {
-                               gint deleted = camel_folder_get_deleted_message_count (folder);
+                       total = camel_folder_get_message_count (folder);
+                       deleted = camel_folder_get_deleted_message_count (folder);
 
-                               if (deleted != -1)
-                                       total -= deleted;
-                       }
+                       if (total > 0 && deleted != -1)
+                               total -= deleted;
 
-                       unread = total > 0 ? total : 0;
+                       unread = MAX (total, 0);
                }
 
                g_object_unref (folder);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]