File indexing completed on 2025-04-27 04:08:15

0001 /* This file is part of the KDE project
0002  *
0003  * SPDX-FileCopyrightText: 2012 Arjen Hiemstra <ahiemstra@heimr.nl>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 
0008 #include "FileSystemModel.h"
0009 
0010 #include <QDateTime>
0011 #include <KisMimeDatabase.h>
0012 #include <QFileInfo>
0013 #include <QDir>
0014 #include <QStandardPaths>
0015 #include <QDebug>
0016 
0017 class FileSystemModel::Private
0018 {
0019 public:
0020     QDir dir;
0021     QFileInfoList list;
0022 
0023     static const QString drivesPath;
0024 };
0025 
0026 const QString FileSystemModel::Private::drivesPath("special://drives");
0027 
0028 FileSystemModel::FileSystemModel(QObject* parent)
0029     : QAbstractListModel(parent), d(new Private)
0030 {
0031     d->dir.setFilter(QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot);
0032     d->dir.setSorting(QDir::DirsFirst | QDir::Name | QDir::IgnoreCase);
0033 }
0034 
0035 FileSystemModel::~FileSystemModel()
0036 {
0037     delete d;
0038 }
0039 
0040 QVariant FileSystemModel::data(const QModelIndex& index, int role) const
0041 {
0042     if (index.isValid()) {
0043         const QFileInfo &fileInfo = d->list.at(index.row());
0044 
0045         switch(role) {
0046         case FileNameRole:
0047             return fileInfo.fileName();
0048             break;
0049         case FilePathRole:
0050             return fileInfo.absoluteFilePath();
0051             break;
0052         case FileIconRole:
0053             return fileInfo.isDir() ? "inode/directory" : QString("image://recentimage/%1").arg(fileInfo.absoluteFilePath());
0054             break;
0055         case FileDateRole:
0056             return fileInfo.lastModified().toString(Qt::SystemLocaleShortDate);
0057         }
0058     }
0059     return QVariant();
0060 }
0061 
0062 int FileSystemModel::rowCount(const QModelIndex&) const
0063 {
0064     return d->list.count();
0065 }
0066 
0067 void FileSystemModel::classBegin()
0068 {
0069 
0070 }
0071 
0072 void FileSystemModel::componentComplete()
0073 {
0074     setPath(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation));
0075 }
0076 
0077 QString FileSystemModel::path()
0078 {
0079     QString path = d->dir.absolutePath();
0080     if (path.isEmpty()) {
0081         return Private::drivesPath;
0082     } else {
0083         return d->dir.absolutePath();
0084     }
0085 }
0086 
0087 void FileSystemModel::setPath(const QString& path)
0088 {
0089     if (path != d->dir.path()) {
0090         if (d->list.count() > 0) {
0091             beginRemoveRows(QModelIndex(), 0, d->list.count() - 1);
0092             endRemoveRows();
0093         }
0094 
0095         if (path != Private::drivesPath) {
0096             d->dir.setPath(path);
0097             d->dir.refresh();
0098             d->list = d->dir.entryInfoList();
0099             if (d->list.count() > 0) {
0100                 beginInsertRows(QModelIndex(), 0, d->list.count() - 1);
0101                 endInsertRows();
0102             }
0103         } else {
0104             d->dir.setPath("");
0105             d->dir.refresh();
0106             d->list = QDir::drives();
0107 
0108             beginInsertRows(QModelIndex(), 0, d->list.count() - 1);
0109             endInsertRows();
0110         }
0111         emit pathChanged();
0112     }
0113 }
0114 
0115 QString FileSystemModel::parentFolder()
0116 {
0117     if (path() != Private::drivesPath) {
0118         if (QRegExp("^[A-Z]{1,3}:/$").exactMatch(path())) {
0119             return Private::drivesPath;
0120         } else {
0121             QDir root(path());
0122             root.cdUp();
0123             return root.path();
0124         }
0125     } else {
0126         return Private::drivesPath;
0127     }
0128 }
0129 
0130 QString FileSystemModel::filter()
0131 {
0132     return d->dir.nameFilters().join(" ");
0133 }
0134 
0135 void FileSystemModel::setFilter(const QString& filter)
0136 {
0137     d->dir.setNameFilters(filter.split(" "));
0138 }
0139 
0140 QHash<int, QByteArray> FileSystemModel::roleNames() const
0141 {
0142     QHash<int, QByteArray> roles;
0143     roles.insert(FileNameRole, "fileName");
0144     roles.insert(FilePathRole, "path");
0145     roles.insert(FileIconRole, "icon");
0146     roles.insert(FileDateRole, "date");
0147     return roles;
0148 }
0149