summaryrefslogtreecommitdiffstats
path: root/kcontrol/kio/kenvvarproxydlg.cpp
blob: 1069aa79748955366937e412ddd61e20a3653e74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
   kenvvarproxydlg.cpp - Proxy configuration dialog

   Copyright (C) 2001- Dawit Alemayehu <adawit@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License (GPL) version 2 as published by the Free Software
   Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include <stdlib.h>

#include <tqlabel.h>
#include <layout.h>
#include <tqcheckbox.h>
#include <tqwhatsthis.h>
#include <tqpushbutton.h>

#include <kdebug.h>
#include <klocale.h>
#include <klineedit.h>
#include <kmessagebox.h>

#include "envvarproxy_ui.h"
#include "kenvvarproxydlg.h"


#define ENV_FTP_PROXY     "FTP_PROXY,ftp_proxy,FTPPROXY,ftpproxy,PROXY,proxy"
#define ENV_HTTP_PROXY    "HTTP_PROXY,http_proxy,HTTPPROXY,httpproxy,PROXY,proxy"
#define ENV_HTTPS_PROXY   "HTTPS_PROXY,https_proxy,HTTPSPROXY,httpsproxy,PROXY,proxy"
#define NO_PROXY          "NO_PROXY,no_proxy"


static TQString getProxyEnv(const TQString& var)
{
  return TQString::fromLocal8Bit(::getenv(var.local8Bit().data()));
}

static bool autoDetectProxySetting(const TQString& type, TQString& proxyEnv)
{
  TQStringList list = TQStringList::split( ',', type);
  TQStringList::ConstIterator it = list.begin();
  TQStringList::ConstIterator end = list.end();

  for(; it != end; ++it)
  {
    if(!getProxyEnv(*it).isEmpty())
    {
      proxyEnv = *it;
      return true;
    }
  }

  return false;
}

KEnvVarProxyDlg::KEnvVarProxyDlg( TQWidget* parent, const char* name )
                :KProxyDialogBase( parent, name, true,
                                   i18n( "Variable Proxy Configuration" ) )
{
  mDlg = new EnvVarProxyDlgUI( this );
  setMainWidget( mDlg );
  mDlg->leHttp->setMinimumWidth( mDlg->leHttp->fontMetrics().maxWidth() * 20 );
  init();
}

KEnvVarProxyDlg::~KEnvVarProxyDlg ()
{
}

void KEnvVarProxyDlg::init()
{
  m_bHasValidData = false;

  connect( mDlg->cbShowValue, TQT_SIGNAL( clicked() ), TQT_SLOT( showValuePressed() ) );
  connect( mDlg->pbVerify, TQT_SIGNAL( clicked() ), TQT_SLOT( verifyPressed() ) );
  connect( mDlg->pbDetect, TQT_SIGNAL( clicked() ), TQT_SLOT( autoDetectPressed() ) );
}

void KEnvVarProxyDlg::setProxyData( const KProxyData& data )
{
  // Setup HTTP Proxy...
  if (!getProxyEnv(data.proxyList["http"]).isEmpty())
    mEnvVarsMap["http"] = data.proxyList["http"];

  // Setup HTTPS Proxy...
  if (!getProxyEnv(data.proxyList["https"]).isEmpty())
    mEnvVarsMap["https"] = data.proxyList["https"];

  // Setup FTP Proxy...
  if (!getProxyEnv(data.proxyList["ftp"]).isEmpty())
    mEnvVarsMap["ftp"] = data.proxyList["ftp"];

  // Setup NO Proxy For...
  TQString noProxyFor = data.noProxyFor.join("");
  if (!getProxyEnv(noProxyFor).isEmpty())
    mEnvVarsMap["noProxy"] = noProxyFor;

  mDlg->cbShowValue->setChecked( data.showEnvVarValue );
  showValue();
}

const KProxyData KEnvVarProxyDlg::data() const
{
  KProxyData data;

  if (m_bHasValidData)
  {
    data.proxyList["http"] = mEnvVarsMap["http"];
    data.proxyList["https"] = mEnvVarsMap["https"];
    data.proxyList["ftp"] = mEnvVarsMap["ftp"];
    data.noProxyFor = mEnvVarsMap["noProxy"];
    data.type = KProtocolManager::EnvVarProxy;
    data.showEnvVarValue = mDlg->cbShowValue->isChecked();
  }

  return data;
}


void KEnvVarProxyDlg::verifyPressed()
{
  if ( !validate() )
  {
    TQString msg = i18n("You must specify at least one valid proxy "
                       "environment variable.");

    TQString details = i18n("<qt>Make sure you entered the actual environment "
                           "variable name rather than its value. For "
                           "example, if the environment variable is <br><b>"
                           "HTTP_PROXY=http://localhost:3128</b><br> you need "
                           "to enter <b>HTTP_PROXY</b> here instead of the "
                           "actual value http://localhost:3128.</qt>");

    KMessageBox::detailedSorry( this, msg, details,
                                i18n("Invalid Proxy Setup") );
  }
  else
  {
    KMessageBox::information( this, i18n("Successfully verified."),
                                    i18n("Proxy Setup") );
  }
}

void KEnvVarProxyDlg::autoDetectPressed()
{
  bool found = false;

  setHighLight (mDlg->lbHttp, false);
  setHighLight (mDlg->lbHttps, false);
  setHighLight (mDlg->lbFtp, false);
  setHighLight (mDlg->lbNoProxy, false);

  // Detect HTTP proxy settings...
  found |= autoDetectProxySetting (TQString::fromLatin1(ENV_HTTP_PROXY), mEnvVarsMap["http"]);

  // Detect HTTPS proxy settings...
  found |= autoDetectProxySetting (TQString::fromLatin1(ENV_HTTPS_PROXY), mEnvVarsMap["https"]);

  // Detect FTP proxy settings...
  found |= autoDetectProxySetting (TQString::fromLatin1(ENV_FTP_PROXY), mEnvVarsMap["ftp"]);

  // Detect the NO_PROXY settings...
  found |= autoDetectProxySetting (TQString::fromLatin1(NO_PROXY), mEnvVarsMap["noProxy"]);

  if ( !found )
  {
    TQString msg = i18n("Did not detect any environment variables "
                       "commonly used to set system wide proxy "
                       "information.");

    TQString details = i18n("<qt>To learn about the variable names the "
                           "automatic detection process searches for, "
                           "press OK, click on the quick help button "
                           "on the window title bar of the "
                           "previous dialog and then click on the "
                           "\"<b>Auto Detect</b>\" button.</qt>");

    KMessageBox::detailedSorry( this, msg, details,
                                i18n("Automatic Proxy Variable Detection") );
    return;
  }

  showValue();
}

void KEnvVarProxyDlg::updateVariables()
{
  TQString text = mDlg->leHttp->text();
  if (mEnvVarsMap["http"] != text)
    mEnvVarsMap["http"] = text;

  text = mDlg->leHttps->text();
  if (mEnvVarsMap["https"] != text)
    mEnvVarsMap["https"] = text;

  text = mDlg->leFtp->text();
  if (mEnvVarsMap["ftp"] != text)
    mEnvVarsMap["ftp"] = text;

  text = mDlg->leNoProxy->text();
  if (mEnvVarsMap["noProxy"] != text)
    mEnvVarsMap["noProxy"] = text;
}

void KEnvVarProxyDlg::showValuePressed()
{
  // Only update the variables whenever
  if (mDlg->cbShowValue->isChecked())
    updateVariables();

  showValue();
}

void KEnvVarProxyDlg::showValue()
{
  bool enable = mDlg->cbShowValue->isChecked();

  mDlg->leHttp->setReadOnly (enable);
  mDlg->leHttps->setReadOnly (enable);
  mDlg->leFtp->setReadOnly (enable);
  mDlg->leNoProxy->setReadOnly (enable);

  if (enable)
  {
    mDlg->leHttp->setText(getProxyEnv(mEnvVarsMap["http"]));
    mDlg->leHttps->setText(getProxyEnv(mEnvVarsMap["https"]));
    mDlg->leFtp->setText(getProxyEnv(mEnvVarsMap["ftp"]));
    mDlg->leNoProxy->setText(getProxyEnv(mEnvVarsMap["noProxy"]));
  }
  else
  {
    mDlg->leHttp->setText(mEnvVarsMap["http"]);
    mDlg->leHttps->setText(mEnvVarsMap["https"]);
    mDlg->leFtp->setText(mEnvVarsMap["ftp"]);
    mDlg->leNoProxy->setText(mEnvVarsMap["noProxy"]);
  }
}

bool KEnvVarProxyDlg::validate(bool erase)
{
  m_bHasValidData = false;

  if(!mDlg->cbShowValue->isChecked())
    updateVariables();

  bool notFound = getProxyEnv(mEnvVarsMap["http"]).isEmpty();
  m_bHasValidData |= !notFound;
  setHighLight (mDlg->lbHttp, notFound);
  if(notFound && erase) mEnvVarsMap["http"] = TQString::null;

  notFound = getProxyEnv(mEnvVarsMap["https"]).isEmpty();
  m_bHasValidData |= !notFound;
  setHighLight (mDlg->lbHttps, notFound);
  if(notFound && erase) mEnvVarsMap["https"] = TQString::null;

  notFound = getProxyEnv(mEnvVarsMap["ftp"]).isEmpty();
  m_bHasValidData |= !notFound;
  setHighLight (mDlg->lbFtp, notFound);
  if(notFound && erase) mEnvVarsMap["ftp"] = TQString::null;

  notFound = getProxyEnv(mEnvVarsMap["noProxy"]).isEmpty();
  m_bHasValidData |= !notFound;
  setHighLight (mDlg->lbNoProxy, notFound);
  if(notFound && erase) mEnvVarsMap["noProxy"] = TQString::null;

  return m_bHasValidData;
}

void KEnvVarProxyDlg::slotOk()
{
  if(!validate(true))
  {
    TQString msg = i18n("You must specify at least one valid proxy "
                       "environment variable.");

    TQString details = i18n("<qt>Make sure you entered the actual environment "
                           "variable name rather than its value. For "
                           "example, if the environment variable is <br><b>"
                           "HTTP_PROXY=http://localhost:3128</b><br> you need "
                           "to enter <b>HTTP_PROXY</b> here instead of the "
                           "actual value http://localhost:3128.</qt>");

    KMessageBox::detailedError( this, msg, details,
                                i18n("Invalid Proxy Setup") );
    return;
  }

  KDialogBase::slotOk ();
}

#include "kenvvarproxydlg.moc"