SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. diff --git a/applets/sensorchart/CMakeLists.txt b/applets/sensorchart/CMakeLists.txt
  2. index bdb5dc2b..001387a6 100644
  3. --- a/applets/sensorchart/CMakeLists.txt
  4. +++ b/applets/sensorchart/CMakeLists.txt
  5. @@ -10,12 +10,14 @@ kcoreaddons_desktop_to_json(plasma_applet_ksysguard_sensorchart package/metadata
  6.  target_link_libraries(plasma_applet_ksysguard_sensorchart
  7.                        Qt5::Gui
  8.                        Qt5::Qml
  9. +                      Qt5::DBus
  10.                        KF5::Plasma
  11.                        KF5::Package
  12.                        KF5::I18n
  13.                        KF5::ConfigCore
  14.                        KF5::ConfigGui
  15.                        KF5::Declarative
  16. +                      KF5::SysGuard2
  17.                       )
  18.  
  19.  
  20. diff --git a/applets/sensorchart/package/contents/config/main.xml b/applets/sensorchart/package/contents/config/main.xml
  21. index fbe10d67..13ca0fc8 100644
  22. --- a/applets/sensorchart/package/contents/config/main.xml
  23. +++ b/applets/sensorchart/package/contents/config/main.xml
  24. @@ -20,14 +20,12 @@
  25.  
  26.    <group name="Sensors">
  27.      <entry name="totalSensor" type="String">
  28. -      <default>mem/physical/used</default>
  29.      </entry>
  30.      <entry name="sensorIds" type="StringList">
  31. -      <default>mem/physical/application,mem/physical/buf,mem/physical/cached,mem/physical/free</default>
  32.      </entry>
  33.      <entry name="sensorColors" type="StringList"/>
  34.  
  35. -    <entry name="textOnlySensorsIds" type="StringList"/>
  36. +    <entry name="textOnlySensorIds" type="StringList"/>
  37.    </group>
  38.  
  39.  </kcfg>
  40. diff --git a/applets/sensorchart/package/contents/ui/components/ExtendedLegend.qml b/applets/sensorchart/package/contents/ui/components/ExtendedLegend.qml
  41. index cea59836..4ebc8647 100644
  42. --- a/applets/sensorchart/package/contents/ui/components/ExtendedLegend.qml
  43. +++ b/applets/sensorchart/package/contents/ui/components/ExtendedLegend.qml
  44. @@ -41,7 +41,7 @@ Legend {
  45.      Repeater {
  46.          id: textOnlySesnsorsRepeater
  47.          //FIXME: don't bind to plasmoid here, but make this a top-level property for this component
  48. -        model: plasmoid.configuration.textOnlySensorsIds
  49. +        model: plasmoid.configuration.textOnlySensorIds
  50.          delegate: LegendDelegate {
  51.              sensorName: sensor.name
  52.              color: "transparent"
  53. diff --git a/applets/sensorchart/package/contents/ui/config/ConfigAppearance.qml b/applets/sensorchart/package/contents/ui/config/ConfigAppearance.qml
  54. index 0b5b1aeb..813ae2bf 100644
  55. --- a/applets/sensorchart/package/contents/ui/config/ConfigAppearance.qml
  56. +++ b/applets/sensorchart/package/contents/ui/config/ConfigAppearance.qml
  57. @@ -39,117 +39,16 @@ Kirigami.FormLayout {
  58.      property string cfg_chartFace
  59.  
  60.      // config keys of the selected preset to be applied on save
  61. -    property var pendingPresetConfig
  62. -
  63. -    function matchSensorIdsInModel(model, parent, regExp) {
  64. -        var matches = [];
  65. -
  66. -        var count = model.rowCount(parent);
  67. -        for (var i = 0; i < count; ++i) {
  68. -            var idx = model.index(i, 0, parent);
  69. -            var sensorId = model.data(idx, KSGRD.SensorTreeModel.SensorId);
  70. -
  71. -            if (regExp.test(sensorId)) {
  72. -                matches.push(sensorId);
  73. -            }
  74. -
  75. -            if (model.rowCount(idx) > 0) {
  76. -                Array.prototype.push.apply(matches, matchSensorIdsInModel(model, idx, regExp));
  77. -            }
  78. -        }
  79. -
  80. -        return matches;
  81. -    }
  82. -
  83. -    function expandSensorWildcards(value) {
  84. -        value = value.map(function (item) {
  85. -            if (!item.includes("*")) {
  86. -                return [item];
  87. -            }
  88. -
  89. -            // A RegExp that only allows wildcards * in the source string
  90. -            var regExp = new RegExp("^" + item.split("*").map(function (str) {
  91. -                return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
  92. -            }).join(".*") + "$");
  93. -
  94. -            var matches = [];
  95. -            Array.prototype.push.apply(matches,
  96. -                                       matchSensorIdsInModel(sensorTreeModel,
  97. -                                                             sensorTreeModel.index(-1, -1) /*invalid index (root) */,
  98. -                                                             regExp));
  99. -            return matches;
  100. -        });
  101. -
  102. -        // Array.flat() is ES7 which Qt doesn't support :(
  103. -        value = value.reduce(function (acc, cur) {
  104. -            if (Array.isArray(cur)) {
  105. -                Array.prototype.push.apply(acc, cur);
  106. -            } else {
  107. -                acc.push(cur);
  108. -            }
  109. -            return acc;
  110. -        }, []);
  111. -
  112. -        return value;
  113. -    }
  114. -
  115. -    function applyPresetConfigValue(presetConfig, targetConfig, key) {
  116. -        var value = presetConfig[key];
  117. -
  118. -        switch (typeof targetConfig[key]) {
  119. -        case "undefined":
  120. -            // there is no "hasOwnProperty" on QQmlPropertyMap
  121. -            break;
  122. -        case "boolean":
  123. -            targetConfig[key] = (value === "true");
  124. -            break;
  125. -        case "number":
  126. -            targetConfig[key] = Number(value);
  127. -            break;
  128. -        case "object":
  129. -            // Check if it's an Array. Array.isArray doesn't work but .constructor == Array does...
  130. -            if (targetConfig[key].constructor === Array) {
  131. -                value = value.split(",")
  132. -                             .filter(item => !!item); // "SkipEmptyParts"
  133. -
  134. -                if (key === "sensorIds" || key === "textOnlySensorsIds") {
  135. -                    value = expandSensorWildcards(value);
  136. -                }
  137. -            }
  138. -
  139. -            targetConfig[key] = value;
  140. -            break;
  141. -        // Should we convert "number" and the like explicitly or will that implicitly cast on assignment?
  142. -        default:
  143. -            targetConfig[key] = value;
  144. -            break;
  145. -        }
  146. -    }
  147. +    property string pendingPreset
  148.  
  149.      function saveConfig() {
  150. -        var config = pendingPresetConfig;
  151. -        pendingPresetConfig = undefined;
  152. -        if (config) {
  153. -            // Reset the sensors and colors, too
  154. -            plasmoid.configuration.sensorIds = [];
  155. -            plasmoid.configuration.sensorColors = [];
  156. -            // TODO what about if it doesn't specify rangeAuto and rangeFrom/rangeTo? Initialize with defaults?
  157. -
  158. -            for (var key in config) {
  159. -                var value = config[key];
  160. -
  161. -                applyPresetConfigValue(config, plasmoid.configuration, key);
  162. -                applyPresetConfigValue(config, plasmoid.nativeInterface.faceConfiguration, key);
  163. -            }
  164. +        var preset = pendingPreset;
  165. +        pendingPreset = "";
  166. +        if (preset != "") {
  167. +            plasmoid.nativeInterface.loadPreset(preset);
  168.          }
  169.      }
  170.  
  171. -    // For preset wildcard support
  172. -    // TODO create on demand?
  173. -    KSGRD.SensorTreeModel {
  174. -        id: sensorTreeModel
  175. -    }
  176. -
  177.      Controls.TextField {
  178.          id: titleField
  179.          Kirigami.FormData.label: i18n("Title:")
  180. @@ -210,7 +109,9 @@ Kirigami.FormLayout {
  181.  
  182.                      cfg_title = presetsModel.data(idx, /* DisplayRole */) || "";
  183.  
  184. -                    pendingPresetConfig = presetsModel.data(idx, Qt.UserRole + 2); // FIXME proper enum
  185. +                    var pendingPresetConfig = presetsModel.data(idx, Qt.UserRole + 2); // FIXME proper enum
  186. +                    print(presetsModel.data(idx, Qt.UserRole + 1))
  187. +                    pendingPreset = presetsModel.data(idx, Qt.UserRole + 1); // FIXME proper enum
  188.                      if (pendingPresetConfig.chartFace) {
  189.                          cfg_chartFace = pendingPresetConfig.chartFace;
  190.                      }
  191. diff --git a/applets/sensorchart/package/contents/ui/config/ConfigSensors.qml b/applets/sensorchart/package/contents/ui/config/ConfigSensors.qml
  192. index 80c32a81..b862ba50 100644
  193. --- a/applets/sensorchart/package/contents/ui/config/ConfigSensors.qml
  194. +++ b/applets/sensorchart/package/contents/ui/config/ConfigSensors.qml
  195. @@ -38,7 +38,7 @@ ColumnLayout {
  196.      property alias cfg_sensorIds: usedSensorsView.sensorIds
  197.      property alias cfg_sensorColors: usedSensorsView.sensorColors
  198.  
  199. -    property alias cfg_textOnlySensorsIds: textOnlySensorsView.sensorIds
  200. +    property alias cfg_textOnlySensorIds: textOnlySensorsView.sensorIds
  201.  
  202.      KSGRD.Sensor {
  203.          id: totalSensor
  204. diff --git a/applets/sensorchart/presets/IndividualCoreUsage.sensorchartpresetrc b/applets/sensorchart/presets/IndividualCoreUsage.sensorchartpresetrc
  205. index acc778b4..335fb5cb 100644
  206. --- a/applets/sensorchart/presets/IndividualCoreUsage.sensorchartpresetrc
  207. +++ b/applets/sensorchart/presets/IndividualCoreUsage.sensorchartpresetrc
  208. @@ -3,9 +3,12 @@ Name=Individual Core Usage
  209.  Name[de]=Einzelne Prozessorkernbelastung
  210.  
  211.  [Config]
  212. +chartFace=org.kde.ksysguard.barchart
  213.  sensorIds=cpu/cpu*/TotalLoad
  214.  totalSensor=cpu/system/TotalLoad
  215. -textOnlySensorsIds=cpu/system/loadavg1,cpu/system/loadavg5
  216. +textOnlySensorIds=cpu/system/loadavg1,cpu/system/loadavg5
  217. +
  218. +[FaceConfig]
  219.  rangeAuto=false
  220.  rangeFrom=0
  221.  rangeTo=100
  222. diff --git a/applets/sensorchart/presets/MemoryUsage.sensorchartpresetrc b/applets/sensorchart/presets/MemoryUsage.sensorchartpresetrc
  223. index cb7c4a77..1620322d 100644
  224. --- a/applets/sensorchart/presets/MemoryUsage.sensorchartpresetrc
  225. +++ b/applets/sensorchart/presets/MemoryUsage.sensorchartpresetrc
  226. @@ -3,9 +3,12 @@ Name=Memory Usage
  227.  Name[de]=Speicherverwendung
  228.  
  229.  [Config]
  230. +chartFace=org.kde.ksysguard.piechart
  231.  sensorIds=mem/physical/application,mem/physical/buf,mem/physical/cached,mem/physical/free
  232.  totalSensor=mem/physical/used
  233. -textOnlySensorsIds=
  234. +textOnlySensorIds=
  235. +
  236. +[FaceConfig]
  237.  rangeAuto=true
  238.  rangeFrom=0
  239.  rangeTo=100
  240. diff --git a/applets/sensorchart/presets/ProcessTable.sensorchartpresetrc b/applets/sensorchart/presets/ProcessTable.sensorchartpresetrc
  241. index 8529297d..ebbf0205 100644
  242. --- a/applets/sensorchart/presets/ProcessTable.sensorchartpresetrc
  243. +++ b/applets/sensorchart/presets/ProcessTable.sensorchartpresetrc
  244. @@ -4,11 +4,9 @@ Name[de]=Prozesstabelle
  245.  
  246.  [Config]
  247.  chartFace=org.kde.ksysguard.table
  248. -showTableHeaders=true
  249. -
  250.  sensorIds=process/name,process/pid,process/username,process/usage,process/vmShared,process/command
  251.  totalSensor=
  252. -textOnlySensorsIds=
  253. -#rangeAuto=true
  254. -#rangeFrom=0
  255. -#rangeTo=100
  256. +textOnlySensorIds=
  257. +
  258. +[FaceConfig]
  259. +showTableHeaders=true
  260. diff --git a/applets/sensorchart/presets/TotalCpuUsage.sensorchartpresetrc b/applets/sensorchart/presets/TotalCpuUsage.sensorchartpresetrc
  261. index 9604505a..dcd23d0e 100644
  262. --- a/applets/sensorchart/presets/TotalCpuUsage.sensorchartpresetrc
  263. +++ b/applets/sensorchart/presets/TotalCpuUsage.sensorchartpresetrc
  264. @@ -3,9 +3,12 @@ Name=Total CPU Use
  265.  Name[de]=Gesamte Prozessorauslastung
  266.  
  267.  [Config]
  268. +chartFace=org.kde.ksysguard.piechart
  269.  sensorIds=cpu/system/TotalLoad
  270.  totalSensor=cpu/system/TotalLoad
  271. -textOnlySensorsIds=
  272. +textOnlySensorIds=
  273. +
  274. +[FaceConfig]
  275.  rangeAuto=false
  276.  rangeFrom=0
  277.  rangeTo=100
  278. diff --git a/applets/sensorchart/sensorchart.cpp b/applets/sensorchart/sensorchart.cpp
  279. index ec1f5842..6214a088 100644
  280. --- a/applets/sensorchart/sensorchart.cpp
  281. +++ b/applets/sensorchart/sensorchart.cpp
  282. @@ -24,6 +24,8 @@
  283.  #include <QDebug>
  284.  #include <QStandardPaths>
  285.  
  286. +#include <ksgrd2/SensorManager.h>
  287. +
  288.  #include <KConfigLoader>
  289.  #include <KPackage/PackageLoader>
  290.  #include <KDeclarative/ConfigPropertyMap>
  291. @@ -73,6 +75,17 @@ SensorChart::SensorChart(QObject *parent, const QVariantList &args)
  292.      KPackage::PackageLoader::self()->addKnownPackageStructure("Plasma/SensorApplet", new SensorAppletPackage(this));
  293.  
  294.      m_facePackage = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Plasma/SensorApplet"));
  295. +
  296. +    if (args.count() > 2 && args.mid(3).length() > 0) {
  297. +        const QString preset = args.mid(3).first().toString();
  298. +        if (preset.length() > 0) {
  299. +            loadPreset(preset);
  300. +        } else {
  301. +            loadPreset(QStringLiteral("MemoryUsage"));
  302. +        }
  303. +    } else {
  304. +        loadPreset(QStringLiteral("MemoryUsage"));
  305. +    }
  306.  }
  307.  
  308.  SensorChart::~SensorChart()
  309. @@ -113,14 +126,14 @@ void SensorChart::setFace(const QString &face)
  310.          QFile file(xmlPath);
  311.          KConfigGroup cg = config();
  312.          cg = KConfigGroup(&cg, face);
  313. -        if (m_configuration) {
  314. -            m_configuration->deleteLater();
  315. +        if (m_faceConfiguration) {
  316. +            m_faceConfiguration->deleteLater();
  317.          }
  318. -        if (m_configLoader) {
  319. -            m_configLoader->deleteLater();
  320. +        if (m_faceConfigLoader) {
  321. +            m_faceConfigLoader->deleteLater();
  322.          }
  323. -        m_configLoader = new KConfigLoader(cg, &file, this);
  324. -        m_configuration = new KDeclarative::ConfigPropertyMap(m_configLoader, this);
  325. +        m_faceConfigLoader = new KConfigLoader(cg, &file, this);
  326. +        m_faceConfiguration = new KDeclarative::ConfigPropertyMap(m_faceConfigLoader, this);
  327.      }
  328.      
  329.      emit faceChanged();
  330. @@ -147,6 +160,92 @@ QString SensorChart::faceIcon() const
  331.      return m_faceMetadata->readIcon();
  332.  }
  333.  
  334. +void SensorChart::loadPreset(const QString &preset)
  335. +{
  336. +     qWarning() << "AAAAAALOADING" << preset;
  337. +    const auto presetDirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("ksysguard/sensorchart-presets"), QStandardPaths::LocateDirectory);
  338. +
  339. +    for (const QString &presetDirPath : presetDirs) {
  340. +        const QString path = presetDirPath + QStringLiteral("/") + preset + QStringLiteral(".sensorchartpresetrc");
  341. +        if (!QFile::exists(path)) {
  342. +            continue;
  343. +        }
  344. +
  345. +        KDesktopFile df(path);
  346. +        KConfigGroup configGroup(df.group("Config"));
  347. +
  348. +        // Load the title
  349. +        KConfigSkeletonItem *item = configScheme()->findItemByName(QStringLiteral("title"));
  350. +        if (item) {
  351. +            item->setProperty(df.readName());
  352. +            configScheme()->save();
  353. +            //why read? read will update KConfigSkeletonItem::mLoadedValue,
  354. +            //allowing a write operation to be performed next time
  355. +            configScheme()->read();
  356. +        }
  357. +
  358. +        // Load the global config keys
  359. +        for (const QString &key : configGroup.keyList()) {
  360. +            KConfigSkeletonItem *item = configScheme()->findItemByName(key);
  361. +
  362. +            if (item) {
  363. +                if (item->property().type() == QVariant::StringList) {
  364. +                    // Special case: sensor ids or textOnlySensorIds can have wildchars that need to be expanded
  365. +                    if (key == "sensorIds" || key == "textOnlySensorIds") {
  366. +                        const QStringList partialEntries = configGroup.readEntry(key, QStringList());
  367. +                        QStringList sensors;
  368. +
  369. +                        KSGRD2::SensorManager manager;
  370. +                        const QStringList allSensors = manager.availableSensors().keys();
  371. +                        for (const QString &id : partialEntries) {
  372. +                            if (id.contains(QLatin1Char('*'))) {
  373. +                                QString copy = id;
  374. +                                QRegularExpression regExp(copy.replace(QLatin1Char('*'), QStringLiteral(".+")));
  375. +                                for (const auto &sensor : manager.availableSensors().keys()) {
  376. +                                    if (regExp.match(sensor).hasMatch()) {
  377. +                                        sensors << sensor;
  378. +                                    }
  379. +                                }
  380. +                            } else {
  381. +                                sensors << id;
  382. +                            }
  383. +                        }
  384. +                        item->setProperty(QVariant::fromValue(sensors));
  385. +                    } else {
  386. +                        item->setProperty(QVariant::fromValue(configGroup.readEntry(key, QStringList())));
  387. +                    }
  388. +                } else {
  389. +                    const QString &value = configGroup.readEntry(key);
  390. +                    if (key == QStringLiteral("chartFace") && value.length() > 0) {
  391. +                        setFace(value);
  392. +                    }
  393. +                    qWarning()<<"scroto"<<key<<value;
  394. +                    item->setProperty(value);
  395. +                }
  396. +                configScheme()->save();
  397. +                //why read? read will update KConfigSkeletonItem::mLoadedValue,
  398. +                //allowing a write operation to be performed next time
  399. +                configScheme()->read();
  400. +            }
  401. +        }
  402. +
  403. +        configGroup = KConfigGroup(df.group("FaceConfig"));
  404. +        for (const QString &key : configGroup.keyList()) {
  405. +qWarning()<<"EEE"<<key.toLatin1()<<configGroup.readEntry(key);
  406. +            KConfigSkeletonItem *item = m_faceConfigLoader->findItemByName(key);
  407. +            if (item) {
  408. +                if (item->property().type() == QVariant::StringList) {
  409. +                    item->setProperty(configGroup.readEntry(key, QStringList()));
  410. +                } else {
  411. +                    item->setProperty(configGroup.readEntry(key));
  412. +                }
  413. +                m_faceConfigLoader->save();
  414. +                m_faceConfigLoader->read();
  415. +            }
  416. +        }
  417. +    }
  418. +}
  419. +
  420.  QString SensorChart::compactRepresentationPath() const
  421.  {
  422.      return m_facePackage.filePath("CompactRepresentation");
  423. @@ -242,6 +341,7 @@ QAbstractItemModel *SensorChart::availablePresetsModel()
  424.                  config.insert(key, configGroup.readEntry(key));
  425.              }
  426.  
  427. +            item->setData(presetInfo.baseName(), PresetsModel::PluginIdRole);
  428.              item->setData(config, PresetsModel::ConfigRole);
  429.  
  430.              m_availablePresetsModel->appendRow(item);
  431. @@ -253,7 +353,7 @@ QAbstractItemModel *SensorChart::availablePresetsModel()
  432.  
  433.  QObject *SensorChart::faceConfiguration() const
  434.  {
  435. -    return m_configuration;
  436. +    return m_faceConfiguration;
  437.  }
  438.  
  439.  K_EXPORT_PLASMA_APPLET_WITH_JSON(sensorchart, SensorChart, "metadata.json")
  440. diff --git a/applets/sensorchart/sensorchart.h b/applets/sensorchart/sensorchart.h
  441. index 812eefb6..b80521e9 100644
  442. --- a/applets/sensorchart/sensorchart.h
  443. +++ b/applets/sensorchart/sensorchart.h
  444. @@ -107,6 +107,8 @@ public:
  445.      QString faceName() const;
  446.      QString faceIcon() const;
  447.  
  448. +    Q_INVOKABLE void loadPreset(const QString &preset);
  449. +
  450.      QString compactRepresentationPath() const;
  451.      QString fullRepresentationPath() const;
  452.      QString configPath() const;
  453. @@ -132,7 +134,7 @@ private:
  454.      KDesktopFile *m_faceMetadata = nullptr;
  455.      FacesModel *m_availableFacesModel = nullptr;
  456.      PresetsModel *m_availablePresetsModel = nullptr;
  457. -    KConfigLoader *m_configLoader = nullptr;
  458. -    KDeclarative::ConfigPropertyMap *m_configuration = nullptr;
  459. +    KConfigLoader *m_faceConfigLoader = nullptr;
  460. +    KDeclarative::ConfigPropertyMap *m_faceConfiguration = nullptr;
  461.  };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top