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>
30 #include <QtGui/QSpinBox>
34 #include <kiconloader.h>
35 #include <kicontheme.h>
36 #include <kinputdialog.h>
37 #include <kio/ioslave_defaults.h>
38 #include <klineedit.h>
39 #include <klistwidget.h>
41 #include <kmessagebox.h>
42 #include <knuminput.h>
43 #include <kurifilter.h>
47 KManualProxyDlg::KManualProxyDlg( QWidget
* parent
, const char* name
)
48 :KProxyDialogBase( parent
, name
, true,
49 i18n("Manual Proxy Configuration") )
51 mDlg
= new ManualProxyDlgUI (this);
53 mDlg
->pbCopyDown
->setIcon( KIcon("go-down") );
54 QSizePolicy
sizePolicy( QSizePolicy::Fixed
, QSizePolicy::Fixed
);
55 sizePolicy
.setHeightForWidth( mDlg
->pbCopyDown
->sizePolicy().hasHeightForWidth() );
56 mDlg
->pbCopyDown
->setSizePolicy( sizePolicy
);
61 void KManualProxyDlg::init()
63 mDlg
->sbHttp
->setRange( 0, MAX_PORT_VALUE
);
64 mDlg
->sbHttps
->setRange( 0, MAX_PORT_VALUE
);
65 mDlg
->sbFtp
->setRange( 0, MAX_PORT_VALUE
);
67 connect( mDlg
->pbNew
, SIGNAL( clicked() ), SLOT( newPressed() ) );
68 connect( mDlg
->pbChange
, SIGNAL( clicked() ), SLOT( changePressed() ) );
69 connect( mDlg
->pbDelete
, SIGNAL( clicked() ), SLOT( deletePressed() ) );
70 connect( mDlg
->pbDeleteAll
, SIGNAL( clicked() ), SLOT( deleteAllPressed() ) );
72 connect( mDlg
->lbExceptions
, SIGNAL(itemSelectionChanged()), SLOT(updateButtons()) );
73 connect( mDlg
->lbExceptions
, SIGNAL(itemDoubleClicked (QListWidgetItem
*)), SLOT(changePressed()));
75 connect( mDlg
->cbSameProxy
, SIGNAL( toggled(bool) ), SLOT( sameProxy(bool) ) );
76 connect( mDlg
->pbCopyDown
, SIGNAL( clicked() ), SLOT( copyDown() ) );
78 connect( mDlg
->leHttp
, SIGNAL(textChanged(const QString
&)), SLOT(textChanged(const QString
&)) );
79 connect( mDlg
->sbHttp
, SIGNAL(valueChanged(int)), SLOT(valueChanged (int)) );
81 connect( this, SIGNAL(okClicked()), this, SLOT(slotOk()));
84 void KManualProxyDlg::setProxyData( const KProxyData
&data
)
88 // Set the HTTP proxy...
89 if (!isValidURL(data
.proxyList
["http"], &url
))
90 mDlg
->sbHttp
->setValue( DEFAULT_PROXY_PORT
);
93 int port
= url
.port();
95 port
= DEFAULT_PROXY_PORT
;
98 mDlg
->leHttp
->setText( url
.url() );
99 mDlg
->sbHttp
->setValue( port
);
102 bool useSameProxy
= (!mDlg
->leHttp
->text().isEmpty () &&
103 data
.proxyList
["http"] == data
.proxyList
["https"] &&
104 data
.proxyList
["http"] == data
.proxyList
["ftp"]);
106 mDlg
->cbSameProxy
->setChecked ( useSameProxy
);
110 mDlg
->leHttps
->setText ( mDlg
->leHttp
->text() );
111 mDlg
->leFtp
->setText ( mDlg
->leHttp
->text() );
112 mDlg
->sbHttps
->setValue( mDlg
->sbHttp
->value() );
113 mDlg
->sbFtp
->setValue( mDlg
->sbHttp
->value() );
119 // Set the HTTPS proxy...
120 if( !isValidURL( data
.proxyList
["https"], &url
) )
121 mDlg
->sbHttps
->setValue( DEFAULT_PROXY_PORT
);
124 int port
= url
.port();
126 port
= DEFAULT_PROXY_PORT
;
129 mDlg
->leHttps
->setText( url
.url() );
130 mDlg
->sbHttps
->setValue( port
);
133 // Set the FTP proxy...
134 if( !isValidURL( data
.proxyList
["ftp"], &url
) )
135 mDlg
->sbFtp
->setValue( DEFAULT_PROXY_PORT
);
138 int port
= url
.port();
140 port
= DEFAULT_PROXY_PORT
;
143 mDlg
->leFtp
->setText( url
.url() );
144 mDlg
->sbFtp
->setValue( port
);
148 QStringList::ConstIterator it
= data
.noProxyFor
.begin();
149 for( ; it
!= data
.noProxyFor
.end(); ++it
)
151 // "no_proxy" is a keyword used by the environment variable
152 // based configuration. We ignore it here as it is not applicable...
153 if ((*it
).toLower() != "no_proxy" && !(*it
).isEmpty())
155 // Validate the NOPROXYFOR entries and use only hostnames if the entry is
156 // a valid or legitimate URL. NOTE: needed to catch manual manipulation
157 // of the proxy config files...
158 if( isValidURL( *it
) || ((*it
).length() >= 3 && (*it
).startsWith('.')) )
159 mDlg
->lbExceptions
->addItem( *it
);
163 mDlg
->cbReverseProxy
->setChecked( data
.useReverseProxy
);
166 const KProxyData
KManualProxyDlg::data() const
170 if (!m_bHasValidData
)
173 data
.proxyList
["http"] = urlFromInput( mDlg
->leHttp
, mDlg
->sbHttp
);
175 if ( mDlg
->cbSameProxy
->isChecked () )
177 data
.proxyList
["https"] = data
.proxyList
["http"];
178 data
.proxyList
["ftp"] = data
.proxyList
["http"];
182 data
.proxyList
["https"] = urlFromInput( mDlg
->leHttps
, mDlg
->sbHttps
);
183 data
.proxyList
["ftp"] = urlFromInput( mDlg
->leFtp
, mDlg
->sbFtp
);
186 if ( mDlg
->lbExceptions
->count() )
188 for ( int rowIndex
= 0 ; rowIndex
< mDlg
->lbExceptions
->count() ; rowIndex
++ ) {
189 data
.noProxyFor
<< mDlg
->lbExceptions
->item(rowIndex
)->text();
193 data
.type
= KProtocolManager::ManualProxy
;
194 data
.useReverseProxy
= mDlg
->cbReverseProxy
->isChecked();
199 void KManualProxyDlg::sameProxy( bool enable
)
201 mDlg
->leHttps
->setEnabled (!enable
);
202 mDlg
->leFtp
->setEnabled (!enable
);
203 mDlg
->sbHttps
->setEnabled (!enable
);
204 mDlg
->sbFtp
->setEnabled (!enable
);
205 mDlg
->pbCopyDown
->setEnabled( !enable
);
209 mOldFtpText
= mDlg
->leFtp
->text();
210 mOldHttpsText
= mDlg
->leHttps
->text();
212 mOldFtpPort
= mDlg
->sbFtp
->value();
213 mOldHttpsPort
= mDlg
->sbHttps
->value();
215 int port
= mDlg
->sbHttp
->value();
216 QString text
= mDlg
->leHttp
->text();
218 mDlg
->leFtp
->setText (text
);
219 mDlg
->leHttps
->setText (text
);
221 mDlg
->sbFtp
->setValue (port
);
222 mDlg
->sbHttps
->setValue (port
);
224 if (mDlg
->lbHttps
->font().bold())
225 setHighLight( mDlg
->lbHttps
, false );
227 if (mDlg
->lbFtp
->font().bold())
228 setHighLight( mDlg
->lbFtp
, false );
232 mDlg
->leFtp
->setText (mOldFtpText
);
233 mDlg
->leHttps
->setText (mOldHttpsText
);
235 mDlg
->sbFtp
->setValue (mOldFtpPort
);
236 mDlg
->sbHttps
->setValue (mOldHttpsPort
);
240 bool KManualProxyDlg::validate()
243 unsigned short count
= 0;
245 if ( isValidURL( mDlg
->leHttp
->text(), &filteredURL
) )
247 mDlg
->leHttp
->setText( filteredURL
.url() );
251 setHighLight( mDlg
->lbHttp
, true );
253 if ( !mDlg
->cbSameProxy
->isChecked () )
255 if ( isValidURL( mDlg
->leHttps
->text(), &filteredURL
) )
257 mDlg
->leHttps
->setText( filteredURL
.url() );
261 setHighLight( mDlg
->lbHttps
, true );
263 if ( isValidURL( mDlg
->leFtp
->text(), &filteredURL
) )
265 mDlg
->leFtp
->setText( filteredURL
.url() );
269 setHighLight( mDlg
->lbFtp
, true );
274 showErrorMsg( i18n("Invalid Proxy Setting"),
275 i18n("One or more of the specified proxy settings are "
276 "invalid. The incorrect entries are highlighted.") );
282 void KManualProxyDlg::textChanged(const QString
& text
)
284 if (!mDlg
->cbSameProxy
->isChecked())
287 mDlg
->leFtp
->setText( text
);
288 mDlg
->leHttps
->setText( text
);
291 void KManualProxyDlg::valueChanged(int value
)
293 if (!mDlg
->cbSameProxy
->isChecked())
296 mDlg
->sbFtp
->setValue (value
);
297 mDlg
->sbHttps
->setValue (value
);
300 void KManualProxyDlg::copyDown()
304 if ( !mDlg
->leHttp
->text().isEmpty() )
306 else if ( !mDlg
->leHttps
->text().isEmpty() )
312 mDlg
->leHttps
->setText( mDlg
->leHttp
->text() );
313 mDlg
->sbHttps
->setValue( mDlg
->sbHttp
->value() );
314 mDlg
->leFtp
->setText( mDlg
->leHttp
->text() );
315 mDlg
->sbFtp
->setValue( mDlg
->sbHttp
->value() );
318 mDlg
->leFtp
->setText( mDlg
->leHttps
->text() );
319 mDlg
->sbFtp
->setValue( mDlg
->sbHttps
->value() );
328 void KManualProxyDlg::slotOk()
330 if ( m_bHasValidData
|| validate() )
331 m_bHasValidData
= true;
334 bool KManualProxyDlg::handleDuplicate( const QString
& site
)
336 for ( int rowIndex
= 0 ; rowIndex
< mDlg
->lbExceptions
->count() ; rowIndex
++ )
338 QListWidgetItem
* item
= mDlg
->lbExceptions
->item(rowIndex
);
340 if ( item
->text().lastIndexOf( site
) != -1 &&
341 item
!= mDlg
->lbExceptions
->currentItem() )
343 QString msg
= i18n("You entered a duplicate address. "
344 "Please try again.");
345 QString details
= i18n("<qt><center><b>%1</b></center> "
346 "is already in the list.</qt>", site
);
347 KMessageBox::detailedError( this, msg
, details
, i18n("Duplicate Entry") );
354 void KManualProxyDlg::newPressed()
357 if( getException(result
, i18n("New Exception")) && !handleDuplicate(result
) )
358 mDlg
->lbExceptions
->addItem( result
);
361 void KManualProxyDlg::changePressed()
364 if( getException( result
, i18n("Change Exception"),
365 mDlg
->lbExceptions
->currentItem()->text() ) &&
366 !handleDuplicate( result
) )
367 mDlg
->lbExceptions
->currentItem()->setText(result
);
370 void KManualProxyDlg::deletePressed()
372 delete mDlg
->lbExceptions
->takeItem( mDlg
->lbExceptions
->currentRow() );
373 if(mDlg
->lbExceptions
->currentItem())
374 mDlg
->lbExceptions
->currentItem()->setSelected(true);
378 void KManualProxyDlg::deleteAllPressed()
380 mDlg
->lbExceptions
->clear();
384 void KManualProxyDlg::updateButtons()
386 bool hasItems
= mDlg
->lbExceptions
->count() > 0;
387 bool itemSelected
= (hasItems
&& mDlg
->lbExceptions
->currentItem()!=0);
389 mDlg
->pbDeleteAll
->setEnabled( hasItems
);
390 mDlg
->pbDelete
->setEnabled( itemSelected
);
391 mDlg
->pbChange
->setEnabled( itemSelected
);
394 QString
KManualProxyDlg::urlFromInput(const KLineEdit
* edit
,
395 const QSpinBox
* spin
) const
400 KUrl
u( edit
->text() );
403 u
.setPort( spin
->value() );
408 bool KManualProxyDlg::isValidURL( const QString
& _url
, KUrl
* result
) const
413 filters
<< "kshorturifilter" << "localdomainurifilter";
415 // If the typed URL is malformed, and the filters cannot filter it
416 // then it must be an invalid entry.
417 if( !(url
.isValid() && KUriFilter::self()->filterUri(url
, filters
) &&
421 QString
host (url
.host());
423 // We only check for a relevant subset of characters that are
424 // not allowed in <authority> component of a URL.
425 if ( host
.contains ('*') || host
.contains (' ') || host
.contains ('?') )
434 void KManualProxyDlg::showErrorMsg( const QString
& caption
,
435 const QString
& message
)
437 QString
cap( caption
);
438 QString
msg( message
);
441 cap
= i18n("Invalid Entry");
444 msg
= i18n("The address you have entered is not valid.");
446 QString details
= i18n("<qt>Make sure none of the addresses or URLs you "
447 "specified contain invalid or wildcard characters "
448 "such as spaces, asterisks (*), or question marks(?).<br /><br />"
449 "<u>Examples of VALID entries:</u><br />"
450 "<code>http://mycompany.com, 192.168.10.1, "
451 "mycompany.com, localhost, http://localhost</code><br /><br />"
452 "<u>Examples of INVALID entries:</u><br />"
453 "<code>http://my company.com, http:/mycompany,com "
454 "file:/localhost</code></qt>");
456 KMessageBox::detailedError( this, msg
, details
, cap
);
459 bool KManualProxyDlg::getException ( QString
& result
,
460 const QString
& caption
,
461 const QString
& value
)
465 // Specify the appropriate message...
466 if ( mDlg
->cbReverseProxy
->isChecked() )
467 label
= i18n("Enter the URL or address that should use the above proxy "
470 label
= i18n("Enter the address or URL that should be excluded from "
471 "using the above proxy settings:");
473 QString whatsThis
= i18n("<qt>Enter a valid address or URL.<br /><br />"
474 "<b><u>NOTE:</u></b> Wildcard matching such as "
475 "<code>*.kde.org</code> is not supported. If you want "
476 "to match any host in the <code>.kde.org</code> domain, "
477 "e.g. <code>printing.kde.org</code>, then simply enter "
478 "<code>.kde.org</code>.</qt>");
481 result
= KInputDialog::getText( caption
, label
, value
, &ok
, this,
482 0, QString(), whatsThis
);
484 // If the user pressed cancel, do nothing...
488 // If the typed URL is malformed, and the filters cannot filter it
489 // then it must be an invalid entry,
490 if( isValidURL(result
) || (result
.length() >= 3 && result
.startsWith('.')))
497 #include "kmanualproxydlg.moc"