node.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #include "node.h"
  2. #include <QDir>
  3. #include <notebookconfigmgr/inotebookconfigmgr.h>
  4. #include <notebookbackend/inotebookbackend.h>
  5. #include <utils/pathutils.h>
  6. #include <core/exception.h>
  7. #include "notebook.h"
  8. using namespace vnotex;
  9. Node::Node(Type p_type,
  10. ID p_id,
  11. const QString &p_name,
  12. const QDateTime &p_createdTimeUtc,
  13. Notebook *p_notebook,
  14. Node *p_parent)
  15. : m_type(p_type),
  16. m_id(p_id),
  17. m_name(p_name),
  18. m_createdTimeUtc(p_createdTimeUtc),
  19. m_loaded(true),
  20. m_notebook(p_notebook),
  21. m_parent(p_parent)
  22. {
  23. if (m_notebook) {
  24. m_configMgr = m_notebook->getConfigMgr();
  25. m_backend = m_notebook->getBackend();
  26. }
  27. }
  28. Node::Node(Type p_type,
  29. const QString &p_name,
  30. Notebook *p_notebook,
  31. Node *p_parent)
  32. : m_type(p_type),
  33. m_name(p_name),
  34. m_notebook(p_notebook),
  35. m_parent(p_parent)
  36. {
  37. if (m_notebook) {
  38. m_configMgr = m_notebook->getConfigMgr();
  39. m_backend = m_notebook->getBackend();
  40. }
  41. }
  42. Node::~Node()
  43. {
  44. }
  45. bool Node::isLoaded() const
  46. {
  47. return m_loaded;
  48. }
  49. void Node::setLoaded(bool p_loaded)
  50. {
  51. m_loaded = p_loaded;
  52. }
  53. void Node::loadInfo(ID p_id, const QDateTime &p_createdTimeUtc)
  54. {
  55. Q_ASSERT(!m_loaded);
  56. m_id = p_id;
  57. m_createdTimeUtc = p_createdTimeUtc;
  58. m_loaded = true;
  59. }
  60. bool Node::isRoot() const
  61. {
  62. return !m_parent;
  63. }
  64. const QString &Node::getName() const
  65. {
  66. return m_name;
  67. }
  68. bool Node::hasChild(const QString &p_name, bool p_caseSensitive) const
  69. {
  70. return findChild(p_name, p_caseSensitive) != nullptr;
  71. }
  72. bool Node::hasChild(const QSharedPointer<Node> &p_node) const
  73. {
  74. return getChildren().indexOf(p_node) != -1;
  75. }
  76. QSharedPointer<Node> Node::findChild(const QString &p_name, bool p_caseSensitive) const
  77. {
  78. auto targetName = p_caseSensitive ? p_name : p_name.toLower();
  79. for (auto &child : getChildren()) {
  80. if (p_caseSensitive ? child->getName() == targetName
  81. : child->getName().toLower() == targetName) {
  82. return child;
  83. }
  84. }
  85. return nullptr;
  86. }
  87. void Node::setParent(Node *p_parent)
  88. {
  89. m_parent = p_parent;
  90. }
  91. Node *Node::getParent() const
  92. {
  93. return m_parent;
  94. }
  95. Node::Type Node::getType() const
  96. {
  97. return m_type;
  98. }
  99. Node::Flags Node::getFlags() const
  100. {
  101. return m_flags;
  102. }
  103. void Node::setFlags(Node::Flags p_flags)
  104. {
  105. m_flags = p_flags;
  106. }
  107. Node::Use Node::getUse() const
  108. {
  109. return m_use;
  110. }
  111. void Node::setUse(Node::Use p_use)
  112. {
  113. m_use = p_use;
  114. }
  115. ID Node::getId() const
  116. {
  117. return m_id;
  118. }
  119. const QDateTime &Node::getCreatedTimeUtc() const
  120. {
  121. return m_createdTimeUtc;
  122. }
  123. Notebook *Node::getNotebook() const
  124. {
  125. return m_notebook;
  126. }
  127. QString Node::fetchRelativePath() const
  128. {
  129. if (!m_parent) {
  130. return QString();
  131. } else {
  132. return PathUtils::concatenateFilePath(m_parent->fetchRelativePath(), m_name);
  133. }
  134. }
  135. QString Node::fetchAbsolutePath() const
  136. {
  137. return PathUtils::concatenateFilePath(m_notebook->getRootFolderAbsolutePath(),
  138. fetchRelativePath());
  139. }
  140. QString Node::fetchContentPath() const
  141. {
  142. return fetchAbsolutePath();
  143. }
  144. void Node::load()
  145. {
  146. Q_ASSERT(m_notebook);
  147. m_notebook->load(this);
  148. }
  149. void Node::save()
  150. {
  151. Q_ASSERT(m_notebook);
  152. m_notebook->save(this);
  153. }
  154. void Node::setName(const QString &p_name)
  155. {
  156. m_name = p_name;
  157. }
  158. void Node::updateName(const QString &p_name)
  159. {
  160. if (m_name == p_name) {
  161. return;
  162. }
  163. m_notebook->rename(this, p_name);
  164. Q_ASSERT(m_name == p_name);
  165. }
  166. bool Node::isAncestor(const Node *p_ancestor, const Node *p_child)
  167. {
  168. if (!p_ancestor || !p_child) {
  169. return false;
  170. }
  171. while (p_child) {
  172. p_child = p_child->getParent();
  173. if (p_child == p_ancestor) {
  174. return true;
  175. }
  176. }
  177. return false;
  178. }
  179. bool Node::existsOnDisk() const
  180. {
  181. return m_configMgr->nodeExistsOnDisk(this);
  182. }
  183. QString Node::read() const
  184. {
  185. return m_configMgr->readNode(this);
  186. }
  187. void Node::write(const QString &p_content)
  188. {
  189. m_configMgr->writeNode(this, p_content);
  190. }
  191. QString Node::fetchImageFolderPath()
  192. {
  193. return m_configMgr->fetchNodeImageFolderPath(this);
  194. }
  195. QString Node::insertImage(const QString &p_srcImagePath, const QString &p_imageFileName)
  196. {
  197. const auto imageFolderPath = fetchImageFolderPath();
  198. auto destFilePath = m_backend->renameIfExistsCaseInsensitive(PathUtils::concatenateFilePath(imageFolderPath, p_imageFileName));
  199. m_backend->copyFile(p_srcImagePath, destFilePath);
  200. return destFilePath;
  201. }
  202. QString Node::insertImage(const QImage &p_image, const QString &p_imageFileName)
  203. {
  204. const auto imageFolderPath = fetchImageFolderPath();
  205. auto destFilePath = m_backend->renameIfExistsCaseInsensitive(PathUtils::concatenateFilePath(imageFolderPath, p_imageFileName));
  206. p_image.save(destFilePath);
  207. m_backend->addFile(destFilePath);
  208. return destFilePath;
  209. }
  210. void Node::removeImage(const QString &p_imagePath)
  211. {
  212. // Just move it to recycle bin but not added as a child node of recycle bin.
  213. m_notebook->moveFileToRecycleBin(p_imagePath);
  214. }
  215. QString Node::getAttachmentFolder() const
  216. {
  217. Q_ASSERT(false);
  218. return QString();
  219. }
  220. void Node::setAttachmentFolder(const QString &p_folder)
  221. {
  222. Q_UNUSED(p_folder);
  223. Q_ASSERT(false);
  224. }
  225. QString Node::fetchAttachmentFolderPath()
  226. {
  227. return m_configMgr->fetchNodeAttachmentFolderPath(this);
  228. }
  229. QStringList Node::addAttachment(const QString &p_destFolderPath, const QStringList &p_files)
  230. {
  231. Q_UNUSED(p_destFolderPath);
  232. Q_UNUSED(p_files);
  233. Q_ASSERT(false);
  234. return QStringList();
  235. }
  236. QString Node::newAttachmentFile(const QString &p_destFolderPath, const QString &p_name)
  237. {
  238. Q_UNUSED(p_destFolderPath);
  239. Q_UNUSED(p_name);
  240. Q_ASSERT(false);
  241. return QString();
  242. }
  243. QString Node::newAttachmentFolder(const QString &p_destFolderPath, const QString &p_name)
  244. {
  245. Q_UNUSED(p_destFolderPath);
  246. Q_UNUSED(p_name);
  247. Q_ASSERT(false);
  248. return QString();
  249. }
  250. QString Node::renameAttachment(const QString &p_path, const QString &p_name)
  251. {
  252. Q_UNUSED(p_path);
  253. Q_UNUSED(p_name);
  254. Q_ASSERT(false);
  255. return QString();
  256. }
  257. void Node::removeAttachment(const QStringList &p_paths)
  258. {
  259. Q_UNUSED(p_paths);
  260. Q_ASSERT(false);
  261. }
  262. QStringList Node::getTags() const
  263. {
  264. Q_ASSERT(false);
  265. return QStringList();
  266. }
  267. QDir Node::toDir() const
  268. {
  269. Q_ASSERT(false);
  270. return QDir();
  271. }
  272. INotebookBackend *Node::getBackend() const
  273. {
  274. return m_backend.data();
  275. }
  276. bool Node::isReadOnly() const
  277. {
  278. return m_flags & Flag::ReadOnly;
  279. }