* make mu-cmd reference-counted (make mu_msg_destroy internal, use

mu_msg_unref instead)
This commit is contained in:
Dirk-Jan C. Binnema 2011-01-09 18:54:14 +02:00
parent f1cedf7341
commit e0b484e922
11 changed files with 182 additions and 146 deletions

View File

@ -166,7 +166,7 @@ save_parts (const char *path, MuConfig *opts)
else else
g_assert_not_reached (); g_assert_not_reached ();
mu_msg_destroy (msg); mu_msg_unref (msg);
return rv; return rv;
} }
@ -200,7 +200,7 @@ show_parts (const char* path, MuConfig *opts)
g_print ("MIME-parts in this message:\n"); g_print ("MIME-parts in this message:\n");
mu_msg_msg_part_foreach (msg, each_part_show, NULL); mu_msg_msg_part_foreach (msg, each_part_show, NULL);
mu_msg_destroy (msg); mu_msg_unref (msg);
return TRUE; return TRUE;
@ -317,7 +317,7 @@ mu_cmd_view (MuConfig *opts)
if (!view_msg (msg, NULL, opts->summary_len)) if (!view_msg (msg, NULL, opts->summary_len))
rv = MU_EXITCODE_ERROR; rv = MU_EXITCODE_ERROR;
mu_msg_destroy (msg); mu_msg_unref (msg);
} }
return rv; return rv;
} }

View File

@ -145,7 +145,7 @@ insert_or_update_maybe (const char* fullpath, const char* mdir,
return MU_ERROR; return MU_ERROR;
} }
mu_msg_destroy (msg); mu_msg_unref (msg);
*updated = TRUE; *updated = TRUE;
return MU_OK; return MU_OK;

View File

@ -49,6 +49,8 @@ enum _StringFields {
typedef enum _StringFields StringFields; typedef enum _StringFields StringFields;
struct _MuMsg { struct _MuMsg {
guint _refcount;
GMimeMessage *_mime_msg; GMimeMessage *_mime_msg;
MuMsgFlags _flags; MuMsgFlags _flags;

View File

@ -65,7 +65,7 @@ mu_msg_gmime_uninit (void)
} }
void static void
mu_msg_destroy (MuMsg *msg) mu_msg_destroy (MuMsg *msg)
{ {
int i; int i;
@ -182,6 +182,25 @@ init_mime_msg (MuMsg *msg, GError **err)
return TRUE; 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* MuMsg*
mu_msg_new (const char* filepath, const gchar* mdir, GError **err) 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); mu_msg_destroy (msg);
return NULL; return NULL;
} }
msg->_refcount = 1;
return msg; return msg;
} }
@ -829,3 +849,4 @@ mu_msg_get_field_numeric (MuMsg *msg, const MuMsgFieldId mfid)
default: g_return_val_if_reached (-1); default: g_return_val_if_reached (-1);
} }
} }

View File

@ -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 * @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 * 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, MuMsg *mu_msg_new (const char* filepath, const char *maildir,
GError **err) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT; GError **err) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
/** /**
* destroy a MuMsg* instance; call this function when done with * increase the reference count for this message
* a MuMsg
* *
* @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 msg a valid MuMsg instance
* @param wanted_idx index of the attachment you want to save * @param wanted_idx index of the attachment you want to save
* @param targetdir filesystem directory to save the attachment * @param targetdir filesystem directory to save the attachment
* @param overwrite existing files? * @param overwrite overwrite existing files?
* @param try to 'play' (open) the saved mime-part after saving * @param play try to 'play' (open) the saved mime-part after saving
* *
* @return TRUE if saving succeeded, FALSE otherwise * @return TRUE if saving succeeded, FALSE otherwise
*/ */
gboolean mu_msg_mime_part_save (MuMsg *msg, unsigned wanted_idx, gboolean mu_msg_mime_part_save (MuMsg *msg, unsigned wanted_idx,
const char *targetdir, gboolean overwrite, const char *targetdir, gboolean overwrite,
gboolean tryplay); gboolean play);
/** /**
* get the sender (From:) of this message * get the sender (From:) of this message

View File

@ -184,7 +184,7 @@ print_summary (MuMsgIter *iter, size_t summary_len)
summ = mu_msg_get_summary (msg, summary_len); summ = mu_msg_get_summary (msg, summary_len);
g_print ("Summary: %s\n", summ ? summ : "<none>"); g_print ("Summary: %s\n", summ ? summ : "<none>");
mu_msg_destroy (msg); mu_msg_unref (msg);
} }

View File

@ -35,25 +35,25 @@
static gboolean static gboolean
check_contact_01 (MuMsgContact *contact, int *idx) check_contact_01 (MuMsgContact *contact, int *idx)
{ {
switch (*idx) { switch (*idx) {
case 0: case 0:
g_assert_cmpstr (mu_msg_contact_name (contact), g_assert_cmpstr (mu_msg_contact_name (contact),
==, "Mickey Mouse"); ==, "Mickey Mouse");
g_assert_cmpstr (mu_msg_contact_address (contact), g_assert_cmpstr (mu_msg_contact_address (contact),
==, "anon@example.com"); ==, "anon@example.com");
break; break;
case 1: case 1:
g_assert_cmpstr (mu_msg_contact_name (contact), g_assert_cmpstr (mu_msg_contact_name (contact),
==, "Donald Duck"); ==, "Donald Duck");
g_assert_cmpstr (mu_msg_contact_address (contact), g_assert_cmpstr (mu_msg_contact_address (contact),
==, "gcc-help@gcc.gnu.org"); ==, "gcc-help@gcc.gnu.org");
break; break;
default: default:
g_assert_not_reached (); g_assert_not_reached ();
} }
++(*idx); ++(*idx);
return TRUE; return TRUE;
} }
@ -62,35 +62,35 @@ check_contact_01 (MuMsgContact *contact, int *idx)
static void static void
test_mu_msg_01 (void) test_mu_msg_01 (void)
{ {
MuMsg *msg; MuMsg *msg;
gint i; gint i;
msg = mu_msg_new (MU_TESTMAILDIR msg = mu_msg_new (MU_TESTMAILDIR
"cur/1220863042.12663_1.mindcrime!2,S", "cur/1220863042.12663_1.mindcrime!2,S",
NULL, NULL); NULL, NULL);
g_assert_cmpstr (mu_msg_get_to(msg), g_assert_cmpstr (mu_msg_get_to(msg),
==, "Donald Duck <gcc-help@gcc.gnu.org>"); ==, "Donald Duck <gcc-help@gcc.gnu.org>");
g_assert_cmpstr (mu_msg_get_subject(msg), g_assert_cmpstr (mu_msg_get_subject(msg),
==, "gcc include search order"); ==, "gcc include search order");
g_assert_cmpstr (mu_msg_get_from(msg), g_assert_cmpstr (mu_msg_get_from(msg),
==, "Mickey Mouse <anon@example.com>"); ==, "Mickey Mouse <anon@example.com>");
g_assert_cmpstr (mu_msg_get_msgid(msg), g_assert_cmpstr (mu_msg_get_msgid(msg),
==, "3BE9E6535E3029448670913581E7A1A20D852173@" ==, "3BE9E6535E3029448670913581E7A1A20D852173@"
"emss35m06.us.lmco.com"); "emss35m06.us.lmco.com");
g_assert_cmpstr (mu_msg_get_header(msg, "Mailing-List"), g_assert_cmpstr (mu_msg_get_header(msg, "Mailing-List"),
==, "contact gcc-help-help@gcc.gnu.org; run by ezmlm"); ==, "contact gcc-help-help@gcc.gnu.org; run by ezmlm");
g_assert_cmpuint (mu_msg_get_prio(msg), /* 'klub' */ g_assert_cmpuint (mu_msg_get_prio(msg), /* 'klub' */
==, MU_MSG_PRIO_NORMAL); ==, MU_MSG_PRIO_NORMAL);
g_assert_cmpuint (mu_msg_get_date(msg), g_assert_cmpuint (mu_msg_get_date(msg),
==, 1217530645); ==, 1217530645);
i = 0; i = 0;
mu_msg_contact_foreach (msg, (MuMsgContactForeachFunc)check_contact_01, mu_msg_contact_foreach (msg, (MuMsgContactForeachFunc)check_contact_01,
&i); &i);
g_assert_cmpint (i,==,2); g_assert_cmpint (i,==,2);
mu_msg_destroy (msg); mu_msg_unref (msg);
} }
@ -98,25 +98,25 @@ test_mu_msg_01 (void)
static gboolean static gboolean
check_contact_02 (MuMsgContact *contact, int *idx) check_contact_02 (MuMsgContact *contact, int *idx)
{ {
switch (*idx) { switch (*idx) {
case 0: case 0:
g_assert_cmpstr (mu_msg_contact_name (contact), g_assert_cmpstr (mu_msg_contact_name (contact),
==, NULL); ==, NULL);
g_assert_cmpstr (mu_msg_contact_address (contact), g_assert_cmpstr (mu_msg_contact_address (contact),
==, "anon@example.com"); ==, "anon@example.com");
break; break;
case 1: case 1:
g_assert_cmpstr (mu_msg_contact_name (contact), g_assert_cmpstr (mu_msg_contact_name (contact),
==, NULL); ==, NULL);
g_assert_cmpstr (mu_msg_contact_address (contact), g_assert_cmpstr (mu_msg_contact_address (contact),
==, "help-gnu-emacs@gnu.org"); ==, "help-gnu-emacs@gnu.org");
break; break;
default: default:
g_assert_not_reached (); g_assert_not_reached ();
} }
++(*idx); ++(*idx);
return TRUE; return TRUE;
} }
@ -124,94 +124,94 @@ check_contact_02 (MuMsgContact *contact, int *idx)
static void static void
test_mu_msg_02 (void) test_mu_msg_02 (void)
{ {
MuMsg *msg; MuMsg *msg;
int i; int i;
msg = mu_msg_new (MU_TESTMAILDIR msg = mu_msg_new (MU_TESTMAILDIR
"cur/1220863087.12663_19.mindcrime!2,S", "cur/1220863087.12663_19.mindcrime!2,S",
NULL, NULL); NULL, NULL);
g_assert_cmpstr (mu_msg_get_to(msg), g_assert_cmpstr (mu_msg_get_to(msg),
==, "help-gnu-emacs@gnu.org"); ==, "help-gnu-emacs@gnu.org");
g_assert_cmpstr (mu_msg_get_subject(msg), g_assert_cmpstr (mu_msg_get_subject(msg),
==, "Re: Learning LISP; Scheme vs elisp."); ==, "Re: Learning LISP; Scheme vs elisp.");
g_assert_cmpstr (mu_msg_get_from(msg), g_assert_cmpstr (mu_msg_get_from(msg),
==, "anon@example.com"); ==, "anon@example.com");
g_assert_cmpstr (mu_msg_get_msgid(msg), g_assert_cmpstr (mu_msg_get_msgid(msg),
==, "r6bpm5-6n6.ln1@news.ducksburg.com"); ==, "r6bpm5-6n6.ln1@news.ducksburg.com");
g_assert_cmpstr (mu_msg_get_header(msg, "Errors-To"), g_assert_cmpstr (mu_msg_get_header(msg, "Errors-To"),
==, "help-gnu-emacs-bounces+xxxx.klub=gmail.com@gnu.org"); ==, "help-gnu-emacs-bounces+xxxx.klub=gmail.com@gnu.org");
g_assert_cmpuint (mu_msg_get_prio(msg), /* 'low' */ g_assert_cmpuint (mu_msg_get_prio(msg), /* 'low' */
==, MU_MSG_PRIO_LOW); ==, MU_MSG_PRIO_LOW);
g_assert_cmpuint (mu_msg_get_date(msg), g_assert_cmpuint (mu_msg_get_date(msg),
==, 1218051515); ==, 1218051515);
i = 0; i = 0;
mu_msg_contact_foreach (msg, mu_msg_contact_foreach (msg,
(MuMsgContactForeachFunc)check_contact_02, (MuMsgContactForeachFunc)check_contact_02,
&i); &i);
g_assert_cmpint (i,==,2); g_assert_cmpint (i,==,2);
g_assert_cmpuint (mu_msg_get_flags(msg), g_assert_cmpuint (mu_msg_get_flags(msg),
==, MU_MSG_FLAG_SEEN); ==, MU_MSG_FLAG_SEEN);
mu_msg_destroy (msg); mu_msg_unref (msg);
} }
static void static void
test_mu_msg_03 (void) test_mu_msg_03 (void)
{ {
MuMsg *msg; MuMsg *msg;
msg = mu_msg_new (MU_TESTMAILDIR msg = mu_msg_new (MU_TESTMAILDIR
"cur/1283599333.1840_11.cthulhu!2,", "cur/1283599333.1840_11.cthulhu!2,",
NULL, NULL); NULL, NULL);
g_assert_cmpstr (mu_msg_get_to(msg), g_assert_cmpstr (mu_msg_get_to(msg),
==, "Bilbo Baggins <bilbo@anotherexample.com>"); ==, "Bilbo Baggins <bilbo@anotherexample.com>");
g_assert_cmpstr (mu_msg_get_subject(msg), g_assert_cmpstr (mu_msg_get_subject(msg),
==, "Greetings from Lothlórien"); ==, "Greetings from Lothlórien");
g_assert_cmpstr (mu_msg_get_from(msg), g_assert_cmpstr (mu_msg_get_from(msg),
==, "Frodo Baggins <frodo@example.com>"); ==, "Frodo Baggins <frodo@example.com>");
g_assert_cmpuint (mu_msg_get_prio(msg), /* 'low' */ g_assert_cmpuint (mu_msg_get_prio(msg), /* 'low' */
==, MU_MSG_PRIO_NORMAL); ==, MU_MSG_PRIO_NORMAL);
g_assert_cmpuint (mu_msg_get_date(msg), g_assert_cmpuint (mu_msg_get_date(msg),
==, 0); ==, 0);
g_assert_cmpstr (mu_msg_get_body_text(msg), g_assert_cmpstr (mu_msg_get_body_text(msg),
==, ==,
"\nLet's write some fünkÿ text\nusing umlauts.\n\nFoo.\n"); "\nLet's write some fünkÿ text\nusing umlauts.\n\nFoo.\n");
g_assert_cmpuint (mu_msg_get_flags(msg), g_assert_cmpuint (mu_msg_get_flags(msg),
==, 0); ==, 0);
mu_msg_destroy (msg); mu_msg_unref (msg);
} }
static void static void
test_mu_msg_04 (void) test_mu_msg_04 (void)
{ {
MuMsg *msg; MuMsg *msg;
msg = mu_msg_new (MU_TESTMAILDIR2 msg = mu_msg_new (MU_TESTMAILDIR2
"Foo/cur/mail5", NULL, NULL); "Foo/cur/mail5", NULL, NULL);
g_assert_cmpstr (mu_msg_get_to(msg), g_assert_cmpstr (mu_msg_get_to(msg),
==, "George Custer <gac@example.com>"); ==, "George Custer <gac@example.com>");
g_assert_cmpstr (mu_msg_get_subject(msg), g_assert_cmpstr (mu_msg_get_subject(msg),
==, "pics for you"); ==, "pics for you");
g_assert_cmpstr (mu_msg_get_from(msg), g_assert_cmpstr (mu_msg_get_from(msg),
==, "Sitting Bull <sb@example.com>"); ==, "Sitting Bull <sb@example.com>");
g_assert_cmpuint (mu_msg_get_prio(msg), /* 'low' */ g_assert_cmpuint (mu_msg_get_prio(msg), /* 'low' */
==, MU_MSG_PRIO_NORMAL); ==, MU_MSG_PRIO_NORMAL);
g_assert_cmpuint (mu_msg_get_date(msg), g_assert_cmpuint (mu_msg_get_date(msg),
==, 0); ==, 0);
g_assert_cmpuint (mu_msg_get_flags(msg), g_assert_cmpuint (mu_msg_get_flags(msg),
==, MU_MSG_FLAG_HAS_ATTACH); ==, MU_MSG_FLAG_HAS_ATTACH);
mu_msg_destroy (msg); mu_msg_unref (msg);
} }

View File

@ -224,7 +224,7 @@ test_mu_query_05 (void)
g_assert_cmpstr (mu_msg_get_summary(msg,5),==, g_assert_cmpstr (mu_msg_get_summary(msg,5),==,
"Let's write some fünkÿ text using umlauts. Foo."); "Let's write some fünkÿ text using umlauts. Foo.");
mu_msg_destroy (msg); mu_msg_unref (msg);
mu_msg_iter_destroy (iter); mu_msg_iter_destroy (iter);
mu_query_destroy (query); mu_query_destroy (query);
g_free (xpath); g_free (xpath);

View File

@ -108,7 +108,7 @@ test_mu_store_store_and_count (void)
g_assert_cmpuint (1,==,mu_store_count (store)); g_assert_cmpuint (1,==,mu_store_count (store));
g_assert_cmpuint (TRUE,==,mu_store_contains_message g_assert_cmpuint (TRUE,==,mu_store_contains_message
(store, MU_TESTMAILDIR "cur/1283599333.1840_11.cthulhu!2,")); (store, MU_TESTMAILDIR "cur/1283599333.1840_11.cthulhu!2,"));
mu_msg_destroy (msg); mu_msg_unref (msg);
/* add another one */ /* add another one */
msg = mu_msg_new (MU_TESTMAILDIR2 "bar/cur/mail3", NULL, NULL); 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 (mu_store_store (store, msg), ==, MU_OK);
g_assert_cmpuint (2,==,mu_store_count (store)); g_assert_cmpuint (2,==,mu_store_count (store));
g_assert_cmpuint (TRUE,==,mu_store_contains_message (store, MU_TESTMAILDIR2 "bar/cur/mail3")); 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 */ /* 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); 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 (mu_store_store (store, msg), ==, MU_OK);
g_assert_cmpuint (2,==,mu_store_count (store)); g_assert_cmpuint (2,==,mu_store_count (store));
mu_msg_destroy (msg); mu_msg_unref (msg);
mu_msg_gmime_uninit (); mu_msg_gmime_uninit ();
mu_store_destroy (store); mu_store_destroy (store);
@ -156,7 +156,7 @@ test_mu_store_store_remove_and_count (void)
g_assert (msg); g_assert (msg);
g_assert_cmpuint (mu_store_store (store, msg), ==, MU_OK); g_assert_cmpuint (mu_store_store (store, msg), ==, MU_OK);
g_assert_cmpuint (1,==,mu_store_count (store)); g_assert_cmpuint (1,==,mu_store_count (store));
mu_msg_destroy (msg); mu_msg_unref (msg);
/* remove one */ /* remove one */
mu_store_remove (store, MU_TESTMAILDIR "cur/1283599333.1840_11.cthulhu!2,"); mu_store_remove (store, MU_TESTMAILDIR "cur/1283599333.1840_11.cthulhu!2,");

View File

@ -302,7 +302,7 @@ mug_msg_view_set_msg (MugMsgView * self, const char *msgpath)
rv = set_text (self, mu_msg_get_body_text (msg)); rv = set_text (self, mu_msg_get_body_text (msg));
fill_header (priv, msg); fill_header (priv, msg);
mu_msg_destroy (msg); mu_msg_unref (msg);
return rv; return rv;
} }

View File

@ -267,7 +267,7 @@ mug_msg_view_set_msg (MugMsgView * self, const char *msgpath)
MuMsg *msg = mu_msg_new (msgpath, NULL, NULL); MuMsg *msg = mu_msg_new (msgpath, NULL, NULL);
mu_msg_view_set_message (MU_MSG_VIEW(priv->_view), msg); mu_msg_view_set_message (MU_MSG_VIEW(priv->_view), msg);
fill_header (priv, msg); fill_header (priv, msg);
mu_msg_destroy (msg); mu_msg_unref (msg);
} }
return TRUE; return TRUE;