loader.cpp 25 KB

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