Add Traditional Chinese desktop entry
[lxde/compton-conf.git] / maindialog.cpp
CommitLineData
012d3267
HJYP
1/*
2 * <one line to give the library's name and an idea of what it does.>
3 * Copyright (C) 2013 <copyright holder> <email>
1f6b91ac 4 *
012d3267
HJYP
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
1f6b91ac 9 *
012d3267
HJYP
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
1f6b91ac 14 *
012d3267
HJYP
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1f6b91ac 18 *
012d3267
HJYP
19 */
20
21#include "maindialog.h"
22#include "ui_maindialog.h"
23#include <QDebug>
c3b7cf80
HJYP
24#include <QDir>
25#include <QFile>
26#include <QFileInfo>
27#include <QDBusInterface>
28#include <QDBusConnection>
b6646b0a
HJYP
29#include <QDialogButtonBox>
30#include <QColorDialog>
aaecf43e 31#include <QMessageBox>
012d3267 32
c3b7cf80 33// dbus interface of compton
b6646b0a 34#define COMPTON_SERVICE_PREFIX "com.github.chjj.compton."
c3b7cf80
HJYP
35#define COMPTON_PATH "/"
36#define COMPTON_INTERFACE "com.github.chjj.compton"
37
38
39MainDialog::MainDialog(QString userConfigFile) {
012d3267
HJYP
40 ui = new Ui::MainDialog;
41 ui->setupUi(this);
42
c3b7cf80
HJYP
43 if(userConfigFile.isEmpty()) {
44 userConfigFile_ = qgetenv("XDG_CONFIG_HOME");
45 if(userConfigFile_.isEmpty()) {
46 userConfigFile_ = QDir::homePath();
47 userConfigFile_ += "/.config";
48 }
49 // QDir configDir = QDir(userConfigFile);
50 // if(!configDir.exists())
51 userConfigFile_ += "/compton.conf";
52 }
53 else
54 userConfigFile_ = userConfigFile;
55
56 config_init(&config_);
57 if(config_read_file(&config_, userConfigFile_.toLocal8Bit().constData()) == CONFIG_FALSE) {
58 // loading user config file failed
59 // try our default example
60 qDebug() << "load fail, try " << COMPTON_CONF_DATA_DIR << "/compton.conf.example";
61 config_read_file(&config_, COMPTON_CONF_DATA_DIR "/compton.conf.example");
62 }
63
64 // set up signal handlers and initial values of the controls
b6646b0a 65 connect(ui->buttonBox, SIGNAL(clicked(QAbstractButton*)), SLOT(onDialogButtonClicked(QAbstractButton*)));
aaecf43e 66 connect(ui->aboutButton, SIGNAL(clicked(bool)), SLOT(onAboutButtonClicked()));
b6646b0a
HJYP
67 connect(ui->shadow_color, SIGNAL(clicked(bool)), SLOT(onColorButtonClicked()));
68 double color;
69 shadowColor_.setRedF(config_lookup_float(&config_, "shadow-red", &color) == CONFIG_TRUE ? color : 0.0);
70 shadowColor_.setGreenF(config_lookup_float(&config_, "shadow-green", &color) == CONFIG_TRUE ? color : 0.0);
71 shadowColor_.setBlueF(config_lookup_float(&config_, "shadow-blue", &color) == CONFIG_TRUE ? color : 0.0);
72 updateShadowColorButton();
73
c3b7cf80 74 // objectNames are kept the same as config file key names.
c8feddb4
LP
75 const QList<QWidget*> widgets = findChildren<QWidget*>();
76 QList<QWidget*>::const_iterator i;
77 for(i = widgets.constBegin(); i != widgets.constEnd(); ++i) {
78 QWidget *child = *i;
012d3267
HJYP
79 if(!child->isWidgetType() || child->objectName().isEmpty())
80 continue;
c3b7cf80
HJYP
81 // objectName uses _ while config file keys uses - as separator.
82 QByteArray keyName = child->objectName().replace('_', '-').toLatin1(); // generate config key from objectName.
012d3267 83 if(child->inherits("QCheckBox")) {
c3b7cf80
HJYP
84 int val = -1;
85 if(config_lookup_bool(&config_, keyName.constData(), &val) == CONFIG_TRUE)
1f6b91ac 86 static_cast<QCheckBox*>(child)->setChecked((bool)val);
012d3267
HJYP
87 connect(child, SIGNAL(toggled(bool)), SLOT(onButtonToggled(bool)));
88 }
89 else if(child->inherits("QDoubleSpinBox")) {
90 double val;
c3b7cf80 91 if(config_lookup_float(&config_, keyName.constData(), &val) == CONFIG_TRUE)
1f6b91ac 92 static_cast<QDoubleSpinBox*>(child)->setValue(val);
012d3267
HJYP
93 connect(child, SIGNAL(valueChanged(double)), SLOT(onSpinValueChanged(double)));
94 }
95 else if(child->inherits("QSpinBox")) {
96 int val;
c3b7cf80 97 if(config_lookup_int(&config_, keyName.constData(), &val) == CONFIG_TRUE)
1f6b91ac 98 static_cast<QSpinBox*>(child)->setValue(val);
012d3267
HJYP
99 connect(child, SIGNAL(valueChanged(int)), SLOT(onSpinValueChanged(int)));
100 }
96ff239b 101 else if(child->inherits("QRadioButton")) {
102 if(child->parent()->inherits("QGroupBox")) {
103 QByteArray groupKeyName = child->parent()->objectName().replace('_', '-').toLatin1();
104 if(keyName.startsWith(groupKeyName)) {
105 const char *val;
106 if(config_lookup_string(&config_, groupKeyName.constData(), &val) == CONFIG_TRUE)
107 static_cast<QRadioButton*>(child)->setChecked(keyName == groupKeyName.append('-').append(val));
108 connect(child, SIGNAL(toggled(bool)), SLOT(onRadioGroupToggled(bool)));
109 continue;
110 }
111 }
112 int val = -1;
113 if(config_lookup_bool(&config_, keyName.constData(), &val) == CONFIG_TRUE)
114 static_cast<QRadioButton*>(child)->setChecked((bool)val);
115 connect(child, SIGNAL(toggled(bool)), SLOT(onButtonToggled(bool)));
116 }
012d3267
HJYP
117 }
118}
119
120MainDialog::~MainDialog() {
c3b7cf80 121 config_destroy(&config_);
012d3267
HJYP
122 delete ui;
123}
124
125void MainDialog::onButtonToggled(bool checked) {
126 qDebug() << "toggled: " << sender()->objectName();
c3b7cf80
HJYP
127 // generate config key from objectName.
128 QByteArray keyName = sender()->objectName().replace('_', '-').toLatin1();
b6646b0a
HJYP
129 configSetBool(keyName.constData(), checked);
130 // saveConfig();
012d3267
HJYP
131}
132
133void MainDialog::onSpinValueChanged(double d) {
134 qDebug() << "changed: " << sender()->objectName() << ": " << d;
c3b7cf80
HJYP
135 // generate config key from objectName.
136 QByteArray keyName = sender()->objectName().replace('_', '-').toLatin1();
b6646b0a
HJYP
137 configSetFloat(keyName.constData(), d);
138 // saveConfig();
012d3267
HJYP
139}
140
141void MainDialog::onSpinValueChanged(int i) {
142 qDebug() << "changed: " << sender()->objectName() << ": " << i;
c3b7cf80
HJYP
143 // generate config key from objectName.
144 QByteArray keyName = sender()->objectName().replace('_', '-').toLatin1();
b6646b0a
HJYP
145 configSetInt(keyName.constData(), i);
146 // saveConfig();
c3b7cf80
HJYP
147}
148
96ff239b 149void MainDialog::onRadioGroupToggled(bool checked) {
150 if (checked) {
151 qDebug() << "toggled: " << sender()->objectName();
152 // generate config key from objectName.
153 QByteArray keyName = sender()->parent()->objectName().replace('_', '-').toLatin1();
154 QByteArray val = sender()->objectName().right(sender()->objectName().size() - (keyName.size() + 1)).replace('_', '-').toLatin1();
155 configSetString(keyName.constData(), val);
156 // saveConfig();
157 }
158}
159
c3b7cf80
HJYP
160void MainDialog::saveConfig() {
161 // ensure the existance of user config dir
162 QString configDir = QFileInfo(userConfigFile_).dir().path();
163 QDir().mkpath(configDir);
164 qDebug() << userConfigFile_;
165 // save the config file
166 config_write_file(&config_, userConfigFile_.toLocal8Bit().constData());
1f6b91ac 167
c3b7cf80 168 // ask compton to reload the config
b6646b0a
HJYP
169 QString displayName = qgetenv("DISPLAY");
170 for(int i = 0; i < displayName.length(); ++i) {
171 if(!displayName[i].isNumber()) // replace non-numeric chars with _
172 displayName[i] = '_';
173 }
174 QString comptonServiceName = COMPTON_SERVICE_PREFIX + displayName;
175 QDBusInterface iface(comptonServiceName, COMPTON_PATH, COMPTON_INTERFACE);
176 if(iface.isValid()) {
c3b7cf80 177 iface.call("reset");
d12a19f0
HJYP
178 // raise ourself to the top again (we'll loosing focus after reloading compton)
179 activateWindow();
b6646b0a 180 }
c3b7cf80
HJYP
181 // FIXME: dbus interface of compton is not always available and reset() creates
182 // much flickers. Maybe we should use internal dbus method set_opts().
183 // Or, we can patch compton to do what we want.
184}
185
186void MainDialog::done(int res) {
187 QDialog::done(res);
012d3267 188}
b6646b0a
HJYP
189
190void MainDialog::onDialogButtonClicked(QAbstractButton* button) {
191 if(ui->buttonBox->buttonRole(button) == QDialogButtonBox::ApplyRole) {
192 saveConfig();
193 }
194}
195
196void MainDialog::onColorButtonClicked() {
197 QColorDialog dlg(shadowColor_);
198 dlg.setOption(QColorDialog::ShowAlphaChannel, false);
199 if(dlg.exec() == QDialog::Accepted) {
200 shadowColor_ = dlg.selectedColor();
201 updateShadowColorButton();
202 configSetFloat("shadow-red", shadowColor_.redF());
203 configSetFloat("shadow-green", shadowColor_.greenF());
204 configSetFloat("shadow-blue", shadowColor_.blueF());
205 }
206}
207
aaecf43e
HJYP
208void MainDialog::onAboutButtonClicked() {
209 QMessageBox::about(this, tr("About ComptonConf"),
210 tr("ComptonConf - configuration tool for compton\n\nCopyright (C) 2013\nAuthor: Hong Jen Yee (PCMan) <pcman.tw@gmail.com>"));
211}
212
b6646b0a
HJYP
213void MainDialog::updateShadowColorButton() {
214 QString qss = QString("QPushButton {"
215 "background-color:%1;"
216 "}").arg(shadowColor_.name());
217 ui->shadow_color->setStyleSheet(qss);
218}
219
220void MainDialog::configSetInt(const char* key, int val) {
221 config_setting_t* setting = config_lookup(&config_, key);
222 if(!setting) { // setting not found
223 // add a new setting for it
224 config_setting_t* root = config_root_setting(&config_);
225 setting = config_setting_add(root, key, CONFIG_TYPE_INT);
226 }
227 config_setting_set_int(setting, val);
228}
229
230void MainDialog::configSetFloat(const char* key, double val) {
231 config_setting_t* setting = config_lookup(&config_, key);
232 if(!setting) { // setting not found
233 // add a new setting for it
234 config_setting_t* root = config_root_setting(&config_);
235 setting = config_setting_add(root, key, CONFIG_TYPE_FLOAT);
236 }
237 config_setting_set_float(setting, val);
238}
239
240void MainDialog::configSetBool(const char* key, bool val) {
241 config_setting_t* setting = config_lookup(&config_, key);
242 if(!setting) { // setting not found
243 // add a new setting for it
244 config_setting_t* root = config_root_setting(&config_);
245 setting = config_setting_add(root, key, CONFIG_TYPE_BOOL);
246 }
247 config_setting_set_bool(setting, val);
248}
96ff239b 249
250void MainDialog::configSetString(const char *key, const char *val)
251{
252 config_setting_t* setting = config_lookup(&config_, key);
253 if(!setting) { // setting not found
254 // add a new setting for it
255 config_setting_t* root = config_root_setting(&config_);
256 setting = config_setting_add(root, key, CONFIG_TYPE_STRING);
257 }
258 config_setting_set_string(setting, val);
259}