22#include <kconfiggroup.h>
23#include <ksharedconfig.h>
34 : QAbstractTableModel(parent)
56 if (parent.isValid()) {
59 return d->storages.size();
65 if (parent.isValid()) {
74 const QString storageLocation = query.value(
"location").toString();
75 const QString storageType = query.value(
"storage_type").toString();
76 const QString storageIdAsString = query.value(
"id").toString();
82 const int storageId = query.value(
"id").toInt();
87 result = thumbQuery.prepare(
"SELECT thumbnail FROM storages WHERE id = :id");
89 qWarning() <<
"Failed to prepare query for thumbnail of" << storageId << thumbQuery.lastError();
93 thumbQuery.bindValue(
":id", storageId);
95 result = thumbQuery.exec();
98 qWarning() <<
"Failed to execute query for thumbnail of" << storageId << thumbQuery.lastError();
102 if (!thumbQuery.next()) {
103 qWarning() <<
"Failed to find thumbnail of" << storageId;
107 QByteArray ba = thumbQuery.value(
"thumbnail").toByteArray();
109 buf.open(QBuffer::ReadOnly);
110 img.load(&buf,
"PNG");
120 if (!index.isValid())
return v;
122 if (index.column() > (
int)
MetaData)
return v;
124 if (role == Qt::FontRole) {
128 QString location =
d->storages.at(index.row());
132 bool r = query.prepare(
133 "SELECT storages.id as id\n"
134 ", storage_types.name as storage_type\n"
141 "WHERE storages.storage_type_id = storage_types.id\n"
142 "AND location = :location");
145 qWarning() <<
"Could not prepare KisStorageModel data query" << query.lastError();
149 query.bindValue(
":location", location);
154 qWarning() <<
"Could not execute KisStorageModel data query" << query.lastError() << query.boundValues();
158 if (!query.first()) {
159 qWarning() <<
"KisStorageModel data query did not return anything";
163 if ((role == Qt::DisplayRole || role == Qt::EditRole) && index.column() ==
Active) {
164 return query.value(
"active");
167 case Qt::DisplayRole:
169 switch(index.column()) {
171 return query.value(
"id");
173 return query.value(
"storage_type");
175 return query.value(
"location");
177 return QDateTime::fromSecsSinceEpoch(query.value(
"timestamp").value<
int>()).toString();
179 return query.value(
"pre_installed");
181 return query.value(
"active");
189 QVariant name = query.value(
"location");
207 case Qt::CheckStateRole: {
208 switch (index.column()) {
210 if (query.value(
"pre_installed").toInt() == 0) {
211 return Qt::Unchecked;
216 if (query.value(
"active").toInt() == 0) {
217 return Qt::Unchecked;
225 case Qt::DecorationRole: {
231 case Qt::UserRole +
Id:
232 return query.value(
"id");
236 QVariant name = query.value(
"location");
246 return query.value(
"storage_type");
248 return query.value(
"location");
250 return query.value(
"timestamp");
252 return query.value(
"pre_installed");
253 case Qt::UserRole +
Active:
254 return query.value(
"active");
273 if (index.isValid()) {
275 if (role == Qt::CheckStateRole) {
277 bool r = query.prepare(
"UPDATE storages\n"
278 "SET active = :active\n"
280 query.bindValue(
":active",
value);
281 query.bindValue(
":id", index.data(Qt::UserRole +
Id));
284 qWarning() <<
"Could not prepare KisStorageModel update query" << query.lastError();
291 qWarning() <<
"Could not execute KisStorageModel update query" << query.lastError();
297 Q_EMIT dataChanged(index, index, {role});
299 if (
value.toBool()) {
312 if (!index.isValid()) {
313 return Qt::NoItemFlags;
315 return QAbstractTableModel::flags(index) | Qt::ItemIsEditable | Qt::ItemNeverHasChildren;
321 if (!index.isValid())
return 0;
322 if (index.row() >
rowCount())
return 0;
323 if (index.column() > (
int)
MetaData)
return 0;
325 QString location =
d->storages.at(index.row());
334 bool r = query.prepare(
"SELECT location\n"
336 "WHERE storages.id = :storageId");
339 qWarning() <<
"Could not prepare KisStorageModel data query" << query.lastError();
343 query.bindValue(
":storageId", storageId);
348 qWarning() <<
"Could not execute KisStorageModel data query" << query.lastError() << query.boundValues();
352 if (!query.first()) {
353 qWarning() <<
"KisStorageModel data query did not return anything";
365 QFileInfo info = QFileInfo(location +
"/" + filename);
366 if (!info.exists()) {
370 QString extension = info.suffix();
371 QString filenameNoExtension = filename.left(filename.length() - extension.length());
374 QDir dir = QDir(location);
378 int maxVersionUsed = -1;
379 for (
int i = 0; i < similarEntries.count(); i++) {
380 QString entry = similarEntries[i];
382 if (!entry.endsWith(extension)) {
385 QString versionStr = entry.right(entry.length() - filenameNoExtension.length());
386 versionStr = versionStr.left(versionStr.length() - extension.length());
387 if (!versionStr.startsWith(
"_")) {
390 versionStr = versionStr.right(versionStr.length() - 1);
393 int version = versionStr.toInt(&ok);
397 if (version > maxVersionUsed) {
398 maxVersionUsed = version;
402 int versionToUse = maxVersionUsed > -1 ? maxVersionUsed + 1 : 1;
403 int versionStringLength = 3;
404 QString baseNewVersion = QString::number(versionToUse);
405 while (baseNewVersion.length() < versionStringLength) {
406 baseNewVersion.prepend(
"0");
409 QString newFilename = filenameNoExtension +
"_" + QString::number(versionToUse) + extension;
410 bool success = !QFileInfo(location +
"/" + newFilename).exists();
413 qCritical() <<
"The new filename for the bundle does exist." << newFilename;
423 QFileInfo oldFileInfo(filename);
425 QString newName = oldFileInfo.fileName();
426 QString newLocation = newDir +
'/' + newName;
428 QFileInfo newFileInfo(newLocation);
429 if (newFileInfo.exists()) {
433 }
else if (importOption ==
Rename) {
435 newLocation = newDir +
'/' + newName;
436 newFileInfo = QFileInfo(newLocation);
441 QFile::copy(filename, newLocation);
446 if (storage.isNull()) {
return false; }
448 qWarning() <<
"Could not add bundle to the storages" << newLocation;
456 QVariant
v = QVariant();
457 if (role != Qt::DisplayRole) {
460 if (orientation == Qt::Horizontal) {
467 return i18n(
"Location");
469 return i18n(
"Creation Date");
471 return i18n(
"Preinstalled");
473 return i18n(
"Active");
475 return i18n(
"Thumbnail");
479 v = QString::number(section);
483 return QAbstractTableModel::headerData(section, orientation, role);
489 d->storages.append(location);
495 int row =
d->storages.indexOf(QFileInfo(location).fileName());
496 beginRemoveRows(QModelIndex(), row, row);
497 d->storages.removeAt(row);
514 bool r = query.prepare(
519 qWarning() <<
"Could not prepare KisStorageModel query" << query.lastError();
525 qWarning() <<
"Could not execute KisStorageModel query" << query.lastError();
529 while (query.next()) {
530 d->storages << query.value(0).toString();
float value(const T *src, size_t ch)
QList< QString > QStringList
Q_GLOBAL_STATIC(KisStoragePluginRegistry, s_instance)
QString findUnusedName(QString location, QString filename)
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
QMap< QString, QVariant > metaDataForStorage(const QString &storageLocation) const
metaDataForStorage
KisResourceStorageSP storageByLocation(const QString &location) const
void storageRemoved(const QString &location)
Emitted whenever a storage is removed.
void storageAdded(const QString &location)
Emitted whenever a storage is added.
void storagesBulkSynchronizationFinished()
void storageResynchronized(const QString &storage, bool isBulkResynchronization)
static KisResourceLocator * instance()
static const QString s_meta_title
static const QString s_meta_name
void insert(const QString &storageLocation, const QString &resourceType, const QString &filename, const QImage &image)
static KisResourceThumbnailCache * instance()
QImage originalImage(const QString &storageLocation, const QString &resourceType, const QString &filename) const
static KisStorageModel * instance()
void storageDisabled(const QString &storage)
~KisStorageModel() override
bool importStorage(QString filename, StorageImportOption importOption) const
static QImage getThumbnailFromQuery(const QSqlQuery &query)
bool setData(const QModelIndex &index, const QVariant &value, int role) override
Enable and disable the storage represented by index.
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
KisStorageModel(QObject *parent=0)
void removeStorage(const QString &location)
This is called when a storage really is deleted both from database and anywhere else.
QScopedPointer< Private > d
int columnCount(const QModelIndex &parent=QModelIndex()) const override
void addStorage(const QString &location)
Called whenever a storage is added.
void storageEnabled(const QString &storage)
void storageResynchronized(const QString &storage, bool isBulkResynchronization)
Emitted when an individual storage is initialized.
KisResourceStorageSP storageForIndex(const QModelIndex &index) const
int rowCount(const QModelIndex &parent=QModelIndex()) const override
KisResourceStorageSP storageForId(const int storageId) const
Qt::ItemFlags flags(const QModelIndex &index) const override
QVariant data(const QModelIndex &index, int role) const override
void slotStoragesBulkSynchronizationFinished()
called when storages finished synchronization process
void storagesBulkSynchronizationFinished()
Emitted on loading when all the storages are finished initialization.
static QString getAppDataLocation()
#define KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(cond, val)
QList< QString > storages