Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / apps / konqueror / settings / kio / kenvvarproxydlg.cpp
blob3fc527049700f5814fbcd7badac100cba43fb514
1 /*
2 kenvvarproxydlg.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 // Local
23 #include "kenvvarproxydlg.h"
25 // std
26 #include <stdlib.h>
28 // Qt
29 #include <QtGui/QLabel>
30 #include <QtGui/QLayout>
31 #include <QtGui/QCheckBox>
32 #include <QtGui/QPushButton>
34 // KDE
35 #include <kdebug.h>
36 #include <klocale.h>
37 #include <klineedit.h>
38 #include <kmessagebox.h>
39 #include <kurl.h>
41 #define ENV_FTP_PROXY "FTP_PROXY,ftp_proxy,FTPPROXY,ftpproxy,PROXY,proxy"
42 #define ENV_HTTP_PROXY "HTTP_PROXY,http_proxy,HTTPPROXY,httpproxy,PROXY,proxy"
43 #define ENV_HTTPS_PROXY "HTTPS_PROXY,https_proxy,HTTPSPROXY,httpsproxy,PROXY,proxy"
44 #define NO_PROXY "NO_PROXY,no_proxy"
47 KEnvVarProxyDlg::KEnvVarProxyDlg( QWidget* parent, const char* name )
48 :KProxyDialogBase( parent, name, true,
49 i18n( "Variable Proxy Configuration" ) )
51 mDlg = new EnvVarProxyDlgUI( this );
52 setMainWidget( mDlg );
53 mDlg->leHttp->setMinimumWidth( mDlg->leHttp->fontMetrics().maxWidth() * 20 );
54 init();
57 KEnvVarProxyDlg::~KEnvVarProxyDlg ()
61 void KEnvVarProxyDlg::init()
63 m_bHasValidData = false;
65 connect( mDlg->cbShowValue, SIGNAL( clicked() ), SLOT( showValue() ) );
66 connect( mDlg->pbVerify, SIGNAL( clicked() ), SLOT( verifyPressed() ) );
67 connect( mDlg->pbDetect, SIGNAL( clicked() ), SLOT( autoDetectPressed() ) );
70 void KEnvVarProxyDlg::setProxyData( const KProxyData& data )
72 // Setup HTTP Proxy...
73 KUrl u ( data.proxyList["http"] );
74 if (!u.isEmpty() && !u.isValid())
76 mEnvVarsMap["http"].name = data.proxyList["http"];
77 mEnvVarsMap["http"].value = QString::fromLocal8Bit( getenv(data.proxyList["http"].toLocal8Bit()) );
80 // Setup HTTPS Proxy...
81 u = data.proxyList["https"];
82 if (!u.isEmpty() && !u.isValid())
84 mEnvVarsMap["https"].name = data.proxyList["https"];
85 mEnvVarsMap["https"].value = QString::fromLocal8Bit( getenv(data.proxyList["https"].toLocal8Bit()) );
88 // Setup FTP Proxy...
89 u = data.proxyList["ftp"];
90 if (!u.isEmpty() && !u.isValid())
92 mEnvVarsMap["ftp"].name = data.proxyList["ftp"];
93 mEnvVarsMap["ftp"].value = QString::fromLocal8Bit( getenv(data.proxyList["ftp"].toLocal8Bit()) );
96 u = data.noProxyFor.join(",");
97 if (!u.isEmpty() && !u.isValid())
99 QString noProxy = u.url();
100 mEnvVarsMap["noProxy"].name = noProxy;
101 mEnvVarsMap["noProxy"].value = QString::fromLocal8Bit( getenv(noProxy.toLocal8Bit()) );
104 mDlg->cbShowValue->setChecked( data.showEnvVarValue );
105 showValue();
108 const KProxyData KEnvVarProxyDlg::data() const
110 KProxyData data;
112 if (m_bHasValidData)
114 data.proxyList["http"] = mEnvVarsMap["http"].name;
115 data.proxyList["https"] = mEnvVarsMap["https"].name;
116 data.proxyList["ftp"] = mEnvVarsMap["ftp"].name;
117 data.noProxyFor = QStringList(mEnvVarsMap["noProxy"].name);
118 data.type = KProtocolManager::EnvVarProxy;
119 data.showEnvVarValue = mDlg->cbShowValue->isChecked();
122 return data;
126 void KEnvVarProxyDlg::verifyPressed()
128 if ( !validate() )
130 QString msg = i18n("You must specify at least one valid proxy "
131 "environment variable.");
133 QString details = i18n("<qt>Make sure you entered the actual environment "
134 "variable name rather than its value. For "
135 "example, if the environment variable is <br /><b>"
136 "HTTP_PROXY=http://localhost:3128</b><br /> you need "
137 "to enter <b>HTTP_PROXY</b> here instead of the "
138 "actual value http://localhost:3128.</qt>");
140 KMessageBox::detailedSorry( this, msg, details,
141 i18n("Invalid Proxy Setup") );
143 else
145 KMessageBox::information( this, i18n("Successfully verified."),
146 i18n("Proxy Setup") );
150 void KEnvVarProxyDlg::autoDetectPressed()
152 QString env;
153 QStringList::ConstIterator it;
155 bool found = false;
157 setHighLight (mDlg->lbHttp, false);
158 setHighLight (mDlg->lbHttps, false);
159 setHighLight (mDlg->lbFtp, false);
161 // Detect HTTP proxy settings...
162 QStringList list = QString::fromLatin1(ENV_HTTP_PROXY).split( ',', QString::SkipEmptyParts );
163 for( it = list.begin(); it != list.end(); ++it )
165 env = QString::fromLocal8Bit( getenv( (*it).toLocal8Bit() ) );
166 if ( !env.isEmpty() )
168 mEnvVarsMap ["http"].name = *it;
169 mEnvVarsMap ["http"].value = env;
170 mDlg->leHttp->setText( (mDlg->cbShowValue->isChecked() ? env : *it) );
171 found = true;
172 break;
176 // Detect HTTPS proxy settings...
177 list = QString::fromLatin1(ENV_HTTPS_PROXY).split( ',', QString::SkipEmptyParts );
178 for( it = list.begin(); it != list.end(); ++it )
180 env = QString::fromLocal8Bit( getenv( (*it).toLocal8Bit() ) );
181 if ( !env.isEmpty() )
183 mEnvVarsMap ["https"].name = *it;
184 mEnvVarsMap ["https"].value = env;
185 mDlg->leHttps->setText( (mDlg->cbShowValue->isChecked() ? env : *it) );
186 found = true;
187 break;
191 // Detect FTP proxy settings...
192 list = QString::fromLatin1(ENV_FTP_PROXY).split( ',', QString::SkipEmptyParts );
193 for(it = list.begin(); it != list.end(); ++it )
195 env = QString::fromLocal8Bit( getenv( (*it).toLocal8Bit() ) );
196 if ( !env.isEmpty() )
198 mEnvVarsMap ["ftp"].name = *it;
199 mEnvVarsMap ["ftp"].value = env;
200 mDlg->leFtp->setText( (mDlg->cbShowValue->isChecked() ? env : *it) );
201 found = true;
202 break;
206 // Detect the NO_PROXY settings...
207 list = QString::fromLatin1(NO_PROXY).split (',', QString::SkipEmptyParts );
208 for(it = list.begin(); it != list.end(); ++it )
210 env = QString::fromLocal8Bit( getenv( (*it).toLocal8Bit() ) );
211 if ( !env.isEmpty() )
213 mEnvVarsMap ["noProxy"].name = *it;
214 mEnvVarsMap ["noProxy"].value = env;
215 mDlg->leNoProxy->setText( (mDlg->cbShowValue->isChecked() ? env : *it) );
216 break;
220 if ( !found )
222 QString msg = i18n("Did not detect any environment variables "
223 "commonly used to set system wide proxy "
224 "information.");
226 QString details = i18n("<qt>To learn about the variable names the "
227 "automatic detection process searches for, "
228 "press OK, click on the quick help button "
229 "on the window title bar of the "
230 "previous dialog and then click on the "
231 "\"<b>Auto Detect</b>\" button.</qt>");
233 KMessageBox::detailedSorry( this, msg, details,
234 i18n("Automatic Proxy Variable Detection") );
238 void KEnvVarProxyDlg::showValue()
240 bool enable = mDlg->cbShowValue->isChecked();
242 mDlg->leHttp->setReadOnly (enable);
243 mDlg->leHttps->setReadOnly (enable);
244 mDlg->leFtp->setReadOnly (enable);
245 mDlg->leNoProxy->setReadOnly (enable);
247 if (enable)
249 mDlg->leHttp->setText( mEnvVarsMap["http"].value );
250 mDlg->leHttps->setText( mEnvVarsMap["https"].value );
251 mDlg->leFtp->setText( mEnvVarsMap["ftp"].value );
252 mDlg->leNoProxy->setText( mEnvVarsMap["noProxy"].value );
254 else
256 mDlg->leHttp->setText( mEnvVarsMap["http"].name );
257 mDlg->leHttps->setText( mEnvVarsMap["https"].name );
258 mDlg->leFtp->setText( mEnvVarsMap["ftp"].name );
259 mDlg->leNoProxy->setText( mEnvVarsMap["noProxy"].name );
263 bool KEnvVarProxyDlg::validate()
265 int count = 0;
267 QString value = mEnvVarsMap["http"].value;
268 if ( !value.isEmpty() )
269 count++;
271 value = mEnvVarsMap["https"].value;
272 if ( !value.isEmpty() )
273 count++;
275 value = mEnvVarsMap["ftp"].value;
276 if ( !value.isEmpty() )
277 count++;
279 m_bHasValidData = (count > 0);
281 return m_bHasValidData;
284 void KEnvVarProxyDlg::accept()
286 if ( !validate() )
288 setHighLight (mDlg->lbHttp, true);
289 setHighLight (mDlg->lbHttps, true);
290 setHighLight (mDlg->lbFtp, true);
292 QString msg = i18n("You must specify at least one valid proxy "
293 "environment variable.");
295 QString details = i18n("<qt>Make sure you entered the actual environment "
296 "variable name rather than its value. For "
297 "example, if the environment variable is <br /><b>"
298 "HTTP_PROXY=http://localhost:3128</b><br /> you need "
299 "to enter <b>HTTP_PROXY</b> here instead of the "
300 "actual value http://localhost:3128.</qt>");
302 KMessageBox::detailedError( this, msg, details,
303 i18n("Invalid Proxy Setup") );
304 return;
307 KDialog::accept();
310 #include "kenvvarproxydlg.moc"