app_settings.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "app_settings.h"
  2. const AppSettings::Section AppSettings::m_sections[] = {
  3. {SECTION_ID_GENERAL, "General"},
  4. {SECTION_ID_INFO, "Info"},
  5. {SECTION_ID_RECENTFILES, "RecentFiles"},
  6. {SECTION_ID_SAVEDPLAYLISTFILES, "SavedPlaylistFiles"},
  7. };
  8. AppSettings::AppSettings(const QString& file)
  9. {
  10. m_pSettings = std::make_unique<QSettings>(file, QSettings::IniFormat);
  11. print_settings();
  12. }
  13. void AppSettings::print_settings() const
  14. {
  15. if (m_pSettings)
  16. {
  17. qDebug() << "videoplayer configure file:" << toNativePath(m_pSettings->fileName());
  18. qDebug() << "organizationName:" << m_pSettings->organizationName();
  19. qDebug() << "applicationName:" << m_pSettings->applicationName();
  20. for (const auto& group : m_pSettings->childGroups())
  21. {
  22. m_pSettings->beginGroup(group);
  23. qDebug() << "group:" << group;
  24. for (const auto& key : m_pSettings->childKeys())
  25. {
  26. qDebug() << QString("key:%1, value:%2").arg(key).arg(m_pSettings->value(key).toString());
  27. }
  28. m_pSettings->endGroup();
  29. }
  30. }
  31. }
  32. void AppSettings::set_value(SectionID id, const QString& key, const QVariant& value)
  33. {
  34. if (id > SECTION_ID_NONE && id < SECTION_ID_MAX)
  35. set_value(QString(m_sections[id].str), key, value);
  36. }
  37. QVariant AppSettings::get_value(SectionID id, const QString& key) const
  38. {
  39. if (id > SECTION_ID_NONE && id < SECTION_ID_MAX)
  40. {
  41. return get_value(QString(m_sections[id].str), key);
  42. }
  43. return QVariant(QVariant::Invalid);
  44. }
  45. inline QString AppSettings::group_key(const QString& group, const QString& key)
  46. {
  47. return group + "/" + key;
  48. }
  49. void AppSettings::set_value(const QString& group, const QString& key, const QVariant& value)
  50. {
  51. m_pSettings->setValue(group_key(group, key), value);
  52. }
  53. QVariant AppSettings::get_value(const QString& group, const QString& key) const
  54. {
  55. return m_pSettings->value(group_key(group, key));
  56. }
  57. void AppSettings::set_general(const QString& key, const QVariant& value)
  58. {
  59. set_value(SECTION_ID_GENERAL, key, value);
  60. }
  61. QVariant AppSettings::get_general(const QString& key) const
  62. {
  63. return get_value(SECTION_ID_GENERAL, key);
  64. }
  65. void AppSettings::set_info(const QString& key, const QVariant& value)
  66. {
  67. set_value(SECTION_ID_INFO, key, value);
  68. }
  69. QVariant AppSettings::get_info(const QString& key) const
  70. {
  71. return get_value(SECTION_ID_INFO, key);
  72. }
  73. void AppSettings::set_recentfiles(const QVariant& value, const QString& key)
  74. {
  75. set_value(SECTION_ID_RECENTFILES, key, value);
  76. }
  77. QVariant AppSettings::get_recentfiles(const QString& key) const
  78. {
  79. return get_value(SECTION_ID_RECENTFILES, key);
  80. }
  81. QVariant AppSettings::get_savedplaylists(const QString& key) const
  82. {
  83. return get_value(SECTION_ID_SAVEDPLAYLISTFILES, key);
  84. }
  85. void AppSettings::set_savedplaylists(const QVariant& value, const QString& key)
  86. {
  87. set_value(SECTION_ID_SAVEDPLAYLISTFILES, key, value);
  88. }