MauiKit Accounts

mauiaccounts.cpp
1/*
2 * <one line to give the program's name and a brief idea of what it does.>
3 * Copyright (C) 2019 camilo <chiguitar@unal.edu.co>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "mauiaccounts.h"
20#include "accountsdb.h"
21
22MauiAccounts *MauiAccounts::m_instance = nullptr;
23
24MauiAccounts::MauiAccounts()
25 : MauiList(nullptr)
26 , db(new AccountsDB(nullptr))
27{
28 this->setAccounts();
29}
30
31MauiAccounts::~MauiAccounts()
32{
33 qDebug() << "DELETING MAUI ACCOUNTS INSTANCE";
34 this->db->deleteLater();
35 this->db = nullptr;
36}
37
38const FMH::MODEL_LIST &MauiAccounts::items() const
39{
40 return this->m_data;
41}
42
43void MauiAccounts::setAccounts()
44{
45 Q_EMIT this->preListChanged();
46 this->m_data = this->getCloudAccounts();
47 qDebug() << "ACCOUNTS LIST" << this->m_data;
48
49 Q_EMIT this->countChanged();
50 Q_EMIT this->postListChanged();
51}
52
54{
55 auto accounts = this->get("select * from cloud");
57 for (const auto &account : qAsConst(accounts)) {
58 auto map = account.toMap();
59 /*{FMH::MODEL_KEY::PATH, FMH::PATHTYPE_URI[FMH::PATHTYPE_KEY::CLOUD_PATH] + map[FMH::MODEL_NAME[FMH::MODEL_KEY::USER]].toString()},*/
60 /* {FMH::MODEL_KEY::TYPE, FMH::PATHTYPE_LABEL[FMH::PATHTYPE_KEY::CLOUD_PATH]}*/
61 res << FMH::MODEL {
62 {FMH::MODEL_KEY::ICON, "folder-cloud"},
63 {FMH::MODEL_KEY::LABEL, map[FMH::MODEL_NAME[FMH::MODEL_KEY::USER]].toString()},
64 {FMH::MODEL_KEY::USER, map[FMH::MODEL_NAME[FMH::MODEL_KEY::USER]].toString()},
65 {FMH::MODEL_KEY::SERVER, map[FMH::MODEL_NAME[FMH::MODEL_KEY::SERVER]].toString()},
66 {FMH::MODEL_KEY::PASSWORD, map[FMH::MODEL_NAME[FMH::MODEL_KEY::PASSWORD]].toString()}};
67 }
68 return res;
69}
70
71bool MauiAccounts::addCloudAccount(const QString &server, const QString &user, const QString &password)
72{
73 const QVariantMap account = {{FMH::MODEL_NAME[FMH::MODEL_KEY::SERVER], server}, {FMH::MODEL_NAME[FMH::MODEL_KEY::USER], user}, {FMH::MODEL_NAME[FMH::MODEL_KEY::PASSWORD], password}};
74
75 if (this->db->insert("cloud", account)) {
76 Q_EMIT this->accountAdded(account);
77 return true;
78 }
79
80 return false;
81}
82
83bool MauiAccounts::removeCloudAccount(const QString &server, const QString &user)
84{
85 FMH::MODEL account = {
86 {FMH::MODEL_KEY::SERVER, server},
87 {FMH::MODEL_KEY::USER, user},
88 };
89
90 if (this->db->remove("cloud", account)) {
91 Q_EMIT this->accountRemoved(FMH::toMap(account));
92 return true;
93 }
94
95 return false;
96}
97
98QVariantList MauiAccounts::get(const QString &queryTxt)
99{
100 QVariantList mapList;
101
102 auto query = this->db->getQuery(queryTxt);
103
104 if (query.exec()) {
105 const auto keys = FMH::MODEL_NAME.keys();
106
107 while (query.next()) {
108 QVariantMap data;
109 for (auto key : keys)
110 if (query.record().indexOf(FMH::MODEL_NAME[key]) > -1)
111 data[FMH::MODEL_NAME[key]] = query.value(FMH::MODEL_NAME[key]).toString();
112
113 mapList << data;
114 }
115
116 } else
117 qDebug() << query.lastError() << query.lastQuery();
118
119 return mapList;
120}
121
123{
124 return this->m_currentAccountIndex;
125}
126
128{
129 return this->m_currentAccount;
130}
131
132void MauiAccounts::registerAccount(const QVariantMap &account)
133{
134 // register the account to the backend needed
135 auto model = FMH::toModel(account);
136
137 if (this->addCloudAccount(model[FMH::MODEL_KEY::SERVER], model[FMH::MODEL_KEY::USER], model[FMH::MODEL_KEY::PASSWORD])) {
138 this->setAccounts();
139 }
140}
141
143{
144 if (index >= this->m_data.size() || index < 0)
145 return;
146
147 if (index == this->m_currentAccountIndex)
148 return;
149
150 // make sure the account exists
151 this->m_currentAccountIndex = index;
152 this->m_currentAccount = FMH::toMap(this->m_data.at(m_currentAccountIndex));
153
154 Q_EMIT this->currentAccountChanged(this->m_currentAccount);
155 Q_EMIT this->currentAccountIndexChanged(this->m_currentAccountIndex);
156}
157
159{
160 QVariantList res;
161
162 const auto data = this->getCloudAccounts();
163 for (const auto &item : data)
164 res << FMH::toMap(item);
165
166 return res;
167}
168
170{
171 this->setAccounts();
172}
173
174void MauiAccounts::removeAccount(const int &index)
175{
176 if (index >= this->m_data.size() || index < 0)
177 return;
178
179 if (this->removeCloudAccount(this->m_data.at(index)[FMH::MODEL_KEY::SERVER], this->m_data.at(index)[FMH::MODEL_KEY::USER])) {
180 this->refresh();
181 }
182}
183
185{
186 if (index >= this->m_data.size() || index < 0)
187 return;
188
189 if (this->removeCloudAccount(this->m_data.at(index)[FMH::MODEL_KEY::SERVER], this->m_data.at(index)[FMH::MODEL_KEY::USER])) {
190 this->refresh();
191 }
192
193 // FM_STATIC::removeDir(FM::resolveUserCloudCachePath(this->m_data.at(index)[FMH::MODEL_KEY::SERVER], this->m_data.at(index)[FMH::MODEL_KEY::USER]));
194}
MauiAccounts class provides an interface to access the system registered accounts,...
QVariantList getCloudAccountsList()
getCloudAccountsList
void refresh()
refresh
void accountRemoved(QVariantMap account)
accountRemoved
FMH::MODEL_LIST getCloudAccounts()
getCloudAccounts
int getCurrentAccountIndex() const
getCurrentAccountIndex
QVariantMap get(const int &index) const
get
QVariantMap getCurrentAccount() const
getCurrentAccount
void removeAccount(const int &index)
removeAccount
void setCurrentAccountIndex(const int &index)
setCurrentAccountIndex
void accountAdded(QVariantMap account)
accountAdded
void currentAccountIndexChanged(int index)
currentAccountIndexChanged
void registerAccount(const QVariantMap &account)
registerAccount
void removeAccountAndFiles(const int &index)
removeAccountAndFiles
void currentAccountChanged(QVariantMap account)
currentAccountChanged
void countChanged()
void preListChanged()
void postListChanged()
static const QHash< MODEL_KEY, QString > MODEL_NAME
const FMH::MODEL toModel(const QVariantMap &map)
const QVariantMap toMap(const MODEL &model)
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
const_reference at(qsizetype i) const const
qsizetype indexOf(const AT &value, qsizetype from) const const
qsizetype size() const const
T value(qsizetype i) const const
Q_EMITQ_EMIT
void deleteLater()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:39:19 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.