From 5e18359d6f32660c5de19f2ab18219e315192018 Mon Sep 17 00:00:00 2001 From: reionwong Date: Sat, 4 Sep 2021 04:35:54 +0800 Subject: [PATCH] Add basic CIO(Cutefish IO) --- CMakeLists.txt | 3 + cio/cfilejob.cpp | 7 ++ cio/cfilejob.h | 17 +++++ cio/cfilesizejob.cpp | 113 +++++++++++++++++++++++++++++++ cio/cfilesizejob.h | 55 +++++++++++++++ dialogs/filepropertiesdialog.cpp | 43 ++++++------ dialogs/filepropertiesdialog.h | 8 ++- dialogs/openwithdialog.cpp | 5 ++ 8 files changed, 227 insertions(+), 24 deletions(-) create mode 100644 cio/cfilejob.cpp create mode 100644 cio/cfilejob.h create mode 100644 cio/cfilesizejob.cpp create mode 100644 cio/cfilesizejob.h diff --git a/CMakeLists.txt b/CMakeLists.txt index cdb5175..f6ed1fc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,9 @@ add_executable(cutefish-filemanager model/dirlister.cpp model/positioner.cpp + cio/cfilejob.cpp + cio/cfilesizejob.cpp + dialogs/createfolderdialog.cpp dialogs/filepropertiesdialog.cpp dialogs/openwithdialog.cpp diff --git a/cio/cfilejob.cpp b/cio/cfilejob.cpp new file mode 100644 index 0000000..0ab6694 --- /dev/null +++ b/cio/cfilejob.cpp @@ -0,0 +1,7 @@ +#include "cfilejob.h" + +CFileJob::CFileJob(QObject *parent) + : QThread(parent) +{ + +} diff --git a/cio/cfilejob.h b/cio/cfilejob.h new file mode 100644 index 0000000..1481a98 --- /dev/null +++ b/cio/cfilejob.h @@ -0,0 +1,17 @@ +#ifndef CFILEJOB_H +#define CFILEJOB_H + +#include + +class CFileJob : public QThread +{ + Q_OBJECT + +public: + explicit CFileJob(QObject *parent = nullptr); + +signals: + +}; + +#endif // CFILEJOB_H diff --git a/cio/cfilesizejob.cpp b/cio/cfilesizejob.cpp new file mode 100644 index 0000000..be2c5bd --- /dev/null +++ b/cio/cfilesizejob.cpp @@ -0,0 +1,113 @@ +/* + * Copyright (C) 2021 CutefishOS Team. + * + * Author: Reion Wong + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "cfilesizejob.h" +#include +#include +#include +#include +#include +#include + +CFileSizeJob::CFileSizeJob(QObject *parent) + : QThread(parent) +{ + +} + +CFileSizeJob::~CFileSizeJob() +{ +} + +qint64 CFileSizeJob::totalSize() const +{ + return m_totalSize; +} + +void CFileSizeJob::start(const QList &urls) +{ + if (urls.isEmpty()) + return; + + m_urls = urls; + m_running = true; + + QThread::start(); +} + +void CFileSizeJob::stop() +{ + m_running = false; + + QThread::wait(); +} + +void CFileSizeJob::run() +{ + m_totalSize = 0; + m_filesCount = 0; + m_directoryCount = 0; + + for (QUrl &url : m_urls) { + if (!m_running) + return; + + QFileInfo i(url.toLocalFile()); + + if (i.filePath() == "/proc/kcore" || i.filePath() == "/dev/core") + continue; + + if (i.isSymLink() && i.symLinkTarget() == "/proc/kcore") + continue; + + if (i.isFile()) { + m_totalSize += i.size(); + m_filesCount++; + emit sizeChanged(); + } else if (i.isDir()) { + m_directoryCount++; + + QDirIterator it(url.toLocalFile(), QDir::AllEntries | QDir::Hidden | QDir::System | QDir::NoDotAndDotDot, QDirIterator::Subdirectories); + + while (it.hasNext()) { + if (!m_running) + return; + + QFileInfo info(it.next()); + + if (info.filePath() == "/proc/kcore" || info.filePath() == "/dev/core") + continue; + + if (info.isSymLink()) + continue; + + if (info.isFile()) + m_filesCount++; + else if (info.isDir()) + m_directoryCount++; + + m_totalSize += info.size(); + + emit sizeChanged(); + } + } + } + + emit result(); +} diff --git a/cio/cfilesizejob.h b/cio/cfilesizejob.h new file mode 100644 index 0000000..73d1295 --- /dev/null +++ b/cio/cfilesizejob.h @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2021 CutefishOS Team. + * + * Author: Reion Wong + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef CFILESIZEJOB_H +#define CFILESIZEJOB_H + +#include +#include +#include + +class CFileSizeJob : public QThread +{ + Q_OBJECT + +public: + explicit CFileSizeJob(QObject *parent = nullptr); + ~CFileSizeJob(); + + qint64 totalSize() const; + + void start(const QList &urls); + void stop(); + +signals: + void sizeChanged(); + void result(); + +protected: + void run() override; + +private: + bool m_running; + QList m_urls; + qint64 m_totalSize; + int m_filesCount; + int m_directoryCount; +}; + +#endif // CFILESIZEJOB_H diff --git a/dialogs/filepropertiesdialog.cpp b/dialogs/filepropertiesdialog.cpp index 78648d1..e181ec6 100644 --- a/dialogs/filepropertiesdialog.cpp +++ b/dialogs/filepropertiesdialog.cpp @@ -64,10 +64,10 @@ FilePropertiesDialog::FilePropertiesDialog(const QUrl &url, QQuickView *parent) FilePropertiesDialog::~FilePropertiesDialog() { - if (m_dirSizeJob) { - m_dirSizeJob->kill(); - m_dirSizeJob->deleteLater(); - m_dirSizeJob = nullptr; + if (m_sizeJob) { + m_sizeJob->stop(); + m_sizeJob->deleteLater(); + m_sizeJob = nullptr; } } @@ -107,6 +107,12 @@ void FilePropertiesDialog::accept(const QString &text) void FilePropertiesDialog::reject() { + if (m_sizeJob) { + m_sizeJob->stop(); + m_sizeJob->deleteLater(); + m_sizeJob = nullptr; + } + this->destroy(); this->deleteLater(); } @@ -181,17 +187,17 @@ void FilePropertiesDialog::init() setSource(QUrl("qrc:/qml/Dialogs/PropertiesDialog.qml")); m_multiple = m_items.count() > 1; - m_dirSizeJob = KIO::directorySize(m_items); - // Update - m_dirSizeUpdateTimer = new QTimer(this); - connect(m_dirSizeUpdateTimer, &QTimer::timeout, this, [=] { - m_size = KIO::convertSize(m_dirSizeJob->totalSize()); - emit fileSizeChanged(); - }); - m_dirSizeUpdateTimer->start(500); + QList list; + for (KFileItem item : m_items) { + list.append(item.url()); + } - connect(m_dirSizeJob, &KIO::DirectorySizeJob::result, this, &FilePropertiesDialog::slotDirSizeFinished); + m_sizeJob = std::shared_ptr(new CFileSizeJob); + m_sizeJob->start(list); + + connect(m_sizeJob.get(), &CFileSizeJob::sizeChanged, this, &FilePropertiesDialog::updateTotalSize); + connect(m_sizeJob.get(), &CFileSizeJob::result, this, &FilePropertiesDialog::updateTotalSize); if (!m_multiple) { KFileItem item = m_items.first(); @@ -236,16 +242,11 @@ void FilePropertiesDialog::init() emit isWritableChanged(); } -void FilePropertiesDialog::slotDirSizeFinished(KJob *job) +void FilePropertiesDialog::updateTotalSize() { - if (job->error()) + if (!m_sizeJob) return; - m_dirSizeUpdateTimer->stop(); - m_size = KIO::convertSize(m_dirSizeJob->totalSize()); - - m_dirSizeJob = 0; - + m_size = KIO::convertSize(m_sizeJob->totalSize()); emit fileSizeChanged(); } - diff --git a/dialogs/filepropertiesdialog.h b/dialogs/filepropertiesdialog.h index aff09b1..809f695 100644 --- a/dialogs/filepropertiesdialog.h +++ b/dialogs/filepropertiesdialog.h @@ -23,10 +23,13 @@ #include #include #include +#include #include #include +#include "cio/cfilesizejob.h" + class FilePropertiesDialog : public QQuickView { Q_OBJECT @@ -82,7 +85,7 @@ private: void init(); private slots: - void slotDirSizeFinished(KJob *job); + void updateTotalSize(); private: KFileItemList m_items; @@ -95,8 +98,7 @@ private: QString m_modifiedTime; QString m_accessedTime; - QTimer *m_dirSizeUpdateTimer = nullptr; - KIO::DirectorySizeJob *m_dirSizeJob; + std::shared_ptr m_sizeJob; bool m_multiple; bool m_isWritable; diff --git a/dialogs/openwithdialog.cpp b/dialogs/openwithdialog.cpp index 4e8cef4..1231de3 100644 --- a/dialogs/openwithdialog.cpp +++ b/dialogs/openwithdialog.cpp @@ -41,6 +41,11 @@ OpenWithDialog::OpenWithDialog(const QUrl &url, QQuickView *parent) QRect rect = geometry(); setMinimumSize(rect.size()); setMaximumSize(rect.size()); + + connect(this, &QQuickView::visibleChanged, this, [=] { + if (!this->isVisible()) + this->deleteLater(); + }); } QString OpenWithDialog::url() const