Loading com.emacsos.zero.ZeroPinyinService1.ZeroPinyinServiceInterface.xml +12 −0 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ <interface name="com.emacsos.zero.ZeroPinyinService1.ZeroPinyinServiceInterface"> <!-- GetCandidates: @preedit_str: the preedit string @fetch_size: how many candidates to fetch @candidates: the candidates Loading @@ -23,6 +24,17 @@ <arg type="au" name="matched_preedit_str_lengths" direction="out"/> </method> <!-- DeleteCandidate @candidate: the candidate to delete. This will make given candidate disappear for current user in future typing. delete single character is not allowed. it will be a no-op. --> <method name="DeleteCandidate"> <arg type="s" name="candidate"/> </method> <method name="Quit"/> <annotation name="org.gtk.GDBus.C.Name" value="ZeroPinyinService"/> Loading main.c +59 −0 Original line number Diff line number Diff line #include <locale.h> #include <gio/gio.h> #include <glib-unix.h> #include "zero-pinyin-service.h" Loading Loading @@ -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; } Loading @@ -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), Loading Loading @@ -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; Loading Loading @@ -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); Loading meson.build +1 −1 Original line number Diff line number Diff line # -*- mode: conf -*- project('zero-pinyin-service', ['c', 'cpp'], version: '0.4.0', version: '0.5.0', license: 'GPL', default_options: [ 'warning_level=2', Loading zero-pinyin-service.c +4 −1 Original line number Diff line number Diff line Loading @@ -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); Loading Loading
com.emacsos.zero.ZeroPinyinService1.ZeroPinyinServiceInterface.xml +12 −0 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ <interface name="com.emacsos.zero.ZeroPinyinService1.ZeroPinyinServiceInterface"> <!-- GetCandidates: @preedit_str: the preedit string @fetch_size: how many candidates to fetch @candidates: the candidates Loading @@ -23,6 +24,17 @@ <arg type="au" name="matched_preedit_str_lengths" direction="out"/> </method> <!-- DeleteCandidate @candidate: the candidate to delete. This will make given candidate disappear for current user in future typing. delete single character is not allowed. it will be a no-op. --> <method name="DeleteCandidate"> <arg type="s" name="candidate"/> </method> <method name="Quit"/> <annotation name="org.gtk.GDBus.C.Name" value="ZeroPinyinService"/> Loading
main.c +59 −0 Original line number Diff line number Diff line #include <locale.h> #include <gio/gio.h> #include <glib-unix.h> #include "zero-pinyin-service.h" Loading Loading @@ -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; } Loading @@ -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), Loading Loading @@ -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; Loading Loading @@ -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); Loading
meson.build +1 −1 Original line number Diff line number Diff line # -*- mode: conf -*- project('zero-pinyin-service', ['c', 'cpp'], version: '0.4.0', version: '0.5.0', license: 'GPL', default_options: [ 'warning_level=2', Loading
zero-pinyin-service.c +4 −1 Original line number Diff line number Diff line Loading @@ -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); Loading