vnotebook.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #ifndef VNOTEBOOK_H
  2. #define VNOTEBOOK_H
  3. #include <QObject>
  4. #include <QString>
  5. #include <QDateTime>
  6. #include <QStringList>
  7. class VDirectory;
  8. class VFile;
  9. class VNoteFile;
  10. class VNotebook : public QObject
  11. {
  12. Q_OBJECT
  13. public:
  14. VNotebook(const QString &name, const QString &path, QObject *parent = 0);
  15. ~VNotebook();
  16. // Open the root directory to load contents
  17. bool open();
  18. // Whether this notebook is opened.
  19. bool isOpened() const;
  20. // Close all the directory and files of this notebook.
  21. // Please make sure all files belonging to this notebook have been closed in the tab.
  22. void close();
  23. bool containsFile(const VFile *p_file) const;
  24. // Try to load the file @p_path.
  25. // Returns the corresponding VNoteFile struct if @p_path is a note inside this notebook.
  26. // Otherwise, returns NULL.
  27. // If notebook is not opened currently, it will open itself and close itself
  28. // if @p_path is not inside this notebook.
  29. VNoteFile *tryLoadFile(const QString &p_path);
  30. // Try to load the directory @p_path.
  31. // Returns the corresponding VDirectory struct if @p_path is a folder inside this notebook.
  32. // Otherwise, returns NULL.
  33. // If notebook is not opened currently, it will open itself and close itself
  34. // if @p_path is not inside this notebook.
  35. VDirectory *tryLoadDirectory(const QString &p_path);
  36. const QString &getName() const;
  37. const QString &getPath() const;
  38. const QString &getPathInConfig() const;
  39. void updatePath(const QString &p_path);
  40. VDirectory *getRootDir() const;
  41. void rename(const QString &p_name);
  42. const QStringList &getTags() const;
  43. bool addTag(const QString &p_tag);
  44. // Walk through @p_dir recursively to add all tags to notebook.
  45. bool addTags(VDirectory *p_dir);
  46. void removeTag(const QString &p_tag);
  47. bool hasTag(const QString &p_tag) const;
  48. static VNotebook *createNotebook(const QString &p_name,
  49. const QString &p_path,
  50. bool p_import,
  51. const QString &p_imageFolder,
  52. const QString &p_attachmentFolder,
  53. QObject *p_parent = 0);
  54. static bool deleteNotebook(VNotebook *p_notebook, bool p_deleteFiles);
  55. // Get the image folder for this notebook to use (not exactly the same as
  56. // m_imageFolder if it is empty).
  57. const QString &getImageFolder() const;
  58. // Return m_imageFolder.
  59. const QString &getImageFolderConfig() const;
  60. // Different from image folder. We could not change the attachment folder
  61. // of a notebook once it has been created.
  62. // Get the attachment folder for this notebook to use.
  63. const QString &getAttachmentFolder() const;
  64. // Return m_recycleBinFolder.
  65. const QString &getRecycleBinFolder() const;
  66. // Get the recycle folder path for this notebook to use.
  67. QString getRecycleBinFolderPath() const;
  68. void setImageFolder(const QString &p_imageFolder);
  69. void setAttachmentFolder(const QString &p_attachmentFolder);
  70. // Read configurations (only notebook part) directly from root directory config file.
  71. bool readConfigNotebook();
  72. // Write configurations only related to notebook to root directory config file.
  73. bool writeConfigNotebook() const;
  74. // Return only the info of notebook part in json.
  75. QJsonObject toConfigJsonNotebook() const;
  76. // Need to check if this notebook has been opened.
  77. QDateTime getCreatedTimeUtc();
  78. bool isValid() const;
  79. QList<QString> collectFiles();
  80. // Create configuration files recursively to build a notebook based on
  81. // a external directory.
  82. static bool buildNotebook(const QString &p_name,
  83. const QString &p_path,
  84. const QString &p_imgFolder,
  85. const QString &p_attachmentFolder,
  86. QString *p_errMsg = NULL);
  87. private:
  88. // Serialize current instance to json.
  89. QJsonObject toConfigJson() const;
  90. // Write current instance to config file.
  91. bool writeToConfig() const;
  92. void setPath(const QString &p_path);
  93. QString m_name;
  94. QString m_path;
  95. // Path in vnote.ini.
  96. // May be relative path to VNote's executable.
  97. QString m_pathInConfig;
  98. // Folder name to store images.
  99. // If not empty, VNote will store images in this folder within the same directory of the note.
  100. // Otherwise, VNote will use the global configured folder.
  101. QString m_imageFolder;
  102. // Folder name to store attachments.
  103. // Should not be empty and changed once a notebook is created.
  104. QString m_attachmentFolder;
  105. // Folder name to store deleted files.
  106. // Could be relative or absolute.
  107. QString m_recycleBinFolder;
  108. // List of all the tags of notes.
  109. // Used for index and auto-completion.
  110. QStringList m_tags;
  111. // Parent is NULL for root directory
  112. VDirectory *m_rootDir;
  113. // Whether this notebook is valid.
  114. // Will set to true after readConfigNotebook().
  115. bool m_valid;
  116. };
  117. inline VDirectory *VNotebook::getRootDir() const
  118. {
  119. return m_rootDir;
  120. }
  121. inline const QString &VNotebook::getRecycleBinFolder() const
  122. {
  123. return m_recycleBinFolder;
  124. }
  125. inline bool VNotebook::isValid() const
  126. {
  127. return m_valid;
  128. }
  129. inline const QString &VNotebook::getPath() const
  130. {
  131. return m_path;
  132. }
  133. inline const QString &VNotebook::getPathInConfig() const
  134. {
  135. return m_pathInConfig;
  136. }
  137. inline const QString &VNotebook::getName() const
  138. {
  139. return m_name;
  140. }
  141. inline bool VNotebook::hasTag(const QString &p_tag) const
  142. {
  143. return m_tags.contains(p_tag);
  144. }
  145. inline const QStringList &VNotebook::getTags() const
  146. {
  147. return m_tags;
  148. }
  149. #endif // VNOTEBOOK_H