diff --git a/src/mu-cmd.c b/src/mu-cmd.c index 67c7417f..be364f89 100644 --- a/src/mu-cmd.c +++ b/src/mu-cmd.c @@ -166,7 +166,7 @@ save_parts (const char *path, MuConfig *opts) else g_assert_not_reached (); - mu_msg_destroy (msg); + mu_msg_unref (msg); return rv; } @@ -200,7 +200,7 @@ show_parts (const char* path, MuConfig *opts) g_print ("MIME-parts in this message:\n"); mu_msg_msg_part_foreach (msg, each_part_show, NULL); - mu_msg_destroy (msg); + mu_msg_unref (msg); return TRUE; @@ -317,7 +317,7 @@ mu_cmd_view (MuConfig *opts) if (!view_msg (msg, NULL, opts->summary_len)) rv = MU_EXITCODE_ERROR; - mu_msg_destroy (msg); + mu_msg_unref (msg); } return rv; } diff --git a/src/mu-index.c b/src/mu-index.c index 466b45b3..d46ad49f 100644 --- a/src/mu-index.c +++ b/src/mu-index.c @@ -145,7 +145,7 @@ insert_or_update_maybe (const char* fullpath, const char* mdir, return MU_ERROR; } - mu_msg_destroy (msg); + mu_msg_unref (msg); *updated = TRUE; return MU_OK; diff --git a/src/mu-msg-priv.h b/src/mu-msg-priv.h index 34c2ec54..f98cd94d 100644 --- a/src/mu-msg-priv.h +++ b/src/mu-msg-priv.h @@ -49,6 +49,8 @@ enum _StringFields { typedef enum _StringFields StringFields; struct _MuMsg { + guint _refcount; + GMimeMessage *_mime_msg; MuMsgFlags _flags; diff --git a/src/mu-msg.c b/src/mu-msg.c index 94689f2f..36263b6e 100644 --- a/src/mu-msg.c +++ b/src/mu-msg.c @@ -65,7 +65,7 @@ mu_msg_gmime_uninit (void) } -void +static void mu_msg_destroy (MuMsg *msg) { int i; @@ -182,6 +182,25 @@ init_mime_msg (MuMsg *msg, GError **err) return TRUE; } +MuMsg* +mu_msg_ref (MuMsg *msg) +{ + g_return_val_if_fail (msg, NULL); + ++msg->_refcount; + + return msg; +} + +void +mu_msg_unref (MuMsg *msg) +{ + g_return_if_fail (msg); + g_return_if_fail (msg->_refcount >= 1); + + if (--msg->_refcount == 0) + mu_msg_destroy (msg); +} + MuMsg* mu_msg_new (const char* filepath, const gchar* mdir, GError **err) @@ -203,7 +222,8 @@ mu_msg_new (const char* filepath, const gchar* mdir, GError **err) mu_msg_destroy (msg); return NULL; } - + + msg->_refcount = 1; return msg; } @@ -829,3 +849,4 @@ mu_msg_get_field_numeric (MuMsg *msg, const MuMsgFieldId mfid) default: g_return_val_if_reached (-1); } } + diff --git a/src/mu-msg.h b/src/mu-msg.h index dafb31ca..3862dc8d 100644 --- a/src/mu-msg.h +++ b/src/mu-msg.h @@ -59,18 +59,31 @@ void mu_msg_gmime_uninit (void); * @param err receive error information (MU_ERROR_FILE or MU_ERROR_GMIME), or NULL. There * will only be err info if the function returns NULL * - * @return a new MuMsg instance or NULL in case of error + * @return a new MuMsg instance or NULL in case of error; call + * mu_msg_unref when done with this message */ MuMsg *mu_msg_new (const char* filepath, const char *maildir, GError **err) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT; + /** - * destroy a MuMsg* instance; call this function when done with - * a MuMsg + * increase the reference count for this message * - * @param msg a MuMsg* instance or NULL + * @param msg a message + * + * @return the message with its reference count increased, or NULL in + * case of error. */ -void mu_msg_destroy (MuMsg *msg); +MuMsg * mu_msg_ref (MuMsg *msg); + +/** + * decrease the reference count for this message. if the reference + * count reaches 0, the message will be destroyed. + * + * @param msg a message + */ +void mu_msg_unref (MuMsg *msg); + /** @@ -118,14 +131,14 @@ const char* mu_msg_get_summary (MuMsg *msg, size_t max_lines); * @param msg a valid MuMsg instance * @param wanted_idx index of the attachment you want to save * @param targetdir filesystem directory to save the attachment - * @param overwrite existing files? - * @param try to 'play' (open) the saved mime-part after saving + * @param overwrite overwrite existing files? + * @param play try to 'play' (open) the saved mime-part after saving * * @return TRUE if saving succeeded, FALSE otherwise */ gboolean mu_msg_mime_part_save (MuMsg *msg, unsigned wanted_idx, const char *targetdir, gboolean overwrite, - gboolean tryplay); + gboolean play); /** * get the sender (From:) of this message diff --git a/src/mu-output.c b/src/mu-output.c index a28a79a7..35edea3e 100644 --- a/src/mu-output.c +++ b/src/mu-output.c @@ -184,7 +184,7 @@ print_summary (MuMsgIter *iter, size_t summary_len) summ = mu_msg_get_summary (msg, summary_len); g_print ("Summary: %s\n", summ ? summ : ""); - mu_msg_destroy (msg); + mu_msg_unref (msg); } diff --git a/src/tests/test-mu-msg.c b/src/tests/test-mu-msg.c index 9435e0f5..2ee4292b 100644 --- a/src/tests/test-mu-msg.c +++ b/src/tests/test-mu-msg.c @@ -35,25 +35,25 @@ static gboolean check_contact_01 (MuMsgContact *contact, int *idx) { - switch (*idx) { - case 0: - g_assert_cmpstr (mu_msg_contact_name (contact), - ==, "Mickey Mouse"); - g_assert_cmpstr (mu_msg_contact_address (contact), - ==, "anon@example.com"); - break; - case 1: - g_assert_cmpstr (mu_msg_contact_name (contact), - ==, "Donald Duck"); - g_assert_cmpstr (mu_msg_contact_address (contact), - ==, "gcc-help@gcc.gnu.org"); - break; - default: - g_assert_not_reached (); - } - ++(*idx); + switch (*idx) { + case 0: + g_assert_cmpstr (mu_msg_contact_name (contact), + ==, "Mickey Mouse"); + g_assert_cmpstr (mu_msg_contact_address (contact), + ==, "anon@example.com"); + break; + case 1: + g_assert_cmpstr (mu_msg_contact_name (contact), + ==, "Donald Duck"); + g_assert_cmpstr (mu_msg_contact_address (contact), + ==, "gcc-help@gcc.gnu.org"); + break; + default: + g_assert_not_reached (); + } + ++(*idx); - return TRUE; + return TRUE; } @@ -62,35 +62,35 @@ check_contact_01 (MuMsgContact *contact, int *idx) static void test_mu_msg_01 (void) { - MuMsg *msg; - gint i; + MuMsg *msg; + gint i; - msg = mu_msg_new (MU_TESTMAILDIR - "cur/1220863042.12663_1.mindcrime!2,S", - NULL, NULL); + msg = mu_msg_new (MU_TESTMAILDIR + "cur/1220863042.12663_1.mindcrime!2,S", + NULL, NULL); - g_assert_cmpstr (mu_msg_get_to(msg), - ==, "Donald Duck "); - g_assert_cmpstr (mu_msg_get_subject(msg), - ==, "gcc include search order"); - g_assert_cmpstr (mu_msg_get_from(msg), - ==, "Mickey Mouse "); - g_assert_cmpstr (mu_msg_get_msgid(msg), - ==, "3BE9E6535E3029448670913581E7A1A20D852173@" - "emss35m06.us.lmco.com"); - g_assert_cmpstr (mu_msg_get_header(msg, "Mailing-List"), - ==, "contact gcc-help-help@gcc.gnu.org; run by ezmlm"); - g_assert_cmpuint (mu_msg_get_prio(msg), /* 'klub' */ - ==, MU_MSG_PRIO_NORMAL); - g_assert_cmpuint (mu_msg_get_date(msg), - ==, 1217530645); + g_assert_cmpstr (mu_msg_get_to(msg), + ==, "Donald Duck "); + g_assert_cmpstr (mu_msg_get_subject(msg), + ==, "gcc include search order"); + g_assert_cmpstr (mu_msg_get_from(msg), + ==, "Mickey Mouse "); + g_assert_cmpstr (mu_msg_get_msgid(msg), + ==, "3BE9E6535E3029448670913581E7A1A20D852173@" + "emss35m06.us.lmco.com"); + g_assert_cmpstr (mu_msg_get_header(msg, "Mailing-List"), + ==, "contact gcc-help-help@gcc.gnu.org; run by ezmlm"); + g_assert_cmpuint (mu_msg_get_prio(msg), /* 'klub' */ + ==, MU_MSG_PRIO_NORMAL); + g_assert_cmpuint (mu_msg_get_date(msg), + ==, 1217530645); - i = 0; - mu_msg_contact_foreach (msg, (MuMsgContactForeachFunc)check_contact_01, - &i); - g_assert_cmpint (i,==,2); + i = 0; + mu_msg_contact_foreach (msg, (MuMsgContactForeachFunc)check_contact_01, + &i); + g_assert_cmpint (i,==,2); - mu_msg_destroy (msg); + mu_msg_unref (msg); } @@ -98,25 +98,25 @@ test_mu_msg_01 (void) static gboolean check_contact_02 (MuMsgContact *contact, int *idx) { - switch (*idx) { - case 0: - g_assert_cmpstr (mu_msg_contact_name (contact), - ==, NULL); - g_assert_cmpstr (mu_msg_contact_address (contact), - ==, "anon@example.com"); - break; - case 1: - g_assert_cmpstr (mu_msg_contact_name (contact), - ==, NULL); - g_assert_cmpstr (mu_msg_contact_address (contact), - ==, "help-gnu-emacs@gnu.org"); - break; - default: - g_assert_not_reached (); - } - ++(*idx); + switch (*idx) { + case 0: + g_assert_cmpstr (mu_msg_contact_name (contact), + ==, NULL); + g_assert_cmpstr (mu_msg_contact_address (contact), + ==, "anon@example.com"); + break; + case 1: + g_assert_cmpstr (mu_msg_contact_name (contact), + ==, NULL); + g_assert_cmpstr (mu_msg_contact_address (contact), + ==, "help-gnu-emacs@gnu.org"); + break; + default: + g_assert_not_reached (); + } + ++(*idx); - return TRUE; + return TRUE; } @@ -124,94 +124,94 @@ check_contact_02 (MuMsgContact *contact, int *idx) static void test_mu_msg_02 (void) { - MuMsg *msg; - int i; + MuMsg *msg; + int i; - msg = mu_msg_new (MU_TESTMAILDIR - "cur/1220863087.12663_19.mindcrime!2,S", - NULL, NULL); + msg = mu_msg_new (MU_TESTMAILDIR + "cur/1220863087.12663_19.mindcrime!2,S", + NULL, NULL); - g_assert_cmpstr (mu_msg_get_to(msg), - ==, "help-gnu-emacs@gnu.org"); - g_assert_cmpstr (mu_msg_get_subject(msg), - ==, "Re: Learning LISP; Scheme vs elisp."); - g_assert_cmpstr (mu_msg_get_from(msg), - ==, "anon@example.com"); - g_assert_cmpstr (mu_msg_get_msgid(msg), - ==, "r6bpm5-6n6.ln1@news.ducksburg.com"); - g_assert_cmpstr (mu_msg_get_header(msg, "Errors-To"), - ==, "help-gnu-emacs-bounces+xxxx.klub=gmail.com@gnu.org"); - g_assert_cmpuint (mu_msg_get_prio(msg), /* 'low' */ - ==, MU_MSG_PRIO_LOW); - g_assert_cmpuint (mu_msg_get_date(msg), - ==, 1218051515); + g_assert_cmpstr (mu_msg_get_to(msg), + ==, "help-gnu-emacs@gnu.org"); + g_assert_cmpstr (mu_msg_get_subject(msg), + ==, "Re: Learning LISP; Scheme vs elisp."); + g_assert_cmpstr (mu_msg_get_from(msg), + ==, "anon@example.com"); + g_assert_cmpstr (mu_msg_get_msgid(msg), + ==, "r6bpm5-6n6.ln1@news.ducksburg.com"); + g_assert_cmpstr (mu_msg_get_header(msg, "Errors-To"), + ==, "help-gnu-emacs-bounces+xxxx.klub=gmail.com@gnu.org"); + g_assert_cmpuint (mu_msg_get_prio(msg), /* 'low' */ + ==, MU_MSG_PRIO_LOW); + g_assert_cmpuint (mu_msg_get_date(msg), + ==, 1218051515); - i = 0; - mu_msg_contact_foreach (msg, - (MuMsgContactForeachFunc)check_contact_02, - &i); - g_assert_cmpint (i,==,2); + i = 0; + mu_msg_contact_foreach (msg, + (MuMsgContactForeachFunc)check_contact_02, + &i); + g_assert_cmpint (i,==,2); - g_assert_cmpuint (mu_msg_get_flags(msg), - ==, MU_MSG_FLAG_SEEN); + g_assert_cmpuint (mu_msg_get_flags(msg), + ==, MU_MSG_FLAG_SEEN); - mu_msg_destroy (msg); + mu_msg_unref (msg); } static void test_mu_msg_03 (void) { - MuMsg *msg; + MuMsg *msg; - msg = mu_msg_new (MU_TESTMAILDIR - "cur/1283599333.1840_11.cthulhu!2,", - NULL, NULL); + msg = mu_msg_new (MU_TESTMAILDIR + "cur/1283599333.1840_11.cthulhu!2,", + NULL, NULL); - g_assert_cmpstr (mu_msg_get_to(msg), - ==, "Bilbo Baggins "); - g_assert_cmpstr (mu_msg_get_subject(msg), - ==, "Greetings from Lothlórien"); - g_assert_cmpstr (mu_msg_get_from(msg), - ==, "Frodo Baggins "); - g_assert_cmpuint (mu_msg_get_prio(msg), /* 'low' */ - ==, MU_MSG_PRIO_NORMAL); - g_assert_cmpuint (mu_msg_get_date(msg), - ==, 0); - g_assert_cmpstr (mu_msg_get_body_text(msg), - ==, - "\nLet's write some fünkÿ text\nusing umlauts.\n\nFoo.\n"); + g_assert_cmpstr (mu_msg_get_to(msg), + ==, "Bilbo Baggins "); + g_assert_cmpstr (mu_msg_get_subject(msg), + ==, "Greetings from Lothlórien"); + g_assert_cmpstr (mu_msg_get_from(msg), + ==, "Frodo Baggins "); + g_assert_cmpuint (mu_msg_get_prio(msg), /* 'low' */ + ==, MU_MSG_PRIO_NORMAL); + g_assert_cmpuint (mu_msg_get_date(msg), + ==, 0); + g_assert_cmpstr (mu_msg_get_body_text(msg), + ==, + "\nLet's write some fünkÿ text\nusing umlauts.\n\nFoo.\n"); - g_assert_cmpuint (mu_msg_get_flags(msg), - ==, 0); + g_assert_cmpuint (mu_msg_get_flags(msg), + ==, 0); - mu_msg_destroy (msg); + mu_msg_unref (msg); } static void test_mu_msg_04 (void) { - MuMsg *msg; + MuMsg *msg; - msg = mu_msg_new (MU_TESTMAILDIR2 - "Foo/cur/mail5", NULL, NULL); + msg = mu_msg_new (MU_TESTMAILDIR2 + "Foo/cur/mail5", NULL, NULL); - g_assert_cmpstr (mu_msg_get_to(msg), - ==, "George Custer "); - g_assert_cmpstr (mu_msg_get_subject(msg), - ==, "pics for you"); - g_assert_cmpstr (mu_msg_get_from(msg), - ==, "Sitting Bull "); - g_assert_cmpuint (mu_msg_get_prio(msg), /* 'low' */ - ==, MU_MSG_PRIO_NORMAL); - g_assert_cmpuint (mu_msg_get_date(msg), - ==, 0); + g_assert_cmpstr (mu_msg_get_to(msg), + ==, "George Custer "); + g_assert_cmpstr (mu_msg_get_subject(msg), + ==, "pics for you"); + g_assert_cmpstr (mu_msg_get_from(msg), + ==, "Sitting Bull "); + g_assert_cmpuint (mu_msg_get_prio(msg), /* 'low' */ + ==, MU_MSG_PRIO_NORMAL); + g_assert_cmpuint (mu_msg_get_date(msg), + ==, 0); - g_assert_cmpuint (mu_msg_get_flags(msg), - ==, MU_MSG_FLAG_HAS_ATTACH); + g_assert_cmpuint (mu_msg_get_flags(msg), + ==, MU_MSG_FLAG_HAS_ATTACH); - mu_msg_destroy (msg); + mu_msg_unref (msg); } diff --git a/src/tests/test-mu-query.c b/src/tests/test-mu-query.c index cd4d6752..bd724807 100644 --- a/src/tests/test-mu-query.c +++ b/src/tests/test-mu-query.c @@ -224,7 +224,7 @@ test_mu_query_05 (void) g_assert_cmpstr (mu_msg_get_summary(msg,5),==, "Let's write some fünkÿ text using umlauts. Foo."); - mu_msg_destroy (msg); + mu_msg_unref (msg); mu_msg_iter_destroy (iter); mu_query_destroy (query); g_free (xpath); diff --git a/src/tests/test-mu-store.c b/src/tests/test-mu-store.c index bf8c36b5..1d5f4450 100644 --- a/src/tests/test-mu-store.c +++ b/src/tests/test-mu-store.c @@ -108,7 +108,7 @@ test_mu_store_store_and_count (void) g_assert_cmpuint (1,==,mu_store_count (store)); g_assert_cmpuint (TRUE,==,mu_store_contains_message (store, MU_TESTMAILDIR "cur/1283599333.1840_11.cthulhu!2,")); - mu_msg_destroy (msg); + mu_msg_unref (msg); /* add another one */ msg = mu_msg_new (MU_TESTMAILDIR2 "bar/cur/mail3", NULL, NULL); @@ -116,7 +116,7 @@ test_mu_store_store_and_count (void) g_assert_cmpuint (mu_store_store (store, msg), ==, MU_OK); g_assert_cmpuint (2,==,mu_store_count (store)); g_assert_cmpuint (TRUE,==,mu_store_contains_message (store, MU_TESTMAILDIR2 "bar/cur/mail3")); - mu_msg_destroy (msg); + mu_msg_unref (msg); /* try to add the first one again. count should be 2 still */ msg = mu_msg_new (MU_TESTMAILDIR "cur/1283599333.1840_11.cthulhu!2,", NULL, NULL); @@ -124,7 +124,7 @@ test_mu_store_store_and_count (void) g_assert_cmpuint (mu_store_store (store, msg), ==, MU_OK); g_assert_cmpuint (2,==,mu_store_count (store)); - mu_msg_destroy (msg); + mu_msg_unref (msg); mu_msg_gmime_uninit (); mu_store_destroy (store); @@ -156,7 +156,7 @@ test_mu_store_store_remove_and_count (void) g_assert (msg); g_assert_cmpuint (mu_store_store (store, msg), ==, MU_OK); g_assert_cmpuint (1,==,mu_store_count (store)); - mu_msg_destroy (msg); + mu_msg_unref (msg); /* remove one */ mu_store_remove (store, MU_TESTMAILDIR "cur/1283599333.1840_11.cthulhu!2,"); diff --git a/toys/mug/mug-msg-view.c b/toys/mug/mug-msg-view.c index 2f3f5779..a3163318 100644 --- a/toys/mug/mug-msg-view.c +++ b/toys/mug/mug-msg-view.c @@ -302,7 +302,7 @@ mug_msg_view_set_msg (MugMsgView * self, const char *msgpath) rv = set_text (self, mu_msg_get_body_text (msg)); fill_header (priv, msg); - mu_msg_destroy (msg); + mu_msg_unref (msg); return rv; } diff --git a/toys/mug2/mug-msg-view.c b/toys/mug2/mug-msg-view.c index ec50cbe5..ce5288ba 100644 --- a/toys/mug2/mug-msg-view.c +++ b/toys/mug2/mug-msg-view.c @@ -267,7 +267,7 @@ mug_msg_view_set_msg (MugMsgView * self, const char *msgpath) MuMsg *msg = mu_msg_new (msgpath, NULL, NULL); mu_msg_view_set_message (MU_MSG_VIEW(priv->_view), msg); fill_header (priv, msg); - mu_msg_destroy (msg); + mu_msg_unref (msg); } return TRUE;