fileitemaction: show menu for all files, preserve extension, keep existing identifier

This commit is contained in:
Levi Neely 2026-07-26 21:17:16 +02:00
parent 9469cad843
commit bd05e3f1be
1 changed files with 43 additions and 22 deletions

View File

@ -48,11 +48,7 @@ QList<QAction *> DenoteFileItemAction::actions(const KFileItemListProperties &fi
return {action}; return {action};
} }
// Non-denote file: show "Rename with Denote..." // Show "Rename with Denote..." for any file
Denote::Metadata meta = Denote::parseFilename(item.localPath());
if (meta.isValid())
return {};
auto *action = new QAction(i18n("Rename with Denote..."), parentWidget); auto *action = new QAction(i18n("Rename with Denote..."), parentWidget);
action->setIcon(QIcon::fromTheme(QStringLiteral("edit-rename"))); action->setIcon(QIcon::fromTheme(QStringLiteral("edit-rename")));
@ -73,23 +69,39 @@ void DenoteFileItemAction::renameWithDenote(const KFileItemList &items, QWidget
const QString path = item.localPath(); const QString path = item.localPath();
const QFileInfo fi(path); const QFileInfo fi(path);
// Pre-fill title from filename (without extension) // Preserve original extension (e.g. .txt, .tar.gz, or empty for no extension)
QString defaultTitle = fi.completeBaseName(); const QString suffix = fi.completeSuffix();
defaultTitle.replace(QLatin1Char('-'), QLatin1Char(' ')); const QString ext = suffix.isEmpty() ? QString() : QStringLiteral(".") + suffix;
defaultTitle.replace(QLatin1Char('_'), QLatin1Char(' ')); const Denote::FileType fileType = Denote::fileTypeFromExtension(ext);
// Try to extract title from frontmatter if applicable // Parse existing denote filename metadata
const QString ext = QStringLiteral(".") + fi.completeSuffix(); Denote::Metadata meta = Denote::parseFilename(path);
Denote::FileType fileType = Denote::fileTypeFromExtension(ext); QString defaultTitle;
if (fileType != Denote::FileType::Unknown) { QStringList defaultTags;
QFile file(path);
if (file.open(QIODevice::ReadOnly)) { if (meta.isValid()) {
QByteArray content = file.readAll(); // File already follows denote naming
file.close(); defaultTitle = meta.title;
Denote::FrontMatter fm = Denote::parseFrontMatter(content, fileType); defaultTags = meta.tags;
if (!fm.title.isEmpty())
defaultTitle = fm.title; // For frontmatter-capable files, prefer frontmatter values
if (fileType != Denote::FileType::Unknown) {
QFile file(path);
if (file.open(QIODevice::ReadOnly)) {
QByteArray content = file.readAll();
file.close();
Denote::FrontMatter fm = Denote::parseFrontMatter(content, fileType);
if (!fm.title.isEmpty())
defaultTitle = fm.title;
if (!fm.tags.isEmpty())
defaultTags = fm.tags;
}
} }
} else {
// Not a denote file — pre-fill title from filename
defaultTitle = fi.completeBaseName();
defaultTitle.replace(QLatin1Char('-'), QLatin1Char(' '));
defaultTitle.replace(QLatin1Char('_'), QLatin1Char(' '));
} }
// Build dialog // Build dialog
@ -106,7 +118,7 @@ void DenoteFileItemAction::renameWithDenote(const KFileItemList &items, QWidget
auto *titleEdit = new QLineEdit(defaultTitle); auto *titleEdit = new QLineEdit(defaultTitle);
form->addRow(i18n("Title:"), titleEdit); form->addRow(i18n("Title:"), titleEdit);
auto *tagsEdit = new QLineEdit(); auto *tagsEdit = new QLineEdit(defaultTags.join(QStringLiteral(", ")));
tagsEdit->setPlaceholderText(i18n("tag1, tag2, tag3")); tagsEdit->setPlaceholderText(i18n("tag1, tag2, tag3"));
form->addRow(i18n("Tags:"), tagsEdit); form->addRow(i18n("Tags:"), tagsEdit);
@ -139,7 +151,16 @@ void DenoteFileItemAction::renameWithDenote(const KFileItemList &items, QWidget
const QString signature = sigEdit->text().trimmed(); const QString signature = sigEdit->text().trimmed();
// Build new filename // Build new filename
const QString newName = Denote::buildFilename(title, tags, signature, ext); QString newName;
if (meta.isValid()) {
// Preserve existing identifier — reuse it with updated title/tags/signature
// Use Denote::buildFilename to get the slug and tag formatting, then swap the identifier
const QString baseName = Denote::buildFilename(title, tags, signature, ext);
// Replace the generated identifier with the existing one
newName = meta.identifier + baseName.mid(baseName.indexOf(QLatin1Char('-')));
} else {
newName = Denote::buildFilename(title, tags, signature, ext);
}
const QString newPath = fi.dir().filePath(newName); const QString newPath = fi.dir().filePath(newName);
// Rename (use ::rename for atomic, no-copy behavior) // Rename (use ::rename for atomic, no-copy behavior)