123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- #include "node.h"
- #include <QDir>
- #include <notebookconfigmgr/inotebookconfigmgr.h>
- #include <notebookbackend/inotebookbackend.h>
- #include <utils/pathutils.h>
- #include <core/exception.h>
- #include "notebook.h"
- using namespace vnotex;
- Node::Node(Type p_type,
- ID p_id,
- const QString &p_name,
- const QDateTime &p_createdTimeUtc,
- Notebook *p_notebook,
- Node *p_parent)
- : m_type(p_type),
- m_id(p_id),
- m_name(p_name),
- m_createdTimeUtc(p_createdTimeUtc),
- m_loaded(true),
- m_notebook(p_notebook),
- m_parent(p_parent)
- {
- if (m_notebook) {
- m_configMgr = m_notebook->getConfigMgr();
- m_backend = m_notebook->getBackend();
- }
- }
- Node::Node(Type p_type,
- const QString &p_name,
- Notebook *p_notebook,
- Node *p_parent)
- : m_type(p_type),
- m_name(p_name),
- m_notebook(p_notebook),
- m_parent(p_parent)
- {
- if (m_notebook) {
- m_configMgr = m_notebook->getConfigMgr();
- m_backend = m_notebook->getBackend();
- }
- }
- Node::~Node()
- {
- }
- bool Node::isLoaded() const
- {
- return m_loaded;
- }
- void Node::setLoaded(bool p_loaded)
- {
- m_loaded = p_loaded;
- }
- void Node::loadInfo(ID p_id, const QDateTime &p_createdTimeUtc)
- {
- Q_ASSERT(!m_loaded);
- m_id = p_id;
- m_createdTimeUtc = p_createdTimeUtc;
- m_loaded = true;
- }
- bool Node::isRoot() const
- {
- return !m_parent;
- }
- const QString &Node::getName() const
- {
- return m_name;
- }
- bool Node::hasChild(const QString &p_name, bool p_caseSensitive) const
- {
- return findChild(p_name, p_caseSensitive) != nullptr;
- }
- bool Node::hasChild(const QSharedPointer<Node> &p_node) const
- {
- return getChildren().indexOf(p_node) != -1;
- }
- QSharedPointer<Node> Node::findChild(const QString &p_name, bool p_caseSensitive) const
- {
- auto targetName = p_caseSensitive ? p_name : p_name.toLower();
- for (auto &child : getChildren()) {
- if (p_caseSensitive ? child->getName() == targetName
- : child->getName().toLower() == targetName) {
- return child;
- }
- }
- return nullptr;
- }
- void Node::setParent(Node *p_parent)
- {
- m_parent = p_parent;
- }
- Node *Node::getParent() const
- {
- return m_parent;
- }
- Node::Type Node::getType() const
- {
- return m_type;
- }
- Node::Flags Node::getFlags() const
- {
- return m_flags;
- }
- void Node::setFlags(Node::Flags p_flags)
- {
- m_flags = p_flags;
- }
- Node::Use Node::getUse() const
- {
- return m_use;
- }
- void Node::setUse(Node::Use p_use)
- {
- m_use = p_use;
- }
- ID Node::getId() const
- {
- return m_id;
- }
- const QDateTime &Node::getCreatedTimeUtc() const
- {
- return m_createdTimeUtc;
- }
- Notebook *Node::getNotebook() const
- {
- return m_notebook;
- }
- QString Node::fetchRelativePath() const
- {
- if (!m_parent) {
- return QString();
- } else {
- return PathUtils::concatenateFilePath(m_parent->fetchRelativePath(), m_name);
- }
- }
- QString Node::fetchAbsolutePath() const
- {
- return PathUtils::concatenateFilePath(m_notebook->getRootFolderAbsolutePath(),
- fetchRelativePath());
- }
- QString Node::fetchContentPath() const
- {
- return fetchAbsolutePath();
- }
- void Node::load()
- {
- Q_ASSERT(m_notebook);
- m_notebook->load(this);
- }
- void Node::save()
- {
- Q_ASSERT(m_notebook);
- m_notebook->save(this);
- }
- void Node::setName(const QString &p_name)
- {
- m_name = p_name;
- }
- void Node::updateName(const QString &p_name)
- {
- if (m_name == p_name) {
- return;
- }
- m_notebook->rename(this, p_name);
- Q_ASSERT(m_name == p_name);
- }
- bool Node::isAncestor(const Node *p_ancestor, const Node *p_child)
- {
- if (!p_ancestor || !p_child) {
- return false;
- }
- while (p_child) {
- p_child = p_child->getParent();
- if (p_child == p_ancestor) {
- return true;
- }
- }
- return false;
- }
- bool Node::existsOnDisk() const
- {
- return m_configMgr->nodeExistsOnDisk(this);
- }
- QString Node::read() const
- {
- return m_configMgr->readNode(this);
- }
- void Node::write(const QString &p_content)
- {
- m_configMgr->writeNode(this, p_content);
- }
- QString Node::fetchImageFolderPath()
- {
- return m_configMgr->fetchNodeImageFolderPath(this);
- }
- QString Node::insertImage(const QString &p_srcImagePath, const QString &p_imageFileName)
- {
- const auto imageFolderPath = fetchImageFolderPath();
- auto destFilePath = m_backend->renameIfExistsCaseInsensitive(PathUtils::concatenateFilePath(imageFolderPath, p_imageFileName));
- m_backend->copyFile(p_srcImagePath, destFilePath);
- return destFilePath;
- }
- QString Node::insertImage(const QImage &p_image, const QString &p_imageFileName)
- {
- const auto imageFolderPath = fetchImageFolderPath();
- auto destFilePath = m_backend->renameIfExistsCaseInsensitive(PathUtils::concatenateFilePath(imageFolderPath, p_imageFileName));
- p_image.save(destFilePath);
- m_backend->addFile(destFilePath);
- return destFilePath;
- }
- void Node::removeImage(const QString &p_imagePath)
- {
- // Just move it to recycle bin but not added as a child node of recycle bin.
- m_notebook->moveFileToRecycleBin(p_imagePath);
- }
- QString Node::getAttachmentFolder() const
- {
- Q_ASSERT(false);
- return QString();
- }
- void Node::setAttachmentFolder(const QString &p_folder)
- {
- Q_UNUSED(p_folder);
- Q_ASSERT(false);
- }
- QString Node::fetchAttachmentFolderPath()
- {
- return m_configMgr->fetchNodeAttachmentFolderPath(this);
- }
- QStringList Node::addAttachment(const QString &p_destFolderPath, const QStringList &p_files)
- {
- Q_UNUSED(p_destFolderPath);
- Q_UNUSED(p_files);
- Q_ASSERT(false);
- return QStringList();
- }
- QString Node::newAttachmentFile(const QString &p_destFolderPath, const QString &p_name)
- {
- Q_UNUSED(p_destFolderPath);
- Q_UNUSED(p_name);
- Q_ASSERT(false);
- return QString();
- }
- QString Node::newAttachmentFolder(const QString &p_destFolderPath, const QString &p_name)
- {
- Q_UNUSED(p_destFolderPath);
- Q_UNUSED(p_name);
- Q_ASSERT(false);
- return QString();
- }
- QString Node::renameAttachment(const QString &p_path, const QString &p_name)
- {
- Q_UNUSED(p_path);
- Q_UNUSED(p_name);
- Q_ASSERT(false);
- return QString();
- }
- void Node::removeAttachment(const QStringList &p_paths)
- {
- Q_UNUSED(p_paths);
- Q_ASSERT(false);
- }
- QStringList Node::getTags() const
- {
- Q_ASSERT(false);
- return QStringList();
- }
- QDir Node::toDir() const
- {
- Q_ASSERT(false);
- return QDir();
- }
- INotebookBackend *Node::getBackend() const
- {
- return m_backend.data();
- }
- bool Node::isReadOnly() const
- {
- return m_flags & Flag::ReadOnly;
- }
|