---
config.h | 11 +++++++++++
main.c | 18 ++++++++++++++++++
utilities.c | 32 +++++++++++++++++++++++++++++++-
utilities.h | 3 +++
vimprobable.h | 5 +++++
5 files changed, 68 insertions(+), 1 deletions(-)
diff --git a/config.h b/config.h
index c4b5b27..876e190 100644
--- a/config.h
+++ b/config.h
@@ -67,6 +67,17 @@ static URIHandler uri_handlers[] = {
{ "ftp://", "x-terminal-emulator -e wget ftp://%s" },
};
+
+/* external file handlers:
+ * when a download is finished, look for an entry where the handle (first
+ * string) matches the beginning of its MIME type (Content-Type header). Then
+ * call the handler (second string) on it.
+ */
+static FileHandler file_handlers[] = {
+ { "application/pdf", "xpdf %s" },
+ { "application/postscript", "gv %s" },
+};
+
/* cookies */
#define ENABLE_COOKIE_SUPPORT
#define COOKIES_STORAGE_FILENAME "%s/vimprobable/cookies", config_base
diff --git a/main.c b/main.c
index 48e884f..e7c1aad 100644
--- a/main.c
+++ b/main.c
@@ -88,6 +88,7 @@ static void ascii_bar(int total, int state, char *string);
static gchar *jsapi_ref_to_string(JSContextRef context, JSValueRef ref);
static void jsapi_evaluate_script(const gchar *script, gchar **value, gchar **message);
static void download_progress(WebKitDownload *d, GParamSpec *pspec);
+static gboolean try_open_file_handler(WebKitDownload* d);
static void set_widget_font_and_color(GtkWidget *widget, const char *font_str,
const char *bg_color_str, const char *fg_color_str);
@@ -333,6 +334,7 @@ download_progress(WebKitDownload *d, GParamSpec *pspec) {
a.i = Info;
a.s = g_strdup_printf("Download %s finished", webkit_download_get_suggested_filename(d));
echo(&a);
+ try_open_file_handler(d);
}
g_free(a.s);
activeDownloads = g_list_remove(activeDownloads, d);
@@ -340,6 +342,21 @@ download_progress(WebKitDownload *d, GParamSpec *pspec) {
update_state();
}
+gboolean
+try_open_file_handler(WebKitDownload* d) {
+ WebKitNetworkResponse *resp = webkit_download_get_network_response(d);
+ SoupMessage *msg = webkit_network_response_get_message(resp);
+ SoupMessageHeaders *resp_headers = msg->response_headers;
+
+ const char* content_type = soup_message_headers_get_one(resp_headers,
+ "Content-Type");
+ const char* dst_uri = (const char*)
+ webkit_download_get_destination_uri(d);
+
+ if(!content_type)
+ return FALSE;
+ return open_file_handler(dst_uri, content_type);
+}
gboolean
process_keypress(GdkEventKey *event) {
@@ -2556,6 +2573,7 @@ main(int argc, char *argv[]) {
make_searchengines_list(searchengines, LENGTH(searchengines));
make_uri_handlers_list(uri_handlers, LENGTH(uri_handlers));
+ make_file_handlers_list(file_handlers, LENGTH(file_handlers));
/* Check if the specified file exists. */
/* And only warn the user, if they explicitly asked for a config on the
diff --git a/utilities.c b/utilities.c
index 387a466..7d41867 100644
--- a/utilities.c
+++ b/utilities.c
@@ -20,7 +20,8 @@ extern Key keys[];
extern char *error_msg;
extern gboolean complete_case_sensitive;
extern char *config_base;
-static GList *dynamic_searchengines = NULL, *dynamic_uri_handlers = NULL;
+static GList *dynamic_searchengines = NULL, *dynamic_uri_handlers = NULL,
+ *dynamic_file_handlers = NULL;
void add_modkeys(char key);
@@ -841,3 +842,32 @@ open_handler(char *uri) {
return FALSE;
}
+void make_file_handlers_list(FileHandler *file_handlers, int length)
+{
+ int i;
+ for (i = 0; i < length; i++, file_handlers++) {
+ dynamic_file_handlers = g_list_prepend(dynamic_file_handlers, file_handlers);
+ }
+}
+
+gboolean open_file_handler(const char *dst_uri, const char* content_type) {
+ const char* strip = "file://";
+ size_t striplen = strlen(strip);
+ if(!(strlen(dst_uri) >= striplen && !strncmp(dst_uri, strip, striplen)))
+ return FALSE; /* strange */
+ const char* path = dst_uri + striplen;
+
+ if(dynamic_file_handlers) {
+ GList *l;
+ for(l = dynamic_file_handlers; l; l = g_list_next(l)) {
+ FileHandler *s = (FileHandler*)l->data;
+ if(is_prefix_of(s->handle, content_type)) {
+ if(strlen(s->handler) > 0)
+ spawn_command_on(s->handler, path);
+ return TRUE;
+ }
+ }
+ }
+ return FALSE;
+}
+
diff --git a/utilities.h b/utilities.h
index 8cfd7b9..67338c1 100644
--- a/utilities.h
+++ b/utilities.h
@@ -34,7 +34,10 @@ void free_list(Listelement *elementlist);
char *find_uri_for_searchengine(const char *handle);
void make_searchengines_list(Searchengine *searchengines, int length);
void make_uri_handlers_list(URIHandler *uri_handlers, int length);
+void make_file_handlers_list(FileHandler *file_handlers, int length);
void spawn_command_on(const char* p_cmd, char* arg);
gboolean open_handler(char *uri);
+gboolean open_file_handler(const char *dst_uri, const char* content_type);
+
gboolean is_prefix_of(char* s, char* t);
diff --git a/vimprobable.h b/vimprobable.h
index 5a6c2df..55fbd67 100644
--- a/vimprobable.h
+++ b/vimprobable.h
@@ -143,6 +143,11 @@ typedef struct {
char *handler;
} URIHandler;
+typedef struct {
+ char *handle;
+ char *handler;
+} FileHandler;
+
struct map_pair {
char *line;
char what[20];
--
1.7.9.1
|