feat: initial database creation, loading

fixes for cartographer and automatically add track to album when adding to artist
This commit is contained in:
Joshua Strobl 2024-10-05 00:03:50 +03:00
parent 72bbcaba9e
commit 62c99ee67c
18 changed files with 515 additions and 108 deletions

View file

@ -2,18 +2,60 @@
#include <QDir>
#include <QStandardPaths>
#include <QTextStream>
#include <filesystem>
namespace fs = std::filesystem;
KotoConfig::KotoConfig() {
// Define our application's config location
auto configDir = QDir(QStandardPaths::writableLocation(QStandardPaths::StandardLocation::AppConfigLocation));
auto configDirPath = configDir.absolutePath();
auto configDir = QDir(QStandardPaths::writableLocation(QStandardPaths::StandardLocation::AppConfigLocation));
auto configDirPath = configDir.absolutePath();
this->i_configDirPath = configDirPath;
this->i_libraries = QList<KotoLibraryConfig*>();
fs::path filePath {};
auto configPathStd = configDirPath.toStdString();
filePath /= configPathStd;
filePath /= "config.toml";
this->i_configPath = QString {filePath.c_str()};
if (QFileInfo::exists(i_configPath)) {
this->parseConfigFile(filePath);
} else {
this->bootstrap();
}
}
KotoConfig& KotoConfig::instance() {
static KotoConfig _instance;
return _instance;
}
void KotoConfig::bootstrap() {
this->i_uiPreferences = new KotoUiPreferences();
auto musicDir = QDir(QStandardPaths::writableLocation(QStandardPaths::StandardLocation::MusicLocation));
auto musicLibrary = new KotoLibraryConfig("Music", musicDir.absolutePath().toStdString(), KotoLibraryType::Music);
this->i_libraries.append(musicLibrary);
this->save();
}
QString KotoConfig::getConfigDirPath() {
return QString {this->i_configDirPath};
}
KotoUiPreferences* KotoConfig::getUiPreferences() {
return this->i_uiPreferences;
}
QList<KotoLibraryConfig*> KotoConfig::getLibraries() {
return this->i_libraries;
}
void KotoConfig::parseConfigFile(std::string filePath) {
auto data = toml::parse(filePath);
std::optional<toml::value> ui_prefs;
@ -22,22 +64,35 @@ KotoConfig::KotoConfig() {
if (ui_prefs_at.is_table()) ui_prefs = ui_prefs_at.as_table();
}
auto prefs = KotoUiPreferences(ui_prefs);
this->i_uiPreferences = &prefs;
auto prefs = new KotoUiPreferences(ui_prefs);
this->i_uiPreferences = prefs;
this->i_libraries = {};
for (const auto& lib_value : toml::find<std::vector<toml::value>>(data, "libraries")) {
auto lib = KotoLibraryConfig(lib_value);
this->i_libraries.push_back(lib);
auto lib = new KotoLibraryConfig(lib_value);
this->i_libraries.append(lib);
}
}
KotoConfig::~KotoConfig() {}
void KotoConfig::save() {
toml::ordered_value config_table(toml::ordered_table {});
config_table["preferences.ui"] = this->i_uiPreferences->serialize();
KotoUiPreferences* KotoConfig::getUiPreferences() {
return this->i_uiPreferences;
}
toml::ordered_value libraries_array(toml::ordered_array {});
for (auto lib : this->i_libraries) {
auto lib_table = lib->serialize();
libraries_array.push_back(lib_table);
}
config_table["libraries"] = libraries_array;
std::vector<KotoLibraryConfig> KotoConfig::getLibraries() {
return this->i_libraries;
auto configContent = toml::format(config_table);
auto config_dir = QDir {this->i_configDirPath};
if (!config_dir.exists()) config_dir.mkpath(".");
auto config_file = QFile {this->i_configPath};
auto out = QTextStream {&config_file};
if (config_file.open(QIODevice::WriteOnly | QIODevice::Text)) {
out << configContent.c_str();
config_file.close();
}
}

View file

@ -1,18 +1,28 @@
#pragma once
#include <vector>
#include <QList>
#include <QString>
#include "library.hpp"
#include "ui_prefs.hpp"
class KotoConfig {
public:
KotoConfig();
~KotoConfig();
std::vector<KotoLibraryConfig> getLibraries();
KotoUiPreferences * getUiPreferences();
public:
KotoConfig();
static KotoConfig& instance();
static KotoConfig* create() { return &instance(); }
void save();
private:
std::vector<KotoLibraryConfig> i_libraries;
KotoUiPreferences * i_uiPreferences;
QString getConfigDirPath();
QList<KotoLibraryConfig*> getLibraries();
KotoUiPreferences* getUiPreferences();
private:
void bootstrap();
void parseConfigFile(std::string filePath);
QString i_configDirPath;
QString i_configPath;
QList<KotoLibraryConfig*> i_libraries;
KotoUiPreferences* i_uiPreferences;
};

View file

@ -3,9 +3,10 @@
#include <QDebug>
#include <string>
KotoLibraryConfig::KotoLibraryConfig(std::string name, fs::path path) {
KotoLibraryConfig::KotoLibraryConfig(std::string name, fs::path path, KotoLibraryType type) {
this->i_name = name;
this->i_path = path;
this->i_type = type;
qDebug() << "Library: " << this->i_name.c_str() << " at " << this->i_path.c_str();
}
@ -14,6 +15,7 @@ KotoLibraryConfig::~KotoLibraryConfig() {}
KotoLibraryConfig::KotoLibraryConfig(const toml::value& v) {
this->i_name = toml::find<std::string>(v, "name");
this->i_path = toml::find<std::string>(v, "path");
this->i_type = libraryTypeFromString(toml::find<std::string>(v, "type"));
}
std::string KotoLibraryConfig::getName() {
@ -23,3 +25,36 @@ std::string KotoLibraryConfig::getName() {
fs::path KotoLibraryConfig::getPath() {
return this->i_path;
}
KotoLibraryType KotoLibraryConfig::getType() {
return this->i_type;
}
toml::ordered_value KotoLibraryConfig::serialize() {
toml::ordered_value library_table(toml::ordered_table {});
library_table["name"] = this->i_name;
library_table["path"] = this->i_path.string();
auto stringifiedType = libraryTypeToString(this->i_type);
library_table["type"] = stringifiedType;
return library_table;
}
std::string libraryTypeToString(KotoLibraryType type) {
switch (type) {
case KotoLibraryType::Audiobooks:
return std::string {"audiobooks"};
case KotoLibraryType::Music:
return std::string {"music"};
case KotoLibraryType::Podcasts:
return std::string {"podcasts"};
default:
return std::string {"unknown"};
}
}
KotoLibraryType libraryTypeFromString(const std::string& type) {
if (type == "audiobooks") return KotoLibraryType::Audiobooks;
if (type == "music") return KotoLibraryType::Music;
if (type == "podcasts") return KotoLibraryType::Podcasts;
throw std::invalid_argument("Unknown KotoLibraryType: " + type);
}

View file

@ -6,15 +6,27 @@
namespace fs = std::filesystem;
enum class KotoLibraryType {
Audiobooks,
Music,
Podcasts,
};
KotoLibraryType libraryTypeFromString(const std::string& type);
std::string libraryTypeToString(KotoLibraryType type);
class KotoLibraryConfig {
public:
KotoLibraryConfig(std::string name, fs::path path);
KotoLibraryConfig(std::string name, fs::path path, KotoLibraryType type);
KotoLibraryConfig(const toml::value& v);
~KotoLibraryConfig();
std::string getName();
fs::path getPath();
std::string getName();
fs::path getPath();
KotoLibraryType getType();
toml::ordered_value serialize();
private:
std::string i_name;
fs::path i_path;
std::string i_name;
fs::path i_path;
KotoLibraryType i_type;
};

View file

@ -1,23 +1,24 @@
#include "ui_prefs.hpp"
KotoUiPreferences::KotoUiPreferences(std::optional<toml::value> v) {
this->i_albumInfoShowDescription = true;
this->i_albumInfoShowGenre = true;
this->i_albumInfoShowNarrator = true;
this->i_albumInfoShowYear = true;
this->i_lastUsedVolume = 0.5;
KotoUiPreferences::KotoUiPreferences()
: i_albumInfoShowDescription(true), i_albumInfoShowGenre(true), i_albumInfoShowNarrator(true), i_albumInfoShowYear(true), i_lastUsedVolume(0.5) {}
KotoUiPreferences::KotoUiPreferences(std::optional<toml::value> v) {
// No UI prefs provided
if (!v.has_value()) return;
toml::value& uiPrefs = v.value();
this->i_albumInfoShowDescription = toml::find_or<bool>(uiPrefs, "album_info_show_description", false);
this->i_albumInfoShowGenre = toml::find_or<bool>(uiPrefs, "album_info_show_genre", false);
this->i_albumInfoShowNarrator = toml::find_or<bool>(uiPrefs, "album_info_show_narrator", false);
this->i_albumInfoShowYear = toml::find_or<bool>(uiPrefs, "album_info_show_year", false);
this->i_lastUsedVolume = toml::find_or<float>(uiPrefs, "last_used_volume", 0.5);
}
toml::value& uiPrefs = v.value();
auto showDescription = toml::find_or<bool>(uiPrefs, "album_info_show_description", false);
auto showGenre = toml::find_or<bool>(uiPrefs, "album_info_show_genre", false);
auto showNarrator = toml::find_or<bool>(uiPrefs, "album_info_show_narrator", false);
auto showYear = toml::find_or<bool>(uiPrefs, "album_info_show_year", false);
auto lastUsedVolume = toml::find_or<float>(uiPrefs, "last_used_volume", 0.5);
KotoUiPreferences::~KotoUiPreferences() {}
this->setAlbumInfoShowDescription(showDescription);
this->setAlbumInfoShowGenre(showGenre);
this->setAlbumInfoShowNarrator(showNarrator);
this->setAlbumInfoShowYear(showYear);
this->setLastUsedVolume(lastUsedVolume);
}
bool KotoUiPreferences::getAlbumInfoShowDescription() {
return this->i_albumInfoShowDescription;
@ -38,3 +39,33 @@ bool KotoUiPreferences::getAlbumInfoShowYear() {
float KotoUiPreferences::getLastUsedVolume() {
return this->i_lastUsedVolume;
}
toml::ordered_value KotoUiPreferences::serialize() {
toml::ordered_value ui_prefs_table(toml::ordered_table {});
ui_prefs_table["album_info_show_description"] = this->i_albumInfoShowDescription;
ui_prefs_table["album_info_show_genre"] = this->i_albumInfoShowGenre;
ui_prefs_table["album_info_show_narrator"] = this->i_albumInfoShowNarrator;
ui_prefs_table["album_info_show_year"] = this->i_albumInfoShowYear;
ui_prefs_table["last_used_volume"] = this->i_lastUsedVolume;
return ui_prefs_table;
}
void KotoUiPreferences::setAlbumInfoShowDescription(bool show) {
this->i_albumInfoShowDescription = show;
}
void KotoUiPreferences::setAlbumInfoShowGenre(bool show) {
this->i_albumInfoShowGenre = show;
}
void KotoUiPreferences::setAlbumInfoShowNarrator(bool show) {
this->i_albumInfoShowNarrator = show;
}
void KotoUiPreferences::setAlbumInfoShowYear(bool show) {
this->i_albumInfoShowYear = show;
}
void KotoUiPreferences::setLastUsedVolume(float volume) {
this->i_lastUsedVolume = volume;
}

View file

@ -8,6 +8,7 @@
class KotoUiPreferences {
public:
KotoUiPreferences();
KotoUiPreferences(std::optional<toml::value> v);
~KotoUiPreferences();
@ -17,6 +18,8 @@ class KotoUiPreferences {
bool getAlbumInfoShowYear();
float getLastUsedVolume();
toml::ordered_value serialize();
void setAlbumInfoShowDescription(bool show);
void setAlbumInfoShowGenre(bool show);
void setAlbumInfoShowNarrator(bool show);