ember/model/projectitem.cpp

138 lines
3.2 KiB
C++
Raw Normal View History

2018-08-06 18:37:16 +00:00
#include <algorithm>
#include <boost/filesystem.hpp>
#include <properties/config.h>
#include "project.h"
#include "projectitem.h"
namespace Ember
{
ProjectItem::ProjectItem(boost::filesystem::path path, ProjectItem* parent, Project* project)
: m_path(path),
m_type(),
m_typeLoaded(false),
m_parent(parent),
m_children(),
m_childrenLoaded(false),
m_project(project)
{
}
std::string ProjectItem::name() const
{
return m_path.filename().string();
}
ProjectItem::ProjectItemType ProjectItem::type()
{
if (!m_typeLoaded)
{
if (boost::filesystem::is_directory(m_path))
{
m_type = ProjectItemType::DIRECTORY;
}
else if (boost::filesystem::exists(m_path))
{
std::string ext = m_path.extension().string();
if (ext == ".png" || ext == ".jpg" || ext == ".bmp" || ext == ".jpeg")
m_type = ProjectItemType::IMAGE;
else if (ext == ".mp3" || ext == ".ogg" || ext == ".wav")
m_type = ProjectItemType::AUDIO;
else if (ext == ".avi" || ext == ".mp4" || ext == ".wmv")
m_type = ProjectItemType::VIDEO;
else if (ext == ".srt")
m_type = ProjectItemType::SUBTITLES;
else if (ext == EMBER_PROJECT_EXTENSION)
m_type = ProjectItemType::PROJECT;
else if (ext == EMBER_COMPOSITION_EXTENSION)
m_type = ProjectItemType::COMPOSITION;
else if (ext == EMBER_SEQUENCE_EXTENSION)
m_type = ProjectItemType::SEQUENCE;
else m_type = ProjectItemType::UNKNOWN;
}
else
{
m_type = ProjectItemType::MISSING;
}
m_typeLoaded = true;
}
return m_type;
}
ProjectItem *ProjectItem::parent() const
{
return m_parent;
}
Project *ProjectItem::project() const
{
return m_project;
}
const std::vector<ProjectItem *>& ProjectItem::children()
{
if (!m_childrenLoaded)
{
if (type() == ProjectItemType::DIRECTORY)
{
for (auto&& entry : boost::filesystem::directory_iterator(m_path))
{
ProjectItem* child = new ProjectItem(entry.path(), this, m_project);
m_children.push_back(child);
}
}
m_childrenLoaded = true;
}
return m_children;
}
boost::filesystem::path ProjectItem::path() const
{
return m_path;
}
void ProjectItem::rename(const std::string &name)
{
auto newPath = m_path.parent_path().append(name);
boost::filesystem::rename(m_path, newPath);
}
void ProjectItem::move(ProjectItem *newParent)
{
auto newPath = newParent->path().append(name());
boost::filesystem::rename(m_path, newPath);
// Update parenting
if (m_parent)
m_parent->unparentChild(this);
newParent->m_children.push_back(this);
}
void ProjectItem::unlink()
{
boost::filesystem::remove(m_path);
// Update parenting
if (m_parent)
delete m_parent->unparentChild(this);
}
ProjectItem *ProjectItem::unparentChild(ProjectItem *child)
{
auto it = std::find(m_children.begin(), m_children.end(), child);
if (it != m_children.end())
m_parent->m_children.erase(it);
return child;
}
}