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