temp.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * <one line to give the library's name and an idea of what it does.>
  3. * Copyright 2015 Malte Veerman maldela@halloarsch.de
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License or (at your option) version 3 or any later version
  9. * accepted by the membership of KDE e.V. (or its successor approved
  10. * by the membership of KDE e.V.), which shall act as a proxy
  11. * defined in Section 14 of version 3 of the license.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "temp.h"
  23. #include <QtCore/QTextStream>
  24. #include <QtCore/QFile>
  25. #include <QtCore/QDir>
  26. #include <QtCore/QDebug>
  27. #include <KConfigCore/KSharedConfig>
  28. #include <KConfigCore/KConfigGroup>
  29. #include "hwmon.h"
  30. namespace Fancontrol
  31. {
  32. Temp::Temp(Hwmon *parent, uint index) :
  33. Sensor(parent, index, QString(parent->name() + QString("/temp") + QString::number(index))),
  34. m_valueStream(new QTextStream)
  35. {
  36. if (QDir(parent->path()).isReadable())
  37. {
  38. QFile *valueFile = new QFile(parent->path() + "/temp" + QString::number(index) + "_input", this);
  39. QFile labelFile(parent->path() + "/temp" + QString::number(index) + "_label");
  40. if (valueFile->open(QFile::ReadOnly))
  41. {
  42. m_valueStream->setDevice(valueFile);
  43. *m_valueStream >> m_value;
  44. m_value /= 1000;
  45. }
  46. else
  47. qCritical() << "Can't open valueFile " << parent->path() + "/temp" + QString::number(index) + "_input";
  48. if (labelFile.open(QFile::ReadOnly))
  49. m_label = QTextStream(&labelFile).readLine();
  50. }
  51. }
  52. Temp::~Temp()
  53. {
  54. delete m_valueStream;
  55. }
  56. QString Temp::name() const
  57. {
  58. KConfigGroup names = KSharedConfig::openConfig("fancontrol-gui")->group("names");
  59. KConfigGroup localNames = names.group(m_parent->name());
  60. QString name = localNames.readEntry("temp" + QString::number(m_index), QString());
  61. if (name.isEmpty())
  62. {
  63. if (m_label.isEmpty())
  64. return "temp" + QString::number(m_index);
  65. return m_label;
  66. }
  67. return name;
  68. }
  69. void Temp::setName(const QString &name)
  70. {
  71. KConfigGroup names = KSharedConfig::openConfig("fancontrol-gui")->group("names");
  72. KConfigGroup localNames = names.group(m_parent->name());
  73. if (name != localNames.readEntry("temp" + QString::number(m_index), QString())
  74. && !name.isEmpty())
  75. {
  76. localNames.writeEntry(m_parent->name() + "temp" + QString::number(m_index), name);
  77. emit nameChanged();
  78. }
  79. }
  80. void Temp::reset()
  81. {
  82. QIODevice *oldFile = m_valueStream->device();
  83. delete m_valueStream;
  84. delete oldFile;
  85. if (QDir(m_parent->path()).isReadable())
  86. {
  87. QFile *valueFile = new QFile(m_parent->path() + "/temp" + QString::number(m_index) + "_input", this);
  88. if (valueFile->open(QFile::ReadOnly))
  89. {
  90. m_valueStream = new QTextStream(valueFile);
  91. *m_valueStream >> m_value;
  92. m_value /= 1000;
  93. }
  94. else
  95. qCritical() << "Can't open valueFile " << m_parent->path() + "/temp" + QString::number(m_index) + "_input";
  96. }
  97. }
  98. void Temp::update()
  99. {
  100. m_valueStream->seek(0);
  101. *m_valueStream >> m_value;
  102. m_value /= 1000;
  103. emit valueChanged();
  104. }
  105. }