#!/usr/bin/python3 # -*- coding: utf-8 -*- # Copyright 2009-2011 Linnea Skogtvedt # Copyright 2016 Mike Gabriel # # 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 # msg = Click here 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' 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_()