Skip to content

Commit

Permalink
[gallery] Move indexing into separate thread
Browse files Browse the repository at this point in the history
  • Loading branch information
neochapay committed Jan 1, 2025
1 parent d01d166 commit 1492042
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 18 deletions.
8 changes: 5 additions & 3 deletions src/plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ endfunction()

set(SRC
plugin.cpp
gallerymodel.cpp)
gallerymodel.cpp
filesystemworker.cpp)

set(HEADERS
plugin.h
gallerymodel.h)
gallerymodel.h
filesystemworker.h)

set(QML
qml/GalleryDelegate.qml
Expand All @@ -39,7 +41,7 @@ qt6_add_qml_module(glaciergalleryplugin
OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/Glacier/Gallery
RESOURCES qml/qmldir
SOURCES gallerymodel.h gallerymodel.cpp
SOURCES ${SOURCES}
)

target_link_libraries(glaciergalleryplugin PUBLIC
Expand Down
43 changes: 43 additions & 0 deletions src/plugin/filesystemworker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2025 Chupligin Sergey <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

#include "filesystemworker.h"

#include <QDir>
#include <QDirIterator>

FileSystemWorker::FileSystemWorker(QStringList dirList, QStringList suffixes, QObject *parent)
: QObject{parent}
, m_dirs(dirList)
, m_suffixes(suffixes)
, m_busy(false)
{
}

void FileSystemWorker::start()
{
m_busy = true;
foreach (const QString& dirString, m_dirs) {
QDirIterator it(dirString, m_suffixes, QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext()) {
emit foundFile(it.next());
}
}
m_busy = false;
}
45 changes: 45 additions & 0 deletions src/plugin/filesystemworker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2025 Chupligin Sergey <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

#ifndef FILESYSTEMWORKER_H
#define FILESYSTEMWORKER_H

#include <QObject>

class FileSystemWorker : public QObject
{
Q_OBJECT
public:
explicit FileSystemWorker(QStringList dirList, QStringList suffixes, QObject *parent = nullptr);

bool busy() {return m_busy;}

void start();
void stop();

signals:
void foundFile(QString path);

private:
QStringList m_dirs;
QStringList m_suffixes;
bool m_busy;
};

#endif // FILESYSTEMWORKER_H
76 changes: 62 additions & 14 deletions src/plugin/gallerymodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
*/

#include "gallerymodel.h"
#include "filesystemworker.h"

#include <QDir>
#include <QMimeDatabase>
#include <QStandardPaths>
#include <QDirIterator>
#include <QThread>

GalleryModel::GalleryModel(QObject* parent)
: QAbstractListModel { parent }
Expand Down Expand Up @@ -98,7 +102,7 @@ void GalleryModel::addPath(QString url)
}

if (url.isEmpty()) {
m_urls.append(QStandardPaths::standardLocations(QStandardPaths::PicturesLocation));
m_urls.append(QStandardPaths::standardLocations(QStandardPaths::HomeLocation));
emit urlsChanged();
}
}
Expand Down Expand Up @@ -131,7 +135,22 @@ void GalleryModel::formatFileList()

m_files.clear();

foreach (const QString& dirString, m_urls) {
QStringList suffixes;
foreach (const QMimeType mType, m_mimeTypes) {
foreach (QString suff, mType.suffixes()) {
suffixes << "*." + suff;
}
};

FileSystemWorker* work = new FileSystemWorker(m_urls, suffixes);
QThread* scanTread = new QThread;
connect(scanTread, &QThread::started, work, &FileSystemWorker::start);
connect(work, &FileSystemWorker::foundFile, this, &GalleryModel::appendFiles);

work->moveToThread(scanTread);
scanTread->start();

/*foreach (const QString& dirString, m_urls) {
QDir dir(dirString);
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks);
switch (m_sortMode) {
Expand All @@ -152,16 +171,26 @@ void GalleryModel::formatFileList()
break;
}
QFileInfoList filelistinfo = dir.entryInfoList();
foreach (const QFileInfo& fileinfo, filelistinfo) {
if (m_mimeTypes.contains(db.mimeTypeForFile(fileinfo.absoluteFilePath()).name())) {
m_files.append(fileinfo.absoluteFilePath());
}
/*QDirIterator it(dirString, suffixes, QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext()) {
qDebug() << it.next();
}
}
}*/
endResetModel();
}

void GalleryModel::appendFiles(QString path)
{
beginInsertRows(QModelIndex(), m_files.count(), m_files.count());
if(!m_files.contains(path)) {
m_files.push_back(path);
}
endInsertRows();
}

void GalleryModel::onFileSystemChanged(QString path)
{
qDebug() << Q_FUNC_INFO << path;
Expand All @@ -173,20 +202,19 @@ void GalleryModel::formatMimeTypes()
QList<QMimeType> mimeList = db.allMimeTypes();

m_mimeTypes.clear();
m_mimeTypes << "inode/directory";

for (const QMimeType& mime : std::as_const(mimeList)) {
if (m_filter == FilterMode::AllFiles) {
if (mime.name().startsWith(QStringLiteral("image/")) || mime.name().startsWith(QStringLiteral("video/"))) {
m_mimeTypes << mime.name();
m_mimeTypes << mime;
}
} else if (m_filter == FilterMode::OnlyImages) {
if (mime.name().startsWith(QStringLiteral("image/"))) {
m_mimeTypes << mime.name();
m_mimeTypes << mime;
}
} else if (m_filter == FilterMode::OnlyVideo) {
if (mime.name().startsWith(QStringLiteral("video/"))) {
m_mimeTypes << mime.name();
m_mimeTypes << mime;
}
}
}
Expand All @@ -208,13 +236,33 @@ void GalleryModel::setSortMode(const GalleryModel::SortMode& newSort)
formatFileList();
}

QString GalleryModel::sizeTotext(float size)
{
QStringList list;
list << tr("kb") << tr("mb") << tr("gb") << tr("tb");

QStringListIterator i(list);
QString unit("bytes");

while (size >= 1024.0 && i.hasNext()) {
unit = i.next();
size /= 1024.0;
}
return QString().setNum(size, 'f', 2) + " " + unit;
}

bool GalleryModel::isVideo(int index)
{
QMimeDatabase db;
if (index < 0 || index > m_files.count()) {
if (index < 0 || index >= m_files.count()) {
return false;
}
QFileInfo fileInfo(m_files.at(index));
QString url = m_files.at(index);
if(url.isEmpty()) {
return false;
}

QFileInfo fileInfo(url);
if (db.mimeTypeForFile(fileInfo.absoluteFilePath()).name().startsWith("video/")) {
return true;
}
Expand Down
5 changes: 4 additions & 1 deletion src/plugin/gallerymodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <QAbstractListModel>
#include <QFileSystemWatcher>
#include <QMimeType>

class GalleryModel : public QAbstractListModel {
Q_OBJECT
Expand Down Expand Up @@ -67,6 +68,7 @@ class GalleryModel : public QAbstractListModel {
GalleryModel::SortMode sortMode() const;
void setSortMode(const GalleryModel::SortMode& newSort);

Q_INVOKABLE QString sizeTotext(float size);
Q_INVOKABLE bool isVideo(int index);

signals:
Expand All @@ -81,6 +83,7 @@ private slots:
void onUrlsChanged();
void onFileSystemChanged(QString path);
void formatFileList();
void appendFiles(QString path);

private:
QHash<int, QByteArray> m_hash;
Expand All @@ -89,7 +92,7 @@ private slots:
GalleryModel::FilterMode m_filter;
GalleryModel::SortMode m_sortMode;

QStringList m_mimeTypes;
QList<QMimeType> m_mimeTypes;
QStringList m_urls;
QList<QString> m_files;

Expand Down

0 comments on commit 1492042

Please sign in to comment.