Add a small web server service to respond to redirect on the login page.

This commit is contained in:
Mark Washeim 2022-11-04 15:40:35 +01:00
parent 90f92722be
commit f3c9873766
4 changed files with 119 additions and 5 deletions

View file

@ -73,6 +73,8 @@ DISTFILES += qml/harbour-tooterb.qml \
qml/pages/LoginPage.qml \
qml/pages/Browser.qml \
qml/lib/API.js \
qml/lib/server.py \
qml/lib/index.html \
qml/images/icon-s-following \
qml/images/icon-s-bookmark \
qml/images/icon-m-emoji.svg \

26
qml/lib/index.html Normal file
View file

@ -0,0 +1,26 @@
<html>
<head>
<meta name="viewport" content="width=device-width, user-scalable=no"/>
<style>
body { font-family: sans-serif; background-color:black;}
button { width: 32%; font-size: 20px; height: 40px; }
#output { width: 100%; text-align: center; font-size: 120px; }
</style>
<script type="text/javascript">
function action(topic) {
var output = document.getElementById('output');
output.textContent = {"one": "1", "two": "2", "three": "3"}[topic]
}
function send(topic) {
var customEvent = new CustomEvent("framescript:log",
{detail: { topic: topic}});
document.dispatchEvent(customEvent);
}
</script>
</head>
<body>
<div id="output"><p>Login Complete</p></div>
</body>
</html>

43
qml/lib/server.py Normal file
View file

@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
# POETASTER
import sys
sys.path.append('/usr/share/harbour-tooterb/qml')
sys.path.append('/usr/share/harbour-tooterb/qml/lib')
sys.path.append('/usr/share/harbour-tooterb/qml/pages')
# POETASTER
import pyotherside
import threading
import time
import random
import os
import http.server
import socketserver
PORT = 8000
directory = '/usr/share/harbour-tooterb/qml/lib'
Handler = http.server.SimpleHTTPRequestHandler
os.chdir(directory)
def serveMe():
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print("serving at port", PORT)
pyotherside.send('finished', PORT)
httpd.serve_forever()
class Downloader:
def __init__(self):
# Set bgthread to a finished thread so we never
# have to check if it is None.
self.bgthread = threading.Thread()
self.bgthread.start()
def serve(self):
if self.bgthread.is_alive():
return
self.bgthread = threading.Thread(target=serveMe)
self.bgthread.start()
downloader = Downloader()

View file

@ -1,12 +1,33 @@
import QtQuick 2.0
// import QtWebKit 3.0
import Sailfish.Silica 1.0
import Sailfish.WebView 1.0
import Sailfish.WebEngine 1.0
import io.thp.pyotherside 1.5
import "../lib/API.js" as Logic
Page {
// Python connections and signals, callable from QML side
// This is not ideal but keeps the page from erroring out on redirect
Python {
id: py
Component.onCompleted: {
addImportPath(Qt.resolvedUrl('../lib/'));
importModule('server', function () {});
setHandler('finished', function(newvalue) {
console.debug(newvalue)
});
startDownload();
}
function startDownload() {
call('server.downloader.serve', function() {});
console.debug("called")
}
}
id: loginPage
// The effective value will be restricted by ApplicationWindow.allowedOrientations
allowedOrientations: Orientation.All
@ -41,7 +62,7 @@ Page {
EnterKey.onClicked: {
Logic.api = Logic.mastodonAPI({ instance: instance.text, api_user_token: "" });
Logic.api.registerApplication("Tooter",
'http://localhost/harbour-tooter', // redirect uri, we will need this later on
'http://localhost:8000/index.html', // redirect uri, we will need this later on
["read", "write", "follow"], //scopes
"http://grave-design.com/harbour-tooter", //website on the login screen
function(data) {
@ -100,6 +121,28 @@ Page {
WebEngineSettings.setPreference("security.disable_cors_checks", true, WebEngineSettings.BoolPref)
WebEngineSettings.setPreference("security.fileuri.strict_origin_policy", false, WebEngineSettings.BoolPref)
}
onViewInitialized: {
//webview.loadFrameScript(Qt.resolvedUrl("../html/framescript.js"));
//webview.addMessageListener("webview:action");
//webview.runJavaScript("return latlon('" + lat + "','" + lon + "')");
}
onRecvAsyncMessage: {
console.log('async changed: ' + url)
console.debug(message)
switch (message) {
case "embed:contentOrientationChanged":
break
case "webview:action":
if ( data.topic != lon ) {
//webview.runJavaScript("return latlon('" + lat + "','" + lon + "')");
//if (debug) console.debug(data.topic)
//if (debug) console.debug(data.also)
//if (debug) console.debug(data.src)
}
break
}
}
visible: false
//opacity: 0
anchors {
@ -110,10 +153,10 @@ Page {
}
onLoadingChanged: {
console.log(url)
console.log('loading changed: ' + url)
if (
(url+"").substr(0, 37) === 'http://localhost/harbour-tooter?code=' ||
(url+"").substr(0, 38) === 'https://localhost/harbour-tooter?code='
(url+"").substr(0, 38) === 'http://localhost:8000/index.html?code=' ||
(url+"").substr(0, 39) === 'https://localhost:8000/index.html?code='
) {
visible = false;