summaryrefslogtreecommitdiffstats
path: root/setup/main.py
blob: f6f2b1818308b6f42bd0490bc5ca6946b98680e9 (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
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
# vim:set et sts=4 sw=4:
#
# ibus - The Input Bus
#
# Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA  02111-1307  USA

from os import path
import gtk
import gobject
import ibus
from gtk import gdk, glade

(
    NAME_COLUMN,
    ENABLE_COLUMN,
    PRELOAD_COLUMN,
    VISIBLE_COLUMN,
    ICON_COLUMN,
    DATA_COLUMN,
) = range(6)

class Setup(object):
    def __init__(self):
        super(Setup, self).__init__()
        try:
            self.__bus = ibus.Bus()
        except:
            import traceback
            traceback.print_exc()
            self.__bus = None

        glade_file = path.join(path.dirname(__file__), "./setup.glade")
        self.__xml = glade.XML(glade_file)
        self.__dialog = self.__xml.get_widget("dialog_setup")
        self.__tree = self.__xml.get_widget("treeview_engines")
        model = self.__create_model()
        self.__tree.set_model(model)

        # add icon search path
        icon_theme = gtk.icon_theme_get_default()
        dir = path.dirname(__file__)
        icondir = path.join(dir, "..", "icons")
        icon_theme.prepend_search_path(icondir)

        # column for holiday names
        column = gtk.TreeViewColumn()
        column.set_title("Name")

        renderer = gtk.CellRendererPixbuf()
        renderer.set_property("xalign", 0.5)

        column.pack_start(renderer)
        column.set_attributes(renderer, icon_name = ICON_COLUMN, visible = VISIBLE_COLUMN)

        renderer = gtk.CellRendererText()
        renderer.set_property("xalign", 0.0)

        # column.set_clickable(True)
        column.pack_start(renderer)
        column.set_attributes(renderer, text = NAME_COLUMN)

        self.__tree.append_column(column)

        # column for started names
        renderer = gtk.CellRendererToggle()
        renderer.set_data('column', ENABLE_COLUMN)
        renderer.set_property("xalign", 0.5)
        renderer.connect("toggled", self.__item_toggled_cb, model)

        #col_offset = gtk.TreeViewColumn("Holiday", renderer, text=HOLIDAY_NAME_COLUMN)
        column = gtk.TreeViewColumn("Started", renderer, active = ENABLE_COLUMN, visible = VISIBLE_COLUMN)
        # column.set_clickable(True)
        # column.set_resizable(False)
        # column.set_fixed_width(30)
        # column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
        self.__tree.append_column(column)
        
        # column for preload names
        renderer = gtk.CellRendererToggle()
        renderer.set_data('column', PRELOAD_COLUMN)
        renderer.set_property("xalign", 0.5)
        renderer.connect("toggled", self.__item_toggled_cb, model)
       
        column = gtk.TreeViewColumn("Preload", renderer, active = PRELOAD_COLUMN, visible = VISIBLE_COLUMN)
        # column.set_clickable(True)
        # column.set_resizable(False)
        # column.set_fixed_width(30)
        # column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
        self.__tree.append_column(column)
        
        
        renderer = gtk.CellRendererText()
        column = gtk.TreeViewColumn("", renderer, active = PRELOAD_COLUMN, visible = VISIBLE_COLUMN)
        self.__tree.append_column(column)



    def __item_toggled_cb(self, cell, path_str, model):

        # get toggled iter
        iter = model.get_iter_from_string(path_str)
        data = model.get_value(iter, DATA_COLUMN)

        # do something with the value
        if data[6] == False:
            try:
                self.__bus.register_start_engine(data[1], data[0])
            except Exception, e:
                dlg = gtk.MessageDialog(type = gtk.MESSAGE_ERROR,
                        buttons = gtk.BUTTONS_CLOSE,
                        message_format = str(e))
                dlg.run()
                return
        else:
            try:
                self.__bus.register_stop_engine(data[1], data[0])
            except Exception, e:
                dlg = gtk.MessageDialog(type = gtk.MESSAGE_ERROR,
                        buttons = gtk.BUTTONS_CLOSE,
                        message_format = str(e))
                dlg.run()
                return
        data[6] = not data[6]

        # set new value
        model.set(iter, ENABLE_COLUMN, data[6])


    def __create_model(self):
        # create tree store
        model = gtk.TreeStore(
            gobject.TYPE_STRING,
            gobject.TYPE_BOOLEAN,
            gobject.TYPE_BOOLEAN,
            gobject.TYPE_BOOLEAN,
            gobject.TYPE_STRING,
            gobject.TYPE_PYOBJECT)

        langs = dict()

        for name, lang, icon, author, credits, _exec, started in self.__bus.register_list_engines():
            _lang = ibus.LANGUAGES.get(lang, "other")
            if _lang not in langs:
                langs[_lang] = list()
            langs[_lang].append([name, lang, icon, author, credits, _exec, started])

        keys = langs.keys()
        keys.sort()
        for key in keys:
            iter = model.append(None)
            model.set(iter,
                NAME_COLUMN, key,
                ENABLE_COLUMN, False,
                PRELOAD_COLUMN, False,
                VISIBLE_COLUMN, False,
                ICON_COLUMN, None,
                DATA_COLUMN, None)
            langs[key].sort()
            for name, lang, icon, author, credits, _exec, started in langs[key]:
                child_iter = model.append(iter)
                model.set(child_iter,
                    NAME_COLUMN, name,
                    ENABLE_COLUMN, started,
                    PRELOAD_COLUMN, False,
                    VISIBLE_COLUMN, True,
                    ICON_COLUMN, icon,
                    DATA_COLUMN, 
                    [name, lang, icon, author, credits, _exec, started])

        return model


    def run(self):
        return self.__dialog.run()

if __name__ == "__main__":
    Setup().run()