summaryrefslogtreecommitdiff
path: root/bin/kun-tips
blob: a279856dcfff3dfaf59754038c1b8d6afdf5ecea (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/python3
# -*- coding: utf-8 -*-

# Copyright 2009-2011 Linnea Skogtvedt <linnea@linuxavdelingen.no>
# Copyright 2016 Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.


# Example /etc/tips.cfg:
# [tips]
# title = Tips
# user message = Click <a href="/usr/share/applications/putty.desktop">here</a> to launch the PuTTY application.
# open urls with = kfmclient exec


import sys
import locale
import configparser
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QApplication, QDialog, QLabel, QVBoxLayout

class ConfigException(BaseException):
    pass

class Form(QDialog):

    clicked = pyqtSignal()

    def __init__(self):
        super().__init__()

        lang, encoding = locale.getlocale()
        lang, region = lang.split("_")

        cfg = configparser.RawConfigParser()
        if sys.argv[1:]:
            cfgFile = sys.argv[1]
        else:
            cfgFile = '/etc/tips.cfg'
        cfg.readfp(open(cfgFile, 'r'))

        # check if the tips.cfg file has valid sections...
        section = 'tips'
        if not cfg.has_section(section):
            section = 'rapporter-fail'
            if not cfg.has_section(section):
                raise ConfigException

        title_options = [
            'title[{lang}_{region}]'.format(lang=lang, region=region),
            'title[{lang}]'.format(lang=lang),
            'title',
        ]
        for option in title_options:
           if cfg.has_option(section, option):
               title = cfg.get(section, option)
               break
        else:
           raise ConfigException

        try:
            urlOpener = cfg.get(section, 'open urls with')
        except configparser.NoOptionError:
            urlOpener = "xdg-open"

        msg_options = [
            'msg[{lang}_{region}]'.format(lang=lang, region=region),
            'msg[{lang}]'.format(lang=lang),
            'msg',
        ]
        for option in msg_options:
           if cfg.has_option(section, option):
               msg = cfg.get(section, option)
               break
        else:
           raise ConfigException

        def openLink(link):
            import shlex
            argv = shlex.split(urlOpener) + [link]
            QProcess.startDetached(argv[0], argv[1:])

        msgLabel = QLabel(msg)
        msgLabel.setTextFormat(Qt.RichText)
        msgLabel.linkActivated.connect(openLink)

        layout = QVBoxLayout()
        layout.addWidget(msgLabel)
        self.setLayout(layout)
        self.setWindowTitle(title)


app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()