2
0

fan.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. namespace Fancontrol
  27. {
  28. Fan::Fan(Hwmon *parent, uint index) :
  29. Sensor(parent, index, QString(parent->name() + QString("/fan") + QString::number(index))),
  30. m_rpmStream(new QTextStream)
  31. {
  32. if (QDir(parent->path()).isReadable())
  33. {
  34. QFile *rpmFile = new QFile(parent->path() + "/fan" + QString::number(index) + "_input", this);
  35. if (rpmFile->open(QFile::ReadOnly))
  36. {
  37. m_rpmStream->setDevice(rpmFile);
  38. *m_rpmStream >> m_rpm;
  39. }
  40. else
  41. emit errorChanged("Can't open rpmFile " + parent->path() + "/fan" + QString::number(index) + "_input");
  42. }
  43. }
  44. Fan::~Fan()
  45. {
  46. delete m_rpmStream;
  47. }
  48. QString Fan::name() const
  49. {
  50. KConfigGroup names = KSharedConfig::openConfig("fancontrol-gui")->group("names");
  51. KConfigGroup localNames = names.group(m_parent->name());
  52. QString name = localNames.readEntry("fan" + QString::number(m_index), QString());
  53. if (name.isEmpty())
  54. return "fan" + QString::number(m_index);
  55. return name;
  56. }
  57. void Fan::setName(const QString &name)
  58. {
  59. KConfigGroup names = KSharedConfig::openConfig("fancontrol-gui")->group("names");
  60. KConfigGroup localNames = names.group(m_parent->name());
  61. if (name != localNames.readEntry("fan" + QString::number(m_index), QString())
  62. && !name.isEmpty())
  63. {
  64. localNames.writeEntry("fan" + QString::number(m_index), name);
  65. emit nameChanged();
  66. }
  67. }
  68. void Fan::reset()
  69. {
  70. QIODevice *oldFile = m_rpmStream->device();
  71. delete m_rpmStream;
  72. delete oldFile;
  73. if (QDir(m_parent->path()).isReadable())
  74. {
  75. QFile *rpmFile = new QFile(m_parent->path() + "/fan" + QString::number(m_index) + "_input", this);
  76. if (rpmFile->open(QFile::ReadOnly))
  77. {
  78. m_rpmStream = new QTextStream(rpmFile);
  79. *m_rpmStream >> m_rpm;
  80. }
  81. else
  82. emit errorChanged("Can't open rpmFile " + m_parent->path() + "/fan" + QString::number(m_index) + "_input");
  83. }
  84. }
  85. void Fan::update()
  86. {
  87. m_rpmStream->seek(0);
  88. *m_rpmStream >> m_rpm;
  89. emit rpmChanged();
  90. }
  91. }