File: agent_dialog.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 (307 lines) | stat: -rw-r--r-- 11,749 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
/****************** agent_dialog.cpp *********************************** 

Code to manage the agent user interface.  When the connman daemon
needs to communicate with the user it does so through the agent.  The
agent has a QDialog as a class member, and agent_dialog.cpp manages
that 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 <QList>
#	include <QWhatsThis>
# include <QProcess>
# include <QProcessEnvironment>
# include <QDir>
# include <QMessageBox>
# include <QStringListModel>

# include "./agent_dialog.h"
# include "./code/trstring/tr_strings.h"

AgentDialog::AgentDialog(QWidget* parent)
    : QDialog(parent)
{
	// setup the user interface
  ui.setupUi(this);
  	  
  // data members
  cli_browsers.clear();	  
 	cli_browsers << "lynx" << "w3m" << "links" << "elinks";
 	gui_browsers.clear();
	gui_browsers << "google-chrome" << "google-chrome-unstable" << "chromium" << "opera" << "brave";			// blink based 	  
	gui_browsers << "firefox" << "seamonkey";																															// gecko based 	  
	gui_browsers << "qupzilla" << "luakit" << "dwb" << "jumanji" << "midoir" << "surf" << "vimprobable";	// webkit based 	  
	gui_browsers << "konqueror" << "dillo" << "xdg-open";																									// other	 	  
  	  
  // connect signals to slots
	connect(ui.toolButton_whatsthis, SIGNAL(clicked()), this, SLOT(showWhatsThis()));  
  connect (ui.checkBox_hide_passphrase, SIGNAL(clicked(bool)), this, SLOT(hidePassphrase(bool)));
  connect (ui.checkBox_wps_no_pin, SIGNAL(clicked(bool)), this, SLOT(useWPSPushButton(bool)));  
  connect (ui.pushButton_launch_browser,SIGNAL(clicked()), this, SLOT(launchBrowser())); 
  connect (ui.lineEdit_browser, SIGNAL(textEdited(const QString&)), this, SLOT(enteringBrowser()));
  
	// find the PATH of the current environment
	sys_env_path = QString();
	QProcessEnvironment se = QProcessEnvironment::systemEnvironment();
	if (se.keys().contains("PATH")) {
		sys_env_path = se.toStringList().at(se.keys().indexOf("PATH"));
		sys_env_path.remove("PATH=");
	}  		  
}    

///////////////////////////////////////////////// Public Functions /////////////////////////////////////////////
//
//	Function to show page 0 of the stackWidget
//	imap - is map of QStrings with input keys that connman has requested the user to fill in, and any values
//	that it has sent back for informational purposes.  Return the accept state of the dialog
int AgentDialog::showPage0(const QMap<QString,QString>& imap)
{
	// set all input widgets to disabled
	this->initialize();
	
	// turn on the boxes that need to be filled in
	if (imap.contains("Passphrase")) {
		ui.lineEdit_passphrase->setEnabled(true);
		ui.lineEdit_passphrase->setText(imap.value("Passphrase") );
		ui.checkBox_hide_passphrase->setEnabled(true);
	} 
	if (imap.contains("PreviousPassphrase")) {
		ui.lineEdit_old_passphrase->setEnabled(true);
		ui.lineEdit_old_passphrase->setText(imap.value("PreviousPassphrase") );
	}
	if (imap.contains("SSID")) {
		ui.lineEdit_ssid->setEnabled(true);
		ui.lineEdit_ssid->setText(imap.value("SSID") );
	}
	if (imap.contains("Name")) {
		ui.lineEdit_hidden_name->setEnabled(true); 
		ui.lineEdit_hidden_name->setText(imap.value("Name") );
	}		
	if (imap.contains("Userame")) {
		ui.lineEdit_wispr_username->setEnabled(true); 
		ui.lineEdit_wispr_username->setText(imap.value("Username") );
	}
	if (imap.contains("Password")) {
		ui.lineEdit_wispr_password->setEnabled(true);
		ui.lineEdit_wispr_password->setText(imap.value("Password") );
	}
	if (imap.contains("Identity")) {
		ui.lineEdit_eap_identity->setEnabled(true); 	
		ui.lineEdit_eap_identity->setText(imap.value("Identity") );
	}		
	if (imap.contains("WPS")) {
		ui.lineEdit_wps_pin->setEnabled(true);
		ui.lineEdit_wps_pin->setText(imap.value("WPS") );
		ui.checkBox_wps_no_pin->setEnabled(true);
	} 	
	
	this->ui.stackedWidget->setCurrentIndex(0);
	return this->exec();
}

//
//	Function to show page 1 of the stackWidget
//	url - is the url that the user needs to open
//	Return the accept state of the dialog
int AgentDialog::showPage1(const QString& url)
{
	// set all input widgets to disabled
	this->initialize();	
	
	// show the url connman is requesting we connect to
	ui.lineEdit_url->setText(url);
	
	// find browser executables
	QStringList sl_browsers;	
	if (! sys_env_path.isEmpty() ) {
		QStringList sl_loop = sys_env_path.split(':');
		QStringList sl_targets = cli_browsers;
		sl_targets.append(gui_browsers);
		sl_browsers.clear();
		for (int i = 0; i < sl_loop.size(); ++i) {
			QDir dir = QDir(sl_loop.at(i));
			sl_browsers.append(dir.entryList(sl_targets, QDir::Files|QDir::Executable));
		}	// for dir in path
	}	// if path not empty	
	
	// add found browsers to the listView
	ui.listView_browsers->setModel(new QStringListModel(sl_browsers));
	connect (ui.listView_browsers->selectionModel(), SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(updateBrowserChoice(const QModelIndex&, const QModelIndex&)));
	ui.listView_browsers->setEnabled(true);
	
	this->ui.stackedWidget->setCurrentIndex(1);
	return this->exec();	
}

//
//	Function to extract the input data from the QLineEdit's in the dialog and put it into
//	a QMap<QString,QVariant>.  The QMap is sent to this function as a reference and is modified by the function.
void AgentDialog::createDict(QMap<QString,QVariant>& r_map)
{
	// Initialize the map
	r_map.clear();
	
	// Create the dict entries
	if (! ui.lineEdit_passphrase->text().isEmpty() ) 
		r_map["Passphrase"] = ui.lineEdit_passphrase->text();
	
	if (! ui.lineEdit_ssid->text().isEmpty() )
		r_map["SSID"] = ui.lineEdit_ssid->text().toLatin1();	
	
	if (! ui.lineEdit_hidden_name->text().isEmpty() )
		r_map["Name"] = ui.lineEdit_hidden_name->text();
	
	if (! ui.lineEdit_wispr_username->text().isEmpty() )
		r_map["Username"] = ui.lineEdit_wispr_username->text();
	
	if (! ui.lineEdit_wispr_password->text().isEmpty() )
		r_map["Password"] = ui.lineEdit_wispr_password->text();
	
	if (! ui.lineEdit_eap_identity->text().isEmpty() )
		r_map["Identity"] = ui.lineEdit_eap_identity->text();
	
	if (! ui.lineEdit_wps_pin->text().isEmpty() )
		r_map["WPS"] = ui.lineEdit_wps_pin->text();
	
	return;
}
///////////////////////////////////////////////// Private Functions /////////////////////////////////////////////
//
//	Function to initialize the fields in the dialog box, everything is disabled to start
void AgentDialog::initialize()
{
	// QList of widget pointers
	QList <QWidget*> list;
	list.clear();
	list.append(ui.lineEdit_passphrase);
	list.append(ui.lineEdit_old_passphrase);
	list.append(ui.checkBox_hide_passphrase);	
	list.append(ui.lineEdit_ssid);
	list.append(ui.lineEdit_hidden_name);
	list.append(ui.lineEdit_wispr_username);
	list.append(ui.lineEdit_wispr_password);
	list.append(ui.lineEdit_eap_identity);
	list.append(ui.lineEdit_wps_pin);
	list.append(ui.checkBox_wps_no_pin);	
	list.append(ui.listView_browsers);
	
	//	set disabled true for all widgets in the list and clear contents
	for (int i = 0; i < list.size(); ++i) {
		list.at(i)->setDisabled(true);
		if (qobject_cast<QLineEdit *> (list.at(i)) != NULL ) qobject_cast<QLineEdit *> (list.at(i))->clear();
		if (qobject_cast<QAbstractButton *> (list.at(i)) != NULL ) qobject_cast<QAbstractButton *> (list.at(i))->setChecked(false);
	}
}

///////////////////////////////////////////////// Private Slots /////////////////////////////////////////////
//
//	Slot to show or obscure the passphrase
void AgentDialog::hidePassphrase(bool checked)
{
	checked ? ui.lineEdit_passphrase->setEchoMode(QLineEdit::Password) : ui.lineEdit_passphrase->setEchoMode(QLineEdit::Normal);
}

//
//	Slot to use WPS PushButton Authentication
//	Called when ui.checkBox_wps_no_pin is checked or unchecked
void AgentDialog::useWPSPushButton(bool checked)
{
	if (checked) ui.lineEdit_wps_pin->clear(); 
	ui.lineEdit_wps_pin->setDisabled(checked);	
}

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

//
//  Slot to set browser as the chosen item in the list
//  Called when ui.listView_browsers selectionChanged() signal is emitted
//
void AgentDialog::updateBrowserChoice(const QModelIndex & current, const QModelIndex & previous)
{
	(void) previous;
	
	ui.lineEdit_browser->setText(current.data().toString());
}

//
//  Slot to clear selection in the browser list view
//  Called when ui.lineEdit_browser textEdited() signal is emitted
void AgentDialog::enteringBrowser()
{
	ui.listView_browsers->selectionModel()->clearSelection();
}

//
//	Slot to launch the selected browser
//	Called when ui.pushButton_launch_browser is pressed
void AgentDialog::launchBrowser()
{
	// variables
	QProcess* process = new QProcess(this);

	// find installed terminals that we can deal with
	QStringList sl_terminals = QStringList();	
	if (! sys_env_path.isEmpty() ) {
		QStringList sl_loop = sys_env_path.split(':');
		QStringList sl_targets;
		sl_targets << "roxterm" << "xterm";
		for (int i = 0; i < sl_loop.size(); ++i) {
			QDir dir = QDir(sl_loop.at(i));
			sl_terminals.append(dir.entryList(sl_targets, QDir::Files|QDir::Executable));
		}	// for dir in path
	}	// if path not empty	
		
	// Code below assumes that every browser will take a URL as an argument	
	QString chosenBrowser = ui.lineEdit_browser->text();
	if (cli_browsers.contains(chosenBrowser, Qt::CaseSensitive) ) {
		// Support 2 terminals, roxterm and xterm. This probably won't work right if
		// someone uses XDG-OPEN and it happens to point to a cli browser
		QStringList sl_args = QStringList();
		if (sl_terminals.contains("roxterm")) {
			sl_args << "-T" << "Web Login" << "--execute" << chosenBrowser << ui.lineEdit_url->text();
			 process->startDetached(QString("roxterm"), sl_args);
		 }
		 else if (sl_terminals.contains("xterm")) {
			 sl_args << "-T"  << "Web Login" << "-e" << chosenBrowser << ui.lineEdit_url->text();
			 process->startDetached(QString("xterm"), sl_args);
		 }
		 else
		 	QMessageBox::information(qobject_cast<QWidget *> (parent()),
		 	QString(TranslateStrings::cmtr("cmst")) + tr(" Information"),
				tr("You have requested the %1 browser, but we cannot find a terminal program to open it with.  "	\
				"Currenty we can start %1 using these terminals: <b>roxterm</b> and <b>xterm</b>." \
				"<br><br>To continue you need to manually open a terminal and then enter: \"%1 %2\"").arg(chosenBrowser).arg(ui.lineEdit_url->text()) );
	} else {
		process->startDetached(chosenBrowser, QStringList(ui.lineEdit_url->text()) );	
	}
	
	return;
}