filemanager/dialogs/filepropertiesdialog.cpp

266 lines
6.5 KiB
C++
Raw Permalink Normal View History

2021-03-29 01:51:34 -07:00
/*
* Copyright (C) 2021 CutefishOS Team.
*
* 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/>.
*/
#include "filepropertiesdialog.h"
2021-03-29 01:51:34 -07:00
#include "../desktopiconprovider.h"
2022-01-16 11:08:30 -08:00
#include "../helper/fm.h"
2021-03-29 01:51:34 -07:00
#include <QQmlContext>
#include <QQmlEngine>
2021-03-16 00:02:20 -07:00
#include <QFileInfo>
#include <QDir>
2021-03-16 00:02:20 -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;
}
}
FilePropertiesDialog::FilePropertiesDialog(const KFileItem &item, QQuickView *parent)
: QQuickView(parent)
2021-03-16 00:02:20 -07:00
{
m_items.append(item);
init();
}
FilePropertiesDialog::FilePropertiesDialog(const KFileItemList &items, QQuickView *parent)
: QQuickView(parent)
, m_items(items)
2021-03-16 00:02:20 -07:00
{
init();
}
FilePropertiesDialog::FilePropertiesDialog(const QUrl &url, QQuickView *parent)
: QQuickView(parent)
2021-03-16 00:02:20 -07:00
{
2021-08-30 09:17:39 -07:00
m_items.append(KFileItem(url));
2021-03-16 00:02:20 -07:00
init();
}
FilePropertiesDialog::~FilePropertiesDialog()
2021-03-16 00:02:20 -07:00
{
2021-09-03 13:35:54 -07:00
if (m_sizeJob) {
m_sizeJob->stop();
m_sizeJob->deleteLater();
m_sizeJob = nullptr;
}
2021-03-16 00:02:20 -07:00
}
2021-09-20 07:13:42 -07:00
void FilePropertiesDialog::updateSize(int width, int height)
{
2021-09-20 08:29:48 -07:00
resize(QSize(width, height));
2021-09-20 07:13:42 -07:00
setMinimumSize(QSize(width, height));
setMaximumSize(QSize(width, height));
}
void FilePropertiesDialog::accept(const QString &text)
2021-03-16 00:02:20 -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;
2022-01-16 11:08:30 -08:00
if (!location().isEmpty() && !Fm::isFixedFolder(m_items.first().url())) {
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
}
void FilePropertiesDialog::reject()
2021-03-29 01:51:34 -07:00
{
2021-09-03 13:35:54 -07:00
if (m_sizeJob) {
m_sizeJob->stop();
m_sizeJob->deleteLater();
m_sizeJob = nullptr;
}
this->destroy();
this->deleteLater();
2021-03-16 00:02:20 -07:00
}
bool FilePropertiesDialog::multiple() const
2021-03-16 00:02:20 -07:00
{
return m_multiple;
}
bool FilePropertiesDialog::isWritable() const
2021-03-29 01:51:34 -07:00
{
return m_isWritable;
2021-03-29 01:51:34 -07:00
}
QString FilePropertiesDialog::location() const
2021-03-16 00:02:20 -07:00
{
return m_location;
}
QString FilePropertiesDialog::fileName() const
2021-03-16 00:02:20 -07:00
{
return m_fileName;
}
QString FilePropertiesDialog::iconName() const
2021-03-16 00:02:20 -07:00
{
return m_iconName;
}
QString FilePropertiesDialog::mimeType() const
2021-03-16 00:02:20 -07:00
{
return m_mimeType;
}
QString FilePropertiesDialog::fileSize() const
2021-03-16 00:02:20 -07:00
{
return m_size;
}
QString FilePropertiesDialog::creationTime() const
2021-03-16 00:02:20 -07:00
{
return m_creationTime;
}
QString FilePropertiesDialog::modifiedTime() const
2021-03-16 00:02:20 -07:00
{
return m_modifiedTime;
}
QString FilePropertiesDialog::accessedTime() const
2021-03-16 00:02:20 -07:00
{
return m_accessedTime;
}
bool FilePropertiesDialog::event(QEvent *e)
2021-03-29 01:51:34 -07:00
{
if (e->type() == QEvent::Close) {
this->deleteLater();
2021-03-29 01:51:34 -07:00
}
return QQuickView::event(e);
2021-03-29 01:51:34 -07:00
}
void FilePropertiesDialog::init()
2021-03-16 00:02:20 -07:00
{
engine()->rootContext()->setContextProperty("main", this);
2021-08-17 00:58:29 -07:00
// engine()->addImageProvider(QStringLiteral("icontheme"), new DesktopIconProvider());
setFlag(Qt::Dialog);
setTitle(tr("Properties"));
setResizeMode(QQuickView::SizeViewToRootObject);
setSource(QUrl("qrc:/qml/Dialogs/PropertiesDialog.qml"));
2021-03-16 00:02:20 -07:00
m_multiple = m_items.count() > 1;
2021-03-16 00:02:20 -07:00
2021-09-03 13:35:54 -07:00
QList<QUrl> list;
for (KFileItem item : m_items) {
list.append(item.url());
}
2021-09-03 13:35:54 -07:00
m_sizeJob = std::shared_ptr<CFileSizeJob>(new CFileSizeJob);
m_sizeJob->start(list);
connect(m_sizeJob.get(), &CFileSizeJob::sizeChanged, this, &FilePropertiesDialog::updateTotalSize);
connect(m_sizeJob.get(), &CFileSizeJob::result, this, &FilePropertiesDialog::updateTotalSize);
2021-03-16 00:02:20 -07:00
if (!m_multiple) {
KFileItem item = m_items.first();
2021-09-15 00:46:38 -07:00
QFileInfo info(item.url().toLocalFile());
2021-03-16 00:02:20 -07:00
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());
2021-09-15 00:46:38 -07:00
m_location = info.dir().path();
m_creationTime = info.birthTime().toString(Qt::SystemLocaleLongDate);
m_modifiedTime = info.lastModified().toString(Qt::SystemLocaleLongDate);
m_accessedTime = info.lastRead().toString(Qt::SystemLocaleLongDate);
2021-03-16 00:02:20 -07:00
2021-09-15 00:46:38 -07:00
// m_creationTime = item.time(KFileItem::CreationTime).toString();
// m_modifiedTime = item.time(KFileItem::ModificationTime).toString();
// m_accessedTime = item.time(KFileItem::AccessTime).toString();
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 {
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";
emit fileNameChanged();
emit locationChanged();
2021-08-17 00:58:29 -07:00
emit iconNameChanged();
2021-03-16 00:02:20 -07:00
}
emit isWritableChanged();
2021-03-16 00:02:20 -07:00
}
2021-09-03 13:35:54 -07:00
void FilePropertiesDialog::updateTotalSize()
2021-03-16 00:02:20 -07:00
{
2021-09-03 13:35:54 -07:00
if (!m_sizeJob)
2021-03-16 00:02:20 -07:00
return;
2021-09-03 13:35:54 -07:00
m_size = KIO::convertSize(m_sizeJob->totalSize());
emit fileSizeChanged();
2021-03-16 00:02:20 -07:00
}