From 12bbdc84a744aaa2af4130539a2a7188996c7659 Mon Sep 17 00:00:00 2001 From: "Kali Kaneko (leap communications)" Date: Fri, 21 Oct 2016 17:13:37 -0400 Subject: [feat] fallback support for PySide basically, I give up trying to compile pyqt5 for python2. not worthy. fortunately, the api changes are minimal so we can support two. the way forward should be Py3. still some bugs with rendering the viewport. trying to help that, I introduced the web inspector, use DEBUG=1 flag to have it visible. --- src/leap/bitmask/gui/app.py | 78 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 65 insertions(+), 13 deletions(-) (limited to 'src/leap/bitmask') diff --git a/src/leap/bitmask/gui/app.py b/src/leap/bitmask/gui/app.py index cf4ac4b..f812a6b 100644 --- a/src/leap/bitmask/gui/app.py +++ b/src/leap/bitmask/gui/app.py @@ -22,32 +22,72 @@ when the web service is running. """ import os +import platform import signal import sys from functools import partial -from PyQt5 import QtCore, QtGui, QtWidgets -from PyQt5 import QtWebKit, QtWebKitWidgets +if platform.system() == 'Windows': + from multiprocessing import freeze_support + from PySide import QtCore, QtGui + from PySide import QtWebKit + from PySide.QtGui import QDialog + from PySide.QtGui import QApplication + from PySide.QtWebKit import QWebView, QGraphicsWebView + from PySide.QtCore import QSize +else: + from PyQt5 import QtCore, QtGui + from PyQt5 import QtWebKit + from PyQt5.QtWidgets import QDialog + from PyQt5.QtWidgets import QApplication + from PyQt5.QtWebKitWidgets import QWebView + +from multiprocessing import Process from leap.bitmask.core.launcher import run_bitmaskd, pid -from multiprocessing import Process BITMASK_URI = 'http://localhost:7070' +IS_WIN = platform.system() == "Windows" +DEBUG = os.environ.get("DEBUG", False) + qApp = None bitmaskd = None -class BrowserWindow(QtWidgets.QDialog): +class BrowserWindow(QDialog): def __init__(self, parent): super(BrowserWindow, self).__init__(parent) - self.view = QtWebKitWidgets.QWebView(self) + if IS_WIN: + self.view = QWebView(self) + win_size = QSize(1024, 600) + self.setMinimumSize(win_size) + self.view.page().setViewportSize(win_size) + self.view.page().setPreferredContentsSize(win_size) + else: + self.view = QWebView(self) + win_size = QSize(800, 600) + self.win_size = win_size + self.resize(win_size) + + if DEBUG: + self.view.settings().setAttribute( + QtWebKit.QWebSettings.WebAttribute.DeveloperExtrasEnabled, True) + self.inspector = QtWebKit.QWebInspector(self) + self.inspector.setPage(self.view.page()) + self.inspector.show() + self.splitter = QtGui.QSplitter() + self.splitter.addWidget(self.view) + self.splitter.addWidget(self.inspector) + #TODO add layout also in non-DEBUG mode + layout = QtGui.QVBoxLayout(self) + layout.addWidget(self.splitter) + self.setWindowTitle('Bitmask') - self.resize(800, 600) self.load_app() self.closing = False @@ -60,10 +100,11 @@ class BrowserWindow(QtWidgets.QDialog): self.closing = True global bitmaskd bitmaskd.join() - with open(pid) as f: - pidno = int(f.read()) - print('[bitmask] terminating bitmaskd...') - os.kill(pidno, signal.SIGTERM) + if os.path.isfile(pid): + with open(pid) as f: + pidno = int(f.read()) + print('[bitmask] terminating bitmaskd...') + os.kill(pidno, signal.SIGTERM) print('[bitmask] shutting down gui...') try: self.view.stop() @@ -74,6 +115,7 @@ class BrowserWindow(QtWidgets.QDialog): sys.exit(1) + def _handle_kill(*args, **kw): win = kw.get('win') if win: @@ -84,10 +126,12 @@ def launch_gui(): global qApp global bitmaskd + if IS_WIN: + freeze_support() bitmaskd = Process(target=run_bitmaskd) bitmaskd.start() - qApp = QtWidgets.QApplication([]) + qApp = QApplication([]) browser = BrowserWindow(None) qApp.setQuitOnLastWindowClosed(True) @@ -97,6 +141,7 @@ def launch_gui(): signal.SIGINT, partial(_handle_kill, win=browser)) + # Avoid code to get stuck inside c++ loop, returning control # to python land. timer = QtCore.QTimer() @@ -113,10 +158,17 @@ def start_app(): # Allow the frozen binary in the bundle double as the cli entrypoint # Why have only a user interface when you can have two? - if STANDALONE and len(sys.argv) > 1: + if platform.system() == 'Windows': + # In windows, there are some args added to the invocation + # by PyInstaller, I guess... + MIN_ARGS = 3 + else: + MIN_ARGS = 1 + + # DEBUG ==================================== + if STANDALONE and len(sys.argv) > MIN_ARGS: from leap.bitmask.cli import bitmask_cli return bitmask_cli.main() - launch_gui() -- cgit v1.2.3