File: disabledapplets.py

package info (click to toggle)
pessulus 2.16.3-1.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,280 kB
  • ctags: 136
  • sloc: sh: 3,235; python: 944; makefile: 68
file content (263 lines) | stat: -rw-r--r-- 9,761 bytes parent folder | download | duplicates (3)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/env python

# vim: set ts=4 sw=4 et:

#
# Copyright (C) 2005 Vincent Untz <vuntz@gnome.org>
#
# 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 2 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
#

import os
import bonobo
import gconf
import gtk

try:
    set
except:
    from sets import Set as set

import globalvar
import icons

# there's no wrapper for g_get_language_names (). Ugly workaround:
# Note that we don't handle locale alias...
def get_language_names ():
    if "LANGUAGE" in os.environ.keys () and os.environ["LANGUAGE"] != "":
        env_lang = os.environ["LANGUAGE"].split ()
    elif "LC_ALL" in os.environ.keys () and os.environ["LC_ALL"] != "":
        env_lang = os.environ["LC_ALL"].split ()
    elif "LC_MESSAGES" in os.environ.keys () and os.environ["LC_MESSAGES"] != "":
        env_lang = os.environ["LC_MESSAGES"].split ()
    elif "LANG" in os.environ.keys () and os.environ["LANG"] != "":
        env_lang = os.environ["LANG"].split ()
    else:
        env_lang = []

    env_lang.reverse ()
    languages = []

    for language in env_lang:
        start_pos = 0
        mask = 0
        uscore_pos = language.find ("_")
        if uscore_pos != -1:
            start_pos = uscore_pos
            mask += 1 << 2
        dot_pos = language.find (".", start_pos)
        if dot_pos != -1:
            start_pos = dot_pos
            mask += 1 << 0
        at_pos = language.find ("@", start_pos)
        if at_pos != -1:
            start_pos = at_pos
            mask += 1 << 1

        if uscore_pos != -1:
            lang = language[:uscore_pos]
        elif dot_pos != -1:
            lang = language[:dot_pos]
        elif at_pos != -1:
            lang = language[:at_pos]
        else:
            lang = language

        if uscore_pos != -1:
            if dot_pos != -1:
                territory = language[uscore_pos:dot_pos]
            elif at_pos != -1:
                territory = language[uscore_pos:at_pos]
            else:
                territory = language[uscore_pos:]
        else:
            territory = ""

        if dot_pos != -1:
            if at_pos != -1:
                codeset = language[dot_pos:at_pos]
            else:
                codeset = language[dot_pos:]
        else:
            codeset = ""

        if at_pos != -1:
            modifier = language[at_pos:]
        else:
            modifier = ""

        for i in range (mask + 1):
            if i & ~mask == 0:
                newlang = lang
                if (i & 1 << 2):
                    newlang += territory
                if (i & 1 << 0):
                    newlang += codeset
                if (i & 1 << 1):
                    newlang += modifier
                languages.insert (0, newlang)

    return languages

class PessulusDisabledApplets:
    (
        COLUMN_IID,
        COLUMN_NAME,
        COLUMN_ICON_NAME,
        COLUMN_ICON,
        COLUMN_DISABLED
    ) = range (5)

    def __init__ (self, treeview, lockdownbutton):
        self.notify_id = None
        self.key = "/apps/panel/global/disabled_applets"
        self.disabled_applets = None

        self.liststore = gtk.ListStore (str, str, str, gtk.gdk.Pixbuf, bool)
        self.liststore.set_sort_column_id (self.COLUMN_NAME, gtk.SORT_ASCENDING)

        self.treeview = treeview
        self.treeview.get_selection ().set_mode (gtk.SELECTION_SINGLE)
        self.treeview.set_model (self.liststore)
        self.treeview.connect ("screen-changed", self.__on_screen_changed)
        self.treeview.connect ("destroy", self.__on_destroyed)

        self.lockdownbutton = lockdownbutton
        self.lockdownbutton.connect ("toggled",
                                     self.__on_lockdownbutton_toggled)

        screen = self.treeview.get_screen ()
        self.icon_theme = gtk.icon_theme_get_for_screen (screen)

        self.icon_theme.connect ("changed", self.__on_icontheme_changed)

        self.__fill_liststore ()
        self.__create_columns ()

        (list, mandatory) = globalvar.applier.get_list (self.key,
                                                        gconf.VALUE_STRING)
        self.disabled_applets = set (list)
        self.__update_toggles ()
        self.lockdownbutton.set (mandatory)
        self.notify_id = globalvar.applier.notify_add (self.key,
                                                       self.__on_notified)

    def __on_screen_changed (self, widget, screen):
        self.icon_theme = gtk.icon_theme_get_for_screen (screen)
        self.__on_icontheme_changed (self.icon_theme)

    def __on_icontheme_changed (self, icontheme):
        def update_icon (model, path, iter, data):
            if model[iter][self.COLUMN_ICON_NAME] != "":
                model[iter][self.COLUMN_ICON] = icons.load_icon (self.icon_theme, model[iter][self.COLUMN_ICON_NAME])

        self.liststore.foreach (update_icon, self)

    def __fill_liststore (self):
        applets = bonobo.activation.query ("has_all (repo_ids, ['IDL:Bonobo/Control:1.0', 'IDL:GNOME/Vertigo/PanelAppletShell:1.0'])")

        languages = get_language_names ()

        for applet in applets:
            name = None
            icon = None
            # Workaround: bonobo_server_info_prop_lookup () is not wrapped
            for prop in applet.props:
                bestname = -1
                if prop.name[:5] == "name-" and prop.name[5:] in languages:
                    if bestname > languages.index (prop.name[5:]) or bestname == -1:
                        name = prop.v.value_string
                elif prop.name == "name" and bestname == -1:
                    name = prop.v.value_string
                elif prop.name == "panel:icon":
                    icon = prop.v.value_string
            iter = self.liststore.append ()
            self.liststore.set (iter,
                                self.COLUMN_IID, applet.iid,
                                self.COLUMN_NAME, name + " (" + applet.iid + ")",
                                self.COLUMN_ICON_NAME, icon,
                                self.COLUMN_ICON, icons.load_icon (self.icon_theme, icon))

    def __create_columns (self):
        column = gtk.TreeViewColumn ()
        self.treeview.append_column (column)
        
        cell = gtk.CellRendererToggle ()
        cell.connect ("toggled", self.__on_toggled)
        column.pack_start (cell, False)
        column.set_attributes (cell, active = self.COLUMN_DISABLED)

        column = gtk.TreeViewColumn ()
        column.set_spacing (6)
        self.treeview.append_column (column)

        cell = gtk.CellRendererPixbuf ()
        column.pack_start (cell, False)
        column.set_attributes (cell, pixbuf = self.COLUMN_ICON)

        cell = gtk.CellRendererText ()
        column.pack_start (cell, True)
        column.set_attributes (cell, text = self.COLUMN_NAME)

    def __on_lockdownbutton_toggled (self, lockdownbutton, mandatory):
        globalvar.applier.set_list (self.key, gconf.VALUE_STRING,
                                    list (self.disabled_applets),
                                    mandatory)

    def __on_toggled (self, toggle, path):
        def toggle_value (model, iter, column):
            model[iter][column] = not model[iter][column]
            return model[iter][column]

        iter = self.liststore.get_iter (path)
        active = toggle_value (self.liststore, iter, self.COLUMN_DISABLED)

        iid = self.liststore[iter][self.COLUMN_IID]
        if active:
            if iid not in self.disabled_applets:
                self.disabled_applets.add (iid)
                globalvar.applier.set_list (self.key, gconf.VALUE_STRING,
                                            list (self.disabled_applets),
                                            self.lockdownbutton.get ())
        elif iid in self.disabled_applets:
            self.disabled_applets.remove (iid)
            globalvar.applier.set_list (self.key, gconf.VALUE_STRING,
                                        list (self.disabled_applets),
                                        self.lockdownbutton.get ())

    def __on_notified (self, data):
        (list, mandatory) = globalvar.applier.get_list (self.key,
                                                        gconf.VALUE_STRING)
        gconf_set = set (list)
        if gconf_set != self.disabled_applets:
            self.disabled_applets = gconf_set
            self.__update_toggles ()
        if mandatory != self.lockdownbutton.get ():
            self.lockdownbutton.set (mandatory)

    def __update_toggles (self):
        def update_toggle (model, path, iter, data):
            active = model[iter][self.COLUMN_IID] in data.disabled_applets
            if model[iter][self.COLUMN_DISABLED] != active:
                model[iter][self.COLUMN_DISABLED] = active

        self.liststore.foreach (update_toggle, self)
        self.treeview.set_sensitive (globalvar.applier.key_is_writable (self.key))

    def __on_destroyed (self, treeview):
        if self.notify_id:
            if globalvar.applier:
                globalvar.applier.notify_remove (self.notify_id)
            self.notify_id = None