File indexing completed on 2024-05-12 15:54:49

0001 /*
0002  * SPDX-FileCopyrightText: (C) 2017 Atul Sharma <atulsharma406@gmail.com>
0003  * SPDX-FileCopyrightText: (C) 2017 by Marco Martin <mart@kde.org>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 
0008 #include "imagefoldermodel.h"
0009 #include "roles.h"
0010 #include "types.h"
0011 
0012 #include <QDebug>
0013 #include <QDir>
0014 #include <QImage>
0015 #include <QMimeDatabase>
0016 #include <QPixmap>
0017 #include <QProcess>
0018 #include <QStandardPaths>
0019 
0020 #include <KIO/EmptyTrashJob>
0021 #include <kdirlister.h>
0022 
0023 ImageFolderModel::ImageFolderModel(QObject *parent)
0024     : KDirModel(parent)
0025 {
0026     QMimeDatabase db;
0027     QList<QMimeType> mimeList = db.allMimeTypes();
0028 
0029     m_mimeTypes << "inode/directory";
0030     for (auto &mime : qAsConst(mimeList)) {
0031         if (mime.name().startsWith(QStringLiteral("image/")) || mime.name().startsWith(QStringLiteral("video/"))) {
0032             m_mimeTypes << mime.name();
0033         }
0034     }
0035 
0036     dirLister()->setMimeFilter(m_mimeTypes);
0037 
0038     connect(this, &QAbstractItemModel::rowsInserted, this, &ImageFolderModel::countChanged);
0039     connect(this, &QAbstractItemModel::rowsRemoved, this, &ImageFolderModel::countChanged);
0040     connect(this, &QAbstractItemModel::modelReset, this, &ImageFolderModel::countChanged);
0041     // we need the complete one, not the qurl one
0042     connect(dirLister(), QOverload<>::of(&KCoreDirLister::completed), this, &ImageFolderModel::jobFinished);
0043 }
0044 
0045 void ImageFolderModel::jobFinished()
0046 {
0047     if (dirLister()->isFinished()) {
0048         Q_EMIT finishedLoading();
0049     }
0050 }
0051 
0052 QHash<int, QByteArray> ImageFolderModel::roleNames() const
0053 {
0054     return Roles::roleNames();
0055 }
0056 
0057 QUrl ImageFolderModel::url() const
0058 {
0059     return dirLister()->url();
0060 }
0061 
0062 void ImageFolderModel::setUrl(QUrl &url)
0063 {
0064     if (url.isEmpty()) {
0065         QStringList locations = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation);
0066         Q_ASSERT(locations.size() >= 1);
0067         url = QUrl::fromLocalFile(locations.first().append("/"));
0068     }
0069 
0070     if (dirLister()->url() == url) {
0071         dirLister()->updateDirectory(QUrl(url));
0072         return;
0073     }
0074 
0075     beginResetModel();
0076     dirLister()->openUrl(QUrl(url));
0077     endResetModel();
0078     Q_EMIT urlChanged();
0079 }
0080 
0081 int ImageFolderModel::indexForUrl(const QString &url) const
0082 {
0083     return KDirModel::indexForUrl(QUrl(url)).row();
0084 }
0085 
0086 QVariantMap ImageFolderModel::get(int i) const
0087 {
0088     QModelIndex modelIndex = index(i, 0);
0089 
0090     KFileItem item = itemForIndex(modelIndex);
0091     const QString url = item.url().toString();
0092     const QString mimeType = item.mimetype();
0093 
0094     QVariantMap ret;
0095     ret.insert(QStringLiteral("url"), QVariant(url));
0096     ret.insert(QStringLiteral("mimeType"), QVariant(mimeType));
0097 
0098     return ret;
0099 }
0100 
0101 void ImageFolderModel::emptyTrash()
0102 {
0103     KIO::emptyTrash();
0104 }
0105 
0106 QVariant ImageFolderModel::data(const QModelIndex &index, int role) const
0107 {
0108     Q_ASSERT(checkIndex(index, CheckIndexOption::ParentIsInvalid | CheckIndexOption::IndexIsValid));
0109 
0110     switch (role) {
0111     case Roles::ImageUrlRole: {
0112         KFileItem item = itemForIndex(index);
0113         return item.url().toString();
0114     }
0115     case Roles::MimeTypeRole: {
0116         KFileItem item = itemForIndex(index);
0117         return item.mimetype();
0118     }
0119 
0120     case Roles::ItemTypeRole: {
0121         KFileItem item = itemForIndex(index);
0122         if (item.isDir()) {
0123             return Types::Folder;
0124         } else {
0125             return Types::Image;
0126         }
0127     }
0128 
0129     case Roles::SelectedRole:
0130         return false;
0131 
0132     case Roles::ContentRole:
0133         return KDirModel::data(index, Qt::DisplayRole);
0134 
0135     default:
0136         return KDirModel::data(index, role);
0137     }
0138 }