~ubuntu-branches/debian/sid/kde-baseapps/sid

« back to all changes in this revision

Viewing changes to konqueror/settings/kio/kmanualproxydlg.cpp

  • Committer: Package Import Robot
  • Author(s): Modestas Vainius, Eshat Cakar, Pino Toscano
  • Date: 2012-06-09 22:18:08 UTC
  • mfrom: (4.2.1) (6.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20120609221808-h1l0ekd5qmb8nefr
Tags: 4:4.8.4-1
* New upstream release.

[ Eshat Cakar ]
* Add watch file.
* Bump kde-sc-dev-latest build dependency to version 4:4.8.4.
* Update installed files.

[ Pino Toscano ]
* Move files of the konqueror documentation from kde-baseapps-data to
  konqueror itself.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
   kmanualproxydlg.cpp - Proxy configuration dialog
3
 
 
4
 
   Copyright (C) 2001-2004 Dawit Alemayehu <adawit@kde.org>
5
 
 
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.
10
 
 
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.
15
 
 
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.
20
 
*/
21
 
 
22
 
// Own
23
 
#include "kmanualproxydlg.h"
24
 
 
25
 
// Qt
26
 
#include <QtGui/QCheckBox>
27
 
#include <QtGui/QLabel>
28
 
#include <QtGui/QLayout>
29
 
#include <QtGui/QPushButton>
30
 
 
31
 
// KDE
32
 
#include <kdebug.h>
33
 
#include <kiconloader.h>
34
 
#include <kicontheme.h>
35
 
#include <kinputdialog.h>
36
 
#include <kio/ioslave_defaults.h>
37
 
#include <klineedit.h>
38
 
#include <klistwidget.h>
39
 
#include <klocale.h>
40
 
#include <kmessagebox.h>
41
 
#include <knuminput.h>
42
 
#include <kurifilter.h>
43
 
 
44
 
 
45
 
 
46
 
KManualProxyDlg::KManualProxyDlg( QWidget* parent, const char* name )
47
 
                :KProxyDialogBase( parent, name, true,
48
 
                                   i18nc("@title:window", "Manual Proxy Configuration") )
49
 
{
50
 
    mDlg = new ManualProxyDlgUI (this);
51
 
    setMainWidget(mDlg);
52
 
    mDlg->pbCopyDown->setIcon( KIcon("go-down") );
53
 
    QSizePolicy sizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
54
 
    sizePolicy.setHeightForWidth( mDlg->pbCopyDown->sizePolicy().hasHeightForWidth() );
55
 
    mDlg->pbCopyDown->setSizePolicy( sizePolicy );
56
 
 
57
 
    init();
58
 
}
59
 
 
60
 
void KManualProxyDlg::init()
61
 
{
62
 
    mDlg->sbHttp->setRange( 0, MAX_PORT_VALUE );
63
 
    mDlg->sbHttps->setRange( 0, MAX_PORT_VALUE );
64
 
    mDlg->sbFtp->setRange( 0, MAX_PORT_VALUE );
65
 
 
66
 
    connect( mDlg->pbNew, SIGNAL( clicked() ), SLOT( newPressed() ) );
67
 
    connect( mDlg->pbChange, SIGNAL( clicked() ), SLOT( changePressed() ) );
68
 
    connect( mDlg->pbDelete, SIGNAL( clicked() ), SLOT( deletePressed() ) );
69
 
    connect( mDlg->pbDeleteAll, SIGNAL( clicked() ), SLOT( deleteAllPressed() ) );
70
 
 
71
 
    connect( mDlg->lbExceptions, SIGNAL(itemSelectionChanged()), SLOT(updateButtons()) );
72
 
    connect( mDlg->lbExceptions, SIGNAL(itemDoubleClicked (QListWidgetItem *)), SLOT(changePressed()));
73
 
 
74
 
    connect( mDlg->cbSameProxy, SIGNAL( toggled(bool) ), SLOT( sameProxy(bool) ) );
75
 
    connect( mDlg->pbCopyDown, SIGNAL( clicked() ), SLOT( copyDown() ) );
76
 
 
77
 
    connect( mDlg->leHttp, SIGNAL(textChanged(const QString&)), SLOT(textChanged(const QString&)) );
78
 
    connect( mDlg->sbHttp, SIGNAL(valueChanged(int)), SLOT(valueChanged (int)) );
79
 
 
80
 
    connect( this, SIGNAL(okClicked()), this, SLOT(slotOk()));
81
 
}
82
 
 
83
 
void KManualProxyDlg::setProxyData( const KProxyData &data )
84
 
{
85
 
    KUrl url;
86
 
 
87
 
    // Set the HTTP proxy...
88
 
    if (!isValidURL(data.proxyList["http"], &url))
89
 
        mDlg->sbHttp->setValue( DEFAULT_PROXY_PORT );
90
 
    else
91
 
    {
92
 
        int port = url.port();
93
 
        if ( port <= 0 )
94
 
            port = DEFAULT_PROXY_PORT;
95
 
 
96
 
        url.setPort( -1 );
97
 
        mDlg->leHttp->setText( url.url() );
98
 
        mDlg->sbHttp->setValue( port );
99
 
    }
100
 
 
101
 
    bool useSameProxy = (!mDlg->leHttp->text().isEmpty () &&
102
 
                         data.proxyList["http"] == data.proxyList["https"] &&
103
 
                         data.proxyList["http"] == data.proxyList["ftp"]);
104
 
 
105
 
    mDlg->cbSameProxy->setChecked ( useSameProxy );
106
 
 
107
 
    if ( useSameProxy )
108
 
    {
109
 
      mDlg->leHttps->setText ( mDlg->leHttp->text() );
110
 
      mDlg->leFtp->setText ( mDlg->leHttp->text() );
111
 
      mDlg->sbHttps->setValue( mDlg->sbHttp->value() );
112
 
      mDlg->sbFtp->setValue( mDlg->sbHttp->value() );
113
 
 
114
 
      sameProxy ( true );
115
 
    }
116
 
    else
117
 
    {
118
 
      // Set the HTTPS proxy...
119
 
      if( !isValidURL( data.proxyList["https"], &url ) )
120
 
          mDlg->sbHttps->setValue( DEFAULT_PROXY_PORT );
121
 
      else
122
 
      {
123
 
          int port = url.port();
124
 
          if ( port <= 0 )
125
 
              port = DEFAULT_PROXY_PORT;
126
 
 
127
 
          url.setPort( -1 );
128
 
          mDlg->leHttps->setText( url.url() );
129
 
          mDlg->sbHttps->setValue( port );
130
 
      }
131
 
 
132
 
      // Set the FTP proxy...
133
 
      if( !isValidURL( data.proxyList["ftp"], &url ) )
134
 
          mDlg->sbFtp->setValue( DEFAULT_PROXY_PORT );
135
 
      else
136
 
      {
137
 
          int port = url.port();
138
 
          if ( port <= 0 )
139
 
              port = DEFAULT_PROXY_PORT;
140
 
 
141
 
          url.setPort( -1 );
142
 
          mDlg->leFtp->setText( url.url() );
143
 
          mDlg->sbFtp->setValue( port );
144
 
      }
145
 
    }
146
 
 
147
 
    QStringList::ConstIterator it = data.noProxyFor.begin();
148
 
    for( ; it != data.noProxyFor.end(); ++it )
149
 
    {
150
 
      // "no_proxy" is a keyword used by the environment variable
151
 
      // based configuration. We ignore it here as it is not applicable...
152
 
      if ((*it).toLower() != "no_proxy" && !(*it).isEmpty())
153
 
      {
154
 
        // Validate the NOPROXYFOR entries and use only hostnames if the entry is
155
 
        // a valid or legitimate URL. NOTE: needed to catch manual manipulation
156
 
        // of the proxy config files...
157
 
        if( isValidURL( *it ) || ((*it).length() >= 3 && (*it).startsWith('.')) )
158
 
          mDlg->lbExceptions->addItem( *it );
159
 
      }
160
 
    }
161
 
 
162
 
    mDlg->cbReverseProxy->setChecked( data.useReverseProxy );
163
 
}
164
 
 
165
 
const KProxyData KManualProxyDlg::data() const
166
 
{
167
 
    KProxyData data;
168
 
 
169
 
    if (!m_bHasValidData)
170
 
      return data;
171
 
 
172
 
    data.proxyList["http"] = urlFromInput( mDlg->leHttp, mDlg->sbHttp );
173
 
 
174
 
    if ( mDlg->cbSameProxy->isChecked () )
175
 
    {
176
 
        data.proxyList["https"] = data.proxyList["http"];
177
 
        data.proxyList["ftp"] = data.proxyList["http"];
178
 
    }
179
 
    else
180
 
    {
181
 
        data.proxyList["https"] = urlFromInput( mDlg->leHttps, mDlg->sbHttps );
182
 
        data.proxyList["ftp"] = urlFromInput( mDlg->leFtp, mDlg->sbFtp );
183
 
    }
184
 
 
185
 
    if ( mDlg->lbExceptions->count() )
186
 
    {
187
 
        for ( int rowIndex = 0 ; rowIndex < mDlg->lbExceptions->count() ; rowIndex++ ) {
188
 
            data.noProxyFor << mDlg->lbExceptions->item(rowIndex)->text();
189
 
        }
190
 
    }
191
 
 
192
 
    data.type = KProtocolManager::ManualProxy;
193
 
    data.useReverseProxy = mDlg->cbReverseProxy->isChecked();
194
 
 
195
 
    return data;
196
 
}
197
 
 
198
 
void KManualProxyDlg::sameProxy( bool enable )
199
 
{
200
 
    mDlg->leHttps->setEnabled (!enable );
201
 
    mDlg->leFtp->setEnabled (!enable );
202
 
    mDlg->sbHttps->setEnabled (!enable );
203
 
    mDlg->sbFtp->setEnabled (!enable );
204
 
    mDlg->pbCopyDown->setEnabled( !enable );
205
 
 
206
 
    if (enable)
207
 
    {
208
 
      mOldFtpText = mDlg->leFtp->text();
209
 
      mOldHttpsText = mDlg->leHttps->text();
210
 
 
211
 
      mOldFtpPort = mDlg->sbFtp->value();
212
 
      mOldHttpsPort = mDlg->sbHttps->value();
213
 
 
214
 
      int port = mDlg->sbHttp->value();
215
 
      QString text = mDlg->leHttp->text();
216
 
 
217
 
      mDlg->leFtp->setText (text);
218
 
      mDlg->leHttps->setText (text);
219
 
 
220
 
      mDlg->sbFtp->setValue (port);
221
 
      mDlg->sbHttps->setValue (port);
222
 
 
223
 
      if (mDlg->lbHttps->font().bold())
224
 
        setHighLight( mDlg->lbHttps, false );
225
 
 
226
 
      if (mDlg->lbFtp->font().bold())
227
 
        setHighLight( mDlg->lbFtp, false );
228
 
    }
229
 
    else
230
 
    {
231
 
      mDlg->leFtp->setText (mOldFtpText);
232
 
      mDlg->leHttps->setText (mOldHttpsText);
233
 
 
234
 
      mDlg->sbFtp->setValue (mOldFtpPort);
235
 
      mDlg->sbHttps->setValue (mOldHttpsPort);
236
 
    }
237
 
}
238
 
 
239
 
bool KManualProxyDlg::validate()
240
 
{
241
 
    KUrl filteredURL;
242
 
    unsigned short count = 0;
243
 
 
244
 
    if ( isValidURL( mDlg->leHttp->text(), &filteredURL ) )
245
 
    {
246
 
        mDlg->leHttp->setText( filteredURL.url() );
247
 
        count++;
248
 
    }
249
 
    else
250
 
        setHighLight( mDlg->lbHttp, true );
251
 
 
252
 
    if ( !mDlg->cbSameProxy->isChecked () )
253
 
    {
254
 
        if ( isValidURL( mDlg->leHttps->text(), &filteredURL ) )
255
 
        {
256
 
            mDlg->leHttps->setText( filteredURL.url() );
257
 
            count++;
258
 
        }
259
 
        else
260
 
            setHighLight( mDlg->lbHttps, true );
261
 
 
262
 
        if ( isValidURL( mDlg->leFtp->text(), &filteredURL ) )
263
 
        {
264
 
            mDlg->leFtp->setText( filteredURL.url() );
265
 
            count++;
266
 
        }
267
 
        else
268
 
            setHighLight( mDlg->lbFtp, true );
269
 
    }
270
 
 
271
 
    if ( count == 0 )
272
 
    {
273
 
        showErrorMsg( i18nc("@title:window", "Invalid Proxy Setting"),
274
 
                      i18n("One or more of the specified proxy settings are "
275
 
                           "invalid. The incorrect entries are highlighted.") );
276
 
    }
277
 
 
278
 
    return (count > 0);
279
 
}
280
 
 
281
 
void KManualProxyDlg::textChanged(const QString& text)
282
 
{
283
 
    if (!mDlg->cbSameProxy->isChecked())
284
 
        return;
285
 
 
286
 
    mDlg->leFtp->setText( text );
287
 
    mDlg->leHttps->setText( text );
288
 
}
289
 
 
290
 
void KManualProxyDlg::valueChanged(int value)
291
 
{
292
 
    if (!mDlg->cbSameProxy->isChecked())
293
 
        return;
294
 
 
295
 
    mDlg->sbFtp->setValue (value);
296
 
    mDlg->sbHttps->setValue (value);
297
 
 }
298
 
 
299
 
void KManualProxyDlg::copyDown()
300
 
{
301
 
    int action = -1;
302
 
 
303
 
    if ( !mDlg->leHttp->text().isEmpty() )
304
 
        action += 4;
305
 
    else if ( !mDlg->leHttps->text().isEmpty() )
306
 
        action += 3;
307
 
 
308
 
    switch ( action )
309
 
    {
310
 
      case 3:
311
 
        mDlg->leHttps->setText( mDlg->leHttp->text() );
312
 
        mDlg->sbHttps->setValue( mDlg->sbHttp->value() );
313
 
        mDlg->leFtp->setText( mDlg->leHttp->text() );
314
 
        mDlg->sbFtp->setValue( mDlg->sbHttp->value() );
315
 
        break;
316
 
      case 2:
317
 
        mDlg->leFtp->setText( mDlg->leHttps->text() );
318
 
        mDlg->sbFtp->setValue( mDlg->sbHttps->value() );
319
 
        break;
320
 
      case 1:
321
 
      case 0:
322
 
      default:
323
 
        break;
324
 
    }
325
 
}
326
 
 
327
 
void KManualProxyDlg::slotOk()
328
 
{
329
 
    if ( m_bHasValidData || validate() )
330
 
      m_bHasValidData = true;
331
 
}
332
 
 
333
 
bool KManualProxyDlg::handleDuplicate( const QString& site )
334
 
{
335
 
    for ( int rowIndex = 0 ; rowIndex < mDlg->lbExceptions->count() ; rowIndex++ )
336
 
    {
337
 
        QListWidgetItem* item = mDlg->lbExceptions->item(rowIndex);
338
 
 
339
 
        if ( item->text().lastIndexOf( site ) != -1 &&
340
 
             item != mDlg->lbExceptions->currentItem() )
341
 
        {
342
 
            QString msg = i18n("You entered a duplicate address. "
343
 
                               "Please try again.");
344
 
            QString details = i18n("<qt><center><b>%1</b></center> "
345
 
                                   "is already in the list.</qt>", site);
346
 
            KMessageBox::detailedError( this, msg, details, i18nc("@title:window", "Duplicate Entry") );
347
 
            return true;
348
 
        }
349
 
    }
350
 
    return false;
351
 
}
352
 
 
353
 
void KManualProxyDlg::newPressed()
354
 
{
355
 
  QString result;
356
 
  if( getException(result, i18nc("@title:window", "New Exception")) && !handleDuplicate(result) )
357
 
    mDlg->lbExceptions->addItem( result );
358
 
}
359
 
 
360
 
void KManualProxyDlg::changePressed()
361
 
{
362
 
  QString result;
363
 
  if( getException( result, i18nc("@title:window", "Change Exception"),
364
 
                    mDlg->lbExceptions->currentItem()->text() ) &&
365
 
      !handleDuplicate( result ) )
366
 
      mDlg->lbExceptions->currentItem()->setText(result);
367
 
}
368
 
 
369
 
void KManualProxyDlg::deletePressed()
370
 
{
371
 
    delete mDlg->lbExceptions->takeItem( mDlg->lbExceptions->currentRow() );
372
 
    if(mDlg->lbExceptions->currentItem())
373
 
       mDlg->lbExceptions->currentItem()->setSelected(true);
374
 
    updateButtons();
375
 
}
376
 
 
377
 
void KManualProxyDlg::deleteAllPressed()
378
 
{
379
 
    mDlg->lbExceptions->clear();
380
 
    updateButtons();
381
 
}
382
 
 
383
 
void KManualProxyDlg::updateButtons()
384
 
{
385
 
    bool hasItems = mDlg->lbExceptions->count() > 0;
386
 
    bool itemSelected = (hasItems && mDlg->lbExceptions->currentItem()!=0);
387
 
 
388
 
    mDlg->pbDeleteAll->setEnabled( hasItems );
389
 
    mDlg->pbDelete->setEnabled( itemSelected );
390
 
    mDlg->pbChange->setEnabled( itemSelected );
391
 
}
392
 
 
393
 
QString KManualProxyDlg::urlFromInput(const KLineEdit* edit,
394
 
                                      const KIntSpinBox* spin) const
395
 
{
396
 
  if (!edit || edit->text().isEmpty())
397
 
    return QString();
398
 
 
399
 
  KUrl u( edit->text() );
400
 
 
401
 
  if (spin)
402
 
    u.setPort( spin->value() );
403
 
 
404
 
  return u.url();
405
 
}
406
 
 
407
 
bool KManualProxyDlg::isValidURL( const QString& _url, KUrl* result ) const
408
 
{
409
 
    KUrl url (_url);
410
 
 
411
 
    QStringList filters;
412
 
    filters << "kshorturifilter" << "localdomainurifilter";
413
 
 
414
 
    // If the typed URL is malformed, and the filters cannot filter it
415
 
    // then it must be an invalid entry.
416
 
    if( !(url.isValid() && KUriFilter::self()->filterUri(url, filters) &&
417
 
        url.hasHost()) )
418
 
      return false;
419
 
 
420
 
    QString host (url.host());
421
 
 
422
 
    // We only check for a relevant subset of characters that are
423
 
    // not allowed in <authority> component of a URL.
424
 
    if ( host.contains ('*') || host.contains (' ') || host.contains ('?') )
425
 
      return false;
426
 
 
427
 
    if ( result )
428
 
      *result = url;
429
 
 
430
 
    return true;
431
 
}
432
 
 
433
 
void KManualProxyDlg::showErrorMsg( const QString& caption,
434
 
                                    const QString& message )
435
 
{
436
 
  QString cap( caption );
437
 
  QString msg( message );
438
 
 
439
 
  if ( cap.isEmpty() )
440
 
    cap = i18nc("@title:window", "Invalid Entry");
441
 
 
442
 
  if ( msg.isEmpty() )
443
 
    msg = i18n("The address you have entered is not valid.");
444
 
 
445
 
  QString details = i18n("<qt>Make sure none of the addresses or URLs you "
446
 
                          "specified contain invalid or wildcard characters "
447
 
                          "such as spaces, asterisks (*), or question marks(?).<br /><br />"
448
 
                          "<u>Examples of VALID entries:</u><br />"
449
 
                          "<code>http://mycompany.com, 192.168.10.1, "
450
 
                          "mycompany.com, localhost, http://localhost</code><br /><br />"
451
 
                          "<u>Examples of INVALID entries:</u><br />"
452
 
                          "<code>http://my company.com, http:/mycompany,com "
453
 
                          "file:/localhost</code></qt>");
454
 
 
455
 
  KMessageBox::detailedError( this, msg, details, cap );
456
 
}
457
 
 
458
 
bool KManualProxyDlg::getException ( QString& result,
459
 
                                     const QString& caption,
460
 
                                     const QString& value )
461
 
{
462
 
    QString label;
463
 
 
464
 
    // Specify the appropriate message...
465
 
    if ( mDlg->cbReverseProxy->isChecked() )
466
 
      label = i18n("Enter the URL or address that should use the above proxy "
467
 
                   "settings:");
468
 
    else
469
 
      label = i18n("Enter the address or URL that should be excluded from "
470
 
                   "using the above proxy settings:");
471
 
 
472
 
    QString whatsThis = i18n("<qt>Enter a valid address or URL.<br /><br />"
473
 
                            "<b><u>NOTE:</u></b> Wildcard matching such as "
474
 
                            "<code>*.kde.org</code> is not supported. If you want "
475
 
                            "to match any host in the <code>.kde.org</code> domain, "
476
 
                            "e.g. <code>printing.kde.org</code>, then simply enter "
477
 
                            "<code>.kde.org</code>.</qt>");
478
 
 
479
 
    bool ok;
480
 
    result = KInputDialog::getText( caption, label, value, &ok, this,
481
 
                                    0, QString(), whatsThis );
482
 
 
483
 
    // If the user pressed cancel, do nothing...
484
 
    if (!ok)
485
 
      return false;
486
 
 
487
 
    // If the typed URL is malformed, and the filters cannot filter it
488
 
    // then it must be an invalid entry,
489
 
    if( isValidURL(result) || (result.length() >= 3 && result.startsWith('.')))
490
 
      return true;
491
 
 
492
 
    showErrorMsg();
493
 
    return false;
494
 
}
495
 
 
496
 
#include "kmanualproxydlg.moc"