Use Object as the base class to replace Pointer.
authorPeng Huang <shawn.p.huang@gmail.com>
Sun, 18 Apr 2010 04:27:37 +0000 (12:27 +0800)
committerPeng Huang <shawn.p.huang@gmail.com>
Mon, 19 Apr 2010 02:49:40 +0000 (10:49 +0800)
12 files changed:
src/Bus.h
src/Config.cc
src/Config.h
src/LookupTable.h
src/Main.cc
src/Makefile.am
src/Object.h [new file with mode: 0644]
src/PinyinEngine.cc
src/PinyinEngine.h
src/PinyinProperties.cc
src/Property.h
src/Text.h

index 6de9d66..74b0ad4 100644 (file)
--- a/src/Bus.h
+++ b/src/Bus.h
@@ -2,19 +2,22 @@
 #define __PY_BUS_H_
 
 #include <ibus.h>
-#include "Pointer.h"
+#include "Object.h"
 
 namespace PY {
 
-class Bus : public Pointer <IBusBus> {
+class Bus : Object {
 public:
-    Bus (void) {
-        set (ibus_bus_new ());
+    Bus (void) : Object (ibus_bus_new ()) {
     }
 
     bool isConnected (void) {
         return ibus_bus_is_connected (*this);
     }
+
+    operator IBusBus * (void) const {
+        return get<IBusBus> ();
+    }
 };
 
 };
index e01d40f..c73b2de 100644 (file)
@@ -152,7 +152,7 @@ inline bool
 Config::read (const gchar *section, const gchar *name, bool defval)
 {
     GValue value = {0};
-    if (ibus_config_get_value (m_config, section, name, &value)) {
+    if (ibus_config_get_value (get<IBusConfig> (), section, name, &value)) {
         if (G_VALUE_TYPE (&value) == G_TYPE_BOOLEAN)
             return g_value_get_boolean (&value);
     }
@@ -163,7 +163,7 @@ inline gint
 Config::read (const gchar *section, const gchar *name, gint defval)
 {
     GValue value = {0};
-    if (ibus_config_get_value (m_config, section, name, &value)) {
+    if (ibus_config_get_value (get<IBusConfig> (), section, name, &value)) {
         if (G_VALUE_TYPE (&value) == G_TYPE_INT)
             return g_value_get_int (&value);
     }
index 07f16f1..e2f5a05 100644 (file)
@@ -4,16 +4,16 @@
 #include <glib.h>
 #include <glib-object.h>
 #include <ibus.h>
-#include "Pointer.h"
+#include "Object.h"
+#include "Bus.h"
 
 namespace PY {
 
-class Config {
+class Config : Object {
 public:
-    Config (Pointer<IBusBus> & bus) {
-        m_config = ibus_bus_get_config (bus);
+    Config (Bus & bus) : Object (ibus_bus_get_config (bus)) {
         readDefaultValues ();
-        g_signal_connect ((IBusConfig *) m_config, "value-changed", G_CALLBACK (valueChangedCallback), this);
+        g_signal_connect (get<IBusConfig> (), "value-changed", G_CALLBACK (valueChangedCallback), this);
     }
 
     static guint option (void) { return m_option & m_option_mask; }
@@ -44,9 +44,6 @@ private:
                                       Config        *self);
 
 private:
-    Pointer<IBusConfig> m_config;
-
-private:
     static guint m_option;
     static guint m_option_mask;
 
index 91ff809..2fd8fb2 100644 (file)
@@ -2,18 +2,18 @@
 #define __PY_LOOKUP_TABLE_H_
 
 #include <ibus.h>
-#include "Pointer.h"
+#include "Object.h"
 #include "Text.h"
 
 namespace PY {
 
-class LookupTable : public Pointer <IBusLookupTable> {
+class LookupTable : Object {
 public:
     LookupTable (guint page_size = 10,
                  guint cursor_pos = 0,
                  gboolean cursor_visible = TRUE,
                  gboolean round = FALSE)
-        : Pointer <IBusLookupTable> (ibus_lookup_table_new (page_size, cursor_pos, cursor_visible, round)) { }
+        : Object (ibus_lookup_table_new (page_size, cursor_pos, cursor_visible, round)) { }
 
     guint pageSize (void) { return ibus_lookup_table_get_page_size (*this); }
     guint orientation (void) { return ibus_lookup_table_get_orientation (*this); }
@@ -32,6 +32,10 @@ public:
     void appendCandidate (IBusText *text) {
         ibus_lookup_table_append_candidate (*this, text);
     }
+
+    operator IBusLookupTable * (void) const {
+        return get<IBusLookupTable> ();
+    }
 };
 
 };
index f2e1126..d7a11cc 100644 (file)
@@ -50,7 +50,7 @@ start_component (void)
 
     Config config (bus);
 
-    g_signal_connect (bus, "disconnected", G_CALLBACK (ibus_disconnected_cb), NULL);
+    g_signal_connect ((IBusBus *)bus, "disconnected", G_CALLBACK (ibus_disconnected_cb), NULL);
 
     component = ibus_component_new ("org.freedesktop.IBus.Pinyin",
                                     N_("Pinyin input method"),
index 1b56c86..a936803 100644 (file)
@@ -73,6 +73,7 @@ ibus_engine_h_sources = \
        FullPinyinEditor.h \
        HalfFullConverter.h \
        LookupTable.h \
+       Object.h \
        Phrase.h \
        PhraseArray.h \
        PhraseEditor.h \
diff --git a/src/Object.h b/src/Object.h
new file mode 100644 (file)
index 0000000..8b1a6c8
--- /dev/null
@@ -0,0 +1,30 @@
+#ifndef __PY_OBJECT_H_
+#define __PY_OBJECT_H_
+
+#include <glib-object.h>
+#include "Pointer.h"
+
+namespace PY {
+
+class Object {
+protected:
+    template <typename T>
+    Object (T *p) : m_p ((GObject *)p) {
+    }
+
+    operator GObject * (void) const {
+        return m_p;
+    }
+
+    template <typename T>
+    T * get (void) const {
+        return (T *) (GObject *) m_p;
+    }
+
+private:
+    Pointer<GObject> m_p;
+};
+
+};
+
+#endif
index 89cb515..5451021 100644 (file)
@@ -194,7 +194,7 @@ PinyinEngine::candidateClicked (guint index, guint button, guint state)
 void
 PinyinEngine::slotCommitText (Text & text)
 {
-    m_last_commit_text = text;
+    // m_last_commit_text = text;
     ibus_engine_commit_text (m_engine, text);
     if (m_input_mode != MODE_INIT)
         m_input_mode = MODE_INIT;
index bbcced0..6d81ec8 100644 (file)
@@ -35,7 +35,7 @@ public:
             m_editors[i]->reset ();
         }
         m_fallback_editor->reset ();
-        m_last_commit_text = NULL;
+        // m_last_commit_text = NULL;
     }
 
 
@@ -90,7 +90,7 @@ private:
 
     EditorPtr m_editors[MODE_LAST];
     EditorPtr m_fallback_editor;
-    Text m_last_commit_text;
+    // Text m_last_commit_text;
 };
 
 };
index 94b7a80..b884f3f 100644 (file)
@@ -9,70 +9,45 @@ PinyinProperties::PinyinProperties (void)
     : m_mode_chinese (Config::initChinese ()),
       m_mode_full (Config::initFull ()),
       m_mode_full_punct (Config::initFullPunct ()),
-      m_mode_simp (Config::initSimpChinese ())
+      m_mode_simp (Config::initSimpChinese ()),
+      m_prop_chinese ("mode.chinese",
+                PROP_TYPE_NORMAL,
+                StaticText ("CN"),
+                m_mode_chinese ?
+                    PKGDATADIR"/icons/chinese.svg" :
+                    PKGDATADIR"/icons/english.svg",
+                StaticText (_("Chinese"))),
+      m_prop_full ("mode.full",
+                PROP_TYPE_NORMAL,
+                StaticText (m_mode_full ? "Aa" : "Aa"),
+                m_mode_full ?
+                    PKGDATADIR"/icons/full.svg" :
+                    PKGDATADIR"/icons/half.svg",
+                StaticText (_("Full/Half width"))),
+      m_prop_full_punct ("mode.full_punct",
+                PROP_TYPE_NORMAL,
+                StaticText (m_mode_full_punct ? ",。" : ",."),
+                m_mode_full_punct ?
+                    PKGDATADIR"/icons/full-punct.svg" :
+                    PKGDATADIR"/icons/half-punct.svg",
+                StaticText (_("Full/Half width punctuation"))),
+      m_prop_simp ( "mode.simp",
+                PROP_TYPE_NORMAL,
+                StaticText (m_mode_simp ? "简" : "繁"),
+                m_mode_simp ?
+                    PKGDATADIR"/icons/simp-chinese.svg" :
+                    PKGDATADIR"/icons/trad-chinese.svg",
+                StaticText (_("Simplfied/Traditional Chinese"))),
+      m_prop_setup ("setup",
+                PROP_TYPE_NORMAL,
+                StaticText (_("Pinyin preferences")),
+                "ibus-setup",
+                StaticText (_("Pinyin preferences")))
 {
-    /* create properties */
-    m_prop_chinese = ibus_property_new ("mode.chinese",
-                                        PROP_TYPE_NORMAL,
-                                        StaticText ("CN"),
-                                        m_mode_chinese ?
-                                            PKGDATADIR"/icons/chinese.svg" :
-                                            PKGDATADIR"/icons/english.svg",
-                                        StaticText (_("Chinese")),
-                                        TRUE,
-                                        TRUE,
-                                        PROP_STATE_UNCHECKED,
-                                        NULL);
     m_props.append (m_prop_chinese);
-
-    m_prop_full = ibus_property_new ("mode.full",
-                                     PROP_TYPE_NORMAL,
-                                     StaticText (m_mode_full? "Aa" : "Aa"),
-                                     m_mode_full ?
-                                        PKGDATADIR"/icons/full.svg" :
-                                        PKGDATADIR"/icons/half.svg",
-                                     StaticText (_("Full/Half width")),
-                                     TRUE,
-                                     TRUE,
-                                     PROP_STATE_UNCHECKED,
-                                     NULL);
     m_props.append (m_prop_full);
-
-    m_prop_full_punct = ibus_property_new ("mode.full_punct",
-                                           PROP_TYPE_NORMAL,
-                                           StaticText (m_mode_full_punct ? ",。" : ",."),
-                                           m_mode_full_punct ?
-                                                PKGDATADIR"/icons/full-punct.svg" :
-                                                PKGDATADIR"/icons/half-punct.svg",
-                                           StaticText (_("Full/Half width punctuation")),
-                                           TRUE,
-                                           TRUE,
-                                           PROP_STATE_UNCHECKED,
-                                           NULL);
     m_props.append (m_prop_full_punct);
-
-    m_prop_simp = ibus_property_new ("mode.simp",
-                                      PROP_TYPE_NORMAL,
-                                      StaticText (m_mode_simp ? "简" : "繁"),
-                                      m_mode_simp ?
-                                        PKGDATADIR"/icons/simp-chinese.svg" :
-                                        PKGDATADIR"/icons/trad-chinese.svg",
-                                      StaticText (_("Simplfied/Traditional Chinese")),
-                                      TRUE,
-                                      TRUE,
-                                      PROP_STATE_UNCHECKED,
-                                      NULL);
     m_props.append (m_prop_simp);
-
-    m_prop_setup = ibus_property_new ("setup",
-                                      PROP_TYPE_NORMAL,
-                                      StaticText (_("Pinyin preferences")),
-                                      "ibus-setup",
-                                      StaticText (_("Pinyin preferences")),
-                                      TRUE,
-                                      TRUE,
-                                      PROP_STATE_UNCHECKED,
-                                      NULL);
     m_props.append (m_prop_setup);
 
 }
index 5237cd7..0d7b72c 100644 (file)
@@ -2,39 +2,56 @@
 #define __PY_PROPERTY_H_
 
 #include <ibus.h>
-#include "Pointer.h"
+#include "Object.h"
 #include "Text.h"
 
 namespace PY {
 
-class Property : public Pointer<IBusProperty> {
+class Property : public Object {
 public:
-    Property & operator= (IBusProperty *p) {
-        set (p);
-        return *this;
+    Property (const gchar   *key,
+              IBusPropType   type = PROP_TYPE_NORMAL,
+              IBusText      *label = NULL,
+              const gchar   *icon = NULL,
+              IBusText      *tooltip = NULL,
+              gboolean       sensitive = TRUE,
+              gboolean       visible = TRUE,
+              IBusPropState  state = PROP_STATE_UNCHECKED,
+              IBusPropList  *props = NULL)
+        : Object (ibus_property_new (key, type, label, icon, tooltip, sensitive, visible, state, props)) { }
+
+    void setLabel (IBusText *text) {
+        ibus_property_set_label (get<IBusProperty> (), text);
     }
 
-    void setLabel (const Text & text) {
-        ibus_property_set_label (*this, text);
-    }
     void setLabel (const gchar *text) {
         setLabel (Text (text));
     }
+
     void setIcon (const gchar *icon) {
-        ibus_property_set_icon (*this, icon);
+        ibus_property_set_icon (get<IBusProperty> (), icon);
     }
+
     void setSensitive (gboolean sensitive) {
-        ibus_property_set_sensitive (*this, sensitive);
+        ibus_property_set_sensitive (get<IBusProperty> (), sensitive);
+    }
+
+    operator IBusProperty * (void) const {
+        return get<IBusProperty> ();
     }
 };
 
 
-class PropList : public Pointer<IBusPropList> {
+class PropList : Object {
 public:
-    PropList (void) : Pointer<IBusPropList> (ibus_prop_list_new ()) { }
+    PropList (void) : Object (ibus_prop_list_new ()) { }
 
     void append (Property &prop) {
-        ibus_prop_list_append (*this, prop);
+        ibus_prop_list_append (get<IBusPropList> (), prop);
+    }
+
+    operator IBusPropList * (void) const {
+        return get<IBusPropList> ();
     }
 };
 
index 983295c..451cd63 100644 (file)
@@ -2,32 +2,30 @@
 #define __PY_TEXT_H_
 
 #include <ibus.h>
-#include "Pointer.h"
+#include "Object.h"
 #include "String.h"
 
 namespace PY {
 
-class Text : public Pointer<IBusText> {
+class Text : Object {
 public:
-    Text () : Pointer<IBusText> () {}
     Text (IBusText *text)
-        : Pointer<IBusText> (text) {}
+        : Object (text) {}
     Text (const gchar *str)
-        : Pointer<IBusText> (ibus_text_new_from_string (str)) {}
+        : Object (ibus_text_new_from_string (str)) {}
 
     Text (const String & str)
-        : Pointer<IBusText> (ibus_text_new_from_string ((const gchar *) str)) {}
+        : Object (ibus_text_new_from_string ((const gchar *) str)) {}
 
     Text (gunichar ch)
-        : Pointer<IBusText> (ibus_text_new_from_unichar (ch)) {}
+        : Object (ibus_text_new_from_unichar (ch)) {}
 
     void appendAttribute (guint type, guint value, guint start, guint end) {
-        ibus_text_append_attribute (*this, type, value, start, end);
+        ibus_text_append_attribute (get<IBusText> (), type, value, start, end);
     }
 
-    Text & operator= (IBusText *p) {
-        set (p);
-        return *this;
+    operator IBusText * (void) const {
+        return get<IBusText> ();
     }
 };
 
@@ -42,6 +40,10 @@ public:
 
     StaticText (gunichar ch)
         : Text (ch) {}
+
+    operator IBusText * (void) const {
+        return Text::operator IBusText * ();
+    }
 };
 
 };