summaryrefslogtreecommitdiff
path: root/kberylsettings/aboutpage.py
blob: 75e057da82a46d7901acd6a7eeac7935dd033ac6 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" kberylsettings.contentframe -> defines the right side about/setting views.

"""
from os import popen

from kdecore import KIcon, i18n, locate
from khtml import KHTMLPart
from pykdeconfig import _pkg_config

from kberylsettings.beryl import Setting
from kberylsettings.lib import Signals, iconLoader
from kberylsettings.widget import Frame


class AboutPage(Frame):
    """ AboutPage -> displays info about Beryl, environment, and plugins.

    """
    berylProto = 'beryl:/'
    pluginHREF = '<a href="' + berylProto + '%s">%s</a>'
    settingHREF = '<a href="' + berylProto + '%s@%s">%s</a>'
    contentContainer = '<table class="kc_table">%s</table>%s'
    contentRow = '<tr>%s</tr>'
    contentCell = '''
    <td class="kc_leftcol">%s</td><td "kc_rightcol"><b>%s</b></td>
    '''
    contentImg = '''
    <img src="file://%s"
    ''' + ('width="%s" height="%s" />' % (KIcon.SizeSmall, KIcon.SizeSmall, ))
    contextExtra = '''
    Use the "Search" field if you are unsure where to look for a
    particular configuration option.
    '''
    
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.infoHtml = HTMLPart(self)
        self.loader = iconLoader()
        self.connect(self.infoHtml, Signals.partURL, self.onURL)
    
    def pluginContent(self, plugin):
        """ constructs html content for a berylsettings Plugin

        @param plugin berylsettings Plugin instance
        @return markup string
        """
        rows = []
        keys = plugin.settings.keys()
        keys.sort()
        for key in keys:
            row = (self.contentImg % Setting.iconPath(key) + '&nbsp;' + \
                       self.settingHREF % (plugin.Name, key, key),
                   i18n(plugin.settingTypeDesc(key)))
            rows.append(row)
        rows = [self.contentCell % row for row in rows]
        rows = [self.contentRow % row for row in rows]
        return self.contentContainer % (str.join('', rows), '')

    def contextContent(self, context):
        """ constructs html content for a berylsettings Context

        @param context berylsettings Context instance
        @return markup string
        """
        def cmd(line):
            return popen(line).read().strip()
        rows = [
            ('Beryl Version', cmd('beryl --version')),
            ('KDE Version', _pkg_config['kde_version_str']),
            ('PyKDE Version', _pkg_config['pykde_version_str']),
            ('User', cmd('whoami')),
            ('Hostname', cmd('uname -n')),
            ('System', cmd('uname -s')),
            ('Release', cmd('uname -r')),
            ('Machine', cmd('uname -m')),
            ('<br />', '<br />'),
            ('Active Plugins:', '')
        ]
        for plugin in [p for p in context.plugins if p.Enabled]:
            row = (self.contentImg % plugin.iconPath() + '&nbsp;' + \
                       self.pluginHREF % (plugin.Name, plugin.ShortDesc),
                   plugin.LongDesc)
            rows.append(row)
        rows = [self.contentCell % (i18n(str(a)), b) for a, b in rows]
        rows = [self.contentRow % row for row in rows]
        extra = '<div>%s</div>' % i18n(self.contextExtra)
        return self.contentContainer % (str.join('', rows), extra)

    def showAbout(self, context, plugin):
        """ displays the About Plugin page with information from the plugin

        @param plugin berylsettings Plugin instance
        @return None
        """
        self.context = context
        htmlsrc = open(locate('templates', 'main.html')).read()
        extracssurl = locate('data', 'kdeui/about/kde_infopage.css')
        extracssurl = 'file://%s' % extracssurl
        kcontrolcssurl = locate('data', 'kcontrol/about/kcontrol.css')        
        kcontrolcssurl = 'file://%s' % kcontrolcssurl
        logourl = 'file://%s' % locate('templates', 'beryl-manager.png')
        if plugin is None:
            title = i18n('KDE Beryl Settings')
            shortdesc = i18n('Configure Your Beryl Settings')
            longdesc = i18n('Beryl Settings - Get Your Effects On!')
            content = self.contextContent(context)
        else:
            title = plugin.ShortDesc
            shortdesc = plugin.ShortDesc
            longdesc = plugin.LongDesc
            content = self.pluginContent(plugin)
        self.infoHtml.begin()
        self.infoHtml.write(htmlsrc % locals())
        self.infoHtml.end()

    def onURL(self, url):
        """ emits a signal to show the plugin and setting on a URL click

        @param url string
        @return None
        """
        protocol, path = url.split(self.berylProto)
        path = path.split('@')
        try:
            pluginname, settingkey = path
        except (ValueError, ):
            pluginname = path[0]
            settingkey = None
        plugin = self.context.Plugin(pluginname)
        self.emit(Signals.selectPrevious, (plugin, settingkey, True))


class HTMLPart(KHTMLPart):
    """ HTMLPart -> simple subclass that emits urls as they're selected.

    """
    def urlSelected(self, url, button, state, target, args):
        """ emit a url when selected

        @return None
        """
        self.emit(Signals.partURL, (str(url), ))