refactor lua-plugin
authorPeng Wu <epico@dhcp-65-116.nay.redhat.com>
Sun, 11 Apr 2010 07:26:30 +0000 (15:26 +0800)
committerPeng Wu <alexepico@gmail.com>
Wed, 19 May 2010 02:09:32 +0000 (10:09 +0800)
lua/lua-plugin.c
lua/lua-plugin.h
lua/test-lua-plugin.c

index beab34c..d78fca9 100644 (file)
@@ -12,40 +12,8 @@ struct _IBusEnginePluginPrivate{
   GArray * lua_commands; /* Array of lua_command_t. */
 };
 
-
 G_DEFINE_TYPE (IBusEnginePlugin, ibus_engine_plugin, G_TYPE_OBJECT);
 
-static void
-ibus_engine_plugin_dispose (GObject *gobject)
-{
-  IBusEnginePlugin *self = IBUS_ENGINE_PLUGIN (gobject);
-  
-  /* do some cleaning here. */
-
-  /* Chain up to the parent class */
-  G_OBJECT_CLASS (ibus_engine_plugin_parent_class)->dispose(gobject);
-}
-
-static void
-ibus_engine_plugin_finalize (GObject *gobject)
-{
-  IBusEnginePlugin *self = IBUS_ENGINE_PLUGIN (gobject);
-
-  /* Chain up to the parent class */
-  G_OBJECT_CLASS (ibus_engine_plugin_parent_class)->dispose(gobject);
-}
-
-static void
-ibus_engine_plugin_class_init (IBusEnginePluginClass *klass)
-{
-  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
-
-  gobject_class->dispose = ibus_engine_plugin_dispose;
-  gobject_class->finalize = ibus_engine_plugin_finalize;
-
-  g_type_class_add_private (klass, sizeof (IBusEnginePluginPrivate));
-}
-
 static int
 lua_plugin_init(IBusEnginePluginPrivate * plugin){
   g_assert(NULL == plugin->L);
@@ -55,9 +23,8 @@ lua_plugin_init(IBusEnginePluginPrivate * plugin){
   /* enable libs in sandbox */
   lua_plugin_openlibs(plugin->L);
 
-  if ( NULL == plugin->lua_commands )
-    plugin->lua_commands = g_array_new(TRUE, TRUE, sizeof(lua_command_t));
-
+  g_assert ( NULL == plugin->lua_commands );
+  plugin->lua_commands = g_array_new(TRUE, TRUE, sizeof(lua_command_t));
   return 0;
 }
 
@@ -80,10 +47,43 @@ lua_plugin_fini(IBusEnginePluginPrivate * plugin){
   }
 
   lua_close(plugin->L);
+  plugin->L = NULL;
   return 0;
 }
 
 static void
+ibus_engine_plugin_dispose (GObject *gobject)
+{
+  IBusEnginePlugin *self = IBUS_ENGINE_PLUGIN (gobject);
+  
+  /* Chain up to the parent class */
+  G_OBJECT_CLASS (ibus_engine_plugin_parent_class)->dispose(gobject);
+}
+
+static void
+ibus_engine_plugin_finalize (GObject *gobject)
+{
+  IBusEnginePlugin *self = IBUS_ENGINE_PLUGIN (gobject);
+
+  /* do some cleaning here. */
+  lua_plugin_fini(self->priv);
+
+  /* Chain up to the parent class */
+  G_OBJECT_CLASS (ibus_engine_plugin_parent_class)->dispose(gobject);
+}
+
+static void
+ibus_engine_plugin_class_init (IBusEnginePluginClass *klass)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
+  gobject_class->dispose = ibus_engine_plugin_dispose;
+  gobject_class->finalize = ibus_engine_plugin_finalize;
+
+  g_type_class_add_private (klass, sizeof (IBusEnginePluginPrivate));
+}
+
+static void
 ibus_engine_plugin_init (IBusEnginePlugin *self)
 {
   IBusEnginePluginPrivate *priv;
@@ -91,5 +91,21 @@ ibus_engine_plugin_init (IBusEnginePlugin *self)
   self->priv = priv = IBUS_ENGINE_PLUGIN_GET_PRIVATE (self);
 
   memset(priv, 0, sizeof(IBusEnginePluginPrivate));
+
+  lua_plugin_init(priv);
+}
+
+IBusEnginePlugin * ibus_engine_plugin_new(){
+  IBusEnginePlugin * plugin;
+
+  plugin = (IBusEnginePlugin *) g_object_new (IBUS_TYPE_ENGINE_PLUGIN,
+                                              NULL);
+
+  return plugin;
+}
+
+/* will drop this function soon. */
+lua_State * ibus_engine_plugin_get_lua_State(IBusEnginePlugin * plugin){
+  return plugin->priv->L;
 }
 
index 677aab3..2d50329 100644 (file)
@@ -46,24 +46,30 @@ struct _IBusEnginePluginClass
 
 GType ibus_engine_plugin_get_type(void);
 
+IBusEnginePlugin * ibus_engine_plugin_new();
+
 /**
  * retrieve all available lua plugin commands.
  * return array of command informations of type lua_command_t.
  */
-GArray * lua_plugin_ime_get_available_commands(IBusEnginePlugin * plugin);
+GArray * ibus_engine_plugin_ime_get_available_commands(IBusEnginePlugin * plugin);
 
 /**
  * retval int: only support string or string array.
  */
-int lua_plugin_ime_call(IBusEnginePlugin * plugin, const lua_command_t * command, const char * argument /*optional, maybe NULL.*/);
+int ibus_engine_plugin_ime_call(IBusEnginePlugin * plugin, const lua_command_t * command, const char * argument /*optional, maybe NULL.*/);
 
 /**
  * retrieve the retval string value. (value has been copied.)
  */
-const char * lua_plugin_ime_get_retval(IBusEnginePlugin * plugin);
+const char * ibus_engine_plugin_ime_get_retval(IBusEnginePlugin * plugin);
 /**
  * retrieve the array of string values. (string values have been copied.)
  */
-GArray * lua_plugin_ime_get_retvals(IBusEnginePlugin * plugin);
+GArray * ibus_engine_plugin_ime_get_retvals(IBusEnginePlugin * plugin);
+
 
+/*< private >*/
+/* will drop this function soon. */
+lua_State * ibus_engine_plugin_get_lua_State(IBusEnginePlugin * plugin);
 #endif
index 44bce4a..c9a5f12 100644 (file)
@@ -35,16 +35,14 @@ static int run_test(lua_State *L, const char * filename){
 
 int main(int argc, char * argv[]){
   printf("starting test...\n");
+  g_type_init();
+  
+  IBusEnginePlugin * plugin;
+  plugin = ibus_engine_plugin_new();
 
-  IBusEnginePluginPrivate priv;
-
-  priv.L = NULL;
-  priv.lua_commands = NULL;
-
-  lua_plugin_init(&priv);
-
-  run_test(priv.L, "test.lua");
+  lua_State * L = ibus_engine_plugin_get_lua_State(plugin);
+  run_test(L, "test.lua");
   
-  lua_plugin_fini(&priv);
+  g_object_unref(plugin);
   return 0;
 }