summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeng Huang <shawn.p.huang@gmail.com>2011-02-07 11:33:37 -0500
committerPeng Huang <shawn.p.huang@gmail.com>2011-02-07 11:33:37 -0500
commitf60c85749674f38b8e5fee222cd89fcc46ba2688 (patch)
treeccf8d2eca99e34bf292610d2abc71fb7220350a9
parenta142d959cdba0406a2c614fadfa9367803c31286 (diff)
downloadibus-pinyin-f60c85749674f38b8e5fee222cd89fcc46ba2688.tar.gz
Storing user database to disk, if engine idles more than 60 seconds.
BUG=none TEST=manual Review URL: http://codereview.appspot.com/4127050
-rw-r--r--src/PYDatabase.cc22
-rw-r--r--src/PYDatabase.h1
2 files changed, 17 insertions, 6 deletions
diff --git a/src/PYDatabase.cc b/src/PYDatabase.cc
index 4411d45..d6810ce 100644
--- a/src/PYDatabase.cc
+++ b/src/PYDatabase.cc
@@ -36,6 +36,7 @@ namespace PY {
#define DB_COLUMN_S0 (3)
#define DB_PREFETCH_LEN (6)
+#define DB_BACKUP_TIMEOUT (60)
std::unique_ptr<Database> Database::m_instance;
@@ -185,14 +186,16 @@ Query::fill (PhraseArray &phrases, gint count)
}
Database::Database (void)
- : m_db (NULL),
- m_timeout_id (0)
+ : m_db (NULL)
+ , m_timeout_id (0)
+ , m_timer (g_timer_new ())
{
open ();
}
Database::~Database (void)
{
+ g_timer_destroy (m_timer);
if (m_timeout_id != 0) {
saveUserDB ();
g_source_remove (m_timeout_id);
@@ -431,7 +434,11 @@ Database::timeoutCallback (gpointer data)
{
Database *self = static_cast<Database*> (data);
- if (self->saveUserDB ()) {
+ /* Get elapsed time since last modification of database. */
+ guint elapsed = (guint)g_timer_elapsed (self->m_timer, NULL);
+
+ if (elapsed >= DB_BACKUP_TIMEOUT &&
+ self->saveUserDB ()) {
self->m_timeout_id = 0;
return FALSE;
}
@@ -442,12 +449,15 @@ Database::timeoutCallback (gpointer data)
void
Database::modified (void)
{
+ /* Restart the timer */
+ g_timer_start (m_timer);
+
if (m_timeout_id != 0)
return;
- m_timeout_id = g_timeout_add (60000, // one minute
- Database::timeoutCallback,
- static_cast<gpointer> (this));
+ m_timeout_id = g_timeout_add_seconds (DB_BACKUP_TIMEOUT,
+ Database::timeoutCallback,
+ static_cast<gpointer> (this));
}
inline static gboolean
diff --git a/src/PYDatabase.h b/src/PYDatabase.h
index 8a474e1..70566e5 100644
--- a/src/PYDatabase.h
+++ b/src/PYDatabase.h
@@ -94,6 +94,7 @@ private:
String m_sql; /* sql stmt */
String m_buffer; /* temp buffer */
guint m_timeout_id;
+ GTimer *m_timer;
private:
static std::unique_ptr<Database> m_instance;