diff --git a/com.emacsos.zero.ZeroPinyinService1.ZeroPinyinServiceInterface.xml b/com.emacsos.zero.ZeroPinyinService1.ZeroPinyinServiceInterface.xml index f0876deedd51a87de711fd636483ae0a3d60c3fd..24401bc7a9f5a2712696abada9701df91242a3dc 100644 --- a/com.emacsos.zero.ZeroPinyinService1.ZeroPinyinServiceInterface.xml +++ b/com.emacsos.zero.ZeroPinyinService1.ZeroPinyinServiceInterface.xml @@ -5,6 +5,7 @@ + + + + diff --git a/main.c b/main.c index 81186ad2320f0089817c685d2393d386f4f9284b..a4bd63fa40b292fa307ad5c3137c59e5adefacd3 100644 --- a/main.c +++ b/main.c @@ -1,3 +1,4 @@ +#include #include #include #include "zero-pinyin-service.h" @@ -51,12 +52,54 @@ on_handle_get_candidates (ZeroPinyinService *object, return TRUE; } +static gboolean +on_handle_delete_candidate (ZeroPinyinService *object, + GDBusMethodInvocation *invocation, + const char *candidate, + AppData *appdata) +{ + if (! candidate) { + g_dbus_method_invocation_return_value (invocation, NULL); + return TRUE; + } + guint len = g_utf8_strlen (candidate, -1); + if (len == 1) { + g_message ("delete single character %s is a no-op", candidate); + g_dbus_method_invocation_return_value (invocation, NULL); + return TRUE; + } + g_message ("delete candidate %s", candidate); + + /* insert phrase to userdb.not_phrase table. */ + char *sql = NULL; + gboolean rb = FALSE; + sql = sqlite3_mprintf ("INSERT INTO userdb.not_phrase (phrase) VALUES (%Q);", candidate); + rb = sqlite3_exec_simple (appdata->db, sql); + if (! rb) { + g_warning ("insert phrase to not_phrase table failed"); + } + sqlite3_free (sql); + + /* delete phrase from userdb.py_phrase_x table. */ + guint table_suffix = len - 1; + sql = sqlite3_mprintf ("DELETE FROM userdb.py_phrase_%u WHERE phrase = %Q;", table_suffix, candidate); + rb = sqlite3_exec_simple (appdata->db, sql); + if (! rb) { + g_warning ("delete phrase from py_phrase_%u table failed", table_suffix); + } + sqlite3_free (sql); + + g_dbus_method_invocation_return_value (invocation, NULL); + return TRUE; +} + static gboolean on_handle_quit (ZeroPinyinService *object, GDBusMethodInvocation *invocation, AppData *appdata) { g_application_quit (appdata->app); + g_dbus_method_invocation_return_value (invocation, NULL); return TRUE; } @@ -75,6 +118,10 @@ on_bus_acquired (GDBusConnection *connection, "handle-get-candidates", G_CALLBACK (on_handle_get_candidates), appdata); + g_signal_connect (appdata->interface, + "handle-delete-candidate", + G_CALLBACK (on_handle_delete_candidate), + appdata); g_signal_connect (appdata->interface, "handle-quit", G_CALLBACK (on_handle_quit), @@ -171,6 +218,16 @@ config_db (AppData* appdata) g_warning ("attach userdb failed, query will not work."); goto attach_fail; } + sqlite3_free (sql); + + sql = "CREATE TABLE IF NOT EXISTS userdb.not_phrase (phrase TEXT UNIQUE);"; + rb = sqlite3_exec_simple (db, sql); + if (! rb) { + g_warning ("create userdb.not_phrase table failed, query will not work."); + sql = NULL; + goto attach_fail; + } + appdata->db = db; return; @@ -243,6 +300,8 @@ main (int argc, char *argv[]) GApplication *app = NULL; int status = 0; + setlocale (LC_ALL, ""); + app = g_application_new ("com.emacsos.zero.ZeroPinyinServiceApp", G_APPLICATION_FLAGS_NONE); g_assert_nonnull (app); diff --git a/meson.build b/meson.build index 417afdf9c66549089afcd14a1a4d4f8ba19c99d9..a0e565db464df4f132ef3551d2c27912c7243ba0 100644 --- a/meson.build +++ b/meson.build @@ -1,6 +1,6 @@ # -*- mode: conf -*- project('zero-pinyin-service', ['c', 'cpp'], - version: '0.4.0', + version: '0.5.0', license: 'GPL', default_options: [ 'warning_level=2', diff --git a/zero-pinyin-service.c b/zero-pinyin-service.c index 908cea2d3c0c117546ca062172f087eb84a7659a..5f993a83364c33935e1c730c82b9a513f47680fc 100644 --- a/zero-pinyin-service.c +++ b/zero-pinyin-service.c @@ -101,7 +101,10 @@ build_sql_for_n_pinyin (GList* pylist, sql, "SELECT user_freq, phrase, freq FROM " "userdb.py_phrase_%u WHERE ", n - 1); sql = g_string_append (sql, where_clause); - sql = g_string_append (sql, ") GROUP BY phrase ORDER BY user_freq DESC, freq DESC "); + sql = g_string_append ( + sql, ") " + "WHERE phrase NOT IN (SELECT phrase FROM userdb.not_phrase) " + "GROUP BY phrase ORDER BY user_freq DESC, freq DESC "); g_string_append_printf (sql, "LIMIT %u;", limit); char* result = sql->str; g_free (where_clause);