File: lockdownbutton.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 (124 lines) | stat: -rw-r--r-- 3,461 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
#!/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 gobject
import gtk

import globalvar

def load_image (name):
    image = gtk.Image ()
    image.set_from_icon_name (name, gtk.ICON_SIZE_MENU)
    image.show ()
    return image

class PessulusLockdownButton (gobject.GObject):

    __gsignals__ = {
        "toggled" : ( gobject.SIGNAL_RUN_LAST, None, (gobject.TYPE_BOOLEAN,) )
    }

    def __init__ (self):
        gobject.GObject.__init__(self)

        self.locked = False
        self.button = None

        self.locked_image = load_image ("stock_lock")
        self.unlocked_image = load_image ("stock_lock-open")

    def new ():
        lockdownbutton = PessulusLockdownButton ()

        lockdownbutton.button = gtk.Button ()
        lockdownbutton.button.show ()

        lockdownbutton.__connect_and_update ()
        return lockdownbutton
    new = staticmethod (new)

    def new_with_widget (button):
        lockdownbutton = PessulusLockdownButton ()

        lockdownbutton.button = button
        button.remove (button.get_child ())

        lockdownbutton.__connect_and_update ()
        return lockdownbutton
    new_with_widget = staticmethod (new_with_widget)

    def get_widget (self):
        return self.button

    def get (self):
        return self.locked

    def set (self, bool):
        if self.locked != bool:
            self.locked = bool
            self.emit ("toggled", self.locked)

        self.__update ()

    def __connect_and_update (self):
        self.button.set_relief (gtk.RELIEF_NONE)
        self.button.connect ("clicked", self.__on_button_clicked)

        self.set (False)

        if globalvar.applier.supports_mandatory_settings ():
            self.button.show ()
        else:
            self.button.hide ()

    def __update (self):
        self.__set_button_icon ()
        self.__set_tooltip ()

    def __set_tooltip (self):
        if self.locked:
            tooltip = _("Click to make this setting not mandatory")
        else:
            tooltip = _("Click to make this setting mandatory")

        globalvar.tooltips.set_tip (self.button, tooltip)

    def __set_button_icon (self):
        if self.locked:
            newimage = self.locked_image
        else:
            newimage = self.unlocked_image

        child = self.button.get_child ()
        if child != newimage:
            if child != None:
                self.button.remove (child)
            self.button.add (newimage)

    def __on_button_clicked (self, button):
        self.locked = not self.locked
        self.__update ()

        self.emit ("toggled", self.locked)

if gtk.pygtk_version < (2, 8, 0):
    gobject.type_register (PessulusLockdownButton)