vnotefile.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include "vnotefile.h"
  2. #include <QDir>
  3. #include <QDebug>
  4. #include <QTextEdit>
  5. #include <QFileInfo>
  6. #include <QDebug>
  7. #include "utils/vutils.h"
  8. #include "vdirectory.h"
  9. VNoteFile::VNoteFile(VDirectory *p_directory,
  10. const QString &p_name,
  11. FileType p_type,
  12. bool p_modifiable,
  13. QDateTime p_createdTimeUtc,
  14. QDateTime p_modifiedTimeUtc)
  15. : VFile(p_directory, p_name, p_type, p_modifiable, p_createdTimeUtc, p_modifiedTimeUtc)
  16. {
  17. }
  18. QString VNoteFile::fetchPath() const
  19. {
  20. return QDir(getDirectory()->fetchPath()).filePath(m_name);
  21. }
  22. QString VNoteFile::fetchBasePath() const
  23. {
  24. return getDirectory()->fetchPath();
  25. }
  26. QString VNoteFile::fetchImageFolderPath() const
  27. {
  28. return QDir(fetchBasePath()).filePath(getNotebook()->getImageFolder());
  29. }
  30. bool VNoteFile::useRelativeImageFolder() const
  31. {
  32. // Always use relative image folder.
  33. return true;
  34. }
  35. QString VNoteFile::getImageFolderInLink() const
  36. {
  37. return getNotebook()->getImageFolder();
  38. }
  39. void VNoteFile::setName(const QString &p_name)
  40. {
  41. Q_ASSERT(m_name.isEmpty()
  42. || (m_docType == VUtils::docTypeFromName(p_name)));
  43. m_name = p_name;
  44. }
  45. bool VNoteFile::rename(const QString &p_name)
  46. {
  47. if (m_name == p_name) {
  48. return true;
  49. }
  50. QString oldName = m_name;
  51. VDirectory *dir = getDirectory();
  52. Q_ASSERT(dir);
  53. // Rename it in disk.
  54. QDir diskDir(dir->fetchPath());
  55. if (!diskDir.rename(m_name, p_name)) {
  56. qWarning() << "fail to rename file" << m_name << "to" << p_name << "in disk";
  57. return false;
  58. }
  59. m_name = p_name;
  60. // Update parent directory's config file.
  61. if (!dir->writeToConfig()) {
  62. m_name = oldName;
  63. diskDir.rename(p_name, m_name);
  64. return false;
  65. }
  66. // Can't not change doc type.
  67. Q_ASSERT(m_docType == VUtils::docTypeFromName(m_name));
  68. qDebug() << "file renamed from" << oldName << "to" << m_name;
  69. return true;
  70. }
  71. VDirectory *VNoteFile::getDirectory()
  72. {
  73. Q_ASSERT(parent());
  74. return (VDirectory *)parent();
  75. }
  76. const VDirectory *VNoteFile::getDirectory() const
  77. {
  78. Q_ASSERT(parent());
  79. return (const VDirectory *)parent();
  80. }
  81. VNotebook *VNoteFile::getNotebook()
  82. {
  83. return getDirectory()->getNotebook();
  84. }
  85. const VNotebook *VNoteFile::getNotebook() const
  86. {
  87. return getDirectory()->getNotebook();
  88. }
  89. QString VNoteFile::getNotebookName() const
  90. {
  91. return getDirectory()->getNotebookName();
  92. }
  93. QString VNoteFile::fetchRelativePath() const
  94. {
  95. return QDir(getDirectory()->fetchRelativePath()).filePath(m_name);
  96. }
  97. VNoteFile *VNoteFile::fromJson(VDirectory *p_directory,
  98. const QJsonObject &p_json,
  99. FileType p_type,
  100. bool p_modifiable)
  101. {
  102. return new VNoteFile(p_directory,
  103. p_json[DirConfig::c_name].toString(),
  104. p_type,
  105. p_modifiable,
  106. QDateTime::fromString(p_json[DirConfig::c_createdTime].toString(),
  107. Qt::ISODate),
  108. QDateTime::fromString(p_json[DirConfig::c_modifiedTime].toString(),
  109. Qt::ISODate));
  110. }
  111. QJsonObject VNoteFile::toConfigJson() const
  112. {
  113. QJsonObject item;
  114. item[DirConfig::c_name] = m_name;
  115. item[DirConfig::c_createdTime] = m_createdTimeUtc.toString(Qt::ISODate);
  116. item[DirConfig::c_modifiedTime] = m_modifiedTimeUtc.toString(Qt::ISODate);
  117. return item;
  118. }
  119. bool VNoteFile::deleteFile()
  120. {
  121. Q_ASSERT(parent());
  122. bool ret = false;
  123. // Delete local images if it is Markdown.
  124. if (m_docType == DocType::Markdown) {
  125. deleteInternalImages();
  126. }
  127. // TODO: Delete attachments.
  128. // Delete the file.
  129. QString filePath = fetchPath();
  130. if (VUtils::deleteFile(getNotebook(), filePath, false)) {
  131. ret = true;
  132. qDebug() << "deleted" << m_name << filePath;
  133. } else {
  134. qWarning() << "fail to delete" << m_name << filePath;
  135. }
  136. return ret;
  137. }
  138. void VNoteFile::deleteInternalImages()
  139. {
  140. Q_ASSERT(parent() && m_docType == DocType::Markdown);
  141. QVector<ImageLink> images = VUtils::fetchImagesFromMarkdownFile(this,
  142. ImageLink::LocalRelativeInternal);
  143. int deleted = 0;
  144. for (int i = 0; i < images.size(); ++i) {
  145. if (VUtils::deleteFile(getNotebook(), images[i].m_path, false)) {
  146. ++deleted;
  147. }
  148. }
  149. qDebug() << "delete" << deleted << "images for" << m_name << fetchPath();
  150. }