app_settings.cpp 3.1 KB

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