Add addon: downloader
This commit is contained in:
parent
fbfebe7c58
commit
c68e6843c3
|
@ -4,6 +4,7 @@ title
|
|||
favicon
|
||||
history
|
||||
newWindow
|
||||
downloader
|
||||
|
||||
# Settings
|
||||
settings
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
import gi
|
||||
gi.require_version("Gtk","3.0")
|
||||
gi.require_version("WebKit2","4.0")
|
||||
from gi.repository import Gtk,WebKit2,GLib
|
||||
|
||||
browser = mfp.require("browser")
|
||||
eventHandler = mfp.Bunch()
|
||||
|
||||
def downloadProgress(download,data_length):
|
||||
progress = str(int(download.props.estimated_progress * 100))
|
||||
if progress == download.cProgress: return
|
||||
download.cProgress = progress
|
||||
print(download.props.destination.replace("file://","",1)+ ": " +progress+ "%")
|
||||
|
||||
def downloadFailed(download,error):
|
||||
download.cError = error.message
|
||||
|
||||
def downloadFinished(download):
|
||||
print(download.props.destination.replace("file://","",1)+ ": " +download.cError)
|
||||
|
||||
def decideDestination(download,suggested_filename):
|
||||
fileChooser = Gtk.FileChooserNative()
|
||||
fileChooser.set_action(Gtk.FileChooserAction.SAVE)
|
||||
response = fileChooser.run()
|
||||
if response != Gtk.ResponseType.ACCEPT:
|
||||
download.cancel()
|
||||
return False
|
||||
|
||||
download.props.allow_overwrite = True
|
||||
download.set_destination("file://" +fileChooser.get_filename())
|
||||
download.cProgress = "-1.00"
|
||||
download.connect("received-data",downloadProgress)
|
||||
download.cError = "Finished."
|
||||
download.connect("failed",downloadFailed)
|
||||
download.connect("finished",downloadFinished)
|
||||
return False
|
||||
|
||||
def download(webContext,download):
|
||||
download.connect("decide-destination",decideDestination)
|
||||
|
||||
contextConnected = None
|
||||
def onWebviewCreated(webView):
|
||||
global contextConnected
|
||||
if contextConnected == None:
|
||||
contextConnected = webView.props.web_context
|
||||
contextConnected.connect("download-started",download)
|
||||
eventHandler["webview:created"] = onWebviewCreated
|
Loading…
Reference in New Issue