summaryrefslogtreecommitdiff
path: root/libappstream-builder/plugins/asb-plugin-ibus-sql.c
blob: 66f5fb5f78280a8813e8650f5c7abb342a3fdde5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2014-2015 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU Lesser General Public License Version 2.1
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <config.h>
#include <fnmatch.h>
#include <sqlite3.h>

#include <asb-plugin.h>

/**
 * asb_plugin_get_name:
 */
const gchar *
asb_plugin_get_name (void)
{
	return "ibus-sqlite";
}

/**
 * asb_plugin_add_globs:
 */
void
asb_plugin_add_globs (AsbPlugin *plugin, GPtrArray *globs)
{
	asb_plugin_add_glob (globs, "/usr/share/ibus-table/tables/*.db");
}

/**
 * _asb_plugin_check_filename:
 */
static gboolean
_asb_plugin_check_filename (const gchar *filename)
{
	if (asb_plugin_match_glob ("/usr/share/ibus-table/tables/*.db", filename))
		return TRUE;
	return FALSE;
}

/**
 * asb_plugin_check_filename:
 */
gboolean
asb_plugin_check_filename (AsbPlugin *plugin, const gchar *filename)
{
	return _asb_plugin_check_filename (filename);
}

/**
 * asb_plugin_sqlite_callback_cb:
 */
static int
asb_plugin_sqlite_callback_cb (void *user_data, int argc, char **argv, char **data)
{
	gchar **tmp = (gchar **) user_data;
	*tmp = g_strdup (argv[1]);
	return 0;
}

/**
 * asb_plugin_process_filename:
 */
static gboolean
asb_plugin_process_filename (AsbPlugin *plugin,
			     AsbPackage *pkg,
			     const gchar *filename,
			     GList **apps,
			     const gchar *tmpdir,
			     GError **error)
{
	gboolean ret = TRUE;
	gchar *error_msg = 0;
	gchar *filename_tmp;
	gint rc;
	guint i;
	sqlite3 *db = NULL;
	_cleanup_free_ gchar *basename = NULL;
	_cleanup_free_ gchar *description = NULL;
	_cleanup_free_ gchar *language_string = NULL;
	_cleanup_free_ gchar *name = NULL;
	_cleanup_free_ gchar *symbol = NULL;
	_cleanup_object_unref_ AsbApp *app = NULL;
	_cleanup_object_unref_ AsIcon *icon = NULL;
	_cleanup_strv_free_ gchar **languages = NULL;

	/* open IME database */
	filename_tmp = g_build_filename (tmpdir, filename, NULL);
	rc = sqlite3_open (filename_tmp, &db);
	if (rc) {
		ret = FALSE;
		g_set_error (error,
			     ASB_PLUGIN_ERROR,
			     ASB_PLUGIN_ERROR_FAILED,
			     "Can't open database: %s",
			     sqlite3_errmsg (db));
		goto out;
	}

	/* get name */
	rc = sqlite3_exec(db, "SELECT * FROM ime WHERE attr = 'name' LIMIT 1;",
			  asb_plugin_sqlite_callback_cb,
			  &name, &error_msg);
	if (rc != SQLITE_OK) {
		ret = FALSE;
		g_set_error (error,
			     ASB_PLUGIN_ERROR,
			     ASB_PLUGIN_ERROR_FAILED,
			     "Can't get IME name from %s: %s",
			     filename, error_msg);
		sqlite3_free(error_msg);
		goto out;
	}

	/* get symbol */
	rc = sqlite3_exec(db, "SELECT * FROM ime WHERE attr = 'symbol' LIMIT 1;",
			  asb_plugin_sqlite_callback_cb,
			  &symbol, &error_msg);
	if (rc != SQLITE_OK) {
		ret = FALSE;
		g_set_error (error,
			     ASB_PLUGIN_ERROR,
			     ASB_PLUGIN_ERROR_FAILED,
			     "Can't get IME symbol from %s: %s",
			     filename, error_msg);
		sqlite3_free(error_msg);
		goto out;
	}

	/* get languages */
	rc = sqlite3_exec(db, "SELECT * FROM ime WHERE attr = 'languages' LIMIT 1;",
			  asb_plugin_sqlite_callback_cb,
			  &language_string, &error_msg);
	if (rc != SQLITE_OK) {
		ret = FALSE;
		g_set_error (error,
			     ASB_PLUGIN_ERROR,
			     ASB_PLUGIN_ERROR_FAILED,
			     "Can't get IME languages from %s: %s",
			     filename, error_msg);
		sqlite3_free(error_msg);
		goto out;
	}

	/* get description */
	rc = sqlite3_exec(db, "SELECT * FROM ime WHERE attr = 'description' LIMIT 1;",
			  asb_plugin_sqlite_callback_cb,
			  &description, &error_msg);
	if (rc != SQLITE_OK) {
		ret = FALSE;
		g_set_error (error,
			     ASB_PLUGIN_ERROR,
			     ASB_PLUGIN_ERROR_FAILED,
			     "Can't get IME name from %s: %s",
			     filename, error_msg);
		sqlite3_free(error_msg);
		goto out;
	}

	/* this is _required_ */
	if (name == NULL || description == NULL) {
		ret = FALSE;
		g_set_error (error,
			     ASB_PLUGIN_ERROR,
			     ASB_PLUGIN_ERROR_FAILED,
			     "No 'name' and 'description' in %s",
			     filename);
		goto out;
	}

	/* create new app */
	basename = g_path_get_basename (filename);
	app = asb_app_new (pkg, basename);
	as_app_set_id_kind (AS_APP (app), AS_ID_KIND_INPUT_METHOD);
	as_app_add_category (AS_APP (app), "Addons");
	as_app_add_category (AS_APP (app), "InputSources");
	as_app_set_name (AS_APP (app), "C", name);
	as_app_set_comment (AS_APP (app), "C", description);
	if (symbol != NULL && symbol[0] != '\0')
		as_app_add_metadata (AS_APP (app), "X-IBus-Symbol", symbol);
	if (language_string != NULL) {
		languages = g_strsplit (language_string, ",", -1);
		for (i = 0; languages[i] != NULL; i++) {
			if (g_strcmp0 (languages[i], "other") == 0)
				continue;
			as_app_add_language (AS_APP (app),
					     100, languages[i]);
		}
	}
	asb_app_set_requires_appdata (app, TRUE);
	asb_app_set_hidpi_enabled (app, asb_context_get_flag (plugin->ctx, ASB_CONTEXT_FLAG_HIDPI_ICONS));

	/* add icon */
	icon = as_icon_new ();
	as_icon_set_kind (icon, AS_ICON_KIND_STOCK);
	as_icon_set_name (icon, "system-run-symbolic");
	as_app_add_icon (AS_APP (app), icon);

	asb_plugin_add_app (apps, AS_APP (app));
out:
	if (db != NULL)
		sqlite3_close (db);
	return ret;
}

/**
 * asb_plugin_process:
 */
GList *
asb_plugin_process (AsbPlugin *plugin,
		    AsbPackage *pkg,
		    const gchar *tmpdir,
		    GError **error)
{
	gboolean ret;
	GList *apps = NULL;
	guint i;
	gchar **filelist;

	filelist = asb_package_get_filelist (pkg);
	for (i = 0; filelist[i] != NULL; i++) {
		if (!_asb_plugin_check_filename (filelist[i]))
			continue;
		ret = asb_plugin_process_filename (plugin,
						   pkg,
						   filelist[i],
						   &apps,
						   tmpdir,
						   error);
		if (!ret) {
			g_list_free_full (apps, (GDestroyNotify) g_object_unref);
			apps = NULL;
			goto out;
		}
	}

	/* no desktop files we care about */
	if (apps == NULL) {
		g_set_error (error,
			     ASB_PLUGIN_ERROR,
			     ASB_PLUGIN_ERROR_FAILED,
			     "nothing interesting in %s",
			     asb_package_get_basename (pkg));
		goto out;
	}
out:
	return apps;
}