settings.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. "CuteGit")
  14. {
  15. }
  16. void Settings::load(QStringList &repos)
  17. {
  18. QStringList groups = m_settings.childGroups();
  19. foreach (QString group, groups) {
  20. if(group == GeneralGroupKey) {
  21. continue;
  22. }
  23. m_settings.beginGroup(group);
  24. QString path = m_settings.value(RepoPathKey).toString();
  25. QFileInfo fileInfo(path);
  26. if(!fileInfo.exists(path) || !fileInfo.isDir()) {
  27. m_settings.remove("");
  28. } else {
  29. repos.append(path);
  30. }
  31. m_settings.endGroup();
  32. }
  33. }
  34. void Settings::add(GitRepository *repo)
  35. {
  36. if(repo == nullptr) {
  37. return;
  38. }
  39. m_settings.beginGroup(repo->id());
  40. m_settings.setValue(RepoNameKey, repo->name());
  41. m_settings.setValue(RepoPathKey, repo->path());
  42. m_settings.endGroup();
  43. }
  44. void Settings::saveLastRepo(GitRepository *repo)
  45. {
  46. m_settings.beginGroup(GeneralGroupKey);
  47. m_settings.setValue(LastRepoKey, repo->id());
  48. m_settings.endGroup();
  49. }
  50. QString Settings::loadLastRepo()
  51. {
  52. m_settings.beginGroup(GeneralGroupKey);
  53. QString result = m_settings.value(LastRepoKey).toString();
  54. m_settings.endGroup();
  55. return result;
  56. }