[evolution-data-server/wip/camel-more-gobject] Remove confusing camel_folder_summary_insert()



commit b5fa33eef1cd97cc673a81aa3d79380f6d482f01
Author: Milan Crha <mcrha redhat com>
Date:   Wed Sep 14 18:53:26 2016 +0200

    Remove confusing camel_folder_summary_insert()
    
    Also change prototype and behaviour of camel_folder_summary_add(),
    which current adds its own reference to the passed-in info, thus
    the caller is responsible to unref the info after the call.

 camel/camel-folder-summary.c                  |   82 ++++++++++---------------
 camel/camel-folder-summary.h                  |    6 +--
 camel/camel-vee-summary.c                     |    8 +--
 camel/providers/imapx/camel-imapx-server.c    |   14 ++---
 camel/providers/local/camel-local-summary.c   |    2 +-
 camel/providers/local/camel-maildir-folder.c  |    5 +-
 camel/providers/local/camel-maildir-summary.c |    3 +-
 camel/providers/local/camel-mbox-folder.c     |    6 ++-
 camel/providers/local/camel-mbox-summary.c    |    3 +-
 camel/providers/local/camel-mh-folder.c       |    2 +
 camel/providers/local/camel-mh-summary.c      |    3 +-
 camel/providers/nntp/camel-nntp-summary.c     |    6 +-
 12 files changed, 63 insertions(+), 77 deletions(-)
---
diff --git a/camel/camel-folder-summary.c b/camel/camel-folder-summary.c
index 4ff7b14..d5d35ff 100644
--- a/camel/camel-folder-summary.c
+++ b/camel/camel-folder-summary.c
@@ -1804,11 +1804,15 @@ camel_read_mir_callback (gpointer ref,
        if (camel_message_info_load (info, &mir, &bdata_ptr)) {
                /* Just now we are reading from the DB, it can't be dirty. */
                camel_message_info_set_dirty (info, FALSE);
-               if (data->add)
-                       camel_folder_summary_add (summary, info);
-               else
-                       camel_folder_summary_insert (summary, info, TRUE);
-
+               if (data->add) {
+                       camel_folder_summary_add (summary, info, FALSE);
+                       g_clear_object (&info);
+               } else {
+                       camel_folder_summary_lock (summary);
+                       /* Summary always holds a ref for the loaded infos; this consumes it */
+                       g_hash_table_insert (summary->priv->loaded_infos, (gchar *) 
camel_message_info_get_uid (info), info);
+                       camel_folder_summary_unlock (summary);
+               }
        } else {
                g_clear_object (&info);
                g_warning ("Loading messageinfo from db failed");
@@ -2153,30 +2157,44 @@ summary_assign_uid (CamelFolderSummary *summary,
  * camel_folder_summary_add:
  * @summary: a #CamelFolderSummary object
  * @info: a #CamelMessageInfo
+ * @force_keep_uid: whether to keep set UID of the @info
  *
- * Adds a new @info record to the summary.  If @info->uid is %NULL,
+ * Adds a new @info record to the summary. If the @force_keep_uid is %FALSE,
  * then a new uid is automatically re-assigned by calling
- * camel_folder_summary_next_uid_string().
+ * camel_folder_summary_next_uid_string(). It's an error to to use
+ * @force_keep_uid whe nthe @info has none set.
  *
- * The @info record should have been generated by calling one of the
- * info_new_*() functions, as it will be free'd based on the summary
- * class.  And MUST NOT be allocated directly using malloc.
+ * The @summary adds its own reference to @info, if needed.
  **/
 void
 camel_folder_summary_add (CamelFolderSummary *summary,
-                          CamelMessageInfo *info)
+                          CamelMessageInfo *info,
+                         gboolean force_keep_uid)
 {
        g_return_if_fail (CAMEL_IS_FOLDER_SUMMARY (summary));
 
-       if (info == NULL)
+       if (!info)
                return;
 
+       g_return_if_fail (CAMEL_IS_MESSAGE_INFO (info));
+
        camel_folder_summary_lock (summary);
-       if (!summary_assign_uid (summary, info)) {
+       if (!force_keep_uid && !summary_assign_uid (summary, info)) {
                camel_folder_summary_unlock (summary);
                return;
        }
 
+       if (force_keep_uid) {
+               const gchar *uid;
+
+               uid = camel_message_info_get_uid (info);
+               if (!uid || !*uid) {
+                       g_warning ("%s: Cannot add message info without UID, when disabled to assign new UID; 
skipping it", G_STRFUNC);
+                       camel_folder_summary_unlock (summary);
+                       return;
+               }
+       }
+
        folder_summary_update_counts_by_flags (summary, camel_message_info_get_flags (info), 
UPDATE_COUNTS_ADD);
        camel_message_info_set_folder_flagged (info, TRUE);
        camel_message_info_set_dirty (info, TRUE);
@@ -2187,7 +2205,7 @@ camel_folder_summary_add (CamelFolderSummary *summary,
                GUINT_TO_POINTER (camel_message_info_get_flags (info)));
 
        /* Summary always holds a ref for the loaded infos */
-       g_hash_table_insert (summary->priv->loaded_infos, (gpointer) camel_message_info_get_uid (info), info);
+       g_hash_table_insert (summary->priv->loaded_infos, (gpointer) camel_message_info_get_uid (info), 
g_object_ref (info));
 
        camel_folder_summary_touch (summary);
 
@@ -2195,42 +2213,6 @@ camel_folder_summary_add (CamelFolderSummary *summary,
 }
 
 /**
- * camel_folder_summary_insert:
- *
- * Since: 2.24
- **/
-void
-camel_folder_summary_insert (CamelFolderSummary *summary,
-                             CamelMessageInfo *info,
-                             gboolean load)
-{
-       g_return_if_fail (CAMEL_IS_FOLDER_SUMMARY (summary));
-
-       if (info == NULL)
-               return;
-
-       camel_folder_summary_lock (summary);
-
-       if (!load) {
-               folder_summary_update_counts_by_flags (summary, camel_message_info_get_flags (info), 
UPDATE_COUNTS_ADD);
-               camel_message_info_set_folder_flagged (info, TRUE);
-               camel_message_info_set_dirty (info, TRUE);
-
-               g_hash_table_insert (
-                       summary->priv->uids,
-                       (gpointer) camel_pstring_strdup (camel_message_info_get_uid (info)),
-                       GUINT_TO_POINTER (camel_message_info_get_flags (info)));
-
-               camel_folder_summary_touch (summary);
-       }
-
-       /* Summary always holds a ref for the loaded infos */
-       g_hash_table_insert (summary->priv->loaded_infos, (gchar *) camel_message_info_get_uid (info), info);
-
-       camel_folder_summary_unlock (summary);
-}
-
-/**
  * camel_folder_summary_info_new_from_header:
  * @summary: a #CamelFolderSummary object
  * @headers: rfc822 headers
diff --git a/camel/camel-folder-summary.h b/camel/camel-folder-summary.h
index 0e8346e..1a0132d 100644
--- a/camel/camel-folder-summary.h
+++ b/camel/camel-folder-summary.h
@@ -201,12 +201,8 @@ CamelMessageInfo *
 
 /* add a new raw summary item */
 void           camel_folder_summary_add        (CamelFolderSummary *summary,
-                                                CamelMessageInfo *info);
-
-/* insert mi to summary */
-void           camel_folder_summary_insert     (CamelFolderSummary *summary,
                                                 CamelMessageInfo *info,
-                                                gboolean load);
+                                                gboolean force_keep_uid);
 
 gboolean       camel_folder_summary_remove     (CamelFolderSummary *summary,
                                                 CamelMessageInfo *info);
diff --git a/camel/camel-vee-summary.c b/camel/camel-vee-summary.c
index d1c472e..2fdb7e4 100644
--- a/camel/camel-vee-summary.c
+++ b/camel/camel-vee-summary.c
@@ -83,9 +83,7 @@ message_info_from_uid (CamelFolderSummary *s,
 
                camel_message_info_set_dirty (info, FALSE);
 
-               g_object_ref (info);
-
-               camel_folder_summary_insert (s, info, FALSE);
+               camel_folder_summary_add (s, info, TRUE);
        }
 
        return info;
@@ -233,8 +231,6 @@ camel_vee_summary_add (CamelVeeSummary *s,
 
        vmi = (CamelVeeMessageInfo *) camel_vee_message_info_new (CAMEL_FOLDER_SUMMARY (s), 
orig_folder->summary, vuid);
 
-       g_object_ref (vmi);
-
        vuids = g_hash_table_lookup (s->priv->vuids_by_subfolder, orig_folder);
        if (vuids) {
                g_hash_table_insert (vuids, (gpointer) camel_pstring_strdup (vuid), GINT_TO_POINTER (1));
@@ -244,7 +240,7 @@ camel_vee_summary_add (CamelVeeSummary *s,
                g_hash_table_insert (s->priv->vuids_by_subfolder, orig_folder, vuids);
        }
 
-       camel_folder_summary_insert (&s->summary, (CamelMessageInfo *) vmi, FALSE);
+       camel_folder_summary_add (&s->summary, (CamelMessageInfo *) vmi, TRUE);
        camel_folder_summary_unlock (&s->summary);
 
        return vmi;
diff --git a/camel/providers/imapx/camel-imapx-server.c b/camel/providers/imapx/camel-imapx-server.c
index 2c49989..511824e 100644
--- a/camel/providers/imapx/camel-imapx-server.c
+++ b/camel/providers/imapx/camel-imapx-server.c
@@ -1387,7 +1387,7 @@ imapx_untagged_fetch (CamelIMAPXServer *is,
 
                        if (!camel_folder_summary_check_uid (folder->summary, finfo->uid)) {
                                imapx_set_message_info_flags_for_new_message (mi, server_flags, 
server_user_flags, FALSE, NULL, camel_imapx_mailbox_get_permanentflags (mailbox));
-                               camel_folder_summary_add (folder->summary, mi);
+                               camel_folder_summary_add (folder->summary, mi, FALSE);
 
                                g_mutex_lock (&is->priv->changes_lock);
 
@@ -1401,10 +1401,9 @@ imapx_untagged_fetch (CamelIMAPXServer *is,
 
                                        camel_operation_progress (cancellable, cnt ? cnt : 1);
                                }
-                       } else {
-                               g_clear_object (&mi);
                        }
 
+                       g_clear_object (&mi);
                        camel_folder_summary_unlock (folder->summary);
 
                        if (free_user_flags)
@@ -4418,13 +4417,12 @@ camel_imapx_server_copy_message_sync (CamelIMAPXServer *is,
                                                if (remove_junk_flags)
                                                        camel_message_info_set_flags (destination_info, 
CAMEL_MESSAGE_JUNK, 0);
                                                if (is_new)
-                                                       camel_folder_summary_add 
(destination_folder->summary, destination_info);
+                                                       camel_folder_summary_add 
(destination_folder->summary, destination_info, FALSE);
                                                camel_folder_change_info_add_uid (changes, 
camel_message_info_get_uid (destination_info));
 
                                                camel_message_info_property_unlock (source_info);
 
-                                               if (!is_new)
-                                                       g_clear_object (&destination_info);
+                                               g_clear_object (&destination_info);
                                        }
 
                                        if (camel_folder_change_info_changed (changes)) {
@@ -4705,7 +4703,7 @@ camel_imapx_server_append_message_sync (CamelIMAPXServer *is,
                                        camel_message_info_get_user_tags (info),
                                        camel_imapx_mailbox_get_permanentflags (mailbox));
 
-                               camel_folder_summary_add (folder->summary, mi);
+                               camel_folder_summary_add (folder->summary, mi, FALSE);
 
                                g_mutex_lock (&is->priv->changes_lock);
                                camel_folder_change_info_add_uid (is->priv->changes, 
camel_message_info_get_uid (mi));
@@ -4716,7 +4714,7 @@ camel_imapx_server_append_message_sync (CamelIMAPXServer *is,
                                else
                                        g_free (uid);
 
-                               mi = NULL;
+                               g_clear_object (&mi);
 
                                g_free (cur);
                        } else {
diff --git a/camel/providers/local/camel-local-summary.c b/camel/providers/local/camel-local-summary.c
index db81d0d..77e2b4b 100644
--- a/camel/providers/local/camel-local-summary.c
+++ b/camel/providers/local/camel-local-summary.c
@@ -546,7 +546,7 @@ local_summary_add (CamelLocalSummary *cls,
        g_free (xev);
 
        camel_message_info_set_abort_notifications (mi, FALSE);
-       camel_folder_summary_add (summary, mi);
+       camel_folder_summary_add (summary, mi, FALSE);
        camel_folder_change_info_add_uid (ci, camel_message_info_get_uid (mi));
 
        return mi;
diff --git a/camel/providers/local/camel-maildir-folder.c b/camel/providers/local/camel-maildir-folder.c
index 047b637..48083b1 100644
--- a/camel/providers/local/camel-maildir-folder.c
+++ b/camel/providers/local/camel-maildir-folder.c
@@ -252,6 +252,8 @@ maildir_folder_append_message_sync (CamelFolder *folder,
                camel_folder_change_info_clear (lf->changes);
        }
 
+       g_clear_object (&mi);
+
        return success;
 }
 
@@ -379,7 +381,7 @@ maildir_folder_transfer_messages_to_sync (CamelFolder *source,
                                /* unset junk flag when transferring from junk folder */
                                if ((source->folder_flags & CAMEL_FOLDER_IS_JUNK) != 0)
                                        camel_message_info_set_flags (info, CAMEL_MESSAGE_JUNK, 0);
-                               camel_folder_summary_add (dest->summary, clone);
+                               camel_folder_summary_add (dest->summary, clone, FALSE);
 
                                camel_folder_change_info_add_uid (df->changes, camel_message_info_get_uid 
(clone));
 
@@ -388,6 +390,7 @@ maildir_folder_transfer_messages_to_sync (CamelFolder *source,
                                        CAMEL_MESSAGE_SEEN, ~0);
                                camel_folder_change_info_remove_uid (lf->changes, camel_message_info_get_uid 
(info));
                                camel_folder_summary_remove (source->summary, info);
+                               g_clear_object (&clone);
                        }
 
                        g_clear_object (&info);
diff --git a/camel/providers/local/camel-maildir-summary.c b/camel/providers/local/camel-maildir-summary.c
index 5145e5f..ddc1a12 100644
--- a/camel/providers/local/camel-maildir-summary.c
+++ b/camel/providers/local/camel-maildir-summary.c
@@ -519,7 +519,8 @@ camel_maildir_summary_add (CamelLocalSummary *cls,
        maildirs->priv->current_file = (gchar *) name;
 
        info = camel_folder_summary_info_new_from_parser (summary, mp);
-       camel_folder_summary_add (summary, info);
+       camel_folder_summary_add (summary, info, FALSE);
+       g_clear_object (&info);
 
        g_object_unref (mp);
        maildirs->priv->current_file = NULL;
diff --git a/camel/providers/local/camel-mbox-folder.c b/camel/providers/local/camel-mbox-folder.c
index 8e200ec..efd3aeb 100644
--- a/camel/providers/local/camel-mbox-folder.c
+++ b/camel/providers/local/camel-mbox-folder.c
@@ -156,7 +156,7 @@ mbox_folder_append_message_sync (CamelFolder *folder,
        CamelStream *output_stream = NULL, *filter_stream = NULL;
        CamelMimeFilter *filter_from;
        CamelMboxSummary *mbs = (CamelMboxSummary *) folder->summary;
-       CamelMessageInfo *mi;
+       CamelMessageInfo *mi = NULL;
        gchar *fromline = NULL;
        struct stat st;
        gint retval;
@@ -250,6 +250,8 @@ mbox_folder_append_message_sync (CamelFolder *folder,
        if (appended_uid)
                *appended_uid = g_strdup(camel_message_info_get_uid(mi));
 
+       g_clear_object (&mi);
+
        return TRUE;
 
 fail_write:
@@ -295,6 +297,8 @@ fail:
                camel_folder_change_info_clear (lf->changes);
        }
 
+       g_clear_object (&mi);
+
        return FALSE;
 }
 
diff --git a/camel/providers/local/camel-mbox-summary.c b/camel/providers/local/camel-mbox-summary.c
index bae6c0e..e86a3b8 100644
--- a/camel/providers/local/camel-mbox-summary.c
+++ b/camel/providers/local/camel-mbox-summary.c
@@ -427,7 +427,8 @@ summary_update (CamelLocalSummary *cls,
                camel_operation_progress (cancellable, progress);
 
                info = camel_folder_summary_info_new_from_parser (s, mp);
-               camel_folder_summary_add (s, info);
+               camel_folder_summary_add (s, info, FALSE);
+               g_clear_object (&info);
 
                g_warn_if_fail (camel_mime_parser_step (mp, NULL, NULL) == CAMEL_MIME_PARSER_STATE_FROM_END);
        }
diff --git a/camel/providers/local/camel-mh-folder.c b/camel/providers/local/camel-mh-folder.c
index b47c04e..b9dd91f 100644
--- a/camel/providers/local/camel-mh-folder.c
+++ b/camel/providers/local/camel-mh-folder.c
@@ -130,6 +130,8 @@ mh_folder_append_message_sync (CamelFolder *folder,
                camel_folder_change_info_clear (lf->changes);
        }
 
+       g_clear_object (&mi);
+
        return TRUE;
 }
 
diff --git a/camel/providers/local/camel-mh-summary.c b/camel/providers/local/camel-mh-summary.c
index acbd2c2..ae24f11 100644
--- a/camel/providers/local/camel-mh-summary.c
+++ b/camel/providers/local/camel-mh-summary.c
@@ -185,7 +185,8 @@ camel_mh_summary_add (CamelLocalSummary *cls,
        mhs->priv->current_uid = (gchar *) name;
 
        info = camel_folder_summary_info_new_from_parser (summary, mp);
-       camel_folder_summary_add (summary, info);
+       camel_folder_summary_add (summary, info, FALSE);
+       g_clear_object (&info);
 
        g_object_unref (mp);
        mhs->priv->current_uid = NULL;
diff --git a/camel/providers/nntp/camel-nntp-summary.c b/camel/providers/nntp/camel-nntp-summary.c
index acb75bc..2ae341e 100644
--- a/camel/providers/nntp/camel-nntp-summary.c
+++ b/camel/providers/nntp/camel-nntp-summary.c
@@ -266,12 +266,13 @@ add_range_xover (CamelNNTPSummary *cns,
 
                                mi = camel_folder_summary_info_new_from_header (s, headers);
                                camel_message_info_set_size (mi, size);
-                               camel_folder_summary_add (s, mi);
+                               camel_folder_summary_add (s, mi, FALSE);
 
                                cns->high = n;
                                camel_folder_change_info_add_uid (changes, camel_message_info_get_uid (mi));
                                if (folder_filter_recent)
                                        camel_folder_change_info_recent_uid (changes, 
camel_message_info_get_uid (mi));
+                               g_clear_object (&mi);
                        }
                }
 
@@ -367,7 +368,7 @@ add_range_head (CamelNNTPSummary *cns,
                                if (camel_mime_parser_init_with_stream (mp, CAMEL_STREAM (nntp_stream), 
error) == -1)
                                        goto error;
                                mi = camel_folder_summary_info_new_from_parser (s, mp);
-                               camel_folder_summary_add (s, mi);
+                               camel_folder_summary_add (s, mi, FALSE);
                                while (camel_mime_parser_step (mp, NULL, NULL) != CAMEL_MIME_PARSER_STATE_EOF)
                                        ;
                                if (mi == NULL) {
@@ -377,6 +378,7 @@ add_range_head (CamelNNTPSummary *cns,
                                camel_folder_change_info_add_uid (changes, camel_message_info_get_uid (mi));
                                if (folder_filter_recent)
                                        camel_folder_change_info_recent_uid (changes, 
camel_message_info_get_uid (mi));
+                               g_clear_object (&mi);
                        }
                        if (cns->priv->uid) {
                                g_free (cns->priv->uid);


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