Add new icons from GNOME, KDE Oxygen and Elementary themes.
[dhcpcd-ui] / src / dhcpcd-qt / dhcpcd-preferences.cpp
1 /*
2  * dhcpcd-qt
3  * Copyright 2014 Roy Marples <roy@marples.name>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <QDialog>
28 #include <QCheckBox>
29 #include <QComboBox>
30 #include <QFormLayout>
31 #include <QFrame>
32 #include <QGridLayout>
33 #include <QHBoxLayout>
34 #include <QIcon>
35 #include <QLabel>
36 #include <QLineEdit>
37 #include <QMessageBox>
38 #include <QPixmap>
39 #include <QPushButton>
40 #include <QStandardItemModel>
41 #include <QVBoxLayout>
42
43 #include <cerrno>
44
45 #include "config.h"
46 #include "dhcpcd-preferences.h"
47 #include "dhcpcd-ipv4validator.h"
48 #include "dhcpcd-qt.h"
49 #include "dhcpcd-wi.h"
50
51 DhcpcdPreferences::DhcpcdPreferences(DhcpcdQt *parent)
52     : QDialog(parent)
53 {
54         this->parent = parent;
55         resize(400, 200);
56         setWindowIcon(DhcpcdQt::getIcon("status", "network-transmit-receive"));
57         setWindowTitle(tr("Network Preferences"));
58
59         name = NULL;
60         config = NULL;
61         eWhat = eBlock = NULL;
62         iface = NULL;
63
64         QVBoxLayout *layout = new QVBoxLayout();
65
66         QGridLayout *topLayout = new QGridLayout();
67         QLabel *conf = new QLabel(tr("Configure:"));
68         topLayout->addWidget(conf, 0, 0);
69
70         what = new QComboBox();
71         connect(what, SIGNAL(currentIndexChanged(const QString &)),
72             this, SLOT(listBlocks(const QString &)));
73         topLayout->addWidget(what, 0, 1);
74         blocks = new QComboBox();
75         connect(blocks, SIGNAL(currentIndexChanged(const QString &)),
76             this, SLOT(showBlock(const QString &)));
77         topLayout->addWidget(blocks, 0, 2);
78
79         topLayout->setColumnStretch(2, 10);
80
81         QWidget *topBox = new QWidget();
82         topBox->setLayout(topLayout);
83         layout->addWidget(topBox);
84         QFrame *topSep = new QFrame();
85         topSep->setFrameShape(QFrame::HLine);
86         topSep->setFrameShadow(QFrame::Sunken);
87         layout->addWidget(topSep);
88
89         autoConf = new QCheckBox(tr("Automatically configure empty options"));
90         autoConf->setChecked(true);
91         layout->addWidget(autoConf);
92
93         DhcpcdIPv4Validator *v =
94             new DhcpcdIPv4Validator(DhcpcdIPv4Validator::Plain, this);
95         DhcpcdIPv4Validator *vc =
96             new DhcpcdIPv4Validator(DhcpcdIPv4Validator::CIDR, this);
97         DhcpcdIPv4Validator *vs =
98             new DhcpcdIPv4Validator(DhcpcdIPv4Validator::Spaced, this);
99         ip = new QLineEdit();
100         ip->setValidator(vc);
101         router = new QLineEdit();
102         router->setValidator(v);
103         rdnss = new QLineEdit();
104         rdnss->setValidator(vs);
105         dnssl = new QLineEdit();
106 #if defined(__NetBSD__) || (__OpenBSD__)
107         dnssl->setMaxLength(1024);
108 #else
109         dnssl->setMaxLength(256);
110 #endif
111         QFormLayout *ipLayout = new QFormLayout();
112         ipLayout->addRow(tr("IP Address:"), ip);
113         ipLayout->addRow(tr("Router:"), router);
114         ipLayout->addRow(tr("DNS Servers:"), rdnss);
115         ipLayout->addRow(tr("DNS Search:"), dnssl);
116         ipSetup = new QWidget();
117         ipSetup->setLayout(ipLayout);
118         layout->addWidget(ipSetup);
119
120         QHBoxLayout *buttonLayout = new QHBoxLayout();
121         clear = new QPushButton(tr("&Clear"));
122         clear->setIcon(QIcon::fromTheme("edit-clear"));
123         buttonLayout->addWidget(clear);
124         QPushButton *rebind = new QPushButton(tr("&Rebind"));
125         rebind->setIcon(QIcon::fromTheme("application-x-executable"));
126         buttonLayout->addWidget(rebind);
127         QPushButton *close = new QPushButton(tr("&Close"));
128         close->setIcon(QIcon::fromTheme("window-close"));
129         buttonLayout->addWidget(close);
130         QWidget *buttons = new QWidget();
131         buttons->setLayout(buttonLayout);
132         layout->addWidget(buttons);
133
134         QIcon wired = DhcpcdQt::getIcon("devices", "network-wired");
135         what->addItem(wired, tr("interface"));
136         QIcon wireless = DhcpcdQt::getIcon("devices", "network-wireless");
137         what->addItem(wireless, tr("SSID"));
138
139         connect(clear, SIGNAL(clicked()), this, SLOT(clearConfig()));
140         connect(rebind, SIGNAL(clicked()), this, SLOT(rebind()));
141         connect(close, SIGNAL(clicked()), this, SLOT(tryClose()));
142
143         setLayout(layout);
144
145         autoConf->setEnabled(false);
146         ipSetup->setEnabled(false);
147         clear->setEnabled(false);
148
149         DHCPCD_CONNECTION *con = parent->getConnection();
150         if (!dhcpcd_config_writeable(con))
151                 QMessageBox::warning(this, tr("Not writeable"),
152                     tr("The dhcpcd configuration file is not writeable\n\n%1")
153                     .arg(dhcpcd_cffile(con)));
154 }
155
156 DhcpcdPreferences::~DhcpcdPreferences()
157 {
158
159         free(eWhat);
160         eWhat = NULL;
161         free(eBlock);
162         eBlock = NULL;
163 }
164
165 void DhcpcdPreferences::closeEvent(QCloseEvent *e)
166 {
167
168         parent->dialogClosed(this);
169         QDialog::closeEvent(e);
170 }
171
172 void DhcpcdPreferences::listBlocks(const QString &txt)
173 {
174
175         /* clear and then disconnect so we trigger a save */
176         blocks->clear();
177         blocks->disconnect(this);
178
179         free(eWhat);
180         eWhat = strdup(txt.toLower().toAscii());
181
182         if (txt == "interface") {
183                 DHCPCD_IF *i;
184
185                 blocks->addItem(tr("Select an interface"));
186                 for (i = dhcpcd_interfaces(parent->getConnection());
187                     i; i = i->next)
188                 {
189                         if (strcmp(i->type, "link") == 0) {
190                                 QIcon icon = DhcpcdQt::getIcon("devices",
191                                     i->wireless ?
192                                     "network-wireless" : "network-wired");
193                                 blocks->addItem(icon, i->ifname);
194                         }
195                 }
196         } else {
197                 QList<DhcpcdWi *> *wis = parent->getWis();
198
199                 blocks->addItem(tr("Select a SSID"));
200                 for (int i = 0; i < wis->size(); i++) {
201                         DHCPCD_WI_SCAN *scan;
202                         DhcpcdWi *wi = wis->at(i);
203
204                         for (scan = wi->getScans(); scan; scan = scan->next) {
205                                 QIcon icon;
206
207                                 icon = DhcpcdQt::getIcon(
208                                     scan->flags[0] == '\0' ?"devices" :"status",
209                                     scan->flags[0] == '\0' ?"network-wireless" :
210                                         "network-wireless-encrypted");
211                                 blocks->addItem(icon, scan->ssid);
212                         }
213                 }
214         }
215
216         /* Now make the 1st item unselectable and reconnect */
217         qobject_cast<QStandardItemModel *>
218             (blocks->model())->item(0)->setEnabled(false);
219         connect(blocks, SIGNAL(currentIndexChanged(const QString &)),
220             this, SLOT(showBlock(const QString &)));
221
222 }
223
224 void DhcpcdPreferences::clearConfig()
225 {
226
227         autoConf->setChecked(true);
228         ip->setText("");
229         router->setText("");
230         rdnss->setText("");
231         dnssl->setText("");
232 }
233
234 void DhcpcdPreferences::showConfig()
235 {
236         const char *val;
237         bool a;
238
239         if ((val = dhcpcd_config_get_static(config, "ip_address=")) != NULL)
240                 a = false;
241         else
242                 a = !((val = dhcpcd_config_get(config, "inform")) == NULL &&
243                     (iface && iface->flags & IFF_POINTOPOINT));
244         autoConf->setChecked(a);
245         ip->setText(val);
246         router->setText(dhcpcd_config_get_static(config, "routers="));
247         rdnss->setText(dhcpcd_config_get_static(config,"domain_name_servers="));
248         dnssl->setText(dhcpcd_config_get_static(config, "domain_search="));
249 }
250
251 bool DhcpcdPreferences::changedConfig()
252 {
253         const char *val;
254         bool a;
255
256         if ((val = dhcpcd_config_get_static(config, "ip_address=")) != NULL)
257                 a = false;
258         else
259                 a = !((val = dhcpcd_config_get(config, "inform")) == NULL &&
260                     (iface && iface->flags & IFF_POINTOPOINT));
261         if (autoConf->isChecked() != a)
262                 return true;
263         if (ip->text().compare(val))
264                 return true;
265         val = dhcpcd_config_get_static(config, "routers=");
266         if (router->text().compare(val))
267                 return true;
268         val = dhcpcd_config_get_static(config, "domain_name_servers=");
269         if (rdnss->text().compare(val))
270                 return true;
271         val = dhcpcd_config_get_static(config, "domain_search=");
272         if (rdnss->text().compare(val))
273                 return true;
274         return false;
275 }
276
277
278 const char *DhcpcdPreferences::getString(QLineEdit *le)
279 {
280         if (le->text().isEmpty())
281                 return NULL;
282         return le->text().trimmed().toAscii();
283 }
284
285 bool DhcpcdPreferences::setOption(const char *opt, const char *val, bool *ret)
286 {
287         if (opt[strlen(opt) - 1] == '=') {
288                 if (!dhcpcd_config_set_static(&config, opt, val))
289                         qCritical("dhcpcd_config_set_static: %s",
290                             strerror(errno));
291                 else
292                         return true;
293         } else {
294                 if (!dhcpcd_config_set(&config, opt, val))
295                         qCritical("dhcpcd_config_set: %s",
296                             strerror(errno));
297                 else
298                         return true;
299         }
300
301         if (ret)
302                 *ret = false;
303         return false;
304 }
305
306
307 bool DhcpcdPreferences::makeConfig()
308 {
309         const char ns[] = "", *val;
310         bool a, ret;
311
312         a = autoConf->isChecked();
313         ret = true;
314         if (iface && iface->flags & IFF_POINTOPOINT)
315                 setOption("ip_address=", a ? NULL : ns, &ret);
316         else {
317                 val = getString(ip);
318                 setOption("inform", a ? val : NULL, &ret);
319                 setOption("ip_address=", a ? NULL : val, &ret);
320         }
321
322         val = getString(router);
323         setOption("routers=", val, &ret);
324
325         val = getString(rdnss);
326         setOption("domain_name_servers=", val, &ret);
327
328         val = getString(dnssl);
329         setOption("domain_search=", val, &ret);
330
331         return ret;
332 }
333
334 bool DhcpcdPreferences::writeConfig(bool *cancel)
335 {
336
337         switch (QMessageBox::question(this, tr("Save Configuration"),
338             tr("Do you want to save your changes?"),
339             QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel)) {
340         case QMessageBox::Cancel:
341                 *cancel = true;
342                 return false;
343         case QMessageBox::Discard:
344                 return true;
345         default:
346                 break;
347         }
348         *cancel = false;
349
350         DHCPCD_CONNECTION *con = parent->getConnection();
351         if (!makeConfig()) {
352                 qCritical("failed to make config");
353                 goto err;
354         }
355         if (!dhcpcd_config_write(con, eWhat, eBlock, config)) {
356                 qCritical("dhcpcd_config_write: %s", strerror(errno));
357                 QMessageBox::critical(parent,
358                     tr("Failed to write configuration"),
359                     tr("Failed to write configuration:\n\n%1: %2")
360                     .arg(dhcpcd_cffile(con))
361                     .arg(strerror(errno)));
362                 goto err;
363         }
364         return true;
365
366 err:
367         /* Reload our config if there is a problem */
368         config = dhcpcd_config_read(con, eWhat, eBlock);
369         return false;
370 }
371
372 void DhcpcdPreferences::showBlock(const QString &txt)
373 {
374
375         if (eBlock) {
376                 if (changedConfig()) {
377                         bool cancel;
378                         if (!writeConfig(&cancel))
379                                 return;
380                 }
381                 free(eBlock);
382         }
383         if (txt.isEmpty())
384                 eBlock = NULL;
385         else
386                 eBlock = strdup(txt.toAscii());
387
388         dhcpcd_config_free(config);
389         iface = NULL;
390         DHCPCD_CONNECTION *con = parent->getConnection();
391
392         if (eBlock && eWhat) {
393                 if (strcmp(eWhat, "interface") == 0)
394                         iface = dhcpcd_get_if(con, eBlock, "link");
395                 ip->setEnabled(iface == NULL ||
396                     !(iface->flags & IFF_POINTOPOINT));
397                 errno = 0;
398                 config = dhcpcd_config_read(con, eWhat, eBlock);
399                 if (config == NULL && errno) {
400                         const char *s;
401
402                         s = strerror(errno);
403                         qCritical("dhcpcd_config_read: %s", s);
404                         QMessageBox::critical(this,
405                             tr("Error reading configuration"),
406                             tr("Error reading: ") + dhcpcd_cffile(con) +
407                             "\n\n" + s);
408                 }
409         } else
410                 config = NULL;
411
412         showConfig();
413         bool enabled = dhcpcd_config_writeable(con) && eBlock != NULL;
414         autoConf->setEnabled(enabled);
415         ipSetup->setEnabled(enabled);
416         clear->setEnabled(enabled);
417 }
418
419 bool DhcpcdPreferences::tryRebind(const char *ifname)
420 {
421
422         if (dhcpcd_rebind(parent->getConnection(), ifname) == 0)
423                 return true;
424
425         qCritical("dhcpcd_rebind: %s", strerror(errno));
426         QMessageBox::critical(this,
427             tr("Rebind failed"),
428             ifname ? tr("Failed to rebind interface %1: %2")
429             .arg(ifname).arg(strerror(errno)) :
430             tr("Failed to rebind: %1")
431             .arg(strerror(errno)));
432         return false;
433 }
434
435 void DhcpcdPreferences::rebind()
436 {
437
438         if (changedConfig()) {
439                 bool cancel;
440                 writeConfig(&cancel);
441                 if (cancel)
442                         return;
443         }
444
445         DHCPCD_CONNECTION *con = parent->getConnection();
446         DHCPCD_IF *i;
447         bool worked;
448         bool found;
449         if (eBlock == NULL || strcmp(eWhat, "interface") == 0) {
450                 worked = tryRebind(iface ? iface->ifname : NULL);
451                 goto done;
452         }
453
454         found = false;
455         worked = true;
456         for (i = dhcpcd_interfaces(con); i; i = i->next) {
457                 if (strcmp(i->type, "link") == 0 &&
458                     (i->ssid && strcmp(i->ssid, eBlock) == 0))
459                 {
460                         found = true;
461                         if (!tryRebind(i->ifname))
462                                 worked = false;
463                 }
464         }
465         if (!found) {
466                 QMessageBox::information(this,
467                     tr("No matching interface"),
468                     tr("No interface is bound to this SSID to rebind"));
469                 return;
470         }
471
472 done:
473         if (worked)
474                 close();
475 }
476
477 void DhcpcdPreferences::tryClose()
478 {
479
480         if (changedConfig()) {
481                 bool cancel;
482                 writeConfig(&cancel);
483                 if (cancel)
484                         return;
485         }
486         close();
487 }