vnote.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <QSettings>
  2. #include <QDebug>
  3. #include "vnote.h"
  4. #include "utils/vutils.h"
  5. const QString VNote::orgName = QString("tamlok");
  6. const QString VNote::appName = QString("VNote");
  7. const QString VNote::welcomePagePath = QString(":/resources/welcome.html");
  8. const QString VNote::preTemplatePath = QString(":/resources/pre_template.html");
  9. const QString VNote::postTemplatePath = QString(":/resources/post_template.html");
  10. const QString VNote::templatePath = QString(":/resources/template.html");
  11. const QString VNote::defaultCssUrl = QString("qrc:/resources/markdown.css");
  12. QString VNote::templateHtml;
  13. QString VNote::cssUrl = VNote::defaultCssUrl;
  14. VNote::VNote()
  15. : curNotebookIndex(0)
  16. {
  17. decorateTemplate();
  18. }
  19. void VNote::decorateTemplate()
  20. {
  21. templateHtml = VUtils::readFileFromDisk(templatePath);
  22. templateHtml.replace("CSS_PLACE_HOLDER", cssUrl);
  23. }
  24. void VNote::readGlobalConfig()
  25. {
  26. QSettings settings(QSettings::IniFormat, QSettings::UserScope,
  27. orgName, appName);
  28. // [global] section
  29. settings.beginGroup("global");
  30. curNotebookIndex = settings.value("current_notebook", 0).toInt();
  31. qDebug() << "read current_notebook=" << curNotebookIndex;
  32. settings.endGroup();
  33. readGlobalConfigNotebooks(settings);
  34. }
  35. void VNote::writeGlobalConfig()
  36. {
  37. QSettings settings(QSettings::IniFormat, QSettings::UserScope,
  38. orgName, appName);
  39. // [global] section
  40. settings.beginGroup("global");
  41. settings.setValue("current_notebook", curNotebookIndex);
  42. qDebug() << "write current_notebook=" << curNotebookIndex;
  43. settings.endGroup();
  44. writeGlobalConfigNotebooks(settings);
  45. }
  46. void VNote::readGlobalConfigNotebooks(QSettings &settings)
  47. {
  48. notebooks.clear();
  49. int size = settings.beginReadArray("notebooks");
  50. for (int i = 0; i < size; ++i) {
  51. settings.setArrayIndex(i);
  52. VNotebook notebook;
  53. QString name = settings.value("name").toString();
  54. QString path = settings.value("path").toString();
  55. notebook.setName(name);
  56. notebook.setPath(path);
  57. notebooks.append(notebook);
  58. }
  59. settings.endArray();
  60. qDebug() << "read" << notebooks.size()
  61. << "notebook items from [notebooks] section";
  62. }
  63. void VNote::writeGlobalConfigNotebooks(QSettings &settings)
  64. {
  65. settings.beginWriteArray("notebooks");
  66. for (int i = 0; i < notebooks.size(); ++i) {
  67. settings.setArrayIndex(i);
  68. settings.setValue("name", notebooks[i].getName());
  69. settings.setValue("path", notebooks[i].getPath());
  70. }
  71. settings.endArray();
  72. qDebug() << "write" << notebooks.size()
  73. << "notebook items in [notebooks] section";
  74. }
  75. const QVector<VNotebook>& VNote::getNotebooks()
  76. {
  77. return notebooks;
  78. }
  79. int VNote::getCurNotebookIndex() const
  80. {
  81. return curNotebookIndex;
  82. }
  83. void VNote::setCurNotebookIndex(int index)
  84. {
  85. curNotebookIndex = index;
  86. // Update settings
  87. QSettings settings(QSettings::IniFormat, QSettings::UserScope,
  88. orgName, appName);
  89. settings.setValue("global/current_notebook", curNotebookIndex);
  90. qDebug() << "write current_notebook=" << curNotebookIndex;
  91. }