MultimediaPersistent.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #include "MultimediaPersistent.h"
  2. #include <QDebug>
  3. #include <QFile>
  4. #include <QSettings>
  5. #include <QCoreApplication>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. static const QString persistentPath("/data/Multimedia.ini");
  11. static const QString persistentPathBackup("/data/MultimediaBackup.ini");
  12. static const QString Exists("Exists");
  13. static const QString SDMultimedia("SDMultimedia");
  14. static const QString USBMultimedia("USBMultimedia");
  15. static const QString SDMusicMillesmial("SDMusicMillesmial");
  16. static const QString SDMusicPathInfo("SDMusicPathInfo");
  17. static const QString USBMusicMillesmial("USBMusicMillesmial");
  18. static const QString USBMusicPathInfo("USBMusicPathInfo");
  19. static const QString SDImagePathInfo("SDImagePathInfo");
  20. static const QString USBImagePathInfo("USBImagePathInfo");
  21. static const QString SDVideoMillesmial("SDVideoMillesmial");
  22. static const QString SDVideoPathInfo("SDVideoPathInfo");
  23. static const QString USBVideoMillesmial("USBVideoMillesmial");
  24. static const QString USBVideoPathInfo("USBVideoPathInfo");
  25. class MultimediaSettings : public QSettings
  26. {
  27. Q_DISABLE_COPY(MultimediaSettings)
  28. #ifdef g_Settings
  29. #undef g_Settings
  30. #endif
  31. #define g_Settings (MultimediaSettings::instance())
  32. public:
  33. inline static MultimediaSettings* instance() {
  34. static MultimediaSettings* settings(new MultimediaSettings(qApp));
  35. return settings;
  36. }
  37. void clear();
  38. void sync();
  39. void setValue(const QString &key, const QVariant &value);
  40. private:
  41. explicit MultimediaSettings(QObject* parent = NULL);
  42. ~MultimediaSettings();
  43. QSettings m_SettingsBackup;
  44. };
  45. void MultimediaSettings::clear()
  46. {
  47. QSettings::clear();
  48. m_SettingsBackup.clear();
  49. }
  50. void MultimediaSettings::sync()
  51. {
  52. QSettings::sync();
  53. m_SettingsBackup.sync();
  54. }
  55. void MultimediaSettings::setValue(const QString &key, const QVariant &value)
  56. {
  57. if (!contains(Exists)) {
  58. QSettings::setValue(Exists, QVariant(bool(true)));
  59. }
  60. QSettings::setValue(key, value);
  61. if (!m_SettingsBackup.contains(Exists)) {
  62. m_SettingsBackup.setValue(Exists, QVariant(bool(true)));
  63. }
  64. m_SettingsBackup.setValue(key, value);
  65. }
  66. MultimediaSettings::MultimediaSettings(QObject *parent)
  67. : QSettings(persistentPath, QSettings::IniFormat, parent)
  68. , m_SettingsBackup(persistentPathBackup, QSettings::IniFormat, parent)
  69. {
  70. bool ret(true);
  71. if (contains(Exists) && (!m_SettingsBackup.contains(Exists))) {
  72. QFile::remove(persistentPathBackup);
  73. ret = QFile::copy(persistentPath, persistentPathBackup);
  74. } else if ((!contains(Exists)) && m_SettingsBackup.contains(Exists)) {
  75. QFile::remove(persistentPath);
  76. ret = QFile::copy(persistentPathBackup, persistentPath);
  77. }
  78. if (!ret) {
  79. qWarning() << __FUNCTION__ << "copy failed!";
  80. }
  81. }
  82. MultimediaSettings::~MultimediaSettings()
  83. {
  84. qDebug();
  85. }
  86. int MultimediaPersistent::getMusicMillesmial(const DeviceWatcherType type, const bool reload)
  87. {
  88. QString pathInfo("");
  89. switch (type) {
  90. case DWT_SDDisk: {
  91. pathInfo = SDMusicMillesmial;
  92. break;
  93. }
  94. case DWT_USBDisk: {
  95. pathInfo = USBMusicMillesmial;
  96. break;
  97. }
  98. default: {
  99. return 0;
  100. break;
  101. }
  102. }
  103. if (reload) {
  104. g_Settings->sync();
  105. }
  106. bool flag(false);
  107. int ret = g_Settings->value(pathInfo, QVariant(0)).toInt(&flag);
  108. if (!flag) {
  109. ret = 0;
  110. }
  111. return ret;
  112. }
  113. QString MultimediaPersistent::getMusicPathInfo(const DeviceWatcherType type, const bool reload)
  114. {
  115. QString pathInfo("");
  116. switch (type) {
  117. case DWT_SDDisk: {
  118. pathInfo = SDMusicPathInfo;
  119. break;
  120. }
  121. case DWT_USBDisk: {
  122. pathInfo = USBMusicPathInfo;
  123. break;
  124. }
  125. default: {
  126. return pathInfo;
  127. break;
  128. }
  129. }
  130. if (reload) {
  131. g_Settings->sync();
  132. }
  133. return g_Settings->value(pathInfo, QVariant(QString(""))).toString();
  134. }
  135. QString MultimediaPersistent::getImagePathInfo(const DeviceWatcherType type, const bool reload)
  136. {
  137. QString pathInfo("");
  138. QString imagePathInfo("");
  139. switch (type) {
  140. case DWT_SDDisk: {
  141. pathInfo = SDImagePathInfo;
  142. break;
  143. }
  144. case DWT_USBDisk: {
  145. pathInfo = USBImagePathInfo;
  146. break;
  147. }
  148. default: {
  149. return pathInfo;
  150. break;
  151. }
  152. }
  153. if (reload) {
  154. g_Settings->sync();
  155. }
  156. imagePathInfo = g_Settings->value(pathInfo, QVariant(QString(""))).toString();
  157. return imagePathInfo;
  158. }
  159. int MultimediaPersistent::getVideoMillesmial(const DeviceWatcherType type, const bool reload)
  160. {
  161. QString pathInfo("");
  162. switch (type) {
  163. case DWT_SDDisk: {
  164. pathInfo = SDVideoMillesmial;
  165. break;
  166. }
  167. case DWT_USBDisk: {
  168. pathInfo = USBVideoMillesmial;
  169. break;
  170. }
  171. default: {
  172. return 0;
  173. break;
  174. }
  175. }
  176. if (reload) {
  177. g_Settings->sync();
  178. }
  179. bool flag(false);
  180. int ret = g_Settings->value(pathInfo, QVariant(0)).toInt(&flag);
  181. if (!flag) {
  182. ret = 0;
  183. }
  184. return ret;
  185. }
  186. QString MultimediaPersistent::getVideoPathInfo(const DeviceWatcherType type, const bool reload)
  187. {
  188. QString pathInfo("");
  189. switch (type) {
  190. case DWT_SDDisk: {
  191. pathInfo = SDVideoPathInfo;
  192. break;
  193. }
  194. case DWT_USBDisk: {
  195. pathInfo = USBVideoPathInfo;
  196. break;
  197. }
  198. default: {
  199. return pathInfo;
  200. break;
  201. }
  202. }
  203. if (reload) {
  204. g_Settings->sync();
  205. }
  206. return g_Settings->value(pathInfo, QVariant(QString(""))).toString();
  207. }
  208. void MultimediaPersistent::setMultimediaType(const DeviceWatcherType deviceWatcherType, const MultimediaType multimediaType)
  209. {
  210. QString path("");
  211. switch (deviceWatcherType) {
  212. case DWT_SDDisk: {
  213. path = SDMultimedia;
  214. break;
  215. }
  216. case DWT_USBDisk: {
  217. path = USBMultimedia;
  218. break;
  219. }
  220. default: {
  221. return;
  222. break;
  223. }
  224. }
  225. g_Settings->setValue(path, multimediaType);
  226. g_Settings->sync();
  227. }
  228. void MultimediaPersistent::setMusicMillesmial(const DeviceWatcherType type, const int elapsed)
  229. {
  230. switch (type) {
  231. case DWT_SDDisk: {
  232. g_Settings->setValue(SDMusicMillesmial, elapsed);
  233. break;
  234. }
  235. case DWT_USBDisk: {
  236. g_Settings->setValue(USBMusicMillesmial, elapsed);
  237. break;
  238. }
  239. default: {
  240. break;
  241. }
  242. }
  243. g_Settings->sync();
  244. }
  245. void MultimediaPersistent::setMusicPathInfo(const DeviceWatcherType type, const QString &pathInfo)
  246. {
  247. switch (type) {
  248. case DWT_SDDisk: {
  249. g_Settings->setValue(SDMusicPathInfo, pathInfo);
  250. break;
  251. }
  252. case DWT_USBDisk: {
  253. g_Settings->setValue(USBMusicPathInfo, pathInfo);
  254. break;
  255. }
  256. default: {
  257. break;
  258. }
  259. }
  260. g_Settings->sync();
  261. }
  262. void MultimediaPersistent::setImagePathInfo(const DeviceWatcherType type, const QString &pathInfo)
  263. {
  264. switch (type) {
  265. case DWT_SDDisk: {
  266. g_Settings->setValue(SDImagePathInfo, pathInfo);
  267. break;
  268. }
  269. case DWT_USBDisk: {
  270. g_Settings->setValue(USBImagePathInfo, pathInfo);
  271. break;
  272. }
  273. default: {
  274. break;
  275. }
  276. }
  277. g_Settings->sync();
  278. }
  279. /*设置视频断点记忆*///modify by wandz 20190418不再视频记忆,解码错误
  280. void MultimediaPersistent::setVideoMillesmial(const DeviceWatcherType type, const int elapsed)
  281. {
  282. switch (type) {
  283. case DWT_SDDisk: {
  284. g_Settings->setValue(SDVideoMillesmial, elapsed);
  285. break;
  286. }
  287. case DWT_USBDisk: {
  288. g_Settings->setValue(USBVideoMillesmial, elapsed);
  289. break;
  290. }
  291. default: {
  292. break;
  293. }
  294. }
  295. g_Settings->sync();
  296. }
  297. void MultimediaPersistent::setVideoPathInfo(const DeviceWatcherType type, const QString &pathInfo)
  298. {
  299. switch (type) {
  300. case DWT_SDDisk: {
  301. g_Settings->setValue(SDVideoPathInfo, pathInfo);
  302. break;
  303. }
  304. case DWT_USBDisk: {
  305. g_Settings->setValue(USBVideoPathInfo, pathInfo);
  306. break;
  307. }
  308. default: {
  309. break;
  310. }
  311. }
  312. g_Settings->sync();
  313. }
  314. int MultimediaPersistent::getMultimediaType(const DeviceWatcherType deviceWatcherType, const bool reload)
  315. {
  316. QString pathInfo("");
  317. switch (deviceWatcherType) {
  318. case DWT_SDDisk: {
  319. pathInfo = SDMultimedia;
  320. break;
  321. }
  322. case DWT_USBDisk: {
  323. pathInfo = USBMultimedia;
  324. break;
  325. }
  326. default: {
  327. return 0;
  328. break;
  329. }
  330. }
  331. if (reload) {
  332. g_Settings->sync();
  333. }
  334. bool flag(false);
  335. int ret = g_Settings->value(pathInfo, QVariant(0)).toInt(&flag);
  336. if (!flag) {
  337. ret = 0;
  338. }
  339. return ret;
  340. }
  341. void MultimediaPersistent::reset()
  342. {
  343. g_Settings->clear();
  344. g_Settings->sync();
  345. }