2
kmanualproxydlg.cpp - Proxy configuration dialog
4
Copyright (C) 2001-2004 Dawit Alemayehu <adawit@kde.org>
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU General Public
8
License (GPL) version 2 as published by the Free Software
11
This library is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
Library General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this library; see the file COPYING.LIB. If not, write to
18
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
Boston, MA 02110-1301, USA.
23
#include "kmanualproxydlg.h"
26
#include <QtGui/QCheckBox>
27
#include <QtGui/QLabel>
28
#include <QtGui/QLayout>
29
#include <QtGui/QPushButton>
33
#include <kiconloader.h>
34
#include <kicontheme.h>
35
#include <kinputdialog.h>
36
#include <kio/ioslave_defaults.h>
37
#include <klineedit.h>
38
#include <klistwidget.h>
40
#include <kmessagebox.h>
41
#include <knuminput.h>
42
#include <kurifilter.h>
46
KManualProxyDlg::KManualProxyDlg( QWidget* parent, const char* name )
47
:KProxyDialogBase( parent, name, true,
48
i18nc("@title:window", "Manual Proxy Configuration") )
50
mDlg = new ManualProxyDlgUI (this);
52
mDlg->pbCopyDown->setIcon( KIcon("go-down") );
53
QSizePolicy sizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
54
sizePolicy.setHeightForWidth( mDlg->pbCopyDown->sizePolicy().hasHeightForWidth() );
55
mDlg->pbCopyDown->setSizePolicy( sizePolicy );
60
void KManualProxyDlg::init()
62
mDlg->sbHttp->setRange( 0, MAX_PORT_VALUE );
63
mDlg->sbHttps->setRange( 0, MAX_PORT_VALUE );
64
mDlg->sbFtp->setRange( 0, MAX_PORT_VALUE );
66
connect( mDlg->pbNew, SIGNAL( clicked() ), SLOT( newPressed() ) );
67
connect( mDlg->pbChange, SIGNAL( clicked() ), SLOT( changePressed() ) );
68
connect( mDlg->pbDelete, SIGNAL( clicked() ), SLOT( deletePressed() ) );
69
connect( mDlg->pbDeleteAll, SIGNAL( clicked() ), SLOT( deleteAllPressed() ) );
71
connect( mDlg->lbExceptions, SIGNAL(itemSelectionChanged()), SLOT(updateButtons()) );
72
connect( mDlg->lbExceptions, SIGNAL(itemDoubleClicked (QListWidgetItem *)), SLOT(changePressed()));
74
connect( mDlg->cbSameProxy, SIGNAL( toggled(bool) ), SLOT( sameProxy(bool) ) );
75
connect( mDlg->pbCopyDown, SIGNAL( clicked() ), SLOT( copyDown() ) );
77
connect( mDlg->leHttp, SIGNAL(textChanged(const QString&)), SLOT(textChanged(const QString&)) );
78
connect( mDlg->sbHttp, SIGNAL(valueChanged(int)), SLOT(valueChanged (int)) );
80
connect( this, SIGNAL(okClicked()), this, SLOT(slotOk()));
83
void KManualProxyDlg::setProxyData( const KProxyData &data )
87
// Set the HTTP proxy...
88
if (!isValidURL(data.proxyList["http"], &url))
89
mDlg->sbHttp->setValue( DEFAULT_PROXY_PORT );
92
int port = url.port();
94
port = DEFAULT_PROXY_PORT;
97
mDlg->leHttp->setText( url.url() );
98
mDlg->sbHttp->setValue( port );
101
bool useSameProxy = (!mDlg->leHttp->text().isEmpty () &&
102
data.proxyList["http"] == data.proxyList["https"] &&
103
data.proxyList["http"] == data.proxyList["ftp"]);
105
mDlg->cbSameProxy->setChecked ( useSameProxy );
109
mDlg->leHttps->setText ( mDlg->leHttp->text() );
110
mDlg->leFtp->setText ( mDlg->leHttp->text() );
111
mDlg->sbHttps->setValue( mDlg->sbHttp->value() );
112
mDlg->sbFtp->setValue( mDlg->sbHttp->value() );
118
// Set the HTTPS proxy...
119
if( !isValidURL( data.proxyList["https"], &url ) )
120
mDlg->sbHttps->setValue( DEFAULT_PROXY_PORT );
123
int port = url.port();
125
port = DEFAULT_PROXY_PORT;
128
mDlg->leHttps->setText( url.url() );
129
mDlg->sbHttps->setValue( port );
132
// Set the FTP proxy...
133
if( !isValidURL( data.proxyList["ftp"], &url ) )
134
mDlg->sbFtp->setValue( DEFAULT_PROXY_PORT );
137
int port = url.port();
139
port = DEFAULT_PROXY_PORT;
142
mDlg->leFtp->setText( url.url() );
143
mDlg->sbFtp->setValue( port );
147
QStringList::ConstIterator it = data.noProxyFor.begin();
148
for( ; it != data.noProxyFor.end(); ++it )
150
// "no_proxy" is a keyword used by the environment variable
151
// based configuration. We ignore it here as it is not applicable...
152
if ((*it).toLower() != "no_proxy" && !(*it).isEmpty())
154
// Validate the NOPROXYFOR entries and use only hostnames if the entry is
155
// a valid or legitimate URL. NOTE: needed to catch manual manipulation
156
// of the proxy config files...
157
if( isValidURL( *it ) || ((*it).length() >= 3 && (*it).startsWith('.')) )
158
mDlg->lbExceptions->addItem( *it );
162
mDlg->cbReverseProxy->setChecked( data.useReverseProxy );
165
const KProxyData KManualProxyDlg::data() const
169
if (!m_bHasValidData)
172
data.proxyList["http"] = urlFromInput( mDlg->leHttp, mDlg->sbHttp );
174
if ( mDlg->cbSameProxy->isChecked () )
176
data.proxyList["https"] = data.proxyList["http"];
177
data.proxyList["ftp"] = data.proxyList["http"];
181
data.proxyList["https"] = urlFromInput( mDlg->leHttps, mDlg->sbHttps );
182
data.proxyList["ftp"] = urlFromInput( mDlg->leFtp, mDlg->sbFtp );
185
if ( mDlg->lbExceptions->count() )
187
for ( int rowIndex = 0 ; rowIndex < mDlg->lbExceptions->count() ; rowIndex++ ) {
188
data.noProxyFor << mDlg->lbExceptions->item(rowIndex)->text();
192
data.type = KProtocolManager::ManualProxy;
193
data.useReverseProxy = mDlg->cbReverseProxy->isChecked();
198
void KManualProxyDlg::sameProxy( bool enable )
200
mDlg->leHttps->setEnabled (!enable );
201
mDlg->leFtp->setEnabled (!enable );
202
mDlg->sbHttps->setEnabled (!enable );
203
mDlg->sbFtp->setEnabled (!enable );
204
mDlg->pbCopyDown->setEnabled( !enable );
208
mOldFtpText = mDlg->leFtp->text();
209
mOldHttpsText = mDlg->leHttps->text();
211
mOldFtpPort = mDlg->sbFtp->value();
212
mOldHttpsPort = mDlg->sbHttps->value();
214
int port = mDlg->sbHttp->value();
215
QString text = mDlg->leHttp->text();
217
mDlg->leFtp->setText (text);
218
mDlg->leHttps->setText (text);
220
mDlg->sbFtp->setValue (port);
221
mDlg->sbHttps->setValue (port);
223
if (mDlg->lbHttps->font().bold())
224
setHighLight( mDlg->lbHttps, false );
226
if (mDlg->lbFtp->font().bold())
227
setHighLight( mDlg->lbFtp, false );
231
mDlg->leFtp->setText (mOldFtpText);
232
mDlg->leHttps->setText (mOldHttpsText);
234
mDlg->sbFtp->setValue (mOldFtpPort);
235
mDlg->sbHttps->setValue (mOldHttpsPort);
239
bool KManualProxyDlg::validate()
242
unsigned short count = 0;
244
if ( isValidURL( mDlg->leHttp->text(), &filteredURL ) )
246
mDlg->leHttp->setText( filteredURL.url() );
250
setHighLight( mDlg->lbHttp, true );
252
if ( !mDlg->cbSameProxy->isChecked () )
254
if ( isValidURL( mDlg->leHttps->text(), &filteredURL ) )
256
mDlg->leHttps->setText( filteredURL.url() );
260
setHighLight( mDlg->lbHttps, true );
262
if ( isValidURL( mDlg->leFtp->text(), &filteredURL ) )
264
mDlg->leFtp->setText( filteredURL.url() );
268
setHighLight( mDlg->lbFtp, true );
273
showErrorMsg( i18nc("@title:window", "Invalid Proxy Setting"),
274
i18n("One or more of the specified proxy settings are "
275
"invalid. The incorrect entries are highlighted.") );
281
void KManualProxyDlg::textChanged(const QString& text)
283
if (!mDlg->cbSameProxy->isChecked())
286
mDlg->leFtp->setText( text );
287
mDlg->leHttps->setText( text );
290
void KManualProxyDlg::valueChanged(int value)
292
if (!mDlg->cbSameProxy->isChecked())
295
mDlg->sbFtp->setValue (value);
296
mDlg->sbHttps->setValue (value);
299
void KManualProxyDlg::copyDown()
303
if ( !mDlg->leHttp->text().isEmpty() )
305
else if ( !mDlg->leHttps->text().isEmpty() )
311
mDlg->leHttps->setText( mDlg->leHttp->text() );
312
mDlg->sbHttps->setValue( mDlg->sbHttp->value() );
313
mDlg->leFtp->setText( mDlg->leHttp->text() );
314
mDlg->sbFtp->setValue( mDlg->sbHttp->value() );
317
mDlg->leFtp->setText( mDlg->leHttps->text() );
318
mDlg->sbFtp->setValue( mDlg->sbHttps->value() );
327
void KManualProxyDlg::slotOk()
329
if ( m_bHasValidData || validate() )
330
m_bHasValidData = true;
333
bool KManualProxyDlg::handleDuplicate( const QString& site )
335
for ( int rowIndex = 0 ; rowIndex < mDlg->lbExceptions->count() ; rowIndex++ )
337
QListWidgetItem* item = mDlg->lbExceptions->item(rowIndex);
339
if ( item->text().lastIndexOf( site ) != -1 &&
340
item != mDlg->lbExceptions->currentItem() )
342
QString msg = i18n("You entered a duplicate address. "
343
"Please try again.");
344
QString details = i18n("<qt><center><b>%1</b></center> "
345
"is already in the list.</qt>", site);
346
KMessageBox::detailedError( this, msg, details, i18nc("@title:window", "Duplicate Entry") );
353
void KManualProxyDlg::newPressed()
356
if( getException(result, i18nc("@title:window", "New Exception")) && !handleDuplicate(result) )
357
mDlg->lbExceptions->addItem( result );
360
void KManualProxyDlg::changePressed()
363
if( getException( result, i18nc("@title:window", "Change Exception"),
364
mDlg->lbExceptions->currentItem()->text() ) &&
365
!handleDuplicate( result ) )
366
mDlg->lbExceptions->currentItem()->setText(result);
369
void KManualProxyDlg::deletePressed()
371
delete mDlg->lbExceptions->takeItem( mDlg->lbExceptions->currentRow() );
372
if(mDlg->lbExceptions->currentItem())
373
mDlg->lbExceptions->currentItem()->setSelected(true);
377
void KManualProxyDlg::deleteAllPressed()
379
mDlg->lbExceptions->clear();
383
void KManualProxyDlg::updateButtons()
385
bool hasItems = mDlg->lbExceptions->count() > 0;
386
bool itemSelected = (hasItems && mDlg->lbExceptions->currentItem()!=0);
388
mDlg->pbDeleteAll->setEnabled( hasItems );
389
mDlg->pbDelete->setEnabled( itemSelected );
390
mDlg->pbChange->setEnabled( itemSelected );
393
QString KManualProxyDlg::urlFromInput(const KLineEdit* edit,
394
const KIntSpinBox* spin) const
396
if (!edit || edit->text().isEmpty())
399
KUrl u( edit->text() );
402
u.setPort( spin->value() );
407
bool KManualProxyDlg::isValidURL( const QString& _url, KUrl* result ) const
412
filters << "kshorturifilter" << "localdomainurifilter";
414
// If the typed URL is malformed, and the filters cannot filter it
415
// then it must be an invalid entry.
416
if( !(url.isValid() && KUriFilter::self()->filterUri(url, filters) &&
420
QString host (url.host());
422
// We only check for a relevant subset of characters that are
423
// not allowed in <authority> component of a URL.
424
if ( host.contains ('*') || host.contains (' ') || host.contains ('?') )
433
void KManualProxyDlg::showErrorMsg( const QString& caption,
434
const QString& message )
436
QString cap( caption );
437
QString msg( message );
440
cap = i18nc("@title:window", "Invalid Entry");
443
msg = i18n("The address you have entered is not valid.");
445
QString details = i18n("<qt>Make sure none of the addresses or URLs you "
446
"specified contain invalid or wildcard characters "
447
"such as spaces, asterisks (*), or question marks(?).<br /><br />"
448
"<u>Examples of VALID entries:</u><br />"
449
"<code>http://mycompany.com, 192.168.10.1, "
450
"mycompany.com, localhost, http://localhost</code><br /><br />"
451
"<u>Examples of INVALID entries:</u><br />"
452
"<code>http://my company.com, http:/mycompany,com "
453
"file:/localhost</code></qt>");
455
KMessageBox::detailedError( this, msg, details, cap );
458
bool KManualProxyDlg::getException ( QString& result,
459
const QString& caption,
460
const QString& value )
464
// Specify the appropriate message...
465
if ( mDlg->cbReverseProxy->isChecked() )
466
label = i18n("Enter the URL or address that should use the above proxy "
469
label = i18n("Enter the address or URL that should be excluded from "
470
"using the above proxy settings:");
472
QString whatsThis = i18n("<qt>Enter a valid address or URL.<br /><br />"
473
"<b><u>NOTE:</u></b> Wildcard matching such as "
474
"<code>*.kde.org</code> is not supported. If you want "
475
"to match any host in the <code>.kde.org</code> domain, "
476
"e.g. <code>printing.kde.org</code>, then simply enter "
477
"<code>.kde.org</code>.</qt>");
480
result = KInputDialog::getText( caption, label, value, &ok, this,
481
0, QString(), whatsThis );
483
// If the user pressed cancel, do nothing...
487
// If the typed URL is malformed, and the filters cannot filter it
488
// then it must be an invalid entry,
489
if( isValidURL(result) || (result.length() >= 3 && result.startsWith('.')))
496
#include "kmanualproxydlg.moc"