1
0

node.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. #include "nodeparameters.h"
  9. using namespace vnotex;
  10. Node::Node(Flags p_flags,
  11. const QString &p_name,
  12. const NodeParameters &p_paras,
  13. Notebook *p_notebook,
  14. Node *p_parent)
  15. : m_notebook(p_notebook),
  16. m_loaded(true),
  17. m_flags(p_flags),
  18. m_id(p_paras.m_id),
  19. m_signature(p_paras.m_signature),
  20. m_name(p_name),
  21. m_createdTimeUtc(p_paras.m_createdTimeUtc),
  22. m_modifiedTimeUtc(p_paras.m_modifiedTimeUtc),
  23. m_tags(p_paras.m_tags),
  24. m_attachmentFolder(p_paras.m_attachmentFolder),
  25. m_parent(p_parent)
  26. {
  27. Q_ASSERT(m_notebook);
  28. checkSignature();
  29. }
  30. Node::Node(Flags p_flags,
  31. const QString &p_name,
  32. Notebook *p_notebook,
  33. Node *p_parent)
  34. : m_notebook(p_notebook),
  35. m_flags(p_flags),
  36. m_name(p_name),
  37. m_parent(p_parent)
  38. {
  39. Q_ASSERT(m_notebook);
  40. }
  41. Node::~Node()
  42. {
  43. }
  44. bool Node::isLoaded() const
  45. {
  46. return m_loaded;
  47. }
  48. void Node::loadCompleteInfo(const NodeParameters &p_paras,
  49. const QVector<QSharedPointer<Node>> &p_children)
  50. {
  51. Q_ASSERT(!m_loaded);
  52. m_id = p_paras.m_id;
  53. m_signature = p_paras.m_signature;
  54. m_createdTimeUtc = p_paras.m_createdTimeUtc;
  55. m_modifiedTimeUtc = p_paras.m_modifiedTimeUtc;
  56. Q_ASSERT(p_paras.m_tags.isEmpty());
  57. Q_ASSERT(p_paras.m_attachmentFolder.isEmpty());
  58. m_children = p_children;
  59. m_loaded = true;
  60. checkSignature();
  61. }
  62. bool Node::isRoot() const
  63. {
  64. return !m_parent && m_use == Use::Root;
  65. }
  66. const QString &Node::getName() const
  67. {
  68. return m_name;
  69. }
  70. void Node::setName(const QString &p_name)
  71. {
  72. m_name = p_name;
  73. }
  74. void Node::updateName(const QString &p_name)
  75. {
  76. if (m_name == p_name) {
  77. return;
  78. }
  79. getConfigMgr()->renameNode(this, p_name);
  80. Q_ASSERT(m_name == p_name);
  81. emit m_notebook->nodeUpdated(this);
  82. }
  83. bool Node::containsChild(const QString &p_name, bool p_caseSensitive) const
  84. {
  85. return findChild(p_name, p_caseSensitive) != nullptr;
  86. }
  87. bool Node::containsChild(const QSharedPointer<Node> &p_node) const
  88. {
  89. return m_children.indexOf(p_node) != -1;
  90. }
  91. bool Node::isLegalNameForNewChild(const QString &p_name) const
  92. {
  93. if (p_name.isEmpty()) {
  94. return false;
  95. }
  96. auto mgr = getConfigMgr();
  97. if (mgr->isBuiltInFile(this, p_name) || mgr->isBuiltInFolder(this, p_name)) {
  98. return false;
  99. }
  100. if (containsChild(p_name, false)) {
  101. return false;
  102. }
  103. return true;
  104. }
  105. QSharedPointer<Node> Node::findChild(const QString &p_name, bool p_caseSensitive) const
  106. {
  107. auto targetName = p_caseSensitive ? p_name : p_name.toLower();
  108. for (const auto &child : m_children) {
  109. if (p_caseSensitive ? child->getName() == targetName
  110. : child->getName().toLower() == targetName) {
  111. return child;
  112. }
  113. }
  114. return nullptr;
  115. }
  116. void Node::setParent(Node *p_parent)
  117. {
  118. m_parent = p_parent;
  119. }
  120. Node *Node::getParent() const
  121. {
  122. return m_parent;
  123. }
  124. Node::Flags Node::getFlags() const
  125. {
  126. return m_flags;
  127. }
  128. Node::Use Node::getUse() const
  129. {
  130. return m_use;
  131. }
  132. void Node::setUse(Node::Use p_use)
  133. {
  134. m_use = p_use;
  135. }
  136. ID Node::getId() const
  137. {
  138. return m_id;
  139. }
  140. void Node::updateId(ID p_id)
  141. {
  142. if (m_id == p_id) {
  143. return;
  144. }
  145. m_id = p_id;
  146. save();
  147. emit m_notebook->nodeUpdated(this);
  148. }
  149. ID Node::getSignature() const
  150. {
  151. return m_signature;
  152. }
  153. const QDateTime &Node::getCreatedTimeUtc() const
  154. {
  155. return m_createdTimeUtc;
  156. }
  157. const QDateTime &Node::getModifiedTimeUtc() const
  158. {
  159. return m_modifiedTimeUtc;
  160. }
  161. void Node::setModifiedTimeUtc()
  162. {
  163. m_modifiedTimeUtc = QDateTime::currentDateTimeUtc();
  164. }
  165. const QVector<QSharedPointer<Node>> &Node::getChildrenRef() const
  166. {
  167. return m_children;
  168. }
  169. QVector<QSharedPointer<Node>> Node::getChildren() const
  170. {
  171. return m_children;
  172. }
  173. int Node::getChildrenCount() const
  174. {
  175. return m_children.size();
  176. }
  177. void Node::addChild(const QSharedPointer<Node> &p_node)
  178. {
  179. insertChild(m_children.size(), p_node);
  180. }
  181. void Node::insertChild(int p_idx, const QSharedPointer<Node> &p_node)
  182. {
  183. Q_ASSERT(isContainer());
  184. p_node->setParent(this);
  185. m_children.insert(p_idx, p_node);
  186. }
  187. void Node::removeChild(const QSharedPointer<Node> &p_child)
  188. {
  189. if (m_children.removeOne(p_child)) {
  190. p_child->setParent(nullptr);
  191. }
  192. }
  193. Notebook *Node::getNotebook() const
  194. {
  195. return m_notebook;
  196. }
  197. bool Node::isAncestor(const Node *p_ancestor, const Node *p_child)
  198. {
  199. if (!p_ancestor || !p_child) {
  200. return false;
  201. }
  202. while (p_child) {
  203. p_child = p_child->getParent();
  204. if (p_child == p_ancestor) {
  205. return true;
  206. }
  207. }
  208. return false;
  209. }
  210. const QStringList &Node::getTags() const
  211. {
  212. return m_tags;
  213. }
  214. void Node::updateTags(const QStringList &p_tags)
  215. {
  216. if (p_tags == m_tags) {
  217. return;
  218. }
  219. m_tags = p_tags;
  220. save();
  221. emit m_notebook->nodeUpdated(this);
  222. }
  223. bool Node::isReadOnly() const
  224. {
  225. return m_flags & Flag::ReadOnly;
  226. }
  227. void Node::setReadOnly(bool p_readOnly)
  228. {
  229. if (p_readOnly) {
  230. m_flags |= Flag::ReadOnly;
  231. } else {
  232. m_flags &= ~Flag::ReadOnly;
  233. }
  234. }
  235. QString Node::fetchPath() const
  236. {
  237. if (!m_parent) {
  238. return QString();
  239. } else {
  240. return PathUtils::concatenateFilePath(m_parent->fetchPath(), m_name);
  241. }
  242. }
  243. bool Node::isContainer() const
  244. {
  245. return m_flags & Flag::Container;
  246. }
  247. bool Node::hasContent() const
  248. {
  249. return m_flags & Flag::Content;
  250. }
  251. QDir Node::toDir() const
  252. {
  253. if (isContainer()) {
  254. return QDir(fetchAbsolutePath());
  255. }
  256. Q_ASSERT(false);
  257. return QDir();
  258. }
  259. void Node::load()
  260. {
  261. if (isLoaded()) {
  262. return;
  263. }
  264. getConfigMgr()->loadNode(this);
  265. }
  266. void Node::save()
  267. {
  268. getConfigMgr()->saveNode(this);
  269. }
  270. INotebookConfigMgr *Node::getConfigMgr() const
  271. {
  272. return m_notebook->getConfigMgr().data();
  273. }
  274. const QString &Node::getAttachmentFolder() const
  275. {
  276. return m_attachmentFolder;
  277. }
  278. void Node::setAttachmentFolder(const QString &p_attachmentFolder)
  279. {
  280. m_attachmentFolder = p_attachmentFolder;
  281. }
  282. QString Node::fetchAttachmentFolderPath()
  283. {
  284. return getConfigMgr()->fetchNodeAttachmentFolderPath(this);
  285. }
  286. INotebookBackend *Node::getBackend() const
  287. {
  288. return m_notebook->getBackend().data();
  289. }
  290. bool Node::canRename(const QString &p_newName) const
  291. {
  292. if (p_newName == m_name) {
  293. return true;
  294. }
  295. if (p_newName.isEmpty()) {
  296. return false;
  297. }
  298. Q_ASSERT(m_parent);
  299. if (p_newName.toLower() == m_name.toLower()) {
  300. if (m_parent->containsChild(p_newName, true)) {
  301. return false;
  302. } else {
  303. return true;
  304. }
  305. }
  306. if (!m_parent->isLegalNameForNewChild(p_newName)) {
  307. return false;
  308. }
  309. return true;
  310. }
  311. void Node::sortChildren(const QVector<int> &p_beforeIdx, const QVector<int> &p_afterIdx)
  312. {
  313. Q_ASSERT(isContainer());
  314. Q_ASSERT(p_beforeIdx.size() == p_afterIdx.size());
  315. if (p_beforeIdx == p_afterIdx) {
  316. return;
  317. }
  318. auto ori = m_children;
  319. for (int i = 0; i < p_beforeIdx.size(); ++i) {
  320. if (p_beforeIdx[i] != p_afterIdx[i]) {
  321. m_children[p_beforeIdx[i]] = ori[p_afterIdx[i]];
  322. }
  323. }
  324. save();
  325. }
  326. QVector<QSharedPointer<ExternalNode>> Node::fetchExternalChildren() const
  327. {
  328. return getConfigMgr()->fetchExternalChildren(const_cast<Node *>(this));
  329. }
  330. bool Node::containsContainerChild(const QString &p_name) const
  331. {
  332. // TODO: we assume that m_children is sorted first the container children.
  333. for (auto &child : m_children) {
  334. if (!child->isContainer()) {
  335. break;
  336. }
  337. if (child->getName() == p_name) {
  338. return true;
  339. }
  340. }
  341. return false;
  342. }
  343. bool Node::containsContentChild(const QString &p_name) const
  344. {
  345. // TODO: we assume that m_children is sorted: first the container children then content children.
  346. for (int i = m_children.size() - 1; i >= 0; --i) {
  347. if (m_children[i]->isContainer()) {
  348. break;
  349. }
  350. if (m_children[i]->getName() == p_name) {
  351. return true;
  352. }
  353. }
  354. return false;
  355. }
  356. bool Node::exists() const
  357. {
  358. return m_flags & Flag::Exists;
  359. }
  360. void Node::setExists(bool p_exists)
  361. {
  362. if (p_exists) {
  363. m_flags |= Flag::Exists;
  364. } else {
  365. m_flags &= ~Flag::Exists;
  366. }
  367. }
  368. bool Node::checkExists()
  369. {
  370. bool before = exists();
  371. bool after = getConfigMgr()->checkNodeExists(this);
  372. if (before != after) {
  373. emit m_notebook->nodeUpdated(this);
  374. }
  375. return after;
  376. }
  377. QList<QSharedPointer<File>> Node::collectFiles()
  378. {
  379. QList<QSharedPointer<File>> files;
  380. load();
  381. if (hasContent()) {
  382. files.append(getContentFile());
  383. }
  384. if (isContainer()) {
  385. for (const auto &child : m_children) {
  386. files.append(child->collectFiles());
  387. }
  388. }
  389. return files;
  390. }
  391. ID Node::generateSignature()
  392. {
  393. return static_cast<ID>(QDateTime::currentDateTime().toSecsSinceEpoch() + (static_cast<qulonglong>(qrand()) << 32));
  394. }
  395. void Node::checkSignature()
  396. {
  397. if (m_signature == InvalidId) {
  398. m_signature = generateSignature();
  399. }
  400. }