2021-03-29 01:51:34 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2021 CutefishOS Team.
|
|
|
|
*
|
2021-07-29 01:17:03 -07:00
|
|
|
* Author: Reion Wong <reionwong@gmail.com>
|
2021-03-29 01:51:34 -07:00
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
#include "filepropertiesdialog.h"
|
2021-03-29 01:51:34 -07:00
|
|
|
#include "../desktopiconprovider.h"
|
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
#include <QQmlContext>
|
|
|
|
#include <QQmlEngine>
|
2021-03-16 00:02:20 -07:00
|
|
|
#include <QFileInfo>
|
2021-07-29 01:17:03 -07:00
|
|
|
#include <QDir>
|
2021-03-16 00:02:20 -07:00
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
#include <KIO/CopyJob>
|
2021-03-16 00:02:20 -07:00
|
|
|
|
2021-03-29 01:51:34 -07:00
|
|
|
inline QString concatPaths(const QString &path1, const QString &path2)
|
|
|
|
{
|
|
|
|
Q_ASSERT(!path2.startsWith(QLatin1Char('/')));
|
|
|
|
|
|
|
|
if (path1.isEmpty()) {
|
|
|
|
return path2;
|
|
|
|
} else if (!path1.endsWith(QLatin1Char('/'))) {
|
|
|
|
return path1 + QLatin1Char('/') + path2;
|
|
|
|
} else {
|
|
|
|
return path1 + path2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
FilePropertiesDialog::FilePropertiesDialog(const KFileItem &item, QQuickView *parent)
|
|
|
|
: QQuickView(parent)
|
2021-03-16 00:02:20 -07:00
|
|
|
{
|
|
|
|
m_items.append(item);
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
FilePropertiesDialog::FilePropertiesDialog(const KFileItemList &items, QQuickView *parent)
|
|
|
|
: QQuickView(parent)
|
|
|
|
, m_items(items)
|
2021-03-16 00:02:20 -07:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
FilePropertiesDialog::FilePropertiesDialog(const QUrl &url, QQuickView *parent)
|
|
|
|
: QQuickView(parent)
|
|
|
|
, m_items { KFileItem(url) }
|
2021-03-16 00:02:20 -07:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
FilePropertiesDialog::~FilePropertiesDialog()
|
2021-03-16 00:02:20 -07:00
|
|
|
{
|
2021-07-29 01:17:03 -07:00
|
|
|
if (m_dirSizeJob) {
|
2021-03-16 00:02:20 -07:00
|
|
|
m_dirSizeJob->kill();
|
2021-07-29 01:17:03 -07:00
|
|
|
m_dirSizeJob->deleteLater();
|
|
|
|
m_dirSizeJob = nullptr;
|
|
|
|
}
|
2021-03-16 00:02:20 -07:00
|
|
|
}
|
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
void FilePropertiesDialog::accept(const QString &text)
|
2021-03-16 00:02:20 -07:00
|
|
|
{
|
2021-07-29 01:17:03 -07:00
|
|
|
KFileItemList list = m_items;
|
|
|
|
|
|
|
|
if (list.size() == 1) {
|
|
|
|
KFileItem item = list.first();
|
|
|
|
|
|
|
|
QString n = text;
|
|
|
|
while (!n.isEmpty() && n[n.length() - 1].isSpace())
|
|
|
|
n.chop(1);
|
|
|
|
|
|
|
|
if (n.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
QString newFileName = KIO::encodeFileName(n);
|
|
|
|
|
|
|
|
if (fileName() != newFileName) {
|
|
|
|
QUrl newUrl;
|
|
|
|
|
|
|
|
if (!location().isEmpty()) {
|
|
|
|
newUrl = location();
|
|
|
|
newUrl.setPath(concatPaths(newUrl.path(), newFileName));
|
|
|
|
newUrl.setScheme(item.url().scheme());
|
|
|
|
|
|
|
|
auto job = KIO::move(item.url(), newUrl, KIO::HideProgressInfo);
|
|
|
|
job->start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this->destroy();
|
|
|
|
this->deleteLater();
|
2021-03-16 00:02:20 -07:00
|
|
|
}
|
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
void FilePropertiesDialog::reject()
|
2021-03-29 01:51:34 -07:00
|
|
|
{
|
2021-07-29 01:17:03 -07:00
|
|
|
this->destroy();
|
|
|
|
this->deleteLater();
|
2021-03-16 00:02:20 -07:00
|
|
|
}
|
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
bool FilePropertiesDialog::multiple() const
|
2021-03-16 00:02:20 -07:00
|
|
|
{
|
|
|
|
return m_multiple;
|
|
|
|
}
|
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
bool FilePropertiesDialog::isWritable() const
|
2021-03-29 01:51:34 -07:00
|
|
|
{
|
2021-07-29 01:17:03 -07:00
|
|
|
return m_isWritable;
|
2021-03-29 01:51:34 -07:00
|
|
|
}
|
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
QString FilePropertiesDialog::location() const
|
2021-03-16 00:02:20 -07:00
|
|
|
{
|
|
|
|
return m_location;
|
|
|
|
}
|
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
QString FilePropertiesDialog::fileName() const
|
2021-03-16 00:02:20 -07:00
|
|
|
{
|
|
|
|
return m_fileName;
|
|
|
|
}
|
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
QString FilePropertiesDialog::iconName() const
|
2021-03-16 00:02:20 -07:00
|
|
|
{
|
|
|
|
return m_iconName;
|
|
|
|
}
|
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
QString FilePropertiesDialog::mimeType() const
|
2021-03-16 00:02:20 -07:00
|
|
|
{
|
|
|
|
return m_mimeType;
|
|
|
|
}
|
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
QString FilePropertiesDialog::fileSize() const
|
2021-03-16 00:02:20 -07:00
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
QString FilePropertiesDialog::creationTime() const
|
2021-03-16 00:02:20 -07:00
|
|
|
{
|
|
|
|
return m_creationTime;
|
|
|
|
}
|
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
QString FilePropertiesDialog::modifiedTime() const
|
2021-03-16 00:02:20 -07:00
|
|
|
{
|
|
|
|
return m_modifiedTime;
|
|
|
|
}
|
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
QString FilePropertiesDialog::accessedTime() const
|
2021-03-16 00:02:20 -07:00
|
|
|
{
|
|
|
|
return m_accessedTime;
|
|
|
|
}
|
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
bool FilePropertiesDialog::event(QEvent *e)
|
2021-03-29 01:51:34 -07:00
|
|
|
{
|
2021-07-29 01:17:03 -07:00
|
|
|
if (e->type() == QEvent::Close) {
|
|
|
|
this->deleteLater();
|
2021-03-29 01:51:34 -07:00
|
|
|
}
|
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
return QQuickView::event(e);
|
2021-03-29 01:51:34 -07:00
|
|
|
}
|
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
void FilePropertiesDialog::init()
|
2021-03-16 00:02:20 -07:00
|
|
|
{
|
2021-07-29 01:17:03 -07:00
|
|
|
engine()->rootContext()->setContextProperty("main", this);
|
2021-08-17 00:58:29 -07:00
|
|
|
// engine()->addImageProvider(QStringLiteral("icontheme"), new DesktopIconProvider());
|
2021-07-29 01:17:03 -07:00
|
|
|
|
|
|
|
setFlag(Qt::Dialog);
|
|
|
|
setTitle(tr("Properties"));
|
|
|
|
setResizeMode(QQuickView::SizeViewToRootObject);
|
|
|
|
setSource(QUrl("qrc:/qml/Dialogs/PropertiesDialog.qml"));
|
2021-03-16 00:02:20 -07:00
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
m_multiple = m_items.count() > 1;
|
2021-03-16 00:02:20 -07:00
|
|
|
m_dirSizeJob = KIO::directorySize(m_items);
|
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
// 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);
|
|
|
|
|
|
|
|
connect(m_dirSizeJob, &KIO::DirectorySizeJob::result, this, &FilePropertiesDialog::slotDirSizeFinished);
|
2021-03-16 00:02:20 -07:00
|
|
|
|
|
|
|
if (!m_multiple) {
|
|
|
|
KFileItem item = m_items.first();
|
|
|
|
|
|
|
|
QString path;
|
|
|
|
m_fileName = m_items.first().name();
|
|
|
|
|
|
|
|
if (item.isDir())
|
|
|
|
m_iconName = "folder";
|
|
|
|
else
|
|
|
|
m_iconName = m_items.first().iconName();
|
|
|
|
|
|
|
|
m_mimeType = m_items.first().mimetype();
|
|
|
|
m_size = KIO::convertSize(m_items.first().size());
|
|
|
|
m_location = QFileInfo(m_items.first().localPath()).dir().path();
|
|
|
|
|
|
|
|
m_creationTime = item.time(KFileItem::CreationTime).toString();
|
|
|
|
m_modifiedTime = item.time(KFileItem::ModificationTime).toString();
|
|
|
|
m_accessedTime = item.time(KFileItem::AccessTime).toString();
|
2021-07-29 01:17:03 -07:00
|
|
|
|
|
|
|
m_isWritable = m_items.first().isWritable();
|
|
|
|
|
|
|
|
emit fileNameChanged();
|
|
|
|
emit iconNameChanged();
|
|
|
|
emit mimeTypeChanged();
|
|
|
|
emit fileSizeChanged();
|
|
|
|
emit locationChanged();
|
|
|
|
emit creationTimeChanged();
|
|
|
|
emit modifiedTimeChanged();
|
|
|
|
emit accessedTimeChanged();
|
2021-03-16 00:02:20 -07:00
|
|
|
} else {
|
2021-07-29 01:17:03 -07:00
|
|
|
m_isWritable = false;
|
2021-03-29 01:51:34 -07:00
|
|
|
m_fileName = tr("%1 files").arg(m_items.count());
|
2021-03-16 00:02:20 -07:00
|
|
|
m_location = QFileInfo(m_items.first().localPath()).dir().path();
|
2021-08-17 00:58:29 -07:00
|
|
|
m_iconName = "unknown";
|
2021-07-29 01:17:03 -07:00
|
|
|
|
|
|
|
emit fileNameChanged();
|
|
|
|
emit locationChanged();
|
2021-08-17 00:58:29 -07:00
|
|
|
emit iconNameChanged();
|
2021-03-16 00:02:20 -07:00
|
|
|
}
|
2021-07-29 01:17:03 -07:00
|
|
|
|
|
|
|
emit isWritableChanged();
|
2021-03-16 00:02:20 -07:00
|
|
|
}
|
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
void FilePropertiesDialog::slotDirSizeFinished(KJob *job)
|
2021-03-16 00:02:20 -07:00
|
|
|
{
|
|
|
|
if (job->error())
|
|
|
|
return;
|
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
m_dirSizeUpdateTimer->stop();
|
2021-03-16 00:02:20 -07:00
|
|
|
m_size = KIO::convertSize(m_dirSizeJob->totalSize());
|
|
|
|
|
|
|
|
m_dirSizeJob = 0;
|
|
|
|
|
2021-07-29 01:17:03 -07:00
|
|
|
emit fileSizeChanged();
|
2021-03-16 00:02:20 -07:00
|
|
|
}
|
2021-07-29 01:17:03 -07:00
|
|
|
|