File: main.py

package info (click to toggle)
ibus-array 0.2.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,432 kB
  • sloc: ansic: 1,060; python: 279; sed: 6; makefile: 5; sh: 1
file content (149 lines) | stat: -rw-r--r-- 5,859 bytes parent folder | download
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# ibus-array - The Array 30 Engine for IBus
#
# Copyright (c) 2009-2019 Keng-Yu Lin  <kengyu@debian.org>
#                         Yu-Chun Wang <mainlander1122@gmail.com>
#
# 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, or
# 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.

import sys
import os
import gi
gi.require_version("Gtk", "3.0")
gi.require_version("IBus", "1.0")
from gi.repository import Gtk
from gi.repository import GLib
from gi.repository import IBus
from gettext import gettext as _
import gettext
import config

class Setup:
    def __init__(self, bus):
        self.__bus = bus
        self.__config = self.__bus.get_config()
        self.__config.connect("value-changed", self.on_value_changed, None)
        self.__create_ui()

    def __create_ui(self):
        gettext.bindtextdomain("ibus-array")
        gettext.textdomain("ibus-array")
        self.__window = Gtk.Dialog(
            title=_('ibus-array setup'),
            parent=None,
            modal=True)
        self.__window.add_buttons(
            Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
            Gtk.STOCK_OK, Gtk.ResponseType.OK)
        icon_file = os.path.join(config.datadir, "ibus-array", "icons", "ibus-array.png")
        self.__window.set_icon_from_file(icon_file)
        self.__special_notify_button = Gtk.CheckButton(label=_("Special Code Notification"))
        self.__window.vbox.pack_start(self.__special_notify_button, True, True, 10)
        self.__special_only_button = Gtk.CheckButton(label=_("Special Code Only Mode"))
        self.__window.vbox.pack_start(self.__special_only_button, True, True ,10)
        self.__output_simplified_button = Gtk.CheckButton(label=_("Convert output to simplified Chinese"))
        self.__window.vbox.pack_start(self.__output_simplified_button, True, True, 10)
        self.__use_shift_button = Gtk.CheckButton(label=_("Use Shift to switch English/Chinese mode"))
        self.__window.vbox.pack_start(self.__use_shift_button, True, True, 10)

        current_special_mode = self.__read("SpecialOnly", False)
        current_special_notify = self.__read("SpecialNotify", False)
        current_output_simplified = self.__read("OutputSimplified", False)
        current_use_shift = self.__read("UseShift", False)

        if current_special_notify:
            self.__special_notify_button.set_active(True)
        if current_special_mode:
            self.__special_only_button.set_active(True)
        if current_output_simplified:
            self.__output_simplified_button.set_active(True)
        if current_use_shift:
            self.__use_shift_button.set_active(True)

        self.__window.show_all()

    def run(self):
        res = self.__window.run()
        if res == Gtk.ResponseType.OK:
            self.apply()
        self.__window.destroy()

    def apply(self):
        select_special_notify = self.__special_notify_button.get_active()
        select_special_mode = self.__special_only_button.get_active()
        select_output_simplified = self.__output_simplified_button.get_active()
        select_use_shift = self.__use_shift_button.get_active()

        if select_special_notify:
            self.__write("SpecialNotify", GLib.Variant.new_boolean(True))
        else:
            self.__write("SpecialNotify", GLib.Variant.new_boolean(False))

        if select_special_mode:
            self.__write("SpecialOnly", GLib.Variant.new_boolean(True))
        else:
            self.__write("SpecialOnly", GLib.Variant.new_boolean(False))

        if select_output_simplified:
            self.__write("OutputSimplified", GLib.Variant.new_boolean(True))
        else:
            self.__write("OutputSimplified", GLib.Variant.new_boolean(False))

        if select_use_shift:
            self.__write("UseShift", GLib.Variant.new_boolean(True))
        else:
            self.__write("UseShift", GLib.Variant.new_boolean(False))

    def on_value_changed(self, config, section, name, value, data):
        if section == 'engine/Array':
            if name == 'SpecialNotify':
                if value:
                    self.__special_notify_button.set_active(True)
                else:
                    self.__special_notify_button.set_active(False)

            elif name == 'SpecialOnly':
                if value:
                    self.__special_only_button.set_active(True)
                else:
                    self.__special_only_button.set_active(False)

            elif name == 'OutputSimplified':
                if value:
                    self.__output_simplified_button.set_active(True)
                else:
                    self.__output_simplified_button.set_active(False)

            elif name == 'UseShift':
                if value:
                    self.__use_shift_button.set_active(True)
                else:
                    self.__use_shift_button.set_active(False)

    def __read(self, name, v):
        value = self.__config.get_value("engine/Array", name)
        if value is None:
            return v
        return value

    def __write(self, name, v):
        return self.__config.set_value("engine/Array", name, v)

if __name__ == '__main__':
    bus = IBus.Bus()
    if bus.is_connected():
        Setup(bus).run()