Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

117 json errors at startup #123

Merged
merged 3 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions src/Models/StationStore.vala
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,30 @@
* SPDX-FileCopyrightText: 2020-2022 Louis Brauer <[email protected]>
*/

// StationStore can store and retrieve a collection of stations
// in a JSON file
/**
StationStore

Store and retrieve a collection of stations in a JSON file, i.e. favorites

Uses libgee for data structures.
*/

using Gee;

namespace Tuner.Model {

public class StationStore : Object {
private ArrayList<Station> _store;
private File _data_file;
private File _favorites_file;

public StationStore (string data_path) {
public StationStore (string favorites_path) {
Object ();

_store = new ArrayList<Station> ();
_data_file = File.new_for_path (data_path);
_favorites_file = File.new_for_path (favorites_path);
ensure ();
load ();
debug (@"store initialized in path $data_path");
debug (@"store initialized in path $favorites_path");
}

public void add (Station station) {
Expand All @@ -43,7 +48,7 @@ public class StationStore : Object {
// Non-racy approach is to try to create the file first
// and ignore errors if it already exists
try {
var df = _data_file.create (FileCreateFlags.PRIVATE);
var df = _favorites_file.create (FileCreateFlags.PRIVATE);
df.close ();
debug (@"store created");
} catch (Error e) {
Expand All @@ -56,16 +61,19 @@ public class StationStore : Object {
Json.Parser parser = new Json.Parser ();

try {
var stream = _data_file.read ();
var stream = _favorites_file.read ();
parser.load_from_stream (stream);
stream.close ();
} catch (Error e) {
warning (@"store: unable to load data, does it exist? $(e.message)");
}

Json.Node node = parser.get_root ();
Json.Array array = node.get_array ();
array.foreach_element ((a, i, elem) => {
Json.Node? node = parser.get_root ();

if ( node == null ) return; // No favorites store

Json.Array array = node.get_array (); // Json-CRITICAL **: 21:02:51.821: json_node_get_array: assertion 'JSON_NODE_IS_VALID (node)' failed
array.foreach_element ((a, i, elem) => { // json_array_foreach_element: assertion 'array != NULL' failed
Station station = Json.gobject_deserialize (typeof (Station), elem) as Station;
// TODO This should probably not be here but in
// DirectoryController
Expand All @@ -88,8 +96,8 @@ public class StationStore : Object {
var data = serialize ();

try {
_data_file.delete ();
var stream = _data_file.create (
_favorites_file.delete ();
var stream = _favorites_file.create (
FileCreateFlags.REPLACE_DESTINATION | FileCreateFlags.PRIVATE
);
var s = new DataOutputStream (stream);
Expand Down Expand Up @@ -130,4 +138,4 @@ public class StationStore : Object {
}
}

}
}
4 changes: 2 additions & 2 deletions src/Widgets/Window.vala
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ public class Tuner.Window : Gtk.ApplicationWindow {
var stack = new Gtk.Stack ();
stack.transition_type = Gtk.StackTransitionType.CROSSFADE;

var data_file = Path.build_filename (Application.instance.data_dir, "favorites.json");
var store = new Model.StationStore (data_file);
var favorites_file = Path.build_filename (Application.instance.data_dir, "favorites.json");
var store = new Model.StationStore (favorites_file);
_directory = new DirectoryController (store);

var primary_box = new Gtk.Paned (Gtk.Orientation.HORIZONTAL);
Expand Down
Loading