File indexing completed on 2025-03-16 04:25:29
0001 /* 0002 * <one line to give the program's name and a brief idea of what it does.> 0003 * Copyright (C) 2019 camilo <chiguitar@unal.edu.co> 0004 * 0005 * This program is free software: you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation, either version 3 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This program is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU General Public License 0016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0017 */ 0018 0019 #include "mauiaccounts.h" 0020 #include "accountsdb.h" 0021 0022 MauiAccounts *MauiAccounts::m_instance = nullptr; 0023 0024 MauiAccounts::MauiAccounts() 0025 : MauiList(nullptr) 0026 , db(new AccountsDB(nullptr)) 0027 { 0028 this->setAccounts(); 0029 } 0030 0031 MauiAccounts::~MauiAccounts() 0032 { 0033 qDebug() << "DELETING MAUI ACCOUNTS INSTANCE"; 0034 this->db->deleteLater(); 0035 this->db = nullptr; 0036 } 0037 0038 const FMH::MODEL_LIST &MauiAccounts::items() const 0039 { 0040 return this->m_data; 0041 } 0042 0043 void MauiAccounts::setAccounts() 0044 { 0045 Q_EMIT this->preListChanged(); 0046 this->m_data = this->getCloudAccounts(); 0047 qDebug() << "ACCOUNTS LIST" << this->m_data; 0048 0049 Q_EMIT this->countChanged(); 0050 Q_EMIT this->postListChanged(); 0051 } 0052 0053 FMH::MODEL_LIST MauiAccounts::getCloudAccounts() 0054 { 0055 auto accounts = this->get("select * from cloud"); 0056 FMH::MODEL_LIST res; 0057 for (const auto &account : qAsConst(accounts)) { 0058 auto map = account.toMap(); 0059 /*{FMH::MODEL_KEY::PATH, FMH::PATHTYPE_URI[FMH::PATHTYPE_KEY::CLOUD_PATH] + map[FMH::MODEL_NAME[FMH::MODEL_KEY::USER]].toString()},*/ 0060 /* {FMH::MODEL_KEY::TYPE, FMH::PATHTYPE_LABEL[FMH::PATHTYPE_KEY::CLOUD_PATH]}*/ 0061 res << FMH::MODEL { 0062 {FMH::MODEL_KEY::ICON, "folder-cloud"}, 0063 {FMH::MODEL_KEY::LABEL, map[FMH::MODEL_NAME[FMH::MODEL_KEY::USER]].toString()}, 0064 {FMH::MODEL_KEY::USER, map[FMH::MODEL_NAME[FMH::MODEL_KEY::USER]].toString()}, 0065 {FMH::MODEL_KEY::SERVER, map[FMH::MODEL_NAME[FMH::MODEL_KEY::SERVER]].toString()}, 0066 {FMH::MODEL_KEY::PASSWORD, map[FMH::MODEL_NAME[FMH::MODEL_KEY::PASSWORD]].toString()}}; 0067 } 0068 return res; 0069 } 0070 0071 bool MauiAccounts::addCloudAccount(const QString &server, const QString &user, const QString &password) 0072 { 0073 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}}; 0074 0075 if (this->db->insert("cloud", account)) { 0076 Q_EMIT this->accountAdded(account); 0077 return true; 0078 } 0079 0080 return false; 0081 } 0082 0083 bool MauiAccounts::removeCloudAccount(const QString &server, const QString &user) 0084 { 0085 FMH::MODEL account = { 0086 {FMH::MODEL_KEY::SERVER, server}, 0087 {FMH::MODEL_KEY::USER, user}, 0088 }; 0089 0090 if (this->db->remove("cloud", account)) { 0091 Q_EMIT this->accountRemoved(FMH::toMap(account)); 0092 return true; 0093 } 0094 0095 return false; 0096 } 0097 0098 QVariantList MauiAccounts::get(const QString &queryTxt) 0099 { 0100 QVariantList mapList; 0101 0102 auto query = this->db->getQuery(queryTxt); 0103 0104 if (query.exec()) { 0105 const auto keys = FMH::MODEL_NAME.keys(); 0106 0107 while (query.next()) { 0108 QVariantMap data; 0109 for (auto key : keys) 0110 if (query.record().indexOf(FMH::MODEL_NAME[key]) > -1) 0111 data[FMH::MODEL_NAME[key]] = query.value(FMH::MODEL_NAME[key]).toString(); 0112 0113 mapList << data; 0114 } 0115 0116 } else 0117 qDebug() << query.lastError() << query.lastQuery(); 0118 0119 return mapList; 0120 } 0121 0122 int MauiAccounts::getCurrentAccountIndex() const 0123 { 0124 return this->m_currentAccountIndex; 0125 } 0126 0127 QVariantMap MauiAccounts::getCurrentAccount() const 0128 { 0129 return this->m_currentAccount; 0130 } 0131 0132 void MauiAccounts::registerAccount(const QVariantMap &account) 0133 { 0134 // register the account to the backend needed 0135 auto model = FMH::toModel(account); 0136 0137 if (this->addCloudAccount(model[FMH::MODEL_KEY::SERVER], model[FMH::MODEL_KEY::USER], model[FMH::MODEL_KEY::PASSWORD])) { 0138 this->setAccounts(); 0139 } 0140 } 0141 0142 void MauiAccounts::setCurrentAccountIndex(const int &index) 0143 { 0144 if (index >= this->m_data.size() || index < 0) 0145 return; 0146 0147 if (index == this->m_currentAccountIndex) 0148 return; 0149 0150 // make sure the account exists 0151 this->m_currentAccountIndex = index; 0152 this->m_currentAccount = FMH::toMap(this->m_data.at(m_currentAccountIndex)); 0153 0154 Q_EMIT this->currentAccountChanged(this->m_currentAccount); 0155 Q_EMIT this->currentAccountIndexChanged(this->m_currentAccountIndex); 0156 } 0157 0158 QVariantList MauiAccounts::getCloudAccountsList() 0159 { 0160 QVariantList res; 0161 0162 const auto data = this->getCloudAccounts(); 0163 for (const auto &item : data) 0164 res << FMH::toMap(item); 0165 0166 return res; 0167 } 0168 0169 void MauiAccounts::refresh() 0170 { 0171 this->setAccounts(); 0172 } 0173 0174 void MauiAccounts::removeAccount(const int &index) 0175 { 0176 if (index >= this->m_data.size() || index < 0) 0177 return; 0178 0179 if (this->removeCloudAccount(this->m_data.at(index)[FMH::MODEL_KEY::SERVER], this->m_data.at(index)[FMH::MODEL_KEY::USER])) { 0180 this->refresh(); 0181 } 0182 } 0183 0184 void MauiAccounts::removeAccountAndFiles(const int &index) 0185 { 0186 if (index >= this->m_data.size() || index < 0) 0187 return; 0188 0189 if (this->removeCloudAccount(this->m_data.at(index)[FMH::MODEL_KEY::SERVER], this->m_data.at(index)[FMH::MODEL_KEY::USER])) { 0190 this->refresh(); 0191 } 0192 0193 // FM_STATIC::removeDir(FM::resolveUserCloudCachePath(this->m_data.at(index)[FMH::MODEL_KEY::SERVER], this->m_data.at(index)[FMH::MODEL_KEY::USER])); 0194 }