From 28860e1051c2bedbe99f1a62468f2c9b866fc83f Mon Sep 17 00:00:00 2001 From: Martchus Date: Thu, 15 Nov 2018 21:36:12 +0100 Subject: [PATCH] Improve renaming tool * Allow to pass a note to the skip function * Add useful properties to the file info object * Extend example --- renamingutility/renamingengine.cpp | 2 +- renamingutility/tageditorobject.cpp | 9 +++++++-- renamingutility/tageditorobject.h | 10 +++++++++- resources/scripts/renamefiles/example1.js | 15 +++++++++++++++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/renamingutility/renamingengine.cpp b/renamingutility/renamingengine.cpp index f172ce2..b6507a6 100644 --- a/renamingutility/renamingengine.cpp +++ b/renamingutility/renamingengine.cpp @@ -338,7 +338,7 @@ void RenamingEngine::executeScriptForItem(const QFileInfo &fileInfo, FileSystemI } break; default: - item->setNote(tr("skipped")); + item->setNote(m_tagEditorQObj->note().isEmpty() ? tr("skipped") : m_tagEditorQObj->note()); } } #endif diff --git a/renamingutility/tageditorobject.cpp b/renamingutility/tageditorobject.cpp index 83802dd..0f7282e 100644 --- a/renamingutility/tageditorobject.cpp +++ b/renamingutility/tageditorobject.cpp @@ -145,6 +145,8 @@ TAGEDITOR_JS_VALUE TagEditorObject::parseFileInfo(const QString &fileName) // add basic file information auto fileInfoObject = m_engine->newObject(); + fileInfoObject.setProperty(QStringLiteral("currentPath"), QString::fromUtf8(fileInfo.path().data())); + fileInfoObject.setProperty(QStringLiteral("currentPathWithoutExtension"), QString::fromUtf8(fileInfo.pathWithoutExtension().data())); fileInfoObject.setProperty(QStringLiteral("currentName"), QString::fromUtf8(fileInfo.fileName(false).data())); fileInfoObject.setProperty(QStringLiteral("currentBaseName"), QString::fromUtf8(fileInfo.fileName(true).data())); QString suffix = fromNativeFileName(fileInfo.extension().data()); @@ -154,7 +156,7 @@ TAGEDITOR_JS_VALUE TagEditorObject::parseFileInfo(const QString &fileName) fileInfoObject.setProperty(QStringLiteral("currentSuffix"), suffix TAGEDITOR_JS_READONLY); // parse further file information - bool criticalParseingErrorOccured = false; + bool criticalParseingErrorOccured = false, ioErrorOccured = false; try { fileInfo.parseEverything(diag); } catch (const Failure &) { @@ -163,6 +165,7 @@ TAGEDITOR_JS_VALUE TagEditorObject::parseFileInfo(const QString &fileName) } catch (...) { ::IoUtilities::catchIoFailure(); criticalParseingErrorOccured = true; + ioErrorOccured = true; } // gather notifications @@ -170,6 +173,7 @@ TAGEDITOR_JS_VALUE TagEditorObject::parseFileInfo(const QString &fileName) diagObj << diag; criticalParseingErrorOccured |= diag.level() >= DiagLevel::Critical; fileInfoObject.setProperty(QStringLiteral("hasCriticalMessages"), criticalParseingErrorOccured); + fileInfoObject.setProperty(QStringLiteral("ioErrorOccured"), ioErrorOccured); fileInfoObject.setProperty(QStringLiteral("diagMessages"), diagObj); // add MIME-type, suitable suffix and technical summary @@ -270,9 +274,10 @@ void TagEditorObject::move(const QString &newRelativeDirectory) m_action = ActionType::Rename; } -void TagEditorObject::skip() +void TagEditorObject::skip(const QString ¬e) { m_action = ActionType::Skip; + m_note = note; } } // namespace RenamingUtility diff --git a/renamingutility/tageditorobject.h b/renamingutility/tageditorobject.h index a3a0466..e424278 100644 --- a/renamingutility/tageditorobject.h +++ b/renamingutility/tageditorobject.h @@ -24,6 +24,7 @@ class TagEditorObject : public QObject { Q_PROPERTY(bool isFile READ isFile) Q_PROPERTY(QString newName READ newName WRITE rename) Q_PROPERTY(QString newRelativeDirectory READ newRelativeDirectory WRITE move) + Q_PROPERTY(QString note READ note) public: explicit TagEditorObject(TAGEDITOR_JS_ENGINE *engine); @@ -38,6 +39,7 @@ public: bool isFile() const; const QString &newName() const; const QString &newRelativeDirectory() const; + const QString ¬e() const; public slots: TAGEDITOR_JS_VALUE parseFileInfo(const QString &fileName); @@ -47,7 +49,7 @@ public slots: void writeLog(const QString &message); void rename(const QString &newName); void move(const QString &newRelativeDirectory); - void skip(); + void skip(const QString ¬e = QString()); private: TAGEDITOR_JS_ENGINE *m_engine; @@ -58,6 +60,7 @@ private: ActionType m_action; QString m_newName; QString m_newRelativeDirectory; + QString m_note; }; inline ActionType TagEditorObject::action() const @@ -65,6 +68,11 @@ inline ActionType TagEditorObject::action() const return m_action; } +inline const QString &TagEditorObject::note() const +{ + return m_note; +} + } // namespace RenamingUtility #endif // TAGEDITOR_NO_JSENGINE diff --git a/resources/scripts/renamefiles/example1.js b/resources/scripts/renamefiles/example1.js index 4535dd7..f4d881c 100644 --- a/resources/scripts/renamefiles/example1.js +++ b/resources/scripts/renamefiles/example1.js @@ -78,6 +78,21 @@ if (fileInfo.currentName.indexOf(".") === 0 tageditor.skip() return } + +// treat *.lrc files like their corresponding *.mp3 files +var keepSuffix +if (fileInfo.currentSuffix === "lrc") { + // deduce path of corresponding *.mp3 file + var correspondingMp3File = fileInfo.currentPathWithoutExtension + ".mp3" + // keep the lrc suffix later + keepSuffix = fileInfo.currentSuffix + // use the file info from the corresponding *.mp3 file + fileInfo = tageditor.parseFileInfo(correspondingMp3File) + if (fileInfo.ioErrorOccured) { + tageditor.skip("skipped, " + correspondingMp3File + " not present") + return + } + tag = fileInfo.tag } // skip files which don't contain audio or video tracks