loader.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (C) 2015 Malte Veerman <malte.veerman@gmail.com>
  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. #ifndef LOADER_H
  20. #define LOADER_H
  21. #include <QtCore/QObject>
  22. #include <QtCore/QUrl>
  23. #include <QtCore/QList>
  24. #include <QtCore/QString>
  25. #include <QtCore/QPair>
  26. class QTimer;
  27. class KJob;
  28. class QProcess;
  29. namespace Fancontrol
  30. {
  31. class Hwmon;
  32. class PwmFan;
  33. class Temp;
  34. class Fan;
  35. class GUIBase;
  36. class Loader : public QObject
  37. {
  38. Q_OBJECT
  39. Q_PROPERTY(QUrl configUrl READ configUrl NOTIFY configUrlChanged)
  40. Q_PROPERTY(QString configPath READ configPath NOTIFY configUrlChanged)
  41. Q_PROPERTY(QString config READ config NOTIFY configChanged)
  42. Q_PROPERTY(QList<QObject *> hwmons READ hwmonsAsObjects NOTIFY hwmonsChanged)
  43. Q_PROPERTY(int interval READ interval WRITE setInterval NOTIFY intervalChanged)
  44. Q_PROPERTY(bool sensorsDetected READ sensorsDetected NOTIFY sensorsDetectedChanged)
  45. Q_PROPERTY(bool restartServiceAfterTesting READ restartServiceAfterTesting WRITE setRestartServiceAfterTesting NOTIFY restartServiceAfterTestingChanged)
  46. Q_PROPERTY(bool needsSave READ needsSave NOTIFY needsSaveChanged)
  47. public:
  48. explicit Loader(GUIBase *parent = Q_NULLPTR);
  49. Q_INVOKABLE void parseHwmons();
  50. Q_INVOKABLE bool load(const QUrl & = QUrl());
  51. Q_INVOKABLE bool save(const QUrl & = QUrl());
  52. Q_INVOKABLE void testFans();
  53. Q_INVOKABLE void abortTestingFans();
  54. Q_INVOKABLE void detectSensors();
  55. void load(const QString &config);
  56. QUrl configUrl() const { return m_configUrl; }
  57. QString configPath() const { return m_configUrl.path(); }
  58. QString config() const { return m_config; }
  59. QList<Hwmon *> hwmons() const { return m_hwmons; }
  60. bool sensorsDetected() const { return m_sensorsDetected; }
  61. bool restartServiceAfterTesting() const { return m_reactivateAfterTesting; }
  62. void setRestartServiceAfterTesting(bool restart);
  63. QList<QObject *> hwmonsAsObjects() const;
  64. int interval() const { return m_interval; }
  65. void setInterval(int interval, bool writeNewConfig = true);
  66. PwmFan *pwmFan(QPair<int, int> indexPair) const { return pwmFan(indexPair.first, indexPair.second); }
  67. Temp *temp(QPair<int, int> indexPair) const { return temp(indexPair.first, indexPair.second); }
  68. Fan *fan(QPair<int, int> indexPair) const { return fan(indexPair.first, indexPair.second); }
  69. PwmFan *pwmFan(int hwmonIndex, int pwmFanIndex) const;
  70. Temp *temp(int hwmonIndex, int tempIndex) const;
  71. Fan *fan(int hwmonIndex, int fanIndex) const;
  72. void toDefault();
  73. bool needsSave() const { return m_config != m_configFileContent; }
  74. public slots:
  75. void updateConfig();
  76. void handleDetectSensorsResult(KJob *job);
  77. void handleDetectSensorsResult(int exitCode);
  78. void handleTestStatusChanged();
  79. protected:
  80. bool parseConfig(QString config);
  81. void parseConfigLine(const QString &line, void (PwmFan::*memberSetFunction)(int value));
  82. QPair<int, int> getEntryNumbers(const QString &entry);
  83. QString createConfig() const;
  84. QList<Hwmon *> m_hwmons;
  85. private:
  86. bool m_reactivateAfterTesting;
  87. int m_interval;
  88. QUrl m_configUrl;
  89. QString m_config;
  90. QString m_configFileContent;
  91. QTimer *m_timer;
  92. bool m_sensorsDetected;
  93. signals:
  94. void configUrlChanged();
  95. void configChanged();
  96. void hwmonsChanged();
  97. void intervalChanged();
  98. void error(QString, bool = false);
  99. void info(QString);
  100. void sensorsUpdateNeeded();
  101. void invalidConfigUrl();
  102. void sensorsDetectedChanged();
  103. void restartServiceAfterTestingChanged();
  104. void requestSetServiceActive(bool);
  105. void needsSaveChanged();
  106. };
  107. }
  108. #endif // LOADER_H