Merge pull request #3237 from martinvanzijl/allow-delete-file-on-linux

Enable sending files to Trash in Browser on Linux
This commit is contained in:
Rodney 2020-04-16 00:00:25 -05:00 committed by GitHub
commit ba9cee0d0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -59,7 +59,11 @@
#include <dlfcn.h>
#include <utime.h>
#include <sys/time.h>
#include <QDir>
#include <QFileInfo>
#include <QStorageInfo>
#include <QTextStream>
#include <QUrl>
#endif
#if defined(MACOSX)
@ -452,7 +456,55 @@ void TSystem::moveFileToRecycleBin(const TFilePath &fp) {
} catch (...) {
}
}
#elif defined(LINUX)
//
// From https://stackoverflow.com/questions/17964439/move-files-to-trash-recycle-bin-in-qt
//
QString fileToRecycle = fp.getQString();
QFileInfo FileName(fileToRecycle);
QDateTime currentTime(QDateTime::currentDateTime()); // get system time
// check if the file is on the local drive
const QStorageInfo fileStorageInfo(fileToRecycle);
const QStorageInfo homeStorageInfo(QDir::homePath());
const bool isOnHomeDrive = fileStorageInfo == homeStorageInfo;
QString trashFilePath = QDir::homePath() + "/.local/share/Trash/files/"; // this folder contains deleted files
QString trashInfoPath = QDir::homePath() + "/.local/share/Trash/info/"; // this folder contains information about the deleted files
// different paths are used for external drives
if (!isOnHomeDrive) {
//trashFilePath = fileStorageInfo.rootPath() + "/.Trash-1000/files/";
//trashInfoPath = fileStorageInfo.rootPath() + "/.Trash-1000/info/";
// TODO: Implement this... The standard is /.Trash-<UID>/...
outputDebug("Deleting files on external drives in Linux is not implemented yet.");
return;
}
// check paths exist
if( !QDir(trashFilePath).exists() || !QDir(trashInfoPath).exists() ) {
outputDebug("Could not find the right paths to send the file to the recycle bin.");
return;
}
// create file for the "Trash/info" folder
QFile infoFile(trashInfoPath + FileName.completeBaseName() + "." + FileName.completeSuffix() + ".trashinfo"); // filename+extension+.trashinfo
infoFile.open(QIODevice::ReadWrite);
QTextStream stream(&infoFile);
stream << "[Trash Info]" << endl;
stream << "Path=" + QString(QUrl::toPercentEncoding(FileName.absoluteFilePath(), "~_-./")) << endl; // convert path to percentage encoded string
stream << "DeletionDate=" + currentTime.toString("yyyy-MM-dd") + "T" + currentTime.toString("hh:mm:ss") << endl; // get date and time in format YYYY-MM-DDThh:mm:ss
infoFile.close();
// move the original file to the "Trash/files" folder
QDir file;
file.rename(FileName.absoluteFilePath(), trashFilePath+FileName.completeBaseName() + "." + FileName.completeSuffix()); // rename(original path, trash path)
#else
assert(!"Not implemented yet");
#endif