2015-09-06 20:20:00 +02:00
|
|
|
#include "./filesystemitem.h"
|
2015-04-22 19:33:53 +02:00
|
|
|
|
2017-01-06 22:07:03 +01:00
|
|
|
#include <QCoreApplication>
|
2018-03-07 01:18:01 +01:00
|
|
|
#include <QDir>
|
2015-04-22 19:33:53 +02:00
|
|
|
|
|
|
|
namespace RenamingUtility {
|
|
|
|
|
|
|
|
/*
|
|
|
|
TRANSLATOR RenamingUtility::FileSystemItem
|
|
|
|
Necessary for lupdate.
|
|
|
|
*/
|
|
|
|
|
2018-03-07 01:18:01 +01:00
|
|
|
const QString &emptyStr()
|
|
|
|
{
|
2015-04-22 19:33:53 +02:00
|
|
|
static QString emptyStr;
|
|
|
|
return emptyStr;
|
|
|
|
}
|
|
|
|
|
2018-03-07 01:18:01 +01:00
|
|
|
FileSystemItem::FileSystemItem(ItemStatus status, ItemType type, const QString &name, FileSystemItem *parent)
|
|
|
|
: m_parent(parent)
|
|
|
|
, 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
|
|
|
{
|
2018-03-07 01:18:01 +01:00
|
|
|
if (m_parent) {
|
2015-10-13 20:12:00 +02:00
|
|
|
m_parent->m_children << this;
|
|
|
|
}
|
|
|
|
}
|
2015-04-22 19:33:53 +02:00
|
|
|
|
|
|
|
FileSystemItem::~FileSystemItem()
|
|
|
|
{
|
2018-08-15 21:49:06 +02:00
|
|
|
for (auto *const child : m_children) {
|
2015-04-22 19:33:53 +02:00
|
|
|
child->m_parent = nullptr;
|
|
|
|
delete child;
|
|
|
|
}
|
2018-03-07 01:18:01 +01:00
|
|
|
if (m_parent) {
|
2015-04-22 19:33:53 +02:00
|
|
|
m_parent->m_children.removeAll(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileSystemItem::setParent(FileSystemItem *parent)
|
|
|
|
{
|
2018-08-15 21:49:06 +02:00
|
|
|
if (parent == m_parent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
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;
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString &FileSystemItem::currentName() const
|
|
|
|
{
|
2018-03-07 01:18:01 +01:00
|
|
|
switch (m_status) {
|
2015-04-22 19:33:53 +02:00
|
|
|
case ItemStatus::New:
|
2018-03-07 01:18:01 +01:00
|
|
|
return m_counterpart ? m_counterpart->name() : emptyStr();
|
2015-04-22 19:33:53 +02:00
|
|
|
case ItemStatus::Current:
|
|
|
|
return m_name;
|
|
|
|
}
|
2017-06-10 21:55:07 +02:00
|
|
|
return emptyStr();
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileSystemItem::setCurrentName(const QString ¤tName)
|
|
|
|
{
|
2018-03-07 01:18:01 +01:00
|
|
|
switch (m_status) {
|
2015-04-22 19:33:53 +02:00
|
|
|
case ItemStatus::New:
|
2018-03-07 01:18:01 +01:00
|
|
|
if (!m_counterpart) {
|
2015-04-22 19:33:53 +02:00
|
|
|
// 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
|
|
|
|
{
|
2018-03-07 01:18:01 +01:00
|
|
|
switch (m_status) {
|
2015-04-22 19:33:53 +02:00
|
|
|
case ItemStatus::Current:
|
2018-03-07 01:18:01 +01:00
|
|
|
return m_counterpart ? m_counterpart->name() : emptyStr();
|
2015-04-22 19:33:53 +02:00
|
|
|
case ItemStatus::New:
|
|
|
|
return m_name;
|
|
|
|
}
|
2017-06-10 21:55:07 +02:00
|
|
|
return emptyStr();
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileSystemItem::setNewName(const QString &newName)
|
|
|
|
{
|
2018-03-07 01:18:01 +01:00
|
|
|
switch (m_status) {
|
2015-04-22 19:33:53 +02:00
|
|
|
case ItemStatus::Current:
|
2018-08-15 21:49:06 +02:00
|
|
|
if (m_counterpart) {
|
2015-04-22 19:33:53 +02:00
|
|
|
m_counterpart->setName(newName);
|
2018-08-15 21:49:06 +02:00
|
|
|
return true;
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
2018-08-15 21:49:06 +02:00
|
|
|
if (m_parent) {
|
|
|
|
m_counterpart = new FileSystemItem(ItemStatus::New, m_type, newName, m_parent);
|
|
|
|
m_counterpart->m_counterpart = this;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2015-04-22 19:33:53 +02:00
|
|
|
case ItemStatus::New:
|
|
|
|
setName(newName);
|
|
|
|
return true;
|
|
|
|
}
|
2017-06-10 21:55:07 +02:00
|
|
|
return false;
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FileSystemItem *FileSystemItem::findChild(const QString &name) const
|
|
|
|
{
|
2018-08-15 21:49:06 +02:00
|
|
|
for (auto *const child : m_children) {
|
2018-03-07 01:18:01 +01:00
|
|
|
if (child->name() == name) {
|
2015-04-22 19:33:53 +02:00
|
|
|
return child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileSystemItem *FileSystemItem::findChild(const QString &name, const FileSystemItem *exclude) const
|
|
|
|
{
|
2018-08-15 21:49:06 +02:00
|
|
|
for (auto *const child : m_children) {
|
2018-03-07 01:18:01 +01:00
|
|
|
if (child != exclude && child->name() == name) {
|
2015-04-22 19:33:53 +02:00
|
|
|
return child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileSystemItem *FileSystemItem::makeChildAvailable(const QString &relativePath)
|
|
|
|
{
|
2020-06-11 21:33:06 +02:00
|
|
|
auto dirs = relativePath.split(QDir::separator(),
|
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
|
|
|
|
QString::SkipEmptyParts
|
|
|
|
#else
|
|
|
|
Qt::SkipEmptyParts
|
|
|
|
#endif
|
|
|
|
);
|
2018-08-15 21:49:06 +02:00
|
|
|
if (dirs.isEmpty()) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto *parent = this;
|
|
|
|
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) {
|
|
|
|
auto *child = parent->findChild(dir);
|
|
|
|
if (!child) {
|
|
|
|
child = new FileSystemItem(ItemStatus::New, ItemType::Dir, dir, parent);
|
|
|
|
child->setNote(QCoreApplication::translate("RenamingUtility::FileSystemItem", "will be created"));
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
2018-08-15 21:49:06 +02:00
|
|
|
parent = child;
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileSystemItem::relativeDir(QString &res) const
|
|
|
|
{
|
2018-03-07 01:18:01 +01:00
|
|
|
if (m_parent) {
|
2015-04-22 19:33:53 +02:00
|
|
|
m_parent->relativePath(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString FileSystemItem::relativeDir() const
|
|
|
|
{
|
|
|
|
QString dir;
|
|
|
|
relativeDir(dir);
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileSystemItem::relativePath(QString &res) const
|
|
|
|
{
|
2018-08-15 21:49:06 +02:00
|
|
|
if (!m_parent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_parent->relativePath(res);
|
|
|
|
if (!res.isEmpty()) {
|
|
|
|
res.append(QLatin1Char('/'));
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
2018-08-15 21:49:06 +02:00
|
|
|
res.append(name());
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString FileSystemItem::relativePath() const
|
|
|
|
{
|
|
|
|
QString path;
|
|
|
|
relativePath(path);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileSystemItem::hasSibling(const QString &name) const
|
|
|
|
{
|
2018-08-15 21:49:06 +02:00
|
|
|
if (!m_parent) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const auto &siblings = m_parent->children();
|
|
|
|
for (const auto *const siblingItem : siblings) {
|
|
|
|
if (siblingItem == this) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((!siblingItem->newName().isEmpty() && siblingItem->newName() == name) || siblingItem->name() == name) {
|
|
|
|
return true;
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-07 01:18:01 +01:00
|
|
|
} // namespace RenamingUtility
|