| 1 |
/* font-manager-database.c |
| 2 |
* |
| 3 |
* Copyright (C) 2009-2024 Jerry Casiano |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. |
| 17 |
* |
| 18 |
* If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "font-manager-database.h" |
| 22 |
|
| 23 |
#include "font-manager-database-iterator.h" |
| 24 |
#include "font-manager-database-error.h" |
| 25 |
|
| 26 |
/** |
| 27 |
* SECTION: font-manager-database |
| 28 |
* @short_description: Database related functions |
| 29 |
* @title: Database |
| 30 |
* @include: font-manager-database.h |
| 31 |
* @stability: Unstable |
| 32 |
* |
| 33 |
* Database class and related functions. |
| 34 |
*/ |
| 35 |
|
| 36 |
#define CREATE_FONTS_TABLE "CREATE TABLE IF NOT EXISTS Fonts ( " \ |
| 37 |
"uid INTEGER PRIMARY KEY, filepath TEXT, findex INTEGER, family TEXT, " \ |
| 38 |
"style TEXT, spacing INTEGER, slant INTEGER, weight INTEGER, " \ |
| 39 |
"width INTEGER, description TEXT );\n" |
| 40 |
|
| 41 |
#define CREATE_INFO_TABLE "CREATE TABLE IF NOT EXISTS Metadata ( " \ |
| 42 |
"uid INTEGER PRIMARY KEY, filepath TEXT, findex INTEGER, family TEXT, " \ |
| 43 |
"style TEXT, owner INTEGER, psname TEXT, filetype TEXT, 'n-glyphs' INTEGER, " \ |
| 44 |
"copyright TEXT, version TEXT, description TEXT, 'license-data' TEXT, " \ |
| 45 |
"'license-url' TEXT, vendor TEXT, designer TEXT, 'designer-url' TEXT, " \ |
| 46 |
"'license-type' TEXT, fsType INTEGER, filesize TEXT, checksum TEXT );\n" |
| 47 |
|
| 48 |
#define CREATE_PANOSE_TABLE "CREATE TABLE IF NOT EXISTS Panose ( " \ |
| 49 |
"uid INTEGER PRIMARY KEY, P0 INTEGER, P1 INTEGER, P2 INTEGER, P3 INTEGER, " \ |
| 50 |
"P4 INTEGER, P5 INTEGER, P6 INTEGER, P7 INTEGER, P8 INTEGER, P9 INTEGER, " \ |
| 51 |
"filepath TEXT, findex INTEGER );\n" |
| 52 |
|
| 53 |
#define CREATE_ORTH_TABLE "CREATE TABLE IF NOT EXISTS Orthography ( " \ |
| 54 |
"uid INTEGER PRIMARY KEY, filepath TEXT, findex INT, support TEXT, sample TEXT );\n" |
| 55 |
|
| 56 |
#define CREATE_FONT_MATCH_INDEX "CREATE INDEX IF NOT EXISTS font_match_idx " \ |
| 57 |
"ON Fonts (filepath, findex, family, description);\n" |
| 58 |
|
| 59 |
#define CREATE_INFO_MATCH_INDEX "CREATE INDEX IF NOT EXISTS info_match_idx " \ |
| 60 |
"ON Metadata (filepath, findex, owner, filetype, vendor, 'license-type');\n" |
| 61 |
|
| 62 |
#define CREATE_PANOSE_MATCH_INDEX "CREATE INDEX IF NOT EXISTS panose_match_idx " \ |
| 63 |
"ON Panose (filepath, findex, P0);\n" |
| 64 |
|
| 65 |
#define DROP_FONT_MATCH_INDEX "DROP INDEX IF EXISTS font_match_idx;\n" |
| 66 |
#define DROP_INFO_MATCH_INDEX "DROP INDEX IF EXISTS info_match_idx;\n" |
| 67 |
#define DROP_PANOSE_MATCH_INDEX "DROP INDEX IF EXISTS panose_match_idx;\n" |
| 68 |
|
| 69 |
#define INSERT_FONT_ROW "INSERT OR REPLACE INTO Fonts VALUES (NULL,?,?,?,?,?,?,?,?,?);" |
| 70 |
#define INSERT_INFO_ROW "INSERT OR REPLACE INTO Metadata VALUES (NULL,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);" |
| 71 |
#define INSERT_PANOSE_ROW "INSERT OR REPLACE INTO Panose VALUES (NULL,?,?,?,?,?,?,?,?,?,?,?,?);" |
| 72 |
#define INSERT_ORTH_ROW "INSERT OR REPLACE INTO Orthography VALUES (NULL, ?, ?, ?, ?);" |
| 73 |
|
| 74 |
#define FONT_PROPERTIES FontProperties |
| 75 |
#define INFO_PROPERTIES InfoProperties |
| 76 |
|
| 77 |
struct _FontManagerDatabase |
| 78 |
{ |
| 79 |
GObject parent_instance; |
| 80 |
|
| 81 |
sqlite3 *db; |
| 82 |
sqlite3_stmt *stmt; |
| 83 |
gboolean in_transaction; |
| 84 |
gchar *file; |
| 85 |
}; |
| 86 |
|
| 87 |
G_DEFINE_TYPE(FontManagerDatabase, font_manager_database, G_TYPE_OBJECT) |
| 88 |
|
| 89 |
static void |
| 90 |
set_error (FontManagerDatabase *self, const gchar *ctx, GError **error) |
| 91 |
{ |
| 92 |
g_return_if_fail(error == NULL || *error == NULL); |
| 93 |
const gchar *msg_format = "Database Error : (%s) [%i] - %s"; |
| 94 |
g_debug(msg_format, ctx, sqlite3_errcode(self->db), sqlite3_errmsg(self->db)); |
| 95 |
g_set_error(error, |
| 96 |
FONT_MANAGER_DATABASE_ERROR, |
| 97 |
(FontManagerDatabaseError) sqlite3_errcode(self->db), |
| 98 |
msg_format, ctx, sqlite3_errcode(self->db), sqlite3_errmsg(self->db)); |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
static gboolean |
| 103 |
sqlite3_open_failed (FontManagerDatabase *self, GError **error) |
| 104 |
{ |
| 105 |
g_return_val_if_fail(self != NULL, TRUE); |
| 106 |
g_return_val_if_fail((error == NULL || *error == NULL), TRUE); |
| 107 |
if (self->db != NULL) |
| 108 |
return FALSE; |
| 109 |
GError *err = NULL; |
| 110 |
font_manager_database_open(self, &err); |
| 111 |
if (err != NULL) { |
| 112 |
g_propagate_error(error, err); |
| 113 |
g_warning("Database Error : Failed to open database."); |
| 114 |
return TRUE; |
| 115 |
} |
| 116 |
return FALSE; |
| 117 |
} |
| 118 |
|
| 119 |
static gboolean |
| 120 |
sqlite3_step_succeeded (FontManagerDatabase *db, int expected_result) |
| 121 |
{ |
| 122 |
int actual_result = sqlite3_step(db->stmt); |
| 123 |
if (actual_result == expected_result) |
| 124 |
return TRUE; |
| 125 |
if (actual_result != SQLITE_OK && actual_result != SQLITE_ROW && actual_result != SQLITE_DONE) |
| 126 |
g_warning("SQLite Result Code %i : %s", sqlite3_errcode(db->db), sqlite3_errmsg(db->db)); |
| 127 |
return FALSE; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* font_manager_database_close: |
| 132 |
* @self: #FontManagerDatabase |
| 133 |
* @error: (nullable): #GError or %NULL to ignore errors |
| 134 |
* |
| 135 |
* Close database. |
| 136 |
* It is not necessary to call this function in normal usage. |
| 137 |
*/ |
| 138 |
void |
| 139 |
font_manager_database_close (FontManagerDatabase *self, GError **error) |
| 140 |
{ |
| 141 |
g_return_if_fail(self != NULL); |
| 142 |
g_return_if_fail(error == NULL || *error == NULL); |
| 143 |
sqlite3_exec(self->db, "PRAGMA optimize;", NULL, NULL, NULL); |
| 144 |
if (self->db && (sqlite3_close(self->db) != SQLITE_OK)) |
| 145 |
set_error(self, "sqlite3_close", error); |
| 146 |
self->db = NULL; |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
static void |
| 151 |
font_manager_database_dispose (GObject *gobject) |
| 152 |
{ |
| 153 |
g_return_if_fail(gobject != NULL); |
| 154 |
FontManagerDatabase *self = FONT_MANAGER_DATABASE(gobject); |
| 155 |
font_manager_database_end_query(self); |
| 156 |
font_manager_database_close(self, NULL); |
| 157 |
g_clear_pointer(&self->file, g_free); |
| 158 |
G_OBJECT_CLASS(font_manager_database_parent_class)->dispose(gobject); |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
static void |
| 163 |
font_manager_database_class_init (FontManagerDatabaseClass *klass) |
| 164 |
{ |
| 165 |
GObjectClass *object_class = G_OBJECT_CLASS(klass); |
| 166 |
object_class->dispose = font_manager_database_dispose; |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
static void |
| 171 |
font_manager_database_init (FontManagerDatabase *self) |
| 172 |
{ |
| 173 |
g_return_if_fail(self != NULL); |
| 174 |
g_autofree gchar *cache_dir = font_manager_get_package_cache_directory(); |
| 175 |
g_autofree gchar *db_file = g_strdup_printf("%s.sqlite", PACKAGE_NAME); |
| 176 |
self->file = g_build_filename(cache_dir, db_file, NULL); |
| 177 |
font_manager_database_open(self, NULL); |
| 178 |
font_manager_database_initialize(self, NULL); |
| 179 |
return; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* font_manager_database_open: |
| 184 |
* @self: #FontManagerDatabase |
| 185 |
* @error: (nullable): #GError or %NULL to ignore errors |
| 186 |
* |
| 187 |
* Open database. |
| 188 |
* |
| 189 |
* Note: It is not necessary to call this function in normal usage. |
| 190 |
* The methods provided by this class will open the database if needed. |
| 191 |
*/ |
| 192 |
void |
| 193 |
font_manager_database_open (FontManagerDatabase *self, GError **error) |
| 194 |
{ |
| 195 |
g_return_if_fail(self != NULL); |
| 196 |
g_return_if_fail(error == NULL || *error == NULL); |
| 197 |
if (self->db != NULL) |
| 198 |
return; |
| 199 |
if (sqlite3_open(self->file, &self->db) != SQLITE_OK) |
| 200 |
set_error(self, "sqlite3_open", error); |
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* font_manager_database_begin_transaction: |
| 206 |
* @self: #FontManagerDatabase |
| 207 |
* @error: (nullable): #GError or %NULL to ignore errors |
| 208 |
* |
| 209 |
* Begin a transaction, this should be paired with |
| 210 |
* #font_manager_database_commit_transaction(). |
| 211 |
*/ |
| 212 |
void |
| 213 |
font_manager_database_begin_transaction (FontManagerDatabase *self, GError **error) |
| 214 |
{ |
| 215 |
g_return_if_fail(self != NULL); |
| 216 |
g_return_if_fail(error == NULL || *error == NULL); |
| 217 |
if (self->in_transaction) |
| 218 |
return; |
| 219 |
if (sqlite3_open_failed(self, error)) |
| 220 |
return; |
| 221 |
if (sqlite3_exec(self->db, "BEGIN;", NULL, NULL, NULL) != SQLITE_OK) |
| 222 |
set_error(self, "sqlite3_exec", error); |
| 223 |
self->in_transaction = TRUE; |
| 224 |
return; |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* font_manager_database_get_cursor: (skip) |
| 229 |
* @self: #FontManagerDatabase |
| 230 |
* |
| 231 |
* Returns: (transfer none) (nullable): #sqlite3_stmt or %NULL |
| 232 |
*/ |
| 233 |
sqlite3_stmt * |
| 234 |
font_manager_database_get_cursor (FontManagerDatabase *self) { |
| 235 |
g_return_val_if_fail(self != NULL, NULL); |
| 236 |
return self->stmt; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* font_manager_database_commit_transaction: |
| 241 |
* @self: #FontManagerDatabase |
| 242 |
* @error: (nullable): #GError or %NULL to ignore errors |
| 243 |
* |
| 244 |
* End a transaction. It is an error to call this function without having |
| 245 |
* previously called #font_manager_database_begin_transaction(). |
| 246 |
*/ |
| 247 |
void |
| 248 |
font_manager_database_commit_transaction (FontManagerDatabase *self, GError **error) |
| 249 |
{ |
| 250 |
g_return_if_fail(self != NULL); |
| 251 |
g_return_if_fail(error == NULL || *error == NULL); |
| 252 |
if (!self->in_transaction) { |
| 253 |
g_set_error(error, FONT_MANAGER_DATABASE_ERROR, FONT_MANAGER_DATABASE_ERROR_MISUSE, |
| 254 |
G_STRLOC" : Not in transaction. Nothing to commit."); |
| 255 |
g_return_if_reached(); |
| 256 |
} |
| 257 |
if (sqlite3_exec(self->db, "COMMIT;", NULL, NULL, NULL) != SQLITE_OK) |
| 258 |
set_error(self, "sqlite3_exec", error); |
| 259 |
self->in_transaction = FALSE; |
| 260 |
return; |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* font_manager_database_execute_query: |
| 265 |
* @self: #FontManagerDatabase |
| 266 |
* @sql: Valid SQL query |
| 267 |
* @error: (nullable): #GError or %NULL to ignore errors |
| 268 |
* |
| 269 |
* Calls to this function must be paired with a call to #font_manager_database_end_query |
| 270 |
*/ |
| 271 |
void |
| 272 |
font_manager_database_execute_query (FontManagerDatabase *self, const gchar *sql, GError **error) |
| 273 |
{ |
| 274 |
g_return_if_fail(self != NULL); |
| 275 |
g_return_if_fail(sql != NULL); |
| 276 |
g_return_if_fail(error == NULL || *error == NULL); |
| 277 |
if (sqlite3_open_failed(self, error)) |
| 278 |
return; |
| 279 |
if (sqlite3_prepare_v2(self->db, sql, -1, &self->stmt, NULL) != SQLITE_OK) |
| 280 |
set_error(self, sql, error); |
| 281 |
return; |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* font_manager_database_end_query: |
| 286 |
* @self: #fontManagerDatabase |
| 287 |
* |
| 288 |
* Finalize the prepared statement created by a previous call to #font_manager_database_execute_query |
| 289 |
*/ |
| 290 |
void |
| 291 |
font_manager_database_end_query (FontManagerDatabase *self) |
| 292 |
{ |
| 293 |
g_return_if_fail(self != NULL); |
| 294 |
g_clear_pointer(&self->stmt, sqlite3_finalize); |
| 295 |
return; |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* font_manager_database_get_version: |
| 300 |
* @self: #FontManagerDatabase |
| 301 |
* @error: (nullable): #GError or %NULL to ignore errors |
| 302 |
* |
| 303 |
* Returns: Database schema version or -1 on error. |
| 304 |
*/ |
| 305 |
gint |
| 306 |
font_manager_database_get_version (FontManagerDatabase *self, GError **error) |
| 307 |
{ |
| 308 |
int result = -1; |
| 309 |
g_return_val_if_fail(self != NULL, result); |
| 310 |
g_return_val_if_fail((error == NULL || *error == NULL), result); |
| 311 |
if (sqlite3_open_failed(self, error)) |
| 312 |
return result; |
| 313 |
font_manager_database_execute_query(self, "PRAGMA user_version", error); |
| 314 |
g_return_val_if_fail(error == NULL || *error == NULL, result); |
| 315 |
if (sqlite3_step(self->stmt) == SQLITE_ROW) |
| 316 |
result = sqlite3_column_int(self->stmt, 0); |
| 317 |
font_manager_database_end_query(self); |
| 318 |
return result; |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* font_manager_database_vacuum: |
| 323 |
* @self: #FontManagerDatabase |
| 324 |
* @error: (nullable): #GError or %NULL to ignore errors |
| 325 |
* |
| 326 |
* Run sqlite3 VACUUM command on currently selected database. |
| 327 |
*/ |
| 328 |
void |
| 329 |
font_manager_database_vacuum (FontManagerDatabase *self, GError **error) |
| 330 |
{ |
| 331 |
g_return_if_fail(self != NULL); |
| 332 |
g_return_if_fail(error == NULL || *error == NULL); |
| 333 |
if (sqlite3_open_failed(self, error)) |
| 334 |
return; |
| 335 |
if (sqlite3_exec(self->db, "VACUUM", NULL, NULL, NULL) != SQLITE_OK) |
| 336 |
set_error(self, "sqlite3_exec", error); |
| 337 |
return; |
| 338 |
} |
| 339 |
|
| 340 |
void |
| 341 |
cache_locale_value (GFile *locale_file, |
| 342 |
const char *current_locale) |
| 343 |
{ |
| 344 |
glong count = g_utf8_strlen(current_locale, -1); |
| 345 |
g_autoptr(GFileOutputStream) stream = NULL; |
| 346 |
stream = g_file_create(locale_file, G_FILE_CREATE_PRIVATE, NULL, NULL); |
| 347 |
g_output_stream_write(G_OUTPUT_STREAM(stream), current_locale, count, NULL, NULL); |
| 348 |
g_output_stream_close(G_OUTPUT_STREAM(stream), NULL, NULL); |
| 349 |
return; |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* font_manager_database_initialize: |
| 354 |
* @self: #FontManagerDatabase instance |
| 355 |
* @error: (nullable): #GError or %NULL to ignore errors |
| 356 |
* |
| 357 |
* Ensures database is at latest schema version. |
| 358 |
* Creates required tables if needed. |
| 359 |
*/ |
| 360 |
void |
| 361 |
font_manager_database_initialize (FontManagerDatabase *self, |
| 362 |
GError **error) |
| 363 |
{ |
| 364 |
g_return_if_fail(FONT_MANAGER_IS_DATABASE(self)); |
| 365 |
g_return_if_fail(error == NULL || *error == NULL); |
| 366 |
|
| 367 |
const char *current_locale = setlocale(LC_ALL, NULL); |
| 368 |
g_autofree gchar *config_dir = font_manager_get_package_cache_directory(); |
| 369 |
g_autofree gchar *locale_stamp = g_build_filename(config_dir, "locale", NULL); |
| 370 |
g_autoptr(GFile) locale_file = g_file_new_for_path(locale_stamp); |
| 371 |
|
| 372 |
if (g_file_query_exists(locale_file, NULL)) { |
| 373 |
g_autofree gchar *stored_locale = NULL; |
| 374 |
g_file_load_contents(locale_file, NULL, &stored_locale, NULL, NULL, NULL); |
| 375 |
if (g_strcmp0(current_locale, stored_locale) != 0) { |
| 376 |
g_debug("Locale change detected, dumping database..."); |
| 377 |
font_manager_database_close(self, error); |
| 378 |
if (g_remove(self->file) < 0) |
| 379 |
g_warning("Failed to remove outdated database file"); |
| 380 |
g_file_delete(locale_file, NULL, NULL); |
| 381 |
cache_locale_value(locale_file, current_locale); |
| 382 |
} |
| 383 |
} else { |
| 384 |
cache_locale_value(locale_file, current_locale); |
| 385 |
} |
| 386 |
|
| 387 |
bool db_exists = font_manager_exists(self->file); |
| 388 |
int CURRENT_VERSION = FONT_MANAGER_CURRENT_DATABASE_VERSION; |
| 389 |
|
| 390 |
if (db_exists && font_manager_database_get_version(self, NULL) == CURRENT_VERSION) { |
| 391 |
g_debug("Database version is current, skipping initialization"); |
| 392 |
font_manager_database_close(self, error); |
| 393 |
return; |
| 394 |
} else if (db_exists) { |
| 395 |
g_debug("Database version is outdated, removing file"); |
| 396 |
font_manager_database_close(self, error); |
| 397 |
if (g_remove(self->file) < 0) |
| 398 |
g_critical("Failed to remove outdated database file : %s", self->file); |
| 399 |
} else { |
| 400 |
g_debug("Database file not found, creating and initializing database"); |
| 401 |
} |
| 402 |
if (self->db == NULL) |
| 403 |
font_manager_database_open(self, NULL); |
| 404 |
sqlite3_exec(self->db, "PRAGMA journal_mode = WAL;", NULL, 0, 0); |
| 405 |
sqlite3_exec(self->db, "PRAGMA synchronous = NORMAL;", NULL, 0, 0); |
| 406 |
sqlite3_exec(self->db, CREATE_FONTS_TABLE, NULL, 0, 0); |
| 407 |
sqlite3_exec(self->db, CREATE_INFO_TABLE, NULL, 0, 0); |
| 408 |
sqlite3_exec(self->db, CREATE_PANOSE_TABLE, NULL, 0, 0); |
| 409 |
sqlite3_exec(self->db, CREATE_ORTH_TABLE, NULL, 0, 0); |
| 410 |
sqlite3_exec(self->db, CREATE_FONT_MATCH_INDEX, NULL, 0, 0); |
| 411 |
sqlite3_exec(self->db, CREATE_INFO_MATCH_INDEX, NULL, 0, 0); |
| 412 |
sqlite3_exec(self->db, CREATE_PANOSE_MATCH_INDEX, NULL, 0, 0); |
| 413 |
g_autofree gchar *sql = g_strdup_printf("PRAGMA user_version = %i", CURRENT_VERSION); |
| 414 |
sqlite3_exec(self->db, sql, NULL, 0, 0); |
| 415 |
return; |
| 416 |
} |
| 417 |
|
| 418 |
/** |
| 419 |
* font_manager_database_get_object: |
| 420 |
* @self: #FontManagerDatabase |
| 421 |
* @sql: SQL query |
| 422 |
* @error: #GError or %NULL to ignore errors |
| 423 |
* |
| 424 |
* Returns: (transfer full) (nullable): |
| 425 |
* #JsonObject representation of first result, |
| 426 |
* %NULL if there were no results or there was an error. |
| 427 |
*/ |
| 428 |
JsonObject * |
| 429 |
font_manager_database_get_object (FontManagerDatabase *self, const gchar *sql, GError **error) |
| 430 |
{ |
| 431 |
g_return_val_if_fail(FONT_MANAGER_IS_DATABASE(self), NULL); |
| 432 |
g_return_val_if_fail(sql != NULL, NULL); |
| 433 |
g_return_val_if_fail((error == NULL || *error == NULL), NULL); |
| 434 |
|
| 435 |
font_manager_database_execute_query(self, sql, error); |
| 436 |
|
| 437 |
if (error != NULL && *error != NULL) |
| 438 |
return NULL; |
| 439 |
|
| 440 |
if (!sqlite3_step_succeeded(self, SQLITE_ROW)) |
| 441 |
return NULL; |
| 442 |
|
| 443 |
JsonObject *obj = json_object_new(); |
| 444 |
|
| 445 |
for (gint i = 0; i < sqlite3_column_count(self->stmt); i++) { |
| 446 |
const gchar *name = sqlite3_column_origin_name(self->stmt, i); |
| 447 |
gint int_column = -1; |
| 448 |
const unsigned char *text_column = NULL; |
| 449 |
switch (sqlite3_column_type(self->stmt, i)) { |
| 450 |
case SQLITE_INTEGER: |
| 451 |
int_column = sqlite3_column_int(self->stmt, i); |
| 452 |
json_object_set_int_member(obj, name, int_column); |
| 453 |
break; |
| 454 |
case SQLITE_TEXT: |
| 455 |
text_column = sqlite3_column_text(self->stmt, i); |
| 456 |
json_object_set_string_member(obj, name, (const gchar *) text_column); |
| 457 |
break; |
| 458 |
case SQLITE_NULL: |
| 459 |
json_object_set_null_member(obj, name); |
| 460 |
break; |
| 461 |
default: |
| 462 |
break; |
| 463 |
} |
| 464 |
} |
| 465 |
font_manager_database_end_query(self); |
| 466 |
if (json_object_get_size(obj) < 1) |
| 467 |
g_clear_pointer(&obj, json_object_unref); |
| 468 |
return obj; |
| 469 |
} |
| 470 |
|
| 471 |
/** |
| 472 |
* font_manager_database_new: |
| 473 |
* |
| 474 |
* Returns: (transfer full): #FontManagerDatabase |
| 475 |
*/ |
| 476 |
FontManagerDatabase * |
| 477 |
font_manager_database_new (void) |
| 478 |
{ |
| 479 |
return g_object_new(FONT_MANAGER_TYPE_DATABASE, NULL); |
| 480 |
} |
| 481 |
|
| 482 |
/* Related functions */ |
| 483 |
|
| 484 |
typedef struct |
| 485 |
{ |
| 486 |
FontManagerDatabase *db; |
| 487 |
JsonArray *available_fonts; |
| 488 |
FontManagerProgressCallback progress; |
| 489 |
} |
| 490 |
DatabaseSyncData; |
| 491 |
|
| 492 |
static DatabaseSyncData * |
| 493 |
sync_data_new (FontManagerDatabase *db, |
| 494 |
JsonArray *available_fonts, |
| 495 |
FontManagerProgressCallback progress) |
| 496 |
{ |
| 497 |
DatabaseSyncData *sync_data = g_new0(DatabaseSyncData, 1); |
| 498 |
sync_data->db = g_object_ref(db); |
| 499 |
sync_data->available_fonts = json_array_ref(available_fonts); |
| 500 |
sync_data->progress = progress; |
| 501 |
return sync_data; |
| 502 |
} |
| 503 |
|
| 504 |
static void |
| 505 |
sync_data_free (DatabaseSyncData *data) |
| 506 |
{ |
| 507 |
g_clear_object(&data->db); |
| 508 |
g_clear_pointer(&data->available_fonts, json_array_unref); |
| 509 |
g_clear_pointer(&data, g_free); |
| 510 |
return; |
| 511 |
} |
| 512 |
|
| 513 |
static void |
| 514 |
bind_from_properties (sqlite3_stmt *stmt, |
| 515 |
JsonObject *json, |
| 516 |
const FontManagerJsonProxyProperty *properties, |
| 517 |
gint n_properties) |
| 518 |
{ |
| 519 |
for (gint i = 0; i < n_properties; i++) { |
| 520 |
const gchar *str = NULL; |
| 521 |
switch (properties[i].type) { |
| 522 |
case G_TYPE_INT64: |
| 523 |
g_assert(json_object_has_member(json, properties[i].name)); |
| 524 |
gint val = json_object_get_int_member(json, properties[i].name); |
| 525 |
g_assert(val >= -1 && sqlite3_bind_int(stmt, i, val) == SQLITE_OK); |
| 526 |
break; |
| 527 |
case G_TYPE_STRING: |
| 528 |
if (g_strcmp0(properties[i].name, "preview-text") == 0) |
| 529 |
break; |
| 530 |
if (json_object_has_member(json, properties[i].name)) |
| 531 |
str = json_object_get_string_member(json, properties[i].name); |
| 532 |
g_assert(sqlite3_bind_text(stmt, i, str, -1, SQLITE_STATIC) == SQLITE_OK); |
| 533 |
break; |
| 534 |
default: |
| 535 |
break; |
| 536 |
} |
| 537 |
} |
| 538 |
return; |
| 539 |
} |
| 540 |
|
| 541 |
static FontManagerStringSet * |
| 542 |
get_known_files (FontManagerDatabase *db) |
| 543 |
{ |
| 544 |
FontManagerStringSet *result = font_manager_string_set_new(); |
| 545 |
g_return_val_if_fail(FONT_MANAGER_IS_DATABASE(db), result); |
| 546 |
const gchar *sql = "SELECT DISTINCT filepath FROM Metadata"; |
| 547 |
g_autoptr(GError) error = NULL; |
| 548 |
font_manager_database_execute_query(db, sql, &error); |
| 549 |
if (error != NULL) { |
| 550 |
g_critical("%s", error->message); |
| 551 |
return result; |
| 552 |
} |
| 553 |
g_autoptr(FontManagerDatabaseIterator) iter = font_manager_database_iterator(db); |
| 554 |
while (font_manager_database_iterator_next(iter)) { |
| 555 |
sqlite3_stmt *stmt = font_manager_database_iterator_get(iter); |
| 556 |
const gchar *val = (const gchar *) sqlite3_column_text(stmt, 0); |
| 557 |
if (val) |
| 558 |
font_manager_string_set_add(result, val); |
| 559 |
} |
| 560 |
font_manager_database_end_query(db); |
| 561 |
return result; |
| 562 |
} |
| 563 |
|
| 564 |
static const gchar *FONT_MANAGER_SKIP_ORTH_SCAN[] = { |
| 565 |
/* Adobe Blank can take several minutes to process due to number of codepoints. */ |
| 566 |
"Adobe Blank", |
| 567 |
NULL |
| 568 |
}; |
| 569 |
|
| 570 |
static void |
| 571 |
update_available_fonts (DatabaseSyncData *data, |
| 572 |
GCancellable *cancellable, |
| 573 |
GError **error) |
| 574 |
{ |
| 575 |
g_return_if_fail(FONT_MANAGER_IS_DATABASE(data->db)); |
| 576 |
g_return_if_fail(error == NULL || *error == NULL); |
| 577 |
|
| 578 |
FontManagerDatabase *db = FONT_MANAGER_DATABASE(data->db); |
| 579 |
|
| 580 |
uint processed = 0; |
| 581 |
uint total = json_array_get_length(data->available_fonts); |
| 582 |
const gchar *message = _("Updating Database…"); |
| 583 |
g_autoptr(FontManagerStringSet) known_files = get_known_files(db); |
| 584 |
FontManagerProgressData *progress = font_manager_progress_data_new(message, processed, total); |
| 585 |
|
| 586 |
font_manager_database_begin_transaction(db, error); |
| 587 |
g_return_if_fail(error == NULL || *error == NULL); |
| 588 |
|
| 589 |
for (uint i = 0; i < total; i++) { |
| 590 |
if (g_cancellable_is_cancelled(cancellable)) |
| 591 |
break; |
| 592 |
/* Stash results periodically so we don't lose everything if closed */ |
| 593 |
if (processed > 0 && processed % 500 == 0) { |
| 594 |
font_manager_database_commit_transaction(db, error); |
| 595 |
g_return_if_fail(error == NULL || *error == NULL); |
| 596 |
font_manager_database_begin_transaction(db, error); |
| 597 |
g_return_if_fail(error == NULL || *error == NULL); |
| 598 |
} |
| 599 |
JsonObject *family = json_array_get_object_element(data->available_fonts, i); |
| 600 |
const gchar *family_name = json_object_get_string_member(family, "family"); |
| 601 |
JsonArray *variations = json_object_get_array_member(family, "variations"); |
| 602 |
uint n_variations = json_array_get_length(variations); |
| 603 |
for (uint v = 0; v < n_variations; v++) { |
| 604 |
// g_autofree gchar *json_data = font_manager_print_json_object(json, FALSE); |
| 605 |
// g_debug("Database.update_available_fonts : processing : %s", json_data); |
| 606 |
JsonObject *face = json_array_get_object_element(variations, v); |
| 607 |
int index = json_object_get_int_member(face, "findex"); |
| 608 |
const gchar *filepath = json_object_get_string_member(face, "filepath"); |
| 609 |
// Font table |
| 610 |
font_manager_database_execute_query(db, INSERT_FONT_ROW, error); |
| 611 |
g_return_if_fail(error == NULL || *error == NULL); |
| 612 |
bind_from_properties(db->stmt, face, FONT_PROPERTIES, G_N_ELEMENTS(FONT_PROPERTIES)); |
| 613 |
g_assert(sqlite3_step_succeeded(db, SQLITE_DONE)); |
| 614 |
g_return_if_fail(error == NULL || *error == NULL); |
| 615 |
font_manager_database_end_query(db); |
| 616 |
if (font_manager_string_set_contains(known_files, filepath)) { |
| 617 |
/* g_debug("Database.update_available_fonts : ignoring known font path : %i : %s", index, filepath); */ |
| 618 |
continue; |
| 619 |
} else { |
| 620 |
g_debug("Database.update_available_fonts : adding new font path : %i : %s", index, filepath); |
| 621 |
// Metadata table |
| 622 |
g_autoptr(JsonObject) _face = font_manager_get_metadata(filepath, index, error); |
| 623 |
if (error != NULL && *error != NULL) { |
| 624 |
GError *err = *error; |
| 625 |
g_critical("Failed to get metadata for %s::%i - %s", filepath, index, err->message); |
| 626 |
g_return_if_fail(error == NULL || *error == NULL); |
| 627 |
} |
| 628 |
font_manager_database_execute_query(db, INSERT_INFO_ROW, error); |
| 629 |
g_return_if_fail(error == NULL || *error == NULL); |
| 630 |
bind_from_properties(db->stmt, _face, INFO_PROPERTIES, G_N_ELEMENTS(INFO_PROPERTIES)); |
| 631 |
g_assert(sqlite3_step_succeeded(db, SQLITE_DONE)); |
| 632 |
font_manager_database_end_query(db); |
| 633 |
// Panose table |
| 634 |
if (json_object_has_member(_face, "panose")) { |
| 635 |
JsonArray *panose = json_object_get_array_member(_face, "panose"); |
| 636 |
if (panose && json_array_get_length(panose) > 0) { |
| 637 |
font_manager_database_execute_query(db, INSERT_PANOSE_ROW, error); |
| 638 |
g_return_if_fail(error == NULL || *error == NULL); |
| 639 |
for (int i = 0; i < 10; i++) { |
| 640 |
int _index = i + 1; |
| 641 |
int val = (int) json_array_get_int_element(panose, i); |
| 642 |
g_assert(sqlite3_bind_int(db->stmt, _index, val) == SQLITE_OK); |
| 643 |
} |
| 644 |
g_assert(sqlite3_bind_text(db->stmt, 11, filepath, -1, SQLITE_STATIC) == SQLITE_OK); |
| 645 |
g_assert(sqlite3_bind_int(db->stmt, 12, index) == SQLITE_OK); |
| 646 |
g_assert(sqlite3_step_succeeded(db, SQLITE_DONE)); |
| 647 |
font_manager_database_end_query(db); |
| 648 |
} |
| 649 |
} |
| 650 |
// Orthogaphy table |
| 651 |
gboolean blank_font = FALSE; |
| 652 |
if (g_strv_contains(FONT_MANAGER_SKIP_ORTH_SCAN, family_name)) |
| 653 |
blank_font = TRUE; |
| 654 |
g_autoptr(JsonObject) orth = font_manager_get_orthography_results(blank_font ? NULL : face); |
| 655 |
g_autofree gchar *json_obj = font_manager_print_json_object(orth, FALSE); |
| 656 |
const gchar *sample = json_object_get_string_member(orth, "sample"); |
| 657 |
font_manager_database_execute_query(db, INSERT_ORTH_ROW, error); |
| 658 |
g_assert(sqlite3_bind_text(db->stmt, 1, filepath, -1, SQLITE_STATIC) == SQLITE_OK); |
| 659 |
g_assert(sqlite3_bind_int(db->stmt, 2, index) == SQLITE_OK); |
| 660 |
g_assert(sqlite3_bind_text(db->stmt, 3, json_obj, -1, SQLITE_STATIC) == SQLITE_OK); |
| 661 |
g_assert(sqlite3_bind_text(db->stmt, 4, sample, -1, SQLITE_STATIC) == SQLITE_OK); |
| 662 |
g_assert(sqlite3_step_succeeded(db, SQLITE_DONE)); |
| 663 |
font_manager_database_end_query(db); |
| 664 |
} |
| 665 |
} |
| 666 |
processed++; |
| 667 |
if (data->progress) { |
| 668 |
g_object_ref(progress); |
| 669 |
g_object_set(progress, "processed", processed, "total", total, NULL); |
| 670 |
g_main_context_invoke_full(g_main_context_get_thread_default(), |
| 671 |
G_PRIORITY_HIGH_IDLE, |
| 672 |
(GSourceFunc) data->progress, |
| 673 |
progress, |
| 674 |
(GDestroyNotify) g_object_unref); |
| 675 |
} |
| 676 |
} |
| 677 |
font_manager_database_commit_transaction(db, error); |
| 678 |
g_object_unref(progress); |
| 679 |
return; |
| 680 |
} |
| 681 |
|
| 682 |
gboolean |
| 683 |
font_manager_update_database_sync (DatabaseSyncData *data, |
| 684 |
GCancellable *cancellable, |
| 685 |
GError **error) |
| 686 |
{ |
| 687 |
g_return_val_if_fail(FONT_MANAGER_IS_DATABASE(data->db), FALSE); |
| 688 |
g_return_val_if_fail((error == NULL || *error == NULL), FALSE); |
| 689 |
|
| 690 |
if (g_cancellable_is_cancelled(cancellable)) |
| 691 |
return FALSE; |
| 692 |
|
| 693 |
if (data->db->db == NULL) |
| 694 |
font_manager_database_open(data->db, NULL); |
| 695 |
|
| 696 |
sqlite3_exec(data->db->db, "DELETE FROM Fonts;", NULL, 0, 0); |
| 697 |
sqlite3_exec(data->db->db, CREATE_FONTS_TABLE, NULL, 0, 0); |
| 698 |
sqlite3_exec(data->db->db, CREATE_FONT_MATCH_INDEX, NULL, 0, 0); |
| 699 |
update_available_fonts(data, cancellable, error); |
| 700 |
g_return_val_if_fail(error == NULL || *error == NULL, FALSE); |
| 701 |
return TRUE; |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
static void |
| 706 |
sync_database_thread (GTask *task, |
| 707 |
G_GNUC_UNUSED gpointer source, |
| 708 |
gpointer task_data, |
| 709 |
GCancellable *cancellable) |
| 710 |
{ |
| 711 |
GError *error = NULL; |
| 712 |
gboolean result = FALSE; |
| 713 |
DatabaseSyncData *data = task_data; |
| 714 |
|
| 715 |
result = font_manager_update_database_sync(data, cancellable, &error); |
| 716 |
|
| 717 |
if (error == NULL) |
| 718 |
g_task_return_boolean(task, result); |
| 719 |
else |
| 720 |
g_task_return_error(task, error); |
| 721 |
} |
| 722 |
|
| 723 |
/** |
| 724 |
* font_manager_update_database: |
| 725 |
* @db: #FontManagerDatabase instance |
| 726 |
* @available_fonts: #JsonArray returned by #font_manager_sort_json_listing |
| 727 |
* @progress: (scope call) (nullable): #FontManagerProgressCallback |
| 728 |
* @cancellable: (nullable): #GCancellable or %NULL |
| 729 |
* @callback: (nullable) (scope async): #GAsyncReadyCallback or %NULL |
| 730 |
* @user_data: (nullable): user data passed to callback or %NULL |
| 731 |
* |
| 732 |
* Update application database as needed. |
| 733 |
*/ |
| 734 |
void |
| 735 |
font_manager_update_database (FontManagerDatabase *db, |
| 736 |
JsonArray *available_fonts, |
| 737 |
FontManagerProgressCallback progress, |
| 738 |
GCancellable *cancellable, |
| 739 |
GAsyncReadyCallback callback, |
| 740 |
gpointer user_data) |
| 741 |
{ |
| 742 |
g_return_if_fail(cancellable == NULL || G_IS_CANCELLABLE (cancellable)); |
| 743 |
DatabaseSyncData *sync_data = sync_data_new(db, available_fonts, progress); |
| 744 |
g_autoptr(GTask) task = g_task_new(NULL, cancellable, callback, user_data); |
| 745 |
g_task_set_priority(task, G_PRIORITY_DEFAULT); |
| 746 |
g_task_set_return_on_cancel(task, FALSE); |
| 747 |
g_task_set_task_data(task, (gpointer) sync_data, (GDestroyNotify) sync_data_free); |
| 748 |
g_task_run_in_thread(task, sync_database_thread); |
| 749 |
return; |
| 750 |
} |
| 751 |
|
| 752 |
/** |
| 753 |
* font_manager_update_database_finish: |
| 754 |
* @result: #GAsyncResult |
| 755 |
* @error: (nullable): #GError or %NULL to ignore errors |
| 756 |
* |
| 757 |
* Returns: %TRUE on success |
| 758 |
*/ |
| 759 |
gboolean |
| 760 |
font_manager_update_database_finish (GAsyncResult *result, GError **error) |
| 761 |
{ |
| 762 |
g_return_val_if_fail(g_task_is_valid(result, NULL), FALSE); |
| 763 |
g_return_val_if_fail(error == NULL || *error == NULL, FALSE); |
| 764 |
return g_task_propagate_boolean(G_TASK(result), error); |
| 765 |
} |
| 766 |
|
| 767 |
/** |
| 768 |
* font_manager_get_matching_families_and_fonts: |
| 769 |
* @db: #FontManagerDatabase |
| 770 |
* @families: #FontManagerStringSet |
| 771 |
* @fonts: #FontManagerStringSet |
| 772 |
* @sql: SQL query to execute |
| 773 |
* @error: #GError or %NULL to ignore errors |
| 774 |
* |
| 775 |
* Query MUST return two result columns. The first containing the family name |
| 776 |
* and the second containing the font description. |
| 777 |
*/ |
| 778 |
void |
| 779 |
font_manager_get_matching_families_and_fonts (FontManagerDatabase *db, |
| 780 |
FontManagerStringSet *families, |
| 781 |
FontManagerStringSet *fonts, |
| 782 |
const gchar *sql, |
| 783 |
GError **error) |
| 784 |
{ |
| 785 |
g_return_if_fail(FONT_MANAGER_IS_DATABASE(db)); |
| 786 |
g_return_if_fail(FONT_MANAGER_IS_STRING_SET(families)); |
| 787 |
g_return_if_fail(FONT_MANAGER_IS_STRING_SET(fonts)); |
| 788 |
g_return_if_fail(sql != NULL); |
| 789 |
g_return_if_fail(error == NULL || *error == NULL); |
| 790 |
font_manager_database_execute_query(db, sql, error); |
| 791 |
g_return_if_fail(error == NULL || *error == NULL); |
| 792 |
g_autoptr(FontManagerDatabaseIterator) iter = font_manager_database_iterator(db); |
| 793 |
while (font_manager_database_iterator_next(iter)) { |
| 794 |
sqlite3_stmt *stmt = font_manager_database_iterator_get(iter); |
| 795 |
g_warn_if_fail(sqlite3_column_count(stmt) >= 2); |
| 796 |
if (!(sqlite3_column_count(stmt) >= 2)) |
| 797 |
continue; |
| 798 |
const gchar *family = (const gchar *) sqlite3_column_text(stmt, 0); |
| 799 |
const gchar *font = (const gchar *) sqlite3_column_text(stmt, 1); |
| 800 |
if (family == NULL || font == NULL) |
| 801 |
continue; |
| 802 |
font_manager_string_set_add(families, family); |
| 803 |
font_manager_string_set_add(fonts, font); |
| 804 |
} |
| 805 |
font_manager_database_end_query(db); |
| 806 |
return; |
| 807 |
} |
| 808 |
|