hwmon.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 "hwmon.h"
  20. #include "loader.h"
  21. #include <QtCore/QDir>
  22. #include <QtCore/QTextStream>
  23. namespace Fancontrol
  24. {
  25. Hwmon::Hwmon(const QString &path, Loader *parent) : QObject(parent),
  26. m_parent(parent),
  27. m_path(path),
  28. m_valid(true)
  29. {
  30. QDir dir(path);
  31. if (!dir.isReadable())
  32. {
  33. emit errorChanged(path + " is not readable!");
  34. m_valid = false;
  35. }
  36. bool success;
  37. m_index = path.split('/').last().remove(QStringLiteral("hwmon")).toInt(&success);
  38. if (!success)
  39. {
  40. emit errorChanged(path + "is invalid!");
  41. m_valid = false;
  42. }
  43. QFile nameFile(path + "/name");
  44. if (nameFile.open(QFile::ReadOnly))
  45. m_name = QTextStream(&nameFile).readLine();
  46. else
  47. m_name = path.split('/').last();
  48. connect(this, &Hwmon::configUpdateNeeded, parent, &Loader::createConfigFile);
  49. connect(this, &Hwmon::pwmFansChanged, parent, &Loader::emitAllPwmFansChanged);
  50. connect(this, &Hwmon::tempsChanged, parent, &Loader::emitAllTempsChanged);
  51. connect(this, &Hwmon::errorChanged, parent, &Loader::setError);
  52. if (m_valid)
  53. initialize();
  54. }
  55. void Hwmon::initialize()
  56. {
  57. QDir dir(m_path);
  58. QStringList entrys = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
  59. foreach (const QString &entry, entrys)
  60. {
  61. QString str = entry;
  62. uint index = str.remove(QRegExp("\\D+")).toUInt();
  63. if (entry.contains(QStringLiteral("fan")) && entry.contains(QStringLiteral("input")))
  64. {
  65. if (QFile::exists(m_path + "/pwm" + QString::number(index)))
  66. {
  67. PwmFan *newPwmFan = Q_NULLPTR;
  68. foreach (PwmFan *pwmFan, m_pwmFans)
  69. {
  70. if (pwmFan->index() == index)
  71. {
  72. newPwmFan = pwmFan;
  73. break;
  74. }
  75. }
  76. if (!newPwmFan)
  77. {
  78. newPwmFan = new PwmFan(this, index);
  79. connect(this, &Hwmon::sensorsUpdateNeeded, newPwmFan, &PwmFan::update);
  80. connect(newPwmFan, &PwmFan::testStatusChanged, m_parent, &Loader::handleTestStatusChanged);
  81. m_pwmFans << newPwmFan;
  82. emit pwmFansChanged();
  83. }
  84. Fan *newFan = qobject_cast<Fan *>(newPwmFan);
  85. if (!m_fans.contains(newFan))
  86. {
  87. m_fans << newFan;
  88. emit fansChanged();
  89. }
  90. }
  91. else
  92. {
  93. Fan *newFan = Q_NULLPTR;
  94. foreach (Fan *fan, m_fans)
  95. {
  96. if (fan->index() == index)
  97. {
  98. newFan = fan;
  99. newFan->reset();
  100. break;
  101. }
  102. }
  103. if (!newFan)
  104. {
  105. newFan = new Fan(this, index);
  106. connect(this, &Hwmon::sensorsUpdateNeeded, newFan, &Fan::update);
  107. m_fans << newFan;
  108. emit fansChanged();
  109. }
  110. }
  111. }
  112. if (entry.contains(QStringLiteral("temp")) && entry.contains(QStringLiteral("input")))
  113. {
  114. Temp *newTemp = Q_NULLPTR;
  115. foreach (Temp *temp, m_temps)
  116. {
  117. if (temp->index() == index)
  118. {
  119. newTemp = temp;
  120. newTemp->reset();
  121. break;
  122. }
  123. }
  124. if (!newTemp)
  125. {
  126. newTemp = new Temp(this, index);
  127. connect(this, &Hwmon::sensorsUpdateNeeded, newTemp, &Temp::update);
  128. m_temps << newTemp;
  129. emit tempsChanged();
  130. }
  131. }
  132. }
  133. }
  134. QList<QObject *> Hwmon::fansAsObjects() const
  135. {
  136. QList<QObject *> list;
  137. foreach (Fan *fan, m_fans)
  138. {
  139. list << qobject_cast<QObject *>(fan);
  140. }
  141. return list;
  142. }
  143. QList<QObject *> Hwmon::pwmFansAsObjects() const
  144. {
  145. QList<QObject *> list;
  146. foreach (PwmFan *pwmFan, m_pwmFans)
  147. {
  148. list << qobject_cast<QObject *>(pwmFan);
  149. }
  150. return list;
  151. }
  152. QList<QObject *> Hwmon::tempsAsObjects() const
  153. {
  154. QList<QObject *> list;
  155. foreach (Temp *temp, m_temps)
  156. {
  157. list << qobject_cast<QObject *>(temp);
  158. }
  159. return list;
  160. }
  161. void Hwmon::testFans()
  162. {
  163. foreach (PwmFan *const fan, m_pwmFans)
  164. {
  165. fan->test();
  166. }
  167. }
  168. void Hwmon::abortTestingFans()
  169. {
  170. foreach (PwmFan *const fan, m_pwmFans)
  171. {
  172. fan->abortTest();
  173. }
  174. }
  175. Fan* Hwmon::fan(int i) const
  176. {
  177. return m_fans.value(i, Q_NULLPTR);
  178. }
  179. PwmFan* Hwmon::pwmFan(int i) const
  180. {
  181. return m_pwmFans.value(i, Q_NULLPTR);
  182. }
  183. Temp* Hwmon::temp(int i) const
  184. {
  185. return m_temps.value(i, Q_NULLPTR);
  186. }
  187. void Hwmon::setError(const QString &error)
  188. {
  189. emit errorChanged(error);
  190. }
  191. bool Hwmon::testing() const
  192. {
  193. bool testing = false;
  194. foreach(const PwmFan *fan, m_pwmFans)
  195. {
  196. if (fan->testing())
  197. {
  198. testing = true;
  199. break;
  200. }
  201. }
  202. return testing;
  203. }
  204. }