aboutsummaryrefslogtreecommitdiffstats
path: root/scudcloud-1.0/scudcloud
blob: ebc636605313636213a3f4fd672f4c5875a46ba0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/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

VERSION = '1.0.84'

def main():
    global win
    Resources.INSTALL_DIR = INSTALL_DIR
    signal.signal(signal.SIGINT, exit)
    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')))
    args = parse_arguments()
    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:
        print("ScudCloud "+VERSION)
        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()