From 4d3ddd1c417b39f720d9ca6fbbd1a389c12daccc Mon Sep 17 00:00:00 2001 From: Erica Z Date: Fri, 11 Oct 2024 09:20:42 +0000 Subject: [PATCH] more progress --- src/application.vala | 18 +------- src/setup.blp | 1 - src/setup.vala | 106 +++++++++++++++++++++++++------------------ 3 files changed, 62 insertions(+), 63 deletions(-) diff --git a/src/application.vala b/src/application.vala index 4ac4be5..1d23983 100644 --- a/src/application.vala +++ b/src/application.vala @@ -18,7 +18,6 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -Sqlite.Database config_db; Gst.Element playbin; Wavelet.Subsonic public_api; @@ -28,21 +27,6 @@ public class Wavelet.Application : Adw.Application { application_id: "eu.callcc.Wavelet", flags: ApplicationFlags.DEFAULT_FLAGS ); - - var app_config_dir = Path.build_filename (Environment.get_user_config_dir (), "wavelet"); - try { - File.new_build_filename (app_config_dir).make_directory_with_parents (); - } catch (Error e) { - // just ignore if the directory already existed - } - - int ec = Sqlite.Database.open (Path.build_filename (app_config_dir, "config.db"), out config_db); - assert (ec == Sqlite.OK); - - ec = config_db.exec (""" - CREATE TABLE IF NOT EXISTS Setup (key UNIQUE, value); - """, null, null); - assert (ec == Sqlite.OK); } construct { @@ -152,7 +136,7 @@ public class Wavelet.Application : Adw.Application { win.song = song; }); }); - win.setup.load (config_db); + win.setup.load (); } private void on_about_action () { diff --git a/src/setup.blp b/src/setup.blp index c271b56..8aca20b 100644 --- a/src/setup.blp +++ b/src/setup.blp @@ -63,4 +63,3 @@ template $WaveletSetup: Adw.NavigationPage { } } } - diff --git a/src/setup.vala b/src/setup.vala index 138dd0d..8f1c665 100644 --- a/src/setup.vala +++ b/src/setup.vala @@ -36,6 +36,34 @@ public class Wavelet.Setup : Adw.NavigationPage { public signal void connected (Wavelet.Subsonic api); + private Sqlite.Database db; + private Sqlite.Statement db_get; + private Sqlite.Statement db_set; + + construct { + var app_config_dir = Path.build_filename (Environment.get_user_config_dir (), "wavelet"); + + try { + File.new_build_filename (app_config_dir).make_directory_with_parents (); + } catch (Error e) { + // just ignore if the directory already existed + } + + int ec = Sqlite.Database.open (Path.build_filename (app_config_dir, "config.db"), out this.db); + assert (ec == Sqlite.OK); + + ec = this.db.exec (""" + CREATE TABLE IF NOT EXISTS Setup (key UNIQUE, value); + """, null, null); + assert (ec == Sqlite.OK); + + ec = this.db.prepare_v2 ("""SELECT value FROM Setup WHERE key IS ?1""", -1, out this.db_get); + assert (ec == Sqlite.OK); + + ec = this.db.prepare_v2 ("""INSERT OR REPLACE INTO Setup VALUES (?1, ?2)""", -1, out this.db_set); + assert (ec == Sqlite.OK); + } + [GtkCallback] private void on_authn_changed () { this.authn_can_validate = true; } @@ -76,44 +104,38 @@ public class Wavelet.Setup : Adw.NavigationPage { }); } - public void load (Sqlite.Database db) { - Sqlite.Statement stmt; - int rc; - - rc = db.prepare_v2 ("""SELECT value FROM Setup WHERE key IS ?1""", -1, out stmt); - assert (rc == Sqlite.OK); - - stmt.bind_text (1, "server_url"); - if (stmt.step () == Sqlite.ROW) { - this.server_url = stmt.column_text (0); + public void load () { + this.db_get.bind_text (1, "server_url"); + if (this.db_get.step () == Sqlite.ROW) { + this.server_url = this.db_get.column_text (0); } else { this.server_url = ""; } - assert (stmt.reset () == Sqlite.OK); + assert (this.db_get.reset () == Sqlite.OK); - stmt.bind_text (1, "username"); - if (stmt.step () == Sqlite.ROW) { - this.username = stmt.column_text (0); + this.db_get.bind_text (1, "username"); + if (this.db_get.step () == Sqlite.ROW) { + this.username = this.db_get.column_text (0); } else { this.username = ""; } - assert (stmt.reset () == Sqlite.OK); + assert (this.db_get.reset () == Sqlite.OK); - stmt.bind_text (1, "token"); - if (stmt.step () == Sqlite.ROW) { - this.token = stmt.column_text (0); + this.db_get.bind_text (1, "token"); + if (this.db_get.step () == Sqlite.ROW) { + this.token = this.db_get.column_text (0); } else { this.token = ""; } - assert (stmt.reset () == Sqlite.OK); + assert (this.db_get.reset () == Sqlite.OK); - stmt.bind_text (1, "salt"); - if (stmt.step () == Sqlite.ROW) { - this.salt = stmt.column_text (0); + this.db_get.bind_text (1, "salt"); + if (this.db_get.step () == Sqlite.ROW) { + this.salt = this.db_get.column_text (0); } else { this.salt = ""; } - assert (stmt.reset () == Sqlite.OK); + assert (this.db_get.reset () == Sqlite.OK); this.password = ""; @@ -140,31 +162,25 @@ public class Wavelet.Setup : Adw.NavigationPage { } public void save () { - Sqlite.Statement stmt; - int rc; + this.db_set.bind_text (1, "server_url"); + this.db_set.bind_text (2, this.server_url); + assert (this.db_set.step () == Sqlite.DONE); + assert (this.db_set.reset () == Sqlite.OK); - rc = config_db.prepare_v2 ("""INSERT OR REPLACE INTO Setup VALUES (?1, ?2)""", -1, out stmt); - assert (rc == Sqlite.OK); + this.db_set.bind_text (1, "username"); + this.db_set.bind_text (2, this.username); + assert (this.db_set.step () == Sqlite.DONE); + assert (this.db_set.reset () == Sqlite.OK); - stmt.bind_text (1, "server_url"); - stmt.bind_text (2, this.server_url); - assert (stmt.step () == Sqlite.DONE); - assert (stmt.reset () == Sqlite.OK); + this.db_set.bind_text (1, "token"); + this.db_set.bind_text (2, this.token); + assert (this.db_set.step () == Sqlite.DONE); + assert (this.db_set.reset () == Sqlite.OK); - stmt.bind_text (1, "username"); - stmt.bind_text (2, this.username); - assert (stmt.step () == Sqlite.DONE); - assert (stmt.reset () == Sqlite.OK); - - stmt.bind_text (1, "token"); - stmt.bind_text (2, this.token); - assert (stmt.step () == Sqlite.DONE); - assert (stmt.reset () == Sqlite.OK); - - stmt.bind_text (1, "salt"); - stmt.bind_text (2, this.salt); - assert (stmt.step () == Sqlite.DONE); - assert (stmt.reset () == Sqlite.OK); + this.db_set.bind_text (1, "salt"); + this.db_set.bind_text (2, this.salt); + assert (this.db_set.step () == Sqlite.DONE); + assert (this.db_set.reset () == Sqlite.OK); this.password = ""; }