fan.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (C) 2015 Malte Veerman <maldela@halloarsch.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. */
  19. #include "fan.h"
  20. #include "hwmon.h"
  21. #include <QtCore/QTextStream>
  22. #include <QtCore/QDir>
  23. #include <QtCore/QFile>
  24. #include <KConfigCore/KSharedConfig>
  25. #include <KConfigCore/KConfigGroup>
  26. #include <KI18n/KLocalizedString>
  27. #define TEST_HWMON_NAME "test"
  28. namespace Fancontrol
  29. {
  30. Fan::Fan(uint index, Hwmon *parent) : Sensor(parent, index, parent ? parent->name() + "/fan" + QString::number(index) : QString()),
  31. m_rpmStream(new QTextStream),
  32. m_rpm(0)
  33. {
  34. if (m_parent && QDir(parent->path()).isReadable())
  35. {
  36. const auto rpmFile = new QFile(parent->path() + "/fan" + QString::number(index) + "_input", this);
  37. if (rpmFile->open(QFile::ReadOnly))
  38. {
  39. m_rpmStream->setDevice(rpmFile);
  40. *m_rpmStream >> m_rpm;
  41. }
  42. else
  43. {
  44. emit error(i18n("Can't open rpm file: \"%1\"", rpmFile->fileName()));
  45. delete rpmFile;
  46. }
  47. }
  48. }
  49. Fan::~Fan()
  50. {
  51. auto device = m_rpmStream->device();
  52. delete m_rpmStream;
  53. delete device;
  54. }
  55. QString Fan::name() const
  56. {
  57. const auto names = KSharedConfig::openConfig(QStringLiteral("fancontrol-gui"))->group("names");
  58. const auto localNames = names.group(m_parent ? m_parent->name() : QStringLiteral(TEST_HWMON_NAME));
  59. const auto name = localNames.readEntry("fan" + QString::number(m_index), QString());
  60. if (name.isEmpty())
  61. return "fan" + QString::number(m_index);
  62. return name;
  63. }
  64. void Fan::setName(const QString &name)
  65. {
  66. const auto names = KSharedConfig::openConfig(QStringLiteral("fancontrol-gui"))->group("names");
  67. auto localNames = names.group(m_parent ? m_parent->name() : QStringLiteral(TEST_HWMON_NAME));
  68. if (name != localNames.readEntry("fan" + QString::number(m_index), QString())
  69. && !name.isEmpty())
  70. {
  71. localNames.writeEntry("fan" + QString::number(m_index), name);
  72. emit nameChanged();
  73. }
  74. }
  75. void Fan::reset()
  76. {
  77. if (m_rpmStream->device() && m_parent)
  78. {
  79. auto device = m_rpmStream->device();
  80. m_rpmStream->setDevice(Q_NULLPTR);
  81. delete device;
  82. if (QDir(m_parent->path()).isReadable())
  83. {
  84. const auto rpmFile = new QFile(m_parent->path() + "/fan" + QString::number(m_index) + "_input", this);
  85. if (rpmFile->open(QFile::ReadOnly))
  86. {
  87. m_rpmStream->setDevice(rpmFile);
  88. *m_rpmStream >> m_rpm;
  89. }
  90. else
  91. {
  92. emit error(i18n("Can't open rpm file: \"%1\"", rpmFile->fileName()));
  93. delete rpmFile;
  94. }
  95. }
  96. }
  97. }
  98. void Fan::update()
  99. {
  100. m_rpmStream->seek(0);
  101. int rpm;
  102. *m_rpmStream >> rpm;
  103. if (rpm != m_rpm)
  104. {
  105. m_rpm = rpm;
  106. emit rpmChanged();
  107. }
  108. }
  109. bool Fan::isValid() const
  110. {
  111. return m_rpmStream->device() || m_rpmStream->string();
  112. }
  113. }