fileconfigdecode.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include "fileconfigdecode.h"
  2. #include <QDebug>
  3. QScopedPointer<FileConfigDecode> FileConfigDecode::m_instance;
  4. FileConfigDecode::FileConfigDecode()
  5. {
  6. // if (m_instance.isNull()) {
  7. // m_instance.reset(new FileConfigDecode);
  8. // }
  9. initFile();
  10. }
  11. FileConfigDecode *FileConfigDecode::getInstance()
  12. {
  13. if (m_instance.isNull()) {
  14. m_instance.reset(new FileConfigDecode);
  15. }
  16. return m_instance.data();
  17. }
  18. FileConfigDecode::~FileConfigDecode()
  19. {
  20. updateFile();
  21. }
  22. QString FileConfigDecode::getAddress()
  23. {
  24. return this->address;
  25. }
  26. quint16 FileConfigDecode::getPort()
  27. {
  28. return port;
  29. }
  30. bool FileConfigDecode::getAutoSetup()
  31. {
  32. return autoSetup;
  33. }
  34. bool FileConfigDecode::getRename()
  35. {
  36. return rename;
  37. }
  38. bool FileConfigDecode::getTimeRename()
  39. {
  40. return timeRename;
  41. }
  42. bool FileConfigDecode::getAutoCopy()
  43. {
  44. return autoCopy;
  45. }
  46. QString FileConfigDecode::getUserName()
  47. {
  48. return userName;
  49. }
  50. QString FileConfigDecode::getPassword()
  51. {
  52. return password;
  53. }
  54. RenameType FileConfigDecode::getRenameType()
  55. {
  56. return renameType;
  57. }
  58. bool FileConfigDecode::getAutoLogin()
  59. {
  60. return autoLogin;
  61. }
  62. void FileConfigDecode::setAddress(QString value)
  63. {
  64. this->address = value;
  65. updateFile();
  66. }
  67. void FileConfigDecode::setPort(quint16 value)
  68. {
  69. this->port = value;
  70. updateFile();
  71. }
  72. void FileConfigDecode::setAutoSetup(bool value)
  73. {
  74. this->autoSetup = value;
  75. updateFile();
  76. }
  77. void FileConfigDecode::setRename(bool value)
  78. {
  79. this->rename = value;
  80. if (value == true) {
  81. renameType = (renameType == RENAME_TYPE_TIME) ? RENAME_TYPE_TIME : RENAME_TYPE_NORMAL;
  82. }
  83. else {
  84. renameType = (renameType == RENAME_TYPE_TIME) ? RENAME_TYPE_TIME : RENAME_TYPE_NORMAL;
  85. }
  86. // qDebug() << renameType;
  87. updateFile();
  88. }
  89. void FileConfigDecode::setTimeRename(bool value, int flag)
  90. {
  91. qDebug() << value;
  92. this->timeRename = value;
  93. if (value) {
  94. renameType = RENAME_TYPE_TIME;
  95. }
  96. if (!value && flag == 1) {
  97. renameType = RENAME_TYPE_NORMAL;
  98. }
  99. if (!value && flag == 0) {
  100. renameType = RENAME_TYPE_NONE;
  101. }
  102. // qDebug() << renameType;
  103. updateFile();
  104. }
  105. void FileConfigDecode::setAutoCopy(bool value)
  106. {
  107. this->autoCopy = value;
  108. updateFile();
  109. }
  110. void FileConfigDecode::setUserName(QString username)
  111. {
  112. this->userName = username;
  113. updateFile();
  114. }
  115. void FileConfigDecode::setPassword(QString password)
  116. {
  117. this->password = password;
  118. updateFile();
  119. }
  120. void FileConfigDecode::setAutoLogin(bool value)
  121. {
  122. autoLogin = value;
  123. updateFile();
  124. }
  125. void FileConfigDecode::fileWrite(QString path)
  126. {
  127. #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
  128. QSettings* config = new QSettings(path, QSettings::IniFormat);
  129. config->setIniCodec(QTextCodec::codecForName("utf-8"));
  130. QString section = QString("config");
  131. config->beginGroup(section);
  132. config->setValue("address", address);
  133. config->setValue("autosetup", autosetup);
  134. config->setValue("rename", rename);
  135. config->setValue("timerename", timerename);
  136. config->setValue("autocopy", autocopy);
  137. config->endGroup();
  138. delete config;
  139. #else
  140. QSettings* config = new QSettings(path, QSettings::IniFormat);
  141. QString section = QString("config");
  142. config->beginGroup(section);
  143. config->setValue("address", address);
  144. config->setValue("autosetup", autoSetup);
  145. config->setValue("rename", rename);
  146. config->setValue("timerename", timeRename);
  147. config->setValue("autocopy", autoCopy);
  148. config->setValue("username", userName);
  149. config->setValue("password", password);
  150. config->setValue("autologin", autoLogin);
  151. delete config;
  152. #endif
  153. }
  154. void FileConfigDecode::initFile()
  155. {
  156. QString addDataPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
  157. QString addFilePath = addDataPath + "/config.ini";
  158. qDebug() << addFilePath;
  159. QFile file(addFilePath);
  160. if (file.exists()) {
  161. QSettings* config = new QSettings(addFilePath, QSettings::IniFormat);
  162. QString section = "config/";
  163. this->address = config->value(section + "address").toString();
  164. this->port = config->value(section + "port").toUInt();
  165. this->autoSetup = config->value(section + "autosetup").toBool();
  166. this->rename = config->value(section + "rename").toBool();
  167. this->timeRename = config->value(section + "timerename").toBool();
  168. this->autoCopy = config->value(section + "autocopy").toBool();
  169. this->userName = config->value(section+"username").toString();
  170. this->password = config->value(section+"password").toString();
  171. this->autoLogin = config->value(section+"autologin").toBool();
  172. checkConfig();
  173. this->firstCreate = false;
  174. }
  175. else {
  176. qDebug() << "file not exists";
  177. this->firstCreate = true;
  178. this->address = "127.0.0.1:8080";
  179. this->autoSetup = false;
  180. this->rename = false;
  181. this->timeRename = false;
  182. this->autoCopy = false;
  183. this->userName = "admin";
  184. this->password = "admin";
  185. this->autoLogin = false;
  186. fileWrite(addFilePath);
  187. }
  188. }
  189. void FileConfigDecode::checkConfig()
  190. {
  191. if (address.isEmpty()) {
  192. address = "127.0.0.1:8080";
  193. }
  194. if (rename)
  195. renameType = RENAME_TYPE_NORMAL;
  196. if (timeRename)
  197. renameType = RENAME_TYPE_TIME;
  198. qDebug() << renameType;
  199. }
  200. void FileConfigDecode::updateFile()
  201. {
  202. QString addDataPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
  203. QString addFilePath = addDataPath + "/config.ini";
  204. // qDebug() << "write" << this->autoSetup << this->rename << this->timeRename << this->autoCopy;
  205. fileWrite(addFilePath);
  206. }