* refactor mu_msg_(un)load_msg_file from various versions

This commit is contained in:
djcb 2012-07-16 12:55:54 +03:00
parent 668327da84
commit d4abc249c2
5 changed files with 54 additions and 74 deletions

View File

@ -192,7 +192,7 @@ SCM_DEFINE (get_field, "mu:c:get-field", 2, 0, 0,
data = mu_guile_scm_from_str
(mu_msg_get_field_string (msgwrap->_msg, mfid));
/* explicitly close the file backend, so we won't run of fds */
mu_msg_close_file_backend (msgwrap->_msg);
mu_msg_unload_msg_file (msgwrap->_msg);
return data;
}
@ -287,7 +287,7 @@ SCM_DEFINE (get_contacts, "mu:c:get-contacts", 2, 0, 0,
(MuMsgContactForeachFunc)contacts_to_list,
&ecdata);
/* explicitly close the file backend, so we won't run out of fds */
mu_msg_close_file_backend (msgwrap->_msg);
mu_msg_unload_msg_file (msgwrap->_msg);
return ecdata.lst;
}
@ -361,7 +361,7 @@ SCM_DEFINE (get_parts, "mu:c:get-parts", 1, 1, 0,
&attinfo);
/* explicitly close the file backend, so we won't run of fds */
mu_msg_close_file_backend (msgwrap->_msg);
mu_msg_unload_msg_file (msgwrap->_msg);
return attinfo.attlist;
}
@ -389,7 +389,7 @@ SCM_DEFINE (get_header, "mu:c:get-header", 2, 0, 0,
free (header);
/* explicitly close the file backend, so we won't run of fds */
mu_msg_close_file_backend (msgwrap->_msg);
mu_msg_unload_msg_file (msgwrap->_msg);
return val;
}

View File

@ -275,33 +275,6 @@ part_foreach_cb (GMimeObject *parent, GMimeObject *mobj, PartData *pdata)
}
static gboolean
load_msg_file_maybe (MuMsg *msg)
{
GError *err;
if (msg->_file)
return TRUE;
err = NULL;
msg->_file = mu_msg_file_new (mu_msg_get_path(msg), NULL,
&err);
if (!msg->_file) {
MU_HANDLE_G_ERROR(err); /* will free it */
return FALSE;
}
if (!msg->_file->_mime_msg) {
mu_msg_file_destroy (msg->_file);
msg->_file = NULL;
g_warning ("failed to create mime-msg");
return FALSE;
}
return TRUE;
}
void
mu_msg_part_foreach (MuMsg *msg, gboolean recurse_rfc822,
MuMsgPartForeachFunc func, gpointer user_data)
@ -311,7 +284,7 @@ mu_msg_part_foreach (MuMsg *msg, gboolean recurse_rfc822,
g_return_if_fail (msg);
if (!load_msg_file_maybe (msg))
if (!mu_msg_load_msg_file (msg, NULL))
return;
mime_msg = msg->_file->_mime_msg;
@ -437,7 +410,7 @@ mu_msg_part_filepath (MuMsg *msg, const char* targetdir, guint partidx,
char *fname, *filepath;
GMimeObject* mobj;
if (!load_msg_file_maybe (msg))
if (!mu_msg_load_msg_file (msg, NULL))
return NULL;
if (!(mobj = find_part (msg, partidx))) {
@ -485,7 +458,7 @@ mu_msg_part_filepath_cache (MuMsg *msg, guint partid)
g_return_val_if_fail (msg, NULL);
if (!load_msg_file_maybe (msg))
if (!mu_msg_load_msg_file (msg, NULL))
return NULL;
path = mu_msg_get_path (msg);
@ -523,7 +496,7 @@ mu_msg_part_save (MuMsg *msg, const char *fullpath, guint partidx,
g_return_val_if_fail (fullpath, FALSE);
g_return_val_if_fail (!overwrite||!use_cached, FALSE);
if (!load_msg_file_maybe (msg))
if (!mu_msg_load_msg_file (msg, NULL))
return FALSE;
part = find_part (msg, partidx);
@ -620,7 +593,7 @@ mu_msg_part_find_cid (MuMsg *msg, const char* sought_cid)
g_return_val_if_fail (msg, -1);
g_return_val_if_fail (sought_cid, -1);
if (!load_msg_file_maybe (msg))
if (!mu_msg_load_msg_file (msg, NULL))
return -1;
cid = g_str_has_prefix (sought_cid, "cid:") ?
@ -671,7 +644,7 @@ mu_msg_part_find_files (MuMsg *msg, const GRegex *pattern)
g_return_val_if_fail (msg, NULL);
g_return_val_if_fail (pattern, NULL);
if (!load_msg_file_maybe (msg))
if (!mu_msg_load_msg_file (msg, NULL))
return NULL;
mdata._lst = NULL;

View File

@ -81,7 +81,6 @@ gchar* mu_msg_mime_part_to_string (GMimePart *part, gboolean *err);
*/
GMimePart* mu_msg_mime_get_body_part (GMimeMessage *msg, gboolean want_html);
G_END_DECLS
G_END_DECLS
#endif /*__MU_MSG_PRIV_H__*/

View File

@ -107,14 +107,6 @@ mu_msg_new_from_file (const char *path, const char *mdir, GError **err)
}
void
mu_msg_close_file_backend (MuMsg *msg)
{
g_return_if_fail (msg);
mu_msg_file_destroy (msg->_file);
msg->_file = NULL;
}
MuMsg*
@ -220,29 +212,39 @@ get_path (MuMsg *self)
/* for some data, we need to read the message file from disk */
static MuMsgFile*
get_msg_file (MuMsg *self)
gboolean
mu_msg_load_msg_file (MuMsg *self, GError **err)
{
MuMsgFile *mfile;
const char *path;
GError *err;
if (!(path = get_path (self)))
return NULL;
g_return_val_if_fail (self, FALSE);
err = NULL;
mfile = mu_msg_file_new (path, NULL, &err);
if (!mfile) {
g_warning ("%s: failed to create MuMsgFile: %s",
__FUNCTION__, err->message ? err->message : "?");
g_error_free (err);
return NULL;
if (self->_file)
return TRUE; /* nothing to do */
if (!(path = get_path (self))) {
mu_util_g_set_error (err, MU_ERROR_INTERNAL,
"cannot get path for message");
return FALSE;
}
return mfile;
self->_file = mu_msg_file_new (path, NULL, err);
return (self->_file != NULL);
}
void
mu_msg_unload_msg_file (MuMsg *msg)
{
g_return_if_fail (msg);
mu_msg_file_destroy (msg->_file);
msg->_file = NULL;
}
static const GSList*
get_str_list_field (MuMsg *self, MuMsgFieldId mfid)
{
@ -262,9 +264,7 @@ get_str_list_field (MuMsg *self, MuMsgFieldId mfid)
else {
/* if we don't have a file object yet, we need to
* create it from the file on disk */
if (!self->_file)
self->_file = get_msg_file (self);
if (!self->_file && !(self->_file = get_msg_file (self)))
if (!mu_msg_load_msg_file (self, NULL))
return NULL;
val = mu_msg_file_get_str_list_field (self->_file, mfid,
&do_free);
@ -298,9 +298,7 @@ get_str_field (MuMsg *self, MuMsgFieldId mfid)
else if (mu_msg_field_gmime (mfid)) {
/* if we don't have a file object yet, we need to
* create it from the file on disk */
if (!self->_file)
self->_file = get_msg_file (self);
if (!self->_file && !(self->_file = get_msg_file (self)))
if (!mu_msg_load_msg_file (self, NULL))
return NULL;
val = mu_msg_file_get_str_field (self->_file, mfid, &do_free);
} else {
@ -332,9 +330,7 @@ get_num_field (MuMsg *self, MuMsgFieldId mfid)
else {
/* if we don't have a file object yet, we need to
* create it from the file on disk */
if (!self->_file)
self->_file = get_msg_file (self);
if (!self->_file && !(self->_file = get_msg_file (self)))
if (!mu_msg_load_msg_file (self, NULL))
return -1;
val = mu_msg_file_get_num_field (self->_file, mfid);
}
@ -351,9 +347,7 @@ mu_msg_get_header (MuMsg *self, const char *header)
/* if we don't have a file object yet, we need to
* create it from the file on disk */
if (!self->_file)
self->_file = get_msg_file (self);
if (!self->_file && !(self->_file = get_msg_file (self)))
if (!mu_msg_load_msg_file (self, NULL))
return NULL;
return mu_msg_file_get_header (self->_file, header);

View File

@ -69,6 +69,18 @@ MuMsg *mu_msg_new_from_doc (XapianDocument* doc, GError **err)
G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
/**
* if we don't have a message file yet (because this message is
* database-backed), load it.
*
* @param msg a MuMsg
* @param err receives error information
*
* @return TRUE if this succceeded, FALSE in case of error
*/
gboolean mu_msg_load_msg_file (MuMsg *msg, GError **err);
/**
* close the file-backend, if any; this function is for the use case
* where you have a large amount of messages where you need some
@ -79,7 +91,9 @@ MuMsg *mu_msg_new_from_doc (XapianDocument* doc, GError **err)
*
* @param msg a message object
*/
void mu_msg_close_file_backend (MuMsg *msg);
void mu_msg_unload_msg_file (MuMsg *msg);
/**
* increase the reference count for this message