2013-04-23 15:47:26 +02:00
|
|
|
# Copyright (C) 2013 Daniel Narvaez
|
|
|
|
#
|
|
|
|
# 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 of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, write to the
|
|
|
|
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
# Boston, MA 02111-1307, USA.
|
|
|
|
|
2013-06-04 21:36:01 +02:00
|
|
|
import json
|
2013-04-23 15:47:26 +02:00
|
|
|
import os
|
2014-03-29 05:37:50 +01:00
|
|
|
import logging
|
2013-04-23 15:47:26 +02:00
|
|
|
|
2017-05-19 06:22:23 +02:00
|
|
|
import gi
|
|
|
|
gi.require_version('Gtk', '3.0')
|
|
|
|
gi.require_version('WebKit2', '4.0')
|
|
|
|
|
2013-05-06 23:32:40 +02:00
|
|
|
from gi.repository import Gdk
|
2013-05-06 22:07:27 +02:00
|
|
|
from gi.repository import Gio
|
2013-04-23 15:47:26 +02:00
|
|
|
from gi.repository import WebKit2
|
2013-05-11 00:48:18 +02:00
|
|
|
from gi.repository import Gtk
|
2013-05-11 01:17:30 +02:00
|
|
|
from gi.repository import GdkX11
|
|
|
|
assert GdkX11
|
2013-04-23 15:47:26 +02:00
|
|
|
|
2014-03-29 05:37:50 +01:00
|
|
|
from sugar3.graphics.objectchooser import ObjectChooser
|
2013-05-11 00:48:18 +02:00
|
|
|
from gi.repository import SugarExt
|
2013-07-04 22:34:31 +02:00
|
|
|
from sugar3.activity import activity
|
2013-04-23 15:47:26 +02:00
|
|
|
|
2013-07-05 00:57:03 +02:00
|
|
|
|
2014-03-29 05:37:50 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2013-05-30 14:55:27 +02:00
|
|
|
class WebActivity(Gtk.Window):
|
2013-04-23 15:47:26 +02:00
|
|
|
def __init__(self, handle):
|
2013-05-11 00:48:18 +02:00
|
|
|
Gtk.Window.__init__(self)
|
|
|
|
|
|
|
|
self._activity_id = handle.activity_id
|
2013-06-04 21:36:01 +02:00
|
|
|
self._object_id = handle.object_id
|
2013-05-11 00:48:18 +02:00
|
|
|
self._bundle_id = os.environ["SUGAR_BUNDLE_ID"]
|
|
|
|
self._bundle_path = os.environ["SUGAR_BUNDLE_PATH"]
|
2013-05-11 23:01:17 +02:00
|
|
|
self._inspector_visible = False
|
2013-05-11 00:48:18 +02:00
|
|
|
|
|
|
|
self.set_decorated(False)
|
|
|
|
self.maximize()
|
2013-04-23 15:47:26 +02:00
|
|
|
|
2013-05-06 23:32:40 +02:00
|
|
|
self.connect("key-press-event", self._key_press_event_cb)
|
2013-05-11 00:48:18 +02:00
|
|
|
self.connect('realize', self._realize_cb)
|
2013-08-21 18:23:13 +02:00
|
|
|
self.connect('destroy', self._destroy_cb)
|
2013-05-06 23:32:40 +02:00
|
|
|
|
2013-05-06 22:07:27 +02:00
|
|
|
context = WebKit2.WebContext.get_default()
|
|
|
|
context.register_uri_scheme("activity", self._app_scheme_cb, None)
|
|
|
|
|
2013-04-23 15:47:26 +02:00
|
|
|
self._web_view = WebKit2.WebView()
|
2013-05-10 20:44:58 +02:00
|
|
|
self._web_view.connect("load-changed", self._loading_changed_cb)
|
2014-03-29 05:37:50 +01:00
|
|
|
self._web_view.connect('run-file-chooser', self.__run_file_chooser)
|
2013-05-10 20:44:58 +02:00
|
|
|
|
2013-05-11 00:48:18 +02:00
|
|
|
self.add(self._web_view)
|
2013-04-23 15:47:26 +02:00
|
|
|
self._web_view.show()
|
|
|
|
|
2013-05-06 23:32:40 +02:00
|
|
|
settings = self._web_view.get_settings()
|
|
|
|
settings.set_property("enable-developer-extras", True)
|
|
|
|
|
2013-05-11 01:17:30 +02:00
|
|
|
self._web_view.load_uri("activity://%s/index.html" % self._bundle_id)
|
2013-05-11 00:48:18 +02:00
|
|
|
|
2013-08-26 22:15:23 +02:00
|
|
|
self.set_title(activity.get_bundle_name())
|
|
|
|
|
2013-05-11 00:48:18 +02:00
|
|
|
def run_main_loop(self):
|
|
|
|
Gtk.main()
|
|
|
|
|
|
|
|
def _realize_cb(self, window):
|
|
|
|
xid = window.get_window().get_xid()
|
|
|
|
SugarExt.wm_set_bundle_id(xid, self._bundle_id)
|
|
|
|
SugarExt.wm_set_activity_id(xid, str(self._activity_id))
|
2013-05-01 19:03:52 +02:00
|
|
|
|
2013-08-21 18:23:13 +02:00
|
|
|
def _destroy_cb(self, window):
|
|
|
|
self.destroy()
|
|
|
|
Gtk.main_quit()
|
|
|
|
|
2013-05-10 20:44:58 +02:00
|
|
|
def _loading_changed_cb(self, web_view, load_event):
|
|
|
|
if load_event == WebKit2.LoadEvent.FINISHED:
|
2013-05-11 00:09:51 +02:00
|
|
|
key = os.environ["SUGAR_APISOCKET_KEY"]
|
|
|
|
port = os.environ["SUGAR_APISOCKET_PORT"]
|
|
|
|
|
2013-06-04 21:36:01 +02:00
|
|
|
env_json = json.dumps({"apiSocketKey": key,
|
|
|
|
"apiSocketPort": port,
|
|
|
|
"activityId": self._activity_id,
|
|
|
|
"bundleId": self._bundle_id,
|
2013-07-04 22:34:31 +02:00
|
|
|
"objectId": self._object_id,
|
|
|
|
"activityName": activity.get_bundle_name()})
|
2013-06-04 21:36:01 +02:00
|
|
|
|
2013-06-01 14:59:48 +02:00
|
|
|
script = """
|
2013-06-04 21:36:01 +02:00
|
|
|
var environment = %s;
|
2013-06-01 14:59:48 +02:00
|
|
|
|
2013-06-04 21:36:01 +02:00
|
|
|
if (window.sugar === undefined) {
|
|
|
|
window.sugar = {};
|
|
|
|
}
|
2013-06-01 14:59:48 +02:00
|
|
|
|
2013-06-04 21:36:01 +02:00
|
|
|
window.sugar.environment = environment;
|
2013-06-01 14:59:48 +02:00
|
|
|
|
2013-06-04 21:36:01 +02:00
|
|
|
if (window.sugar.onEnvironmentSet)
|
|
|
|
window.sugar.onEnvironmentSet();
|
|
|
|
""" % env_json
|
2013-05-10 20:44:58 +02:00
|
|
|
|
|
|
|
self._web_view.run_javascript(script, None, None, None)
|
|
|
|
|
2014-03-29 05:37:50 +01:00
|
|
|
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
|
|
|
|
|
2013-05-06 23:32:40 +02:00
|
|
|
def _key_press_event_cb(self, window, event):
|
|
|
|
key_name = Gdk.keyval_name(event.keyval)
|
|
|
|
|
|
|
|
if event.get_state() & Gdk.ModifierType.CONTROL_MASK and \
|
|
|
|
event.get_state() & Gdk.ModifierType.SHIFT_MASK:
|
|
|
|
if key_name == "I":
|
|
|
|
inspector = self._web_view.get_inspector()
|
2013-05-11 23:01:17 +02:00
|
|
|
if self._inspector_visible:
|
2013-05-06 23:32:40 +02:00
|
|
|
inspector.close()
|
2013-05-11 23:01:17 +02:00
|
|
|
self._inspector_visible = False
|
2013-05-06 23:32:40 +02:00
|
|
|
else:
|
|
|
|
inspector.show()
|
2013-05-11 23:01:17 +02:00
|
|
|
self._inspector_visible = True
|
|
|
|
|
|
|
|
return True
|
2013-05-06 23:32:40 +02:00
|
|
|
|
2013-05-06 22:07:27 +02:00
|
|
|
def _app_scheme_cb(self, request, user_data):
|
2013-05-11 01:17:30 +02:00
|
|
|
path = os.path.join(self._bundle_path,
|
|
|
|
os.path.relpath(request.get_path(), "/"))
|
2013-05-06 22:07:27 +02:00
|
|
|
|
|
|
|
request.finish(Gio.File.new_for_path(path).read(None),
|
|
|
|
-1, Gio.content_type_guess(path, None)[0])
|