fileitemaction: show menu for all files, preserve extension, keep existing identifier
This commit is contained in:
parent
9469cad843
commit
bd05e3f1be
|
|
@ -48,11 +48,7 @@ QList<QAction *> DenoteFileItemAction::actions(const KFileItemListProperties &fi
|
|||
return {action};
|
||||
}
|
||||
|
||||
// Non-denote file: show "Rename with Denote..."
|
||||
Denote::Metadata meta = Denote::parseFilename(item.localPath());
|
||||
if (meta.isValid())
|
||||
return {};
|
||||
|
||||
// Show "Rename with Denote..." for any file
|
||||
auto *action = new QAction(i18n("Rename with Denote..."), parentWidget);
|
||||
action->setIcon(QIcon::fromTheme(QStringLiteral("edit-rename")));
|
||||
|
||||
|
|
@ -73,23 +69,39 @@ void DenoteFileItemAction::renameWithDenote(const KFileItemList &items, QWidget
|
|||
const QString path = item.localPath();
|
||||
const QFileInfo fi(path);
|
||||
|
||||
// Pre-fill title from filename (without extension)
|
||||
QString defaultTitle = fi.completeBaseName();
|
||||
defaultTitle.replace(QLatin1Char('-'), QLatin1Char(' '));
|
||||
defaultTitle.replace(QLatin1Char('_'), QLatin1Char(' '));
|
||||
// Preserve original extension (e.g. .txt, .tar.gz, or empty for no extension)
|
||||
const QString suffix = fi.completeSuffix();
|
||||
const QString ext = suffix.isEmpty() ? QString() : QStringLiteral(".") + suffix;
|
||||
const Denote::FileType fileType = Denote::fileTypeFromExtension(ext);
|
||||
|
||||
// Try to extract title from frontmatter if applicable
|
||||
const QString ext = QStringLiteral(".") + fi.completeSuffix();
|
||||
Denote::FileType fileType = Denote::fileTypeFromExtension(ext);
|
||||
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;
|
||||
// Parse existing denote filename metadata
|
||||
Denote::Metadata meta = Denote::parseFilename(path);
|
||||
QString defaultTitle;
|
||||
QStringList defaultTags;
|
||||
|
||||
if (meta.isValid()) {
|
||||
// File already follows denote naming
|
||||
defaultTitle = meta.title;
|
||||
defaultTags = meta.tags;
|
||||
|
||||
// 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
|
||||
|
|
@ -106,7 +118,7 @@ void DenoteFileItemAction::renameWithDenote(const KFileItemList &items, QWidget
|
|||
auto *titleEdit = new QLineEdit(defaultTitle);
|
||||
form->addRow(i18n("Title:"), titleEdit);
|
||||
|
||||
auto *tagsEdit = new QLineEdit();
|
||||
auto *tagsEdit = new QLineEdit(defaultTags.join(QStringLiteral(", ")));
|
||||
tagsEdit->setPlaceholderText(i18n("tag1, tag2, tag3"));
|
||||
form->addRow(i18n("Tags:"), tagsEdit);
|
||||
|
||||
|
|
@ -139,7 +151,16 @@ void DenoteFileItemAction::renameWithDenote(const KFileItemList &items, QWidget
|
|||
const QString signature = sigEdit->text().trimmed();
|
||||
|
||||
// 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);
|
||||
|
||||
// Rename (use ::rename for atomic, no-copy behavior)
|
||||
|
|
|
|||
Loading…
Reference in New Issue