Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / konqueror / settings / kio / kproxydlg.cpp
blob1d2facc70ad0c1439edf13f0f99904595973e06f
1 /*
2 kproxydlg.cpp - Proxy configuration dialog
4 Copyright (C) 2001- 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
9 Foundation.
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.
22 // Own
23 #include "kproxydlg.h"
25 // Qt
26 #include <QtCore/QRegExp>
27 #include <QtGui/QBoxLayout>
28 #include <QtGui/QCheckBox>
29 #include <QtGui/QLabel>
30 #include <QtGui/QLayout>
31 #include <QtGui/QRadioButton>
32 #include <QtGui/QTabWidget>
34 // KDE
35 #include <kgenericfactory.h>
36 #include <klineedit.h>
37 #include <klocale.h>
38 #include <kmessagebox.h>
40 // Local
41 #include "kenvvarproxydlg.h"
42 #include "kmanualproxydlg.h"
43 #include "ksaveioconfig.h"
45 K_PLUGIN_FACTORY_DECLARATION(KioConfigFactory)
47 KProxyDialog::KProxyDialog(QWidget *parent, const QVariantList &args)
48 : KCModule(KioConfigFactory::componentData(), parent)
50 mUi.setupUi(this);
52 // signals and slots connections
53 connect( mUi.rbNoProxy, SIGNAL( toggled(bool) ),
54 SLOT( slotUseProxyChanged() ) );
56 connect( mUi.rbAutoDiscover, SIGNAL( toggled(bool) ),
57 SLOT( slotChanged() ) );
58 connect( mUi.rbAutoScript, SIGNAL( toggled(bool) ),
59 SLOT( slotChanged() ) );
61 connect( mUi.rbPrompt, SIGNAL( toggled(bool) ),
62 SLOT( slotChanged() ) );
63 connect( mUi.rbPresetLogin, SIGNAL( toggled(bool) ),
64 SLOT( slotChanged() ) );
66 connect( mUi.cbPersConn, SIGNAL( toggled(bool) ),
67 SLOT( slotChanged() ) );
69 connect( mUi.location, SIGNAL( textChanged(const QString&) ),
70 SLOT( slotChanged() ) );
72 connect( mUi.pbEnvSetup, SIGNAL( clicked() ), SLOT( setupEnvProxy() ) );
73 connect( mUi.pbManSetup, SIGNAL( clicked() ), SLOT( setupManProxy() ) );
77 KProxyDialog::~KProxyDialog()
81 void KProxyDialog::load()
83 mDefaultData = false;
85 KProtocolManager proto;
86 bool useProxy = proto.useProxy();
87 mData.type = proto.proxyType();
88 mData.proxyList["http"] = proto.proxyFor( "http" );
89 mData.proxyList["https"] = proto.proxyFor( "https" );
90 mData.proxyList["ftp"] = proto.proxyFor( "ftp" );
91 mData.proxyList["script"] = proto.proxyConfigScript();
92 mData.useReverseProxy = proto.useReverseProxy();
93 mData.noProxyFor = proto.noProxyFor().split( QRegExp("[',''\t'' ']"), QString::SkipEmptyParts);
95 mUi.gbAuth->setEnabled( useProxy );
96 mUi.gbOptions->setEnabled( useProxy );
98 mUi.cbPersConn->setChecked( proto.persistentProxyConnection() );
100 if ( !mData.proxyList["script"].isEmpty() )
101 mUi.location->lineEdit()->setText( mData.proxyList["script"] );
103 switch ( mData.type )
105 case KProtocolManager::WPADProxy:
106 mUi.rbAutoDiscover->setChecked( true );
107 break;
108 case KProtocolManager::PACProxy:
109 mUi.rbAutoScript->setChecked( true );
110 break;
111 case KProtocolManager::ManualProxy:
112 mUi.rbManual->setChecked( true );
113 break;
114 case KProtocolManager::EnvVarProxy:
115 mUi.rbEnvVar->setChecked( true );
116 break;
117 case KProtocolManager::NoProxy:
118 default:
119 mUi.rbNoProxy->setChecked( true );
120 break;
123 switch( proto.proxyAuthMode() )
125 case KProtocolManager::Prompt:
126 mUi.rbPrompt->setChecked( true );
127 break;
128 case KProtocolManager::Automatic:
129 mUi.rbPresetLogin->setChecked( true );
130 default:
131 break;
135 void KProxyDialog::save()
137 bool updateProxyScout = false;
139 if (mDefaultData)
140 mData.reset ();
142 if ( mUi.rbNoProxy->isChecked() )
144 KSaveIOConfig::setProxyType( KProtocolManager::NoProxy );
146 else
148 if ( mUi.rbAutoDiscover->isChecked() )
150 KSaveIOConfig::setProxyType( KProtocolManager::WPADProxy );
151 updateProxyScout = true;
153 else if ( mUi.rbAutoScript->isChecked() )
155 KUrl u( mUi.location->lineEdit()->text() );
157 if ( !u.isValid() )
159 showInvalidMessage( i18n("The address of the automatic proxy "
160 "configuration script is invalid. Please "
161 "correct this problem before proceeding. "
162 "Otherwise, your changes will be "
163 "ignored.") );
164 return;
166 else
168 KSaveIOConfig::setProxyType( KProtocolManager::PACProxy );
169 mData.proxyList["script"] = u.url();
170 updateProxyScout = true;
173 else if ( mUi.rbManual->isChecked() )
175 if ( mData.type != KProtocolManager::ManualProxy )
177 // Let's try a bit harder to determine if the previous
178 // proxy setting was indeed a manual proxy
179 KUrl u ( mData.proxyList["http"] );
180 bool validProxy = (u.isValid() && u.port() > 0);
181 u = mData.proxyList["https"];
182 validProxy = validProxy || (u.isValid() && u.port() > 0);
183 u = mData.proxyList["ftp"];
184 validProxy = validProxy || (u.isValid() && u.port() > 0);
186 if (!validProxy)
188 showInvalidMessage();
189 return;
192 mData.type = KProtocolManager::ManualProxy;
195 KSaveIOConfig::setProxyType( KProtocolManager::ManualProxy );
197 else if ( mUi.rbEnvVar->isChecked() )
199 if ( mData.type != KProtocolManager::EnvVarProxy )
201 showInvalidMessage();
202 return;
205 KSaveIOConfig::setProxyType( KProtocolManager::EnvVarProxy );
208 if ( mUi.rbPrompt->isChecked() )
209 KSaveIOConfig::setProxyAuthMode( KProtocolManager::Prompt );
210 else if ( mUi.rbPresetLogin->isChecked() )
211 KSaveIOConfig::setProxyAuthMode( KProtocolManager::Automatic );
214 KSaveIOConfig::setPersistentProxyConnection( mUi.cbPersConn->isChecked() );
216 // Save the common proxy setting...
217 KSaveIOConfig::setProxyFor( "ftp", mData.proxyList["ftp"] );
218 KSaveIOConfig::setProxyFor( "http", mData.proxyList["http"] );
219 KSaveIOConfig::setProxyFor( "https", mData.proxyList["https"] );
221 KSaveIOConfig::setProxyConfigScript( mData.proxyList["script"] );
222 KSaveIOConfig::setUseReverseProxy( mData.useReverseProxy );
223 KSaveIOConfig::setNoProxyFor( mData.noProxyFor.join(",") );
226 KSaveIOConfig::updateRunningIOSlaves (this);
227 if ( updateProxyScout )
228 KSaveIOConfig::updateProxyScout( this );
230 emit changed( false );
233 void KProxyDialog::defaults()
235 mDefaultData = true;
236 mUi.rbNoProxy->setChecked( true );
237 mUi.location->lineEdit()->clear();
238 mUi.cbPersConn->setChecked( false );
241 void KProxyDialog::setupManProxy()
243 KManualProxyDlg dlgManual( this );
245 dlgManual.setProxyData( mData );
247 if ( dlgManual.exec() == QDialog::Accepted )
249 mData = dlgManual.data();
250 mUi.rbManual->setChecked(true);
251 emit changed( true );
255 void KProxyDialog::setupEnvProxy()
257 KEnvVarProxyDlg dlgEnv( this );
259 dlgEnv.setProxyData( mData );
261 if ( dlgEnv.exec() == QDialog::Accepted )
263 mData = dlgEnv.data();
264 mUi.rbEnvVar->setChecked(true);
265 emit changed( true );
269 void KProxyDialog::slotChanged()
271 mDefaultData = false;
272 emit changed( true );
275 void KProxyDialog::slotUseProxyChanged()
277 mDefaultData = false;
278 bool useProxy = !(mUi.rbNoProxy->isChecked());
279 mUi.gbAuth->setEnabled(useProxy);
280 mUi.gbOptions->setEnabled(useProxy);
281 emit changed( true );
284 QString KProxyDialog::quickHelp() const
286 return i18n( "<h1>Proxy</h1>"
287 "<p>A proxy server is an intermediate program that sits between "
288 "your machine and the Internet and provides services such as "
289 "web page caching and/or filtering.</p>"
290 "<p>Caching proxy servers give you faster access to sites you have "
291 "already visited by locally storing or caching the content of those "
292 "pages; filtering proxy servers, on the other hand, provide the "
293 "ability to block out requests for ads, spam, or anything else you "
294 "want to block.</p>"
295 "<p><u>Note:</u> Some proxy servers provide both services.</p>" );
298 void KProxyDialog::showInvalidMessage( const QString& _msg )
300 QString msg;
302 if( !_msg.isEmpty() )
303 msg = _msg;
304 else
305 msg = i18n( "<qt>The proxy settings you specified are invalid."
306 "<br /><br />Please click on the <b>Setup...</b> "
307 "button and correct the problem before proceeding; "
308 "otherwise your changes will be ignored.</qt>" );
310 KMessageBox::error( this, msg, i18n("Invalid Proxy Setup") );
313 #include "kproxydlg.moc"