Debian Codegraph @

lxqt-config-monitor/savesettings.cpp

raw
1 /*
2 Copyright (C) 2015 P.L. Lucas <[email protected]>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU 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 General Public License for more details.
13
14 You should have received a copy of the GNU 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 <QJsonArray>
20 #include <QJsonObject>
21 #include "savesettings.h"
22 #include "configure.h"
23 #include <QDebug>
24 #include <QJsonDocument>
25 #include <QProcess>
26 #include <QInputDialog>
27
28 SaveSettings::SaveSettings(LXQt::Settings*applicationSettings, QWidget* parent):
29 QDialog(parent)
30 {
31
32 this->applicationSettings = applicationSettings;
33
34 ui.setupUi(this);
35
36 QSize size(128, 64);
37 ui.save->setIcon(QIcon::fromTheme("document-save"));
38 ui.save->setIconSize(size);
39
40 connect(ui.hardwareCompatibleConfigs, &QListWidget::itemDoubleClicked, this, &SaveSettings::setSavedSettings);
41 connect(ui.deletePushButton, &QPushButton::clicked, this, &SaveSettings::onDeleteItem);
42 connect(ui.renamePushButton, &QPushButton::clicked, this, &SaveSettings::onRenameItem);
43
44 loadSettings();
45 }
46
47 void SaveSettings::setHardwareIdentifier(QString hardwareIdentifier)
48 {
49 this->hardwareIdentifier = hardwareIdentifier;
50 loadSettings();
51 }
52
53 void SaveSettings::setSavedSettings(QListWidgetItem * item)
54 {
55 QJsonObject o = item->data(Qt::UserRole).toJsonObject();
56 QString cmd = o["command"].toString();
57 qDebug() << "[SaveSettings::setSavedSettings]: " << cmd;
58 QProcess::execute(cmd);
59 }
60
61 void SaveSettings::onDeleteItem()
62 {
63 if( ui.allConfigs->currentItem() == NULL )
64 return;
65 QJsonObject obj = ui.allConfigs->currentItem()->data(Qt::UserRole).toJsonObject();
66 applicationSettings->beginGroup("configMonitor");
67 QJsonArray savedConfigs = QJsonDocument::fromJson(applicationSettings->value("saved").toByteArray()).array();
68 for(int i=0; i<savedConfigs.size(); i++) {
69 const QJsonValue & v = savedConfigs[i];
70 QJsonObject o = v.toObject();
71 if( o["name"].toString() == obj["name"].toString() ) {
72 savedConfigs.removeAt(i);
73 break;
74 }
75 }
76 applicationSettings->setValue("saved", QVariant(QJsonDocument(savedConfigs).toJson()));
77 applicationSettings->endGroup();
78 loadSettings();
79 }
80
81 void SaveSettings::onRenameItem()
82 {
83 if( ui.allConfigs->currentItem() == NULL )
84 return;
85 QJsonObject obj = ui.allConfigs->currentItem()->data(Qt::UserRole).toJsonObject();
86 bool ok;
87 QString configName = QInputDialog::getText(this, tr("Name"), tr("Name:"), QLineEdit::Normal, obj["name"].toString(), &ok);
88 if (!ok || configName.isEmpty())
89 return;
90 applicationSettings->beginGroup("configMonitor");
91 QJsonArray savedConfigs = QJsonDocument::fromJson(applicationSettings->value("saved").toByteArray()).array();
92 for(int i=0; i<savedConfigs.size(); i++) {
93 const QJsonValue & v = savedConfigs[i];
94 QJsonObject o = v.toObject();
95 if( o["name"].toString() == obj["name"].toString() ) {
96 savedConfigs.removeAt(i);
97 obj["name"] = configName;
98 savedConfigs.append(obj);
99 break;
100 }
101 }
102 applicationSettings->setValue("saved", QVariant(QJsonDocument(savedConfigs).toJson()));
103 applicationSettings->endGroup();
104 loadSettings();
105 }
106
107 void SaveSettings::loadSettings()
108 {
109 ui.allConfigs->clear();
110 ui.hardwareCompatibleConfigs->clear();
111 applicationSettings->beginGroup("configMonitor");
112 QJsonArray savedConfigs = QJsonDocument::fromJson(applicationSettings->value("saved").toByteArray()).array();
113 foreach (const QJsonValue & v, savedConfigs) {
114 QJsonObject o = v.toObject();
115 QListWidgetItem *item = new QListWidgetItem(o["name"].toString(), ui.allConfigs);
116 item->setData(Qt::UserRole, QVariant(o));
117 if(o["hardwareIdentifier"].toString() == hardwareIdentifier) {
118 QListWidgetItem *item = new QListWidgetItem(o["name"].toString(), ui.hardwareCompatibleConfigs);
119 item->setData(Qt::UserRole, QVariant(o));
120 }
121 }
122 applicationSettings->endGroup();
123 }