2
0

guibase.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 <QtQml/qqml.h>
  26. #include <QtCore/QLocale>
  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. QLocale locale = QLocale::system();
  41. QLocale::MeasurementSystem system = locale.measurementSystem();
  42. m_unit = (system == QLocale::MetricSystem) ? 0 : 2;
  43. qmlRegisterType<Loader>();
  44. qmlRegisterType<Hwmon>();
  45. qmlRegisterType<Fan>();
  46. qmlRegisterType<PwmFan>();
  47. qmlRegisterType<Temp>();
  48. #ifndef NO_SYSTEMD
  49. qmlRegisterType<SystemdCommunicator>();
  50. #endif
  51. m_tempModel->setUnit(m_unit);
  52. foreach (Hwmon *hwmon, m_loader->hwmons())
  53. {
  54. m_pwmFanModel->addPwmFans(hwmon->pwmFans());
  55. m_tempModel->addTemps(hwmon->temps());
  56. }
  57. }
  58. void GUIBase::load()
  59. {
  60. m_config->load();
  61. m_configValid = m_loader->load(configUrl());
  62. #ifndef NO_SYSTEMD
  63. m_com->setServiceName(serviceName());
  64. #endif
  65. emitConfigChanged();
  66. }
  67. void GUIBase::save(bool saveLoader, const QUrl &url)
  68. {
  69. m_config->save();
  70. if (saveLoader)
  71. m_loader->save(url);
  72. }
  73. qreal GUIBase::maxTemp() const
  74. {
  75. return m_config->findItem(QStringLiteral("MaxTemp"))->property().toReal();
  76. }
  77. qreal GUIBase::minTemp() const
  78. {
  79. return m_config->findItem(QStringLiteral("MinTemp"))->property().toReal();
  80. }
  81. QString GUIBase::serviceName() const
  82. {
  83. return m_config->findItem(QStringLiteral("ServiceName"))->property().toString();
  84. }
  85. QUrl GUIBase::configUrl() const
  86. {
  87. return QUrl(m_config->findItem(QStringLiteral("ConfigUrl"))->property().toString());
  88. }
  89. void GUIBase::setMaxTemp(qreal temp)
  90. {
  91. if (temp != maxTemp())
  92. {
  93. m_config->findItem(QStringLiteral("MaxTemp"))->setProperty(temp);
  94. emit maxTempChanged();
  95. }
  96. }
  97. void GUIBase::setMinTemp(qreal temp)
  98. {
  99. if (temp != minTemp())
  100. {
  101. m_config->findItem(QStringLiteral("MinTemp"))->setProperty(temp);
  102. emit minTempChanged();
  103. }
  104. }
  105. void GUIBase::setServiceName(const QString& name)
  106. {
  107. if(name != serviceName())
  108. {
  109. m_config->findItem(QStringLiteral("ServiceName"))->setProperty(name);
  110. #ifndef NO_SYSTEMD
  111. m_com->setServiceName(name);
  112. #endif
  113. emit serviceNameChanged();
  114. }
  115. }
  116. void GUIBase::setConfigUrl(const QUrl &url)
  117. {
  118. if (url != configUrl())
  119. {
  120. m_config->findItem(QStringLiteral("ConfigUrl"))->setProperty(url.toString(QUrl::PreferLocalFile));
  121. m_configValid = m_loader->load(url);
  122. emit configUrlChanged();
  123. }
  124. }
  125. void GUIBase::emitConfigChanged()
  126. {
  127. emit serviceNameChanged();
  128. emit minTempChanged();
  129. emit maxTempChanged();
  130. emit configUrlChanged();
  131. }
  132. }