localnotebookbackend.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "localnotebookbackend.h"
  2. #include <QDir>
  3. #include <QFile>
  4. #include <QTextStream>
  5. #include <QJsonObject>
  6. #include <QJsonDocument>
  7. #include <utils/pathutils.h>
  8. #include "exception.h"
  9. #include <utils/fileutils.h>
  10. using namespace vnotex;
  11. LocalNotebookBackend::LocalNotebookBackend(const QString &p_name,
  12. const QString &p_displayName,
  13. const QString &p_description,
  14. const QString &p_rootPath,
  15. QObject *p_parent)
  16. : INotebookBackend(p_rootPath, p_parent),
  17. m_info(p_name, p_displayName, p_description)
  18. {
  19. }
  20. QString LocalNotebookBackend::getName() const
  21. {
  22. return m_info.m_name;
  23. }
  24. QString LocalNotebookBackend::getDisplayName() const
  25. {
  26. return m_info.m_displayName;
  27. }
  28. QString LocalNotebookBackend::getDescription() const
  29. {
  30. return m_info.m_description;
  31. }
  32. bool LocalNotebookBackend::isEmptyDir(const QString &p_dirPath) const
  33. {
  34. return PathUtils::isEmptyDir(getFullPath(p_dirPath));
  35. }
  36. void LocalNotebookBackend::makePath(const QString &p_dirPath)
  37. {
  38. constrainPath(p_dirPath);
  39. QDir dir(getRootPath());
  40. if (!dir.mkpath(p_dirPath)) {
  41. Exception::throwOne(Exception::Type::FailToCreateDir,
  42. QString("fail to create directory: %1").arg(p_dirPath));
  43. }
  44. }
  45. void LocalNotebookBackend::writeFile(const QString &p_filePath, const QByteArray &p_data)
  46. {
  47. const auto filePath = getFullPath(p_filePath);
  48. FileUtils::writeFile(filePath, p_data);
  49. }
  50. void LocalNotebookBackend::writeFile(const QString &p_filePath, const QString &p_text)
  51. {
  52. const auto filePath = getFullPath(p_filePath);
  53. FileUtils::writeFile(filePath, p_text);
  54. }
  55. void LocalNotebookBackend::writeFile(const QString &p_filePath, const QJsonObject &p_jobj)
  56. {
  57. writeFile(p_filePath, QJsonDocument(p_jobj).toJson());
  58. }
  59. QString LocalNotebookBackend::readTextFile(const QString &p_filePath)
  60. {
  61. const auto filePath = getFullPath(p_filePath);
  62. return FileUtils::readTextFile(filePath);
  63. }
  64. QByteArray LocalNotebookBackend::readFile(const QString &p_filePath)
  65. {
  66. const auto filePath = getFullPath(p_filePath);
  67. return FileUtils::readFile(filePath);
  68. }
  69. bool LocalNotebookBackend::exists(const QString &p_path) const
  70. {
  71. return QFileInfo::exists(getFullPath(p_path));
  72. }
  73. bool LocalNotebookBackend::existsFile(const QString &p_path) const
  74. {
  75. QFileInfo fi(getFullPath(p_path));
  76. return fi.exists() && fi.isFile();
  77. }
  78. bool LocalNotebookBackend::existsDir(const QString &p_path) const
  79. {
  80. QFileInfo fi(getFullPath(p_path));
  81. return fi.exists() && fi.isDir();
  82. }
  83. bool LocalNotebookBackend::childExistsCaseInsensitive(const QString &p_dirPath, const QString &p_name) const
  84. {
  85. return FileUtils::childExistsCaseInsensitive(getFullPath(p_dirPath), p_name);
  86. }
  87. bool LocalNotebookBackend::isFile(const QString &p_path) const
  88. {
  89. QFileInfo fi(getFullPath(p_path));
  90. return fi.isFile();
  91. }
  92. void LocalNotebookBackend::renameFile(const QString &p_filePath, const QString &p_name)
  93. {
  94. Q_ASSERT(isFile(p_filePath));
  95. const auto filePath = getFullPath(p_filePath);
  96. FileUtils::renameFile(filePath, p_name);
  97. }
  98. void LocalNotebookBackend::renameDir(const QString &p_dirPath, const QString &p_name)
  99. {
  100. Q_ASSERT(!isFile(p_dirPath));
  101. const auto dirPath = getFullPath(p_dirPath);
  102. FileUtils::renameFile(dirPath, p_name);
  103. }
  104. void LocalNotebookBackend::copyFile(const QString &p_filePath, const QString &p_destPath, bool p_move)
  105. {
  106. auto filePath = p_filePath;
  107. if (QFileInfo(filePath).isRelative()) {
  108. filePath = getFullPath(filePath);
  109. }
  110. Q_ASSERT(QFileInfo(filePath).isFile());
  111. FileUtils::copyFile(filePath, getFullPath(p_destPath), p_move);
  112. }
  113. void LocalNotebookBackend::copyDir(const QString &p_dirPath, const QString &p_destPath, bool p_move)
  114. {
  115. auto dirPath = p_dirPath;
  116. if (QFileInfo(dirPath).isRelative()) {
  117. dirPath = getFullPath(dirPath);
  118. }
  119. Q_ASSERT(QFileInfo(dirPath).isDir());
  120. FileUtils::copyDir(dirPath, getFullPath(p_destPath), p_move);
  121. }
  122. void LocalNotebookBackend::removeFile(const QString &p_filePath)
  123. {
  124. Q_ASSERT(isFile(p_filePath));
  125. FileUtils::removeFile(getFullPath(p_filePath));
  126. }
  127. bool LocalNotebookBackend::removeDirIfEmpty(const QString &p_dirPath)
  128. {
  129. Q_ASSERT(!isFile(p_dirPath));
  130. return FileUtils::removeDirIfEmpty(getFullPath(p_dirPath));
  131. }
  132. void LocalNotebookBackend::removeDir(const QString &p_dirPath)
  133. {
  134. Q_ASSERT(!isFile(p_dirPath));
  135. return FileUtils::removeDir(getFullPath(p_dirPath));
  136. }
  137. QString LocalNotebookBackend::renameIfExistsCaseInsensitive(const QString &p_path) const
  138. {
  139. return FileUtils::renameIfExistsCaseInsensitive(getFullPath(p_path));
  140. }
  141. void LocalNotebookBackend::addFile(const QString &p_path)
  142. {
  143. Q_UNUSED(p_path);
  144. // Do nothing for now.
  145. }
  146. void LocalNotebookBackend::removeEmptyDir(const QString &p_dirPath)
  147. {
  148. FileUtils::removeEmptyDir(getFullPath(p_dirPath));
  149. }