Commit 58adbc8c authored by Albert Vaca Cintora's avatar Albert Vaca Cintora
Browse files

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
parent 05e3fd29
......@@ -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<qint64>(QStringLiteral("creationTime"), QDateTime::currentMSecsSinceEpoch());
qint64 dateModified = np.get<qint64>(QStringLiteral("lastModified"), QDateTime::currentMSecsSinceEpoch());
const bool open = np.get<bool>(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<FileTransferJob*>(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<QFile> 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<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);
}
} else {
......
......@@ -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<CompositeFileTransferJob> m_compositeJob;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment