1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
/* This file is part of the KDE project
* SPDX-FileCopyrightText: 2014 Dan Leinir Turthra Jensen <admin@leinir.dk>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*
*/
#include "CloudAccountsModel.h"
#include "PropertyContainer.h"
#include <QByteArray>
#include <QDataStream>
#include <QDebug>
#include <QFile>
#include <QMetaObject>
#include <QMetaProperty>
#include <QStandardPaths>
#include <QVariant>
struct AccountEntry {
public:
AccountEntry(QObject *parent)
: selected(false)
, accountDetails(new PropertyContainer("", parent))
{
}
QString text;
bool selected;
QString accountType;
QString stackComponent;
QObject *accountDetails;
QByteArray serialize()
{
// selected is not serialised, because that would be silly
QByteArray ba;
QDataStream stream(&ba, QIODevice::WriteOnly);
stream << text;
stream << accountType;
stream << stackComponent;
Q_FOREACH (const QByteArray &name, accountDetails->dynamicPropertyNames()) {
stream << name << accountDetails->property(name);
}
for (int i = accountDetails->metaObject()->propertyOffset(); i < accountDetails->metaObject()->propertyCount(); ++i) {
stream << accountDetails->metaObject()->property(i).name() << accountDetails->metaObject()->property(i).read(accountDetails);
}
return ba;
}
static AccountEntry *fromByteArray(QByteArray &ba, QObject *parent)
{
AccountEntry *entry = new AccountEntry(parent);
QDataStream stream(&ba, QIODevice::ReadOnly);
stream >> entry->text;
stream >> entry->accountType;
stream >> entry->stackComponent;
entry->accountDetails->setProperty("text", entry->text);
while (!stream.atEnd()) {
QByteArray propertyName;
QVariant data;
stream >> propertyName;
stream >> data;
entry->accountDetails->setProperty(propertyName, data);
}
return entry;
}
};
class CloudAccountsModel::Private
{
public:
Private(CloudAccountsModel *qq)
: q(qq)
{
dataFile = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + QStringLiteral("/calligrageminicloudaccounts");
loadList();
}
~Private()
{
qDeleteAll(entries);
}
CloudAccountsModel *q;
QList<AccountEntry *> entries;
QString dataFile;
void saveList()
{
QByteArray ba;
QDataStream stream(&ba, QIODevice::WriteOnly);
Q_FOREACH (AccountEntry *entry, entries) {
stream << entry->serialize();
}
QFile data(dataFile);
data.open(QIODevice::WriteOnly);
data.write(ba);
data.close();
}
void loadList()
{
QByteArray ba;
QFile data(dataFile);
data.open(QIODevice::ReadOnly);
ba = data.readAll();
data.close();
q->beginResetModel();
qDeleteAll(entries);
entries.clear();
QDataStream stream(&ba, QIODevice::ReadOnly);
QByteArray entryBA;
while (!stream.atEnd()) {
stream >> entryBA;
entries.append(AccountEntry::fromByteArray(entryBA, q));
}
q->endResetModel();
}
};
CloudAccountsModel::CloudAccountsModel(QObject *parent)
: QAbstractListModel(parent)
, d(new Private(this))
{
}
CloudAccountsModel::~CloudAccountsModel()
{
delete d;
}
QHash<int, QByteArray> CloudAccountsModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[TextRole] = "text";
roles[SelectedRole] = "selected";
roles[AccountTypeRole] = "accountType";
roles[StackComponentRole] = "stackComponent";
roles[AccountDetailsRole] = "accountDetails";
return roles;
}
QVariant CloudAccountsModel::data(const QModelIndex &index, int role) const
{
QVariant result;
if (index.isValid() && index.row() > -1 && index.row() < d->entries.count()) {
AccountEntry *entry = d->entries.at(index.row());
switch (role) {
case TextRole:
result = entry->text;
break;
case SelectedRole:
result = entry->selected;
break;
case AccountTypeRole:
result = entry->accountType;
break;
case StackComponentRole:
result = entry->stackComponent;
break;
case AccountDetailsRole:
result = QVariant::fromValue<QObject *>(entry->accountDetails);
break;
default:
break;
}
}
return result;
}
int CloudAccountsModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return d->entries.count();
}
void CloudAccountsModel::selectIndex(int newSelection)
{
Q_FOREACH (AccountEntry *entry, d->entries) {
entry->selected = false;
}
if (newSelection >= 0 && newSelection < d->entries.count()) {
d->entries.at(newSelection)->selected = true;
}
dataChanged(index(0), index(d->entries.count() - 1));
}
void CloudAccountsModel::addAccount(QString text, QString accountType, QString stackComponent, QObject *accountDetails, bool removeExisting)
{
if (removeExisting) {
removeAccountByName(text);
}
AccountEntry *entry = new AccountEntry(this);
entry->text = text;
entry->accountType = accountType;
entry->stackComponent = stackComponent;
if (accountDetails) {
Q_FOREACH (const QByteArray &name, accountDetails->dynamicPropertyNames()) {
entry->accountDetails->setProperty(name, accountDetails->property(name));
}
for (int i = accountDetails->metaObject()->propertyOffset(); i < accountDetails->metaObject()->propertyCount(); ++i) {
entry->accountDetails->setProperty(accountDetails->metaObject()->property(i).name(),
accountDetails->metaObject()->property(i).read(accountDetails));
}
}
int count = d->entries.count();
beginInsertRows(QModelIndex(), count, count);
d->entries.append(entry);
endInsertRows();
d->saveList();
}
void CloudAccountsModel::renameAccount(int index, QString newText)
{
if (index > -1 && index < d->entries.count() - 1) {
d->entries.at(index)->text = newText;
dataChanged(this->index(index), this->index(index));
d->saveList();
}
}
void CloudAccountsModel::removeAccountByName(QString text)
{
beginResetModel();
for (int i = d->entries.count() - 1; i > -1; --i) {
if (d->entries.at(i)->text == text) {
d->entries.removeAt(i);
}
}
endResetModel();
}
void CloudAccountsModel::removeAccount(int index)
{
if (index > -1 && index < d->entries.count()) {
beginRemoveRows(QModelIndex(), index, index);
delete (d->entries.takeAt(index));
endRemoveRows();
d->saveList();
}
}
QObject *CloudAccountsModel::accountDetails(int index)
{
if (index > -1 && index < d->entries.count() - 1) {
return d->entries.at(index)->accountDetails;
}
return nullptr;
}
void CloudAccountsModel::setAccountDetails(int index, QObject *newDetails)
{
if (index > -1 && index < d->entries.count() - 1) {
AccountEntry *entry = d->entries.at(index);
if (newDetails) {
Q_FOREACH (const QByteArray &name, newDetails->dynamicPropertyNames()) {
entry->accountDetails->setProperty(name, newDetails->property(name));
}
for (int i = newDetails->metaObject()->propertyOffset(); i < newDetails->metaObject()->propertyCount(); ++i) {
entry->accountDetails->setProperty(newDetails->metaObject()->property(i).name(), newDetails->metaObject()->property(i).read(newDetails));
}
}
d->saveList();
}
}
|