settings.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "settings.h"
  2. #include "gitrepository.h"
  3. #include <QFileInfo>
  4. #include <QCryptographicHash>
  5. namespace {
  6. const QString GeneralGroupKey = "General";
  7. const QString RepoNameKey = "Name";
  8. const QString RepoPathKey = "Path";
  9. const QString LastRepoKey = "LastRepo";
  10. }
  11. Settings::Settings() : QObject()
  12. ,m_settings("org.semlanik",
  13. "NiceGit")
  14. {
  15. }
  16. void Settings::load(QStringList& repos)
  17. {
  18. QStringList groups = m_settings.childGroups();
  19. foreach (QString group, groups) {
  20. m_settings.beginGroup(group);
  21. QString path = m_settings.value(RepoPathKey).toString();
  22. QFileInfo fileInfo(path);
  23. if(!fileInfo.exists(path) || !fileInfo.isDir()) {
  24. m_settings.remove("");
  25. } else {
  26. repos.append(path);
  27. }
  28. m_settings.endGroup();
  29. }
  30. }
  31. void Settings::add(GitRepository* repo)
  32. {
  33. if(repo == nullptr) {
  34. return;
  35. }
  36. m_settings.beginGroup(repo->id());
  37. m_settings.setValue(RepoNameKey, repo->name());
  38. m_settings.setValue(RepoPathKey, repo->path());
  39. m_settings.endGroup();
  40. }
  41. void Settings::saveLastRepo(GitRepository* repo)
  42. {
  43. m_settings.beginGroup(GeneralGroupKey);
  44. m_settings.setValue(LastRepoKey, repo->id());
  45. m_settings.endGroup();
  46. }
  47. QString Settings::loadLastRepo()
  48. {
  49. m_settings.beginGroup(GeneralGroupKey);
  50. QString result = m_settings.value(LastRepoKey).toString();
  51. m_settings.endGroup();
  52. return result;
  53. }