fileconfigdecode.cpp 5.8 KB

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