#!/usr/bin/env python
import sys
from PySide6 import QtCore, QtWidgets, QtGui
import gemini
class GViewport(QtWidgets.QTextBrowser):
hoverUrlChanged = QtCore.Signal(str)
resourceLoadedStatus = QtCore.Signal(str)
def __init__(self, address_bar):
self.address_bar = address_bar
self._hover_url = None
QtWidgets.QTextBrowser.__init__(self)
def mouseMoveEvent(self, event):
cur = self.cursorForPosition(event.localPos().toPoint())
hover_url = cur.charFormat().anchorHref()
if hover_url:
hover_url = gemini.urljoin(self.address_bar.text(), hover_url)
if hover_url != self._hover_url:
self._hover_url = hover_url
self.hoverUrlChanged.emit(self._hover_url)
return super().mouseMoveEvent(event)
def loadResource(self, type_, url):
gem = gemini.get(url.toString())
print(gem['body'])
if 'body' in gem:
html = gemini.gem2html(gem['body'])
print(html)
else:
html = '
{} {}
'.format(gem['status'], gem['meta'])
self.resourceLoadedStatus.emit(
'{status} {meta}'.format(status=gem['status'], meta=gem['meta'])
)
return html
def setSource(self, url, type_=None):
"""
send all unsupported urls to the OS
"""
if url.scheme() == 'gemini':
if type_ is None:
return super().setSource(url)
return super().setSource(url, type_)
return QtGui.QDesktopServices().openUrl(url)
def setRawSource(self):
return self.setSource(QtCore.QUrl(self.address_bar.text()))
class GUrlBar(QtWidgets.QLineEdit):
def __init__(self):
QtWidgets.QLineEdit.__init__(self)
def setUrl(self, url):
return self.setText(url.toDisplayString())
class GBrowser(QtWidgets.QMainWindow):
def __init__(self, initial_url=None):
QtWidgets.QMainWindow.__init__(self)
## Create widgets
# Navigation Toolbar
back = QtWidgets.QPushButton("back")
forward = QtWidgets.QPushButton("forward")
address = GUrlBar()
toolbar = QtWidgets.QToolBar()
toolbar.setObjectName('navigationToolbar')
toolbar.addWidget(back)
toolbar.addWidget(forward)
toolbar.addWidget(address)
self.addToolBar(toolbar)
# Main Viewport
browser = GViewport(address)
self.setCentralWidget(browser)
# Status Bar
request_status = QtWidgets.QLabel()
self.statusBar().addWidget(request_status)
# Connect signals
back.clicked.connect(browser.backward)
forward.clicked.connect(browser.forward)
browser.sourceChanged.connect(address.setUrl)
address.returnPressed.connect(browser.setRawSource)
browser.hoverUrlChanged.connect(self.set_status_url)
browser.resourceLoadedStatus.connect(request_status.setText)
#
browser.document().setDefaultStyleSheet(open('style.css').read())
browser.setSource(QtCore.QUrl(initial_url or 'gemini://gemini.circumlunar.space/'))
settings = QtCore.QSettings("xcolour.net", "GeminiBrowser")
self.restoreGeometry(settings.value("geometry"))
self.restoreState(settings.value("windowState"))
self.setWindowTitle('Gemini Browser')
self.show()
def set_status_url(self, url):
if url:
self.statusBar().showMessage(url)
else:
self.statusBar().clearMessage()
def closeEvent(self, event):
settings = QtCore.QSettings("xcolour.net", "GeminiBrowser")
settings.setValue("geometry", self.saveGeometry())
settings.setValue("windowState", self.saveState())
super().closeEvent(event)
app = QtWidgets.QApplication(sys.argv)
ex = GBrowser(sys.argv[1] if len(sys.argv) > 1 else None)
sys.exit(app.exec_())