fan.cpp 3.0 KB

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