2015-09-06 20:20:00 +02:00
|
|
|
#include "./renamingengine.h"
|
|
|
|
#include "./filesystemitemmodel.h"
|
|
|
|
#include "./filteredfilesystemitemmodel.h"
|
2016-01-09 02:56:56 +01:00
|
|
|
#include "./tageditorobject.h"
|
2015-04-22 19:33:53 +02:00
|
|
|
|
|
|
|
#include <QDir>
|
2016-01-09 02:56:56 +01:00
|
|
|
#include <QStringBuilder>
|
2016-03-10 22:13:43 +01:00
|
|
|
#include <QtConcurrent>
|
2015-04-22 19:33:53 +02:00
|
|
|
|
2017-02-05 21:04:27 +01:00
|
|
|
#include <memory>
|
|
|
|
|
2015-04-22 19:33:53 +02:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace RenamingUtility {
|
|
|
|
|
2017-01-06 21:29:43 +01:00
|
|
|
RenamingEngine::RenamingEngine(QObject *parent) :
|
2015-04-22 19:33:53 +02:00
|
|
|
QObject(parent),
|
2016-04-21 23:55:22 +02:00
|
|
|
#ifndef TAGEDITOR_NO_JSENGINE
|
2016-01-09 02:56:56 +01:00
|
|
|
m_tagEditorQObj(new TagEditorObject(&m_engine)),
|
|
|
|
m_tagEditorJsObj(TAGEDITOR_JS_QOBJECT(m_engine, m_tagEditorQObj)),
|
2016-04-21 23:55:22 +02:00
|
|
|
#endif
|
2015-04-22 19:33:53 +02:00
|
|
|
m_itemsProcessed(0),
|
|
|
|
m_errorsOccured(0),
|
|
|
|
m_aborted(false),
|
|
|
|
m_includeSubdirs(false),
|
2017-01-06 21:43:59 +01:00
|
|
|
m_isBusy(false),
|
2015-04-22 19:33:53 +02:00
|
|
|
m_model(nullptr),
|
|
|
|
m_currentModel(nullptr),
|
|
|
|
m_previewModel(nullptr)
|
|
|
|
{
|
2016-04-21 23:55:22 +02:00
|
|
|
#ifndef TAGEDITOR_NO_JSENGINE
|
2016-01-09 02:56:56 +01:00
|
|
|
m_engine.globalObject().setProperty(QStringLiteral("tageditor"), m_tagEditorJsObj);
|
2016-04-21 23:55:22 +02:00
|
|
|
#endif
|
2017-01-06 21:29:43 +01:00
|
|
|
connect(this, &RenamingEngine::previewGenerated, this, &RenamingEngine::processPreviewGenerated);
|
|
|
|
connect(this, &RenamingEngine::changingsApplied, this, &RenamingEngine::processChangingsApplied);
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
|
|
|
|
2016-04-21 23:55:22 +02:00
|
|
|
#ifndef TAGEDITOR_NO_JSENGINE
|
2017-01-06 21:29:43 +01:00
|
|
|
bool RenamingEngine::setProgram(const TAGEDITOR_JS_VALUE &program)
|
2016-01-09 02:56:56 +01:00
|
|
|
{
|
|
|
|
if(TAGEDITOR_JS_IS_VALID_PROG(program)) {
|
|
|
|
m_errorMessage.clear();
|
|
|
|
m_errorLineNumber = 0;
|
|
|
|
m_program = program;
|
|
|
|
return true;
|
|
|
|
} else if(program.isError()) {
|
|
|
|
m_errorMessage = program.property(QStringLiteral("message")).toString();
|
|
|
|
m_errorLineNumber = TAGEDITOR_JS_INT(program.property(QStringLiteral("lineNumber")));
|
|
|
|
} else {
|
|
|
|
m_errorMessage = tr("Program is not callable.");
|
|
|
|
m_errorLineNumber = 0;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-21 23:55:22 +02:00
|
|
|
#endif
|
2015-04-22 19:33:53 +02:00
|
|
|
|
2017-01-06 21:29:43 +01:00
|
|
|
bool RenamingEngine::setProgram(const QString &program)
|
2016-01-09 02:56:56 +01:00
|
|
|
{
|
2016-04-21 23:55:22 +02:00
|
|
|
#ifndef TAGEDITOR_NO_JSENGINE
|
2016-01-09 02:56:56 +01:00
|
|
|
return setProgram(m_engine.evaluate(QStringLiteral("(function(){") % program % QStringLiteral("})")));
|
2016-04-21 23:55:22 +02:00
|
|
|
#else
|
|
|
|
m_errorLineNumber = 0;
|
|
|
|
m_errorMessage = tr("Not compiled with ECMA support.");
|
|
|
|
return false;
|
|
|
|
#endif
|
2016-01-09 02:56:56 +01:00
|
|
|
}
|
|
|
|
|
2017-01-06 21:29:43 +01:00
|
|
|
bool RenamingEngine::generatePreview(const QDir &rootDirectory, bool includeSubdirs)
|
2015-04-22 19:33:53 +02:00
|
|
|
{
|
2016-04-21 23:55:22 +02:00
|
|
|
#ifndef TAGEDITOR_NO_JSENGINE
|
2017-01-06 21:43:59 +01:00
|
|
|
if(m_isBusy) {
|
2015-04-22 19:33:53 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-01-06 21:43:59 +01:00
|
|
|
setRootItem();
|
|
|
|
m_includeSubdirs = includeSubdirs;
|
|
|
|
m_dir = rootDirectory;
|
|
|
|
QtConcurrent::run([this] () {
|
|
|
|
m_aborted.store(false);
|
|
|
|
m_itemsProcessed = 0;
|
|
|
|
m_errorsOccured = 0;
|
|
|
|
m_newlyGeneratedRootItem = generatePreview(m_dir);
|
|
|
|
emit previewGenerated();
|
|
|
|
});
|
|
|
|
return m_isBusy = true;
|
2016-04-21 23:55:22 +02:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
|
|
|
|
2017-01-06 21:29:43 +01:00
|
|
|
bool RenamingEngine::applyChangings()
|
2015-04-22 19:33:53 +02:00
|
|
|
{
|
2017-01-06 21:43:59 +01:00
|
|
|
if(!m_rootItem || m_isBusy) {
|
2015-04-22 19:33:53 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-01-06 21:43:59 +01:00
|
|
|
QtConcurrent::run([this] () {
|
|
|
|
m_aborted.store(false);
|
|
|
|
m_itemsProcessed = 0;
|
|
|
|
m_errorsOccured = 0;
|
|
|
|
applyChangings(m_rootItem.get());
|
|
|
|
emit changingsApplied();
|
|
|
|
});
|
|
|
|
return m_isBusy = true;
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
|
|
|
|
2017-01-06 21:29:43 +01:00
|
|
|
bool RenamingEngine::clearPreview()
|
2015-04-22 19:33:53 +02:00
|
|
|
{
|
2017-01-06 21:43:59 +01:00
|
|
|
if(m_isBusy) {
|
2015-04-22 19:33:53 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-01-06 21:43:59 +01:00
|
|
|
|
|
|
|
updateModel(nullptr);
|
|
|
|
m_rootItem.reset();
|
|
|
|
return true;
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
|
|
|
|
2017-01-06 21:29:43 +01:00
|
|
|
FileSystemItemModel *RenamingEngine::model()
|
2015-04-22 19:33:53 +02:00
|
|
|
{
|
|
|
|
if(!m_model) {
|
|
|
|
m_model = new FileSystemItemModel(m_rootItem.get(), this);
|
|
|
|
}
|
|
|
|
return m_model;
|
|
|
|
}
|
|
|
|
|
2017-01-06 21:29:43 +01:00
|
|
|
FilteredFileSystemItemModel *RenamingEngine::currentModel()
|
2015-04-22 19:33:53 +02:00
|
|
|
{
|
|
|
|
if(!m_currentModel) {
|
|
|
|
m_currentModel = new FilteredFileSystemItemModel(ItemStatus::Current, this);
|
|
|
|
m_currentModel->setSourceModel(model());
|
|
|
|
}
|
|
|
|
return m_currentModel;
|
|
|
|
}
|
|
|
|
|
2017-01-06 21:29:43 +01:00
|
|
|
FilteredFileSystemItemModel *RenamingEngine::previewModel()
|
2015-04-22 19:33:53 +02:00
|
|
|
{
|
|
|
|
if(!m_previewModel) {
|
|
|
|
m_previewModel = new FilteredFileSystemItemModel(ItemStatus::New, this);
|
|
|
|
m_previewModel->setSourceModel(model());
|
|
|
|
}
|
|
|
|
return m_previewModel;
|
|
|
|
}
|
|
|
|
|
2017-01-06 21:29:43 +01:00
|
|
|
void RenamingEngine::processPreviewGenerated()
|
2015-04-22 19:33:53 +02:00
|
|
|
{
|
2017-01-06 21:43:59 +01:00
|
|
|
m_isBusy = false;
|
2015-10-13 20:12:00 +02:00
|
|
|
setRootItem(move(m_newlyGeneratedRootItem));
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
|
|
|
|
2017-01-06 21:29:43 +01:00
|
|
|
void RenamingEngine::processChangingsApplied()
|
2015-04-22 19:33:53 +02:00
|
|
|
{
|
2017-01-06 21:43:59 +01:00
|
|
|
m_isBusy = false;
|
2015-04-22 19:33:53 +02:00
|
|
|
updateModel(nullptr);
|
|
|
|
updateModel(m_rootItem.get());
|
|
|
|
}
|
|
|
|
|
2017-01-06 21:29:43 +01:00
|
|
|
inline void RenamingEngine::setRootItem(unique_ptr<FileSystemItem> &&rootItem)
|
2015-04-22 19:33:53 +02:00
|
|
|
{
|
2015-10-13 20:12:00 +02:00
|
|
|
updateModel(rootItem.get());
|
|
|
|
m_rootItem = move(rootItem);
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
|
|
|
|
2017-01-06 21:29:43 +01:00
|
|
|
void RenamingEngine::updateModel(FileSystemItem *rootItem)
|
2015-04-22 19:33:53 +02:00
|
|
|
{
|
|
|
|
if(m_model) {
|
|
|
|
m_model->setRootItem(rootItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-21 23:55:22 +02:00
|
|
|
#ifndef TAGEDITOR_NO_JSENGINE
|
2017-01-06 21:29:43 +01:00
|
|
|
unique_ptr<FileSystemItem> RenamingEngine::generatePreview(const QDir &dir, FileSystemItem *parent)
|
2015-04-22 19:33:53 +02:00
|
|
|
{
|
2015-10-13 20:12:00 +02:00
|
|
|
auto item = make_unique<FileSystemItem>(ItemStatus::Current, ItemType::Dir, dir.dirName(), parent);
|
2015-04-22 19:33:53 +02:00
|
|
|
item->setApplied(false);
|
2016-01-09 02:56:56 +01:00
|
|
|
for(const QFileInfo &entry : dir.entryInfoList()) {
|
2015-04-22 19:33:53 +02:00
|
|
|
if(entry.fileName() == QLatin1String("..")
|
|
|
|
|| entry.fileName() == QLatin1String(".")) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-10-13 20:12:00 +02:00
|
|
|
FileSystemItem *subItem; // will be deleted by parent
|
2015-04-22 19:33:53 +02:00
|
|
|
if(entry.isDir() && m_includeSubdirs) {
|
2015-10-13 20:12:00 +02:00
|
|
|
subItem = generatePreview(QDir(entry.absoluteFilePath()), item.get()).release();
|
2015-04-22 19:33:53 +02:00
|
|
|
} else if(entry.isFile()) {
|
2015-10-13 20:12:00 +02:00
|
|
|
subItem = new FileSystemItem(ItemStatus::Current, ItemType::File, entry.fileName(), item.get());
|
2015-04-22 19:33:53 +02:00
|
|
|
subItem->setApplied(false);
|
|
|
|
} else {
|
|
|
|
subItem = nullptr;
|
|
|
|
}
|
|
|
|
if(subItem) {
|
|
|
|
executeScriptForItem(entry, subItem);
|
|
|
|
if(subItem->errorOccured()) {
|
|
|
|
++m_errorsOccured;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++m_itemsProcessed;
|
|
|
|
if(isAborted()) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
emit progress(m_itemsProcessed, m_errorsOccured);
|
|
|
|
return item;
|
|
|
|
}
|
2016-04-21 23:55:22 +02:00
|
|
|
#endif
|
2015-04-22 19:33:53 +02:00
|
|
|
|
2017-01-06 21:29:43 +01:00
|
|
|
void RenamingEngine::applyChangings(FileSystemItem *parentItem)
|
2015-04-22 19:33:53 +02:00
|
|
|
{
|
2016-03-05 17:12:16 +01:00
|
|
|
for(FileSystemItem *item : parentItem->children()) {
|
2015-04-22 19:33:53 +02:00
|
|
|
if(!item->applied() && !item->errorOccured()) {
|
|
|
|
switch(item->status()) {
|
|
|
|
case ItemStatus::New: {
|
2017-01-06 21:43:59 +01:00
|
|
|
const FileSystemItem *counterpartItem = item->counterpart(); // holds current name
|
|
|
|
const QString currentPath = counterpartItem ? counterpartItem->relativePath() : QString();
|
|
|
|
const QString newPath = item->relativePath();
|
2015-04-22 19:33:53 +02:00
|
|
|
if(item->name().isEmpty()) {
|
|
|
|
// new item name mustn't be empty
|
|
|
|
item->setNote(tr("generated name is empty"));
|
|
|
|
item->setErrorOccured(true);
|
|
|
|
} else if(counterpartItem && !counterpartItem->name().isEmpty()) {
|
|
|
|
// rename current item
|
|
|
|
if(item->parent() != counterpartItem->parent() || item->name() != counterpartItem->name()) {
|
|
|
|
if(m_dir.exists(newPath)) {
|
|
|
|
if(item->parent() == counterpartItem->parent()) {
|
|
|
|
item->setNote(tr("unable to rename, there is already an entry with the same name"));
|
|
|
|
} else {
|
|
|
|
item->setNote(tr("unable to move, there is already an entry with the same name"));
|
|
|
|
}
|
|
|
|
item->setErrorOccured(true);
|
2016-05-06 23:16:37 +02:00
|
|
|
} else if(m_dir.rename(currentPath, newPath)) {
|
2015-04-22 19:33:53 +02:00
|
|
|
if(item->parent() == counterpartItem->parent()) {
|
|
|
|
item->setNote(tr("renamed"));
|
|
|
|
} else {
|
|
|
|
item->setNote(tr("moved"));
|
|
|
|
}
|
|
|
|
item->setApplied(true);
|
|
|
|
} else {
|
|
|
|
item->setNote(tr("unable to rename"));
|
|
|
|
item->setErrorOccured(true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
item->setNote(tr("nothing to be changed"));
|
|
|
|
item->setApplied(true);
|
|
|
|
}
|
|
|
|
} else if(item->type() == ItemType::Dir) {
|
|
|
|
// create new item, but only if its a dir
|
|
|
|
if(m_dir.exists(newPath)) {
|
|
|
|
item->setNote(tr("directory already existed"));
|
|
|
|
item->setApplied(true);
|
|
|
|
} else if(m_dir.mkpath(newPath)) {
|
|
|
|
item->setNote(tr("directory created"));
|
|
|
|
item->setApplied(true);
|
|
|
|
} else {
|
|
|
|
item->setNote(tr("unable to create directory"));
|
|
|
|
item->setErrorOccured(true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// can not create new file
|
|
|
|
item->setNote(tr("unable to create file"));
|
|
|
|
item->setErrorOccured(true);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} case ItemStatus::Current:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(item->errorOccured()) {
|
|
|
|
++m_errorsOccured;
|
|
|
|
}
|
|
|
|
// apply changings for child items as well
|
|
|
|
if(item->type() == ItemType::Dir) {
|
|
|
|
applyChangings(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_itemsProcessed += parentItem->children().size();
|
|
|
|
emit progress(m_itemsProcessed, m_errorsOccured);
|
|
|
|
}
|
|
|
|
|
2017-01-06 21:29:43 +01:00
|
|
|
void RenamingEngine::setError(const QList<FileSystemItem *> items)
|
2015-04-22 19:33:53 +02:00
|
|
|
{
|
2016-01-09 02:56:56 +01:00
|
|
|
for(FileSystemItem *item : items) {
|
2015-04-22 19:33:53 +02:00
|
|
|
item->setErrorOccured(true);
|
|
|
|
item->setNote(tr("skipped due to error of superior item"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-21 23:55:22 +02:00
|
|
|
#ifndef TAGEDITOR_NO_JSENGINE
|
2017-01-06 21:29:43 +01:00
|
|
|
void RenamingEngine::executeScriptForItem(const QFileInfo &fileInfo, FileSystemItem *item)
|
2015-04-22 19:33:53 +02:00
|
|
|
{
|
2016-01-09 02:56:56 +01:00
|
|
|
// make file info for the specified item available in the script
|
|
|
|
m_tagEditorQObj->setFileInfo(fileInfo, item);
|
2015-10-13 20:12:00 +02:00
|
|
|
// execute script
|
2016-01-09 02:56:56 +01:00
|
|
|
auto scriptResult = m_program.call();
|
|
|
|
if(scriptResult.isError()) {
|
2015-10-13 20:12:00 +02:00
|
|
|
// handle error
|
2015-04-22 19:33:53 +02:00
|
|
|
item->setErrorOccured(true);
|
2016-01-09 02:56:56 +01:00
|
|
|
item->setNote(scriptResult.toString());
|
2015-04-22 19:33:53 +02:00
|
|
|
} else {
|
2015-10-13 20:12:00 +02:00
|
|
|
// create preview for action
|
2016-01-09 02:56:56 +01:00
|
|
|
const QString &newName = m_tagEditorQObj->newName();
|
|
|
|
const QString &newRelativeDirectory = m_tagEditorQObj->newRelativeDirectory();
|
|
|
|
switch(m_tagEditorQObj->action()) {
|
|
|
|
case ActionType::None:
|
|
|
|
item->setNote(tr("no action specified"));
|
|
|
|
break;
|
2015-04-22 19:33:53 +02:00
|
|
|
case ActionType::Rename:
|
2016-01-09 02:56:56 +01:00
|
|
|
if(!newRelativeDirectory.isEmpty()) {
|
|
|
|
FileSystemItem *counterpartParent = item->root()->makeChildAvailable(newRelativeDirectory);
|
|
|
|
const QString &counterpartName = newName.isEmpty() ? item->name() : newName;
|
2015-04-22 19:33:53 +02:00
|
|
|
if(counterpartParent->findChild(counterpartName, item)) {
|
|
|
|
item->setNote(tr("name is already used at new location"));
|
|
|
|
item->setErrorOccured(true);
|
|
|
|
} else {
|
2015-10-13 20:12:00 +02:00
|
|
|
auto *counterpart = new FileSystemItem(ItemStatus::New, item->type(), counterpartName, counterpartParent);
|
2015-04-22 19:33:53 +02:00
|
|
|
item->setCounterpart(counterpart);
|
|
|
|
counterpart->setCheckable(true);
|
|
|
|
counterpart->setChecked(true);
|
|
|
|
}
|
2016-01-09 02:56:56 +01:00
|
|
|
} else if(!newName.isEmpty()) {
|
|
|
|
item->setNewName(newName);
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
|
|
|
if(FileSystemItem *newItem = item->counterpart()) {
|
|
|
|
if((newItem->name().isEmpty() || newItem->name() == item->name())
|
|
|
|
&& (newItem->parent() == item->parent())) {
|
|
|
|
item->setNote(tr("name doesn't change"));
|
|
|
|
} else if(newItem->parent() && newItem->parent()->findChild(newItem->name(), newItem)) {
|
|
|
|
item->setNote(tr("generated name is already used"));
|
|
|
|
item->setErrorOccured(true);
|
|
|
|
} else if(newItem->parent() == item->parent()) {
|
|
|
|
item->setNote(tr("will be renamed"));
|
|
|
|
newItem->setCheckable(true);
|
|
|
|
newItem->setChecked(true);
|
|
|
|
} else {
|
|
|
|
item->setNote(tr("will be moved"));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
item->setNote(tr("can not be renamed"));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
item->setNote(tr("skipped"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-21 23:55:22 +02:00
|
|
|
#endif
|
2015-04-22 19:33:53 +02:00
|
|
|
|
|
|
|
} // namespace RenamingUtility
|