Newer
Older
#include "qtlib_dir.h"
ConfigHandler::ConfigHandler(QObject *parent)
: QObject(parent)
{
appConfig_ = qtlib::Config(":/configs");
importAppConfigApplication();
importAppConfigInstallTypes();
usrConfig_ = qtlib::Config(qtlib::Dir::genericConfigPath() + "/" + getAppConfigApplication()["id"].toString());
}
QJsonObject ConfigHandler::getAppConfigApplication() const
QJsonObject ConfigHandler::getAppConfigInstallTypes() const
QJsonObject ConfigHandler::getUsrConfigApplication() const
bool ConfigHandler::setUsrConfigApplication(const QJsonObject &object) const
/* object format
{
"update_checked_at": 1483658977219
}
*/
QJsonObject ConfigHandler::getUsrConfigProviders() const
bool ConfigHandler::setUsrConfigProviders(const QJsonObject &object) const
QJsonObject ConfigHandler::getUsrConfigCategories() const
bool ConfigHandler::setUsrConfigCategories(const QJsonObject &object) const
QJsonObject ConfigHandler::getUsrConfigInstalledItems() const
bool ConfigHandler::setUsrConfigInstalledItems(const QJsonObject &object) const
{
return usrConfig_.set("installed_items", object);
}
QJsonObject ConfigHandler::getUsrConfigUpdateAvailable() const
{
return usrConfig_.get("update_available");
}
bool ConfigHandler::setUsrConfigUpdateAvailable(const QJsonObject &object) const
{
return usrConfig_.set("update_available", object);
}
bool ConfigHandler::setUsrConfigProvidersProvider(const QString &providerKey, const QJsonObject &object) const
{
/* object format
{
"id": "example",
"location": "https://example.com/ocs/v1/",
"name": "Example",
"icon": "https://example.com/icon.png",
"termsofuse": "https://example.com/termsofuse",
"register": "https://example.com/register",
"_providerfile": "https://example.com/ocs/providers.xml"
}
*/
providers[providerKey] = object;
return setUsrConfigProviders(providers);
}
bool ConfigHandler::removeUsrConfigProvidersProvider(const QString &providerKey) const
providers.remove(providerKey);
return setUsrConfigProviders(providers);
}
bool ConfigHandler::setUsrConfigCategoriesProvider(const QString &providerKey, const QJsonObject &object) const
{
/* object format
{
"1": {
"id": "1",
"name": "Desktop Icons",
"install_type": "icons"
},
"2": {
"id": "2",
"name": "Desktop Themes",
"install_type": "themes"
}
}
*/
categories[providerKey] = object;
return setUsrConfigCategories(categories);
}
bool ConfigHandler::removeUsrConfigCategoriesProvider(const QString &providerKey) const
categories.remove(providerKey);
return setUsrConfigCategories(categories);
}
bool ConfigHandler::setUsrConfigCategoriesInstallType(const QString &providerKey, const QString &categoryKey, const QString &installType) const
QJsonObject providerCategories;
if (categories.contains(providerKey)) {
providerCategories = categories[providerKey].toObject();
}
QJsonObject providerCategory;
if (providerCategories.contains(categoryKey)) {
providerCategory = providerCategories[categoryKey].toObject();
}
providerCategory["install_type"] = installType;
providerCategories[categoryKey] = providerCategory;
categories[providerKey] = providerCategories;
return setUsrConfigCategories(categories);
}
bool ConfigHandler::setUsrConfigInstalledItemsItem(const QString &itemKey, const QJsonObject &object) const
{
/* object format
{
"url": "http://example.com/downloads/123-1.tar.gz",
"filename": "123-1.tar.gz",
"install_type": "icons",
"provider": "http://example.com/ocs/v1/",
"content_id": "123",
"files": [
"iconset-light",
"iconset-dark"
],
"installed_at": 1483658977219
}
*/
installedItems[itemKey] = object;
return setUsrConfigInstalledItems(installedItems);
}
bool ConfigHandler::removeUsrConfigInstalledItemsItem(const QString &itemKey) const
installedItems.remove(itemKey);
return setUsrConfigInstalledItems(installedItems);
}
bool ConfigHandler::setUsrConfigUpdateAvailableFile(const QString &fileKey, const QJsonObject &object) const
{
/* object format
{
"path": "/home/user/.local/bin/example.AppImage",
"filename": "example.AppImage",
"installed_item": "http://example.com/downloads/example.AppImage",
"update_method": "appimageupdate"
}
*/
auto updateAvailable = getUsrConfigUpdateAvailable();
updateAvailable[fileKey] = object;
return setUsrConfigUpdateAvailable(updateAvailable);
}
bool ConfigHandler::removeUsrConfigUpdateAvailableFile(const QString &fileKey) const
{
auto updateAvailable = getUsrConfigUpdateAvailable();
updateAvailable.remove(fileKey);
return setUsrConfigUpdateAvailable(updateAvailable);
}
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
void ConfigHandler::importAppConfigApplication()
{
appConfigApplication_ = appConfig_.get("application");
}
void ConfigHandler::importAppConfigInstallTypes()
{
auto installTypes = appConfig_.get("install_types");
for (const auto &key : installTypes.keys()) {
auto installtype = installTypes[key].toObject();
installtype["destination"] = convertPathString(installtype["destination"].toString());
installtype["generic_destination"] = convertPathString(installtype["generic_destination"].toString());
installTypes[key] = installtype;
}
auto installTypesAlias = appConfig_.get("install_types_alias");
for (const auto &key : installTypesAlias.keys()) {
auto installTypeAlias = installTypesAlias[key].toObject();
auto baseKey = installTypeAlias["base"].toString();
if (installTypes.contains(baseKey)) {
auto installType = installTypes[baseKey].toObject();
installType["base"] = baseKey;
installType["name"] = installTypeAlias["name"].toString();
installTypes[key] = installType;
}
}
appConfigInstallTypes_ = installTypes;
}
QString ConfigHandler::convertPathString(const QString &path) const
if (newPath.contains("$HOME")) {
newPath.replace("$HOME", qtlib::Dir::homePath());
}
else if (newPath.contains("$XDG_DOCUMENTS_DIR")) {
newPath.replace("$XDG_DOCUMENTS_DIR", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation));
}
else if (newPath.contains("$XDG_DOWNLOAD_DIR")) {
newPath.replace("$XDG_DOWNLOAD_DIR", QStandardPaths::writableLocation(QStandardPaths::DownloadLocation));
}
else if (newPath.contains("$XDG_PICTURES_DIR")) {
newPath.replace("$XDG_PICTURES_DIR", QStandardPaths::writableLocation(QStandardPaths::PicturesLocation));
}
else if (newPath.contains("$XDG_MUSIC_DIR")) {
newPath.replace("$XDG_MUSIC_DIR", QStandardPaths::writableLocation(QStandardPaths::MusicLocation));
}
else if (newPath.contains("$XDG_VIDEOS_DIR")) {
newPath.replace("$XDG_VIDEOS_DIR", QStandardPaths::writableLocation(QStandardPaths::MoviesLocation));
}
else if (newPath.contains("$XDG_DATA_HOME")) {
newPath.replace("$XDG_DATA_HOME", qtlib::Dir::genericDataPath());
}
else if (newPath.contains("$KDEHOME")) {
newPath.replace("$KDEHOME", qtlib::Dir::kdehomePath());
}
else if (newPath.contains("$APP_DATA")) {
newPath.replace("$APP_DATA", qtlib::Dir::genericDataPath() + "/" + getAppConfigApplication()["id"].toString());
}
return newPath;
}