2
* proxy.cpp - classes for handling proxy profiles
3
* Copyright (C) 2003 Justin Karneges
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
#include<qvgroupbox.h>
32
#include<qpushbutton.h>
35
#include<qguardedptr.h>
36
#include<qwhatsthis.h>
37
#include<qapplication.h>
39
#include"iconwidget.h"
41
static QDomElement textTag(QDomDocument &doc, const QString &name, const QString &content)
43
QDomElement tag = doc.createElement(name);
44
QDomText text = doc.createTextNode(content);
45
tag.appendChild(text);
50
static QDomElement textTag(QDomDocument &doc, const QString &name, bool content)
52
QDomElement tag = doc.createElement(name);
53
QDomText text = doc.createTextNode(content ? "true" : "false");
54
tag.appendChild(text);
59
static QDomElement findSubTag(const QDomElement &e, const QString &name)
61
for(QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
62
QDomElement i = n.toElement();
65
if(i.tagName() == name)
72
//----------------------------------------------------------------------------
74
//----------------------------------------------------------------------------
75
class HostPortEdit::Private
80
QLineEdit *le_host, *le_port;
83
HostPortEdit::HostPortEdit(QWidget *parent, const char *name)
84
:QWidget(parent, name)
89
QHBoxLayout *hb = new QHBoxLayout(this, 0, 4);
90
l = new QLabel(tr("Host:"), this);
92
d->le_host = new QLineEdit(this);
93
d->le_host->setMinimumWidth(128);
94
hb->addWidget(d->le_host);
95
l = new QLabel(tr("Port:"), this);
97
d->le_port = new QLineEdit(this);
98
d->le_port->setFixedWidth(64);
99
hb->addWidget(d->le_port);
103
HostPortEdit::~HostPortEdit()
108
QString HostPortEdit::host() const
110
return d->le_host->text();
113
int HostPortEdit::port() const
115
return d->le_port->text().toInt();
118
void HostPortEdit::setHost(const QString &s)
120
d->le_host->setText(s);
123
void HostPortEdit::setPort(int x)
125
d->le_port->setText(QString::number(x));
128
void HostPortEdit::fixTabbing(QWidget *a, QWidget *b)
130
setTabOrder(a, d->le_host);
131
setTabOrder(d->le_host, d->le_port);
132
setTabOrder(d->le_port, b);
136
//----------------------------------------------------------------------------
138
//----------------------------------------------------------------------------
139
ProxySettings::ProxySettings()
145
QDomElement ProxySettings::toXml(QDomDocument *doc) const
147
QDomElement e = doc->createElement("proxySettings");
148
e.appendChild(textTag(*doc, "host", host));
149
e.appendChild(textTag(*doc, "port", QString::number(port)));
150
e.appendChild(textTag(*doc, "url", url));
151
e.appendChild(textTag(*doc, "useAuth", useAuth));
152
e.appendChild(textTag(*doc, "user", user));
153
e.appendChild(textTag(*doc, "pass", pass));
157
bool ProxySettings::fromXml(const QDomElement &e)
159
host = findSubTag(e, "host").text();
160
port = findSubTag(e, "port").text().toInt();
161
url = findSubTag(e, "url").text();
162
useAuth = (findSubTag(e, "useAuth").text() == "true") ? true: false;
163
user = findSubTag(e, "user").text();
164
pass = findSubTag(e, "pass").text();
169
//----------------------------------------------------------------------------
171
//----------------------------------------------------------------------------
172
class ProxyEdit::Private
177
HostPortEdit *hp_host;
180
QLineEdit *le_url, *le_user, *le_pass;
184
ProxyEdit::ProxyEdit(QWidget *parent, const char *name)
185
:QGroupBox(1, Horizontal, tr("Settings"), parent, name)
189
//QVBoxLayout *vb = new QVBoxLayout(this);
190
//QVBox *gb = new QVBox(this);
196
d->hp_host = new HostPortEdit(this);
198
QHBox *hb = new QHBox(this);
199
d->lb_url = new QLabel(tr("Polling URL:"), hb);
200
d->le_url = new QLineEdit(hb);
202
d->ck_auth = new QCheckBox(tr("Use authentication"), this);
203
connect(d->ck_auth, SIGNAL(toggled(bool)), SLOT(ck_toggled(bool)));
205
d->gr_auth = new QGrid(2, Horizontal, this);
206
d->gr_auth->setSpacing(4);
207
l = new QLabel(tr("Username:"), d->gr_auth);
208
d->le_user = new QLineEdit(d->gr_auth);
209
l = new QLabel(tr("Password:"), d->gr_auth);
210
d->le_pass = new QLineEdit(d->gr_auth);
211
d->le_pass->setEchoMode(QLineEdit::Password);
212
d->gr_auth->setEnabled(false);
214
QWhatsThis::add(d->hp_host,
215
tr("Enter the hostname and port of your proxy server.") + " " +
216
tr("Consult your network administrator if necessary."));
217
QWhatsThis::add(d->le_user,
218
tr("Enter your proxy server login (username) "
219
"or leave this field blank if the proxy server does not require it.") + " " +
220
tr("Consult your network administrator if necessary."));
221
QWhatsThis::add(d->le_pass,
222
tr("Enter your proxy server password "
223
"or leave this field blank if the proxy server does not require it.") + " " +
224
tr("Consult your network administrator if necessary."));
227
ProxyEdit::~ProxyEdit()
232
void ProxyEdit::reset()
234
d->hp_host->setHost("");
235
d->hp_host->setPort(0);
236
d->le_url->setText("");
237
d->ck_auth->setChecked(false);
238
d->le_user->setText("");
239
d->le_pass->setText("");
242
void ProxyEdit::setType(const QString &s)
245
d->lb_url->setEnabled(true);
246
d->le_url->setEnabled(true);
249
d->lb_url->setEnabled(false);
250
d->le_url->setEnabled(false);
254
ProxySettings ProxyEdit::proxySettings() const
257
s.host = d->hp_host->host();
258
s.port = d->hp_host->port();
259
if(d->le_url->isEnabled())
260
s.url = d->le_url->text();
261
s.useAuth = d->ck_auth->isChecked();
262
s.user = d->le_user->text();
263
s.pass = d->le_pass->text();
267
void ProxyEdit::setProxySettings(const ProxySettings &s)
269
d->hp_host->setHost(s.host);
270
d->hp_host->setPort(s.port);
271
d->le_url->setText(s.url);
272
d->ck_auth->setChecked(s.useAuth);
273
d->le_user->setText(s.user);
274
d->le_pass->setText(s.pass);
277
void ProxyEdit::ck_toggled(bool b)
279
d->gr_auth->setEnabled(b);
282
void ProxyEdit::fixTabbing(QWidget *a, QWidget *b)
284
d->hp_host->fixTabbing(a, d->le_url);
285
setTabOrder(d->le_url, d->ck_auth);
286
setTabOrder(d->ck_auth, d->le_user);
287
setTabOrder(d->le_user, d->le_pass);
288
setTabOrder(d->le_pass, b);
292
//----------------------------------------------------------------------------
294
//----------------------------------------------------------------------------
295
class ProxyDlg::Private
300
ProxyEdit *pe_settings;
305
ProxyDlg::ProxyDlg(const ProxyItemList &list, const QStringList &methods, int def, QWidget *parent, const char *name)
306
:ProxyUI(parent, name, QApplication::activeModalWidget() ? true: false, WDestructiveClose)
309
setCaption(CAP(caption()));
313
d->pe_settings = new ProxyEdit(gb_prop);
314
replaceWidget(lb_proxyedit, d->pe_settings);
315
d->pe_settings->fixTabbing(cb_type, pb_close);
319
connect(pb_new, SIGNAL(clicked()), SLOT(proxy_new()));
320
connect(pb_remove, SIGNAL(clicked()), SLOT(proxy_remove()));
321
connect(pb_save, SIGNAL(clicked()), SLOT(doSave()));
322
connect(pb_close, SIGNAL(clicked()), SLOT(reject()));
323
gb_prop->setEnabled(false);
324
pb_remove->setEnabled(false);
325
pb_save->setDefault(true);
327
cb_type->insertStringList(methods);
328
connect(cb_type, SIGNAL(activated(int)), SLOT(cb_activated(int)));
330
for(ProxyItemList::ConstIterator it = d->list.begin(); it != d->list.end(); ++it)
331
lbx_proxy->insertItem((*it).name);
332
if(!list.isEmpty()) {
335
lbx_proxy->setCurrentItem(def);
339
QWhatsThis::add(cb_type,
340
tr("If you require a proxy server to connect, select the type of proxy here.") + " " +
341
tr("Consult your network administrator if necessary."));
342
// TODO: whatsthis for name
345
ProxyDlg::~ProxyDlg()
350
void ProxyDlg::proxy_new()
353
s.id = -1; // id of -1 means 'new'
354
s.name = getUniqueName();
357
lbx_proxy->insertItem(s.name);
358
lbx_proxy->setCurrentItem(lbx_proxy->count()-1);
362
void ProxyDlg::proxy_remove()
364
int x = lbx_proxy->currentItem();
366
ProxyItemList::Iterator it = d->list.begin();
367
for(int n = 0; n < x; ++n)
372
lbx_proxy->removeItem(x);
377
void ProxyDlg::cb_activated(int x)
380
d->pe_settings->setType("http");
382
d->pe_settings->setType("socks");
384
d->pe_settings->setType("poll");
387
void ProxyDlg::selectCurrent()
389
int x = lbx_proxy->currentItem();
391
lbx_proxy->setSelected(x, true);
394
QString ProxyDlg::getUniqueName() const
400
str = QString("Proxy %1").arg(++x);
402
for(ProxyItemList::ConstIterator it = d->list.begin(); it != d->list.end(); ++it) {
403
if(str == (*it).name) {
412
void ProxyDlg::saveIntoItem(int x)
414
ProxyItem &s = d->list[x];
415
s.name = le_name->text();
416
int i = cb_type->currentItem();
424
s.settings = d->pe_settings->proxySettings();
427
void ProxyDlg::qlbx_highlighted(int x)
429
// if we are moving off of another item, save its content
431
saveIntoItem(d->last);
434
// display the new item's content
436
le_name->setText("");
437
cb_type->setCurrentItem(0);
438
d->pe_settings->reset();
439
gb_prop->setEnabled(false);
440
pb_remove->setEnabled(false);
443
ProxyItem &s = d->list[x];
444
le_name->setText(s.name);
448
else if(s.type == "socks")
450
else if(s.type == "poll")
452
cb_type->setCurrentItem(i);
453
d->pe_settings->setProxySettings(s.settings);
455
gb_prop->setEnabled(true);
456
pb_remove->setEnabled(true);
460
void ProxyDlg::qle_textChanged(const QString &s)
462
int x = lbx_proxy->currentItem();
465
lbx_proxy->changeItem(s, x);
470
// hookEdit / unhookEdit - disconnect some signals to prevent recursion
471
void ProxyDlg::hookEdit()
473
connect(lbx_proxy, SIGNAL(highlighted(int)), this, SLOT(qlbx_highlighted(int)));
474
connect(le_name, SIGNAL(textChanged(const QString &)), this, SLOT(qle_textChanged(const QString &)));
477
void ProxyDlg::unhookEdit()
479
disconnect(lbx_proxy, SIGNAL(highlighted(int)), this, SLOT(qlbx_highlighted(int)));
480
disconnect(le_name, SIGNAL(textChanged(const QString &)), this, SLOT(qle_textChanged(const QString &)));
483
void ProxyDlg::doSave()
485
int x = lbx_proxy->currentItem();
488
applyList(d->list, x);
493
//----------------------------------------------------------------------------
495
//----------------------------------------------------------------------------
496
class ProxyChooser::Private
502
QPushButton *pb_edit;
506
ProxyChooser::ProxyChooser(ProxyManager *m, QWidget *parent, const char *name)
507
:QWidget(parent, name)
511
connect(m, SIGNAL(settingsChanged()), SLOT(pm_settingsChanged()));
512
QHBoxLayout *hb = new QHBoxLayout(this, 0, 4);
513
d->cb_proxy = new QComboBox(this);
514
QSizePolicy sp = d->cb_proxy->sizePolicy();
515
d->cb_proxy->setSizePolicy( QSizePolicy(QSizePolicy::Expanding, sp.verData()) );
516
hb->addWidget(d->cb_proxy);
517
d->pb_edit = new QPushButton(tr("Edit..."), this);
518
connect(d->pb_edit, SIGNAL(clicked()), SLOT(doOpen()));
519
hb->addWidget(d->pb_edit);
524
ProxyChooser::~ProxyChooser()
529
int ProxyChooser::currentItem() const
531
return d->cb_proxy->currentItem();
534
void ProxyChooser::setCurrentItem(int x)
536
d->cb_proxy->setCurrentItem(x);
539
void ProxyChooser::pm_settingsChanged()
541
int x = d->cb_proxy->currentItem();
544
x = d->m->findOldIndex(x-1);
546
d->cb_proxy->setCurrentItem(0);
548
d->cb_proxy->setCurrentItem(x+1);
551
x = d->m->lastEdited();
553
d->cb_proxy->setCurrentItem(x+1);
557
void ProxyChooser::buildComboBox()
559
d->cb_proxy->clear();
560
d->cb_proxy->insertItem(tr("None"));
561
ProxyItemList list = d->m->itemList();
562
for(ProxyItemList::ConstIterator it = list.begin(); it != list.end(); ++it)
563
d->cb_proxy->insertItem((*it).name);
566
void ProxyChooser::doOpen()
568
int x = d->cb_proxy->currentItem();
576
void ProxyChooser::fixTabbing(QWidget *a, QWidget *b)
578
setTabOrder(a, d->cb_proxy);
579
setTabOrder(d->cb_proxy, d->pb_edit);
580
setTabOrder(d->pb_edit, b);
584
//----------------------------------------------------------------------------
586
//----------------------------------------------------------------------------
587
class ProxyManager::Private
593
QGuardedPtr<ProxyDlg> pd;
594
QValueList<int> prevMap;
598
ProxyManager::ProxyManager(QObject *parent)
604
ProxyManager::~ProxyManager()
609
ProxyChooser *ProxyManager::createProxyChooser(QWidget *parent)
611
return new ProxyChooser(this, parent);
614
ProxyItemList ProxyManager::itemList() const
619
const ProxyItem & ProxyManager::getItem(int x) const
624
int ProxyManager::lastEdited() const
626
return d->lastEdited;
629
void ProxyManager::setItemList(const ProxyItemList &list)
635
QStringList ProxyManager::methodList() const
638
list += "HTTP \"Connect\"";
639
list += "SOCKS Version 5";
640
list += "HTTP Polling";
644
void ProxyManager::openDialog(int def)
649
d->pd = new ProxyDlg(d->list, methodList(), def, 0);
650
connect(d->pd, SIGNAL(applyList(const ProxyItemList &, int)), SLOT(pd_applyList(const ProxyItemList &, int)));
655
void ProxyManager::pd_applyList(const ProxyItemList &list, int x)
662
for(ProxyItemList::ConstIterator it = d->list.begin(); it != d->list.end(); ++it)
663
d->prevMap += (*it).id;
664
assignIds(); // re-assign proper ids
669
void ProxyManager::assignIds()
672
for(ProxyItemList::Iterator it = d->list.begin(); it != d->list.end(); ++it)
676
int ProxyManager::findOldIndex(int x) const
680
for(QValueList<int>::ConstIterator it = d->prevMap.begin(); it != d->prevMap.end(); ++it) {