From 58adbc8cb4440df87cb8e53cd982b1ab28f34ed8 Mon Sep 17 00:00:00 2001 From: Albert Vaca Cintora Date: Thu, 3 Mar 2022 22:41:18 +0100 Subject: [PATCH] SharePlugin: Read and write shared file creation time When sharing a file, add metadata about the file's creation time and last modified time. When receiving a shared file, read those fields and set them on the file we create. Note: Qt doesn't support setting the creation date on Unix [1]. [1] https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/io/qfilesystemengine_unix.cpp?h=v5.15.2&id=40143c189b7c1bf3c2058b77d00ea5c4e3be8b28#n1590 --- plugins/share/shareplugin.cpp | 20 ++++++++++++++++++-- plugins/share/shareplugin.h | 3 ++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/plugins/share/shareplugin.cpp b/plugins/share/shareplugin.cpp index fb25bdf69..9d2addd4b 100644 --- a/plugins/share/shareplugin.cpp +++ b/plugins/share/shareplugin.cpp @@ -78,6 +78,15 @@ void SharePlugin::setDateModified(const QUrl& destination, const qint64 timestam receivedFile.setFileTime(QDateTime::fromMSecsSinceEpoch(timestamp), QFileDevice::FileTime(QFileDevice::FileModificationTime)); } +void SharePlugin::setDateCreated(const QUrl& destination, const qint64 timestamp) +{ + QFile receivedFile(destination.toLocalFile()); + if (!receivedFile.exists() || !receivedFile.open(QIODevice::ReadWrite | QIODevice::Text)) { + return; + } + receivedFile.setFileTime(QDateTime::fromMSecsSinceEpoch(timestamp), QFileDevice::FileTime(QFileDevice::FileBirthTime)); +} + bool SharePlugin::receivePacket(const NetworkPacket& np) { /* @@ -107,6 +116,7 @@ bool SharePlugin::receivePacket(const NetworkPacket& np) QUrl destination = getFileDestination(filename); if (np.hasPayload()) { + qint64 dateCreated = np.get(QStringLiteral("creationTime"), QDateTime::currentMSecsSinceEpoch()); qint64 dateModified = np.get(QStringLiteral("lastModified"), QDateTime::currentMSecsSinceEpoch()); const bool open = np.get(QStringLiteral("open"), false); @@ -119,7 +129,9 @@ bool SharePlugin::receivePacket(const NetworkPacket& np) FileTransferJob* job = np.createPayloadTransferJob(destination); job->setOriginName(device()->name() + QStringLiteral(": ") + filename); - connect(job, &KJob::result, this, [this, dateModified, open] (KJob* job) -> void { finished(job, dateModified, open); }); + connect(job, &KJob::result, this, [this, dateCreated, dateModified, open] (KJob* job) -> void { + finished(job, dateCreated, dateModified, open); + }); m_compositeJob->addSubjob(job); if (!m_compositeJob->isRunning()) { @@ -165,11 +177,12 @@ bool SharePlugin::receivePacket(const NetworkPacket& np) return true; } -void SharePlugin::finished(KJob* job, const qint64 dateModified, const bool open) +void SharePlugin::finished(KJob* job, const qint64 dateModified, const qint64 dateCreated, const bool open) { FileTransferJob* ftjob = qobject_cast(job); if (ftjob && !job->error()) { Q_EMIT shareReceived(ftjob->destination().toString()); + setDateCreated(ftjob->destination(), dateCreated); setDateModified(ftjob->destination(), dateModified); qCDebug(KDECONNECT_PLUGIN_SHARE) << "File transfer finished." << ftjob->destination(); if (open) { @@ -190,6 +203,7 @@ void SharePlugin::shareUrl(const QUrl& url, bool open) NetworkPacket packet(PACKET_TYPE_SHARE_REQUEST); if (url.isLocalFile()) { QSharedPointer ioFile(new QFile(url.toLocalFile())); + QFileInfo info(*ioFile); if (!ioFile->exists()) { Daemon::instance()->reportError(i18n("Could not share file"), i18n("%1 does not exist", url.toLocalFile())); @@ -197,6 +211,8 @@ void SharePlugin::shareUrl(const QUrl& url, bool open) } else { packet.setPayload(ioFile, ioFile->size()); packet.set(QStringLiteral("filename"), QUrl(url).fileName()); + packet.set(QStringLiteral("creationTime"), info.birthTime().toMSecsSinceEpoch()); + packet.set(QStringLiteral("lastModified"), info.lastModified().toMSecsSinceEpoch()); packet.set(QStringLiteral("open"), open); } } else { diff --git a/plugins/share/shareplugin.h b/plugins/share/shareplugin.h index 88f88d758..c697b60a7 100644 --- a/plugins/share/shareplugin.h +++ b/plugins/share/shareplugin.h @@ -41,11 +41,12 @@ Q_SIGNALS: Q_SCRIPTABLE void shareReceived(const QString& url); private: - void finished(KJob* job, const qint64 dateModified, const bool open); + void finished(KJob* job, const qint64 dateCreated, const qint64 dateModified, const bool open); void shareUrl(const QUrl& url, bool open); QUrl destinationDir() const; QUrl getFileDestination(const QString filename) const; void setDateModified(const QUrl& destination, const qint64 timestamp); + void setDateCreated(const QUrl& destination, const qint64 timestamp); QPointer m_compositeJob; };