Commit 402bd26e authored by Yuanle Song's avatar Yuanle Song
Browse files

v0.5.0 add new method DeleteCandidate

this method allow user to delete candidate so they won't appear in
future queries.
parent fbecc673
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -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
@@ -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"/>
+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"
@@ -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);
+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',
+4 −1
Original line number Diff line number Diff line
@@ -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);