fan.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (C) 2015 Malte Veerman <malte.veerman@gmail.com>
  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, bool device) :
  31. Sensor(parent, index, QStringLiteral("fan"), device),
  32. m_rpmStream(new QTextStream),
  33. m_rpm(0)
  34. {
  35. if (!parent)
  36. return;
  37. auto path = device ? parent->path() + "/device" : parent->path();
  38. if (QDir(path).isReadable())
  39. {
  40. const auto rpmFile = new QFile(path + "/fan" + QString::number(index) + "_input", this);
  41. if (rpmFile->open(QFile::ReadOnly))
  42. {
  43. m_rpmStream->setDevice(rpmFile);
  44. *m_rpmStream >> m_rpm;
  45. }
  46. else
  47. {
  48. emit error(i18n("Can't open rpm file: \'%1\'", rpmFile->fileName()));
  49. delete rpmFile;
  50. }
  51. }
  52. }
  53. Fan::~Fan()
  54. {
  55. auto device = m_rpmStream->device();
  56. delete m_rpmStream;
  57. delete device;
  58. }
  59. QString Fan::name() const
  60. {
  61. const auto names = KSharedConfig::openConfig(QStringLiteral("fancontrol-gui"))->group("names");
  62. const auto localNames = names.group(parent() ? parent()->name() : QStringLiteral(TEST_HWMON_NAME));
  63. const auto name = localNames.readEntry("fan" + QString::number(index()), QString());
  64. if (name.isEmpty())
  65. return "fan" + QString::number(index());
  66. return name;
  67. }
  68. void Fan::setName(const QString &name)
  69. {
  70. const auto names = KSharedConfig::openConfig(QStringLiteral("fancontrol-gui"))->group("names");
  71. auto localNames = names.group(parent() ? parent()->name() : QStringLiteral(TEST_HWMON_NAME));
  72. if (name != localNames.readEntry("fan" + QString::number(index()), QString())
  73. && !name.isEmpty())
  74. {
  75. localNames.writeEntry("fan" + QString::number(index()), name);
  76. emit nameChanged();
  77. }
  78. }
  79. void Fan::toDefault()
  80. {
  81. if (m_rpmStream->device() && parent())
  82. {
  83. auto device = m_rpmStream->device();
  84. m_rpmStream->setDevice(Q_NULLPTR);
  85. delete device;
  86. if (QDir(parent()->path()).isReadable())
  87. {
  88. const auto rpmFile = new QFile(parent()->path() + "/fan" + QString::number(index()) + "_input", this);
  89. if (rpmFile->open(QFile::ReadOnly))
  90. {
  91. m_rpmStream->setDevice(rpmFile);
  92. *m_rpmStream >> m_rpm;
  93. }
  94. else
  95. {
  96. emit error(i18n("Can't open rpm file: \'%1\'", rpmFile->fileName()));
  97. delete rpmFile;
  98. }
  99. }
  100. }
  101. }
  102. void Fan::update()
  103. {
  104. m_rpmStream->seek(0);
  105. int rpm;
  106. *m_rpmStream >> rpm;
  107. if (rpm != m_rpm)
  108. {
  109. m_rpm = rpm;
  110. emit rpmChanged();
  111. }
  112. }
  113. bool Fan::isValid() const
  114. {
  115. return m_rpmStream->device() || m_rpmStream->string();
  116. }
  117. }