AudioPersistent.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #include "AudioPersistent.h"
  2. #include "ark_api.h"
  3. #include <QDebug>
  4. #include <QFile>
  5. #include <QSettings>
  6. #include <QCoreApplication>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11. #ifdef gcc
  12. static const QString persistentPath("/tmp/Audio.ini");
  13. static const QString persistentPathBackup("/tmp/AudioBackup.ini");
  14. #else
  15. static const QString persistentPath("/data/Audio.ini");
  16. static const QString persistentPathBackup("/data/AudioBackup.ini");
  17. #endif
  18. static const QString Exists("Exists");
  19. static const QString Volume("Volume");
  20. static const QString Equalizer("Equalizer");
  21. static const QString Bass("Bass");
  22. static const QString Middle("Middle");
  23. static const QString Treble("Treble");
  24. static const QString Sound("Sound");
  25. static const QString Left("Left");
  26. static const QString Right("Right");
  27. class AudioSettings : public QSettings
  28. {
  29. Q_DISABLE_COPY(AudioSettings)
  30. #define g_Settings (AudioSettings::instance())
  31. public:
  32. inline static AudioSettings* instance() {
  33. static AudioSettings* settings(new AudioSettings(qApp));
  34. return settings;
  35. }
  36. void clear();
  37. void sync();
  38. void setValue(const QString &key, const QVariant &value);
  39. private:
  40. explicit AudioSettings(QObject* parent = NULL);
  41. ~AudioSettings();
  42. QSettings m_SettingsBackup;
  43. };
  44. void AudioPersistent::reset()
  45. {
  46. g_Settings->clear();
  47. g_Settings->sync();
  48. }
  49. int AudioPersistent::getVolume(const bool reload)
  50. {
  51. int defaultValue(30);
  52. if (QString(qgetenv("PROTOCOL_ID")).contains(QString("ark169"))
  53. || QString(qgetenv("PROTOCOL_ID")).contains(QString("dacheng169"))
  54. || QString(qgetenv("PROTOCOL_ID")).contains(QString("mingshang"))
  55. || QString(qgetenv("PROTOCOL_ID")).contains(QString("huixin"))
  56. || QString(qgetenv("PROTOCOL_ID")).size()==0) {
  57. defaultValue = 50;
  58. } else if (QString(qgetenv("PROTOCOL_ID")).contains(QString("yaoxi"))) {
  59. defaultValue = 15;
  60. }
  61. bool flag(false);
  62. if (reload) {
  63. g_Settings->sync();
  64. }
  65. int ret = g_Settings->value(Volume, defaultValue).toInt(&flag);
  66. if (!flag) {
  67. ret = defaultValue;
  68. }
  69. return ret;
  70. }
  71. int AudioPersistent::getEqualizerItem(const bool reload)
  72. {
  73. int defaultValue(EI_Standard);
  74. bool flag(false);
  75. if (reload) {
  76. g_Settings->sync();
  77. }
  78. int ret = g_Settings->value(Equalizer, defaultValue).toInt(&flag);
  79. if (!flag)
  80. {
  81. ret = defaultValue;
  82. }
  83. return ret;
  84. }
  85. int AudioPersistent::getBassEqualizer(const bool reload)
  86. {
  87. int defaultValue(7);
  88. bool flag(false);
  89. if (reload) {
  90. g_Settings->sync();
  91. }
  92. int ret = g_Settings->value(Bass, defaultValue).toInt(&flag);
  93. if (!flag)
  94. {
  95. ret = defaultValue;
  96. }
  97. return ret;
  98. }
  99. int AudioPersistent::getMiddleEqualizer(const bool reload)
  100. {
  101. int defaultValue(7);
  102. bool flag(false);
  103. if (reload) {
  104. g_Settings->sync();
  105. }
  106. int ret = g_Settings->value(Middle, defaultValue).toInt(&flag);
  107. if (!flag)
  108. {
  109. ret = defaultValue;
  110. }
  111. return ret;
  112. }
  113. int AudioPersistent::getTrebleEqualizer(const bool reload)
  114. {
  115. int defaultValue(7);
  116. bool flag(false);
  117. if (reload) {
  118. g_Settings->sync();
  119. }
  120. int ret = g_Settings->value(Treble, defaultValue).toInt(&flag);
  121. if (!flag)
  122. {
  123. ret = defaultValue;
  124. }
  125. return ret;
  126. }
  127. void AudioPersistent::setEqualizerItem(const int item)
  128. {
  129. g_Settings->setValue(Equalizer, item);
  130. g_Settings->sync();
  131. }
  132. void AudioPersistent::setBassEqualizer(const int value)
  133. {
  134. g_Settings->setValue(Bass, value);
  135. g_Settings->sync();
  136. }
  137. void AudioPersistent::setMiddleEqualizer(const int value)
  138. {
  139. g_Settings->setValue(Middle, value);
  140. g_Settings->sync();
  141. }
  142. void AudioPersistent::setTrebleEqualizer(const int value)
  143. {
  144. g_Settings->setValue(Treble, value);
  145. g_Settings->sync();
  146. }
  147. int AudioPersistent::getSoundItem(const bool reload)
  148. {
  149. int defaultValue(SI_Custom);
  150. bool flag(false);
  151. if (reload) {
  152. g_Settings->sync();
  153. }
  154. int ret = g_Settings->value(Sound, defaultValue).toInt(&flag);
  155. if (!flag)
  156. {
  157. ret = defaultValue;
  158. }
  159. return ret;
  160. }
  161. int AudioPersistent::getLeftSound(const bool reload)
  162. {
  163. int defaultValue(0);
  164. bool flag(false);
  165. if (reload) {
  166. g_Settings->sync();
  167. }
  168. int ret = g_Settings->value(Left, defaultValue).toInt(&flag);
  169. if (!flag)
  170. {
  171. ret = defaultValue;
  172. }
  173. return ret;
  174. }
  175. int AudioPersistent::getRightSound(const bool reload)
  176. {
  177. int defaultValue(0);
  178. bool flag(false);
  179. if (reload) {
  180. g_Settings->sync();
  181. }
  182. int ret = g_Settings->value(Right, defaultValue).toInt(&flag);
  183. if (!flag)
  184. {
  185. ret = defaultValue;
  186. }
  187. return ret;
  188. }
  189. void AudioPersistent::setSoundItem(const int item)
  190. {
  191. g_Settings->setValue(Sound, item);
  192. g_Settings->sync();
  193. }
  194. void AudioPersistent::setLeftSound(const int value)
  195. {
  196. g_Settings->setValue(Left, value);
  197. g_Settings->sync();
  198. }
  199. void AudioPersistent::setRightSound(const int value)
  200. {
  201. g_Settings->setValue(Right, value);
  202. g_Settings->sync();
  203. }
  204. void AudioPersistent::setVolume(int value)
  205. {
  206. g_Settings->setValue(Volume, value);
  207. g_Settings->sync();
  208. }
  209. void AudioSettings::clear()
  210. {
  211. QSettings::clear();
  212. m_SettingsBackup.clear();
  213. }
  214. void AudioSettings::sync()
  215. {
  216. QSettings::sync();
  217. m_SettingsBackup.sync();
  218. }
  219. void AudioSettings::setValue(const QString &key, const QVariant &value)
  220. {
  221. if (!contains(Exists)) {
  222. QSettings::setValue(Exists, QVariant(bool(true)));
  223. }
  224. QSettings::setValue(key, value);
  225. if (!m_SettingsBackup.contains(Exists)) {
  226. m_SettingsBackup.setValue(Exists, QVariant(bool(true)));
  227. }
  228. m_SettingsBackup.setValue(key, value);
  229. }
  230. AudioSettings::AudioSettings(QObject *parent)
  231. : QSettings(persistentPath, QSettings::IniFormat, parent)
  232. , m_SettingsBackup(persistentPathBackup, QSettings::IniFormat, parent)
  233. {
  234. bool ret(true);
  235. if (contains(Exists) && (!m_SettingsBackup.contains(Exists))) {
  236. QFile::remove(persistentPathBackup);
  237. ret = QFile::copy(persistentPath, persistentPathBackup);
  238. } else if ((!contains(Exists)) && m_SettingsBackup.contains(Exists)) {
  239. QFile::remove(persistentPath);
  240. ret = QFile::copy(persistentPathBackup, persistentPath);
  241. }
  242. if (!ret) {
  243. qDebug() << "copy failed!";
  244. }
  245. }
  246. AudioSettings::~AudioSettings()
  247. {
  248. }