guibase.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * <one line to give the library's name and an idea of what it does.>
  3. * Copyright 2015 Malte Veerman maldela@halloarsch.de
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License or (at your option) version 3 or any later version
  9. * accepted by the membership of KDE e.V. (or its successor approved
  10. * by the membership of KDE e.V.), which shall act as a proxy
  11. * defined in Section 14 of version 3 of the license.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "guibase.h"
  23. #include "config.h"
  24. #include "hwmon.h"
  25. #include <QtCore/QLocale>
  26. #include <QtCore/QDebug>
  27. namespace Fancontrol
  28. {
  29. GUIBase::GUIBase(QObject *parent) : QObject(parent),
  30. m_config(Config::instance()),
  31. #ifndef NO_SYSTEMD
  32. m_com(new SystemdCommunicator(this)),
  33. #endif
  34. m_loader(new Loader(this)),
  35. m_configValid(false),
  36. m_pwmFanModel(new PwmFanModel(this)),
  37. m_tempModel(new TempModel(this))
  38. {
  39. connect(m_config, &Config::configChanged, this, &GUIBase::emitConfigChanged);
  40. connect(this, &GUIBase::unitChanged, m_tempModel, &TempModel::setUnit);
  41. #ifndef NO_SYSTEMD
  42. connect(m_loader, &Loader::requestSetServiceActive, m_com, &SystemdCommunicator::setServiceActive);
  43. #endif
  44. const auto locale = QLocale::system();
  45. const auto system = locale.measurementSystem();
  46. m_unit = (system == QLocale::ImperialUSSystem) ? QStringLiteral("°F") : QStringLiteral("°C");
  47. emit unitChanged(m_unit);
  48. m_loader->parseHwmons();
  49. const auto hwmons = m_loader->hwmons();
  50. for (const auto &hwmon : hwmons)
  51. {
  52. m_pwmFanModel->addPwmFans(hwmon->pwmFans());
  53. m_tempModel->addTemps(hwmon->temps());
  54. }
  55. }
  56. void GUIBase::load()
  57. {
  58. m_config->load();
  59. m_configValid = m_loader->load(configUrl());
  60. #ifndef NO_SYSTEMD
  61. m_com->setServiceName(serviceName());
  62. #endif
  63. emitConfigChanged();
  64. }
  65. void GUIBase::save(bool saveLoader, const QUrl &url)
  66. {
  67. m_config->save();
  68. if (saveLoader)
  69. m_loader->save(url);
  70. }
  71. qreal GUIBase::maxTemp() const
  72. {
  73. return m_config->findItem(QStringLiteral("MaxTemp"))->property().toReal();
  74. }
  75. qreal GUIBase::minTemp() const
  76. {
  77. return m_config->findItem(QStringLiteral("MinTemp"))->property().toReal();
  78. }
  79. QString GUIBase::serviceName() const
  80. {
  81. return m_config->findItem(QStringLiteral("ServiceName"))->property().toString();
  82. }
  83. QUrl GUIBase::configUrl() const
  84. {
  85. return QUrl(m_config->findItem(QStringLiteral("ConfigUrl"))->property().toString());
  86. }
  87. void GUIBase::setMaxTemp(qreal temp)
  88. {
  89. if (temp != maxTemp())
  90. {
  91. m_config->findItem(QStringLiteral("MaxTemp"))->setProperty(temp);
  92. emit maxTempChanged();
  93. }
  94. }
  95. void GUIBase::setMinTemp(qreal temp)
  96. {
  97. if (temp != minTemp())
  98. {
  99. m_config->findItem(QStringLiteral("MinTemp"))->setProperty(temp);
  100. emit minTempChanged();
  101. }
  102. }
  103. void GUIBase::setServiceName(const QString& name)
  104. {
  105. if(name != serviceName())
  106. {
  107. m_config->findItem(QStringLiteral("ServiceName"))->setProperty(name);
  108. #ifndef NO_SYSTEMD
  109. m_com->setServiceName(name);
  110. #endif
  111. emit serviceNameChanged();
  112. }
  113. }
  114. void GUIBase::setConfigUrl(const QUrl &url)
  115. {
  116. if (url != configUrl())
  117. {
  118. m_configValid = m_loader->load(url);
  119. m_config->findItem(QStringLiteral("ConfigUrl"))->setProperty(url.toString());
  120. emit configUrlChanged();
  121. }
  122. }
  123. void GUIBase::emitConfigChanged()
  124. {
  125. emit serviceNameChanged();
  126. emit minTempChanged();
  127. emit maxTempChanged();
  128. emit configUrlChanged();
  129. }
  130. bool GUIBase::hasSystemdCommunicator() const
  131. {
  132. #ifndef NO_SYSTEMD
  133. return true;
  134. #else
  135. return false;
  136. #endif
  137. }
  138. void GUIBase::handleError(const QString &error, bool critical)
  139. {
  140. if (error.isEmpty() || error == m_error)
  141. return;
  142. m_error = error;
  143. emit errorChanged();
  144. if (critical)
  145. {
  146. qCritical() << error;
  147. emit criticalError();
  148. }
  149. else
  150. qWarning() << error;
  151. }
  152. }