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 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
|
/****************** prov_ed.cpp ***********************************
Code to manage the Provisioning 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 <QDBusMessage>
# include <QDBusConnection>
# include <QDBusInterface>
# include <QMessageBox>
# include <QInputDialog>
# include <QList>
# include <QVariant>
# include <QAction>
# include <QFile>
# include <QFileDialog>
# include <QRegularExpression>
# include "./prov_ed.h"
# include "../resource.h"
# include "./code/trstring/tr_strings.h"
# include "./code/shared/shared.h"
//
// Provisioning Editor constructor
ProvisioningEditor::ProvisioningEditor(QWidget* parent, const float& ver) : QDialog(parent)
{
// Setup the user interface
ui.setupUi(this);
// Data members
menubar = new QMenuBar(this);
ui.verticalLayout01->setMenuBar(menubar);
con_path = "/var/lib/connman";
statusbar = new QStatusBar(this);
ui.verticalLayout01->addWidget(statusbar);
statustimeout = 2000;
i_sel = CMST::ProvEd_No_Selection;
// Setup the buttongroup
bg01 = new QButtonGroup(this);
bg01->addButton(ui.pushButton_open);
bg01->addButton(ui.pushButton_save);
bg01->addButton(ui.pushButton_delete);
// Add actions to actiongroups (signals from actiongroups are connected to slots)
group_template = new QActionGroup(this);
group_template->addAction(ui.actionTemplateEduroamLong);
group_template->addAction(ui.actionTemplateEduroamShort);
group_template->addAction(ui.actionTemplateeap_peap);
group_template->addAction(ui.actionTemplateeap_tls);
group_template->addAction(ui.actionTemplateeap_ttls);
group_freeform = new QActionGroup(this);
group_freeform->addAction(ui.actionGlobal);
group_freeform->addAction(ui.actionGlobalName);
group_freeform->addAction(ui.actionGlobalDescription);
group_freeform->addAction(ui.actionService);
group_freeform->addAction(ui.actionWifiPrivateKeyPassphrase);
group_freeform->addAction(ui.actionWifiIdentity);
group_freeform->addAction(ui.actionWifiAnonymousIdentity);
group_freeform->addAction(ui.actionWifiPassphrase);
group_freeform->addAction(ui.actionWifiPhase2);
group_freeform->addAction(ui.actionServiceDeviceName);
group_combobox = new QActionGroup(this);
group_combobox->addAction(ui.actionServiceType);
group_combobox->addAction(ui.actionWifiEAP);
group_combobox->addAction(ui.actionWifiPrivateKeyPassphraseType);
group_combobox->addAction(ui.actionWifiSecurity);
group_combobox->addAction(ui.actionWifiHidden);
group_combobox->addAction(ui.actionServiceIPv6Privacy);
group_combobox->addAction(ui.actionServiceIPv4);
group_combobox->addAction(ui.actionServiceIPv6);
group_combobox->addAction(ui.actionServicemDNS);
group_validated = new QActionGroup(this);
group_validated->addAction(ui.actionServiceMAC);
group_validated->addAction(ui.actionWifiSSID);
group_validated->addAction(ui.actionServiceNameServers);
group_validated->addAction(ui.actionServiceTimeServers);
group_validated->addAction(ui.actionServiceSearchDomains);
group_validated->addAction(ui.actionServiceDomain);
group_validated->addAction(ui.actionWifiName);
group_validated->addAction(ui.actionWifiSubjectMatch);
group_validated->addAction(ui.actionWifiAltSubjectMatch);
group_validated->addAction(ui.actionWifiDomainMatch);
group_validated->addAction(ui.actionWifiDomainSuffixMatch);
group_selectfile = new QActionGroup(this);
group_selectfile->addAction(ui.actionWifiCACertFile);
group_selectfile->addAction(ui.actionWifiClientCertFile);
group_selectfile->addAction(ui.actionWifiPrivateKeyFile);
// Add Actions from UI to menu's
menu_global = new QMenu(tr("Global"), this);
menu_global->addAction(ui.actionGlobal);
menu_global->addSeparator();
menu_global->addAction(ui.actionGlobalName);
menu_global->addAction(ui.actionGlobalDescription);
menu_service = new QMenu(tr("Service"), this);
menu_service->addAction(ui.actionService);
menu_service->addSeparator();
menu_service->addAction(ui.actionServiceType);
menu_service->addAction(ui.actionServiceDomain);
menu_service->addAction(ui.actionServiceMAC);
if (ver > 1.37f) {
menu_service->addAction(ui.actionServiceDeviceName);
menu_service->addAction(ui.actionServicemDNS);
}
menu_service->addSeparator();
menu_service->addAction(ui.actionServiceIPv4);
menu_service->addSeparator();
menu_service->addAction(ui.actionServiceIPv6);
menu_service->addAction(ui.actionServiceIPv6Privacy);
menu_service->addSeparator();
menu_service->addAction(ui.actionServiceNameServers);
menu_service->addAction(ui.actionServiceTimeServers);
menu_service->addAction(ui.actionServiceSearchDomains);
menu_wifi = new QMenu(tr("WiFi"), this);
menu_wifi->addSeparator();
menu_wifi->addAction(ui.actionWifiName);
menu_wifi->addAction(ui.actionWifiSSID);
menu_wifi->addSeparator();
menu_wifi->addAction(ui.actionWifiSecurity);
menu_wifi->addAction(ui.actionWifiPassphrase);
menu_wifi->addAction(ui.actionWifiHidden);
menu_wifi->addAction(ui.actionWifiPhase2);
menu_wifi->addSeparator();
menu_wifi->addAction(ui.actionWifiEAP);
menu_wifi->addAction(ui.actionWifiIdentity);
if (ver > 1.37f) {
menu_wifi->addAction(ui.actionWifiAnonymousIdentity);
menu_wifi->addAction(ui.actionWifiSubjectMatch);
menu_wifi->addAction(ui.actionWifiAltSubjectMatch);
menu_wifi->addAction(ui.actionWifiDomainMatch);
menu_wifi->addAction(ui.actionWifiDomainSuffixMatch);
}
menu_wifi->addSeparator();
menu_wifi->addAction(ui.actionWifiCACertFile);
menu_wifi->addAction(ui.actionWifiClientCertFile);
menu_wifi->addSeparator();
menu_wifi->addAction(ui.actionWifiPrivateKeyFile);
menu_wifi->addAction(ui.actionWifiPrivateKeyPassphrase);
menu_wifi->addAction(ui.actionWifiPrivateKeyPassphraseType);
menu_template = new QMenu(tr("Templates"), this);menu_template->addAction(ui.actionTemplateEduroamShort);
menu_template->addAction(ui.actionTemplateEduroamLong);
menu_template->addAction(ui.actionTemplateEduroamShort);
menu_template->addSeparator();
menu_template->addAction(ui.actionTemplateeap_peap);
menu_template->addAction(ui.actionTemplateeap_tls);
menu_template->addAction(ui.actionTemplateeap_ttls);
// add menus to UI
menubar->addMenu(menu_global);
menubar->addMenu(menu_service);
menubar->addMenu(menu_wifi);
menubar->addMenu(menu_template);
// connect signals to slots
connect(ui.toolButton_whatsthis, SIGNAL(clicked()), this, SLOT(showWhatsThis()));
connect(ui.pushButton_resetpage, SIGNAL(clicked()), this, SLOT(resetPage()));
connect(bg01, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(requestFileList(QAbstractButton*)));
connect(group_template, SIGNAL(triggered(QAction*)), this, SLOT(templateTriggered(QAction*)));
connect(group_freeform, SIGNAL(triggered(QAction*)), this, SLOT(inputFreeForm(QAction*)));
connect(group_combobox, SIGNAL(triggered(QAction*)), this, SLOT(inputComboBox(QAction*)));
connect(group_validated, SIGNAL(triggered(QAction*)), this, SLOT(inputValidated(QAction*)));
connect(group_selectfile, SIGNAL(triggered(QAction*)), this, SLOT(inputSelectFile(QAction*)));
}
/////////////////////////////////////////////// Private Slots /////////////////////////////////////////////
//
// Slot called when a member of the QActionGroup group_selectfile
void ProvisioningEditor::inputSelectFile(QAction* act)
{
// variables
QString key = act->text();
QString title;
if (act == ui.actionWifiCACertFile) title = tr("File Path to the CA Certificate File");
if (act == ui.actionWifiClientCertFile) title = tr("File Path to the Client Certificate File");
if (act == ui.actionWifiPrivateKeyFile) title = tr("File path to the Client Private Key File");;
QString fname = QFileDialog::getOpenFileName(this, title,
QDir::homePath(),
tr("Key Files (*.pem);;All Files (*.*)"));
// return if the file name returned is empty (cancel pressed in the dialog)
if (fname.isEmpty() ) return;
// put the path into the text edit
key.append(" = %1\n");
ui.plainTextEdit_main->insertPlainText(key.arg(fname) );
return;
}
//
// Slot called when a member of the QActionGroup group_validated is triggered
void ProvisioningEditor::inputValidated(QAction* act)
{
// variables
QString key = act->text();
QChar delim(','); // default delim character for plurals
// create the dialog
shared::ValidatingDialog* vd = new shared::ValidatingDialog(this);
// create some prompts and set validator
if (act == ui.actionServiceMAC) {vd->setLabel(tr("MAC address.")); vd->setValidator(CMST::ValDialog_MAC);}
if (act == ui.actionWifiSSID) {vd->setLabel(tr("SSID: hexadecimal representation of an 802.11 SSID")); vd->setValidator(CMST:: ValDialog_Hex);}
if (act == ui.actionServiceNameServers) {vd->setLabel(tr("List of Nameservers")); vd->setValidator(CMST::ValDialog_46, true);}
if (act == ui.actionServiceTimeServers) {vd->setLabel(tr("List of Timeservers")); vd->setValidator(CMST::ValDialog_46, true);}
if (act == ui.actionServiceSearchDomains) {vd->setLabel(tr("List of DNS Search Domains")); vd->setValidator(CMST::ValDialog_Dom, true);}
if (act == ui.actionServiceDomain) {vd->setLabel(tr("Domain name to be used")); vd->setValidator(CMST::ValDialog_Dom);}
if (act == ui.actionWifiName) {vd->setLabel(tr("Enter the string representation of an 802.11 SSID.")); vd->setValidator(CMST::ValDialog_Word);}
if (act == ui.actionWifiSubjectMatch) { vd->setLabel(tr("Substring to be matched against the subject of the authentication server")); vd->setValidator(CMST::ValDialog_Word);}
if (act == ui.actionWifiAltSubjectMatch) {vd->setLabel(tr("List of entries to be matched against the alternative subject name.")); vd->setValidator(CMST::ValDialog_Word, true); delim=';';}
if (act == ui.actionWifiDomainMatch) {vd->setLabel(tr("A fully qualified domain name used as a full match requirement for the authentication server")); vd->setValidator(CMST::ValDialog_Dom);}
if (act == ui.actionWifiDomainSuffixMatch) {vd->setLabel(tr("A fully qualified domain name used as a suffix match requirement for the authentication server")); vd->setValidator(CMST::ValDialog_Dom);}
// if accepted put an entry in the textedit
if (vd->exec() == QDialog::Accepted) {
QString s = vd->getText();
key.append(" = %1\n");
// format strings with multiple entries
if (vd->isPlural() ) {
s.replace(',', ' ');
s.replace(';', ' ');
s.replace('|', ' ');
s = s.simplified();
s.replace(' ', delim);
}
ui.plainTextEdit_main->insertPlainText(key.arg(s) );
}
// cleanup
vd->deleteLater();
return;
}
//
// Slot called when a member of the QActionGroup group_combobox is triggered
void ProvisioningEditor::inputComboBox(QAction* act)
{
// variables
QString key = act->text();
QString str;
bool ok;
QStringList sl;
// create some prompts
if (act == ui.actionServiceType) {str = tr("Service type."); sl << "ethernet" << "wifi";}
if (act == ui.actionWifiEAP) {str = tr("EAP type."); sl << "tls" << "ttls" << "peap";}
if (act == ui.actionWifiPrivateKeyPassphraseType) {str = tr("Private key passphrase type."); sl << "fsid";}
if (act == ui.actionWifiSecurity) {str = tr("Network security type."); sl << "psk" << "ieee8021x" << "wep" << "none";}
if (act == ui.actionWifiHidden) {str = tr("Hidden network"); sl << "true" << "false";}
if (act == ui.actionServiceIPv6Privacy) {str = tr("IPv6 Privacy"); sl << "disabled" << "enabled" << "preferred";}
if (act == ui.actionServiceIPv4) {str = tr("IPv4 Settings"); sl << "off" << "dhcp" << "address";}
if (act == ui.actionServiceIPv6) {str = tr("IPv6 Settings"); sl << "off" << "auto" << "address";}
if (act == ui.actionServicemDNS) {str = tr("Enable mDNS"); sl << "false" << "true";}
QStringList sl_tr = TranslateStrings::cmtr_sl(sl);
QString item = QInputDialog::getItem(this,
tr("%1 - Item Input").arg(TranslateStrings::cmtr("cmst")),
str,
sl_tr,
0,
false,
&ok);
// if we need ipv4 or ipv6 address information go there
if (act == ui.actionServiceIPv4 && sl.at(sl_tr.indexOf(QRegularExpression(item))) == "address" ) ipv4Address();
else {
if (act == ui.actionServiceIPv6 && sl.at(sl_tr.indexOf(QRegularExpression(item))) == "address" ) ipv6Address();
else {
key.append(" = %1\n");
if (ok) ui.plainTextEdit_main->insertPlainText(key.arg(sl.at(sl_tr.indexOf(QRegularExpression(item)))) );
} // else not ipv4 or ipv6
} //else not ipv4
return;
}
//
// Slot called when a member of the QActionGroup group_freeform is triggered
// Freeform strings may have spaces in them. For strings that cannot have spaces
// use validated text and set b_multiple to false.
void ProvisioningEditor::inputFreeForm(QAction* act)
{
// variables
const QLineEdit::EchoMode echomode = QLineEdit::Normal;
QString str;
bool ok;
QString key = act->text();
// create some prompts
if (act == ui.actionService) str = tr("Tag which will replace the * with<br>an identifier unique to the config file.");
if (act == ui.actionGlobalName) str = tr("Enter the network name.");
if (act == ui.actionGlobalDescription) str = tr("Enter a description of the network.");
if (act == ui.actionWifiPrivateKeyPassphrase) str = tr("Password/Passphrase for the private key file.");
if (act == ui.actionWifiIdentity) str = tr("Identity string for EAP.");
if (act == ui.actionWifiAnonymousIdentity) str = tr("Anonymous identity string for EAP.");
if (act == ui.actionWifiPassphrase) str = tr("RSN/WPA/WPA2 Passphrase");
if (act == ui.actionWifiPhase2) str = tr("Phase 2 (inner authentication with TLS tunnel)<br>authentication method.");
if (act == ui.actionServiceDeviceName) str = tr("The interface name in which to apply the provisioning (ex. eth0)") ;
if (act == ui.actionGlobal) {
key.append("\n");
ui.plainTextEdit_main->insertPlainText(key);
}
else {
act == ui.actionService ? key = "[service_%1]\n" : key.append(" = %1\n");
// get the string from the user
QString text = QInputDialog::getText(this,
tr("%1 - Text Input").arg(TranslateStrings::cmtr("cmst")),
str,
echomode,
"",
&ok);
if (ok) ui.plainTextEdit_main->insertPlainText(key.arg(text));
} // else
return;
}
//
// Not a real slot anymore as we only call this directly when we need to get ipv4 address/netmask/gateway
// information from the user.
void ProvisioningEditor::ipv4Address()
{
// variables
QString s = "IPv4 = %1\n";
QString val;
// process string item
shared::ValidatingDialog* vd = new shared::ValidatingDialog(this);
vd->setLabel(tr("IPv4 Address. <br><br>Enter the IPv4 network address in the form xxx.xxx.xxx.xxx"));
vd->setValidator(CMST::ValDialog_IPv4);
if (vd->exec() == QDialog::Accepted && ! vd->getText().isEmpty() ) {
val = vd->getText();
vd->clear();
vd->setLabel(tr("IPv4 Netmask. <br><br>The entry can be a mask length (example 24) or in the form xxx.xxx.xxx.xxx"));
vd->setValidator(CMST::ValDialog_nmask4);
if (vd->exec() == QDialog::Accepted && ! vd->getText().isEmpty() ) {
val.append("/" + vd->getText() );
vd->clear();
vd->setLabel(tr("IPv4 Gateway.<br><br>This is an optional entry, press cancel if there is no entry for gateway"));
vd->setValidator(CMST::ValDialog_IPv4);
if (vd->exec() == QDialog::Accepted && ! vd->getText().isEmpty() ) {
val.append("/" + vd->getText() );
} // if gateway accpted
ui.plainTextEdit_main->insertPlainText(s.arg(val) );
} // if netmask accepted
} // if address accepted
vd->deleteLater();
return;
}
//
// Not a real slot anymore as we only call this directly when we need to get ipv6 address/prefix/gateway
// information from the user.
void ProvisioningEditor::ipv6Address()
{
// variables
QString s = "IPv6 = %1\n";
bool ok;
QString val;
shared::ValidatingDialog* vd = new shared::ValidatingDialog(this);
vd->setLabel(tr("IPv6 Address"));
vd->setValidator(CMST::ValDialog_IPv6);
if (vd->exec() == QDialog::Accepted && ! vd->getText().isEmpty() ) {
val = vd->getText();
int i = QInputDialog::getInt(this,
tr("%1 - Integer Input").arg(TranslateStrings::cmtr("cmst")),
tr("Enter the IPv6 prefix length"),
0, 0, 255, 1,
&ok);
if (ok) {
val.append(QString("/%1").arg(i) );
shared::ValidatingDialog* vd = new shared::ValidatingDialog(this);
vd->setLabel(tr("IPv6 Gateway .<br><br>This is an optional entry, press cancel if there is no entry for gateway"));
vd->setValidator(CMST::ValDialog_IPv6);
if (vd->exec() == QDialog::Accepted && ! vd->getText().isEmpty() ) {
val.append(QString("/" + vd->getText()) );
} // if gateway was accepted
ui.plainTextEdit_main->insertPlainText(s.arg(val) );
} // if prefix provided
} // if address accepted
vd->deleteLater();
return;
}
//
// Slot called when a member of the QActionGroup group_template is triggered
void ProvisioningEditor::templateTriggered(QAction* act)
{
// variable
QString source;
// get the source string depending on the action
if (act == ui.actionTemplateEduroamLong) source = ":/text/text/eduroam_long.txt";
else if (act == ui.actionTemplateEduroamShort) source = ":/text/text/eduroam_short.txt";
else if (act == ui.actionTemplateeap_peap) source = ":/text/text/eap-peap.txt";
else if (act == ui.actionTemplateeap_tls) source = ":/text/text/eap-tls.txt";
else if (act == ui.actionTemplateeap_ttls) source = ":/text/text/eap-ttls.txt";
else return;
// get the text
QFile file(source);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QByteArray ba = file.readAll();
// append the template to the textedit
ui.plainTextEdit_main->appendPlainText(QString(ba) );
} // if
return;
}
//
// Slot to enter whats this mode
// Called when the ui.toolButton_whatsthis clicked() signal is emitted
void ProvisioningEditor::showWhatsThis()
{
QWhatsThis::enterWhatsThisMode();
}
//
// Function to clear the contents of the textedit
void ProvisioningEditor::resetPage()
{
ui.plainTextEdit_main->document()->clear();
return;
}
//
// Slot to request a file list from the roothelper.
// Roothelper will emit an obtainedFileList signal when finished. This slot
// is connected to the QButtonGroup bg01
void ProvisioningEditor::requestFileList(QAbstractButton* button)
{
// initialize the selection
if (button == ui.pushButton_open) i_sel = CMST::ProvEd_File_Read;
else if (button == ui.pushButton_save) i_sel = CMST::ProvEd_File_Write;
else if (button == ui.pushButton_delete) i_sel = CMST::ProvEd_File_Delete;
else i_sel = CMST::ProvEd_No_Selection;
// request a list of config files from roothelper
QList<QVariant> vlist;
vlist << QVariant::fromValue(con_path);
QDBusInterface* iface_rfl = new QDBusInterface("org.cmst.roothelper", "/", "org.cmst.roothelper", QDBusConnection::systemBus(), this);
iface_rfl->callWithCallback(QLatin1String("getFileList"), vlist, this, SLOT(processFileList(const QStringList&)), SLOT(callbackErrorHandler(QDBusError)));
iface_rfl->deleteLater();
return;
}
//
// Slot to process the file list from /var/lib/connman. Connected to
// the callWithCallback signal in requestFileList()
void ProvisioningEditor::processFileList(const QStringList& sl_conf)
{
// variables
QString filename = "";
QList<QVariant> vlist;
QDBusInterface* iface_pfl = new QDBusInterface("org.cmst.roothelper", "/", "org.cmst.roothelper", QDBusConnection::systemBus(), this);
QInputDialog* qid = new QInputDialog(this);
qid->setOption(QInputDialog::UseListViewForComboBoxItems);
qid->setWindowModality(Qt::WindowModality::ApplicationModal);
qid->setInputMode(QInputDialog::TextInput);
// If we are trying to open and read the file
if (i_sel & CMST::ProvEd_File_Read) {
// display dialogs based on the length of the stringlist
switch (sl_conf.size()) {
case 0:
QMessageBox::information(this,
QString(TranslateStrings::cmtr("cmst")) + tr(" Information"),
tr("<center>No configuration files were found.<br>You may use this dialog to create one."),
QMessageBox::Ok,
QMessageBox::Ok);
break;
case 1:
filename = sl_conf.at(0);
break;
default:
qid->setWindowTitle(tr("%1 - Select File").arg(TranslateStrings::cmtr("cmst")) );
qid->setLabelText(tr("Select a file to load.") );
qid->setComboBoxItems(sl_conf);
qid->exec();
if (qid->result() == QDialog::Accepted)
filename = qid->textValue();
break;
} // switch
// if we have a filename try to open the file
if (! filename.isEmpty() ) {
vlist.clear();
vlist << QVariant::fromValue(con_path);
vlist << QVariant::fromValue(filename);
iface_pfl->callWithCallback(QLatin1String("readFile"), vlist, this, SLOT(seedTextEdit(const QString&)), SLOT(callbackErrorHandler(QDBusError)));
} // if there is a file name
} // if i_sel is File_Read
// If we are trying to delete the file
else if (i_sel & CMST::ProvEd_File_Delete) {
// user will have to select the file to delete it
switch (sl_conf.size()) {
case 0:
QMessageBox::information(this,
QString(TranslateStrings::cmtr("cmst")) + tr(" Information"),
tr("<center>No configuration files were found.<br>Nothing will be deleted."),
QMessageBox::Ok,
QMessageBox::Ok);
break;
default:
qid->setWindowTitle(tr("%1 - Select File").arg(TranslateStrings::cmtr("cmst")) );
qid->setLabelText(tr("Select a file to be deleted.") );
qid->setComboBoxItems(sl_conf);
qid->exec();
if (qid->result() == QDialog::Accepted)
filename = qid->textValue();
break;
} // switch
// if we have a filename try to delete the file
if (! filename.isEmpty() ) {
vlist.clear();
vlist << QVariant::fromValue(con_path);
vlist << QVariant::fromValue(filename);
iface_pfl->callWithCallback(QLatin1String("deleteFile"), vlist, this, SLOT(deleteCompleted(bool)), SLOT(callbackErrorHandler(QDBusError)));
} // if there is a file name
} // if i_sel is File_Delete
// If we are trying to save the file
else if (i_sel & CMST::ProvEd_File_Write) {
qid->setWindowTitle(tr("%1 - Select File").arg(TranslateStrings::cmtr("cmst")) );
qid->setLabelText(tr("Enter a new file name or select<br>an existing file to overwrite.") );
qid->setComboBoxEditable(true);
qid->setComboBoxItems(sl_conf);
qid->exec();
if (qid->result() == QDialog::Accepted) {
filename = qid->textValue();
filename = filename.simplified(); // multiple whitespace to one space
filename = filename.replace(' ', '_'); // replace spaces with underscores
} // if accepted
// if we have a filename try to save the file
if (! filename.isEmpty() ) {
vlist.clear();
vlist<< QVariant::fromValue(con_path);
vlist << QVariant::fromValue(filename);
vlist << QVariant::fromValue(ui.plainTextEdit_main->toPlainText() );
iface_pfl->callWithCallback(QLatin1String("saveFile"), vlist, this, SLOT(writeCompleted(qint64)), SLOT(callbackErrorHandler(QDBusError)));
} // if there is a file namk
} // if i_sel is File_Save
// cleanup
i_sel = CMST::ProvEd_No_Selection;
iface_pfl->deleteLater();
delete qid;
return;
}
//
// Slot to seed the QTextEdit window with data read from file. Connected to
// fileReadCompleted signal in root helper.
void ProvisioningEditor::seedTextEdit(const QString& data)
{
// clear the text edit and seed it with the read data
ui.plainTextEdit_main->document()->clear();
ui.plainTextEdit_main->setPlainText(data);
// show a statusbar message
statusbar->showMessage(tr("File read completed"), statustimeout);
return;
}
//
// Slot to show a statusbar message when a file delete is completed
void ProvisioningEditor::deleteCompleted(bool success)
{
QString msg;
if (success)
msg = tr("File deleted");
else
msg = tr("Error encountered deleting.");
statusbar->showMessage(msg, statustimeout);
return;
}
//
// Slot to show a statusbar message when a file write is completed
void ProvisioningEditor::writeCompleted(qint64 bytes)
{
QString msg = QString();
if (bytes < 0 )
msg = tr("File save failed.");
else {
if (bytes > 1024)
msg = tr("%L1 KB written").arg(bytes / 1024);
else
msg = tr("%L1 Bytes written").arg(bytes);
}
statusbar->showMessage(msg, statustimeout);
return;
}
//
// Slot to handle errors from callWithCallback functions
void ProvisioningEditor::callbackErrorHandler(QDBusError err)
{
QMessageBox::critical(this,
QString(TranslateStrings::cmtr("cmst")) + tr(" Critical"),
QString(tr("<b>DBus Error Name:</b> %1<br><br><b>String:</b> %2<br><br><b>Message:</b> %3")).arg(err.name()).arg(err.errorString(err.type())).arg(TranslateStrings::cmtr(err.message()) ),
QMessageBox::Ok,
QMessageBox::Ok);
return;
}
|