#!/usr/bin/env python3 import fcntl, os, sys, signal, tempfile from PyQt4 import QtGui, QtCore from PyQt4.QtNetwork import QLocalServer, QLocalSocket # Flexible install dir (we assume that 'lib' and 'resources' will be subdirs) INSTALL_DIR = os.path.dirname(os.path.realpath(__file__)) # Append the lib directory in our installation path to get remaining libraries. sys.path.append(os.path.join(INSTALL_DIR, 'lib')) from resources import Resources from scudcloud import ScudCloud # The ScudCloud QMainWindow win = None def main(): global win Resources.INSTALL_DIR = INSTALL_DIR signal.signal(signal.SIGINT, exit) args = parse_arguments() appKey = "scudcloud.pid" socket = QLocalSocket() socket.connectToServer(appKey) if socket.isOpen(): socket.close() socket.deleteLater() return 0 socket.deleteLater() app = QtGui.QApplication(sys.argv) app.setApplicationName(Resources.APP_NAME) app.setWindowIcon(QtGui.QIcon(Resources.get_path('scudcloud.png'))) ScudCloud.debug = args.debug ScudCloud.minimized = True if args.minimized is True else None ScudCloud.plugins = False if args.no_plugins is True else True try: settings_path = load_settings(args.confdir) except: print("Configuration directory "+args.confdir+" could not be created! Exiting...") raise SystemExit() win = ScudCloud(settings_path=settings_path) server = QLocalServer() server.newConnection.connect(restore) server.listen(appKey) win.restore() if win.minimized is None: win.show() sys.exit(app.exec_()) def restore(): global win win.setWindowFlags(win.windowFlags() | QtCore.Qt.WindowStaysOnTopHint) win.showNormal() win.setWindowFlags(win.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint) win.showNormal() win.activateWindow() def load_settings(confdir): if not os.path.isdir(confdir): os.makedirs(confdir) if confdir not in sys.path: sys.path[0:0] = [confdir] return confdir def parse_arguments(): from argparse import ArgumentParser from os.path import expanduser if 'XDG_CONFIG_HOME' in os.environ and os.environ['XDG_CONFIG_HOME']: default_confdir = os.environ['XDG_CONFIG_HOME'] + '/scudcloud' else: default_confdir = '~/.config/scudcloud' parser = ArgumentParser() parser.add_argument('--confdir', dest='confdir', metavar='dir', default=default_confdir, help="change the configuration directory") parser.add_argument('--debug', dest='debug', type=bool, default=False, help="enable webkit debug console (default: False)") parser.add_argument('--minimized', dest='minimized', type=bool, default=False, help="start minimized to tray (default: False)") parser.add_argument('--no_plugins', dest='no_plugins', type=bool, default=False, help="disable web plugins (default: False)") parser.add_argument('--version', action="store_true", help="print version and exit") args = parser.parse_args() if args.version: with open(os.path.join(Resources.INSTALL_DIR, 'VERSION'), "r") as f: print("ScudCloud "+f.read()) sys.exit() args.confdir = expanduser(args.confdir) return args def exit(*args): if win is not None: win.exit() else: QtGui.QApplication.quit() if __name__ == '__main__': main()