Fixed loading html and js into view

This commit is contained in:
Jeena Paradies 2012-11-16 02:40:50 +01:00
parent ced63f7ce9
commit 9e83bfec47
6 changed files with 90 additions and 47 deletions

38
Linux/Helper.py Normal file
View file

@ -0,0 +1,38 @@
from PyQt4 import QtCore, QtGui, QtWebKit
class WebPage(QtWebKit.QWebPage):
def __init__(self, parent):
super(QtWebKit.QWebPage, self).__init__(parent)
def javaScriptConsoleMessage(self, message, lineNumber, sourceId):
print str(message) + " on line: " + str(lineNumber) + " Source: " + str(sourceId)
class WebViewCreator(QtGui.QWidget):
def __init__(self, app, delegate):
QtGui.QWidget.__init__(self)
self.app = app
self.delegate = delegate
self.view = QtWebKit.QWebView(self)
self.view.loadFinished.connect(self.load_finished)
self.page = WebPage(self)
self.view.setPage(self.page)
self.page.settings().setAttribute(QtWebKit.QWebSettings.LocalContentCanAccessRemoteUrls, True)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.view)
frame = self.view.page().mainFrame()
frame.addToJavaScriptWindowObject("controller", self.app.controller)
frame.addToJavaScriptWindowObject("console", self.app.console)
url = self.app.resources_uri() + "/index.html"
self.view.load(QtCore.QUrl(url))
def load_finished(self, ok):
self.view.page().mainFrame().evaluateJavaScript("OS_TYPE = 'linux';")
self.delegate.load_finished(ok)

View file

@ -1,8 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
import os import os, sys, pickle
import sys
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
import TentiaWindows import Windows
class Tentia: class Tentia:
@ -19,36 +18,52 @@ class Tentia:
print "quit" print "quit"
def setup_windows(self): def setup_windows(self):
self.preferences = TentiaWindows.Preferences(self) self.preferences = Windows.Preferences(self)
#self.timeline = TentiaWindows.Timeline(self) #self.timeline = Windows.Timeline(self)
#self.mentions = TentiaWindows.Timeline(self, action="mentions", title="Mentions") #self.mentions = Windows.Timeline(self, action="mentions", title="Mentions")
def resources_path(self): def resources_path(self):
return "../" return os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
def resources_uri(self): def resources_uri(self):
return "file://localhost" + os.path.abspath(os.path.join(os.path.dirname(__file__), '..', "WebKit")) return "file://localhost" + os.path.abspath(os.path.join(self.resources_path(), "WebKit"))
def login_with_entity(self, entity): def login_with_entity(self, entity):
self.controller.setStringForKey("entity", entity) self.controller.setStringForKey(entity, "entity")
self.oauth_implementation = TentiaWindows.OauthImplementation(self) self.oauth_implementation = Windows.Oauth(self)
def controller():
return self.controller;
class Controller(QtCore.QObject): class Controller(QtCore.QObject):
user_defaults = {} def __init__(self):
QtCore.QObject.__init__(self)
self.config_path = os.path.expanduser('~/.tentia.cfg')
if os.access(self.config_path, os.R_OK):
with open(self.config_path, 'r') as f:
self.config = pickle.load(f)
else:
print self.config_path + " is not readable"
self.config = {}
@QtCore.pyqtSlot(str, str) @QtCore.pyqtSlot(str, str)
def setStringForKey(self, string, key): def setStringForKey(self, string, key):
self.user_defaults[string] = key string, key = str(string), str(key)
self.config[key] = string
try:
with open(self.config_path, 'w+') as f:
pickle.dump(self.config, f)
except IOError:
print self.config_path + " is not writable"
print "I/O error({0}): {1}".format(e.errno, e.strerror)
@QtCore.pyqtSlot(str, result=str)
def stringForKey(self, key): def stringForKey(self, key):
if key in self.user_defaults: key = str(key)
return self.user_defaults[key] if key in self.config:
return self.config[key]
else:
return ""
class Console(QtCore.QObject): class Console(QtCore.QObject):

View file

@ -1,4 +1,5 @@
from PyQt4 import QtCore, QtGui, QtWebKit from PyQt4 import QtCore, QtGui, QtWebKit
import Helper
class Preferences: class Preferences:
@ -12,10 +13,10 @@ class Preferences:
self.window.resize(480, 186) self.window.resize(480, 186)
self.window.setMinimumSize(480, 186) self.window.setMinimumSize(480, 186)
self.window.setMaximumSize(480, 186) self.window.setMaximumSize(480, 186)
self.window.move(0, 0) self.window.move(1400, 700)
# image view # image view
image = QtGui.QPixmap(self.app.resources_path() + "Icon.png") image = QtGui.QPixmap(self.app.resources_path() + "/Icon.png")
image_view = QtGui.QLabel(self.window) image_view = QtGui.QLabel(self.window)
image_view.setGeometry(20, 20, 146, 146) image_view.setGeometry(20, 20, 146, 146)
image_view.setPixmap(image) image_view.setPixmap(image)
@ -35,6 +36,7 @@ class Preferences:
# text field # text field
self.text_field = QtGui.QLineEdit(self.window) self.text_field = QtGui.QLineEdit(self.window)
self.text_field.setText("http://jeena.net")
self.text_field.setPlaceholderText("https://example.tent.is") self.text_field.setPlaceholderText("https://example.tent.is")
self.text_field.setGeometry(194, 84, 262, 22) self.text_field.setGeometry(194, 84, 262, 22)
self.window.connect(self.text_field, QtCore.SIGNAL('returnPressed()'), self.on_login_button_clicked) self.window.connect(self.text_field, QtCore.SIGNAL('returnPressed()'), self.on_login_button_clicked)
@ -119,24 +121,14 @@ class Timeline:
self.web_view.execute_script(script) self.web_view.execute_script(script)
class OauthImplementation: class Oauth:
def __init__(self, app): def __init__(self, app):
self.app = app self.app = app
self.web_view = QtWebKit.QWebView() self.window = Helper.WebViewCreator(self.app, self)
self.web_view.settings().setAttribute(QtWebKit.QWebSettings.DeveloperExtrasEnabled, True)
self.init_web_view()
frame = self.web_view.page().mainFrame()
frame.addToJavaScriptWindowObject("controller", self.app.controller)
frame.addToJavaScriptWindowObject("console", self.app.console)
insp = QtWebKit.QWebInspector()
insp.setPage(self.web_view.page())
insp.show()
def init_web_view(self): def load_finished(self, ok):
self.web_view.loadFinished.connect(self.load_finished) if ok:
self.web_view.load(QtCore.QUrl(self.app.resources_uri() + "/index.html"))
def load_finished(self, widget):
script = "function HostAppGo() { start('oauth'); }" script = "function HostAppGo() { start('oauth'); }"
self.web_view.page().mainFrame().evaluateJavaScript(script) self.window.view.page().mainFrame().evaluateJavaScript(script)

View file

@ -5,6 +5,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="css/default.css" type="text/css" /> <link rel="stylesheet" href="css/default.css" type="text/css" />
<script data-main="scripts/main" src="scripts/lib/vendor/require-jquery.js"></script> <script data-main="scripts/main" src="scripts/lib/vendor/require-jquery.js"></script>
<script type="text/javascript">alert(go)</script>
</head> </head>
<body> <body>
</body> </body>

View file

@ -27,13 +27,13 @@ function(HostApp, Paths, Hmac) {
"write_followings": "Follow ne entities" "write_followings": "Follow ne entities"
} }
}; };
this.register_data = null; this.register_data = null;
this.profile = null; this.profile = null;
this.state = null; this.state = null;
} }
Oauth.prototype.authenticate = function() { Oauth.prototype.authenticate = function() {
var entity = HostApp.stringForKey("entity"); var entity = HostApp.stringForKey("entity");
if (entity && (entity.startsWith("http://") || entity.startsWith("https://"))) { if (entity && (entity.startsWith("http://") || entity.startsWith("https://"))) {

View file

@ -1,6 +1,5 @@
var tentia_instance; var tentia_instance;
var tentia_cache = {}; var tentia_cache = {};
var OS_TYPE = "mac";
requirejs.config({ requirejs.config({
baseUrl: 'scripts' baseUrl: 'scripts'
@ -9,7 +8,6 @@ requirejs.config({
function start(view) { function start(view) {
if (view == "oauth") { if (view == "oauth") {
require(["controller/Oauth"], function(Oauth) { require(["controller/Oauth"], function(Oauth) {
tentia_instance = new Oauth(); tentia_instance = new Oauth();
@ -76,7 +74,7 @@ function debug(string) {
function go() { // wait untill everything is loaded function go() { // wait untill everything is loaded
setTimeout(function() { setTimeout(function() {
if (HostAppGo != undefined) { if (typeof HostAppGo != typeof __not_defined__) {
HostAppGo(); HostAppGo();
@ -90,4 +88,3 @@ function go() { // wait untill everything is loaded
} }
go(); go();