File: peditor.cpp

package info (click to toggle)
cmst 2023.03.14-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,608 kB
  • sloc: cpp: 6,999; xml: 142; makefile: 12
file content (403 lines) | stat: -rw-r--r-- 16,616 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/****************** peditor.cpp ***********************************

Code to manage the Properties Editor dialog.

Copyright (C) 2013-2023
by: Andrew J. Bibb
License: MIT

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"),to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
***********************************************************************/

# include <QtCore/QDebug>
# include <QRegularExpression>
# include <QRegularExpressionValidator>

# include "./peditor.h"
# include "./code/shared/shared.h"
# include "./code/trstring/tr_strings.h"
# include "../resource.h"

#define DBUS_SERVICE "net.connman"

PropertiesEditor::PropertiesEditor(QWidget* parent, const arrayElement& ae)
   : QDialog(parent)
{
   // Setup the user interface
   ui.setupUi(this);

   // Data members
   objpath = ae.objpath;
   objmap = ae.objmap;
   sl_ipv4_method << "dhcp" << "manual" << "off";
   sl_ipv6_method << "auto" << "manual" << "off";
   sl_ipv6_privacy << "disabled" << "enabled" << "prefered";            // misspelling prefered is necessary
   sl_proxy_method << "direct" << "auto" << "manual";

   // connect signals to slots
   connect(ui.toolButton_whatsthis, SIGNAL(clicked()), this, SLOT(showWhatsThis()));
   connect(ui.pushButton_resetpage, SIGNAL(clicked()), this, SLOT(resetPage()));
   connect(ui.pushButton_resetall, SIGNAL(clicked()), this, SLOT(resetAll()));
   connect(ui.pushButton_ok, SIGNAL(clicked()), this, SLOT(updateConfiguration()));
   connect(ui.comboBox_ipv4method, SIGNAL(currentIndexChanged(int)), this, SLOT(ipv4Method(int)));

   // Setup comboboxes
   ui.comboBox_ipv4method->addItems(TranslateStrings::cmtr_sl(sl_ipv4_method) );
   ui.comboBox_ipv6method->addItems(TranslateStrings::cmtr_sl(sl_ipv6_method) );
   ui.comboBox_ipv6privacy->addItems(TranslateStrings::cmtr_sl(sl_ipv6_privacy) );
   ui.comboBox_proxymethod->addItems(TranslateStrings::cmtr_sl(sl_proxy_method) );

   // Using the ValidatingDialog only to obtain the validator. This
   // allows all the validating code to be in a single location.
   // QLineEdits (validated)  that allow single address
   QRegularExpressionValidator* qrex_val4= new QRegularExpressionValidator(QRegularExpression(shared::ValidatingDialog(this).getPattern(CMST::ValDialog_IPv4)) );
      ui.lineEdit_ipv4address->setValidator(qrex_val4);
      ui.lineEdit_ipv4netmask->setValidator(qrex_val4);
      ui.lineEdit_ipv4gateway->setValidator(qrex_val4);

   QRegularExpressionValidator* qrex_val6= new QRegularExpressionValidator(QRegularExpression(shared::ValidatingDialog(this).getPattern(CMST::ValDialog_IPv6)) );
      ui.lineEdit_ipv6address->setValidator(qrex_val6);
      ui.lineEdit_ipv6gateway->setValidator(qrex_val6);

    // now QLineEdits (validated)   that allow multiple addresses
   QRegularExpressionValidator* qrex_val46= new QRegularExpressionValidator(QRegularExpression(shared::ValidatingDialog(this).getPattern(CMST::ValDialog_46, true)) );
      ui.lineEdit_nameservers->setValidator(qrex_val46);
      ui.lineEdit_timeservers->setValidator(qrex_val46);

   qrex_val4->deleteLater();
   qrex_val6->deleteLater();
   qrex_val46->deleteLater();

   // initialize and populate submaps
   ipv4map.clear();
   ipv6map.clear();
   proxmap.clear();
   shared::extractMapData(ipv4map, objmap.value("IPv4.Configuration") );
   shared::extractMapData(ipv6map, objmap.value("IPv6.Configuration") );
   shared::extractMapData(proxmap, objmap.value("Proxy.Configuration") );

   // Seed initial values in the dialog.
   ui.checkBox_autoconnect->setChecked(objmap.value("AutoConnect").toBool() );
   ui.lineEdit_nameservers->setText(objmap.value("Nameservers.Configuration").toStringList().join("\n") );
   ui.lineEdit_timeservers->setText(objmap.value("Timeservers.Configuration").toStringList().join("\n"));
   ui.lineEdit_domains->setText(objmap.value("Domains.Configuration").toStringList().join("\n"));

   // ipv4 page
   if (! ipv4map.value("Method").toString().isEmpty() ) {
      ui.comboBox_ipv4method->setCurrentIndex(sl_ipv4_method.indexOf(QRegularExpression(ipv4map.value("Method").toString())) );
   }
   ui.lineEdit_ipv4address->setText(ipv4map.value("Address").toString() );
   ui.lineEdit_ipv4netmask->setText(ipv4map.value("Netmask").toString() );
   ui.lineEdit_ipv4gateway->setText(ipv4map.value("Gateway").toString() );

   // ipv6 page
   if (! ipv6map.value("Method").toString().isEmpty() ) {
      ui.comboBox_ipv6method->setCurrentIndex(sl_ipv6_method.indexOf(QRegularExpression(ipv6map.value("Method").toString())) );
   }
   ui.spinBox_ipv6prefixlength->setValue(ipv6map.value("PrefixLength").toUInt() );
   ui.lineEdit_ipv6address->setText(ipv6map.value("Address").toString() );
   ui.lineEdit_ipv6gateway->setText(ipv6map.value("Gateway").toString() );
   if (! ipv6map.value("Privacy").toString().isEmpty() ) {
      ui.comboBox_ipv6privacy->setCurrentIndex(sl_ipv6_privacy.indexOf(QRegularExpression(ipv6map.value("Privacy").toString())) );
   }

   // proxy page
   if (proxmap.value("Method").toString().isEmpty() )
      ui.comboBox_proxymethod->setCurrentIndex(-1);
   else
      ui.comboBox_proxymethod->setCurrentIndex(sl_proxy_method.indexOf(QRegularExpression(proxmap.value("Method").toString())) );

   ui.lineEdit_proxyservers->setText(proxmap.value("Servers").toStringList().join("\n") );
   ui.lineEdit_proxyexcludes->setText(proxmap.value("Excludes").toStringList().join("\n") );
   ui.lineEdit_proxyurl->setText(proxmap.value("URL").toString() );

   if (ui.comboBox_proxymethod->currentIndex() < 0)
      ui.stackedWidget_proxy01->setCurrentIndex(0);
   else
      ui.stackedWidget_proxy01->setCurrentIndex(ui.comboBox_proxymethod->currentIndex() );

   // disable pages not needed for a service (mainly vpn)
   if (objmap.value("Type").toString() == "vpn") {
      ui.ipv4->setDisabled(true);
      ui.ipv6->setDisabled(true);
   }

   // mDNS page
   ui.checkBox_mdns->setChecked(objmap.value("mDNS").toBool() );

}

///////////////////////////////////////////////// Private Slots /////////////////////////////////////////////
//
// Slot to enter whats this mode
// Called when the ui.toolButton_whatsthis clicked() signal is emitted
void PropertiesEditor::showWhatsThis()
{
   QWhatsThis::enterWhatsThisMode();
}

//
// Function to clear the contents of the specified page. If the page
// argument is less than one (default value is -1) then clear the
// current toolbox page. Called when ui.pushButton_resetpage is clicked()
void PropertiesEditor::resetPage(int page)
{
   // find the page (index) to clear.
   int toolboxindex = ui.toolBox_peditor->currentIndex();
   if (page >= 0 ) toolboxindex = page;

   switch (toolboxindex) {
      case 0:
         ui.checkBox_autoconnect->setChecked(objmap.value("AutoConnect").toBool() );
         break;
      case 1:
         ui.lineEdit_nameservers->clear();
         break;
      case 2:
         ui.lineEdit_timeservers->clear();
         break;
      case 3:
         ui.lineEdit_domains->clear();
         break;
      case 4:
         ui.comboBox_ipv4method->setCurrentIndex(0);
         ui.lineEdit_ipv4address->clear();
         ui.lineEdit_ipv4netmask->clear();
         ui.lineEdit_ipv4gateway->clear();
         break;
      case 5:
         ui.comboBox_ipv6method->setCurrentIndex(0);
         ui.spinBox_ipv6prefixlength->setValue(0);
         ui.lineEdit_ipv6address->clear();
         ui.lineEdit_ipv6gateway->clear();
         ui.comboBox_ipv6privacy->setCurrentIndex(0);
         break;
      case 6:
         ui.comboBox_proxymethod->setCurrentIndex(0);
         ui.lineEdit_proxyurl->clear();
         ui.lineEdit_proxyservers->clear();
         ui.lineEdit_proxyexcludes->clear();
         break;
      default:
         break;
   } // switch

   return;
}

//
// Slot to reset all pages.  Called when ui.pushButton_resetall
// is clicked().
void PropertiesEditor::resetAll()
{
   for (int i = 0; i < ui.toolBox_peditor->count(); ++i) {
      this->resetPage(i);
   }

   return;
}

//
// Slot to update the configuration then exit.  Called when  ui.pushButton_ok
// is clicked. Step through each page of the QToolBox and send any entries
// to connman.
void PropertiesEditor::updateConfiguration()
{
   // Some variables
   QString s;
   QStringList sl;
   QList<QVariant> vlist;
   QMap<QString,QVariant> dict;
   QDBusInterface* iface_serv = new QDBusInterface(DBUS_SERVICE, objpath.path(), "net.connman.Service", QDBusConnection::systemBus(), this);
   QList<QLineEdit*> lep;
   QStringList slp;

   // QCheckboxes
   // Only update if changed
   if (ui.checkBox_autoconnect->isChecked() != objmap.value("AutoConnect").toBool() ) {
      vlist.clear();
      vlist << "AutoConnect";
      vlist << QVariant::fromValue(QDBusVariant(ui.checkBox_autoconnect->isChecked()) );
      shared::processReply(iface_serv->callWithArgumentList(QDBus::AutoDetect, "SetProperty", vlist) );
   }

   // QLineEdits (nameservers, timeservers and domains)
   lep.clear();
   slp.clear();
   lep << ui.lineEdit_nameservers << ui.lineEdit_timeservers << ui.lineEdit_domains;
   slp << "Nameservers.Configuration" << "Timeservers.Configuration" << "Domains.Configuration";
   for (int i = 0; i < lep.count(); ++i) {
      s = lep.at(i)->text();
      s.replace(',', ' ');
      s.replace(';', ' ');
      s.replace('|', ' ');
      s = s.simplified();

      if (s.isEmpty() ) sl.clear();
      else sl = s.split(' ');

      // Only update if an entry has changed.
      if (sl != objmap.value(slp.at(i)).toStringList()) {
         vlist.clear();
         vlist << slp.at(i);
         vlist << QVariant::fromValue(QDBusVariant(sl) );
         shared::processReply(iface_serv->callWithArgumentList(QDBus::AutoDetect, "SetProperty", vlist) );
      } // if
   } //for

   // ipv4
   if (ui.ipv4 == ui.toolBox_peditor->currentWidget() ) {
      // Only update if an entry has changed.
      if ((ui.comboBox_ipv4method->currentText() != TranslateStrings::cmtr(ipv4map.value("Method").toString()) )  |
         (ui.lineEdit_ipv4address->text() != TranslateStrings::cmtr(ipv4map.value("Address").toString()) )        |
         (ui.lineEdit_ipv4netmask->text() != TranslateStrings::cmtr(ipv4map.value("Netmask").toString()) )        |
         (ui.lineEdit_ipv4gateway->text() != TranslateStrings::cmtr(ipv4map.value("Gateway").toString())) ) {

         vlist.clear();
         lep.clear();
         slp.clear();
         dict.clear();

         if (ui.comboBox_ipv4method->currentIndex() >= 0) {
            vlist << "IPv4.Configuration";
            dict.insert("Method", sl_ipv4_method.at(ui.comboBox_ipv4method->currentIndex()) );
            lep << ui.lineEdit_ipv4address << ui.lineEdit_ipv4netmask << ui.lineEdit_ipv4gateway;
            slp << "Address" << "Netmask" << "Gateway";

            for (int i = 0; i < lep.count(); ++i) {
               s = lep.at(i)->text();
               s = s.simplified(); // really should not be needed with the validator
               if (s.isEmpty() ) break;
               dict.insert(slp.at(i), s);
            } // for

            vlist << QVariant::fromValue(QDBusVariant(dict) );
            shared::processReply(iface_serv->callWithArgumentList(QDBus::AutoDetect, "SetProperty", vlist) );
         } // if there is a valid index
      } // if ipv4 changed
   }// ipv4 page is enabled

   // ipv6
   if (ui.ipv6 == ui.toolBox_peditor->currentWidget() ) {
      // Only update if an entry has changed.
      if ((ui.comboBox_ipv6method->currentText() != TranslateStrings::cmtr(ipv6map.value("Method").toString()) )  |
         (static_cast<uint>(ui.spinBox_ipv6prefixlength->value()) != ipv6map.value("PrefixLength").toUInt() )     |
         (ui.lineEdit_ipv6gateway->text() != TranslateStrings::cmtr(ipv6map.value("Gateway").toString()) )        |
         (ui.lineEdit_ipv6address->text() != TranslateStrings::cmtr(ipv6map.value("Address").toString()) )        |
         (ui.comboBox_ipv6privacy->currentText() != TranslateStrings::cmtr(ipv6map.value("Privacy").toString())) ) {

         vlist.clear();
         lep.clear();
         slp.clear();
         dict.clear();

         if (ui.comboBox_ipv6method->currentIndex() >= 0) {
            vlist << "IPv6.Configuration";
            dict.insert("Method", sl_ipv6_method.at(ui.comboBox_ipv6method->currentIndex()) );
            dict.insert("PrefixLength", QVariant::fromValue(static_cast<quint8>(ui.spinBox_ipv6prefixlength->value())) );
            dict.insert("Privacy", sl_ipv6_privacy.at(ui.comboBox_ipv6privacy->currentIndex()) );

            lep << ui.lineEdit_ipv6address <<  ui.lineEdit_ipv6gateway;
            slp << "Address" << "Gateway";
            for (int i = 0; i < lep.count(); ++i) {
               s = lep.at(i)->text();
               s = s.simplified(); // really should not be needed with the validator
               if (s.isEmpty() ) break;
               dict.insert(slp.at(i), s);
            } // for

            vlist << QVariant::fromValue(QDBusVariant(dict) );
            shared::processReply(iface_serv->callWithArgumentList(QDBus::AutoDetect, "SetProperty", vlist) );
         } // if there is a valid index
      } // if ipv6 changed
   } // if ipv6 enabled

   // proxy
   // Only update if an entry has changed.
   if ((ui.comboBox_proxymethod->currentText() != TranslateStrings::cmtr(proxmap.value("Method").toString()) ) |
      (ui.lineEdit_proxyservers->text() != proxmap.value("Servers").toStringList().join("\n") )                |
      (ui.lineEdit_proxyexcludes->text() != proxmap.value("Excludes").toStringList().join("\n") )              |
      (ui.lineEdit_proxyurl->text() != proxmap.value("URL").toString()) ) {

      vlist.clear();
      lep.clear();
      slp.clear();
      dict.clear();
      vlist << "Proxy.Configuration";
      dict.insert("Method", sl_proxy_method.at(ui.comboBox_proxymethod->currentIndex()) );

      lep << ui.lineEdit_proxyurl << ui.lineEdit_proxyservers << ui.lineEdit_proxyexcludes;
      slp << "URL" << "Servers" << "Excludes";
      for (int i = 0; i < lep.count(); ++i) {
         s = lep.at(i)->text();
         s = s.simplified();
         // URL is a single string
         if ( i == 0 ) {
            if (s.isEmpty() ) s.clear();
            dict.insert(slp.at(i), s);
         } // if
         // remanider are an array of strings
         else {
            if (s.isEmpty() ) sl.clear();
            else sl = s.split(' ');
            dict.insert(slp.at(i), sl);
         } //else
      } // for

      vlist << QVariant::fromValue(QDBusVariant(dict) );
      shared::processReply(iface_serv->callWithArgumentList(QDBus::AutoDetect, "SetProperty", vlist) );
   } // if proxy changed

   // mDNS
   if (ui.checkBox_mdns->isChecked() != objmap.value("mDNS").toBool() ) {
      vlist.clear();
      vlist << "mDNS";
      vlist << QVariant::fromValue(QDBusVariant(ui.checkBox_mdns->isChecked()) );
      shared::processReply(iface_serv->callWithArgumentList(QDBus::AutoDetect, "SetProperty", vlist) );
   }

   // cleanup
   iface_serv->deleteLater();
   this->accept();
}

//
//       Slot to hide or show boxes based on ipv4 method
void PropertiesEditor::ipv4Method(int idx)
{
   // variables
   if (idx == 1 ) { // index 1 is manual
      ui.label_ipv4address->show();
      ui.label_ipv4netmask->show();
      ui.label_ipv4gateway->show();
      ui.lineEdit_ipv4address->show();
      ui.lineEdit_ipv4netmask->show();
      ui.lineEdit_ipv4gateway->show();
   }
   else {
      ui.label_ipv4address->hide();
      ui.label_ipv4netmask->hide();
      ui.label_ipv4gateway->hide();
      ui.lineEdit_ipv4address->hide();
      ui.lineEdit_ipv4netmask->hide();
      ui.lineEdit_ipv4gateway->hide();
   }

   return;
}