inotebookbackend.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #ifndef INOTEBOOKBACKEND_H
  2. #define INOTEBOOKBACKEND_H
  3. #include <QObject>
  4. #include <utils/pathutils.h>
  5. class QByteArray;
  6. class QJsonObject;
  7. namespace vnotex
  8. {
  9. // Abstract class for notebook backend, which is responsible for file access
  10. // and synchronization.
  11. class INotebookBackend : public QObject
  12. {
  13. Q_OBJECT
  14. public:
  15. INotebookBackend(const QString &p_rootPath, QObject *p_parent = nullptr)
  16. : QObject(p_parent),
  17. m_rootPath(PathUtils::absolutePath(p_rootPath))
  18. {
  19. }
  20. virtual ~INotebookBackend()
  21. {
  22. }
  23. virtual QString getName() const = 0;
  24. virtual QString getDisplayName() const = 0;
  25. virtual QString getDescription() const = 0;
  26. const QString &getRootPath() const
  27. {
  28. return m_rootPath;
  29. }
  30. void setRootPath(const QString &p_rootPath)
  31. {
  32. m_rootPath = p_rootPath;
  33. }
  34. // Whether @p_dirPath is an empty directory.
  35. virtual bool isEmptyDir(const QString &p_dirPath) const = 0;
  36. // Create the directory path @p_dirPath. Create all parent directories if necessary.
  37. virtual void makePath(const QString &p_dirPath) = 0;
  38. // Write @p_data to @p_filePath.
  39. virtual void writeFile(const QString &p_filePath, const QByteArray &p_data) = 0;
  40. // Write @p_text to @p_filePath.
  41. virtual void writeFile(const QString &p_filePath, const QString &p_text) = 0;
  42. // Write @p_jobj to @p_filePath.
  43. virtual void writeFile(const QString &p_filePath, const QJsonObject &p_jobj) = 0;
  44. // Read content from @p_filePath.
  45. virtual QString readTextFile(const QString &p_filePath) = 0;
  46. // Read file @p_filePath.
  47. virtual QByteArray readFile(const QString &p_filePath) = 0;
  48. QString getFullPath(const QString &p_path) const;
  49. virtual bool exists(const QString &p_path) const = 0;
  50. virtual bool childExistsCaseInsensitive(const QString &p_dirPath, const QString &p_name) const = 0;
  51. virtual bool isFile(const QString &p_path) const = 0;
  52. virtual void renameFile(const QString &p_filePath, const QString &p_name) = 0;
  53. virtual void renameDir(const QString &p_dirPath, const QString &p_name) = 0;
  54. // Copy @p_filePath to @p_destPath.
  55. // @p_filePath could be outside notebook.
  56. virtual void copyFile(const QString &p_filePath, const QString &p_destPath) = 0;
  57. // Delete @p_filePath from disk.
  58. virtual void removeFile(const QString &p_filePath) = 0;
  59. // Copy @p_dirPath to as @p_destPath.
  60. virtual void copyDir(const QString &p_dirPath, const QString &p_destPath) = 0;
  61. // Delete @p_dirPath from disk if it is empty.
  62. // Return false if it is not deleted due to non-empty.
  63. virtual bool removeDirIfEmpty(const QString &p_dirPath) = 0;
  64. virtual void removeDir(const QString &p_dirPath) = 0;
  65. virtual QString renameIfExistsCaseInsensitive(const QString &p_path) const = 0;
  66. // Add one file to backend.
  67. virtual void addFile(const QString &p_path) = 0;
  68. virtual void removeEmptyDir(const QString &p_dirPath) = 0;
  69. protected:
  70. // Constrain @p_path within root path of the notebook.
  71. void constrainPath(const QString &p_path) const;
  72. private:
  73. // Root path of the notebook.
  74. QString m_rootPath;
  75. };
  76. } // ns vnotex
  77. #endif // INOTEBOOKBACKEND_H