vfile.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "vfile.h"
  2. #include <QDir>
  3. #include <QTextEdit>
  4. #include <QFileInfo>
  5. #include <QDebug>
  6. #include <QFile>
  7. #include <QTextStream>
  8. #include "utils/vutils.h"
  9. #include "vconfigmanager.h"
  10. extern VConfigManager *g_config;
  11. const QString VFile::c_backupFileHeadMagic = "vnote_backup_file_826537664";
  12. VFile::VFile(QObject *p_parent,
  13. const QString &p_name,
  14. FileType p_type,
  15. bool p_modifiable,
  16. QDateTime p_createdTimeUtc,
  17. QDateTime p_modifiedTimeUtc)
  18. : QObject(p_parent),
  19. m_name(p_name),
  20. m_opened(false),
  21. m_docType(VUtils::docTypeFromName(p_name)),
  22. m_type(p_type),
  23. m_modifiable(p_modifiable),
  24. m_createdTimeUtc(p_createdTimeUtc),
  25. m_modifiedTimeUtc(p_modifiedTimeUtc)
  26. {
  27. }
  28. VFile::~VFile()
  29. {
  30. }
  31. bool VFile::open()
  32. {
  33. if (m_opened) {
  34. return true;
  35. }
  36. Q_ASSERT(!m_name.isEmpty());
  37. Q_ASSERT(m_content.isEmpty());
  38. QString filePath = fetchPath();
  39. Q_ASSERT(QFileInfo::exists(filePath));
  40. m_content = VUtils::readFileFromDisk(filePath);
  41. m_lastModified = QFileInfo(filePath).lastModified();
  42. m_opened = true;
  43. return true;
  44. }
  45. void VFile::close()
  46. {
  47. if (!m_opened) {
  48. return;
  49. }
  50. m_content.clear();
  51. if (!m_backupName.isEmpty()) {
  52. VUtils::deleteFile(fetchBackupFilePath());
  53. m_backupName.clear();
  54. }
  55. m_opened = false;
  56. }
  57. bool VFile::save()
  58. {
  59. Q_ASSERT(m_opened);
  60. Q_ASSERT(m_modifiable);
  61. bool ret = VUtils::writeFileToDisk(fetchPath(), m_content);
  62. if (ret) {
  63. m_lastModified = QFileInfo(fetchPath()).lastModified();
  64. m_modifiedTimeUtc = QDateTime::currentDateTimeUtc();
  65. }
  66. return ret;
  67. }
  68. QUrl VFile::getBaseUrl() const
  69. {
  70. // Need to judge the path: Url, local file, resource file.
  71. QUrl baseUrl;
  72. QString basePath = fetchBasePath();
  73. QFileInfo pathInfo(basePath);
  74. if (pathInfo.exists()) {
  75. if (pathInfo.isNativePath()) {
  76. // Local file.
  77. baseUrl = QUrl::fromLocalFile(basePath + QDir::separator());
  78. } else {
  79. // Resource file.
  80. baseUrl = QUrl("qrc" + basePath + QDir::separator());
  81. }
  82. } else {
  83. // Url.
  84. baseUrl = QUrl(basePath + QDir::separator());
  85. }
  86. return baseUrl;
  87. }
  88. bool VFile::isInternalImageFolder(const QString &p_path) const
  89. {
  90. return VUtils::equalPath(VUtils::basePathFromPath(p_path),
  91. fetchBasePath())
  92. || VUtils::equalPath(p_path, fetchImageFolderPath());
  93. }
  94. bool VFile::isChangedOutside() const
  95. {
  96. QDateTime lm = QFileInfo(fetchPath()).lastModified();
  97. return lm != m_lastModified;
  98. }
  99. void VFile::reload()
  100. {
  101. Q_ASSERT(m_opened);
  102. QString filePath = fetchPath();
  103. Q_ASSERT(QFileInfo::exists(filePath));
  104. m_content = VUtils::readFileFromDisk(filePath);
  105. m_lastModified = QFileInfo(filePath).lastModified();
  106. }
  107. QString VFile::backupFileOfPreviousSession() const
  108. {
  109. Q_ASSERT(m_modifiable && m_backupName.isEmpty());
  110. QString basePath = QDir(fetchBasePath()).filePath(g_config->getBackupDirectory());
  111. QDir dir(basePath);
  112. QStringList files = getPotentialBackupFiles(basePath);
  113. foreach (const QString &file, files) {
  114. QString filePath = dir.filePath(file);
  115. if (isBackupFile(filePath)) {
  116. return filePath;
  117. }
  118. }
  119. return QString();
  120. }
  121. QString VFile::fetchBackupFilePath()
  122. {
  123. QString basePath = QDir(fetchBasePath()).filePath(g_config->getBackupDirectory());
  124. QDir dir(basePath);
  125. if (m_backupName.isEmpty()) {
  126. m_backupName = VUtils::getFileNameWithSequence(basePath,
  127. m_name + g_config->getBackupExtension(),
  128. true);
  129. m_lastBackupFilePath = dir.filePath(m_backupName);
  130. } else {
  131. QString filePath = dir.filePath(m_backupName);
  132. if (filePath != m_lastBackupFilePath) {
  133. // File has been moved.
  134. // Delete the original backup file if it still exists.
  135. VUtils::deleteFile(m_lastBackupFilePath);
  136. m_lastBackupFilePath = filePath;
  137. }
  138. }
  139. return m_lastBackupFilePath;
  140. }
  141. QStringList VFile::getPotentialBackupFiles(const QString &p_dir) const
  142. {
  143. QString nameFilter = QString("%1*%2").arg(m_name).arg(g_config->getBackupExtension());
  144. QStringList files = QDir(p_dir).entryList(QStringList(nameFilter),
  145. QDir::Files
  146. | QDir::Hidden
  147. | QDir::NoSymLinks
  148. | QDir::NoDotAndDotDot);
  149. return files;
  150. }
  151. bool VFile::isBackupFile(const QString &p_file) const
  152. {
  153. QFile file(p_file);
  154. if (!file.open(QFile::ReadOnly | QIODevice::Text)) {
  155. return false;
  156. }
  157. QTextStream st(&file);
  158. QString head = st.readLine();
  159. return head == fetchBackupFileHead();
  160. }
  161. QString VFile::fetchBackupFileHead() const
  162. {
  163. return c_backupFileHeadMagic + " " + fetchPath();
  164. }
  165. bool VFile::writeBackupFile(const QString &p_content)
  166. {
  167. return VUtils::writeFileToDisk(fetchBackupFilePath(),
  168. fetchBackupFileHead() + "\n" + p_content);
  169. }
  170. QString VFile::readBackupFile(const QString &p_file)
  171. {
  172. const QString content = VUtils::readFileFromDisk(p_file);
  173. int idx = content.indexOf("\n");
  174. return content.mid(idx + 1);
  175. }