CmProjectPrefs.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "CmProjectPrefs.h"
  2. #include "CmFileSystem.h"
  3. #include "3rdParty/pugixml/pugixml.hpp"
  4. using namespace pugi;
  5. namespace CamelotEditor
  6. {
  7. const QString& ProjectPrefs::getProjectName() const
  8. {
  9. return mName;
  10. }
  11. void ProjectPrefs::setProjectName(const QString& name)
  12. {
  13. mName = name;
  14. }
  15. void ProjectPrefs::save(const QString& path, bool overwrite) const
  16. {
  17. String stdPath = path.toStdString();
  18. if(FileSystem::fileExists(stdPath))
  19. {
  20. if(!overwrite)
  21. {
  22. CM_EXCEPT(FileNotFoundException, "File already exists at this location.");
  23. }
  24. else
  25. FileSystem::remove(stdPath);
  26. }
  27. xml_document xmldoc;
  28. xml_node camelotEditor = xmldoc.append_child("CamelotProject");
  29. xml_attribute nameAttrib = camelotEditor.append_attribute("name");
  30. nameAttrib.set_value(mName.toStdString().c_str());
  31. xmldoc.save_file(stdPath.c_str());
  32. }
  33. void ProjectPrefs::load(const QString& path)
  34. {
  35. clear();
  36. String stdPath = path.toStdString();
  37. if(!FileSystem::fileExists(stdPath))
  38. CM_EXCEPT(FileNotFoundException, "Specified file: " + stdPath + " does not exist.");
  39. xml_document xmldoc;
  40. xml_parse_result parseResult = xmldoc.load_file(stdPath.c_str());
  41. if(parseResult.status != status_ok)
  42. CM_EXCEPT(InternalErrorException, "XML parsing failed: " + toString(parseResult.description()));
  43. xml_node camelotProject = xmldoc.child("CamelotProject");
  44. xml_attribute nameAttrib = camelotProject.attribute("name");
  45. mName = nameAttrib.value();
  46. }
  47. void ProjectPrefs::clear()
  48. {
  49. mName = "";
  50. }
  51. ProjectPrefs& gProjectPrefs()
  52. {
  53. return ProjectPrefs::instance();
  54. }
  55. }