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
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"
30 #include <kactionmenu.h>
31 #include <kactioncollection.h>
35 #include <kmimetype.h>
37 #include <kcharsets.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
,
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
);
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()
74 KRemoteEncodingPlugin::slotReload()
80 KRemoteEncodingPlugin::loadSettings()
84 m_encodingDescriptions
= KGlobal::charsets()->descriptiveEncodingNames();
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);
106 m_menu
->setEnabled(false);
111 if (m_currentURL
.host() != oldURL
.host())
116 KRemoteEncodingPlugin::fillMenu()
118 KMenu
*menu
= m_menu
->menu();
121 QStringList::ConstIterator it
;
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
);
133 KRemoteEncodingPlugin::updateMenu()
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(),
144 if (!charset
.isEmpty())
147 QStringList::const_iterator it
;
148 for (it
= m_encodingDescriptions
.constBegin(); it
!= m_encodingDescriptions
.constEnd(); ++it
, ++id
)
149 if ((*it
).indexOf(charset
) != -1)
152 kDebug() << "URL=" << m_currentURL
<< " charset=" << charset
;
154 if (it
== m_encodingDescriptions
.constEnd())
155 kWarning() << "could not find entry for charset=" << charset
;
157 m_menu
->menu()->setItemChecked(id
, true);
160 m_menu
->menu()->setItemChecked(m_idDefault
, true);
164 KRemoteEncodingPlugin::slotAboutToShow()
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
);
182 // Update the io-slaves...
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());
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)
209 if (partList
.count() == 1)
212 domains
<< partList
.join(".");
213 partList
.erase(partList
.begin());
216 for (QStringList::const_iterator it
= domains
.constBegin(); it
!= domains
.constEnd();
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
228 // Update the io-slaves.
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"