2015-09-06 20:20:00 +02:00
|
|
|
#include "./filesystemitem.h"
|
2015-04-22 19:33:53 +02:00
|
|
|
|
|
|
|
#include <QDir>
|
2017-01-06 22:07:03 +01:00
|
|
|
#include <QCoreApplication>
|
2015-04-22 19:33:53 +02:00
|
|
|
|
|
|
|
namespace RenamingUtility {
|
|
|
|
|
|
|
|
/*
|
|
|
|
TRANSLATOR RenamingUtility::FileSystemItem
|
|
|
|
Necessary for lupdate.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const QString &emptyStr() {
|
|
|
|
static QString emptyStr;
|
|
|
|
return emptyStr;
|
|
|
|
}
|
|
|
|
|
2015-10-13 20:12:00 +02:00
|
|
|
FileSystemItem::FileSystemItem(ItemStatus status, ItemType type, const QString &name, FileSystemItem *parent) :
|
|
|
|
m_parent(parent),
|
2015-04-22 19:33:53 +02:00
|
|
|
m_counterpart(nullptr),
|
|
|
|
m_status(status),
|
|
|
|
m_type(type),
|
|
|
|
m_error(false),
|
|
|
|
m_applied(false),
|
|
|
|
m_name(name),
|
|
|
|
m_checked(false),
|
|
|
|
m_checkable(false)
|
2015-10-13 20:12:00 +02:00
|
|
|
{
|
|
|
|
if(m_parent) {
|
|
|
|
m_parent->m_children << this;
|
|
|
|
}
|
|
|
|
}
|
2015-04-22 19:33:53 +02:00
|
|
|
|
|
|
|
FileSystemItem::~FileSystemItem()
|
|
|
|
{
|
2016-03-05 17:12:16 +01:00
|
|
|
for(FileSystemItem *child : m_children) {
|
2015-04-22 19:33:53 +02:00
|
|
|
child->m_parent = nullptr;
|
|
|
|
delete child;
|
|
|
|
}
|
|
|
|
if(m_parent) {
|
|
|
|
m_parent->m_children.removeAll(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileSystemItem::setParent(FileSystemItem *parent)
|
|
|
|
{
|
|
|
|
if(parent != m_parent) {
|
|
|
|
if(m_parent) {
|
|
|
|
m_parent->m_children.removeAll(this);
|
|
|
|
}
|
|
|
|
m_parent = parent;
|
|
|
|
if(m_parent && !m_parent->m_children.contains(this)) {
|
|
|
|
m_parent->m_children << this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString &FileSystemItem::currentName() const
|
|
|
|
{
|
|
|
|
switch(m_status) {
|
|
|
|
case ItemStatus::New:
|
|
|
|
return m_counterpart
|
|
|
|
? m_counterpart->name()
|
|
|
|
: emptyStr();
|
|
|
|
case ItemStatus::Current:
|
|
|
|
return m_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileSystemItem::setCurrentName(const QString ¤tName)
|
|
|
|
{
|
|
|
|
switch(m_status) {
|
|
|
|
case ItemStatus::New:
|
|
|
|
if(!m_counterpart) {
|
|
|
|
// creating an imaginary current file doesn't make sense here
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
m_counterpart->setName(currentName);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ItemStatus::Current:
|
|
|
|
setName(currentName);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString &FileSystemItem::newName() const
|
|
|
|
{
|
|
|
|
switch(m_status) {
|
|
|
|
case ItemStatus::Current:
|
|
|
|
return m_counterpart
|
|
|
|
? m_counterpart->name()
|
|
|
|
: emptyStr();
|
|
|
|
case ItemStatus::New:
|
|
|
|
return m_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileSystemItem::setNewName(const QString &newName)
|
|
|
|
{
|
|
|
|
switch(m_status) {
|
|
|
|
case ItemStatus::Current:
|
|
|
|
if(!m_counterpart) {
|
|
|
|
if(m_parent) {
|
|
|
|
m_counterpart = new FileSystemItem(ItemStatus::New, m_type, newName);
|
|
|
|
m_counterpart->m_counterpart = this;
|
|
|
|
m_counterpart->setParent(m_parent);
|
|
|
|
} else {
|
|
|
|
// parent required
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m_counterpart->setName(newName);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case ItemStatus::New:
|
|
|
|
setName(newName);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FileSystemItem *FileSystemItem::findChild(const QString &name) const
|
|
|
|
{
|
2016-03-05 17:12:16 +01:00
|
|
|
for(FileSystemItem *child : m_children) {
|
2015-04-22 19:33:53 +02:00
|
|
|
if(child->name() == name) {
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileSystemItem *FileSystemItem::findChild(const QString &name, const FileSystemItem *exclude) const
|
|
|
|
{
|
2016-03-05 17:12:16 +01:00
|
|
|
for(FileSystemItem *child : m_children) {
|
2015-04-22 19:33:53 +02:00
|
|
|
if(child != exclude && child->name() == name) {
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileSystemItem *FileSystemItem::makeChildAvailable(const QString &relativePath)
|
|
|
|
{
|
|
|
|
QStringList dirs = relativePath.split(QDir::separator(), QString::SkipEmptyParts);
|
|
|
|
FileSystemItem *parent = this;
|
2016-05-06 23:16:37 +02:00
|
|
|
if(!dirs.isEmpty()) {
|
|
|
|
if(relativePath.startsWith(QChar('/'))) {
|
|
|
|
// we actually just got an absolute path
|
|
|
|
// -> just leave the / there to handle absolute path as well
|
|
|
|
dirs.front().prepend(QChar('/'));
|
|
|
|
}
|
|
|
|
for(const QString &dir : dirs) {
|
|
|
|
FileSystemItem *child = parent->findChild(dir);
|
|
|
|
if(!child) {
|
|
|
|
child = new FileSystemItem(ItemStatus::New, ItemType::Dir, dir);
|
|
|
|
child->setParent(parent);
|
2017-01-06 22:07:03 +01:00
|
|
|
child->setNote(QCoreApplication::translate("RenamingUtility::FileSystemItem", "will be created"));
|
2016-05-06 23:16:37 +02:00
|
|
|
}
|
|
|
|
parent = child;
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileSystemItem::relativeDir(QString &res) const
|
|
|
|
{
|
|
|
|
if(m_parent) {
|
|
|
|
m_parent->relativePath(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString FileSystemItem::relativeDir() const
|
|
|
|
{
|
|
|
|
QString dir;
|
|
|
|
relativeDir(dir);
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileSystemItem::relativePath(QString &res) const
|
|
|
|
{
|
|
|
|
if(m_parent) {
|
|
|
|
m_parent->relativePath(res);
|
|
|
|
if(!res.isEmpty()) {
|
2016-05-06 23:16:37 +02:00
|
|
|
res.append(QLatin1Char('/'));
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
|
|
|
res.append(name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString FileSystemItem::relativePath() const
|
|
|
|
{
|
|
|
|
QString path;
|
|
|
|
relativePath(path);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileSystemItem::hasSibling(const QString &name) const
|
|
|
|
{
|
|
|
|
if(m_parent) {
|
|
|
|
const QList<FileSystemItem *> &siblings = m_parent->children();
|
2017-01-06 22:15:39 +01:00
|
|
|
for(const FileSystemItem *siblingItem : siblings) {
|
2015-04-22 19:33:53 +02:00
|
|
|
if(siblingItem == this) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(!siblingItem->newName().isEmpty() && siblingItem->newName() == name) {
|
|
|
|
return true;
|
|
|
|
} else if(siblingItem->name() == name) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|