File indexing completed on 2025-03-09 04:52:07

0001 /*
0002     Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com>
0003 
0004     This library is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU Library General Public License
0015     along with this library; see the file COPYING.LIB.  If not, write to the
0016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0017     02110-1301, USA.
0018 */
0019 #include "settings.h"
0020 
0021 #include <QDebug>
0022 #include <QStandardPaths>
0023 #include <QMetaObject>
0024 #include <QMetaProperty>
0025 #include <QFile>
0026 
0027 using namespace Kube;
0028 
0029 Settings::Settings(QObject *parent)
0030     : QObject(parent),
0031     mLoaded(false)
0032 {
0033 
0034 }
0035 
0036 Settings::Settings(const QByteArray &id, QObject *parent)
0037     : QObject(parent),
0038     mIdentifier(id),
0039     mLoaded(false)
0040 {
0041     load();
0042 }
0043 
0044 Settings::Settings(const Settings &other)
0045     : QObject(other.parent()),
0046     mIdentifier(other.mIdentifier),
0047     mLoaded(false)
0048 {
0049     load();
0050 }
0051 
0052 Settings::~Settings()
0053 {
0054 }
0055 
0056 QSharedPointer<QSettings> Settings::getSettings()
0057 {
0058     return QSharedPointer<QSettings>::create(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QString("/kube/%1.ini").arg(QString::fromLatin1(mIdentifier)), QSettings::IniFormat);
0059 }
0060 
0061 void Settings::save()
0062 {
0063     qWarning() << "Saving" << mIdentifier;
0064     auto settings = getSettings();
0065 
0066     for (const auto &p : dynamicPropertyNames()) {
0067         qWarning() << "setting " << p << property(p);
0068         if (p == "identifier" || p == "name") {
0069             continue;
0070         }
0071         settings->setValue(p, property(p));
0072     }
0073     for (int i = metaObject()->propertyOffset(); i < metaObject()->propertyCount(); i++) {
0074         const auto p = metaObject()->property(i).name();
0075         if (p == QByteArray("identifier") || p == QByteArray("name")) {
0076             continue;
0077         }
0078         qWarning() << "setting " << p << property(p);
0079         settings->setValue(p, property(p));
0080     }
0081     settings->sync();
0082 }
0083 
0084 void Settings::remove()
0085 {
0086     QFile::remove(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QString("/kube/%1.ini").arg(QString::fromLatin1(mIdentifier)));
0087 }
0088 
0089 void Settings::load()
0090 {
0091     if (mLoaded || mIdentifier.isEmpty() || mIdentifier.endsWith(".")) {
0092         return;
0093     }
0094     mLoaded = true;
0095     for (int i = metaObject()->propertyOffset(); i < metaObject()->propertyCount(); i++) {
0096         auto p = metaObject()->property(i).name();
0097         setProperty(p, QVariant());
0098     }
0099     auto settings = getSettings();
0100     for (const auto &p : settings->allKeys()) {
0101         qWarning() << "loading " << p << settings->value(p);
0102         setProperty(p.toLatin1(), settings->value(p));
0103     }
0104 }
0105 
0106 void Settings::setIdentifier(const QByteArray &id)
0107 {
0108     mIdentifier = id;
0109     load();
0110 }
0111 
0112 QByteArray Settings::identifier() const
0113 {
0114     return mIdentifier;
0115 }
0116 
0117 ApplicationContext::ApplicationContext()
0118     : Settings("applicationcontext")
0119 {
0120 
0121 }
0122 
0123 Account ApplicationContext::currentAccount() const
0124 {
0125     return Account(property("currentAccountId").toByteArray());
0126 }
0127 
0128 Account::Account(const QByteArray &identifier)
0129     : Settings("account." + identifier)
0130 {
0131 
0132 }
0133 
0134 Identity Account::primaryIdentity() const
0135 {
0136     return Identity(property("primaryIdentityId").toByteArray());
0137 }
0138 
0139 QByteArray Account::type() const
0140 {
0141     return property("type").toByteArray();
0142 }
0143 
0144 Identity::Identity(const QByteArray &identifier)
0145     : Settings("identity." + identifier)
0146 {
0147 
0148 }
0149 
0150 Transport Identity::transport() const
0151 {
0152     return Transport(property("transportId").toByteArray());
0153 }
0154 
0155 Transport::Transport(const QByteArray &identifier)
0156     : Settings("transport." + identifier)
0157 {
0158 
0159 }
0160 
0161 QByteArray Transport::username() const
0162 {
0163     return property("username").toByteArray();
0164 }
0165 
0166 QByteArray Transport::password() const
0167 {
0168     return property("password").toByteArray();
0169 }
0170 
0171 QByteArray Transport::server() const
0172 {
0173     return property("server").toByteArray();
0174 }