guibase.cpp 3.5 KB

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