guibase.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * <one line to give the library's name and an idea of what it does.>
  3. * Copyright 2015 Malte Veerman <malte.veerman@gmail.com>
  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. #include <KI18n/KLocalizedString>
  28. namespace Fancontrol
  29. {
  30. GUIBase::GUIBase(QObject *parent) : QObject(parent),
  31. m_config(Config::instance()),
  32. #ifndef NO_SYSTEMD
  33. m_com(new SystemdCommunicator(this)),
  34. #endif
  35. m_loader(new Loader(this)),
  36. m_configValid(false),
  37. m_configChanged(false),
  38. m_pwmFanModel(new PwmFanModel(this)),
  39. m_tempModel(new TempModel(this))
  40. {
  41. connect(this, &GUIBase::unitChanged, m_tempModel, &TempModel::setUnit);
  42. connect(m_loader, &Loader::needsSaveChanged, this, &GUIBase::needsApplyChanged);
  43. #ifndef NO_SYSTEMD
  44. connect(m_loader, &Loader::requestSetServiceActive, m_com, &SystemdCommunicator::setServiceActive);
  45. connect(m_com, &SystemdCommunicator::needsApplyChanged, this, &GUIBase::needsApplyChanged);
  46. #endif
  47. const auto locale = QLocale::system();
  48. const auto system = locale.measurementSystem();
  49. m_unit = (system == QLocale::ImperialUSSystem) ? QStringLiteral("°F") : QStringLiteral("°C");
  50. emit unitChanged(m_unit);
  51. m_loader->parseHwmons();
  52. const auto hwmons = m_loader->hwmons();
  53. for (const auto &hwmon : hwmons)
  54. {
  55. m_pwmFanModel->addPwmFans(hwmon->pwmFans());
  56. m_tempModel->addTemps(hwmon->temps());
  57. }
  58. }
  59. void GUIBase::load()
  60. {
  61. m_config->load();
  62. m_configValid = m_loader->load(configUrl());
  63. #ifndef NO_SYSTEMD
  64. m_com->setServiceName(serviceName());
  65. m_com->reset();
  66. #endif
  67. emit serviceNameChanged();
  68. emit minTempChanged();
  69. emit maxTempChanged();
  70. emit configUrlChanged();
  71. }
  72. qreal GUIBase::maxTemp() const
  73. {
  74. return m_config->findItem(QStringLiteral("MaxTemp"))->property().toReal();
  75. }
  76. qreal GUIBase::minTemp() const
  77. {
  78. return m_config->findItem(QStringLiteral("MinTemp"))->property().toReal();
  79. }
  80. QString GUIBase::serviceName() const
  81. {
  82. return m_config->findItem(QStringLiteral("ServiceName"))->property().toString();
  83. }
  84. QUrl GUIBase::configUrl() const
  85. {
  86. return QUrl(m_config->findItem(QStringLiteral("ConfigUrl"))->property().toString());
  87. }
  88. void GUIBase::setMaxTemp(qreal temp)
  89. {
  90. if (temp != maxTemp())
  91. {
  92. m_config->findItem(QStringLiteral("MaxTemp"))->setProperty(temp);
  93. emit maxTempChanged();
  94. m_configChanged = true;
  95. emit needsApplyChanged();
  96. }
  97. }
  98. void GUIBase::setMinTemp(qreal temp)
  99. {
  100. if (temp != minTemp())
  101. {
  102. m_config->findItem(QStringLiteral("MinTemp"))->setProperty(temp);
  103. emit minTempChanged();
  104. m_configChanged = true;
  105. emit needsApplyChanged();
  106. }
  107. }
  108. void GUIBase::setServiceName(const QString& name)
  109. {
  110. if(name != serviceName())
  111. {
  112. m_config->findItem(QStringLiteral("ServiceName"))->setProperty(name);
  113. #ifndef NO_SYSTEMD
  114. m_com->setServiceName(name);
  115. #endif
  116. emit serviceNameChanged();
  117. m_configChanged = true;
  118. emit needsApplyChanged();
  119. }
  120. }
  121. void GUIBase::setConfigUrl(const QUrl &url)
  122. {
  123. if (url != configUrl())
  124. {
  125. m_configValid = m_loader->load(url);
  126. m_config->findItem(QStringLiteral("ConfigUrl"))->setProperty(url.toString());
  127. emit configUrlChanged();
  128. m_configChanged = true;
  129. emit needsApplyChanged();
  130. }
  131. }
  132. bool GUIBase::needsApply() const
  133. {
  134. #ifndef NO_SYSTEMD
  135. return m_loader->needsSave() || m_configChanged || m_com->needsApply();
  136. #else
  137. return m_loader->needsSave() || m_configChanged;
  138. #endif
  139. }
  140. bool GUIBase::hasSystemdCommunicator() const
  141. {
  142. #ifndef NO_SYSTEMD
  143. return true;
  144. #else
  145. return false;
  146. #endif
  147. }
  148. void GUIBase::apply()
  149. {
  150. qInfo() << i18n("Applying changes");
  151. bool configChanged = m_loader->save(configUrl());
  152. m_config->save();
  153. m_configChanged = false;
  154. #ifndef NO_SYSTEMD
  155. m_com->apply(configChanged);
  156. #endif
  157. emit needsApplyChanged();
  158. }
  159. void GUIBase::reset()
  160. {
  161. qInfo() << i18n("Resetting changes");
  162. m_config->load();
  163. emit serviceNameChanged();
  164. emit minTempChanged();
  165. emit maxTempChanged();
  166. emit configUrlChanged();
  167. m_configChanged = false;
  168. if (m_loader->needsSave() || configUrl() != m_loader->configUrl())
  169. m_loader->load(configUrl());
  170. #ifndef NO_SYSTEMD
  171. m_com->setServiceName(serviceName());
  172. m_com->reset();
  173. #endif
  174. emit needsApplyChanged();
  175. }
  176. void GUIBase::handleError(const QString &error, bool critical)
  177. {
  178. if (error.isEmpty() || error == m_error)
  179. return;
  180. m_error = error;
  181. emit errorChanged();
  182. if (critical)
  183. {
  184. qCritical() << error;
  185. emit criticalError();
  186. }
  187. else
  188. qWarning() << error;
  189. }
  190. void GUIBase::handleInfo(const QString &info)
  191. {
  192. if (info.isEmpty())
  193. return;
  194. else
  195. qInfo() << info;
  196. }
  197. }