~ubuntu-branches/ubuntu/trusty/ibus-cangjie/trusty

« back to all changes in this revision

Viewing changes to src/setup/__init__.py

  • Committer: Package Import Robot
  • Author(s): Anthony Wong
  • Date: 2013-03-14 02:55:52 UTC
  • Revision ID: package-import@ubuntu.com-20130314025552-u537ju4e2ommsegf
Tags: upstream-0.0.1~git20130325
ImportĀ upstreamĀ versionĀ 0.0.1~git20130325

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2012 - The IBus Cangjie authors
 
2
#
 
3
# This file is part of ibus-cangjie, the IBus Cangjie input method engine.
 
4
#
 
5
# ibus-cangjie is free software: you can redistribute it and/or modify
 
6
# it under the terms of the GNU Lesser General Public License as published by
 
7
# the Free Software Foundation, either version 3 of the License, or
 
8
# (at your option) any later version.
 
9
#
 
10
# ibus-cangjie is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# GNU Lesser General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License
 
16
# along with ibus-cangjie.  If not, see <http://www.gnu.org/licenses/>.
 
17
 
 
18
 
 
19
import gettext
 
20
# FIXME: Find a way to de-hardcode the gettext package
 
21
_ = lambda x: gettext.dgettext("ibus-cangjie", x)
 
22
 
 
23
from gi.repository import Gdk
 
24
from gi.repository import GLib
 
25
from gi.repository import Gtk
 
26
 
 
27
from ibus_cangjie.config import options, Config
 
28
 
 
29
 
 
30
titles = {"cangjie": _("Cangjie Preferences"),
 
31
          "quick": _("Quick Preferences")}
 
32
 
 
33
 
 
34
class Setup(object):
 
35
    def __init__ (self, bus, engine, datadir, gettext_package):
 
36
        self.__config = Config(bus, engine, self.on_value_changed)
 
37
 
 
38
        ui_file = GLib.build_filenamev([datadir, "setup.ui"])
 
39
        self.__builder = Gtk.Builder()
 
40
        self.__builder.set_translation_domain(gettext_package)
 
41
        self.__builder.add_from_file(ui_file)
 
42
 
 
43
        for option in options:
 
44
            prepare_func = getattr(self, "prepare_%s" % option["widget"])
 
45
            prepare_func(option)
 
46
 
 
47
        self.__window = self.__builder.get_object("setup_dialog")
 
48
        self.__window.set_title(titles[engine])
 
49
 
 
50
        try:
 
51
            # Pretend to be a GNOME Control Center dialog if appropriate
 
52
            self.gnome_cc_xid = int(GLib.getenv('GNOME_CONTROL_CENTER_XID'))
 
53
            self.__window.set_wmclass('gnome-control-center',
 
54
                                      'Gnome-control-center')
 
55
            self.__window.set_modal(True)
 
56
            self.__window.connect('notify::window', self.set_transient)
 
57
            self.__window.set_type_hint(Gdk.WindowTypeHint.DIALOG)
 
58
 
 
59
        except:
 
60
            # No problem here, we're just a normal dialog
 
61
            pass
 
62
 
 
63
        self.__window.show()
 
64
 
 
65
    def set_transient(self, obj, pspec):
 
66
        from gi.repository import GdkX11
 
67
        window = self.__window.get_window()
 
68
        if window != None:
 
69
            parent = GdkX11.X11Window.foreign_new_for_display(
 
70
                         Gdk.Display.get_default(), self.gnome_cc_xid)
 
71
 
 
72
        if parent != None:
 
73
            window.set_transient_for(parent)
 
74
 
 
75
    def prepare_switch(self, option):
 
76
        """Prepare a Gtk.Switch
 
77
 
 
78
        Set the switch named `option["name"]` (in)active based on the current
 
79
        engine config value.
 
80
        """
 
81
        name = option["name"]
 
82
 
 
83
        switch = self.__builder.get_object(name)
 
84
 
 
85
        v = self.__config.read(name)
 
86
        switch.set_active(v.unpack())
 
87
        switch.connect("notify::active", self.on_switch_toggled,
 
88
                       name, option["type"])
 
89
 
 
90
        setattr(self, name, switch)
 
91
 
 
92
    def prepare_combo(self, option):
 
93
        """Prepare a Gtk.ComboBox
 
94
 
 
95
        Set the combobox named `option['name']` to the current engine config
 
96
        value.
 
97
        """
 
98
        name = option["name"]
 
99
        current_value = self.__config.read(name).unpack()
 
100
 
 
101
        combo = self.__builder.get_object(name)
 
102
 
 
103
        store = combo.get_model()
 
104
        active_index = self.__get_active_index(store, current_value)
 
105
        combo.set_active(active_index)
 
106
 
 
107
        combo.connect("changed", self.on_combo_changed, name, option)
 
108
 
 
109
        setattr(self, name, combo)
 
110
 
 
111
    def __get_active_index(self, store, value):
 
112
        for i, n in enumerate(store):
 
113
            v = store.get_value(store.get_iter(i), 0)
 
114
            if v == value:
 
115
                return i
 
116
 
 
117
    def run(self):
 
118
        res = self.__window.run()
 
119
        self.__window.destroy()
 
120
 
 
121
    def on_value_changed(self, config, section, name, value, data):
 
122
        """Callback when the value of a widget is changed.
 
123
 
 
124
        We need to react, in case the value was changed from somewhere else,
 
125
        for example from another setup UI.
 
126
        """
 
127
        if section != self.__config.config_section:
 
128
            return
 
129
 
 
130
        try:
 
131
            widget = getattr(self, name)
 
132
        except AttributeError:
 
133
            return
 
134
 
 
135
        value = value.unpack()
 
136
 
 
137
        if isinstance(widget, Gtk.ComboBox):
 
138
            store = widget.get_model()
 
139
            v = store.get_value(store.get_iter(widget.get_active()), 0)
 
140
            if v != value:
 
141
                widget.set_active(self.__get_active_index(widget.get_model(),
 
142
                                                          value))
 
143
 
 
144
        else:
 
145
            if widget.get_active() != value:
 
146
                widget.set_active(value)
 
147
 
 
148
    def on_switch_toggled(self, widget, active, setting_name, variant_type):
 
149
        self.__config.write(setting_name, GLib.Variant(variant_type, widget.get_active()))
 
150
 
 
151
    def on_combo_changed(self, widget, setting_name, option):
 
152
        store = widget.get_model()
 
153
        value = store.get_value(store.get_iter(widget.get_active()), 0)
 
154
        self.__config.write(setting_name, GLib.Variant(option["type"], value))