summaryrefslogtreecommitdiffstats
path: root/kcontrol/tdeio/kproxydlg.cpp
blob: a165f95764b681cb81ba97653689f364fd2ba2c5 (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
   kproxydlg.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 <tqlabel.h>
#include <tqlayout.h>
#include <tqregexp.h>
#include <tqcheckbox.h>
#include <tqwhatsthis.h>
#include <tqbuttongroup.h>
#include <tqradiobutton.h>
#include <tqtabwidget.h>

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

#include "ksaveioconfig.h"
#include "kenvvarproxydlg.h"
#include "kmanualproxydlg.h"

#include "socks.h"
#include "kproxydlg.h"
#include "kproxydlg_ui.h"

KProxyOptions::KProxyOptions (TQWidget* parent )
              :TDECModule (parent, "kcmtdeio")
{
  TQVBoxLayout *layout = new TQVBoxLayout(this);
  
  mTab = new TQTabWidget(this);
  layout->addWidget(mTab);

  mProxy  = new KProxyDialog(mTab);
  mSocks = new KSocksConfig(mTab);

  mTab->addTab(mProxy, i18n("&Proxy"));
  mTab->addTab(mSocks, i18n("&SOCKS"));

  connect(mProxy, TQT_SIGNAL(changed(bool)), TQT_SIGNAL(changed(bool)));
  connect(mSocks, TQT_SIGNAL(changed(bool)), TQT_SIGNAL(changed(bool)));
  connect(mTab, TQT_SIGNAL(currentChanged(TQWidget *)), TQT_SIGNAL(quickHelpChanged()));
}

KProxyOptions::~KProxyOptions()
{
}

void KProxyOptions::load()
{
  mProxy->load();
  mSocks->load();
}

void KProxyOptions::save()
{
  mProxy->save();
  mSocks->save();
}

void KProxyOptions::defaults()
{
  mProxy->defaults();
  mSocks->defaults();
}

TQString KProxyOptions::quickHelp() const
{
  TQWidget *w = mTab->currentPage();
  
  if (w && w->inherits("TDECModule"))
  {
     TDECModule *m = static_cast<TDECModule *>(w);
     return m->quickHelp();
  }
  
  return TQString::null;
}


KProxyDialog::KProxyDialog( TQWidget* parent)
             :TDECModule( parent, "kcmtdeio" )
{
  TQVBoxLayout* mainLayout = new TQVBoxLayout( this, KDialog::marginHint(),
                                              KDialog::spacingHint() );
  
  mDlg = new KProxyDialogUI( this );
  mainLayout->addWidget( mDlg );
  mainLayout->addStretch();
  
  // signals and slots connections
  connect( mDlg->rbNoProxy, TQT_SIGNAL( toggled(bool) ),
            TQT_SLOT( slotUseProxyChanged() ) );
  
  connect( mDlg->rbAutoDiscover, TQT_SIGNAL( toggled(bool) ),
            TQT_SLOT( slotChanged() ) );
  connect( mDlg->rbAutoScript, TQT_SIGNAL( toggled(bool) ),
            TQT_SLOT( slotChanged() ) );
  
  connect( mDlg->rbPrompt, TQT_SIGNAL( toggled(bool) ),
            TQT_SLOT( slotChanged() ) );
  connect( mDlg->rbPresetLogin, TQT_SIGNAL( toggled(bool) ),
            TQT_SLOT( slotChanged() ) );
  
  connect( mDlg->cbPersConn, TQT_SIGNAL( toggled(bool) ),
            TQT_SLOT( slotChanged() ) );
  
  connect( mDlg->location, TQT_SIGNAL( textChanged(const TQString&) ),
            TQT_SLOT( slotChanged() ) );
  
  connect( mDlg->pbEnvSetup, TQT_SIGNAL( clicked() ), TQT_SLOT( setupEnvProxy() ) );
  connect( mDlg->pbManSetup, TQT_SIGNAL( clicked() ), TQT_SLOT( setupManProxy() ) );
  
  load();
}

KProxyDialog::~KProxyDialog()
{
  delete mData;
  mData = 0;
}

void KProxyDialog::load()
{
  mDefaultData = false;
  mData = new KProxyData;

  KProtocolManager proto;
  bool useProxy = proto.useProxy();
  mData->type = proto.proxyType();
  mData->proxyList["http"] = proto.proxyFor( "http" );
  mData->proxyList["https"] = proto.proxyFor( "https" );
  mData->proxyList["ftp"] = proto.proxyFor( "ftp" );
  mData->proxyList["script"] = proto.proxyConfigScript();
  mData->useReverseProxy = proto.useReverseProxy();
  mData->noProxyFor = TQStringList::split( TQRegExp("[',''\t'' ']"),
                                           proto.noProxyForRaw() );

  mDlg->gbAuth->setEnabled( useProxy );
  mDlg->gbOptions->setEnabled( useProxy );

  mDlg->cbPersConn->setChecked( proto.persistentProxyConnection() );

  if ( !mData->proxyList["script"].isEmpty() )
    mDlg->location->lineEdit()->setText( mData->proxyList["script"] );

  switch ( mData->type )
  {
    case KProtocolManager::WPADProxy:
      mDlg->rbAutoDiscover->setChecked( true );
      break;
    case KProtocolManager::PACProxy:
      mDlg->rbAutoScript->setChecked( true );
      break;
    case KProtocolManager::ManualProxy:
      mDlg->rbManual->setChecked( true );
      break;
    case KProtocolManager::EnvVarProxy:
      mDlg->rbEnvVar->setChecked( true );
      break;
    case KProtocolManager::NoProxy:
    default:
      mDlg->rbNoProxy->setChecked( true );
      break;
  }

  switch( proto.proxyAuthMode() )
  {
    case KProtocolManager::Prompt:
      mDlg->rbPrompt->setChecked( true );
      break;
    case KProtocolManager::Automatic:
      mDlg->rbPresetLogin->setChecked( true );
    default:
      break;
  }
}

void KProxyDialog::save()
{
  bool updateProxyScout = false;

  if (mDefaultData)
    mData->reset ();

  if ( mDlg->rbNoProxy->isChecked() )
  {
    KSaveIOConfig::setProxyType( KProtocolManager::NoProxy );
  }
  else
  {
    if ( mDlg->rbAutoDiscover->isChecked() )
    {
      KSaveIOConfig::setProxyType( KProtocolManager::WPADProxy );
      updateProxyScout = true;
    }
    else if ( mDlg->rbAutoScript->isChecked() )
    {
      KURL u( mDlg->location->lineEdit()->text() );

      if ( !u.isValid() )
      {
        showInvalidMessage( i18n("The address of the automatic proxy "
                                 "configuration script is invalid. Please "
                                 "correct this problem before proceeding. "
                                 "Otherwise, your changes you will be "
                                 "ignored.") );
        return;
      }
      else
      {
        KSaveIOConfig::setProxyType( KProtocolManager::PACProxy );
        mData->proxyList["script"] = u.url();
        updateProxyScout = true;
      }
    }
    else if ( mDlg->rbManual->isChecked() )
    {
      if ( mData->type != KProtocolManager::ManualProxy )
      {
        // Let's try a bit harder to determine if the previous
        // proxy setting was indeed a manual proxy
        KURL u ( mData->proxyList["http"] );
        bool validProxy = (u.isValid() && u.port() != 0);
        u= mData->proxyList["https"];
        validProxy |= (u.isValid() && u.port() != 0);
        u= mData->proxyList["ftp"];
        validProxy |= (u.isValid() && u.port() != 0);

        if (!validProxy)
        {
          showInvalidMessage();
          return;
        }

        mData->type = KProtocolManager::ManualProxy;
      }

      KSaveIOConfig::setProxyType( KProtocolManager::ManualProxy );
    }
    else if ( mDlg->rbEnvVar->isChecked() )
    {
      if ( mData->type != KProtocolManager::EnvVarProxy )
      {
        showInvalidMessage();
        return;
      }

      KSaveIOConfig::setProxyType( KProtocolManager::EnvVarProxy );
    }

    if ( mDlg->rbPrompt->isChecked() )
      KSaveIOConfig::setProxyAuthMode( KProtocolManager::Prompt );
    else if ( mDlg->rbPresetLogin->isChecked() )
      KSaveIOConfig::setProxyAuthMode( KProtocolManager::Automatic );
  }

  KSaveIOConfig::setPersistentProxyConnection( mDlg->cbPersConn->isChecked() );

  // Save the common proxy setting...
  KSaveIOConfig::setProxyFor( "ftp", mData->proxyList["ftp"] );
  KSaveIOConfig::setProxyFor( "http", mData->proxyList["http"] );
  KSaveIOConfig::setProxyFor( "https", mData->proxyList["https"] );

  KSaveIOConfig::setProxyConfigScript( mData->proxyList["script"] );
  KSaveIOConfig::setUseReverseProxy( mData->useReverseProxy );
  KSaveIOConfig::setNoProxyFor( mData->noProxyFor.join(",") );


  KSaveIOConfig::updateRunningIOSlaves (this);
  if ( updateProxyScout )
    KSaveIOConfig::updateProxyScout( this );

  emit changed( false );
}

void KProxyDialog::defaults()
{
  mDefaultData = true;
  mDlg->rbNoProxy->setChecked( true );
  mDlg->location->lineEdit()->clear();
  mDlg->cbPersConn->setChecked( false );
  emit changed( true );
}

void KProxyDialog::setupManProxy()
{
  KManualProxyDlg dlgManual( this );

  dlgManual.setProxyData( *mData );

  if ( dlgManual.exec() == TQDialog::Accepted )
  {
    *mData = dlgManual.data();
    mDlg->rbManual->setChecked(true);
    emit changed( true );
  }
}

void KProxyDialog::setupEnvProxy()
{
  KEnvVarProxyDlg dlgEnv( this );

  dlgEnv.setProxyData( *mData );

  if ( dlgEnv.exec() == TQDialog::Accepted )
  {
    *mData = dlgEnv.data();
    mDlg->rbEnvVar->setChecked(true);
    emit changed( true );
  }
}

void KProxyDialog::slotChanged()
{
  mDefaultData = false;
  emit changed( true );
}

void KProxyDialog::slotUseProxyChanged()
{
  mDefaultData = false;
  bool useProxy = !(mDlg->rbNoProxy->isChecked());
  mDlg->gbAuth->setEnabled(useProxy);
  mDlg->gbOptions->setEnabled(useProxy);
  emit changed( true );
}

TQString KProxyDialog::quickHelp() const
{
  return i18n( "<h1>Proxy</h1>"
               "<p>A proxy server is an intermediate program that sits between "
               "your machine and the Internet and provides services such as "
               "web page caching and/or filtering.</p>"
               "<p>Caching proxy servers give you faster access to sites you have "
               "already visited by locally storing or caching the content of those "
               "pages; filtering proxy servers, on the other hand, provide the "
               "ability to block out requests for ads, spam, or anything else you "
               "want to block.</p>"
               "<p><u>Note:</u> Some proxy servers provide both services.</p>" );
}

void KProxyDialog::showInvalidMessage( const TQString& _msg )
{
  TQString msg;

  if( !_msg.isEmpty() )
    msg = _msg;
  else
    msg = i18n( "<qt>The proxy settings you specified are invalid."
                "<p>Please click on the <b>Setup...</b> "
                "button and correct the problem before proceeding; "
                "otherwise your changes will be ignored.</qt>" );

  KMessageBox::error( this, msg, i18n("Invalid Proxy Setup") );
}

#include "kproxydlg.moc"