loader.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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 "loader.h"
  20. #include "hwmon.h"
  21. #include "fancontrolaction.h"
  22. #include <QtCore/QFile>
  23. #include <QtCore/QDir>
  24. #include <QtCore/QTextStream>
  25. #include <QtCore/QTimer>
  26. #include <QtCore/QProcess>
  27. #include <QtCore/QDebug>
  28. #include <KAuth/KAuthExecuteJob>
  29. #include <KI18n/KLocalizedString>
  30. #define HWMON_PATH "/sys/class/hwmon"
  31. #ifndef STANDARD_CONFIG_FILE
  32. #define STANDARD_CONFIG_FILE "/etc/fancontrol"
  33. #endif
  34. namespace Fancontrol
  35. {
  36. Loader::Loader(QObject *parent) : QObject(parent),
  37. m_reactivateAfterTesting(true),
  38. m_interval(10),
  39. m_configUrl(QUrl::fromLocalFile(QStringLiteral(STANDARD_CONFIG_FILE))),
  40. m_timer(new QTimer(this)),
  41. m_sensorsDetected(false)
  42. {
  43. parseHwmons();
  44. m_timer->setSingleShot(false);
  45. m_timer->start(1000);
  46. connect(m_timer, &QTimer::timeout, this, &Loader::updateSensors);
  47. }
  48. void Loader::parseHwmons()
  49. {
  50. const auto hwmonDir = QDir(QStringLiteral(HWMON_PATH));
  51. QStringList list;
  52. if (hwmonDir.isReadable())
  53. list = hwmonDir.entryList(QDir::AllEntries | QDir::NoDotAndDotDot);
  54. else if (hwmonDir.exists())
  55. {
  56. setError(i18n("%1 is not readable!", QStringLiteral(HWMON_PATH)), true);
  57. return;
  58. }
  59. else
  60. {
  61. setError(i18n("%1 does not exist!", QStringLiteral(HWMON_PATH)), true);
  62. return;
  63. }
  64. QStringList dereferencedList;
  65. while (!list.isEmpty())
  66. dereferencedList << QFile::symLinkTarget(hwmonDir.absoluteFilePath(list.takeFirst()));
  67. foreach (const auto &hwmon, m_hwmons)
  68. {
  69. if (!dereferencedList.contains(hwmon->path()))
  70. {
  71. hwmon->deleteLater();
  72. m_hwmons.removeOne(hwmon);
  73. emit hwmonsChanged();
  74. }
  75. else
  76. hwmon->initialize();
  77. }
  78. foreach (const auto &hwmonPath, dereferencedList)
  79. {
  80. auto hwmonExists = false;
  81. foreach (const auto &hwmon, m_hwmons)
  82. {
  83. if (hwmon->path() == hwmonPath)
  84. {
  85. hwmonExists = true;
  86. break;
  87. }
  88. }
  89. if (!hwmonExists)
  90. {
  91. auto newHwmon = new Hwmon(hwmonPath, this);
  92. if (newHwmon->isValid())
  93. {
  94. connect(this, &Loader::sensorsUpdateNeeded, newHwmon, &Hwmon::updateSensors);
  95. m_hwmons << newHwmon;
  96. emit hwmonsChanged();
  97. }
  98. else
  99. delete newHwmon;
  100. }
  101. }
  102. }
  103. PwmFan * Loader::getPwmFan(const QPair<int, int> &indexPair) const
  104. {
  105. const auto hwmon = m_hwmons.value(indexPair.first, Q_NULLPTR);
  106. if (!hwmon)
  107. return Q_NULLPTR;
  108. return hwmon->pwmFan(indexPair.second);
  109. }
  110. Temp * Loader::getTemp(const QPair<int, int> &indexPair) const
  111. {
  112. const auto hwmon = m_hwmons.value(indexPair.first, Q_NULLPTR);
  113. if (!hwmon)
  114. return Q_NULLPTR;
  115. return hwmon->temp(indexPair.second);
  116. }
  117. QPair<int, int> Loader::getEntryNumbers(const QString &entry)
  118. {
  119. if (entry.isEmpty())
  120. {
  121. qWarning() << "Loader::getHwmonNumber(): given empty string.";
  122. return QPair<int, int>(-1, -1);
  123. }
  124. auto list = entry.split('/', QString::SkipEmptyParts);
  125. if (list.size() != 2)
  126. {
  127. qWarning() << "Invalid entry to parse:" << entry << "Should contain exactly one \'/\'";
  128. return QPair<int, int>(-1, -1);
  129. }
  130. auto &hwmon = list[0];
  131. auto &sensor = list[1];
  132. if (!hwmon.startsWith(QStringLiteral("hwmon")))
  133. {
  134. qWarning() << "Invalid entry to parse:" << entry << "Should begin with \"hwmon\"";
  135. return QPair<int, int>(-1, -1);
  136. }
  137. if (!sensor.contains(QRegExp("^(pwm|fan|temp)\\d+")))
  138. {
  139. qWarning() << "Invalid entry to parse:" << entry << "\n Sensor should begin with pwm|fan|temp followed by a number";
  140. return QPair<int, int>(-1, -1);
  141. }
  142. auto success = false;
  143. hwmon.remove(QStringLiteral("hwmon"));
  144. sensor.remove(QRegExp("^(pwm|fan|temp)"));
  145. sensor.remove(QStringLiteral("_input"));
  146. const auto hwmonResult = hwmon.toInt(&success);
  147. if (!success)
  148. {
  149. qWarning() << "Invalid entry to parse:" << entry << "Could not convert" << hwmon << "to int";
  150. return QPair<int, int>(-1, -1);
  151. }
  152. const auto sensorResult = sensor.toInt(&success);
  153. if (!success)
  154. {
  155. qWarning() << "Invalid entry to parse:" << entry << "Could not convert" << sensor << "to int";
  156. return QPair<int, int>(-1, -1);
  157. }
  158. return QPair<int, int>(hwmonResult, sensorResult - 1);
  159. }
  160. void Loader::parseConfigLine(const QString &line, void (PwmFan::*memberSetFunction)(int)) const
  161. {
  162. if (!memberSetFunction)
  163. {
  164. qWarning() << "Loader::parseConfigLine(): Null for member function pointer";
  165. return;
  166. }
  167. const auto entries = line.split(' ');
  168. foreach (const auto &entry, entries)
  169. {
  170. const auto fanValuePair = entry.split('=');
  171. if (fanValuePair.size() == 2)
  172. {
  173. const auto fanString = fanValuePair.at(0);
  174. const auto valueString = fanValuePair.at(1);
  175. auto success = false;
  176. const auto value = valueString.toInt(&success);
  177. if (success)
  178. {
  179. auto fan = getPwmFan(getEntryNumbers(fanString));
  180. if (fan)
  181. (fan->*memberSetFunction)(value);
  182. }
  183. else
  184. qWarning() << valueString << "is not an int";
  185. }
  186. else
  187. qWarning() << "Invalid Entry:" << entry;
  188. }
  189. }
  190. bool Loader::load(const QUrl &url)
  191. {
  192. QString fileName;
  193. if (url.isEmpty())
  194. {
  195. qDebug() << "Given empty url. Fallback to" << m_configUrl;
  196. fileName = m_configUrl.toLocalFile();
  197. }
  198. else if (url.isValid())
  199. {
  200. if (url.isLocalFile())
  201. fileName = url.toLocalFile();
  202. else
  203. {
  204. setError(i18n("%1 is not a local file!", url.toDisplayString()));
  205. return false;
  206. }
  207. }
  208. else
  209. {
  210. setError(i18n("%1 is not a valid url!", url.toDisplayString()));
  211. return false;
  212. }
  213. QTextStream stream;
  214. QFile file(fileName);
  215. QString fileContent;
  216. if (file.open(QFile::ReadOnly | QFile::Text))
  217. {
  218. if (!url.isEmpty())
  219. {
  220. m_configUrl = url;
  221. emit configUrlChanged();
  222. }
  223. stream.setDevice(&file);
  224. fileContent = stream.readAll();
  225. }
  226. else if (file.exists())
  227. {
  228. auto action = newFancontrolAction();
  229. if (action.isValid())
  230. {
  231. auto map = QVariantMap();
  232. map[QStringLiteral("action")] = QVariant("read");
  233. map[QStringLiteral("filename")] = fileName;
  234. action.setArguments(map);
  235. auto reply = action.execute();
  236. if (!reply->exec())
  237. {
  238. if (reply->error() == 4)
  239. {
  240. qDebug() << "Aborted by user";
  241. return false;
  242. }
  243. qDebug() << "Error while loading:" << reply->error();
  244. setError(reply->errorString() + reply->errorText(), true);
  245. return false;
  246. }
  247. else
  248. {
  249. if (!url.isEmpty())
  250. {
  251. m_configUrl = url;
  252. emit configUrlChanged();
  253. }
  254. fileContent = reply->data().value(QStringLiteral("content")).toString();
  255. }
  256. }
  257. else
  258. setError(i18n("Action not supported! Try running the application as root."), true);
  259. }
  260. else
  261. {
  262. if (!url.isEmpty())
  263. {
  264. emit invalidConfigUrl();
  265. setError(i18n("%1 does not exist!", file.fileName()));
  266. }
  267. return false;
  268. }
  269. //Disconnect hwmons for performance reasons
  270. //They get reconnected later
  271. foreach (const auto &hwmon, m_hwmons)
  272. {
  273. disconnect(hwmon, &Hwmon::configUpdateNeeded, this, &Loader::createConfigFile);
  274. foreach (const auto &pwmFan, hwmon->pwmFans())
  275. {
  276. qobject_cast<PwmFan *>(pwmFan)->reset();
  277. }
  278. }
  279. stream.setString(&fileContent);
  280. auto lines = QStringList();
  281. do
  282. {
  283. auto line(stream.readLine());
  284. if (line.startsWith('#') || line.trimmed().isEmpty())
  285. continue;
  286. const auto offset = line.indexOf('#');
  287. if (offset != -1)
  288. line.truncate(offset);
  289. line = line.simplified();
  290. lines << line;
  291. }
  292. while(!stream.atEnd());
  293. foreach (auto line, lines)
  294. {
  295. if (line.startsWith(QStringLiteral("INTERVAL=")))
  296. {
  297. line.remove(QStringLiteral("INTERVAL="));
  298. auto success = false;
  299. const auto interval = line.toInt(&success);
  300. if (success)
  301. setInterval(interval, false);
  302. else
  303. {
  304. //Connect hwmons again
  305. foreach (const auto &hwmon, m_hwmons)
  306. connect(hwmon, &Hwmon::configUpdateNeeded, this, &Loader::createConfigFile);
  307. setError(i18n("Unable to parse interval line: \n %1", line), true);
  308. return false;
  309. }
  310. }
  311. else if (line.startsWith(QStringLiteral("FCTEMPS=")))
  312. {
  313. line.remove(QStringLiteral("FCTEMPS="));
  314. const auto fctemps = line.split(' ');
  315. foreach (const auto &fctemp, fctemps)
  316. {
  317. const auto nameValuePair = fctemp.split('=');
  318. if (nameValuePair.size() == 2)
  319. {
  320. const auto pwm = nameValuePair.at(0);
  321. const auto temp = nameValuePair.at(1);
  322. const auto pwmPointer = getPwmFan(getEntryNumbers(pwm));
  323. const auto tempPointer = getTemp(getEntryNumbers(temp));
  324. if (pwmPointer && tempPointer)
  325. {
  326. pwmPointer->setTemp(tempPointer);
  327. pwmPointer->setHasTemp(true);
  328. pwmPointer->setMinPwm(0);
  329. }
  330. }
  331. else
  332. qWarning() << "Invalid entry:" << fctemp;
  333. }
  334. }
  335. else if (line.startsWith(QStringLiteral("DEVNAME=")))
  336. {
  337. line.remove(QStringLiteral("DEVNAME="));
  338. const auto devnames = line.split(' ');
  339. foreach (const auto &devname, devnames)
  340. {
  341. const auto indexNamePair = devname.split('=');
  342. if (indexNamePair.size() == 2)
  343. {
  344. auto index = indexNamePair.at(0);
  345. const auto &name = indexNamePair[1];
  346. auto success = false;
  347. index.remove(QStringLiteral("hwmon"));
  348. const auto hwmonPointer = m_hwmons.value(index.toInt(&success), Q_NULLPTR);
  349. if (!success)
  350. {
  351. //Connect hwmons again
  352. foreach (const auto &hwmon, m_hwmons)
  353. connect(hwmon, &Hwmon::configUpdateNeeded, this, &Loader::createConfigFile);
  354. setError(i18n("Can not parse %1", devname), true);
  355. return false;
  356. }
  357. if (!hwmonPointer || hwmonPointer->name().split('.').first() != name)
  358. {
  359. //Connect hwmons again
  360. foreach (const auto &hwmon, m_hwmons)
  361. connect(hwmon, &Hwmon::configUpdateNeeded, this, &Loader::createConfigFile);
  362. setError(i18n("Invalid config file!"), true);
  363. return false;
  364. }
  365. }
  366. }
  367. }
  368. else if (line.startsWith(QStringLiteral("MINTEMP=")))
  369. {
  370. line.remove(QStringLiteral("MINTEMP="));
  371. parseConfigLine(line, &PwmFan::setMinTemp);
  372. }
  373. else if (line.startsWith(QStringLiteral("MAXTEMP=")))
  374. {
  375. line.remove(QStringLiteral("MAXTEMP="));
  376. parseConfigLine(line, &PwmFan::setMaxTemp);
  377. }
  378. else if (line.startsWith(QStringLiteral("MINSTART=")))
  379. {
  380. line.remove(QStringLiteral("MINSTART="));
  381. parseConfigLine(line, &PwmFan::setMinStart);
  382. }
  383. else if (line.startsWith(QStringLiteral("MINSTOP=")))
  384. {
  385. line.remove(QStringLiteral("MINSTOP="));
  386. parseConfigLine(line, &PwmFan::setMinStop);
  387. }
  388. else if (line.startsWith(QStringLiteral("MINPWM=")))
  389. {
  390. line.remove(QStringLiteral("MINPWM="));
  391. parseConfigLine(line, &PwmFan::setMinPwm);
  392. }
  393. else if (line.startsWith(QStringLiteral("MAXPWM=")))
  394. {
  395. line.remove(QStringLiteral("MAXPWM="));
  396. parseConfigLine(line, &PwmFan::setMaxPwm);
  397. }
  398. else if (!line.startsWith(QStringLiteral("DEVPATH=")) &&
  399. !line.startsWith(QStringLiteral("FCFANS=")))
  400. {
  401. //Connect hwmons again
  402. foreach (const auto &hwmon, m_hwmons)
  403. connect(hwmon, &Hwmon::configUpdateNeeded, this, &Loader::createConfigFile);
  404. setError(i18n("Unrecognized line in config:\n%1", line), true);
  405. return false;
  406. }
  407. }
  408. createConfigFile();
  409. //Connect hwmons again
  410. foreach (const auto &hwmon, m_hwmons)
  411. connect(hwmon, &Hwmon::configUpdateNeeded, this, &Loader::createConfigFile);
  412. emit configUrlChanged();
  413. return true;
  414. }
  415. bool Loader::save(const QUrl &url)
  416. {
  417. QString fileName;
  418. if (url.isEmpty())
  419. {
  420. qDebug() << "Given empty url. Fallback to " << m_configUrl;
  421. fileName = m_configUrl.toLocalFile();
  422. }
  423. else if (url.isLocalFile())
  424. fileName = url.toLocalFile();
  425. else
  426. {
  427. setError(i18n("%1 is not a local file!", url.toDisplayString()), true);
  428. return false;
  429. }
  430. QFile file(fileName);
  431. if (file.open(QFile::WriteOnly | QFile::Text))
  432. {
  433. QTextStream stream(&file);
  434. stream << m_configFile;
  435. }
  436. else
  437. {
  438. auto action = newFancontrolAction();
  439. if (action.isValid())
  440. {
  441. QVariantMap map;
  442. map[QStringLiteral("action")] = QVariant("write");
  443. map[QStringLiteral("filename")] = fileName;
  444. map[QStringLiteral("content")] = m_configFile;
  445. action.setArguments(map);
  446. auto *reply = action.execute();
  447. if (!reply->exec())
  448. {
  449. if (reply->error() == 4)
  450. {
  451. qDebug() << "Aborted by user";
  452. return false;
  453. }
  454. qDebug() << "Error while saving:" << reply->error();
  455. setError(reply->errorString() + reply->errorText(), true);
  456. return false;
  457. }
  458. }
  459. else
  460. setError(i18n("Action not supported! Try running the application as root."), true);
  461. }
  462. return true;
  463. }
  464. void Loader::createConfigFile()
  465. {
  466. QList<Hwmon *> usedHwmons;
  467. QList<PwmFan *> usedFans;
  468. foreach (const auto &hwmon, m_hwmons)
  469. {
  470. if (hwmon->pwmFans().size() > 0 && !usedHwmons.contains(hwmon))
  471. usedHwmons << hwmon;
  472. foreach (const auto &fan, hwmon->pwmFans())
  473. {
  474. auto pwmFan = qobject_cast<PwmFan *>(fan);
  475. if (pwmFan->hasTemp() && pwmFan->temp() && !pwmFan->testing())
  476. {
  477. usedFans << pwmFan;
  478. if (!usedHwmons.contains(pwmFan->temp()->parent()))
  479. usedHwmons << pwmFan->temp()->parent();
  480. }
  481. }
  482. }
  483. auto configFile = QStringLiteral("# This file was created by Fancontrol-GUI") + QChar(QChar::LineFeed);
  484. if (m_interval != 0)
  485. configFile += QStringLiteral("INTERVAL=") + QString::number(m_interval) + QChar(QChar::LineFeed);
  486. if (!usedHwmons.isEmpty())
  487. {
  488. configFile += QStringLiteral("DEVPATH=");
  489. foreach (const auto &hwmon, usedHwmons)
  490. {
  491. auto sanitizedPath = hwmon->path();
  492. sanitizedPath.remove(QRegExp("^/sys/"));
  493. sanitizedPath.remove(QRegExp("/hwmon/hwmon\\d\\s*$"));
  494. configFile += QStringLiteral("hwmon") + QString::number(hwmon->index()) + "=" + sanitizedPath + QChar(QChar::Space);
  495. }
  496. configFile += QChar(QChar::LineFeed);
  497. configFile += QStringLiteral("DEVNAME=");
  498. foreach (const auto &hwmon, usedHwmons)
  499. configFile += QStringLiteral("hwmon") + QString::number(hwmon->index()) + "=" + hwmon->name().split('.').first() + QChar(QChar::Space);
  500. configFile += QChar(QChar::LineFeed);
  501. if (!usedFans.isEmpty())
  502. {
  503. configFile += QStringLiteral("FCTEMPS=");
  504. foreach (const auto &pwmFan, usedFans)
  505. {
  506. configFile += QStringLiteral("hwmon") + QString::number(pwmFan->parent()->index()) + "/";
  507. configFile += QStringLiteral("pwm") + QString::number(pwmFan->index()) + "=";
  508. configFile += QStringLiteral("hwmon") + QString::number(pwmFan->temp()->parent()->index()) + "/";
  509. configFile += QStringLiteral("temp") + QString::number(pwmFan->temp()->index()) + QStringLiteral("_input ");
  510. }
  511. configFile += QChar(QChar::LineFeed);
  512. configFile += QStringLiteral("FCFANS=");
  513. foreach (const auto &pwmFan, usedFans)
  514. {
  515. configFile += QStringLiteral("hwmon") + QString::number(pwmFan->parent()->index()) + "/";
  516. configFile += QStringLiteral("pwm") + QString::number(pwmFan->index()) + "=";
  517. configFile += QStringLiteral("hwmon") + QString::number(pwmFan->parent()->index()) + "/";
  518. configFile += QStringLiteral("fan") + QString::number(pwmFan->index()) + QStringLiteral("_input ");
  519. }
  520. configFile += QChar(QChar::LineFeed);
  521. configFile += QStringLiteral("MINTEMP=");
  522. foreach (const auto &pwmFan, usedFans)
  523. {
  524. configFile += QStringLiteral("hwmon") + QString::number(pwmFan->parent()->index()) + "/";
  525. configFile += QStringLiteral("pwm") + QString::number(pwmFan->index()) + "=";
  526. configFile += QString::number(pwmFan->minTemp()) + QChar(QChar::Space);
  527. }
  528. configFile += QChar(QChar::LineFeed);
  529. configFile += QStringLiteral("MAXTEMP=");
  530. foreach (const auto &pwmFan, usedFans)
  531. {
  532. configFile += QStringLiteral("hwmon") + QString::number(pwmFan->parent()->index()) + "/";
  533. configFile += QStringLiteral("pwm") + QString::number(pwmFan->index()) + "=";
  534. configFile += QString::number(pwmFan->maxTemp()) + QChar(QChar::Space);
  535. }
  536. configFile += QChar(QChar::LineFeed);
  537. configFile += QStringLiteral("MINSTART=");
  538. foreach (const auto &pwmFan, usedFans)
  539. {
  540. configFile += QStringLiteral("hwmon") + QString::number(pwmFan->parent()->index()) + "/";
  541. configFile += QStringLiteral("pwm") + QString::number(pwmFan->index()) + "=";
  542. configFile += QString::number(pwmFan->minStart()) + QChar(QChar::Space);
  543. }
  544. configFile += QChar(QChar::LineFeed);
  545. configFile += QStringLiteral("MINSTOP=");
  546. foreach (const auto &pwmFan, usedFans)
  547. {
  548. configFile += QStringLiteral("hwmon") + QString::number(pwmFan->parent()->index()) + "/";
  549. configFile += QStringLiteral("pwm") + QString::number(pwmFan->index()) + "=";
  550. configFile += QString::number(pwmFan->minStop()) + QChar(QChar::Space);
  551. }
  552. configFile += QChar(QChar::LineFeed);
  553. configFile += QStringLiteral("MINPWM=");
  554. foreach (const auto &pwmFan, usedFans)
  555. {
  556. configFile += QStringLiteral("hwmon") + QString::number(pwmFan->parent()->index()) + "/";
  557. configFile += QStringLiteral("pwm") + QString::number(pwmFan->index()) + "=";
  558. configFile += QString::number(pwmFan->minPwm()) + QChar(QChar::Space);
  559. }
  560. configFile += QChar(QChar::LineFeed);
  561. configFile += QStringLiteral("MAXPWM=");
  562. foreach (const auto &pwmFan, usedFans)
  563. {
  564. configFile += QStringLiteral("hwmon") + QString::number(pwmFan->parent()->index()) + "/";
  565. configFile += QStringLiteral("pwm") + QString::number(pwmFan->index()) + "=";
  566. configFile += QString::number(pwmFan->maxPwm()) + QChar(QChar::Space);
  567. }
  568. configFile += QChar(QChar::LineFeed);
  569. }
  570. }
  571. if (configFile != m_configFile)
  572. {
  573. m_configFile = configFile;
  574. emit configFileChanged();
  575. }
  576. }
  577. void Loader::setInterval(int interval, bool writeNewConfig)
  578. {
  579. if (interval != m_interval)
  580. {
  581. m_interval = interval;
  582. emit intervalChanged();
  583. if (writeNewConfig)
  584. createConfigFile();
  585. }
  586. }
  587. void Loader::testFans()
  588. {
  589. foreach (const auto &hwmon, m_hwmons)
  590. hwmon->testFans();
  591. }
  592. void Loader::abortTestingFans()
  593. {
  594. foreach (const auto &hwmon, m_hwmons)
  595. hwmon->abortTestingFans();
  596. }
  597. void Loader::detectSensors()
  598. {
  599. auto program = QStringLiteral("sensors-detect");
  600. auto arguments = QStringList() << QStringLiteral("--auto");
  601. auto process = new QProcess(this);
  602. process->start(program, arguments);
  603. connect(process, static_cast<void(QProcess::*)(int)>(&QProcess::finished),
  604. this, static_cast<void(Loader::*)(int)>(&Loader::handleDetectSensorsResult));
  605. }
  606. void Loader::handleDetectSensorsResult(int exitCode)
  607. {
  608. auto process = qobject_cast<QProcess *>(sender());
  609. if (exitCode)
  610. {
  611. if (process)
  612. setError(process->readAllStandardOutput());
  613. auto action = newFancontrolAction();
  614. if (action.isValid())
  615. {
  616. QVariantMap map;
  617. map[QStringLiteral("action")] = QVariant("detectSensors");
  618. action.setArguments(map);
  619. auto job = action.execute();
  620. connect(job, &KAuth::ExecuteJob::result, this, static_cast<void(Loader::*)(KJob *)>(&Loader::handleDetectSensorsResult));
  621. job->start();
  622. }
  623. else
  624. setError(i18n("Action not supported! Try running the application as root."), true);
  625. }
  626. else
  627. {
  628. if (!m_sensorsDetected)
  629. {
  630. m_sensorsDetected = true;
  631. emit sensorsDetectedChanged();
  632. }
  633. parseHwmons();
  634. }
  635. if (process)
  636. process->deleteLater();
  637. }
  638. void Loader::handleDetectSensorsResult(KJob *job)
  639. {
  640. if (job->error())
  641. {
  642. if (job->error() == 4)
  643. {
  644. qDebug() << "Aborted by user";
  645. return;
  646. }
  647. qDebug() << "Error while detecting sensors:" << job->error();
  648. setError(job->errorString() + job->errorText(), true);
  649. }
  650. else
  651. {
  652. if (!m_sensorsDetected)
  653. {
  654. m_sensorsDetected = true;
  655. emit sensorsDetectedChanged();
  656. }
  657. parseHwmons();
  658. }
  659. }
  660. QList<QObject *> Loader::hwmonsAsObjects() const
  661. {
  662. auto list = QList<QObject *>();
  663. foreach (const auto &hwmon, m_hwmons)
  664. list << qobject_cast<QObject *>(hwmon);
  665. return list;
  666. }
  667. void Loader::setError (const QString &error, bool critical)
  668. {
  669. m_error = error;
  670. emit errorChanged();
  671. if (critical)
  672. {
  673. qCritical() << error;
  674. emit criticalError();
  675. }
  676. else
  677. qWarning() << error;
  678. }
  679. void Loader::handleTestStatusChanged()
  680. {
  681. auto testing = false;
  682. foreach (const auto &hwmon, m_hwmons)
  683. {
  684. if (hwmon->testing() == true)
  685. {
  686. testing = true;
  687. break;
  688. }
  689. }
  690. if (!testing && !m_reactivateAfterTesting)
  691. return;
  692. emit requestSetServiceActive(!testing);
  693. }
  694. void Loader::setRestartServiceAfterTesting(bool restart)
  695. {
  696. if (m_reactivateAfterTesting == restart)
  697. return;
  698. m_reactivateAfterTesting = restart;
  699. emit restartServiceAfterTestingChanged();
  700. }
  701. }