Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#9001 - Move files to trash instead of deleting them #10154

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 0 additions & 75 deletions src/common/filesystembase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,81 +379,6 @@ bool FileSystem::remove(const QString &fileName, QString *errorString)
return true;
}

bool FileSystem::moveToTrash(const QString &fileName, QString *errorString)
{
// TODO: Qt 5.15 bool QFile::moveToTrash()
#if defined Q_OS_UNIX && !defined Q_OS_MAC
QString trashPath, trashFilePath, trashInfoPath;
QString xdgDataHome = QFile::decodeName(qgetenv("XDG_DATA_HOME"));
if (xdgDataHome.isEmpty()) {
trashPath = QDir::homePath() + QStringLiteral("/.local/share/Trash/"); // trash path that should exist
} else {
trashPath = xdgDataHome + QStringLiteral("/Trash/");
}

trashFilePath = trashPath + QStringLiteral("files/"); // trash file path contain delete files
trashInfoPath = trashPath + QStringLiteral("info/"); // trash info path contain delete files information

if (!(QDir().mkpath(trashFilePath) && QDir().mkpath(trashInfoPath))) {
*errorString = QCoreApplication::translate("FileSystem", "Could not make directories in trash");
return false; //mkpath will return true if path exists
}

QFileInfo f(fileName);

QDir file;
int suffix_number = 1;
if (file.exists(trashFilePath + f.fileName())) { //file in trash already exists, move to "filename.1"
QString path = trashFilePath + f.fileName() + QLatin1Char('.');
while (file.exists(path + QString::number(suffix_number))) { //or to "filename.2" if "filename.1" exists, etc
suffix_number++;
}
if (!file.rename(f.absoluteFilePath(), path + QString::number(suffix_number))) { // rename(file old path, file trash path)
*errorString = QCoreApplication::translate("FileSystem", "Could not move '%1' to '%2'")
.arg(f.absoluteFilePath(), path + QString::number(suffix_number));
return false;
}
} else {
if (!file.rename(f.absoluteFilePath(), trashFilePath + f.fileName())) { // rename(file old path, file trash path)
*errorString = QCoreApplication::translate("FileSystem", "Could not move '%1' to '%2'")
.arg(f.absoluteFilePath(), trashFilePath + f.fileName());
return false;
}
}

// create file format for trash info file----- START
QFile infoFile;
if (file.exists(trashInfoPath + f.fileName() + QStringLiteral(".trashinfo"))) { //TrashInfo file already exists, create "filename.1.trashinfo"
QString filename = trashInfoPath + f.fileName() + QLatin1Char('.') + QString::number(suffix_number) + QStringLiteral(".trashinfo");
infoFile.setFileName(filename); //filename+.trashinfo // create file information file in /.local/share/Trash/info/ folder
} else {
QString filename = trashInfoPath + f.fileName() + QStringLiteral(".trashinfo");
infoFile.setFileName(filename); //filename+.trashinfo // create file information file in /.local/share/Trash/info/ folder
}

infoFile.open(QIODevice::ReadWrite);

QTextStream stream(&infoFile); // for write data on open file

stream << "[Trash Info]\n"
<< "Path="
<< QUrl::toPercentEncoding(f.absoluteFilePath(), "~_-./")
<< "\n"
<< "DeletionDate="
<< QDateTime::currentDateTime().toString(Qt::ISODate)
<< '\n';
infoFile.close();

// create info file format of trash file----- END

return true;
#else
Q_UNUSED(fileName)
*errorString = QCoreApplication::translate("FileSystem", "Moving to the trash is not implemented on this platform");
return false;
#endif
}

#ifdef Q_OS_WIN
Utility::Handle FileSystem::lockFile(const QString &fileName, LockMode mode)
{
Expand Down
6 changes: 1 addition & 5 deletions src/common/filesystembase.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,7 @@ namespace FileSystem {
*/
bool OCSYNC_EXPORT remove(const QString &fileName, QString *errorString = nullptr);

/**
* Move the specified file or folder to the trash. (Only implemented on linux)
*/
bool OCSYNC_EXPORT moveToTrash(const QString &filename, QString *errorString);


/**
* Replacement for QFile::open(ReadOnly) followed by a seek().
* This version sets a more permissive sharing mode on Windows.
Expand Down
2 changes: 1 addition & 1 deletion src/libsync/propagatorjobs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void PropagateLocalRemove::start()
bool ok = false;
QString removeError;
if (_moveToTrash) {
ok = FileSystem::moveToTrash(filename, &removeError);
ok = QFile::moveToTrash(filename, &removeError);
} else {
if (_item->isDirectory()) {
// removeRecursively will call done on error
Expand Down