1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3
* Copyright (C) 2013 Richard Hughes <richard@hughsie.com>
5
* Licensed under the GNU General Public License Version 2
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27
#include <gs-plugin.h>
30
struct GsPluginPrivate {
40
gs_plugin_get_name (void)
42
return "local-ratings";
46
* gs_plugin_initialize:
49
gs_plugin_initialize (GsPlugin *plugin)
51
/* create private area */
52
plugin->priv = GS_PLUGIN_GET_PRIVATE (GsPluginPrivate);
53
plugin->priv->db_path = g_build_filename (g_get_home_dir (),
57
"hardcoded-ratings.db",
65
gs_plugin_get_deps (GsPlugin *plugin)
67
static const gchar *deps[] = {
68
"appstream", /* requires the app_id from the pkgname */
77
gs_plugin_destroy (GsPlugin *plugin)
79
g_free (plugin->priv->db_path);
80
sqlite3_close (plugin->priv->db);
84
* gs_plugin_local_ratings_load_db:
87
gs_plugin_local_ratings_load_db (GsPlugin *plugin,
90
const gchar *statement;
92
gchar *error_msg = NULL;
95
g_debug ("trying to open database '%s'", plugin->priv->db_path);
96
ret = gs_mkdir_parent (plugin->priv->db_path, error);
99
rc = sqlite3_open (plugin->priv->db_path, &plugin->priv->db);
100
if (rc != SQLITE_OK) {
104
GS_PLUGIN_ERROR_FAILED,
105
"Can't open transaction database: %s",
106
sqlite3_errmsg (plugin->priv->db));
110
/* we don't need to keep doing fsync */
111
sqlite3_exec (plugin->priv->db, "PRAGMA synchronous=OFF", NULL, NULL, NULL);
113
/* create table if required */
114
rc = sqlite3_exec (plugin->priv->db, "SELECT * FROM ratings LIMIT 1", NULL, NULL, &error_msg);
115
if (rc != SQLITE_OK) {
116
g_debug ("creating table to repair: %s", error_msg);
117
sqlite3_free (error_msg);
118
statement = "CREATE TABLE ratings ("
119
"app_id TEXT PRIMARY KEY,"
120
"rating INTEGER DEFAULT 0);";
121
sqlite3_exec (plugin->priv->db, statement, NULL, NULL, NULL);
130
* gs_plugin_local_ratings_sqlite_cb:
133
gs_plugin_local_ratings_sqlite_cb (void *data, gint argc, gchar **argv, gchar **col_name)
135
gint *rating = (gint *) data;
136
*rating = atoi (argv[0]);
141
* gs_plugin_local_find_app:
144
gs_plugin_local_find_app (GsPlugin *plugin, const gchar *app_id)
149
statement = g_strdup_printf ("SELECT rating FROM ratings WHERE app_id = '%s'", app_id);
150
sqlite3_exec (plugin->priv->db,
152
gs_plugin_local_ratings_sqlite_cb,
160
* gs_plugin_app_set_rating:
163
gs_plugin_app_set_rating (GsPlugin *plugin,
165
GCancellable *cancellable,
169
gchar *error_msg = NULL;
170
gchar *statement = NULL;
174
if (g_once_init_enter (&plugin->priv->loaded)) {
175
ret = gs_plugin_local_ratings_load_db (plugin, error);
176
g_once_init_leave (&plugin->priv->loaded, TRUE);
182
/* insert the entry */
183
statement = g_strdup_printf ("INSERT OR REPLACE INTO ratings (app_id, rating) "
184
"VALUES ('%s', '%i');",
186
gs_app_get_rating (app));
187
rc = sqlite3_exec (plugin->priv->db, statement, NULL, NULL, &error_msg);
188
if (rc != SQLITE_OK) {
191
GS_PLUGIN_ERROR_FAILED,
192
"SQL error: %s", error_msg);
193
sqlite3_free (error_msg);
207
gs_plugin_refine (GsPlugin *plugin,
209
GsPluginRefineFlags flags,
210
GCancellable *cancellable,
218
/* nothing to do here */
219
if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_RATING) == 0)
223
if (g_once_init_enter (&plugin->priv->loaded)) {
224
ret = gs_plugin_local_ratings_load_db (plugin, error);
225
g_once_init_leave (&plugin->priv->loaded, TRUE);
231
/* add any missing ratings data */
232
for (l = *list; l != NULL; l = l->next) {
233
app = GS_APP (l->data);
234
if (gs_app_get_id (app) == NULL)
236
if (gs_app_get_rating (app) != -1)
238
rating = gs_plugin_local_find_app (plugin, gs_app_get_id (app));
240
gs_app_set_rating (app, rating);
241
gs_app_set_rating_confidence (app, 100);
242
gs_app_set_rating_kind (app, GS_APP_RATING_KIND_USER);
244
gs_app_add_kudo (app, GS_APP_KUDO_POPULAR);