Now show sugar object choosers as file pickers in web activities
This commit is contained in:
parent
01ed63ee4b
commit
4ea52b3173
@ -17,6 +17,7 @@
|
||||
|
||||
import json
|
||||
import os
|
||||
import logging
|
||||
|
||||
from gi.repository import Gdk
|
||||
from gi.repository import Gio
|
||||
@ -25,10 +26,34 @@ from gi.repository import Gtk
|
||||
from gi.repository import GdkX11
|
||||
assert GdkX11
|
||||
|
||||
from sugar3.graphics.objectchooser import ObjectChooser
|
||||
from gi.repository import SugarExt
|
||||
from sugar3.activity import activity
|
||||
|
||||
|
||||
class FilePicker(ObjectChooser):
|
||||
def __init__(self, parent):
|
||||
ObjectChooser.__init__(self, parent)
|
||||
|
||||
def run(self):
|
||||
jobject = None
|
||||
_file = None
|
||||
try:
|
||||
result = ObjectChooser.run(self)
|
||||
if result == Gtk.ResponseType.ACCEPT:
|
||||
jobject = self.get_selected_object()
|
||||
logging.debug('FilePicker.run: %r', jobject)
|
||||
|
||||
if jobject and jobject.file_path:
|
||||
_file = jobject.file_path
|
||||
logging.debug('FilePicker.run: file=%r', _file)
|
||||
finally:
|
||||
if jobject is not None:
|
||||
jobject.destroy()
|
||||
|
||||
return _file
|
||||
|
||||
|
||||
class WebActivity(Gtk.Window):
|
||||
def __init__(self, handle):
|
||||
Gtk.Window.__init__(self)
|
||||
@ -51,6 +76,7 @@ class WebActivity(Gtk.Window):
|
||||
|
||||
self._web_view = WebKit2.WebView()
|
||||
self._web_view.connect("load-changed", self._loading_changed_cb)
|
||||
self._web_view.connect('run-file-chooser', self.__run_file_chooser)
|
||||
|
||||
self.add(self._web_view)
|
||||
self._web_view.show()
|
||||
@ -101,6 +127,17 @@ class WebActivity(Gtk.Window):
|
||||
|
||||
self._web_view.run_javascript(script, None, None, None)
|
||||
|
||||
def __run_file_chooser(self, browser, request):
|
||||
picker = FilePicker(self)
|
||||
chosen = picker.run()
|
||||
picker.destroy()
|
||||
|
||||
if chosen:
|
||||
request.select_files([chosen])
|
||||
else:
|
||||
request.cancel()
|
||||
return True
|
||||
|
||||
def _key_press_event_cb(self, window, event):
|
||||
key_name = Gdk.keyval_name(event.keyval)
|
||||
|
||||
|
@ -36,6 +36,7 @@ import mimetypes
|
||||
|
||||
from gi.repository import SugarExt
|
||||
from sugar3.activity import activity
|
||||
from sugar3.activity.webactivity import FilePicker
|
||||
|
||||
|
||||
class LocalRequestHandler(BaseHTTPRequestHandler):
|
||||
@ -135,6 +136,11 @@ class WebActivity(Gtk.Window):
|
||||
self._loading_changed_cb)
|
||||
self._web_view.connect("resource-request-starting",
|
||||
self._resource_request_starting_cb)
|
||||
try:
|
||||
self._web_view.connect('run-file-chooser', self.__run_file_chooser)
|
||||
except TypeError:
|
||||
# Only present in WebKit1 > 1.9.3 and WebKit2
|
||||
pass
|
||||
|
||||
self.add(self._web_view)
|
||||
self._web_view.show()
|
||||
@ -195,3 +201,12 @@ class WebActivity(Gtk.Window):
|
||||
""" % env_json
|
||||
|
||||
self._web_view.execute_script(script)
|
||||
|
||||
def __run_file_chooser(self, browser, request):
|
||||
picker = FilePicker(self)
|
||||
chosen = picker.run()
|
||||
picker.destroy()
|
||||
|
||||
if chosen:
|
||||
request.select_files([chosen])
|
||||
return True
|
||||
|
Loading…
Reference in New Issue
Block a user