aboutsummaryrefslogtreecommitdiffstats
path: root/scudcloud-1.0/scudcloud
blob: c1f6cc1815c3d85e0634479dabe77071d3b619fc (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
#!/usr/bin/env python3
import fcntl, os, sys, signal, tempfile
from PyQt4 import QtGui

# 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.82'

def main():
    global win
    Resources.INSTALL_DIR = INSTALL_DIR
    signal.signal(signal.SIGINT, exit)
    lock()
    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)
    if win.minimized is None:
        win.show()
    win.restore()
    sys.exit(app.exec_())

def lock():
    global fp
    if 'XDG_RUNTIME_DIR' in os.environ and os.environ['XDG_RUNTIME_DIR']:
        runtime_dir = os.environ['XDG_RUNTIME_DIR']
    else:
        runtime_dir = tempfile.gettempdir()
    pid_file = runtime_dir+'/'+Resources.APP_NAME+'.pid'
    fp = open(pid_file, 'w')
    try:
        fcntl.lockf(fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
    except IOError:
        # Another instance is running
        warning = QtGui.QApplication(sys.argv)
        QtGui.QMessageBox.warning(None, "ScudCloud", warning.tr("Another instance of ScudCloud is already running."))
        sys.exit(0)

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()