/ desktop / config / library.cpp
library.cpp
 1  #include "library.hpp"
 2  
 3  #include <QDebug>
 4  #include <string>
 5  
 6  KotoLibraryConfig::KotoLibraryConfig(std::string name, fs::path path, KotoLibraryType type) {
 7    this->i_name = name;
 8    this->i_path = path;
 9    this->i_type = type;
10    qDebug() << "Library: " << this->i_name.c_str() << " at " << this->i_path.c_str();
11  }
12  
13  KotoLibraryConfig::~KotoLibraryConfig() {}
14  
15  KotoLibraryConfig::KotoLibraryConfig(const toml::value& v) {
16    this->i_name = toml::find<std::string>(v, "name");
17    this->i_path = toml::find<std::string>(v, "path");
18    this->i_type = libraryTypeFromString(toml::find<std::string>(v, "type"));
19  }
20  
21  std::string KotoLibraryConfig::getName() {
22    return this->i_name;
23  }
24  
25  fs::path KotoLibraryConfig::getPath() {
26    return this->i_path;
27  }
28  
29  KotoLibraryType KotoLibraryConfig::getType() {
30    return this->i_type;
31  }
32  
33  toml::ordered_value KotoLibraryConfig::serialize() {
34    toml::ordered_value library_table(toml::ordered_table {});
35    library_table["name"] = this->i_name;
36    library_table["path"] = this->i_path.string();
37    auto stringifiedType  = libraryTypeToString(this->i_type);
38    library_table["type"] = stringifiedType;
39    return library_table;
40  }
41  
42  std::string libraryTypeToString(KotoLibraryType type) {
43    switch (type) {
44      case KotoLibraryType::Audiobooks:
45        return std::string {"audiobooks"};
46      case KotoLibraryType::Music:
47        return std::string {"music"};
48      case KotoLibraryType::Podcasts:
49        return std::string {"podcasts"};
50      default:
51        return std::string {"unknown"};
52    }
53  }
54  
55  KotoLibraryType libraryTypeFromString(const std::string& type) {
56    if (type == "audiobooks") return KotoLibraryType::Audiobooks;
57    if (type == "music") return KotoLibraryType::Music;
58    if (type == "podcasts") return KotoLibraryType::Podcasts;
59    throw std::invalid_argument("Unknown KotoLibraryType: " + type);
60  }