2012-03-15 22:07:02 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2012 André Stösel <andre@stoesel.de>
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
See the file COPYING for the full license text.
|
|
|
|
*/
|
|
|
|
|
|
|
|
using Gtk;
|
2012-03-31 20:12:48 +00:00
|
|
|
using Soup;
|
|
|
|
using Katze;
|
2012-03-15 22:07:02 +00:00
|
|
|
using Midori;
|
2012-03-31 20:12:48 +00:00
|
|
|
using WebKit;
|
2012-03-15 22:07:02 +00:00
|
|
|
|
|
|
|
namespace EDM {
|
|
|
|
[DBus (name = "net.launchpad.steadyflow.App")]
|
|
|
|
interface SteadyflowInterface : GLib.Object {
|
|
|
|
public abstract void AddFile (string url) throws IOError;
|
|
|
|
}
|
|
|
|
|
2012-03-31 20:12:48 +00:00
|
|
|
private class DownloadRequest : GLib.Object {
|
|
|
|
public string uri;
|
|
|
|
public string auth;
|
|
|
|
public string referer;
|
2012-04-05 19:25:50 +00:00
|
|
|
public string? cookie_header;
|
2012-03-31 20:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal Manager manager;
|
|
|
|
|
|
|
|
private class Manager : GLib.Object {
|
2012-04-05 19:25:50 +00:00
|
|
|
private CookieJar cookie_jar;
|
2012-03-31 20:12:48 +00:00
|
|
|
private GLib.PtrArray download_managers = new GLib.PtrArray ();
|
|
|
|
|
2012-03-19 18:08:11 +00:00
|
|
|
public bool download_requested (Midori.View view, WebKit.Download download) {
|
2012-03-15 22:07:02 +00:00
|
|
|
if (download.get_data<void*> ("save-as-download") == null
|
2012-04-22 12:21:11 +00:00
|
|
|
&& download.get_data<void*> ("open-download") == null
|
|
|
|
&& download.get_data<void*> ("cancel-download") == null) {
|
2012-03-31 20:12:48 +00:00
|
|
|
var dlReq = new DownloadRequest ();
|
|
|
|
dlReq.uri = download.get_uri ();
|
|
|
|
|
|
|
|
var request = download.get_network_request ();
|
|
|
|
var message = request.get_message ();
|
2012-04-05 19:25:50 +00:00
|
|
|
weak MessageHeaders headers = message.request_headers;
|
2012-03-31 20:12:48 +00:00
|
|
|
|
|
|
|
dlReq.auth = headers.get ("Authorization");
|
|
|
|
dlReq.referer = headers.get ("Referer");
|
2012-04-05 19:25:50 +00:00
|
|
|
dlReq.cookie_header = this.cookie_jar.get_cookies (new Soup.URI (dlReq.uri), true);
|
2012-03-31 20:12:48 +00:00
|
|
|
|
|
|
|
for (var i = 0 ; i < download_managers.len; i++) {
|
|
|
|
var dm = download_managers.index (i) as ExternalDownloadManager;
|
|
|
|
if (dm.download (dlReq))
|
|
|
|
return true;
|
2012-03-15 22:07:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void tab_added (Midori.Browser browser, Midori.View view) {
|
|
|
|
view.download_requested.connect (download_requested);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void tab_removed (Midori.Browser browser, Midori.View view) {
|
|
|
|
view.download_requested.disconnect(download_requested);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void browser_added (Midori.Browser browser) {
|
|
|
|
foreach (var tab in browser.get_tabs ())
|
|
|
|
tab_added (browser, tab);
|
|
|
|
browser.add_tab.connect (tab_added);
|
|
|
|
browser.remove_tab.connect (tab_removed);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void browser_removed (Midori.Browser browser) {
|
|
|
|
foreach (var tab in browser.get_tabs ())
|
|
|
|
tab_removed (browser, tab);
|
|
|
|
browser.add_tab.disconnect (tab_added);
|
|
|
|
browser.remove_tab.disconnect (tab_removed);
|
|
|
|
}
|
|
|
|
|
2012-03-31 20:12:48 +00:00
|
|
|
public void activated (Midori.Extension extension, Midori.App app) {
|
|
|
|
this.download_managers.add (extension);
|
|
|
|
if (this.download_managers.len == 1) {
|
|
|
|
foreach (var browser in app.get_browsers ())
|
|
|
|
browser_added (browser);
|
|
|
|
app.add_browser.connect (browser_added);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deactivated (Midori.Extension extension) {
|
|
|
|
this.download_managers.remove (extension);
|
|
|
|
if (this.download_managers.len == 0) {
|
|
|
|
var app = extension.get_app ();
|
|
|
|
foreach (var browser in app.get_browsers ())
|
|
|
|
browser_removed (browser);
|
|
|
|
app.add_browser.disconnect (browser_added);
|
|
|
|
}
|
|
|
|
}
|
2012-04-05 19:25:50 +00:00
|
|
|
|
|
|
|
construct {
|
|
|
|
var session = WebKit.get_default_session ();
|
|
|
|
this.cookie_jar = session.get_feature (typeof (CookieJar)) as CookieJar;
|
|
|
|
}
|
2012-03-31 20:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private abstract class ExternalDownloadManager : Midori.Extension {
|
2012-03-15 22:07:02 +00:00
|
|
|
public void activated (Midori.App app) {
|
2012-03-31 20:12:48 +00:00
|
|
|
manager.activated (this, app);
|
2012-03-15 22:07:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void deactivated () {
|
2012-03-31 20:12:48 +00:00
|
|
|
manager.deactivated (this);
|
|
|
|
}
|
|
|
|
|
2012-04-05 20:55:40 +00:00
|
|
|
public void handle_exception (GLib.Error error) {
|
|
|
|
string ext_name;
|
|
|
|
this.get ("name",out ext_name);
|
|
|
|
var dialog = new MessageDialog (null, DialogFlags.MODAL,
|
|
|
|
MessageType.ERROR, ButtonsType.CLOSE,
|
|
|
|
_("An error occurred when attempting to download a file with the following plugin:\n" +
|
|
|
|
"%s\n\n" +
|
|
|
|
"Error:\n%s\n\n" +
|
|
|
|
"Carry on without this plugin."
|
|
|
|
),
|
|
|
|
ext_name, error.message);
|
|
|
|
dialog.response.connect ((a) => { dialog.destroy (); });
|
|
|
|
dialog.run ();
|
|
|
|
}
|
|
|
|
|
2012-03-31 20:12:48 +00:00
|
|
|
public abstract bool download (DownloadRequest dlReq);
|
|
|
|
}
|
|
|
|
|
|
|
|
private class Aria2 : ExternalDownloadManager {
|
|
|
|
public override bool download (DownloadRequest dlReq) {
|
|
|
|
var url = value_array_new ();
|
2012-04-05 19:25:50 +00:00
|
|
|
value_array_insert (url, 0, typeof (string), dlReq.uri);
|
2012-03-31 20:12:48 +00:00
|
|
|
|
2012-04-10 16:53:15 +00:00
|
|
|
GLib.HashTable<string, GLib.Value?> options = value_hash_new ();
|
2012-03-31 20:12:48 +00:00
|
|
|
var referer = new GLib.Value (typeof (string));
|
|
|
|
referer.set_string (dlReq.referer);
|
|
|
|
options.insert ("referer", referer);
|
|
|
|
|
2012-04-05 19:25:50 +00:00
|
|
|
var headers = value_array_new ();
|
|
|
|
if (dlReq.cookie_header != null) {
|
|
|
|
value_array_insert (headers, 0, typeof (string), "Cookie: %s".printf(dlReq.cookie_header));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (headers.n_values > 0)
|
|
|
|
options.insert ("header", headers);
|
|
|
|
|
2012-03-31 20:12:48 +00:00
|
|
|
var message = XMLRPC.request_new ("http://127.0.0.1:6800/rpc",
|
|
|
|
"aria2.addUri",
|
|
|
|
typeof (ValueArray), url,
|
|
|
|
typeof(HashTable), options);
|
|
|
|
var session = new SessionSync ();
|
|
|
|
session.send_message (message);
|
|
|
|
|
|
|
|
try {
|
|
|
|
Value v;
|
|
|
|
XMLRPC.parse_method_response ((string) message.response_body.flatten ().data, -1, out v);
|
|
|
|
return true;
|
|
|
|
} catch (Error e) {
|
2012-04-05 20:55:40 +00:00
|
|
|
this.handle_exception (e);
|
2012-03-31 20:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2012-03-15 22:07:02 +00:00
|
|
|
}
|
|
|
|
|
2012-03-31 20:12:48 +00:00
|
|
|
internal Aria2 () {
|
|
|
|
GLib.Object (name: _("External Download Manager - Aria2"),
|
|
|
|
description: _("Download files with Aria2"),
|
|
|
|
version: "0.1" + Midori.VERSION_SUFFIX,
|
|
|
|
authors: "André Stösel <andre@stoesel.de>",
|
|
|
|
key: "aria2");
|
|
|
|
|
|
|
|
this.activate.connect (activated);
|
|
|
|
this.deactivate.connect (deactivated);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class SteadyFlow : ExternalDownloadManager {
|
|
|
|
public override bool download (DownloadRequest dlReq) {
|
|
|
|
try {
|
|
|
|
SteadyflowInterface dm = Bus.get_proxy_sync (
|
|
|
|
BusType.SESSION,
|
|
|
|
"net.launchpad.steadyflow.App",
|
|
|
|
"/net/launchpad/steadyflow/app");
|
|
|
|
dm.AddFile (dlReq.uri);
|
|
|
|
return true;
|
|
|
|
} catch (Error e) {
|
2012-04-05 20:55:40 +00:00
|
|
|
this.handle_exception (e);
|
2012-03-31 20:12:48 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal SteadyFlow () {
|
|
|
|
GLib.Object (name: _("External Download Manager - SteadyFlow"),
|
2012-03-15 22:07:02 +00:00
|
|
|
description: _("Download files with SteadyFlow"),
|
|
|
|
version: "0.1" + Midori.VERSION_SUFFIX,
|
2012-03-31 20:12:48 +00:00
|
|
|
authors: "André Stösel <andre@stoesel.de>",
|
|
|
|
key: "steadyflow");
|
2012-03-15 22:07:02 +00:00
|
|
|
|
|
|
|
this.activate.connect (activated);
|
|
|
|
this.deactivate.connect (deactivated);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-31 20:12:48 +00:00
|
|
|
public Katze.Array extension_init () {
|
|
|
|
EDM.manager = new EDM.Manager();
|
|
|
|
|
|
|
|
var extensions = new Katze.Array( typeof (Midori.Extension));
|
|
|
|
extensions.add_item (new EDM.Aria2 ());
|
|
|
|
extensions.add_item (new EDM.SteadyFlow ());
|
|
|
|
return extensions;
|
2012-03-15 22:07:02 +00:00
|
|
|
}
|
|
|
|
|