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
|
/*
Copyright (C) 2005-2009 Michel de Boer <michel@twinklephone.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <QRegExp>
#include <QLineEdit>
#include <QLabel>
#include <QValidator>
#include <QComboBox>
#include <QTextStream>
#include "gui.h"
#include <QFile>
#include <QRegExpValidator>
#include "wizardform.h"
#define PROV_NONE QT_TRANSLATE_NOOP("WizardForm", "None (direct IP to IP calls)")
#define PROV_OTHER QT_TRANSLATE_NOOP("WizardForm", "Other")
struct t_provider {
QString domain;
QString sip_proxy;
QString stun_server;
};
/*
* Constructs a WizardForm as a child of 'parent', with the
* name 'name' and widget flags set to 'f'.
*
* The dialog will by default be modeless, unless you set 'modal' to
* true to construct a modal dialog.
*/
WizardForm::WizardForm(QWidget* parent)
: QDialog(parent)
{
setupUi(this);
init();
}
/*
* Destroys the object and frees any allocated resources
*/
WizardForm::~WizardForm()
{
// no need to delete child widgets, Qt does it all for us
}
/*
* Sets the strings of the subwidgets using the current
* language.
*/
void WizardForm::languageChange()
{
retranslateUi(this);
}
void WizardForm::init()
{
QRegExp rxNoSpace("\\S*");
// Set validators
usernameLineEdit->setValidator(new QRegExpValidator(rxNoSpace, this));
domainLineEdit->setValidator(new QRegExpValidator(rxNoSpace, this));
authNameLineEdit->setValidator(new QRegExpValidator(rxNoSpace, this));
proxyLineEdit->setValidator(new QRegExpValidator(rxNoSpace, this));
initProviders();
serviceProviderComboBox->setCurrentIndex(serviceProviderComboBox->count() - 1);
update(tr(PROV_OTHER));
}
void WizardForm::initProviders()
{
serviceProviderComboBox->clear();
serviceProviderComboBox->addItem(tr(PROV_NONE));
QString fname = sys_config->get_dir_share().c_str();
fname.append("/").append(FILE_PROVIDERS);
QFile providersFile(fname);
if (providersFile.open(QIODevice::ReadOnly)) {
QTextStream providersStream(&providersFile);
QString entry;
while ((entry = providersStream.readLine()) != QString::null) {
// Skip comment
if (entry[0] == '#') continue;
QStringList l = entry.split(";", QString::KeepEmptyParts);
// Skip invalid lines
if (l.size() != 4) continue;
t_provider p;
p.domain = l[1];
p.sip_proxy = l[2];
p.stun_server = l[3];
mapProviders[l[0]] = p;
serviceProviderComboBox->addItem(l[0]);
}
providersFile.close();
}
serviceProviderComboBox->addItem(tr(PROV_OTHER));
}
int WizardForm::exec(t_user *user)
{
user_config = user;
// Set user profile name in the titlebar
QString s = PRODUCT_NAME;
s.append(" - ").append(tr("User profile wizard:")).append(" ");
s.append(user_config->get_profile_name().c_str());
setWindowTitle(s);
return QDialog::exec();
}
void WizardForm::show(t_user *user)
{
user_config = user;
// Set user profile name in the titlebar
QString s = PRODUCT_NAME;
s.append(" - ").append(tr("User profile wizard:")).append(" ");
s.append(user_config->get_profile_name().c_str());
setWindowTitle(s);
QDialog::show();
}
void WizardForm::update(const QString &item)
{
// Disable/Enable controls
if (item == tr(PROV_NONE)) {
suggestAuthName = false;
authNameTextLabel->setEnabled(false);
authNameLineEdit->setEnabled(false);
authPasswordTextLabel->setEnabled(false);
authPasswordLineEdit->setEnabled(false);
proxyTextLabel->setEnabled(false);
proxyLineEdit->setEnabled(false);
stunServerTextLabel->setEnabled(false);
stunServerLineEdit->setEnabled(false);
} else {
if (usernameLineEdit->text() == authNameLineEdit->text()) {
suggestAuthName = true;
} else {
suggestAuthName = false;
}
authNameTextLabel->setEnabled(true);
authNameLineEdit->setEnabled(true);
authPasswordTextLabel->setEnabled(true);
authPasswordLineEdit->setEnabled(true);
proxyTextLabel->setEnabled(true);
proxyLineEdit->setEnabled(true);
stunServerTextLabel->setEnabled(true);
stunServerLineEdit->setEnabled(true);
}
// Set values
if (item == tr(PROV_NONE)) {
domainLineEdit->clear();
authNameLineEdit->clear();
authPasswordLineEdit->clear();
proxyLineEdit->clear();
stunServerLineEdit->clear();
} else if (item == tr(PROV_OTHER)) {
domainLineEdit->clear();
stunServerLineEdit->clear();
proxyLineEdit->clear();
} else {
t_provider p = mapProviders[item];
domainLineEdit->setText(p.domain);
proxyLineEdit->setText(p.sip_proxy);
stunServerLineEdit->setText(p.stun_server);
}
}
void WizardForm::updateAuthName(const QString &s)
{
if (suggestAuthName) {
authNameLineEdit->setText(s);
}
}
void WizardForm::disableSuggestAuthName()
{
suggestAuthName = false;
}
void WizardForm::validate()
{
QString s;
// Validity check user page
// SIP username is mandatory
if (usernameLineEdit->text().isEmpty()) {
((t_gui *)ui)->cb_show_msg(this, tr("You must fill in a user name for your SIP account.").toStdString(),
MSG_CRITICAL);
usernameLineEdit->setFocus();
return;
}
// SIP user domain is mandatory
if (domainLineEdit->text().isEmpty()) {
((t_gui *)ui)->cb_show_msg(this, tr(
"You must fill in a domain name for your SIP account.\n"
"This could be the hostname or IP address of your PC "
"if you want direct PC to PC dialing.").toStdString(),
MSG_CRITICAL);
domainLineEdit->setFocus();
return;
}
// SIP proxy
if (proxyLineEdit->text() != "") {
s = USER_SCHEME;
s.append(':').append(proxyLineEdit->text());
t_url u(s.toStdString());
if (!u.is_valid() || u.get_user() != "") {
((t_gui *)ui)->cb_show_msg(this, tr("Invalid value for SIP proxy.").toStdString(),
MSG_CRITICAL);
proxyLineEdit->setFocus();
proxyLineEdit->selectAll();
return;
}
}
// Register and publish presence at startup
if (serviceProviderComboBox->currentText() == tr(PROV_NONE)) {
user_config->set_register_at_startup(false);
user_config->set_pres_publish_startup(false);
}
// STUN server
if (stunServerLineEdit->text() != "") {
s = "stun:";
s.append(stunServerLineEdit->text());
t_url u(s.toStdString());
if (!u.is_valid() || u.get_user() != "") {
((t_gui *)ui)->cb_show_msg(this, tr("Invalid value for STUN server.").toStdString(),
MSG_CRITICAL);
stunServerLineEdit->setFocus();
stunServerLineEdit->selectAll();
return;
}
}
// Set all values in the user_config object
// USER
user_config->set_display(displayLineEdit->text().toStdString());
user_config->set_name(usernameLineEdit->text().toStdString());
user_config->set_domain(domainLineEdit->text().toStdString());
user_config->set_auth_name(authNameLineEdit->text().toStdString());
user_config->set_auth_pass(authPasswordLineEdit->text().toStdString());
// SIP SERVER
user_config->set_use_outbound_proxy(!proxyLineEdit->text().isEmpty());
s = USER_SCHEME;
s.append(':').append(proxyLineEdit->text());
user_config->set_outbound_proxy(t_url(s.toStdString()));
// NAT
user_config->set_use_stun(!stunServerLineEdit->text().isEmpty());
s = "stun:";
s.append(stunServerLineEdit->text());
user_config->set_stun_server(t_url(s.toStdString()));
// Save user config
string error_msg;
if (!user_config->write_config(user_config->get_filename(), error_msg)) {
// Failed to write config file
((t_gui *)ui)->cb_show_msg(this, error_msg, MSG_CRITICAL);
return;
}
emit success();
accept();
}
|