From a4c4bccc4b6abcb254a21302f56d0d1ac104b4b8 Mon Sep 17 00:00:00 2001 From: "Schimon Jehudah, Adv." Date: Wed, 4 Sep 2024 17:31:52 +0300 Subject: [PATCH] Add support for Blasta to operate as a desktop software. --- blasta.py | 8 +++++++- blasta_client.py | 39 ++++++++++++++++++++++++++++++++++++++ blasta_client.sh | 4 ++++ script/regulator.js | 20 +++++++++++++++++++ stylesheet/stylesheet.xsl | 9 +++++---- template/about.xhtml | 15 +++++++++++++-- template/ask.xhtml | 11 +++++++++-- template/atomsub.xhtml | 23 ++++++++++++++++++++-- template/browse.atom | 11 ++++++----- template/browse.xhtml | 17 +++++++++++++++-- template/connect.xhtml | 11 +++++++++-- template/contact.xhtml | 11 +++++++++-- template/edit.xhtml | 13 ++++++++++--- template/feeds.xhtml | 15 +++++++++++++-- template/folksonomy.xhtml | 19 +++++++++++++++++-- template/help.xhtml | 11 +++++++++-- template/ideas.xhtml | 19 +++++++++++++++++-- template/libervia.xhtml | 27 ++++++++++++++++++++++---- template/movim.xhtml | 23 ++++++++++++++++++++-- template/now.xhtml | 11 +++++++++-- template/people.xhtml | 11 +++++++++-- template/philosophy.xhtml | 19 +++++++++++++++++-- template/profile.xhtml | 11 +++++++++-- template/projects.xhtml | 19 +++++++++++++++++-- template/pubsub.xhtml | 23 ++++++++++++++++++++-- template/questions.xhtml | 23 +++++++++++++++++++--- template/register.xhtml | 11 +++++++++-- template/result.xhtml | 11 +++++++++-- template/search.xhtml | 11 +++++++++-- template/software.xhtml | 35 ++++++++++++++++++++++++++++++++-- template/syndication.xhtml | 15 +++++++++++++-- template/tag.xhtml | 11 +++++++++-- template/thanks.xhtml | 19 +++++++++++++++++-- template/utilities.xhtml | 15 +++++++++++++-- template/xmpp.xhtml | 19 +++++++++++++++++-- 35 files changed, 498 insertions(+), 72 deletions(-) create mode 100644 blasta_client.py create mode 100755 blasta_client.sh create mode 100644 script/regulator.js diff --git a/blasta.py b/blasta.py index b216511..97b5db4 100644 --- a/blasta.py +++ b/blasta.py @@ -38,6 +38,7 @@ import tomli_w from typing import Optional import urllib.parse import uvicorn +import webbrowser import xml.etree.ElementTree as ET @@ -266,6 +267,7 @@ class HttpInstance: self.app.mount('/data', StaticFiles(directory='data'), name='data') self.app.mount('/export', StaticFiles(directory='export'), name='export') self.app.mount('/graphic', StaticFiles(directory='graphic'), name='graphic') + self.app.mount('/script', StaticFiles(directory='script'), name='script') self.app.mount('/stylesheet', StaticFiles(directory='stylesheet'), name='stylesheet') filename_configuration = 'configuration.toml' @@ -5318,7 +5320,11 @@ def main(): return http_instance.app app = main() +webbrowser.open('http://localhost:8000/help/about') +# TODO Check first time +webbrowser.open_new_tab('http://localhost:8000') # FIXME if __name__ == '__main__': - uvicorn.run(app, host='127.0.0.1', port=8000, reload=True) + uvicorn.run(app, host='localhost', port=8000, reload=True) + diff --git a/blasta_client.py b/blasta_client.py new file mode 100644 index 0000000..b840341 --- /dev/null +++ b/blasta_client.py @@ -0,0 +1,39 @@ +import webview +import subprocess +import sys + +class HtmlView: + def __init__(self, url_instance): + self.url_instance = url_instance + + def regulator(self, uri): + # Check if the URL is not from url_instance + if not uri.startswith(self.url_instance): + try: + # Open the link using xdg-open + subprocess.run(['xdg-open', uri], check=True) + except subprocess.CalledProcessError as e: + print(f"Failed to open URL: {uri}. Error: {e}") + else: + # If it is from url_instance, just load it in the webview + #webview.load_url(uri) + #webview.evaluate(f"window.location.href = '{uri}';") + webview.windows[0].load_url(uri) + + def setup(self): + # Create a webview window + window = webview.create_window('Blasta', self.url_instance) + # Bind the link click event to the regulator function + window.expose(self.regulator) + + # Start the webview application + webview.start() + +if __name__ == '__main__': + if len(sys.argv) != 2: + print('Usage: {} '.format(sys.argv[0])) + sys.exit(1) + + url_instance = sys.argv[1] + html_view = HtmlView(url_instance) + html_view.setup() diff --git a/blasta_client.sh b/blasta_client.sh new file mode 100755 index 0000000..6b6c36a --- /dev/null +++ b/blasta_client.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +unset http_proxy +WEBKIT_DISABLE_DMABUF_RENDERER=1 python blasta_client.py http://localhost:8000 diff --git a/script/regulator.js b/script/regulator.js new file mode 100644 index 0000000..0eb521c --- /dev/null +++ b/script/regulator.js @@ -0,0 +1,20 @@ +document.addEventListener('DOMContentLoaded', () => { + window.addEventListener('pywebviewready', () => { + // Attach event listeners to all anchor elements + document.querySelectorAll('a').forEach(element => { + element.addEventListener('click', regulator); + }); + }); +}); + +// This function will be called when an anchor is clicked +function regulator(event) { + event.preventDefault(); // Prevent the default link behavior + const url = this.href; // Get the URL from the clicked link + + // Call the exposed Python function + window.pywebview.api.regulator(url) + .catch(error => { + console.error("Error calling regulator:", error); + }); +} diff --git a/stylesheet/stylesheet.xsl b/stylesheet/stylesheet.xsl index daa3256..d2d2ccf 100644 --- a/stylesheet/stylesheet.xsl +++ b/stylesheet/stylesheet.xsl @@ -58,8 +58,9 @@ xmlns:atom='http://www.w3.org/2005/Atom'> <xsl:value-of select='atom:title'/> - + +