localnotebookbackend.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #include "localnotebookbackend.h"
  2. #include <QDir>
  3. #include <QFile>
  4. #include <QTextStream>
  5. #include <QJsonObject>
  6. #include <QJsonDocument>
  7. #include <vtextedit/textutils.h>
  8. #include <utils/pathutils.h>
  9. #include <utils/fileutils.h>
  10. #include "exception.h"
  11. #include "coreconfig.h"
  12. #include "configmgr.h"
  13. using namespace vnotex;
  14. LocalNotebookBackend::LocalNotebookBackend(const QString &p_name,
  15. const QString &p_displayName,
  16. const QString &p_description,
  17. const QString &p_rootPath,
  18. QObject *p_parent)
  19. : INotebookBackend(p_rootPath, p_parent),
  20. m_info(p_name, p_displayName, p_description)
  21. {
  22. }
  23. QString LocalNotebookBackend::getName() const
  24. {
  25. return m_info.m_name;
  26. }
  27. QString LocalNotebookBackend::getDisplayName() const
  28. {
  29. return m_info.m_displayName;
  30. }
  31. QString LocalNotebookBackend::getDescription() const
  32. {
  33. return m_info.m_description;
  34. }
  35. bool LocalNotebookBackend::isEmptyDir(const QString &p_dirPath) const
  36. {
  37. return PathUtils::isEmptyDir(getFullPath(p_dirPath));
  38. }
  39. void LocalNotebookBackend::makePath(const QString &p_dirPath)
  40. {
  41. constrainPath(p_dirPath);
  42. QDir dir(getRootPath());
  43. if (!dir.mkpath(p_dirPath)) {
  44. Exception::throwOne(Exception::Type::FailToCreateDir,
  45. QString("fail to create directory: %1").arg(p_dirPath));
  46. }
  47. }
  48. void LocalNotebookBackend::writeFile(const QString &p_filePath, const QByteArray &p_data)
  49. {
  50. const auto filePath = getFullPath(p_filePath);
  51. FileUtils::writeFile(filePath, p_data);
  52. }
  53. void LocalNotebookBackend::writeFile(const QString &p_filePath, const QString &p_text)
  54. {
  55. const auto filePath = getFullPath(p_filePath);
  56. FileUtils::writeFile(filePath, p_text);
  57. }
  58. void LocalNotebookBackend::writeFile(const QString &p_filePath, const QJsonObject &p_jobj)
  59. {
  60. const auto &coreConfig = ConfigMgr::getInst().getCoreConfig();
  61. auto data = QJsonDocument(p_jobj).toJson();
  62. vte::LineEnding before = vte::LineEnding::LF;
  63. QString text;
  64. switch (coreConfig.getLineEndingPolicy()) {
  65. case LineEndingPolicy::Platform:
  66. #if defined(Q_OS_WIN)
  67. text = QString::fromUtf8(data);
  68. vte::TextUtils::transformLineEnding(text, before, vte::LineEnding::CRLF);
  69. #endif
  70. break;
  71. case LineEndingPolicy::File:
  72. // Not supported.
  73. Q_FALLTHROUGH();
  74. case LineEndingPolicy::LF:
  75. break;
  76. case LineEndingPolicy::CRLF:
  77. text = QString::fromUtf8(data);
  78. vte::TextUtils::transformLineEnding(text, before, vte::LineEnding::CRLF);
  79. break;
  80. case LineEndingPolicy::CR:
  81. text = QString::fromUtf8(data);
  82. vte::TextUtils::transformLineEnding(text, before, vte::LineEnding::CR);
  83. break;
  84. }
  85. if (!text.isEmpty()) {
  86. writeFile(p_filePath, text);
  87. } else {
  88. writeFile(p_filePath, data);
  89. }
  90. }
  91. QString LocalNotebookBackend::readTextFile(const QString &p_filePath)
  92. {
  93. const auto filePath = getFullPath(p_filePath);
  94. return FileUtils::readTextFile(filePath);
  95. }
  96. QByteArray LocalNotebookBackend::readFile(const QString &p_filePath)
  97. {
  98. const auto filePath = getFullPath(p_filePath);
  99. return FileUtils::readFile(filePath);
  100. }
  101. bool LocalNotebookBackend::exists(const QString &p_path) const
  102. {
  103. return QFileInfo::exists(getFullPath(p_path));
  104. }
  105. bool LocalNotebookBackend::existsFile(const QString &p_path) const
  106. {
  107. QFileInfo fi(getFullPath(p_path));
  108. return fi.exists() && fi.isFile();
  109. }
  110. bool LocalNotebookBackend::existsDir(const QString &p_path) const
  111. {
  112. QFileInfo fi(getFullPath(p_path));
  113. return fi.exists() && fi.isDir();
  114. }
  115. bool LocalNotebookBackend::childExistsCaseInsensitive(const QString &p_dirPath, const QString &p_name) const
  116. {
  117. return FileUtils::childExistsCaseInsensitive(getFullPath(p_dirPath), p_name);
  118. }
  119. bool LocalNotebookBackend::isFile(const QString &p_path) const
  120. {
  121. QFileInfo fi(getFullPath(p_path));
  122. return fi.isFile();
  123. }
  124. void LocalNotebookBackend::renameFile(const QString &p_filePath, const QString &p_name)
  125. {
  126. Q_ASSERT(isFile(p_filePath));
  127. const auto filePath = getFullPath(p_filePath);
  128. FileUtils::renameFile(filePath, p_name);
  129. }
  130. void LocalNotebookBackend::renameDir(const QString &p_dirPath, const QString &p_name)
  131. {
  132. Q_ASSERT(!isFile(p_dirPath));
  133. const auto dirPath = getFullPath(p_dirPath);
  134. FileUtils::renameFile(dirPath, p_name);
  135. }
  136. void LocalNotebookBackend::copyFile(const QString &p_filePath, const QString &p_destPath, bool p_move)
  137. {
  138. auto filePath = p_filePath;
  139. if (QFileInfo(filePath).isRelative()) {
  140. filePath = getFullPath(filePath);
  141. }
  142. Q_ASSERT(QFileInfo(filePath).isFile());
  143. FileUtils::copyFile(filePath, getFullPath(p_destPath), p_move);
  144. }
  145. void LocalNotebookBackend::copyDir(const QString &p_dirPath, const QString &p_destPath, bool p_move)
  146. {
  147. auto dirPath = p_dirPath;
  148. if (QFileInfo(dirPath).isRelative()) {
  149. dirPath = getFullPath(dirPath);
  150. }
  151. Q_ASSERT(QFileInfo(dirPath).isDir());
  152. FileUtils::copyDir(dirPath, getFullPath(p_destPath), p_move);
  153. }
  154. void LocalNotebookBackend::removeFile(const QString &p_filePath)
  155. {
  156. Q_ASSERT(isFile(p_filePath));
  157. FileUtils::removeFile(getFullPath(p_filePath));
  158. }
  159. bool LocalNotebookBackend::removeDirIfEmpty(const QString &p_dirPath)
  160. {
  161. Q_ASSERT(!isFile(p_dirPath));
  162. return FileUtils::removeDirIfEmpty(getFullPath(p_dirPath));
  163. }
  164. void LocalNotebookBackend::removeDir(const QString &p_dirPath)
  165. {
  166. Q_ASSERT(!isFile(p_dirPath));
  167. return FileUtils::removeDir(getFullPath(p_dirPath));
  168. }
  169. QString LocalNotebookBackend::renameIfExistsCaseInsensitive(const QString &p_path) const
  170. {
  171. return FileUtils::renameIfExistsCaseInsensitive(getFullPath(p_path));
  172. }
  173. void LocalNotebookBackend::addFile(const QString &p_path)
  174. {
  175. Q_UNUSED(p_path);
  176. // Do nothing for now.
  177. }
  178. void LocalNotebookBackend::removeEmptyDir(const QString &p_dirPath)
  179. {
  180. FileUtils::removeEmptyDir(getFullPath(p_dirPath));
  181. }