Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / konqueror / settings / kio / useragentdlg.cpp
blob6c49a841ecfaa80c9feb2d6b7b938e98462849be
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
7 Completely re-written by:
8 Copyright (C) 2000- Dawit Alemayehu <adawit@kde.org>
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.
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.
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.
25 // Own
26 #include "useragentdlg.h"
28 // Local
29 #include "ksaveioconfig.h"
30 #include "useragentinfo.h"
31 #include "useragentselectordlg.h"
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>
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>
50 K_PLUGIN_FACTORY_DECLARATION(KioConfigFactory)
52 typedef QList<QTreeWidgetItem*> SiteList;
53 typedef SiteList::iterator SiteListIterator;
55 UserAgentDlg::UserAgentDlg(QWidget *parent, const QVariantList &)
56 :KCModule(KioConfigFactory::componentData(), parent),
57 m_userAgentInfo(0),
58 m_config(0)
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"));
67 UserAgentDlg::~UserAgentDlg()
69 delete m_userAgentInfo;
70 delete m_config;
73 void UserAgentDlg::on_sendUACheckBox_clicked()
75 configChanged();
78 void UserAgentDlg::on_newButton_clicked()
80 const QPointer<UserAgentSelectorDlg> pdlg( new UserAgentSelectorDlg( i18n("Add Identification"), m_userAgentInfo, this ) );
82 if ( pdlg->exec() == QDialog::Accepted && pdlg )
84 if ( !handleDuplicate( pdlg->siteName(), pdlg->identity(), pdlg->alias() ) )
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();
94 delete pdlg;
97 void UserAgentDlg::on_changeButton_clicked()
99 on_sitePolicyTreeWidget_itemDoubleClicked(ui.sitePolicyTreeWidget->currentItem(), -1);
102 void UserAgentDlg::on_deleteButton_clicked()
104 SiteList selectedItems = ui.sitePolicyTreeWidget->selectedItems();
105 SiteListIterator endIt = selectedItems.end();
107 QString siteName;
108 for(SiteListIterator it = selectedItems.begin(); it != endIt; ++it)
109 delete (*it);
111 updateButtons();
112 configChanged();
115 void UserAgentDlg::on_deleteAllButton_clicked()
117 ui.sitePolicyTreeWidget->clear();
118 updateButtons();
119 configChanged();
122 void UserAgentDlg::on_osNameCheckBox_clicked()
124 changeDefaultUAModifiers();
127 void UserAgentDlg::on_osVersionCheckBox_clicked()
129 changeDefaultUAModifiers();
132 void UserAgentDlg::on_platformCheckBox_clicked()
134 changeDefaultUAModifiers();
137 void UserAgentDlg::on_processorTypeCheckBox_clicked()
139 changeDefaultUAModifiers();
142 void UserAgentDlg::on_languageCheckBox_clicked()
144 changeDefaultUAModifiers();
147 void UserAgentDlg::on_sitePolicyTreeWidget_itemDoubleClicked(QTreeWidgetItem* item, int)
149 if(item)
151 // Store the current site name...
152 const QString currentSiteName = item->text(0);
154 UserAgentSelectorDlg pdlg ( i18n("Modify Identification"), m_userAgentInfo, this );
155 pdlg.setSiteName( currentSiteName );
156 pdlg.setIdentity( item->text(1) );
158 if ( pdlg.exec() == QDialog::Accepted )
160 if ( pdlg.siteName() == currentSiteName ||
161 !handleDuplicate( pdlg.siteName(), pdlg.identity(), pdlg.alias() ) )
163 item->setText( 0, pdlg.siteName() );
164 item->setText( 1, pdlg.identity() );
165 item->setText( 2, pdlg.alias() );
166 configChanged();
172 void UserAgentDlg::changeDefaultUAModifiers()
174 m_ua_keys = ":"; // Make sure it's not empty
176 if ( ui.osNameCheckBox->isChecked() )
177 m_ua_keys += 'o';
179 if ( ui.osVersionCheckBox->isChecked() )
180 m_ua_keys += 'v';
182 if ( ui.platformCheckBox->isChecked() )
183 m_ua_keys += 'p';
185 if ( ui.processorTypeCheckBox->isChecked() )
186 m_ua_keys += 'm';
188 if ( ui.languageCheckBox->isChecked() )
189 m_ua_keys += 'l';
191 ui.osVersionCheckBox->setEnabled(m_ua_keys.contains('o'));
193 QString modVal = KProtocolManager::defaultUserAgent( m_ua_keys );
194 if ( ui.defaultIdLineEdit->text() != modVal )
196 ui.defaultIdLineEdit->setText(modVal);
197 configChanged();
201 bool UserAgentDlg::handleDuplicate( const QString& site,
202 const QString& identity,
203 const QString& alias )
205 SiteList list = ui.sitePolicyTreeWidget->findItems(site, Qt::MatchExactly, 0);
207 if (!list.isEmpty())
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 )
218 list[0]->setText(0, site);
219 list[0]->setText(1, identity);
220 list[0]->setText(2, alias);
221 configChanged();
224 return true;
227 return false;
230 void UserAgentDlg::configChanged(bool enable)
232 emit changed(enable);
235 void UserAgentDlg::updateButtons()
237 const int selectedItemCount = ui.sitePolicyTreeWidget->selectedItems().count();
238 const bool hasItems = ui.sitePolicyTreeWidget->topLevelItemCount() > 0;
240 ui.changeButton->setEnabled ((hasItems && selectedItemCount == 1));
241 ui.deleteButton->setEnabled ((hasItems && selectedItemCount > 0));
242 ui.deleteAllButton->setEnabled ( hasItems );
245 void UserAgentDlg::on_sitePolicyTreeWidget_itemSelectionChanged()
247 updateButtons();
250 void UserAgentDlg::load()
252 ui.sitePolicyTreeWidget->clear();
254 if (!m_config)
255 m_config = new KConfig("kio_httprc", KConfig::NoGlobals);
256 else
257 m_config->reparseConfiguration();
259 if (!m_userAgentInfo)
260 m_userAgentInfo = new UserAgentInfo();
262 const QStringList list = m_config->groupList();
263 QStringList::ConstIterator endIt = list.end();
264 QString agentStr;
266 for ( QStringList::ConstIterator it = list.begin(); it != endIt; ++it )
268 if ( (*it) == "<default>")
269 continue;
271 KConfigGroup cg(m_config, *it);
272 agentStr = cg.readEntry("UserAgent");
273 if (!agentStr.isEmpty())
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);
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') );
294 updateButtons();
295 configChanged(false);
298 void UserAgentDlg::defaults()
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 );
310 updateButtons();
311 configChanged();
314 void UserAgentDlg::save()
316 Q_ASSERT(m_config);
318 // Put all the groups except the default into the delete list.
319 QStringList deleteList = m_config->groupList();
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)
325 if ( (*it) == QLatin1String("<default>") )
326 continue;
328 KConfigGroup cg(m_config, *it);
329 if (!cg.hasKey("UserAgent"))
330 deleteList.removeAll(*it);
333 QString domain;
334 QTreeWidgetItem* item;
335 int itemCount = ui.sitePolicyTreeWidget->topLevelItemCount();
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++)
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());
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 );
354 // Sync up all the changes so far...
355 m_config->sync();
357 // If delete list is not empty, delete the specified domains.
358 if (!deleteList.isEmpty())
360 // Remove entries from local file.
361 endIt = deleteList.constEnd();
362 KConfig cfg ("kio_httprc", KConfig::SimpleConfig);
364 for ( QStringList::ConstIterator it = deleteList.constBegin(); it != endIt; ++it )
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();
373 // Sync up the configuration...
374 cfg.sync();
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 )
381 KConfigGroup cg(m_config, *it);
382 if (cg.hasKey("UserAgent"))
383 cg.writeEntry("UserAgent", QString());
386 // Sync up the configuration...
387 m_config->sync();
390 KSaveIOConfig::updateRunningIOSlaves (this);
391 configChanged( false );
394 QString UserAgentDlg::quickHelp() const
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>" );
416 #include "useragentdlg.moc"