CmEditorPrefs.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "CmEditorPrefs.h"
  2. #include "CmFileSystem.h"
  3. #include <QtCore/QDir>
  4. #include "3rdParty/pugixml/pugixml.hpp"
  5. using namespace pugi;
  6. namespace CamelotEditor
  7. {
  8. UINT32 EditorPrefs::getNumRecentlyUsedProjects() const
  9. {
  10. return (UINT32)mRecentlyUsedProjects.size();
  11. }
  12. const QString& EditorPrefs::getRecentlyUsedProjectPath(UINT32 idx) const
  13. {
  14. return mRecentlyUsedProjects.at(idx);
  15. }
  16. void EditorPrefs::addRecentlyUsedProjectPath(const QString& path)
  17. {
  18. QString cleanPath = QDir::cleanPath(QDir::toNativeSeparators(path));
  19. for(auto iter = mRecentlyUsedProjects.begin(); iter != mRecentlyUsedProjects.end(); ++iter)
  20. {
  21. if(*iter == cleanPath)
  22. return;
  23. }
  24. mRecentlyUsedProjects.push_back(cleanPath);
  25. }
  26. void EditorPrefs::removeRecentlyUsedProjectPath(UINT32 idx)
  27. {
  28. mRecentlyUsedProjects.erase(mRecentlyUsedProjects.begin() + idx);
  29. }
  30. void EditorPrefs::setLastUsedProjectDirectory(const QString& value)
  31. {
  32. mLastUsedProjectDirectory = QDir::cleanPath(QDir::toNativeSeparators(value));
  33. }
  34. const QString& EditorPrefs::getLastUsedProjectDirectory() const
  35. {
  36. return mLastUsedProjectDirectory;
  37. }
  38. void EditorPrefs::save(const QString& path, bool overwrite) const
  39. {
  40. String stdPath = path.toStdString();
  41. if(FileSystem::fileExists(stdPath))
  42. {
  43. if(!overwrite)
  44. {
  45. CM_EXCEPT(FileNotFoundException, "File already exists at this location.");
  46. }
  47. else
  48. FileSystem::remove(stdPath);
  49. }
  50. xml_document xmldoc;
  51. xml_node camelotEditor = xmldoc.append_child("CamelotEditor");
  52. xml_node recentlyUsedProjects = camelotEditor.append_child("RecentlyUsedProjects");
  53. for(auto iter = mRecentlyUsedProjects.begin(); iter != mRecentlyUsedProjects.end(); ++iter)
  54. {
  55. xml_node recentlyUsedProject = recentlyUsedProjects.append_child("RecentlyUsedProject");
  56. xml_attribute pathAttrib = recentlyUsedProject.append_attribute("path");
  57. pathAttrib.set_value(iter->toStdString().c_str());
  58. }
  59. xml_node lastUsedProjectDirectory = camelotEditor.append_child("LastUsedProjectDir");
  60. xml_attribute pathAttrib = lastUsedProjectDirectory.append_attribute("path");
  61. pathAttrib.set_value(mLastUsedProjectDirectory.toStdString().c_str());
  62. xmldoc.save_file(stdPath.c_str());
  63. }
  64. void EditorPrefs::load(const QString& path)
  65. {
  66. clear();
  67. String stdPath = path.toStdString();
  68. if(!FileSystem::fileExists(stdPath))
  69. CM_EXCEPT(FileNotFoundException, "Specified file: " + stdPath + " does not exist.");
  70. xml_document xmldoc;
  71. xml_parse_result parseResult = xmldoc.load_file(stdPath.c_str());
  72. if(parseResult.status != status_ok)
  73. CM_EXCEPT(InternalErrorException, "XML parsing failed: " + toString(parseResult.description()));
  74. xml_node camelotEditor = xmldoc.child("CamelotEditor");
  75. xml_node recentlyUsedProjects = camelotEditor.child("RecentlyUsedProjects");
  76. for(auto iter = recentlyUsedProjects.begin(); iter != recentlyUsedProjects.end(); ++iter)
  77. {
  78. xml_attribute pathAttrib = iter->attribute("path");
  79. mRecentlyUsedProjects.push_back(pathAttrib.value());
  80. }
  81. xml_node lastUsedProjectDirectory = camelotEditor.child("LastUsedProjectDir");
  82. xml_attribute pathAttrib = lastUsedProjectDirectory.attribute("path");
  83. mLastUsedProjectDirectory = pathAttrib.value();
  84. }
  85. void EditorPrefs::clear()
  86. {
  87. mRecentlyUsedProjects.clear();
  88. mLastUsedProjectDirectory = "";
  89. }
  90. EditorPrefs& gEditorPrefs()
  91. {
  92. return EditorPrefs::instance();
  93. }
  94. }