fileconfigdecode.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. QSettings* config = new QSettings(path, QSettings::IniFormat);
  70. config->setIniCodec(QTextCodec::codecForName("utf-8"));
  71. QString section = QString("config");
  72. config->beginGroup(section);
  73. config->setValue("address", address);
  74. config->setValue("port", port);
  75. config->setValue("autosetup", autosetup);
  76. config->setValue("rename", rename);
  77. config->setValue("timerename", timerename);
  78. config->setValue("autocopy", autocopy);
  79. config->endGroup();
  80. delete config;
  81. }
  82. void FileConfigDecode::initFile()
  83. {
  84. QString addDataPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
  85. QString addFilePath = addDataPath + "config.ini";
  86. qDebug() << addFilePath;
  87. QFile file(addFilePath);
  88. if (file.exists()) {
  89. QSettings* config = new QSettings(addFilePath, QSettings::IniFormat);
  90. config->setIniCodec(QTextCodec::codecForName("utf-8"));
  91. QString section = "config/";
  92. this->address = config->value(section + "address").toString();
  93. this->port = config->value(section + "port").toUInt();
  94. this->autoSetup = config->value(section + "autosetup").toBool();
  95. this->rename = config->value(section + "rename").toBool();
  96. this->timeRename = config->value(section + "timerename").toBool();
  97. this->autoCopy = config->value(section + "autocopy").toBool();
  98. checkConfig();
  99. this->firstCreate = false;
  100. }
  101. else {
  102. this->firstCreate = true;
  103. this->address = "127.0.0.1";
  104. this->port = 0;
  105. this->autoSetup = false;
  106. this->rename = false;
  107. this->timeRename = false;
  108. this->autoCopy = false;
  109. fileWrite(addFilePath, address, port, autoSetup, rename, timeRename, autoCopy);
  110. }
  111. }
  112. void FileConfigDecode::checkConfig()
  113. {
  114. if (address.isEmpty()) {
  115. address = "127.0.0.1";
  116. }
  117. }
  118. void FileConfigDecode::updateFile()
  119. {
  120. QString addDataPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
  121. QString addFilePath = addDataPath + "config.ini";
  122. qDebug() << "write" << autoSetup << rename << timeRename << autoCopy;
  123. fileWrite(addFilePath, address, port, autoSetup, rename, timeRename, autoCopy);
  124. }