File indexing completed on 2024-04-28 03:43:09
0001 /* 0002 SPDX-FileCopyrightText: 2022 Jasem Mutlaq <mutlaqja@ikarustech.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "profilesettings.h" 0008 #include <kstars_debug.h> 0009 0010 #include "kstars.h" 0011 #include "kstarsdata.h" 0012 #include "ksuserdb.h" 0013 0014 #include <QJsonDocument> 0015 0016 namespace Ekos 0017 { 0018 0019 ProfileSettings *ProfileSettings::m_Instance = nullptr; 0020 0021 //////////////////////////////////////////////////////////////////////////// 0022 /// 0023 //////////////////////////////////////////////////////////////////////////// 0024 ProfileSettings *ProfileSettings::Instance() 0025 { 0026 if (m_Instance == nullptr) 0027 m_Instance = new ProfileSettings(KStars::Instance()); 0028 0029 return m_Instance; 0030 } 0031 0032 //////////////////////////////////////////////////////////////////////////// 0033 /// 0034 //////////////////////////////////////////////////////////////////////////// 0035 void ProfileSettings::release() 0036 { 0037 delete (m_Instance); 0038 m_Instance = nullptr; 0039 } 0040 0041 //////////////////////////////////////////////////////////////////////////// 0042 /// 0043 //////////////////////////////////////////////////////////////////////////// 0044 ProfileSettings::ProfileSettings(QObject *parent) : QObject(parent) 0045 { 0046 } 0047 0048 //////////////////////////////////////////////////////////////////////////// 0049 /// 0050 //////////////////////////////////////////////////////////////////////////// 0051 void ProfileSettings::setProfile(const QSharedPointer<ProfileInfo> &profile) 0052 { 0053 m_Profile = profile; 0054 // If not in database yet, create an empty entry. 0055 if (KStars::Instance()->data()->userdb()->GetProfileSettings(m_Profile->id, m_Settings) == false) 0056 { 0057 initSettings(); 0058 KStars::Instance()->data()->userdb()->GetProfileSettings(m_Profile->id, m_Settings); 0059 } 0060 } 0061 0062 //////////////////////////////////////////////////////////////////////////// 0063 /// 0064 //////////////////////////////////////////////////////////////////////////// 0065 void ProfileSettings::initSettings() 0066 { 0067 auto object = QJsonObject::fromVariantMap(QVariantMap()); 0068 KStars::Instance()->data()->userdb()->AddProfileSettings(m_Profile->id, 0069 QJsonDocument(object).toJson(QJsonDocument::Compact)); 0070 } 0071 0072 //////////////////////////////////////////////////////////////////////////// 0073 /// 0074 //////////////////////////////////////////////////////////////////////////// 0075 void ProfileSettings::setSettings(const QVariantMap &settings) 0076 { 0077 m_Settings = settings; 0078 auto object = QJsonObject::fromVariantMap(m_Settings); 0079 KStars::Instance()->data()->userdb()->UpdateProfileSettings(m_Profile->id, 0080 QJsonDocument(object).toJson(QJsonDocument::Compact)); 0081 } 0082 0083 //////////////////////////////////////////////////////////////////////////// 0084 /// 0085 //////////////////////////////////////////////////////////////////////////// 0086 QVariant ProfileSettings::getOneSetting(Settings id) 0087 { 0088 return m_Settings[QString::number(id)]; 0089 } 0090 0091 //////////////////////////////////////////////////////////////////////////// 0092 /// 0093 //////////////////////////////////////////////////////////////////////////// 0094 void ProfileSettings::setOneSetting(Settings id, const QVariant &value) 0095 { 0096 m_Settings[QString::number(id)] = value; 0097 auto object = QJsonObject::fromVariantMap(m_Settings); 0098 KStars::Instance()->data()->userdb()->UpdateProfileSettings(m_Profile->id, 0099 QJsonDocument(object).toJson(QJsonDocument::Compact)); 0100 } 0101 }