summaryrefslogtreecommitdiffstats
path: root/src/PYLibPinyin.cc
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2011-10-26 10:10:58 +0800
committerPeng Wu <alexepico@gmail.com>2011-12-22 12:23:14 +0800
commit59b266d410cedb70f2b8b2aa8c1619184b2fb0ee (patch)
tree481faf6447b0665fa38843ac5f1d8c0a14ccaba3 /src/PYLibPinyin.cc
parent4d6a548b773ab5d59641208b5668a000287075d3 (diff)
downloadibus-libpinyin-59b266d410cedb70f2b8b2aa8c1619184b2fb0ee.tar.gz
ibus-libpinyin-59b266d410cedb70f2b8b2aa8c1619184b2fb0ee.tar.xz
ibus-libpinyin-59b266d410cedb70f2b8b2aa8c1619184b2fb0ee.zip
add save support
Diffstat (limited to 'src/PYLibPinyin.cc')
-rw-r--r--src/PYLibPinyin.cc51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/PYLibPinyin.cc b/src/PYLibPinyin.cc
index 077e7e0..062b3c9 100644
--- a/src/PYLibPinyin.cc
+++ b/src/PYLibPinyin.cc
@@ -23,6 +23,8 @@
#include "PYTypes.h"
#include "PYConfig.h"
+#define LIBPINYIN_SAVE_TIMEOUT (5 * 60)
+
using namespace PY;
std::unique_ptr<LibPinyinBackEnd> LibPinyinBackEnd::m_instance;
@@ -30,11 +32,19 @@ std::unique_ptr<LibPinyinBackEnd> LibPinyinBackEnd::m_instance;
static LibPinyinBackEnd libpinyin_backend;
LibPinyinBackEnd::LibPinyinBackEnd () {
+ m_timeout_id = 0;
+ m_timer = g_timer_new();
m_pinyin_context = NULL;
m_chewing_context = NULL;
}
LibPinyinBackEnd::~LibPinyinBackEnd () {
+ g_timer_destroy (m_timer);
+ if (m_timeout_id != 0) {
+ saveUserDB ();
+ g_source_remove (m_timeout_id);
+ }
+
if (m_pinyin_context)
pinyin_fini(m_pinyin_context);
m_pinyin_context = NULL;
@@ -215,3 +225,44 @@ LibPinyinBackEnd::setChewingOptions (Config *config)
setFuzzyOptions (config, m_chewing_context);
return TRUE;
}
+
+void
+LibPinyinBackEnd::modified (void)
+{
+ /* Restart the timer */
+ g_timer_start (m_timer);
+
+ if (m_timeout_id != 0)
+ return;
+
+ m_timeout_id = g_timeout_add_seconds (LIBPINYIN_SAVE_TIMEOUT,
+ LibPinyinBackEnd::timeoutCallback,
+ static_cast<gpointer> (this));
+}
+
+gboolean
+LibPinyinBackEnd::timeoutCallback (gpointer data)
+{
+ LibPinyinBackEnd *self = static_cast<LibPinyinBackEnd *> (data);
+
+ /* Get the elapsed time since last modification of database. */
+ guint elapsed = (guint)g_timer_elapsed (self->m_timer, NULL);
+
+ if (elapsed >= LIBPINYIN_SAVE_TIMEOUT &&
+ self->saveUserDB ()) {
+ self->m_timeout_id = 0;
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+gboolean
+LibPinyinBackEnd::saveUserDB (void)
+{
+ if (m_pinyin_context)
+ pinyin_save (m_pinyin_context);
+ if (m_chewing_context)
+ pinyin_save (m_chewing_context);
+ return TRUE;
+}