dhcpcd-qt now has the same document-save and new icons as dhcpcd-gtk.
[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         char **list, **lp;
175         QIcon icon;
176
177         /* clear and then disconnect so we trigger a save */
178         blocks->clear();
179         blocks->disconnect(this);
180
181         free(eWhat);
182         eWhat = strdup(txt.toLower().toAscii());
183
184         list = dhcpcd_config_blocks(parent->getConnection(),
185             txt.toLower().toAscii());
186
187         if (txt == "interface") {
188                 DHCPCD_IF *i;
189
190                 blocks->addItem(tr("Select an interface"));
191                 for (i = dhcpcd_interfaces(parent->getConnection());
192                     i; i = i->next)
193                 {
194                         if (strcmp(i->type, "link") == 0) {
195                                 for (lp = list; lp && *lp; lp++) {
196                                         if (strcmp(i->ifname, *lp) == 0)
197                                                 break;
198                                 }
199                                 icon = DhcpcdQt::getIcon("devices",
200                                     lp && *lp ?
201                                     "document-save" : "document-new");
202                                 blocks->addItem(icon, i->ifname);
203                         }
204                 }
205         } else {
206                 QList<DhcpcdWi *> *wis = parent->getWis();
207
208                 blocks->addItem(tr("Select a SSID"));
209                 for (int i = 0; i < wis->size(); i++) {
210                         DHCPCD_WI_SCAN *scan;
211                         DhcpcdWi *wi = wis->at(i);
212
213                         for (scan = wi->getScans(); scan; scan = scan->next) {
214                                 for (lp = list; lp && *lp; lp++) {
215                                         if (strcmp(scan->ssid, *lp) == 0)
216                                                 break;
217                                 }
218                                 icon = DhcpcdQt::getIcon("devices",
219                                     lp && *lp ?
220                                     "document-save" : "document-new");
221                                 blocks->addItem(icon, scan->ssid);
222                         }
223                 }
224         }
225
226         dhcpcd_config_blocks_free(list);
227
228         /* Now make the 1st item unselectable and reconnect */
229         qobject_cast<QStandardItemModel *>
230             (blocks->model())->item(0)->setEnabled(false);
231         connect(blocks, SIGNAL(currentIndexChanged(const QString &)),
232             this, SLOT(showBlock(const QString &)));
233
234 }
235
236 void DhcpcdPreferences::clearConfig()
237 {
238
239         autoConf->setChecked(true);
240         ip->setText("");
241         router->setText("");
242         rdnss->setText("");
243         dnssl->setText("");
244 }
245
246 void DhcpcdPreferences::showConfig()
247 {
248         const char *val;
249         bool a;
250
251         if ((val = dhcpcd_config_get_static(config, "ip_address=")) != NULL)
252                 a = false;
253         else
254                 a = !((val = dhcpcd_config_get(config, "inform")) == NULL &&
255                     (iface && iface->flags & IFF_POINTOPOINT));
256         autoConf->setChecked(a);
257         ip->setText(val);
258         router->setText(dhcpcd_config_get_static(config, "routers="));
259         rdnss->setText(dhcpcd_config_get_static(config,"domain_name_servers="));
260         dnssl->setText(dhcpcd_config_get_static(config, "domain_search="));
261 }
262
263 bool DhcpcdPreferences::changedConfig()
264 {
265         const char *val;
266         bool a;
267
268         if ((val = dhcpcd_config_get_static(config, "ip_address=")) != NULL)
269                 a = false;
270         else
271                 a = !((val = dhcpcd_config_get(config, "inform")) == NULL &&
272                     (iface && iface->flags & IFF_POINTOPOINT));
273         if (autoConf->isChecked() != a)
274                 return true;
275         if (ip->text().compare(val))
276                 return true;
277         val = dhcpcd_config_get_static(config, "routers=");
278         if (router->text().compare(val))
279                 return true;
280         val = dhcpcd_config_get_static(config, "domain_name_servers=");
281         if (rdnss->text().compare(val))
282                 return true;
283         val = dhcpcd_config_get_static(config, "domain_search=");
284         if (rdnss->text().compare(val))
285                 return true;
286         return false;
287 }
288
289
290 const char *DhcpcdPreferences::getString(QLineEdit *le)
291 {
292         if (le->text().isEmpty())
293                 return NULL;
294         return le->text().trimmed().toAscii();
295 }
296
297 bool DhcpcdPreferences::setOption(const char *opt, const char *val, bool *ret)
298 {
299         if (opt[strlen(opt) - 1] == '=') {
300                 if (!dhcpcd_config_set_static(&config, opt, val))
301                         qCritical("dhcpcd_config_set_static: %s",
302                             strerror(errno));
303                 else
304                         return true;
305         } else {
306                 if (!dhcpcd_config_set(&config, opt, val))
307                         qCritical("dhcpcd_config_set: %s",
308                             strerror(errno));
309                 else
310                         return true;
311         }
312
313         if (ret)
314                 *ret = false;
315         return false;
316 }
317
318
319 bool DhcpcdPreferences::makeConfig()
320 {
321         const char ns[] = "", *val;
322         bool a, ret;
323
324         a = autoConf->isChecked();
325         ret = true;
326         if (iface && iface->flags & IFF_POINTOPOINT)
327                 setOption("ip_address=", a ? NULL : ns, &ret);
328         else {
329                 val = getString(ip);
330                 setOption("inform", a ? val : NULL, &ret);
331                 setOption("ip_address=", a ? NULL : val, &ret);
332         }
333
334         val = getString(router);
335         setOption("routers=", val, &ret);
336
337         val = getString(rdnss);
338         setOption("domain_name_servers=", val, &ret);
339
340         val = getString(dnssl);
341         setOption("domain_search=", val, &ret);
342
343         return ret;
344 }
345
346 bool DhcpcdPreferences::writeConfig(bool *cancel)
347 {
348
349         switch (QMessageBox::question(this, tr("Save Configuration"),
350             tr("Do you want to save your changes?"),
351             QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel)) {
352         case QMessageBox::Cancel:
353                 *cancel = true;
354                 return false;
355         case QMessageBox::Discard:
356                 return true;
357         default:
358                 break;
359         }
360         *cancel = false;
361
362         DHCPCD_CONNECTION *con = parent->getConnection();
363         if (!makeConfig()) {
364                 qCritical("failed to make config");
365                 goto err;
366         }
367         if (!dhcpcd_config_write(con, eWhat, eBlock, config)) {
368                 qCritical("dhcpcd_config_write: %s", strerror(errno));
369                 QMessageBox::critical(parent,
370                     tr("Failed to write configuration"),
371                     tr("Failed to write configuration:\n\n%1: %2")
372                     .arg(dhcpcd_cffile(con))
373                     .arg(strerror(errno)));
374                 goto err;
375         }
376         return true;
377
378 err:
379         /* Reload our config if there is a problem */
380         config = dhcpcd_config_read(con, eWhat, eBlock);
381         return false;
382 }
383
384 void DhcpcdPreferences::showBlock(const QString &txt)
385 {
386
387         if (eBlock) {
388                 if (changedConfig()) {
389                         bool cancel;
390                         if (!writeConfig(&cancel))
391                                 return;
392                 }
393                 free(eBlock);
394         }
395         if (txt.isEmpty())
396                 eBlock = NULL;
397         else
398                 eBlock = strdup(txt.toAscii());
399
400         dhcpcd_config_free(config);
401         iface = NULL;
402         DHCPCD_CONNECTION *con = parent->getConnection();
403
404         if (eBlock && eWhat) {
405                 if (strcmp(eWhat, "interface") == 0)
406                         iface = dhcpcd_get_if(con, eBlock, "link");
407                 ip->setEnabled(iface == NULL ||
408                     !(iface->flags & IFF_POINTOPOINT));
409                 errno = 0;
410                 config = dhcpcd_config_read(con, eWhat, eBlock);
411                 if (config == NULL && errno) {
412                         const char *s;
413
414                         s = strerror(errno);
415                         qCritical("dhcpcd_config_read: %s", s);
416                         QMessageBox::critical(this,
417                             tr("Error reading configuration"),
418                             tr("Error reading: ") + dhcpcd_cffile(con) +
419                             "\n\n" + s);
420                 }
421         } else
422                 config = NULL;
423
424         showConfig();
425         bool enabled = dhcpcd_config_writeable(con) && eBlock != NULL;
426         autoConf->setEnabled(enabled);
427         ipSetup->setEnabled(enabled);
428         clear->setEnabled(enabled);
429 }
430
431 bool DhcpcdPreferences::tryRebind(const char *ifname)
432 {
433
434         if (dhcpcd_rebind(parent->getConnection(), ifname) == 0)
435                 return true;
436
437         qCritical("dhcpcd_rebind: %s", strerror(errno));
438         QMessageBox::critical(this,
439             tr("Rebind failed"),
440             ifname ? tr("Failed to rebind interface %1: %2")
441             .arg(ifname).arg(strerror(errno)) :
442             tr("Failed to rebind: %1")
443             .arg(strerror(errno)));
444         return false;
445 }
446
447 void DhcpcdPreferences::rebind()
448 {
449
450         if (changedConfig()) {
451                 bool cancel;
452                 writeConfig(&cancel);
453                 if (cancel)
454                         return;
455         }
456
457         DHCPCD_CONNECTION *con = parent->getConnection();
458         DHCPCD_IF *i;
459         bool worked;
460         bool found;
461         if (eBlock == NULL || strcmp(eWhat, "interface") == 0) {
462                 worked = tryRebind(iface ? iface->ifname : NULL);
463                 goto done;
464         }
465
466         found = false;
467         worked = true;
468         for (i = dhcpcd_interfaces(con); i; i = i->next) {
469                 if (strcmp(i->type, "link") == 0 &&
470                     (i->ssid && strcmp(i->ssid, eBlock) == 0))
471                 {
472                         found = true;
473                         if (!tryRebind(i->ifname))
474                                 worked = false;
475                 }
476         }
477         if (!found) {
478                 QMessageBox::information(this,
479                     tr("No matching interface"),
480                     tr("No interface is bound to this SSID to rebind"));
481                 return;
482         }
483
484 done:
485         if (worked)
486                 close();
487 }
488
489 void DhcpcdPreferences::tryClose()
490 {
491
492         if (changedConfig()) {
493                 bool cancel;
494                 writeConfig(&cancel);
495                 if (cancel)
496                         return;
497         }
498         close();
499 }