Commit 0453af78 authored by camilo higuita's avatar camilo higuita

move mauiaccount db based implementation to sqliteprovider

parent f22f69b6
......@@ -21,13 +21,20 @@
#define ACCOUNTSPROVIDER_ACCOUNTSPROVIDER_H
#include <QObject>
#include "fmh.h"
class AccountsProvider : public QObject {
Q_OBJECT
public:
AccountsProvider(QObject* parent=nullptr) : QObject(parent) {}
virtual QVariant getAccounts(QString service, bool includeDisabled=false) = 0;
virtual FMH::MODEL_LIST getAccounts(QString service, bool includeDisabled=false) = 0;
virtual bool addAccount(const QString &server, const QString &user, const QString &password) = 0;
virtual bool removeAccount(const QString &server, const QString &user) = 0;
signals:
void accountAdded(QVariantMap account);
void accountRemoved(QVariantMap account);
};
#endif
......@@ -29,7 +29,7 @@ KAccountsProvider::KAccountsProvider(QObject *parent) : AccountsProvider(parent)
{
}
QVariant KAccountsProvider::getAccounts(QString service, bool includeDisabled)
FMH::MODEL_LIST KAccountsProvider::getAccounts(QString service, bool includeDisabled)
{
......@@ -56,5 +56,17 @@ QVariant KAccountsProvider::getAccounts(QString service, bool includeDisabled)
qDebug() << "###";
return QVariant();
return FMH::MODEL_LIST();
}
bool KAccountsProvider::addAccount(const QString& server, const QString& user, const QString& password)
{
return false;
}
bool KAccountsProvider::removeAccount(const QString& server, const QString& user)
{
return false;
}
......@@ -34,8 +34,10 @@ class KAccountsProvider : public AccountsProvider {
Q_OBJECT
public:
KAccountsProvider(QObject* parent = nullptr);
QVariant getAccounts(QString service, bool includeDisabled = false) override final;
FMH::MODEL_LIST getAccounts(QString service, bool includeDisabled = false) override final;
bool addAccount(const QString &server, const QString &user, const QString &password) override final;
bool removeAccount(const QString &server, const QString &user) override final;
};
#endif
......
......@@ -16,3 +16,88 @@
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "sqliteprovider.h"
#include "accountsdb.h"
SQLiteProvider::SQLiteProvider(QObject* parent) : AccountsProvider(parent),
db(new AccountsDB(parent))
{}
FMH::MODEL_LIST SQLiteProvider::getAccounts(QString service, bool includeDisabled)
{
auto accounts = this->get("select * from cloud");
FMH::MODEL_LIST res;
for(const auto &account : accounts)
{
auto map = account.toMap();
res << FMH::MODEL {
{FMH::MODEL_KEY::PATH, FMH::PATHTYPE_URI[FMH::PATHTYPE_KEY::CLOUD_PATH]+map[FMH::MODEL_NAME[FMH::MODEL_KEY::USER]].toString()},
{FMH::MODEL_KEY::ICON, "folder-cloud"},
{FMH::MODEL_KEY::LABEL, map[FMH::MODEL_NAME[FMH::MODEL_KEY::USER]].toString()},
{FMH::MODEL_KEY::USER, map[FMH::MODEL_NAME[FMH::MODEL_KEY::USER]].toString()},
{FMH::MODEL_KEY::SERVER, map[FMH::MODEL_NAME[FMH::MODEL_KEY::SERVER]].toString()},
{FMH::MODEL_KEY::PASSWORD, map[FMH::MODEL_NAME[FMH::MODEL_KEY::PASSWORD]].toString()},
{FMH::MODEL_KEY::TYPE, FMH::PATHTYPE_LABEL[FMH::PATHTYPE_KEY::CLOUD_PATH]}};
}
return res;
}
QVariantList SQLiteProvider::get(const QString &queryTxt)
{
QVariantList mapList;
auto query = this->db->getQuery(queryTxt);
if(query.exec())
{
while(query.next())
{
QVariantMap data;
for(auto key : FMH::MODEL_NAME.keys())
if(query.record().indexOf(FMH::MODEL_NAME[key]) > -1)
data[FMH::MODEL_NAME[key]] = query.value(FMH::MODEL_NAME[key]).toString();
mapList<< data;
}
}else qDebug()<< query.lastError()<< query.lastQuery();
return mapList;
}
bool SQLiteProvider::addAccount(const QString &server, const QString &user, const QString &password)
{
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}
};
if(this->db->insert("cloud", account))
{
emit this->accountAdded(account);
return true;
}
return false;
}
bool SQLiteProvider::removeAccount(const QString &server, const QString &user)
{
FMH::MODEL account = {
{FMH::MODEL_KEY::SERVER, server},
{FMH::MODEL_KEY::USER, user},
};
if(this->db->remove("cloud", account))
{
emit this->accountRemoved(FMH::toMap(account));
return true;
}
return false;
}
......@@ -20,4 +20,29 @@
#ifndef ACCOUNTSPROVIDER_SQLITEPROVIDER_H
#define ACCOUNTSPROVIDER_SQLITEPROVIDER_H
#include <QObject>
#include "accountsprovider.h"
#include "fmh.h"
class AccountsDB;
class SQLiteProvider : public AccountsProvider
{
Q_OBJECT
public:
explicit SQLiteProvider(QObject *parent = nullptr);
FMH::MODEL_LIST getAccounts(QString service, bool includeDisabled=false) override final;
bool addAccount(const QString &server, const QString &user, const QString &password) override final;
bool removeAccount(const QString &server, const QString &user) override final;
private:
AccountsDB *db;
QVariantList get(const QString &queryTxt);
};
#endif
......@@ -17,18 +17,15 @@
*/
#include "mauiaccounts.h"
#include "accountsdb.h"
#include "accountsprovider/accountsprovider.h"
#include "accountsprovider/kaccountsprovider.h"
#include "accountsprovider/sqliteprovider.h"
MauiAccounts::MauiAccounts(QObject *parent) : MauiList(parent),
db(new AccountsDB(parent))
MauiAccounts::MauiAccounts(QObject *parent) : MauiList(parent)
{
this->setAccounts();
AccountsProvider *kaccountsProvider = new KAccountsProvider(this);
kaccountsProvider->getAccounts("global", true);
this->provider = new SQLiteProvider(this);
this->setAccounts();
}
MauiAccounts::~MauiAccounts()
......@@ -52,7 +49,8 @@ FMH::MODEL_LIST MauiAccounts::items() const
void MauiAccounts::setAccounts()
{
emit this->preListChanged();
this->m_data = this->getCloudAccounts();
this->m_data = this->provider->getAccounts("global", true);
qDebug()<< "ACCOUNTS LIST"<< this->m_data;
this->m_count = this->m_data.count();
......@@ -60,82 +58,6 @@ void MauiAccounts::setAccounts()
emit this->postListChanged();
}
FMH::MODEL_LIST MauiAccounts::getCloudAccounts()
{
auto accounts = this->get("select * from cloud");
FMH::MODEL_LIST res;
for(const auto &account : accounts)
{
auto map = account.toMap();
res << FMH::MODEL {
{FMH::MODEL_KEY::PATH, FMH::PATHTYPE_URI[FMH::PATHTYPE_KEY::CLOUD_PATH]+map[FMH::MODEL_NAME[FMH::MODEL_KEY::USER]].toString()},
{FMH::MODEL_KEY::ICON, "folder-cloud"},
{FMH::MODEL_KEY::LABEL, map[FMH::MODEL_NAME[FMH::MODEL_KEY::USER]].toString()},
{FMH::MODEL_KEY::USER, map[FMH::MODEL_NAME[FMH::MODEL_KEY::USER]].toString()},
{FMH::MODEL_KEY::SERVER, map[FMH::MODEL_NAME[FMH::MODEL_KEY::SERVER]].toString()},
{FMH::MODEL_KEY::PASSWORD, map[FMH::MODEL_NAME[FMH::MODEL_KEY::PASSWORD]].toString()},
{FMH::MODEL_KEY::TYPE, FMH::PATHTYPE_LABEL[FMH::PATHTYPE_KEY::CLOUD_PATH]}};
}
return res;
}
bool MauiAccounts::addCloudAccount(const QString &server, const QString &user, const QString &password)
{
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}
};
if(this->db->insert("cloud", account))
{
emit this->accountAdded(account);
return true;
}
return false;
}
bool MauiAccounts::removeCloudAccount(const QString &server, const QString &user)
{
FMH::MODEL account = {
{FMH::MODEL_KEY::SERVER, server},
{FMH::MODEL_KEY::USER, user},
};
if(this->db->remove("cloud", account))
{
emit this->accountRemoved(FMH::toMap(account));
return true;
}
return false;
}
QVariantList MauiAccounts::get(const QString &queryTxt)
{
QVariantList mapList;
auto query = this->db->getQuery(queryTxt);
if(query.exec())
{
while(query.next())
{
QVariantMap data;
for(auto key : FMH::MODEL_NAME.keys())
if(query.record().indexOf(FMH::MODEL_NAME[key]) > -1)
data[FMH::MODEL_NAME[key]] = query.value(FMH::MODEL_NAME[key]).toString();
mapList<< data;
}
}else qDebug()<< query.lastError()<< query.lastQuery();
return mapList;
}
int MauiAccounts::getCurrentAccountIndex() const
{
return this->m_currentAccountIndex;
......@@ -151,7 +73,7 @@ void MauiAccounts::registerAccount(const QVariantMap& account)
// register the account to the backend needed
auto model = FMH::toModel(account);
if(this->addCloudAccount(model[FMH::MODEL_KEY::SERVER], model[FMH::MODEL_KEY::USER], model[FMH::MODEL_KEY::PASSWORD]))
if(this->provider->addAccount(model[FMH::MODEL_KEY::SERVER], model[FMH::MODEL_KEY::USER], model[FMH::MODEL_KEY::PASSWORD]))
{
this->setAccounts();
}
......@@ -177,15 +99,14 @@ QVariantMap MauiAccounts::get(const int& index) const
return FMH::toMap(this->m_data.at(index));
}
QVariantList MauiAccounts::getCloudAccountsList()
FMH::MODEL_LIST MauiAccounts::getCloudAccounts()
{
QVariantList res;
const auto data = this->getCloudAccounts();
for(const auto &item : data)
res << FMH::toMap(item);
return this->m_data;
}
return res;
QVariantList MauiAccounts::getCloudAccountsList()
{
return FMH::toList(this->m_data);
}
uint MauiAccounts::getCount() const
......@@ -203,7 +124,7 @@ void MauiAccounts::removeAccount(const int& index)
if(index >= this->m_data.size() || index < 0)
return;
if(this->removeCloudAccount(this->m_data.at(index)[FMH::MODEL_KEY::SERVER], this->m_data.at(index)[FMH::MODEL_KEY::USER]))
if(this->provider->removeAccount(this->m_data.at(index)[FMH::MODEL_KEY::SERVER], this->m_data.at(index)[FMH::MODEL_KEY::USER]))
{
this->refresh();
}
......@@ -214,7 +135,7 @@ void MauiAccounts::removeAccountAndFiles(const int& index)
if(index >= this->m_data.size() || index < 0)
return;
if(this->removeCloudAccount(this->m_data.at(index)[FMH::MODEL_KEY::SERVER], this->m_data.at(index)[FMH::MODEL_KEY::USER]))
if(this->provider->removeAccount(this->m_data.at(index)[FMH::MODEL_KEY::SERVER], this->m_data.at(index)[FMH::MODEL_KEY::USER]))
{
this->refresh();
}
......
......@@ -32,7 +32,7 @@
#include "mauikit_export.h"
#endif
class AccountsDB;
class AccountsProvider;
#ifdef STATIC_MAUIKIT
class MauiAccounts : public MauiList
#else
......@@ -72,8 +72,9 @@ private:
static MauiAccounts *m_instance;
MauiAccounts(QObject *parent = nullptr);
~MauiAccounts() override;
AccountsProvider *provider;
AccountsDB *db;
FMH::MODEL_LIST m_data;
QVariantMap m_currentAccount;
......@@ -81,12 +82,7 @@ private:
uint m_count = 0;
void setAccounts();
bool addCloudAccount(const QString &server, const QString &user, const QString &password);
bool removeCloudAccount(const QString &server, const QString &user);
QVariantList get(const QString &queryTxt);
signals:
void accountAdded(QVariantMap account);
void accountRemoved(QVariantMap account);
......
......@@ -542,6 +542,15 @@ static const inline FMH::MODEL_LIST toModelList(const QVariantList& list)
return res;
}
static const inline QVariantList toList(const FMH::MODEL_LIST& list)
{
QVariantList res;
for(const auto &data : list)
res << FMH::toMap(data);
return res;
}
static const inline FMH::MODEL filterModel(const FMH::MODEL &model, const QVector<FMH::MODEL_KEY> &keys)
{
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment