fileconfigdecode.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include "fileconfigdecode.h"
  2. #include <QDebug>
  3. FileConfigDecode::FileConfigDecode()
  4. {
  5. initFile();
  6. qDebug() << "read:" << autoSetup << rename << timeRename << autoCopy;
  7. }
  8. FileConfigDecode::~FileConfigDecode()
  9. {
  10. updateFile();
  11. // qDebug() << "write:" << autoSetup << rename << timeRename << autoCopy;
  12. }
  13. QString FileConfigDecode::getAddress()
  14. {
  15. return this->address;
  16. }
  17. quint16 FileConfigDecode::getPort()
  18. {
  19. return port;
  20. }
  21. bool FileConfigDecode::getAutoSetup()
  22. {
  23. return autoSetup;
  24. }
  25. bool FileConfigDecode::getRename()
  26. {
  27. return rename;
  28. }
  29. bool FileConfigDecode::getTimeRename()
  30. {
  31. return timeRename;
  32. }
  33. bool FileConfigDecode::getAutoCopy()
  34. {
  35. return autoCopy;
  36. }
  37. void FileConfigDecode::setAddress(QString value)
  38. {
  39. this->address = value;
  40. // updateFile();
  41. }
  42. void FileConfigDecode::setPort(quint16 value)
  43. {
  44. this->port = value;
  45. // updateFile();
  46. }
  47. void FileConfigDecode::setAutoSetup(bool value)
  48. {
  49. this->autoSetup = value;
  50. // updateFile();
  51. }
  52. void FileConfigDecode::setRename(bool value)
  53. {
  54. this->rename = value;
  55. // updateFile();
  56. }
  57. void FileConfigDecode::setTimeRename(bool value)
  58. {
  59. this->timeRename = value;
  60. // updateFile();
  61. }
  62. void FileConfigDecode::setAutoCopy(bool value)
  63. {
  64. this->autoCopy = value;
  65. // updateFile();
  66. }
  67. void FileConfigDecode::fileWrite(QString path, QString address, quint16 port, bool autosetup, bool rename, bool timerename, bool autocopy)
  68. {
  69. #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
  70. QSettings* config = new QSettings(path, QSettings::IniFormat);
  71. config->setIniCodec(QTextCodec::codecForName("utf-8"));
  72. QString section = QString("config");
  73. config->beginGroup(section);
  74. config->setValue("address", address);
  75. config->setValue("port", port);
  76. config->setValue("autosetup", autosetup);
  77. config->setValue("rename", rename);
  78. config->setValue("timerename", timerename);
  79. config->setValue("autocopy", autocopy);
  80. config->endGroup();
  81. delete config;
  82. #else
  83. QSettings* config = new QSettings(path, QSettings::IniFormat);
  84. QString section = QString("config");
  85. config->beginGroup(section);
  86. config->setValue("address", address);
  87. config->setValue("port", port);
  88. config->setValue("autosetup", autosetup);
  89. config->setValue("rename", rename);
  90. config->setValue("timerename", timerename);
  91. config->setValue("autocopy", autocopy);
  92. config->endGroup();
  93. delete config;
  94. #endif
  95. }
  96. void FileConfigDecode::initFile()
  97. {
  98. QString addDataPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
  99. QString addFilePath = addDataPath + "config.ini";
  100. qDebug() << addFilePath;
  101. QFile file(addFilePath);
  102. if (file.exists()) {
  103. QSettings* config = new QSettings(addFilePath, QSettings::IniFormat);
  104. QString section = "config/";
  105. this->address = config->value(section + "address").toString();
  106. this->port = config->value(section + "port").toUInt();
  107. this->autoSetup = config->value(section + "autosetup").toBool();
  108. this->rename = config->value(section + "rename").toBool();
  109. this->timeRename = config->value(section + "timerename").toBool();
  110. this->autoCopy = config->value(section + "autocopy").toBool();
  111. checkConfig();
  112. this->firstCreate = false;
  113. }
  114. else {
  115. this->firstCreate = true;
  116. this->address = "127.0.0.1";
  117. this->port = 0;
  118. this->autoSetup = false;
  119. this->rename = false;
  120. this->timeRename = false;
  121. this->autoCopy = false;
  122. fileWrite(addFilePath, address, port, autoSetup, rename, timeRename, autoCopy);
  123. }
  124. }
  125. void FileConfigDecode::checkConfig()
  126. {
  127. if (address.isEmpty()) {
  128. address = "127.0.0.1";
  129. }
  130. }
  131. void FileConfigDecode::updateFile()
  132. {
  133. QString addDataPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
  134. QString addFilePath = addDataPath + "config.ini";
  135. qDebug() << "write" << autoSetup << rename << timeRename << autoCopy;
  136. fileWrite(addFilePath, address, port, autoSetup, rename, timeRename, autoCopy);
  137. }