~ubuntu-branches/ubuntu/utopic/kdebase/utopic

« back to all changes in this revision

Viewing changes to apps/konqueror/settings/kio/useragentdlg.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Debian Qt/KDE Maintainers, José Manuel Santamaría Lema, Modestas Vainius
  • Date: 2011-05-26 02:53:50 UTC
  • mfrom: (0.7.7 upstream) (0.4.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 296.
  • Revision ID: james.westby@ubuntu.com-20110526025350-7o10g65yegec2rnq
Tags: 4:4.6.3-1
* New upstream release.

[ José Manuel Santamaría Lema ]
* Bump kde-sc-dev-latest build dependency to 4:4.6.3.
* Bump Standards-Version to 3.9.2; no changes needed.

[ Modestas Vainius ]
* Enable DLRestrictions for libraries in this package. Requires
  libdlrestrictions-dev 0.14 and kdelibs5-dev 4:4.6.3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
   Original Authors:
3
 
   Copyright (c) Kalle Dalheimer <kalle@kde.org> 1997
4
 
   Copyright (c) David Faure <faure@kde.org> 1998
5
 
   Copyright (c) Dirk Mueller <mueller@kde.org> 2000
6
 
 
7
 
   Completely re-written by:
8
 
   Copyright (C) 2000- Dawit Alemayehu <adawit@kde.org>
9
 
 
10
 
   This library is free software; you can redistribute it and/or
11
 
   modify it under the terms of the GNU General Public License (GPL)
12
 
   version 2 as published by the Free Software Foundation.
13
 
 
14
 
   This library is distributed in the hope that it will be useful,
15
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 
   Library General Public License for more details.
18
 
 
19
 
   You should have received a copy of the GNU General Public License
20
 
   along with this library; see the file COPYING.LIB.  If not, write to
21
 
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22
 
   Boston, MA 02110-1301, USA.
23
 
*/
24
 
 
25
 
// Own
26
 
#include "useragentdlg.h"
27
 
 
28
 
// Local
29
 
#include "ksaveioconfig.h"
30
 
#include "useragentinfo.h"
31
 
#include "useragentselectordlg.h"
32
 
 
33
 
// Qt
34
 
#include <QtGui/QLayout>
35
 
#include <QtGui/QCheckBox>
36
 
#include <QtGui/QLineEdit>
37
 
#include <QtGui/QPushButton>
38
 
#include <QtGui/QBoxLayout>
39
 
#include <QtGui/QTreeWidget>
40
 
 
41
 
// KDE
42
 
#include <kdebug.h>
43
 
#include <kconfig.h>
44
 
#include <klocale.h>
45
 
#include <kmessagebox.h>
46
 
#include <kio/http_slave_defaults.h>
47
 
#include <kgenericfactory.h>
48
 
 
49
 
 
50
 
K_PLUGIN_FACTORY_DECLARATION(KioConfigFactory)
51
 
 
52
 
typedef QList<QTreeWidgetItem*> SiteList;
53
 
typedef SiteList::iterator SiteListIterator;
54
 
 
55
 
UserAgentDlg::UserAgentDlg(QWidget *parent, const QVariantList &)
56
 
             :KCModule(KioConfigFactory::componentData(), parent),
57
 
              m_userAgentInfo(0),
58
 
              m_config(0)
59
 
{
60
 
  ui.setupUi(this);
61
 
  ui.newButton->setIcon(KIcon("list-add"));
62
 
  ui.changeButton->setIcon(KIcon("edit-rename"));
63
 
  ui.deleteButton->setIcon(KIcon("list-remove"));
64
 
  ui.deleteAllButton->setIcon(KIcon("edit-delete"));
65
 
}
66
 
 
67
 
UserAgentDlg::~UserAgentDlg()
68
 
{
69
 
    delete m_userAgentInfo;
70
 
    delete m_config;
71
 
}
72
 
 
73
 
void UserAgentDlg::on_sendUACheckBox_clicked()
74
 
{
75
 
  configChanged();
76
 
}
77
 
 
78
 
void UserAgentDlg::on_newButton_clicked()
79
 
{
80
 
  const QPointer<UserAgentSelectorDlg> pdlg( new UserAgentSelectorDlg( i18n("Add Identification"), m_userAgentInfo, this ) );
81
 
 
82
 
  if ( pdlg->exec() == QDialog::Accepted && pdlg )
83
 
  {
84
 
    if ( !handleDuplicate( pdlg->siteName(), pdlg->identity(), pdlg->alias() ) )
85
 
    {
86
 
      QTreeWidgetItem* item = new QTreeWidgetItem( ui.sitePolicyTreeWidget);
87
 
      item->setText(0, pdlg->siteName());
88
 
      item->setText(1, pdlg->identity());
89
 
      item->setText(2, pdlg->alias());
90
 
      ui.sitePolicyTreeWidget->setCurrentItem( item );
91
 
      configChanged();
92
 
    }
93
 
  }
94
 
  delete pdlg;
95
 
}
96
 
 
97
 
void UserAgentDlg::on_changeButton_clicked()
98
 
{
99
 
  on_sitePolicyTreeWidget_itemDoubleClicked(ui.sitePolicyTreeWidget->currentItem(), -1);
100
 
}
101
 
 
102
 
void UserAgentDlg::on_deleteButton_clicked()
103
 
{
104
 
  SiteList selectedItems = ui.sitePolicyTreeWidget->selectedItems();
105
 
  SiteListIterator endIt = selectedItems.end();
106
 
 
107
 
  QString siteName;
108
 
  for(SiteListIterator it = selectedItems.begin(); it != endIt; ++it)
109
 
    delete (*it);
110
 
 
111
 
  updateButtons();
112
 
  configChanged();
113
 
}
114
 
 
115
 
void UserAgentDlg::on_deleteAllButton_clicked()
116
 
{
117
 
  ui.sitePolicyTreeWidget->clear();
118
 
  updateButtons();
119
 
  configChanged();
120
 
}
121
 
 
122
 
void UserAgentDlg::on_osNameCheckBox_clicked()
123
 
{
124
 
  changeDefaultUAModifiers();
125
 
}
126
 
 
127
 
void UserAgentDlg::on_osVersionCheckBox_clicked()
128
 
{
129
 
  changeDefaultUAModifiers();
130
 
}
131
 
 
132
 
void UserAgentDlg::on_platformCheckBox_clicked()
133
 
{
134
 
  changeDefaultUAModifiers();
135
 
}
136
 
 
137
 
void UserAgentDlg::on_processorTypeCheckBox_clicked()
138
 
{
139
 
  changeDefaultUAModifiers();
140
 
}
141
 
 
142
 
void UserAgentDlg::on_languageCheckBox_clicked()
143
 
{
144
 
  changeDefaultUAModifiers();
145
 
}
146
 
 
147
 
void UserAgentDlg::on_sitePolicyTreeWidget_itemDoubleClicked(QTreeWidgetItem* item, int)
148
 
{
149
 
  if(item)
150
 
  {
151
 
    // Store the current site name...
152
 
    const QString currentSiteName = item->text(0);
153
 
 
154
 
    UserAgentSelectorDlg pdlg ( i18n("Modify Identification"), m_userAgentInfo, this );
155
 
    pdlg.setSiteName( currentSiteName );
156
 
    pdlg.setIdentity( item->text(1) );
157
 
 
158
 
    if ( pdlg.exec() == QDialog::Accepted )
159
 
    {
160
 
      if ( pdlg.siteName() == currentSiteName ||
161
 
          !handleDuplicate( pdlg.siteName(), pdlg.identity(), pdlg.alias() ) )
162
 
      {
163
 
        item->setText( 0, pdlg.siteName() );
164
 
        item->setText( 1, pdlg.identity() );
165
 
        item->setText( 2, pdlg.alias() );
166
 
        configChanged();
167
 
      }
168
 
    }
169
 
  }
170
 
}
171
 
 
172
 
void UserAgentDlg::changeDefaultUAModifiers()
173
 
{
174
 
  m_ua_keys = ':'; // Make sure it's not empty
175
 
 
176
 
  if ( ui.osNameCheckBox->isChecked() )
177
 
     m_ua_keys += 'o';
178
 
 
179
 
  if ( ui.osVersionCheckBox->isChecked() )
180
 
     m_ua_keys += 'v';
181
 
 
182
 
  if ( ui.platformCheckBox->isChecked() )
183
 
     m_ua_keys += 'p';
184
 
 
185
 
  if ( ui.processorTypeCheckBox->isChecked() )
186
 
     m_ua_keys += 'm';
187
 
 
188
 
  if ( ui.languageCheckBox->isChecked() )
189
 
     m_ua_keys += 'l';
190
 
 
191
 
  ui.osVersionCheckBox->setEnabled(m_ua_keys.contains('o'));
192
 
 
193
 
  QString modVal = KProtocolManager::defaultUserAgent( m_ua_keys );
194
 
  if ( ui.defaultIdLineEdit->text() != modVal )
195
 
  {
196
 
    ui.defaultIdLineEdit->setText(modVal);
197
 
    configChanged();
198
 
  }
199
 
}
200
 
 
201
 
bool UserAgentDlg::handleDuplicate( const QString& site,
202
 
                                    const QString& identity,
203
 
                                    const QString& alias )
204
 
{
205
 
  SiteList list = ui.sitePolicyTreeWidget->findItems(site, Qt::MatchExactly, 0);
206
 
 
207
 
  if (!list.isEmpty())
208
 
  {
209
 
    QString msg = i18n("<qt><center>Found an existing identification for"
210
 
                        "<br/><b>%1</b><br/>"
211
 
                        "Do you want to replace it?</center>"
212
 
                        "</qt>", site);
213
 
    int res = KMessageBox::warningContinueCancel(this, msg,
214
 
                                        i18n("Duplicate Identification"),
215
 
                                        KGuiItem(i18n("Replace")));
216
 
    if ( res == KMessageBox::Continue )
217
 
    {
218
 
      list[0]->setText(0, site);
219
 
      list[0]->setText(1, identity);
220
 
      list[0]->setText(2, alias);
221
 
      configChanged();
222
 
    }
223
 
 
224
 
    return true;
225
 
  }
226
 
 
227
 
  return false;
228
 
}
229
 
 
230
 
void UserAgentDlg::configChanged(bool enable)
231
 
{
232
 
  emit changed(enable);
233
 
}
234
 
 
235
 
void UserAgentDlg::updateButtons()
236
 
{
237
 
  const int selectedItemCount = ui.sitePolicyTreeWidget->selectedItems().count();
238
 
  const bool hasItems = ui.sitePolicyTreeWidget->topLevelItemCount() > 0;
239
 
 
240
 
  ui.changeButton->setEnabled ((hasItems && selectedItemCount == 1));
241
 
  ui.deleteButton->setEnabled ((hasItems && selectedItemCount > 0));
242
 
  ui.deleteAllButton->setEnabled ( hasItems );
243
 
}
244
 
 
245
 
void UserAgentDlg::on_sitePolicyTreeWidget_itemSelectionChanged()
246
 
{
247
 
  updateButtons();
248
 
}
249
 
 
250
 
void UserAgentDlg::load()
251
 
{
252
 
  ui.sitePolicyTreeWidget->clear();
253
 
 
254
 
  if (!m_config)
255
 
    m_config = new KConfig("kio_httprc", KConfig::NoGlobals);
256
 
  else
257
 
    m_config->reparseConfiguration();
258
 
 
259
 
  if (!m_userAgentInfo)
260
 
    m_userAgentInfo = new UserAgentInfo();
261
 
 
262
 
  const QStringList list = m_config->groupList();
263
 
  QStringList::ConstIterator endIt = list.end();
264
 
  QString agentStr;
265
 
 
266
 
  for ( QStringList::ConstIterator it = list.begin(); it != endIt; ++it )
267
 
  {
268
 
    if ( (*it) == "<default>")
269
 
        continue;
270
 
 
271
 
    KConfigGroup cg(m_config, *it);
272
 
    agentStr = cg.readEntry("UserAgent");
273
 
    if (!agentStr.isEmpty())
274
 
    {
275
 
      QTreeWidgetItem* item = new QTreeWidgetItem(ui.sitePolicyTreeWidget);
276
 
      item->setText(0, (*it).toLower());
277
 
      item->setText(1, m_userAgentInfo->aliasStr(agentStr));
278
 
      item->setText(2, agentStr);
279
 
    }
280
 
  }
281
 
 
282
 
  // Update buttons and checkboxes...
283
 
  KConfigGroup cg2(m_config, QString());
284
 
  bool b = cg2.readEntry("SendUserAgent", true);
285
 
  ui.sendUACheckBox->setChecked( b );
286
 
  m_ua_keys = cg2.readEntry("UserAgentKeys", DEFAULT_USER_AGENT_KEYS).toLower();
287
 
  ui.defaultIdLineEdit->setText( KProtocolManager::defaultUserAgent( m_ua_keys ) );
288
 
  ui.osNameCheckBox->setChecked( m_ua_keys.contains('o') );
289
 
  ui.osVersionCheckBox->setChecked( m_ua_keys.contains('v') );
290
 
  ui.platformCheckBox->setChecked( m_ua_keys.contains('p') );
291
 
  ui.processorTypeCheckBox->setChecked( m_ua_keys.contains('m') );
292
 
  ui.languageCheckBox->setChecked( m_ua_keys.contains('l') );
293
 
 
294
 
  updateButtons();
295
 
  configChanged(false);
296
 
}
297
 
 
298
 
void UserAgentDlg::defaults()
299
 
{
300
 
  ui.sitePolicyTreeWidget->clear();
301
 
  m_ua_keys = DEFAULT_USER_AGENT_KEYS;
302
 
  ui.defaultIdLineEdit->setText( KProtocolManager::defaultUserAgent(m_ua_keys) );
303
 
  ui.osNameCheckBox->setChecked( m_ua_keys.contains('o') );
304
 
  ui.osVersionCheckBox->setChecked( m_ua_keys.contains('v') );
305
 
  ui.platformCheckBox->setChecked( m_ua_keys.contains('p') );
306
 
  ui.processorTypeCheckBox->setChecked( m_ua_keys.contains('m') );
307
 
  ui.languageCheckBox->setChecked( m_ua_keys.contains('l') );
308
 
  ui.sendUACheckBox->setChecked( true );
309
 
 
310
 
  updateButtons();
311
 
  configChanged();
312
 
}
313
 
 
314
 
void UserAgentDlg::save()
315
 
{
316
 
  Q_ASSERT(m_config);
317
 
 
318
 
  // Put all the groups except the default into the delete list.
319
 
  QStringList deleteList = m_config->groupList();
320
 
 
321
 
  //Remove all the groups that DO NOT contain a "UserAgent" entry...
322
 
  QStringList::ConstIterator endIt = deleteList.constEnd();
323
 
  for (QStringList::ConstIterator it = deleteList.constBegin(); it != endIt; ++it)
324
 
  {
325
 
    if ( (*it) == QLatin1String("<default>") )
326
 
      continue;
327
 
 
328
 
    KConfigGroup cg(m_config, *it);
329
 
    if (!cg.hasKey("UserAgent"))
330
 
      deleteList.removeAll(*it);
331
 
  }
332
 
 
333
 
  QString domain;
334
 
  QTreeWidgetItem* item;
335
 
  int itemCount = ui.sitePolicyTreeWidget->topLevelItemCount();
336
 
 
337
 
  // Save and remove from the delete list all the groups that were 
338
 
  // not deleted by the end user.
339
 
  for(int i = 0; i < itemCount; i++)
340
 
  {
341
 
    item = ui.sitePolicyTreeWidget->topLevelItem(i);
342
 
    domain = item->text(0);
343
 
    KConfigGroup cg(m_config, domain);
344
 
    cg.writeEntry("UserAgent", item->text(2));
345
 
    deleteList.removeAll(domain);
346
 
    qDebug("UserAgentDlg::save: Removed [%s] from delete list", domain.toLatin1().constData());
347
 
  }
348
 
 
349
 
  // Write the global configuration information...
350
 
  KConfigGroup cg(m_config, QString());
351
 
  cg.writeEntry("SendUserAgent", ui.sendUACheckBox->isChecked());
352
 
  cg.writeEntry("UserAgentKeys", m_ua_keys );
353
 
 
354
 
  // Sync up all the changes so far...
355
 
  m_config->sync();
356
 
 
357
 
  // If delete list is not empty, delete the specified domains.
358
 
  if (!deleteList.isEmpty())
359
 
  {
360
 
    // Remove entries from local file.
361
 
    endIt = deleteList.constEnd();
362
 
    KConfig cfg ("kio_httprc", KConfig::SimpleConfig);
363
 
 
364
 
    for ( QStringList::ConstIterator it = deleteList.constBegin(); it != endIt; ++it )
365
 
    {
366
 
      KConfigGroup cg(&cfg, *it);
367
 
      cg.deleteEntry("UserAgent");
368
 
      qDebug("UserAgentDlg::save: Deleting UserAgent of group [%s]", (*it).toLatin1().constData());
369
 
      if (cg.keyList().count() < 1)
370
 
        cg.deleteGroup();
371
 
    }
372
 
 
373
 
    // Sync up the configuration...
374
 
    cfg.sync();
375
 
 
376
 
    // Check everything is gone, reset to blank otherwise.
377
 
    m_config->reparseConfiguration();
378
 
    endIt = deleteList.constEnd();
379
 
    for (QStringList::ConstIterator it = deleteList.constBegin(); it != endIt; ++it )
380
 
    {
381
 
      KConfigGroup cg(m_config, *it);
382
 
      if (cg.hasKey("UserAgent"))
383
 
        cg.writeEntry("UserAgent", QString());
384
 
    }
385
 
 
386
 
    // Sync up the configuration...
387
 
    m_config->sync();
388
 
  }
389
 
 
390
 
  KSaveIOConfig::updateRunningIOSlaves (this);
391
 
  configChanged( false );
392
 
}
393
 
 
394
 
QString UserAgentDlg::quickHelp() const
395
 
{
396
 
  return i18n( "<p><h1>Browser Identification</h1> "
397
 
               "The browser-identification module allows you to have full "
398
 
               "control over how Konqueror will identify itself to web "
399
 
               "sites you browse.</p>"
400
 
               "<p>This ability to fake identification is necessary because "
401
 
               "some web sites do not display properly when they detect that "
402
 
               "they are not talking to current versions of either Netscape "
403
 
               "Navigator or Internet Explorer, even if the browser actually "
404
 
               "supports all the necessary features to render those pages "
405
 
               "properly. "
406
 
               "For such sites, you can use this feature to try to browse "
407
 
               "them. Please understand that this might not always work, since "
408
 
               "such sites might be using non-standard web protocols and or "
409
 
               "specifications.</p>"
410
 
               "<p><u>NOTE:</u> To obtain specific help on a particular section "
411
 
               "of the dialog box, simply click on the quick help button on "
412
 
               "the window title bar, then click on the section "
413
 
               "for which you are seeking help.</p>" );
414
 
}
415
 
 
416
 
#include "useragentdlg.moc"