File indexing completed on 2025-01-05 04:54:55

0001 /*
0002     Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsystems.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 "accountfactory.h"
0020 
0021 #include <QDebug>
0022 #include <QtQml>
0023 #include <QFileInfo>
0024 #include <QJsonDocument>
0025 #include <QFile>
0026 
0027 #include <sink/store.h>
0028 #include <sink/log.h>
0029 
0030 AccountFactory::AccountFactory(QObject *parent)
0031     : QObject(parent)
0032 {
0033 }
0034 
0035 void AccountFactory::setAccountId(const QString &accountId)
0036 {
0037     mAccountId = accountId;
0038     if (!accountId.isEmpty()) {
0039         Sink::Store::fetchOne<Sink::ApplicationDomain::SinkAccount>(Sink::Query().filter(accountId.toUtf8()))
0040             .then([this](const Sink::ApplicationDomain::SinkAccount &account) {
0041                 mAccountType = account.getProperty("type").toByteArray();
0042                 loadPackage();
0043             }).onError([=](const KAsync::Error &error) {
0044                 SinkError() << "Failed to load the account: " << accountId << error;
0045             }).exec();
0046     }
0047 }
0048 
0049 void AccountFactory::setAccountType(const QString &type)
0050 {
0051     mAccountType = type.toLatin1();
0052     loadPackage();
0053 }
0054 
0055 void AccountFactory::loadPackage()
0056 {
0057     auto engine = QtQml::qmlEngine(this);
0058     Q_ASSERT(engine);
0059     const QString pluginPath = [&] {
0060         for (const auto &p : engine->importPathList()) {
0061             const auto path = p + QString::fromLatin1("/org/kube/accounts/") + mAccountType;
0062             if (QFileInfo::exists(path)) {
0063                 return path;
0064             }
0065         }
0066         return QString{};
0067     }();
0068     mUiPath.clear();
0069     mLoginUi.clear();
0070     mAccountName.clear();
0071     mRequiresKeyring = false;
0072     if (pluginPath.isEmpty()) {
0073         SinkWarning() << "Failed to load account package: " << "org.kube.accounts." + mAccountType;
0074     } else {
0075         mUiPath = QUrl::fromLocalFile(pluginPath + "/AccountSettings.qml");
0076         mLoginUi = QUrl::fromLocalFile(pluginPath + "/Login.qml");
0077         mAccountName = mAccountType;
0078         if (QFileInfo::exists(pluginPath + "/metadata.json")) {
0079             QFile file{pluginPath + "/metadata.json"};
0080             file.open(QIODevice::ReadOnly);
0081             auto json = QJsonDocument::fromJson(file.readAll());
0082             mRequiresKeyring = json.object().value("RequiresKeyring").toBool(true);
0083             mAccountName = json.object().value("Name").toString();
0084         }
0085     }
0086     emit accountLoaded();
0087 }