Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / konqueror / remoteencodingplugin / kremoteencodingplugin.cpp
blobdc804a05aba501e01b29333a0b8a3b85dd65565b
1 /*
2 Copyright (c) 2003 Thiago Macieira <thiago.macieira@kdemail.net>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License (LGPL) as published by the Free Software Foundation;
7 either version 2 of the License, or (at your option) any later
8 version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * This code is largely based on the UserAgent changer plugin (uachanger)
23 * Copyright © 2001 Dawit Alemayehu <adawit@kde.org>
24 * Distributed under the same terms.
27 #include "kremoteencodingplugin.h"
29 #include <kdebug.h>
30 #include <kactionmenu.h>
31 #include <kactioncollection.h>
32 #include <kicon.h>
33 #include <klocale.h>
34 #include <kglobal.h>
35 #include <kmimetype.h>
36 #include <kconfig.h>
37 #include <kcharsets.h>
38 #include <kmenu.h>
39 #include <kgenericfactory.h>
40 #include <kprotocolinfo.h>
41 #include <kprotocolmanager.h>
42 #include <kio/slaveconfig.h>
43 #include <kio/scheduler.h>
44 #include <kparts/browserextension.h>
45 #include <kconfiggroup.h>
47 #define DATA_KEY QLatin1String("Charset")
49 KRemoteEncodingPlugin::KRemoteEncodingPlugin(QObject * parent,
50 const QStringList &)
51 : KParts::Plugin(parent), m_loaded(false), m_idDefault(0)
53 m_menu = new KActionMenu(KIcon("character-set"), i18n("Select Remote Charset"), this);
54 actionCollection()->addAction("changeremoteencoding", m_menu);
55 connect(m_menu->menu(), SIGNAL(aboutToShow()),
56 this, SLOT(slotAboutToShow()));
57 m_menu->setEnabled(false);
58 m_menu->setDelayed(false);
60 m_part = qobject_cast<KParts::ReadOnlyPart*>(parent);
61 if (m_part) {
62 // if parent is not a part, our menu will never show
63 connect(m_part, SIGNAL(aboutToOpenURL()),
64 this, SLOT(slotAboutToOpenURL()));
65 m_part->installEventFilter(this);
69 KRemoteEncodingPlugin::~KRemoteEncodingPlugin()
73 void
74 KRemoteEncodingPlugin::slotReload()
76 loadSettings();
79 void
80 KRemoteEncodingPlugin::loadSettings()
82 m_loaded = true;
84 m_encodingDescriptions = KGlobal::charsets()->descriptiveEncodingNames();
86 fillMenu();
89 void
90 KRemoteEncodingPlugin::slotAboutToOpenURL()
92 KUrl oldURL = m_currentURL;
93 m_currentURL = m_part->url();
95 if (m_currentURL.protocol() != oldURL.protocol())
97 // This plugin works on ftp, fish, etc.
98 // everything whose type is T_FILESYSTEM except for local files
99 if (!m_currentURL.isLocalFile() &&
100 KProtocolManager::outputType(m_currentURL) == KProtocolInfo::T_FILESYSTEM)
102 m_menu->setEnabled(true);
103 loadSettings();
105 else
106 m_menu->setEnabled(false);
108 return;
111 if (m_currentURL.host() != oldURL.host())
112 updateMenu();
115 void
116 KRemoteEncodingPlugin::fillMenu()
118 KMenu *menu = m_menu->menu();
119 menu->clear();
121 QStringList::ConstIterator it;
122 int count = 0;
123 for (it = m_encodingDescriptions.constBegin(); it != m_encodingDescriptions.constEnd(); ++it)
124 menu->insertItem(*it, this, SLOT(slotItemSelected(int)), 0, ++count);
125 menu->addSeparator();
127 menu->insertItem(i18n("Reload"), this, SLOT(slotReload()), 0, ++count);
128 menu->insertItem(i18n("Default"), this, SLOT(slotDefault()), 0, ++count);
129 m_idDefault = count;
132 void
133 KRemoteEncodingPlugin::updateMenu()
135 if (!m_loaded)
136 loadSettings();
138 // uncheck everything
139 for (unsigned i = 0; i < m_menu->menu()->actions().count(); i++)
140 m_menu->menu()->setItemChecked(m_menu->menu()->idAt(i), false);
142 QString charset = KIO::SlaveConfig::self()->configData(m_currentURL.protocol(), m_currentURL.host(),
143 DATA_KEY);
144 if (!charset.isEmpty())
146 int id = 1;
147 QStringList::const_iterator it;
148 for (it = m_encodingDescriptions.constBegin(); it != m_encodingDescriptions.constEnd(); ++it, ++id)
149 if ((*it).indexOf(charset) != -1)
150 break;
152 kDebug() << "URL=" << m_currentURL << " charset=" << charset;
154 if (it == m_encodingDescriptions.constEnd())
155 kWarning() << "could not find entry for charset=" << charset ;
156 else
157 m_menu->menu()->setItemChecked(id, true);
159 else
160 m_menu->menu()->setItemChecked(m_idDefault, true);
163 void
164 KRemoteEncodingPlugin::slotAboutToShow()
166 if (!m_loaded)
167 loadSettings();
168 updateMenu();
171 void
172 KRemoteEncodingPlugin::slotItemSelected(int id)
174 KConfig config(("kio_" + m_currentURL.protocol() + "rc").toLatin1());
175 QString host = m_currentURL.host();
176 if ( m_menu->menu()->isItemChecked(id) )
178 QString charset = KGlobal::charsets()->encodingForName(m_encodingDescriptions[id - 1]);
179 KConfigGroup cg(&config, host);
180 cg.writeEntry(DATA_KEY, charset);
181 config.sync();
182 // Update the io-slaves...
183 updateBrowser();
187 void
188 KRemoteEncodingPlugin::slotDefault()
190 // We have no choice but delete all higher domain level
191 // settings here since it affects what will be matched.
192 KConfig config(("kio_" + m_currentURL.protocol() + "rc").toLatin1());
194 QStringList partList = m_currentURL.host().split('.', QString::SkipEmptyParts);
195 if (!partList.isEmpty())
197 partList.erase(partList.begin());
199 QStringList domains;
200 // Remove the exact name match...
201 domains << m_currentURL.host();
203 while (partList.count())
205 if (partList.count() == 2)
206 if (partList[0].length() <= 2 && partList[1].length() == 2)
207 break;
209 if (partList.count() == 1)
210 break;
212 domains << partList.join(".");
213 partList.erase(partList.begin());
216 for (QStringList::const_iterator it = domains.constBegin(); it != domains.constEnd();
217 ++it)
219 kDebug() << "Domain to remove: " << *it;
220 if (config.hasGroup(*it))
221 config.deleteGroup(*it);
222 else if (config.group("").hasKey(*it))
223 config.group("").deleteEntry(*it); //don't know what group name is supposed to be XXX
226 config.sync();
228 // Update the io-slaves.
229 updateBrowser();
232 void
233 KRemoteEncodingPlugin::updateBrowser()
235 KIO::Scheduler::emitReparseSlaveConfiguration();
236 // Reload the page with the new charset
237 KParts::OpenUrlArguments args = m_part->arguments();
238 args.setReload( true );
239 m_part->setArguments( args );
240 m_part->openUrl(m_currentURL);
243 bool KRemoteEncodingPlugin::eventFilter(QObject*obj, QEvent *ev)
245 if (obj == m_part && KParts::OpenUrlEvent::test(ev)) {
246 const QString mimeType = m_part->arguments().mimeType();
247 if (!mimeType.isEmpty() && KMimeType::mimeType(mimeType)->is("inode/directory"))
248 slotAboutToOpenURL();
250 return KParts::Plugin::eventFilter(obj, ev);
253 typedef KGenericFactory < KRemoteEncodingPlugin > KRemoteEncodingPluginFactory;
254 K_EXPORT_COMPONENT_FACTORY(konq_remoteencoding,
255 KRemoteEncodingPluginFactory("kremoteencodingplugin"))
257 #include "kremoteencodingplugin.moc"