]> git.lizzy.rs Git - torbrowser-launcher.git/blobdiff - torbrowser_launcher/__init__.py
Merge pull request #528 from micahflee/develop
[torbrowser-launcher.git] / torbrowser_launcher / __init__.py
index b78119b806b3aba0ba82c3a19480875c19790ed3..800fe88fca578729f864876d66f99677ea22c131 100644 (file)
@@ -2,7 +2,7 @@
 Tor Browser Launcher
 https://github.com/micahflee/torbrowser-launcher/
 
-Copyright (c) 2013-2014 Micah Lee <micah@micahflee.com>
+Copyright (c) 2013-2017 Micah Lee <micah@micahflee.com>
 
 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
@@ -26,38 +26,80 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 """
 
-import os, sys
+import os
+import sys
+import argparse
+import signal
+
+from PyQt5 import QtCore, QtWidgets
+
+from .common import Common, SHARE
+from .settings import Settings
+from .launcher import Launcher
+
+
+class Application(QtWidgets.QApplication):
+    """
+    Qt's QApplication class. It has been overridden to support threads.
+    """
+
+    def __init__(self):
+        self.setAttribute(QtCore.Qt.AA_X11InitThreads, True)
+        QtWidgets.QApplication.__init__(self, sys.argv)
+        self.installEventFilter(self)
 
-from common import Common, SHARE
-from settings import Settings
-from launcher import Launcher
 
 def main():
-    with open(os.path.join(SHARE, 'version')) as buf:
+    # Parse arguments
+    parser = argparse.ArgumentParser()
+    parser.add_argument(
+        "--settings",
+        action="store_true",
+        dest="settings",
+        help="Open Tor Browser Launcher settings",
+    )
+    parser.add_argument("url", nargs="*", help="URL to load")
+    args = parser.parse_args()
+
+    settings = bool(args.settings)
+    url_list = args.url
+
+    # Load the version and print the banner
+    with open(os.path.join(SHARE, "version")) as buf:
         tor_browser_launcher_version = buf.read().strip()
 
-    print _('Tor Browser Launcher')
-    print _('By Micah Lee, licensed under BSD')
-    print _('version {0}').format(tor_browser_launcher_version)
-    print 'https://github.com/micahflee/torbrowser-launcher'
+    print(_("Tor Browser Launcher"))
+    print(_("By Micah Lee, licensed under MIT"))
+    print(_("version {0}").format(tor_browser_launcher_version))
+    print("https://github.com/micahflee/torbrowser-launcher")
 
     common = Common(tor_browser_launcher_version)
+    app = Application()
 
-    # is torbrowser-launcher already running?
-    tbl_pid = common.get_pid(common.paths['tbl_bin'], True)
-    if tbl_pid:
-        print _('Tor Browser Launcher is already running (pid {0}), bringing to front').format(tbl_pid)
-        common.bring_window_to_front(tbl_pid)
-        sys.exit()
-
-    if '-settings' in sys.argv:
-        # settings mode
-        app = Settings(common)
+    # Open the window
+    gui = None
 
+    if settings:
+        # Settings mode
+        gui = Settings(common, app)
     else:
-        # launcher mode
-        app = Launcher(common)
+        # Launcher mode
+        gui = Launcher(common, app, url_list)
+
+    # Center the window
+    desktop = app.desktop()
+    window_size = gui.size()
+    gui.move(
+        (desktop.width() - window_size.width()) / 2,
+        (desktop.height() - window_size.height()) / 2,
+    )
+    gui.show()
+
+    # Allow ctrl-c to work
+    signal.signal(signal.SIGINT, signal.SIG_DFL)
+
+    sys.exit(app.exec_())
+
 
 if __name__ == "__main__":
     main()
-