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

Add "Delete Permanently" action (i.e. delete without trash) #639

Merged
merged 2 commits into from
May 6, 2024
Merged
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
14 changes: 12 additions & 2 deletions src/actionmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ QMenuBar *ActionManager::buildMenuBar(QWidget *parent)
addCloneOfAction(fileMenu, "closeallwindows");
#endif
#ifdef COCOA_LOADED
QVCocoaFunctions::setAlternates(fileMenu, fileMenu->actions().length()-1, fileMenu->actions().length()-2);
QVCocoaFunctions::setAlternate(fileMenu, fileMenu->actions().length()-1);
#endif
fileMenu->addSeparator();
fileMenu->addMenu(buildOpenWithMenu(fileMenu));
Expand All @@ -207,6 +207,10 @@ QMenuBar *ActionManager::buildMenuBar(QWidget *parent)
addCloneOfAction(editMenu, "rename");
editMenu->addSeparator();
addCloneOfAction(editMenu, "delete");
addCloneOfAction(editMenu, "deletepermanent");
#ifdef COCOA_LOADED
QVCocoaFunctions::setAlternate(editMenu, editMenu->actions().length()-1);
#endif

menuBar->addMenu(editMenu);
// End of edit menu
Expand Down Expand Up @@ -593,7 +597,9 @@ void ActionManager::actionTriggered(QAction *triggeredAction, MainWindow *releva
} else if (key == "showfileinfo") {
relevantWindow->showFileInfo();
} else if (key == "delete") {
relevantWindow->askDeleteFile();
relevantWindow->askDeleteFile(false);
} else if (key == "deletepermanent") {
relevantWindow->askDeleteFile(true);
} else if (key == "undo") {
relevantWindow->undoDelete();
} else if (key == "copy") {
Expand Down Expand Up @@ -696,6 +702,10 @@ void ActionManager::initializeActionLibrary()
deleteAction->setData({"disable"});
actionLibrary.insert("delete", deleteAction);

auto *deletePermanentAction = new QAction(QIcon::fromTheme("edit-delete"), tr("Delete Permanently"));
deletePermanentAction->setData({"disable"});
actionLibrary.insert("deletepermanent", deletePermanentAction);

auto *undoAction = new QAction(QIcon::fromTheme("edit-undo"), tr("&Restore from Trash"));
#ifdef Q_OS_WIN
undoAction->setText(tr("&Undo Delete"));
Expand Down
92 changes: 59 additions & 33 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -701,11 +701,11 @@ void MainWindow::showFileInfo()
info->raise();
}

void MainWindow::askDeleteFile()
void MainWindow::askDeleteFile(bool permanent)
{
if (!qvApp->getSettingsManager().getBoolean("askdelete"))
if (!permanent && !qvApp->getSettingsManager().getBoolean("askdelete"))
{
deleteFile();
deleteFile(permanent);
return;
}

Expand All @@ -718,69 +718,95 @@ void MainWindow::askDeleteFile()
return;
}

auto trashString = tr("Are you sure you want to move %1 to the Trash?").arg(fileName);
QString messageText;
if (permanent)
{
messageText = tr("Are you sure you want to delete %1 permanently? This can't be undone.").arg(fileName);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In practice I found having the adverb separated, especially if there's a longer filename filled in for the placeholder, to be a bit awkward. Could consider wording this "permanently delete %1" instead.

}
else
{
#ifdef Q_OS_WIN
trashString = tr("Are you sure you want to move %1 to the Recycle Bin?").arg(fileName);
messageText = tr("Are you sure you want to move %1 to the Recycle Bin?").arg(fileName);
#else
messageText = tr("Are you sure you want to move %1 to the Trash?").arg(fileName);
#endif
}

auto *msgBox = new QMessageBox(QMessageBox::Question, tr("Delete"), trashString,
auto *msgBox = new QMessageBox(QMessageBox::Question, tr("Delete"), messageText,
QMessageBox::Yes | QMessageBox::No, this);
msgBox->setCheckBox(new QCheckBox(tr("Do not ask again")));
if (!permanent)
msgBox->setCheckBox(new QCheckBox(tr("Do not ask again")));

connect(msgBox, &QMessageBox::finished, this, [this, msgBox](int result){
if (result != 16384)
connect(msgBox, &QMessageBox::finished, this, [this, msgBox, permanent](int result){
if (result != QMessageBox::Yes)
return;

QSettings settings;
settings.beginGroup("options");
settings.setValue("askdelete", !msgBox->checkBox()->isChecked());
qvApp->getSettingsManager().loadSettings();
this->deleteFile();
if (!permanent)
{
QSettings settings;
settings.beginGroup("options");
settings.setValue("askdelete", !msgBox->checkBox()->isChecked());
qvApp->getSettingsManager().loadSettings();
}
this->deleteFile(permanent);
});

msgBox->open();
}

void MainWindow::deleteFile()
void MainWindow::deleteFile(bool permanent)
{
const QFileInfo &fileInfo = getCurrentFileDetails().fileInfo;
const QString filePath = fileInfo.absoluteFilePath();
QString trashFilePath = "";
const QString fileName = fileInfo.fileName();

graphicsView->closeImage();

bool success;
QString trashFilePath;
if (permanent)
{
success = QFile::remove(filePath);
}
else
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
const QString fileName = fileInfo.fileName();
QFile file(filePath);
success = file.moveToTrash();
if (success)
trashFilePath = file.fileName();
#elif defined Q_OS_MACOS && COCOA_LOADED
QString trashedFile = QVCocoaFunctions::deleteFile(filePath);
success = !trashedFile.isEmpty();
if (success)
trashFilePath = QUrl(trashedFile).toLocalFile(); // remove file:// protocol
#elif defined Q_OS_UNIX && !defined Q_OS_MACOS
trashFilePath = deleteFileLinuxFallback(filePath, false);
success = !trashFilePath.isEmpty();
#else
QMessageBox::critical(this, tr("Not Supported"), tr("This program was compiled with an old version of Qt and this feature is not available.\n"
"If you see this message, please report a bug!"));

return;
#endif
}

QFile file(filePath);
bool success = file.moveToTrash();
if (!success || QFile::exists(filePath))
{
openFile(filePath);
QMessageBox::critical(this, tr("Error"), tr("Can't delete %1.").arg(fileName));
return;
}

trashFilePath = file.fileName();
#elif defined Q_OS_MACOS && COCOA_LOADED
QString trashedFile = QVCocoaFunctions::deleteFile(filePath);
trashFilePath = QUrl(trashedFile).toLocalFile(); // remove file:// protocol
#elif defined Q_OS_UNIX && !defined Q_OS_MACOS
trashFilePath = deleteFileLinuxFallback(filePath, false);
#else
QMessageBox::critical(this, tr("Not Supported"), tr("This program was compiled with an old version of Qt and this feature is not available.\n"
"If you see this message, please report a bug!"));

return;
#endif

auto afterDelete = qvApp->getSettingsManager().getInteger("afterdelete");
if (afterDelete > 1)
nextFile();
else if (afterDelete < 1)
previousFile();

lastDeletedFiles.push({trashFilePath, filePath});
if (!trashFilePath.isEmpty())
lastDeletedFiles.push({trashFilePath, filePath});

disableActions();
}

Expand Down
4 changes: 2 additions & 2 deletions src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ class MainWindow : public QMainWindow

void showFileInfo();

void askDeleteFile();
void askDeleteFile(bool permanent);

void deleteFile();
void deleteFile(bool permanent);

QString deleteFileLinuxFallback(const QString &path, bool putBack);

Expand Down
2 changes: 1 addition & 1 deletion src/qvcocoafunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class QVCocoaFunctions

static void setWindowMenu(QMenu *menu);

static void setAlternates(QMenu *menu, int index0, int index1);
static void setAlternate(QMenu *menu, int index);

static void setDockRecents(const QStringList &recentPathsList);

Expand Down
5 changes: 2 additions & 3 deletions src/qvcocoafunctions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,10 @@ static void fixNativeMenuEccentricities(QMenu *menu, NSMenu *nativeMenu)
[[NSApplication sharedApplication] setWindowsMenu:nativeMenu];
}

void QVCocoaFunctions::setAlternates(QMenu *menu, int index0, int index1)
void QVCocoaFunctions::setAlternate(QMenu *menu, int index)
{
NSMenu *nativeMenu = menu->toNSMenu();
[[nativeMenu.itemArray objectAtIndex:index0] setAlternate:true];
[[nativeMenu.itemArray objectAtIndex:index1] setAlternate:true];
[[nativeMenu.itemArray objectAtIndex:index] setAlternate:true];
}

void QVCocoaFunctions::setDockRecents(const QStringList &recentPathsList)
Expand Down
5 changes: 5 additions & 0 deletions src/shortcutmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ void ShortcutManager::initializeShortcutsList()
shortcutsList.last().defaultShortcuts.prepend(QKeySequence(Qt::CTRL | Qt::Key_Backspace).toString());
#ifdef Q_OS_WIN
shortcutsList.last().readableName = tr("Delete");
#endif
shortcutsList.append({tr("Delete Permanently"), "deletepermanent", QStringList(QKeySequence(Qt::SHIFT | Qt::Key_Delete).toString()), {}});
#ifdef Q_OS_MACOS
// cmd+option+backspace
shortcutsList.last().defaultShortcuts.prepend(QKeySequence(Qt::CTRL | Qt::ALT | Qt::Key_Backspace).toString());
#endif
shortcutsList.append({tr("First File"), "firstfile", QStringList(QKeySequence(Qt::Key_Home).toString()), {}});
shortcutsList.append({tr("Previous File"), "previousfile", QStringList(QKeySequence(Qt::Key_Left).toString()), {}});
Expand Down
Loading