fan.cpp 3.1 KB

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