mirror of
https://gitlab.com/liyaowhen/youtubecollector-git.git
synced 2026-10-01 01:12:59 +00:00
made steps to shift from gresource to json, the code for json interop working
This commit is contained in:
+210
-11
@@ -1,22 +1,221 @@
|
|||||||
using Json;
|
/*
|
||||||
|
example Config file structure :
|
||||||
|
{
|
||||||
|
"playlists": [
|
||||||
|
"playlist1": {
|
||||||
|
"properties": {
|
||||||
|
"type": 3, // use PlaylistType enum for video-only, audio-only, or an assortment
|
||||||
|
"playlist_picture": "amazing.png", // store all the playlist_pictures in a given directory
|
||||||
|
}
|
||||||
|
"items": [
|
||||||
|
"video1": {
|
||||||
|
"type": 1, // use enum to write in whether it is audio or video
|
||||||
|
"file": "video1.mp4",
|
||||||
|
"picture": "video1.png" // store the pictures of playlist items in a good directory
|
||||||
|
}
|
||||||
|
"audio2": {
|
||||||
|
"type": 2,
|
||||||
|
"file": "audio2.mp3",
|
||||||
|
"picture": "audio2.png"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
namespace Song {
|
namespace Song {
|
||||||
|
|
||||||
public class JsonConfig : GLib.Object {
|
public enum PlaylistType {
|
||||||
|
VIDEO_ONLY,
|
||||||
|
AUDIO_ONLY,
|
||||||
|
VIDEO_AND_AUDIO_COLLECTION,
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum ItemType {
|
||||||
|
VIDEO,
|
||||||
|
AUDIO,
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Config : GLib.Object {
|
||||||
|
|
||||||
private Settings settings = new Settings("com.liyaowhen.Song");
|
private static Config? instance = null;
|
||||||
|
private static string config_file_path;
|
||||||
|
private Json.Object? json_config;
|
||||||
|
|
||||||
public class JsonConfig () {
|
public List<PlaylistObject> playlists;
|
||||||
var json_text = settings.get_string("songs");
|
public signal void config_changed();
|
||||||
|
|
||||||
var parser = new Json.Parser();
|
public static Config get_instance() {
|
||||||
try {
|
if (instance == null) {
|
||||||
parser.load_from_data (json_text, json_text.length);
|
instance = new Config();
|
||||||
} catch (GLib.Error e) {
|
|
||||||
print(e.message);
|
|
||||||
}
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Config() {
|
||||||
|
playlists = new List<PlaylistObject>();
|
||||||
|
load();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async void load() {
|
||||||
|
print("load");
|
||||||
|
File config_file = File.new_for_path(config_file_path);
|
||||||
|
if (config_file.query_exists(null)) {
|
||||||
|
try {
|
||||||
|
DataInputStream dis = new DataInputStream(config_file.read(null));
|
||||||
|
|
||||||
|
size_t length;
|
||||||
|
string? content = dis.read_until("", out length, null);
|
||||||
|
if (content != null) {
|
||||||
|
Json.Parser parser = new Json.Parser();
|
||||||
|
|
||||||
|
try {
|
||||||
|
parser.load_from_file(config_file.get_path());
|
||||||
|
} catch (GLib.Error e) {
|
||||||
|
print(e.message);
|
||||||
|
}
|
||||||
|
print("\n" + config_file.get_path() + "\n");
|
||||||
|
|
||||||
|
Json.Node root = parser.get_root();
|
||||||
|
json_config = root.get_object();
|
||||||
|
|
||||||
|
print(json_config.get_size().to_string());
|
||||||
|
|
||||||
|
// Parse settings from JSON
|
||||||
|
if (json_config.has_member("playlists")) {
|
||||||
|
Json.Object _playlists = json_config.get_object_member("playlists");
|
||||||
|
_playlists.foreach_member((object) => {
|
||||||
|
PlaylistObject _playList = new PlaylistObject();
|
||||||
|
|
||||||
|
_playList.name = object.get_string_member("name");
|
||||||
|
// TODO: ADD PROPERTIES _playList.properties = whatever
|
||||||
|
object.get_object_member("items").foreach_member((item) => {
|
||||||
|
PlaylistItem _item = new PlaylistItem();
|
||||||
|
_item.item_type = item.get_int_member("item_type");
|
||||||
|
_item.name = item.get_string_member("name");
|
||||||
|
_item.file = item.get_string_member("file");
|
||||||
|
|
||||||
|
if (item.has_member("picture")) {
|
||||||
|
_item.picture = item.get_string_member("picture");
|
||||||
|
}
|
||||||
|
if (item.has_member("source")) {
|
||||||
|
_item.picture = item.get_string_member("source");
|
||||||
|
}
|
||||||
|
_playList.items.append(_item);
|
||||||
|
});
|
||||||
|
playlists.append(_playList);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} catch (Error e) {
|
||||||
|
print("Error reading configuration file: %s\n", e.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save configuration to file
|
||||||
|
public async void save() {
|
||||||
|
print("save");
|
||||||
|
try {
|
||||||
|
Json.Builder builder = new Json.Builder();
|
||||||
|
|
||||||
|
// initialize the root "{}"
|
||||||
|
builder.begin_object();
|
||||||
|
|
||||||
|
// playlists
|
||||||
|
builder.set_member_name("playlists");
|
||||||
|
builder.begin_object();
|
||||||
|
|
||||||
|
foreach (PlaylistObject item in playlists) {
|
||||||
|
builder.set_member_name(item.name);
|
||||||
|
builder.begin_object();
|
||||||
|
|
||||||
|
// name
|
||||||
|
builder.set_member_name("name");
|
||||||
|
builder.add_string_value(item.name);
|
||||||
|
|
||||||
|
// properties
|
||||||
|
builder.set_member_name("properties");
|
||||||
|
builder.begin_object();
|
||||||
|
builder.end_object();
|
||||||
|
|
||||||
|
// items
|
||||||
|
builder.set_member_name("items");
|
||||||
|
builder.begin_object();
|
||||||
|
|
||||||
|
// foreach item
|
||||||
|
item.items.foreach((e) => {
|
||||||
|
builder.set_member_name(e.name);
|
||||||
|
builder.begin_object();
|
||||||
|
|
||||||
|
builder.set_member_name("name");
|
||||||
|
builder.add_string_value("name");
|
||||||
|
|
||||||
|
builder.set_member_name("item_type");
|
||||||
|
builder.add_int_value(e.item_type);
|
||||||
|
|
||||||
|
builder.set_member_name("file");
|
||||||
|
builder.add_string_value(e.file);
|
||||||
|
|
||||||
|
if (e.source != null) {
|
||||||
|
builder.set_member_name("source");
|
||||||
|
builder.add_string_value(e.source);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (e.picture != null) {
|
||||||
|
builder.set_member_name("picture");
|
||||||
|
builder.add_string_value(e.picture);
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.end_object();
|
||||||
|
});
|
||||||
|
|
||||||
|
// items end
|
||||||
|
builder.end_object();
|
||||||
|
|
||||||
|
builder.end_object();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
builder.end_object();
|
||||||
|
|
||||||
|
builder.end_object();
|
||||||
|
|
||||||
|
Json.Generator generator = new Json.Generator();
|
||||||
|
generator.set_root(builder.get_root());
|
||||||
|
generator.to_file(config_file_path);
|
||||||
|
print("Configuration saved to %s\n", config_file_path);
|
||||||
|
} catch (Error e) {
|
||||||
|
print("Error saving configuration file: %s\n", e.message);
|
||||||
|
}
|
||||||
|
config_changed();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void init(string app_name) {
|
||||||
|
string config_dir = Environment.get_user_config_dir();
|
||||||
|
string app_config_dir = Path.build_filename(config_dir, app_name);
|
||||||
|
config_file_path = Path.build_filename(app_config_dir, "config.json");
|
||||||
|
|
||||||
|
File app_dir = File.new_for_path(app_config_dir);
|
||||||
|
if (!app_dir.query_exists(null)) {
|
||||||
|
print("directory never existed");
|
||||||
|
try {
|
||||||
|
app_dir.make_directory_with_parents(null);
|
||||||
|
print("Created directory: %s\n", app_config_dir);
|
||||||
|
} catch (Error e) {
|
||||||
|
print("Error creating directory: %s\n", e.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
File config_file = File.new_for_path(config_file_path);
|
||||||
|
if (!config_file.query_exists(null)) {
|
||||||
|
print("file never existed");
|
||||||
|
Config.get_instance().save(); // Create the file with default values
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -20,6 +20,17 @@
|
|||||||
|
|
||||||
int main (string[] args) {
|
int main (string[] args) {
|
||||||
Gst.init(ref args);
|
Gst.init(ref args);
|
||||||
|
|
||||||
|
Song.Config.init("com.liyaowhen.Song");
|
||||||
|
var config = Song.Config.get_instance ();
|
||||||
|
|
||||||
|
Song.PlaylistObject playlist = new Song.PlaylistObject();
|
||||||
|
playlist.name = "QQ";
|
||||||
|
playlist.add_item(Song.PlaylistItem.load_item(Song.ItemType.AUDIO, "aaa", "eee"));
|
||||||
|
|
||||||
|
config.playlists.append(playlist);
|
||||||
|
config.save();
|
||||||
|
|
||||||
var app = new Song.Application ();
|
var app = new Song.Application ();
|
||||||
return app.run (args);
|
return app.run (args);
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-2
@@ -9,14 +9,15 @@ song_sources = [
|
|||||||
'widgets/playlist.vala',
|
'widgets/playlist.vala',
|
||||||
'controllers/song_controller.vala',
|
'controllers/song_controller.vala',
|
||||||
'widgets/music_list_row.vala',
|
'widgets/music_list_row.vala',
|
||||||
'widgets/song_controls.vala'
|
'widgets/song_controls.vala',
|
||||||
|
'playlist_object.vala'
|
||||||
]
|
]
|
||||||
|
|
||||||
song_deps = [
|
song_deps = [
|
||||||
dependency('gtk4'),
|
dependency('gtk4'),
|
||||||
dependency('libadwaita-1', version: '>= 1.4'),
|
dependency('libadwaita-1', version: '>= 1.4'),
|
||||||
dependency('vte-2.91-gtk4'),
|
dependency('vte-2.91-gtk4'),
|
||||||
dependency('jsonrpc-glib-1.0'),
|
dependency('json-glib-1.0'),
|
||||||
dependency('gstreamer-1.0'),
|
dependency('gstreamer-1.0'),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
namespace Song {
|
||||||
|
|
||||||
|
public class PlaylistObject : Object {
|
||||||
|
|
||||||
|
public string name;
|
||||||
|
public List<PlaylistItem> items; //videos or songs
|
||||||
|
|
||||||
|
public void add_item(PlaylistItem item) {
|
||||||
|
items.append(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class PlaylistItem : Object {
|
||||||
|
public ItemType item_type;
|
||||||
|
public string name;
|
||||||
|
public string? source = null; // null when the source is local
|
||||||
|
public string file;
|
||||||
|
public string? picture = null; // null when the item has no picture
|
||||||
|
|
||||||
|
public static PlaylistItem load_item(ItemType item_type, string file, string picture) {
|
||||||
|
PlaylistItem item = new PlaylistItem();
|
||||||
|
item.item_type = item_type;
|
||||||
|
item.file = file;
|
||||||
|
item.picture = picture;
|
||||||
|
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user