* change mu_util_play a bit to better deal with non-ascii filenames

- don't try to encode anything, but pass the filename into the argv for the
    program to open (before we, used g_spawn_command_line_async, and there we
    need to escape things for the command line. Now we use g_spawn_command, so
    we can pass argv as such.
  - add a GError** arg to mu_util_play, and update all callers
  - cleanup the mu-cmd-extract code a bit
This commit is contained in:
djcb
2012-04-15 19:59:53 +03:00
parent 7a04f14963
commit 6f4448be56
6 changed files with 108 additions and 91 deletions

View File

@ -38,35 +38,30 @@ save_part (MuMsg *msg, const char *targetdir, guint partidx, gboolean overwrite,
{ {
GError *err; GError *err;
gchar *filepath; gchar *filepath;
gboolean rv;
err = NULL; err = NULL;
rv = FALSE;
filepath = mu_msg_part_filepath (msg, targetdir, partidx, &err); filepath = mu_msg_part_filepath (msg, targetdir, partidx, &err);
if (!filepath) { if (!filepath)
if (err) { goto exit;
g_warning ("failed to save MIME-part: %s",
err->message);
g_error_free (err);
}
g_free (filepath);
return FALSE;
}
if (!mu_msg_part_save (msg, filepath, partidx, overwrite, FALSE, &err)) { if (!mu_msg_part_save (msg, filepath, partidx, overwrite, FALSE, &err))
if (err) { goto exit;
g_warning ("failed to save MIME-part: %s",
err->message);
g_error_free (err);
}
g_free (filepath);
return FALSE;
}
if (play) if (play)
mu_util_play (filepath, TRUE, FALSE); rv = mu_util_play (filepath, TRUE, FALSE, &err);
else
rv = TRUE;
exit:
if (err)
g_warning ("error with MIME-part: %s",
err->message);
g_clear_error (&err);
g_free (filepath); g_free (filepath);
return TRUE; return rv;
} }
@ -211,30 +206,27 @@ save_part_if (MuMsg *msg, MuMsgPart *part, SaveData *sd)
err = NULL; err = NULL;
filepath = mu_msg_part_filepath (msg, sd->targetdir, part->index, &err); filepath = mu_msg_part_filepath (msg, sd->targetdir, part->index, &err);
if (!filepath) { if (!filepath)
g_warning ("failed to get file path: %s", goto exit;
err&&err->message ? err->message : "error");
g_clear_error (&err);
goto leave;
}
if (!mu_msg_part_save (msg, filepath, part->index, if (!mu_msg_part_save (msg, filepath, part->index,
sd->overwrite, FALSE, &err)) { sd->overwrite, FALSE, &err))
g_warning ("failed to save MIME-part: %s", goto exit;
err&&err->message ? err->message : "error");
g_clear_error (&err);
goto leave;
}
if (sd->play && !mu_util_play (filepath, TRUE, FALSE)) if (sd->play)
goto leave; rv = mu_util_play (filepath, TRUE, FALSE, &err);
else
rv = TRUE;
rv = TRUE;
++sd->saved_num; ++sd->saved_num;
exit:
if (err)
g_warning ("error saving MIME part: %s", err->message);
leave:
sd->result = rv;
g_free (filepath); g_free (filepath);
g_clear_error (&err);
sd->result = rv;
} }
static gboolean static gboolean

View File

@ -238,6 +238,38 @@ get_docid_from_msgid (MuQuery *query, const char *str, GError **err)
} }
/* get a *list* of all messages with the given message id */
G_GNUC_UNUSED static GSList*
get_docid_from_msgid_list (MuQuery *query, const char *str, GError **err)
{
gchar *querystr;
MuMsgIter *iter;
GSList *lst;
querystr = g_strdup_printf ("msgid:%s", str);
iter = mu_query_run (query, querystr, FALSE,
MU_MSG_FIELD_ID_NONE, FALSE, 1, err);
g_free (querystr);
if (!iter || mu_msg_iter_is_done (iter)) {
mu_util_g_set_error (err, MU_ERROR_NO_MATCHES,
"could not find message %s", str);
return NULL;
}
for (lst = NULL; !mu_msg_iter_is_done (iter); mu_msg_iter_next(iter)) {
unsigned docid;
docid = mu_msg_iter_get_docid (iter);
lst = g_slist_prepend
(lst, GUINT_TO_POINTER(docid));
}
mu_msg_iter_destroy (iter);
return lst;
}
/* the string contains either a number (docid) or a message-id if it /* the string contains either a number (docid) or a message-id if it
* doesn't look like a number, and the query param is non-nil, try to * doesn't look like a number, and the query param is non-nil, try to
* locale the message with message-id in the database, and return its * locale the message with message-id in the database, and return its
@ -510,11 +542,10 @@ open_part (MuMsg *msg, unsigned index, GError **err)
} }
rv = mu_util_play (targetpath, TRUE/*allow local*/, rv = mu_util_play (targetpath, TRUE/*allow local*/,
FALSE/*allow remote*/); FALSE/*allow remote*/, err);
if (!rv) { if (!rv)
print_error (MU_ERROR_FILE, "failed to open"); print_and_clear_g_error (err);
goto leave; else {
} else {
gchar *path; gchar *path;
path = mu_str_escape_c_literal (targetpath, FALSE); path = mu_str_escape_c_literal (targetpath, FALSE);
print_expr ("(:info open :message \"%s has been opened\")", print_expr ("(:info open :message \"%s has been opened\")",
@ -615,10 +646,6 @@ cmd_extract (MuStore *store, MuQuery *query, GSList *args, GError **err)
/* /*
* 'find' finds a list of messages matching some query, and takes a * 'find' finds a list of messages matching some query, and takes a
* parameter 'query' with the search query, and (optionally) a * parameter 'query' with the search query, and (optionally) a

View File

@ -323,12 +323,12 @@ mu_util_is_local_file (const char* path)
gboolean gboolean
mu_util_play (const char *path, gboolean allow_local, gboolean allow_remote) mu_util_play (const char *path, gboolean allow_local, gboolean allow_remote,
GError **err)
{ {
gboolean rv; gboolean rv;
GError *err; const gchar *argv[3];
const gchar *prog; const char *prog;
char *cmdline, *escpath;
g_return_val_if_fail (path, FALSE); g_return_val_if_fail (path, FALSE);
g_return_val_if_fail (mu_util_is_local_file (path) || allow_remote, g_return_val_if_fail (mu_util_is_local_file (path) || allow_remote,
@ -345,19 +345,17 @@ mu_util_play (const char *path, gboolean allow_local, gboolean allow_remote)
#endif /*!__APPLE__*/ #endif /*!__APPLE__*/
} }
escpath = g_strescape (path, NULL); argv[0] = prog;
cmdline = g_strdup_printf ("%s \"%s\"", prog, escpath); argv[1] = path;
g_free (escpath); argv[2] = NULL;
err = NULL; err = NULL;
rv = g_spawn_command_line_async (cmdline, &err); rv = g_spawn_async (NULL,
if (!rv) { (gchar**)&argv,
g_warning ("failed to spawn %s: %s", NULL,
cmdline, err->message ? err->message : "error"); G_SPAWN_SEARCH_PATH,
g_error_free (err); NULL, NULL, NULL,
} err);
g_free (cmdline);
return rv; return rv;
} }

View File

@ -178,11 +178,12 @@ gboolean mu_util_printerr_encoded (const char *frm, ...) G_GNUC_PRINTF(1,2);
* @param path full path of the file to open * @param path full path of the file to open
* @param allow_local allow local files (ie. with file:// prefix or fs paths) * @param allow_local allow local files (ie. with file:// prefix or fs paths)
* @param allow_remote allow URIs (ie., http, mailto) * @param allow_remote allow URIs (ie., http, mailto)
* @param err receives error information, if any
* *
* @return TRUE if it succeeded, FALSE otherwise * @return TRUE if it succeeded, FALSE otherwise
*/ */
gboolean mu_util_play (const char *path, gboolean mu_util_play (const char *path, gboolean allow_local,
gboolean allow_local, gboolean allow_remote); gboolean allow_remote, GError **err);

View File

@ -65,7 +65,7 @@ set_message (MuMsgBodyView *self, MuMsg *msg)
{ {
if (self->_priv->_msg == msg) if (self->_priv->_msg == msg)
return; /* nothing to todo */ return; /* nothing to todo */
if (self->_priv->_msg) { if (self->_priv->_msg) {
mu_msg_unref (self->_priv->_msg); mu_msg_unref (self->_priv->_msg);
self->_priv->_msg = NULL; self->_priv->_msg = NULL;
@ -104,7 +104,7 @@ save_file_for_cid (MuMsg *msg, const char* cid)
gchar *filepath; gchar *filepath;
gboolean rv; gboolean rv;
GError *err; GError *err;
g_return_val_if_fail (msg, NULL); g_return_val_if_fail (msg, NULL);
g_return_val_if_fail (cid, NULL); g_return_val_if_fail (cid, NULL);
@ -144,19 +144,19 @@ on_navigation_policy_decision_requested (MuMsgBodyView *self, WebKitWebFrame *fr
{ {
const char* uri; const char* uri;
WebKitWebNavigationReason reason; WebKitWebNavigationReason reason;
uri = webkit_network_request_get_uri (request); uri = webkit_network_request_get_uri (request);
reason = webkit_web_navigation_action_get_reason (nav_action); reason = webkit_web_navigation_action_get_reason (nav_action);
/* if it wasn't a user click, don't the navigation */ /* if it wasn't a user click, don't the navigation */
if (reason != WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) { if (reason != WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
webkit_web_policy_decision_ignore (policy_decision); webkit_web_policy_decision_ignore (policy_decision);
return TRUE; return TRUE;
} }
/* we handle links clicked ourselves, no need for navigation */ /* we handle links clicked ourselves, no need for navigation */
webkit_web_policy_decision_ignore (policy_decision); webkit_web_policy_decision_ignore (policy_decision);
/* if there are 'cmd:<action>" links in the body text of /* if there are 'cmd:<action>" links in the body text of
* mu-internal messages (ie., notification from mu, not real * mu-internal messages (ie., notification from mu, not real
* e-mail messages), we emit the 'action requested' * e-mail messages), we emit the 'action requested'
@ -171,12 +171,12 @@ on_navigation_policy_decision_requested (MuMsgBodyView *self, WebKitWebFrame *fr
} }
return TRUE; return TRUE;
} }
/* don't try to play files on our local file system, this is not something /* don't try to play files on our local file system, this is not something
* external content should do.*/ * external content should do.*/
if (!mu_util_is_local_file(uri)) if (!mu_util_is_local_file(uri))
mu_util_play (uri, FALSE, TRUE); mu_util_play (uri, FALSE, TRUE, NULL);
return TRUE; return TRUE;
} }
@ -194,7 +194,7 @@ on_resource_request_starting (MuMsgBodyView *self, WebKitWebFrame *frame,
uri = webkit_network_request_get_uri (request); uri = webkit_network_request_get_uri (request);
/* g_warning ("%s: %s", __FUNCTION__, uri); */ /* g_warning ("%s: %s", __FUNCTION__, uri); */
if (g_ascii_strncasecmp (uri, "cid:", 4) == 0) { if (g_ascii_strncasecmp (uri, "cid:", 4) == 0) {
gchar *filepath; gchar *filepath;
filepath = save_file_for_cid (msg, uri); filepath = save_file_for_cid (msg, uri);
@ -214,7 +214,7 @@ on_menu_item_activate (GtkMenuItem *item, MuMsgBodyView *self)
{ {
g_signal_emit (G_OBJECT(self), g_signal_emit (G_OBJECT(self),
signals[ACTION_REQUESTED], 0, signals[ACTION_REQUESTED], 0,
g_object_get_data (G_OBJECT(item), "action")); g_object_get_data (G_OBJECT(item), "action"));
} }
static void static void
@ -230,15 +230,15 @@ popup_menu (MuMsgBodyView *self, guint button, guint32 activate_time)
{ "View source...", "view-source", VIEW_MODE_MSG }, { "View source...", "view-source", VIEW_MODE_MSG },
{ "View message...", "view-message", VIEW_MODE_SOURCE }, { "View message...", "view-message", VIEW_MODE_SOURCE },
}; };
menu = gtk_menu_new (); menu = gtk_menu_new ();
for (i = 0; i != G_N_ELEMENTS(actions); ++i) { for (i = 0; i != G_N_ELEMENTS(actions); ++i) {
GtkWidget *item; GtkWidget *item;
if (self->_priv->_view_mode != actions[i].mode) if (self->_priv->_view_mode != actions[i].mode)
continue; continue;
item = gtk_menu_item_new_with_label(actions[i].title); item = gtk_menu_item_new_with_label(actions[i].title);
g_object_set_data (G_OBJECT(item), "action", (gpointer)actions[i].action); g_object_set_data (G_OBJECT(item), "action", (gpointer)actions[i].action);
g_signal_connect (item, "activate", G_CALLBACK(on_menu_item_activate), g_signal_connect (item, "activate", G_CALLBACK(on_menu_item_activate),
@ -263,8 +263,8 @@ on_button_press_event (MuMsgBodyView *self, GdkEventButton *event, gpointer data
break; break;
default: return TRUE; /* ignore */ default: return TRUE; /* ignore */
} }
return (event->button > 1) ? TRUE : FALSE; return (event->button > 1) ? TRUE : FALSE;
} }
@ -275,7 +275,7 @@ mu_msg_body_view_init (MuMsgBodyView *obj)
obj->_priv->_msg = NULL; obj->_priv->_msg = NULL;
obj->_priv->_view_mode = VIEW_MODE_NONE; obj->_priv->_view_mode = VIEW_MODE_NONE;
obj->_priv->_settings = webkit_web_settings_new (); obj->_priv->_settings = webkit_web_settings_new ();
g_object_set (G_OBJECT(obj->_priv->_settings), g_object_set (G_OBJECT(obj->_priv->_settings),
"enable-scripts", FALSE, "enable-scripts", FALSE,
@ -308,7 +308,7 @@ mu_msg_body_view_finalize (GObject *obj)
g_object_unref (priv->_settings); g_object_unref (priv->_settings);
set_message (MU_MSG_BODY_VIEW(obj), NULL); set_message (MU_MSG_BODY_VIEW(obj), NULL);
G_OBJECT_CLASS(parent_class)->finalize (obj); G_OBJECT_CLASS(parent_class)->finalize (obj);
} }
@ -335,7 +335,7 @@ static void
set_text (MuMsgBodyView *self, const char* txt) set_text (MuMsgBodyView *self, const char* txt)
{ {
g_return_if_fail (MU_IS_MSG_BODY_VIEW(self)); g_return_if_fail (MU_IS_MSG_BODY_VIEW(self));
webkit_web_view_load_string (WEBKIT_WEB_VIEW(self), webkit_web_view_load_string (WEBKIT_WEB_VIEW(self),
txt ? txt : "", txt ? txt : "",
"text/plain", "text/plain",
@ -347,13 +347,13 @@ void
mu_msg_body_view_set_message (MuMsgBodyView *self, MuMsg *msg) mu_msg_body_view_set_message (MuMsgBodyView *self, MuMsg *msg)
{ {
const char* data; const char* data;
g_return_if_fail (self); g_return_if_fail (self);
set_message (self, msg); set_message (self, msg);
data = msg ? mu_msg_get_body_html (msg) : ""; data = msg ? mu_msg_get_body_html (msg) : "";
if (data) if (data)
set_html (self, data); set_html (self, data);
else else
set_text (self, mu_msg_get_body_text (msg)); set_text (self, mu_msg_get_body_text (msg));
@ -372,9 +372,9 @@ mu_msg_body_view_set_message_source (MuMsgBodyView *self, MuMsg *msg)
g_return_if_fail (msg); g_return_if_fail (msg);
set_message (self, NULL); set_message (self, NULL);
path = msg ? mu_msg_get_path (msg) : NULL; path = msg ? mu_msg_get_path (msg) : NULL;
if (path && g_file_get_contents (path, &data, NULL, NULL)) { if (path && g_file_get_contents (path, &data, NULL, NULL)) {
set_text (self, data); set_text (self, data);
g_free (data); g_free (data);
@ -391,9 +391,9 @@ mu_msg_body_view_set_note (MuMsgBodyView *self, const gchar *html)
{ {
g_return_if_fail (self); g_return_if_fail (self);
g_return_if_fail (html); g_return_if_fail (html);
set_message (self, NULL); set_message (self, NULL);
set_html (self, html); set_html (self, html);
self->_priv->_view_mode = VIEW_MODE_NOTE; self->_priv->_view_mode = VIEW_MODE_NOTE;

View File

@ -134,11 +134,10 @@ on_attach_activated (GtkWidget *w, guint partnum, MuMsg *msg)
if (!mu_msg_part_save (msg, filepath, partnum, FALSE, TRUE, &err)) { if (!mu_msg_part_save (msg, filepath, partnum, FALSE, TRUE, &err)) {
g_warning ("failed to save %s: %s", filepath, g_warning ("failed to save %s: %s", filepath,
err&&err->message?err->message:"error"); err&&err->message?err->message:"error");
if (err) g_clear_error (&err);
g_error_free(err);
} }
mu_util_play (filepath, TRUE, FALSE); mu_util_play (filepath, TRUE, FALSE, NULL);
g_free (filepath); g_free (filepath);
} }
} }