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
This commit is contained in:
Albert Vaca Cintora 2022-03-03 22:41:18 +01:00
parent 531418c7d2
commit 0c93f8a1d0
2 changed files with 20 additions and 3 deletions

View file

@ -78,6 +78,15 @@ void SharePlugin::setDateModified(const QUrl& destination, const qint64 timestam
receivedFile.setFileTime(QDateTime::fromMSecsSinceEpoch(timestamp), QFileDevice::FileTime(QFileDevice::FileModificationTime)); 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) bool SharePlugin::receivePacket(const NetworkPacket& np)
{ {
/* /*
@ -107,6 +116,7 @@ bool SharePlugin::receivePacket(const NetworkPacket& np)
QUrl destination = getFileDestination(filename); QUrl destination = getFileDestination(filename);
if (np.hasPayload()) { if (np.hasPayload()) {
qint64 dateCreated = np.get<qint64>(QStringLiteral("creationTime"), QDateTime::currentMSecsSinceEpoch());
qint64 dateModified = np.get<qint64>(QStringLiteral("lastModified"), QDateTime::currentMSecsSinceEpoch()); qint64 dateModified = np.get<qint64>(QStringLiteral("lastModified"), QDateTime::currentMSecsSinceEpoch());
const bool open = np.get<bool>(QStringLiteral("open"), false); const bool open = np.get<bool>(QStringLiteral("open"), false);
@ -119,7 +129,9 @@ bool SharePlugin::receivePacket(const NetworkPacket& np)
FileTransferJob* job = np.createPayloadTransferJob(destination); FileTransferJob* job = np.createPayloadTransferJob(destination);
job->setOriginName(device()->name() + QStringLiteral(": ") + filename); 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); m_compositeJob->addSubjob(job);
if (!m_compositeJob->isRunning()) { if (!m_compositeJob->isRunning()) {
@ -165,11 +177,12 @@ bool SharePlugin::receivePacket(const NetworkPacket& np)
return true; 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<FileTransferJob*>(job); FileTransferJob* ftjob = qobject_cast<FileTransferJob*>(job);
if (ftjob && !job->error()) { if (ftjob && !job->error()) {
Q_EMIT shareReceived(ftjob->destination().toString()); Q_EMIT shareReceived(ftjob->destination().toString());
setDateCreated(ftjob->destination(), dateCreated);
setDateModified(ftjob->destination(), dateModified); setDateModified(ftjob->destination(), dateModified);
qCDebug(KDECONNECT_PLUGIN_SHARE) << "File transfer finished." << ftjob->destination(); qCDebug(KDECONNECT_PLUGIN_SHARE) << "File transfer finished." << ftjob->destination();
if (open) { if (open) {
@ -190,6 +203,7 @@ void SharePlugin::shareUrl(const QUrl& url, bool open)
NetworkPacket packet(PACKET_TYPE_SHARE_REQUEST); NetworkPacket packet(PACKET_TYPE_SHARE_REQUEST);
if (url.isLocalFile()) { if (url.isLocalFile()) {
QSharedPointer<QFile> ioFile(new QFile(url.toLocalFile())); QSharedPointer<QFile> ioFile(new QFile(url.toLocalFile()));
QFileInfo info(*ioFile);
if (!ioFile->exists()) { if (!ioFile->exists()) {
Daemon::instance()->reportError(i18n("Could not share file"), i18n("%1 does not exist", url.toLocalFile())); 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 { } else {
packet.setPayload(ioFile, ioFile->size()); packet.setPayload(ioFile, ioFile->size());
packet.set<QString>(QStringLiteral("filename"), QUrl(url).fileName()); packet.set<QString>(QStringLiteral("filename"), QUrl(url).fileName());
packet.set<qint64>(QStringLiteral("creationTime"), info.birthTime().toMSecsSinceEpoch());
packet.set<qint64>(QStringLiteral("lastModified"), info.lastModified().toMSecsSinceEpoch());
packet.set<bool>(QStringLiteral("open"), open); packet.set<bool>(QStringLiteral("open"), open);
} }
} else { } else {

View file

@ -41,11 +41,12 @@ Q_SIGNALS:
Q_SCRIPTABLE void shareReceived(const QString& url); Q_SCRIPTABLE void shareReceived(const QString& url);
private: 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); void shareUrl(const QUrl& url, bool open);
QUrl destinationDir() const; QUrl destinationDir() const;
QUrl getFileDestination(const QString filename) const; QUrl getFileDestination(const QString filename) const;
void setDateModified(const QUrl& destination, const qint64 timestamp); void setDateModified(const QUrl& destination, const qint64 timestamp);
void setDateCreated(const QUrl& destination, const qint64 timestamp);
QPointer<CompositeFileTransferJob> m_compositeJob; QPointer<CompositeFileTransferJob> m_compositeJob;
}; };