temp.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * <one line to give the library's name and an idea of what it does.>
  3. * Copyright 2015 Malte Veerman <malte.veerman@gmail.com>
  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 "hwmon.h"
  24. #include <QtCore/QTextStream>
  25. #include <QtCore/QFile>
  26. #include <QtCore/QDir>
  27. #include <KConfigCore/KSharedConfig>
  28. #include <KConfigCore/KConfigGroup>
  29. #include <KI18n/KLocalizedString>
  30. #define TEST_HWMON_NAME "test"
  31. namespace Fancontrol
  32. {
  33. Temp::Temp(uint index, Hwmon *parent) :
  34. Sensor(parent, index, parent ? parent->name() + QStringLiteral("/temp") + QString::number(index) : QString()),
  35. m_valueStream(new QTextStream)
  36. {
  37. if (parent && QDir(parent->path()).isReadable())
  38. {
  39. const auto valueFile = new QFile(parent->path() + "/temp" + QString::number(index) + "_input", this);
  40. const auto labelFile = new QFile(parent->path() + "/temp" + QString::number(index) + "_label");
  41. if (valueFile->open(QFile::ReadOnly))
  42. {
  43. m_valueStream->setDevice(valueFile);
  44. *m_valueStream >> m_value;
  45. m_value /= 1000;
  46. }
  47. else
  48. {
  49. delete valueFile;
  50. emit error(i18n("Can't open value file: \'%1\'", parent->path() + "/temp" + QString::number(index) + "_input"));
  51. }
  52. if (labelFile->exists())
  53. {
  54. if (labelFile->open(QFile::ReadOnly))
  55. m_label = QTextStream(labelFile).readLine();
  56. else
  57. emit error(i18n("Can't open label file: \'%1\'", parent->path() + "/temp" + QString::number(index) + "_label"));
  58. }
  59. else
  60. emit error(i18n("Temp has no label: \'%1\'", parent->path() + "/temp" + QString::number(index)));
  61. delete labelFile;
  62. }
  63. }
  64. Temp::~Temp()
  65. {
  66. auto device = m_valueStream->device();
  67. delete m_valueStream;
  68. delete device;
  69. }
  70. QString Temp::name() const
  71. {
  72. const auto names = KSharedConfig::openConfig(QStringLiteral("fancontrol-gui"))->group("names");
  73. const auto localNames = names.group(m_parent ? m_parent->name() : QStringLiteral(TEST_HWMON_NAME));
  74. const auto name = localNames.readEntry("temp" + QString::number(m_index), QString());
  75. if (name.isEmpty())
  76. {
  77. if (m_label.isEmpty())
  78. return "temp" + QString::number(m_index);
  79. return m_label;
  80. }
  81. return name;
  82. }
  83. void Temp::setName(const QString &name)
  84. {
  85. const auto names = KSharedConfig::openConfig(QStringLiteral("fancontrol-gui"))->group("names");
  86. auto localNames = names.group(m_parent ? m_parent->name() : QStringLiteral(TEST_HWMON_NAME));
  87. if (name != localNames.readEntry("temp" + QString::number(m_index), QString())
  88. && !name.isEmpty())
  89. {
  90. localNames.writeEntry("temp" + QString::number(m_index), name);
  91. emit nameChanged();
  92. }
  93. }
  94. void Temp::toDefault()
  95. {
  96. if (m_valueStream->device() && m_parent)
  97. {
  98. auto device = m_valueStream->device();
  99. m_valueStream->setDevice(Q_NULLPTR);
  100. delete device;
  101. if (QDir(m_parent->path()).isReadable())
  102. {
  103. const auto valueFile = new QFile(m_parent->path() + "/temp" + QString::number(m_index) + "_input", this);
  104. if (valueFile->open(QFile::ReadOnly))
  105. {
  106. m_valueStream->setDevice(valueFile);
  107. *m_valueStream >> m_value;
  108. m_value /= 1000;
  109. }
  110. else
  111. emit error(i18n("Can't open value file: \'%1\'", m_parent->path() + "/temp" + QString::number(m_index) + "_input"));
  112. }
  113. }
  114. }
  115. void Temp::update()
  116. {
  117. auto success = false;
  118. m_valueStream->seek(0);
  119. const auto value = m_valueStream->readAll().toInt(&success) / 1000;
  120. if (!success)
  121. emit error(i18n("Can't update value of temp: \'%1\'", m_parent->path() + "/temp" + QString::number(m_index)));
  122. if (value != m_value)
  123. {
  124. m_value = value;
  125. emit valueChanged();
  126. }
  127. }
  128. bool Temp::isValid() const
  129. {
  130. return m_valueStream->device() || m_valueStream->string();
  131. }
  132. }