diff options
| author | Kai Uwe Broulik <[email protected]> | 2025-08-21 12:46:12 +0200 |
|---|---|---|
| committer | Kai Uwe Broulik <[email protected]> | 2025-08-26 17:51:56 +0000 |
| commit | b498e2e43c00d718474d973fb98e6c7d94e32d0a (patch) | |
| tree | 2abb4ddd2b1a85c1259bad1a6cef5516012ffd53 /src/views/dolphinview.cpp | |
| parent | 2577a6c1cbf35e305ed03797d1297cb21899c7f5 (diff) | |
Warn when renaming a file would change its file type
While the file's content won't change, applications may no longer
recognize it.
Diffstat (limited to 'src/views/dolphinview.cpp')
| -rw-r--r-- | src/views/dolphinview.cpp | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/src/views/dolphinview.cpp b/src/views/dolphinview.cpp index 1ed857851..6bd2ad0c1 100644 --- a/src/views/dolphinview.cpp +++ b/src/views/dolphinview.cpp @@ -44,10 +44,12 @@ #include <KIO/Paste> #include <KIO/PasteJob> #include <KIO/RenameFileDialog> +#include <KIconUtils> #include <KJob> #include <KJobWidgets> #include <KLocalizedString> #include <KMessageBox> +#include <KMessageDialog> #include <KProtocolManager> #include <KUrlMimeData> @@ -2057,8 +2059,54 @@ void DolphinView::slotRoleEditingFinished(int index, const QByteArray &role, con if (code == KMessageBox::SecondaryAction) { return; } - } + } else #endif + // Confirm potentially changing the file type. + if (GeneralSettings::confirmRenameFileType() && oldItem.isFile() && oldItem.isLocalFile() && !oldItem.isSlow() && oldItem.isMimeTypeKnown()) { + QMimeDatabase db; + const QMimeType oldMimeType = db.mimeTypeForName(oldItem.mimetype()); + + // Guess what the new file type would be. + // We have to also read the file as its new type could be auto-determined from content. + QFile oldFile(oldItem.localPath()); + const QMimeType newMimeType = db.mimeTypeForFileNameAndData(newName, &oldFile); + + if (oldMimeType.isValid() && !oldMimeType.isDefault() && newMimeType.isValid() && newMimeType != oldMimeType) { + const KGuiItem yesGuiItem(i18nc("@action:button", "Rename"), QStringLiteral("edit-rename")); + + const QIcon mimeTypeIcon = QIcon::fromTheme(newMimeType.iconName(), QIcon::fromTheme(QStringLiteral("unknown"))); + // emblem-warning is non-standard, fall back to emblem-important if necessary. + const QIcon warningBadge = QIcon::fromTheme(QStringLiteral("emblem-warning"), QIcon::fromTheme(QStringLiteral("emblem-important"))); + + const QIcon messageBoxIcon = + KIconUtils::addOverlay(mimeTypeIcon, warningBadge, isRightToLeft() ? Qt::BottomLeftCorner : Qt::BottomRightCorner); + + const QString prompt = newMimeType.isDefault() ? i18n( + "This will make the file type unknown.\n" + "The file's content won't change but applications may no longer recognize it.\n" + "Do you still want to rename it?") + : i18n( + "This will change the file type from \"%1\" to \"%2\".\n" + "The file's content won't change but applications may no longer recognize it.\n" + "Do you still want to rename it?", + oldMimeType.comment(), + newMimeType.comment()); + auto *dialog = new KMessageDialog(KMessageDialog::QuestionTwoActions, prompt, this); + dialog->setWindowTitle(i18nc("@title:window", "Change File Type")); + dialog->setButtons(yesGuiItem, KStandardGuiItem::cancel()); + dialog->setIcon(messageBoxIcon); + dialog->setDontAskAgainText(i18n("Do not ask again")); + + if (dialog->exec() != KMessageDialog::PrimaryAction) { + return; + } + + if (dialog->isDontAskAgainChecked()) { + GeneralSettings::setConfirmRenameFileType(false); + GeneralSettings::self()->save(); + } + } + } KIO::Job *job = KIO::moveAs(oldUrl, newUrl); KJobWidgets::setWindow(job, this); |
