summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Alan Fitton <ajf@eth0.org.uk>2011-09-30 08:28:32 +0000
committerGravatar Alan Fitton <ajf@eth0.org.uk>2011-09-30 08:28:32 +0000
commita61f54aadce62bb71e076f3902a3ca0c59fadb20 (patch)
treefe298593d337a37c87e95e19a35cb3ccf616f812 /src
parente686fec66c00a77ed532013223ca0683c8ebf3d6 (diff)
some improvements to remote exec. can now use profile options like %{hostname}
Diffstat (limited to 'src')
-rw-r--r--src/remote-exec.c54
-rw-r--r--src/remote-exec.h2
-rw-r--r--src/torrent.c21
-rw-r--r--src/torrent.h1
-rw-r--r--src/trg-main-window.c18
-rw-r--r--src/trg-preferences-dialog.c8
-rw-r--r--src/trg-prefs.h2
7 files changed, 81 insertions, 25 deletions
diff --git a/src/remote-exec.c b/src/remote-exec.c
index c0a6b91..91bf867 100644
--- a/src/remote-exec.c
+++ b/src/remote-exec.c
@@ -20,6 +20,9 @@
#include <glib.h>
#include <json-glib/json-glib.h>
+#include "protocol-constants.h"
+#include "torrent.h"
+
static const char json_exceptions[] = { 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84,
0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90,
0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c,
@@ -76,37 +79,52 @@ static gchar *dump_json_value(JsonNode * node) {
return g_string_free(buffer, FALSE);
}
-gchar *build_remote_exec_cmd(JsonObject * obj, const gchar * input) {
+gchar *build_remote_exec_cmd(TrgPrefs *prefs, JsonObject * torrent,
+ const gchar * input) {
+ JsonObject *profile = trg_prefs_get_profile(prefs);
gchar *work = g_strdup(input);
GRegex *regex, *replacerx;
GMatchInfo *match_info;
gchar *whole, *id, *tmp, *valuestr;
JsonNode *replacement;
- GError *error = NULL;
- regex = g_regex_new("%{(\\w+)}", 0, 0, NULL);
+ regex = g_regex_new("%{([A-Za-z\\-]+)}", 0, 0, NULL);
g_regex_match_full(regex, input, -1, 0, 0, &match_info, NULL);
if (match_info) {
while (g_match_info_matches(match_info)) {
id = g_match_info_fetch(match_info, 1);
+ whole = g_match_info_fetch(match_info, 0);
+ replacerx = g_regex_new(whole, 0, 0, NULL);
+ valuestr = NULL;
+
+ if (json_object_has_member(torrent, id)) {
+ replacement = json_object_get_member(torrent, id);
+ } else if (json_object_has_member(profile, id)) {
+ replacement = json_object_get_member(profile, id);
+ } else {
+ replacement = NULL;
+ }
- if (json_object_has_member(obj, id)) {
- replacement = json_object_get_member(obj, id);
- if (JSON_NODE_HOLDS_VALUE(replacement)) {
- whole = g_match_info_fetch(match_info, 0);
- replacerx = g_regex_new(whole, 0, 0, &error);
- valuestr = dump_json_value(replacement);
- tmp = g_regex_replace(replacerx, work, -1, 0, valuestr, 0,
- &error);
- g_free(work);
- work = tmp;
- g_free(valuestr);
- g_free(whole);
- g_free(id);
- g_regex_unref(replacerx);
- }
+ if (replacement && JSON_NODE_HOLDS_VALUE(replacement)) {
+ valuestr = dump_json_value(replacement);
+ } else if (!g_strcmp0(id, "full-path")) {
+ valuestr = torrent_get_full_path(torrent);
}
+
+ if (valuestr)
+ {
+ tmp
+ = g_regex_replace(replacerx, work, -1, 0, valuestr, 0,
+ NULL);
+ g_free(work);
+ work = tmp;
+ g_free(valuestr);
+ }
+
+ g_regex_unref(replacerx);
+ g_free(whole);
+ g_free(id);
g_match_info_next(match_info, NULL);
}
g_match_info_free(match_info);
diff --git a/src/remote-exec.h b/src/remote-exec.h
index 779324c..0596d04 100644
--- a/src/remote-exec.h
+++ b/src/remote-exec.h
@@ -20,6 +20,6 @@
#ifndef REMOTE_EXEC_H_
#define REMOTE_EXEC_H_
-gchar *build_remote_exec_cmd(JsonObject * obj, const gchar * input);
+gchar *build_remote_exec_cmd(TrgPrefs *prefs, JsonObject * torrent, const gchar * input);
#endif /* REMOTE_EXEC_H_ */
diff --git a/src/torrent.c b/src/torrent.c
index 426a2ce..046693a 100644
--- a/src/torrent.c
+++ b/src/torrent.c
@@ -439,3 +439,24 @@ const gchar *tracker_stats_get_host(JsonObject *t)
{
return json_object_get_string_member(t, FIELD_HOST);
}
+
+gchar *torrent_get_full_path(JsonObject * obj) {
+ gchar *containing_path, *name, *delim;
+ const gchar *location;
+ JsonArray *files = torrent_get_files(obj);
+ JsonObject *firstFile;
+
+ location = json_object_get_string_member(obj, FIELD_DOWNLOAD_DIR);
+ firstFile = json_array_get_object_element(files, 0);
+ name = g_strdup(json_object_get_string_member(firstFile, TFILE_NAME));
+
+ if ( (delim = g_strstr_len(name,-1,"/")) ) {
+ *delim = '\0';
+ containing_path = g_strdup_printf("%s/%s",location,name);
+ } else {
+ containing_path = g_strdup(location);
+ }
+
+ g_free(name);
+ return containing_path;
+}
diff --git a/src/torrent.h b/src/torrent.h
index 9d82e28..d53be6a 100644
--- a/src/torrent.h
+++ b/src/torrent.h
@@ -86,6 +86,7 @@ gint64 torrent_get_peer_limit(JsonObject * t);
gboolean torrent_has_tracker(JsonObject * t, GRegex * rx, gchar * search);
gint64 torrent_get_queue_position(JsonObject *args);
gint64 torrent_get_activity_date(JsonObject *t);
+gchar *torrent_get_full_path(JsonObject * obj);
/* outer response object */
diff --git a/src/trg-main-window.c b/src/trg-main-window.c
index e763100..e67577f 100644
--- a/src/trg-main-window.c
+++ b/src/trg-main-window.c
@@ -1452,6 +1452,7 @@ static GtkWidget *limit_menu_new(TrgMainWindow * win, gchar * title,
static void exec_cmd_cb(GtkWidget *w, gpointer data)
{
TrgMainWindowPrivate *priv = TRG_MAIN_WINDOW_GET_PRIVATE(data);
+ TrgPrefs *prefs = trg_client_get_prefs(priv->client);
JsonObject *cmd_obj = (JsonObject*)g_object_get_data(G_OBJECT(w), "cmd-object");
GtkTreeSelection *selection;
gint rowCount;
@@ -1461,13 +1462,13 @@ static void exec_cmd_cb(GtkWidget *w, gpointer data)
if (rowCount==1) {
JsonObject *json;
- GError *cmd_error;
+ GError *cmd_error = NULL;
gchar *cmd_line, **argv;
if (!get_torrent_data(trg_client_get_torrent_table(priv->client),
priv->selectedTorrentId, &json,
NULL))
return;
- cmd_line = build_remote_exec_cmd(json, json_object_get_string_member(cmd_obj, "cmd"));
+ cmd_line = build_remote_exec_cmd(prefs, json, json_object_get_string_member(cmd_obj, TRG_PREFS_KEY_EXEC_COMMANDS_SUBKEY_CMD));
g_debug("Exec: %s",cmd_line);
//GTK has bug, won't let you pass a string here containing a quoted param, so use parse and then spawn
// rather than g_spawn_command_line_async(cmd_line,&cmd_error);
@@ -1476,6 +1477,19 @@ static void exec_cmd_cb(GtkWidget *w, gpointer data)
NULL, NULL, NULL, &cmd_error );
g_strfreev( argv );
g_free( cmd_line );
+
+ if (cmd_error)
+ {
+ GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(data),
+ GTK_DIALOG_MODAL,
+ GTK_MESSAGE_ERROR,
+ GTK_BUTTONS_OK, "%s",
+ cmd_error->message);
+ gtk_window_set_title(GTK_WINDOW(dialog), _("Error"));
+ gtk_dialog_run(GTK_DIALOG(dialog));
+ gtk_widget_destroy(dialog);
+ g_error_free(cmd_error);
+ }
}
}
diff --git a/src/trg-preferences-dialog.c b/src/trg-preferences-dialog.c
index b9e87a6..d56c15f 100644
--- a/src/trg-preferences-dialog.c
+++ b/src/trg-preferences-dialog.c
@@ -609,8 +609,8 @@ static void trg_prefs_etv_refresh(TrgPrefs *prefs, void *wdp) {
JsonObject *jobj = json_node_get_object(ja_node);
gtk_list_store_append(model, &iter);
gtk_list_store_set(model, &iter, 0,
- json_object_get_string_member(jobj, "label"), 1,
- json_object_get_string_member(jobj, "cmd"), -1);
+ json_object_get_string_member(jobj, TRG_PREFS_KEY_EXEC_COMMANDS_SUBKEY_LABEL), 1,
+ json_object_get_string_member(jobj, TRG_PREFS_KEY_EXEC_COMMANDS_SUBKEY_CMD), -1);
}
g_list_free(ja_list);
@@ -624,8 +624,8 @@ static gboolean trg_prefs_etc_save_foreachfunc(GtkTreeModel *model,
gtk_tree_model_get(model, iter, 0, &label, 1, &cmd, -1);
- json_object_set_string_member(new, "label", label);
- json_object_set_string_member(new, "cmd", cmd);
+ json_object_set_string_member(new, TRG_PREFS_KEY_EXEC_COMMANDS_SUBKEY_LABEL, label);
+ json_object_set_string_member(new, TRG_PREFS_KEY_EXEC_COMMANDS_SUBKEY_CMD, cmd);
json_array_add_object_element(ja, new);
diff --git a/src/trg-prefs.h b/src/trg-prefs.h
index 6d89531..c5ab1c2 100644
--- a/src/trg-prefs.h
+++ b/src/trg-prefs.h
@@ -69,6 +69,8 @@
#define TRG_PREFS_KEY_NOTEBOOK_PANED_POS "notebook-paned-pos"
#define TRG_PREFS_KEY_STATES_PANED_POS "states-paned-pos"
#define TRG_PREFS_KEY_EXEC_COMMANDS "exec-commands"
+#define TRG_PREFS_KEY_EXEC_COMMANDS_SUBKEY_CMD "cmd"
+#define TRG_PREFS_KEY_EXEC_COMMANDS_SUBKEY_LABEL "label"
#define TRG_PREFS_NOFLAGS (1 << 0) /* 0x00 */
#define TRG_PREFS_GLOBAL (1 << 1) /* 0x01 */