1# vim:set et sts=4 sw=4:
2#
3# ibus - The Input Bus
4#
5# Copyright (c) 2012 Daiki Ueno <ueno@unixuser.org>
6# Copyright (c) 2011 Peng Huang <shawn.p.huang@gmail.com>
7#
8# This library is free software; you can redistribute it and/or
9# modify it under the terms of the GNU Lesser General Public
10# License as published by the Free Software Foundation; either
11# version 2.1 of the License, or (at your option) any later version.
12#
13# This library is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16# Lesser General Public License for more details.
17#
18# You should have received a copy of the GNU Lesser General Public
19# License along with this library; if not, write to the Free Software
20# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
21# USA
22
23from gi.repository import GObject
24
25from ..overrides import override
26
27# for newer pygobject: https://bugzilla.gnome.org/show_bug.cgi?id=686828
28# from ..module import get_introspection_module
29# IBus = get_introspection_module('IBus')
30from ..importer import modules
31IBus = modules['IBus']._introspection_module
32
33__all__ = []
34
35class Attribute(IBus.Attribute):
36    def __new__(cls, type=0, value=0, start_index=0, end_index=0):
37        return IBus.Attribute.new(type, value, start_index, end_index)
38
39Attribute = override(Attribute)
40__all__.append('Attribute')
41
42class Component(IBus.Component):
43    # Backward compatibility: allow non-keyword arguments
44    def __init__(self,
45                 name='',
46                 description='',
47                 version='',
48                 license='',
49                 author='',
50                 homepage='',
51                 command_line='',
52                 textdomain='',
53                 **kwargs):
54        super(Component, self).__init__(name=name,
55                                        description=description,
56                                        version=version,
57                                        license=license,
58                                        author=author,
59                                        homepage=homepage,
60                                        command_line=command_line,
61                                        textdomain=textdomain,
62                                        **kwargs)
63
64    # Backward compatibility: allow keyword arguments
65    def add_engine(self, engine=None, **kwargs):
66        if engine is None:
67            engine = EngineDesc(**kwargs)
68        super(Component, self).add_engine(engine)
69
70Component = override(Component)
71__all__.append('Component')
72
73class Config(IBus.Config):
74    # Backward compatibility: accept default arg
75    def get_value(self, section, name, default=None):
76        value = super(Config, self).get_value(section, name)
77        if value is None:
78            return default
79        return value
80
81    # Backward compatibility: unset value if value is None
82    # Note that we don't call GLib.Variant.unpack here
83    def set_value(self, section, name, value):
84        if value is None:
85            self.unset(section, name)
86        else:
87            super(Config, self).set_value(section, name, value)
88
89Config = override(Config)
90__all__.append('Config')
91
92class EngineDesc(IBus.EngineDesc):
93    # Backward compatibility: allow non-keyword arguments
94    def __init__(self,
95                 name='',
96                 longname='',
97                 description='',
98                 language='',
99                 license='',
100                 author='',
101                 icon='',
102                 layout='us',
103                 hotkeys='',
104                 rank=0,
105                 symbol='',
106                 setup='',
107                 layout_variant='',
108                 layout_option='',
109                 version='',
110                 textdomain='',
111                 **kwargs):
112        super(EngineDesc, self).__init__(name=name,
113                                         longname=longname,
114                                         description=description,
115                                         language=language,
116                                         license=license,
117                                         author=author,
118                                         icon=icon,
119                                         layout=layout,
120                                         hotkeys=hotkeys,
121                                         rank=rank,
122                                         symbol=symbol,
123                                         setup=setup,
124                                         layout_variant=layout_variant,
125                                         layout_option=layout_option,
126                                         version=version,
127                                         textdomain=textdomain,
128                                         **kwargs)
129
130EngineDesc = override(EngineDesc)
131__all__.append('EngineDesc')
132
133class Factory(IBus.Factory):
134    # Backward compatibility: allow non-keyword arguments
135    def __init__(self, bus=None, **kwargs):
136        if bus is not None:
137            kwargs.setdefault('connection', bus.get_connection())
138            kwargs.setdefault('object_path', IBus.PATH_FACTORY)
139        super(Factory, self).__init__(**kwargs)
140
141Factory = override(Factory)
142__all__.append('Factory')
143
144class Keymap(IBus.Keymap):
145    # Backward compatibility: allow non-keyword arguments
146    def __new__(cls, name):
147        return IBus.Keymap.new(name)
148
149    def __init__(*args, **kwargs):
150        pass
151
152Keymap = override(Keymap)
153__all__.append('Keymap')
154
155class LookupTable(IBus.LookupTable):
156    # Backward compatibility: allow non-keyword arguments
157    def __new__(cls,
158                page_size=5,
159                cursor_pos=0,
160                cursor_visible=True,
161                round=False,
162                orientation=IBus.Orientation.SYSTEM,
163                candidates=[],
164                labels=[]):
165        table = IBus.LookupTable.new(page_size,
166                                     cursor_pos,
167                                     cursor_visible,
168                                     round)
169        table.set_orientation(orientation)
170        for candidate in candidates:
171            table.append_candidate(candidate)
172        for index, label in enumerate(labels):
173            table.set_label(index, label)
174        return table
175
176    def __init__(self, *args, **kwargs):
177        pass
178
179    # Backward compatibility: rename
180    def show_cursor(self, visible):
181        self.set_cursor_visible(visible)
182
183    # Backward compatibility: rename
184    def clean(self):
185        self.clear()
186
187LookupTable = override(LookupTable)
188__all__.append('LookupTable')
189
190class Property(IBus.Property):
191    # Backward compatibility: allow non-keyword arguments
192    def __init__(self,
193                 key='',
194                 type=IBus.PropType.NORMAL,
195                 label='',
196                 icon='',
197                 tooltip='',
198                 sensitive=True,
199                 visible=True,
200                 state=IBus.PropState.UNCHECKED,
201                 symbol='',
202                 **kwargs):
203        prop_type = kwargs.pop('prop_type', type)
204        if label != None and not isinstance(label, IBus.Text):
205            label = Text(label)
206        if tooltip != None and not isinstance(tooltip, IBus.Text):
207            tooltip = Text(tooltip)
208        if symbol != None and not isinstance(symbol, IBus.Text):
209            symbol = Text(symbol)
210        super(Property, self).__init__(key=key,
211                                       prop_type=prop_type,
212                                       label=label,
213                                       icon=icon,
214                                       tooltip=tooltip,
215                                       sensitive=sensitive,
216                                       visible=visible,
217                                       state=state,
218                                       symbol=symbol,
219                                       **kwargs)
220
221Property = override(Property)
222__all__.append('Property')
223
224class Text(IBus.Text):
225    # Backward compatibility: allow non-keyword arguments
226    def __new__(cls, string='', attrs=None):
227        text = IBus.Text.new_from_string(string)
228        if attrs is not None:
229            text.set_attributes(attrs)
230        return text
231
232    def __init__(self, *args, **kwargs):
233        pass
234
235Text = override(Text)
236__all__.append('Text')
237