# -*- coding: utf-8 -*-
# vim: set et sw=4 sts=4
#
# ibus-table - The Tables engine for IBus
#
# Copyright (c) 2008-2009 Yu Yuwei <acevery@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.1 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 library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#
# $Id: $
#

import ibus
import table
import tabsqlitedb
import os
import dbus

from gettext import dgettext
_  = lambda a : dgettext ("ibus-table", a)
N_ = lambda a : a

fatory_base_path = "/com/redhat/IBus/engines/table/%s/factory"
engine_base_path = "/com/redhat/IBus/engines/table/%s/engine/"

class EngineFactory (ibus.EngineFactoryBase):
    """Table IM Engine Factory"""
    def __init__ (self, bus, db, icon=""):
        import locale
        # here the db should be the abs path to sql db
        # this is the backend sql db we need for our IME
        # we need get lots of IME property from this db :)
        #self.db = tabsqlitedb.tabsqlitedb( name = database )
        
        # the name for dbus
        self.dbusname = os.path.basename(db).replace('.db','')
        self.factory_path = fatory_base_path % self.dbusname
        self.engine_path = engine_base_path % self.dbusname
        udb = os.path.basename(db).replace('.db','-user.db') 
        self.db = tabsqlitedb.tabsqlitedb( name = db,user_db = udb )
        ulocale = locale.getdefaultlocale ()[0].lower()
        self.name     =  self.db.get_ime_property ('name.%s' % ulocale) 
        if not self.name:
            self.name         = _( self.db.get_ime_property ('name') )
        self.uuid        = self.db.get_ime_property ('uuid')
        self.authors    = self.db.get_ime_property ('author')
        if icon:
            self.icon = icon
        else:
            self.icon = '%s%s%s%s%s' % ( os.getenv("IBUS_TABLE_LOCATION") ,
                os.path.sep,'icons',os.path.sep, self.db.get_ime_property ('icon') )
        self.credits    = self.db.get_ime_property ('credit')
        self.lang        = self.db.get_ime_property ('languages') 
        # now we construct the info for ibus
        self.info = ibus.FactoryInfo(
            self.factory_path,
            self.name,
            self.lang,
            self.icon,
            self.authors,
            self.credits
            )
        
        # init factory
        self.bus = bus
        #super(EngineFactory,self).__init__(self.info, table.tabengine, self.engine_path, bus, self.factory_path)
        super(EngineFactory,self).__init__ (bus)
        self.engine_id=1
        self.db.db.commit()
        try:
            bus = dbus.Bus()
            user = os.path.basename( os.path.expanduser('~') )
            self._sm_bus = bus.get_object ("org.ibus.table.SpeedMeter.%s"\
                    % user, "/org/ibus/table/SpeedMeter")
            self._sm =  dbus.Interface(self._sm_bus,\
                    "org.ibus.table.SpeedMeter") 
            self._sm.Regist()
        except:
            self._sm = None
    
    def create_engine(self, engine_name):
        # because we need db to be past to Engine
        engine = table.tabengine(self.bus, self.engine_path + str(self.engine_id), self.db)
        self.engine_id += 1
        #return engine.get_dbus_object()
        return engine

    def do_destroy (self):
        '''Destructor, which finish some task for IME'''
        # we need to sync the temp userdb in memory to the user_db on disk
        self.db.sync_usrdb ()
        #print "Have synced user db\n"
        try:
            self._sm.Exit()
        except:
            pass
        super(EngineFactory,self).do_destroy()



@@ -1049,71 +1049,8 @@
  a%0A%0A
-fatory_base_path = %22/com/redhat/IBus/engines/table/%25s/factory%22%0A
 engi
@@ -1236,30 +1236,8 @@
 %22):%0A
-        import locale%0A
     
@@ -1569,69 +1569,8 @@
 '')%0A
-        self.factory_path = fatory_base_path %25 self.dbusname%0A
     
@@ -1683,24 +1683,24 @@
 -user.db') %0A
+
         self
@@ -1760,955 +1760,8 @@
 b )%0A
-        ulocale = locale.getdefaultlocale ()%5B0%5D.lower()%0A        self.name     =  self.db.get_ime_property ('name.%25s' %25 ulocale) %0A        if not self.name:%0A            self.name         = _( self.db.get_ime_property ('name') )%0A        self.uuid        = self.db.get_ime_property ('uuid')%0A        self.authors    = self.db.get_ime_property ('author')%0A        if icon:%0A            self.icon = icon%0A        else:%0A            self.icon = '%25s%25s%25s%25s%25s' %25 ( os.getenv(%22IBUS_TABLE_LOCATION%22) ,%0A                os.path.sep,'icons',os.path.sep, self.db.get_ime_property ('icon') )%0A        self.credits    = self.db.get_ime_property ('credit')%0A        self.lang        = self.db.get_ime_property ('languages') %0A        # now we construct the info for ibus%0A        self.info = ibus.FactoryInfo(%0A            self.factory_path,%0A            self.name,%0A            self.lang,%0A            self.icon,%0A            self.authors,%0A            self.credits%0A            )%0A
     

remove something outdated