2008-07-15 12:57:18 +04:00
|
|
|
# vim:set et sts=4 sw=4:
|
2008-06-19 14:30:26 +04:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
2008-05-12 11:18:48 +04:00
|
|
|
import dbus.lowlevel
|
|
|
|
import ibus
|
|
|
|
import gobject
|
|
|
|
|
2008-07-15 07:17:59 +04:00
|
|
|
class Connection(ibus.Object):
|
2008-07-15 12:57:18 +04:00
|
|
|
__gsignals__ = {
|
|
|
|
"dbus-signal" : (
|
|
|
|
gobject.SIGNAL_RUN_FIRST,
|
|
|
|
gobject.TYPE_NONE,
|
|
|
|
(gobject.TYPE_PYOBJECT, )
|
|
|
|
)
|
|
|
|
}
|
|
|
|
def __init__(self, dbusconn):
|
|
|
|
super(Connection, self).__init__()
|
2008-07-25 11:02:01 +04:00
|
|
|
self.__dbusconn = dbusconn
|
|
|
|
self.__watch_dirs = set()
|
2008-07-15 12:57:18 +04:00
|
|
|
dbusconn.add_message_filter(self.message_filter_cb)
|
2008-07-15 07:01:36 +04:00
|
|
|
|
2008-07-15 12:57:18 +04:00
|
|
|
def message_filter_cb(self, dbusconn, message):
|
|
|
|
if message.is_signal(dbus.LOCAL_IFACE, "Disconnected"):
|
|
|
|
self.destroy()
|
|
|
|
return dbus.lowlevel.HANDLER_RESULT_HANDLED
|
2008-07-15 07:01:36 +04:00
|
|
|
|
2008-07-15 12:57:18 +04:00
|
|
|
if message.get_type() == 4: # is signal
|
|
|
|
if self.dispatch_dbus_signal(message):
|
|
|
|
return dbus.lowlevel.HANDLER_RESULT_HANDLED
|
2008-07-15 07:01:36 +04:00
|
|
|
|
2008-07-15 12:57:18 +04:00
|
|
|
return dbus.lowlevel.HANDLER_RESULT_NOT_YET_HANDLED
|
2008-05-12 11:18:48 +04:00
|
|
|
|
2008-07-15 12:57:18 +04:00
|
|
|
def get_object(self, path):
|
2008-07-25 11:02:01 +04:00
|
|
|
return self.__dbusconn.get_object("no.name", path)
|
2008-05-12 11:18:48 +04:00
|
|
|
|
2008-07-15 12:57:18 +04:00
|
|
|
def emit_dbus_signal(self, name, *args):
|
|
|
|
message = dbus.lowlevel.SignalMessage(ibus.IBUS_PATH, ibus.IBUS_IFACE, name)
|
|
|
|
message.append(*args)
|
2008-07-25 11:02:01 +04:00
|
|
|
self.__dbusconn.send_message(message)
|
|
|
|
self.__dbusconn.flush()
|
2008-05-12 11:18:48 +04:00
|
|
|
|
2008-07-15 12:57:18 +04:00
|
|
|
def do_destroy(self):
|
2008-07-25 11:02:01 +04:00
|
|
|
self.__dbusconn = None
|
2008-05-12 11:18:48 +04:00
|
|
|
|
2008-07-15 12:57:18 +04:00
|
|
|
def dispatch_dbus_signal(self, message):
|
|
|
|
self.emit("dbus-signal", message)
|
2008-05-12 11:18:48 +04:00
|
|
|
|
2008-07-15 12:57:18 +04:00
|
|
|
def add_watch_dir(self, dir):
|
2008-07-25 11:02:01 +04:00
|
|
|
if dir in self.__watch_dirs:
|
2008-07-15 12:57:18 +04:00
|
|
|
return False
|
2008-07-25 11:02:01 +04:00
|
|
|
self.__watch_dirs.add(dir)
|
2008-07-15 12:57:18 +04:00
|
|
|
return True
|
2008-07-05 14:23:54 +04:00
|
|
|
|
2008-07-15 12:57:18 +04:00
|
|
|
def remove_watch_dir(self, dir):
|
2008-07-25 11:02:01 +04:00
|
|
|
if dir not in self.__watch_dirs:
|
2008-07-15 12:57:18 +04:00
|
|
|
return False
|
2008-07-25 11:02:01 +04:00
|
|
|
self.__watch_dirs.remove(dir)
|
2008-07-15 12:57:18 +04:00
|
|
|
return True
|
2008-07-05 14:23:54 +04:00
|
|
|
|
2008-07-15 12:57:18 +04:00
|
|
|
def get_dbusconn(self):
|
2008-07-25 11:02:01 +04:00
|
|
|
return self.__dbusconn
|
2008-07-05 14:23:54 +04:00
|
|
|
|
2008-07-15 07:17:59 +04:00
|
|
|
gobject.type_register(Connection)
|