]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinremoteencoding.cpp
Moving Remote encoding support to Dolphin
[dolphin.git] / src / dolphinremoteencoding.cpp
1 /*
2 Copyright (c) 2003 Thiago Macieira <thiago.macieira@kdemail.net>
3
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.
9
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.
14
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.
19 */
20
21 /*
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.
25 */
26
27 #include "kremoteencodingplugin.h"
28
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>
46
47 #define DATA_KEY QLatin1String("Charset")
48
49 KRemoteEncodingPlugin::KRemoteEncodingPlugin(QObject * parent,
50 const QStringList &)
51 : KParts::Plugin(parent), m_loaded(false), m_idDefault(0)
52 {
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);
59
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);
66 }
67 }
68
69 KRemoteEncodingPlugin::~KRemoteEncodingPlugin()
70 {
71 }
72
73 void
74 KRemoteEncodingPlugin::slotReload()
75 {
76 loadSettings();
77 }
78
79 void
80 KRemoteEncodingPlugin::loadSettings()
81 {
82 m_loaded = true;
83
84 m_encodingDescriptions = KGlobal::charsets()->descriptiveEncodingNames();
85
86 fillMenu();
87 }
88
89 void
90 KRemoteEncodingPlugin::slotAboutToOpenURL()
91 {
92 KUrl oldURL = m_currentURL;
93 m_currentURL = m_part->url();
94
95 if (m_currentURL.protocol() != oldURL.protocol())
96 {
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)
101 {
102 m_menu->setEnabled(true);
103 loadSettings();
104 }
105 else
106 m_menu->setEnabled(false);
107
108 return;
109 }
110
111 if (m_currentURL.host() != oldURL.host())
112 updateMenu();
113 }
114
115 void
116 KRemoteEncodingPlugin::fillMenu()
117 {
118 KMenu *menu = m_menu->menu();
119 menu->clear();
120
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();
126
127 menu->insertItem(i18n("Reload"), this, SLOT(slotReload()), 0, ++count);
128 menu->insertItem(i18n("Default"), this, SLOT(slotDefault()), 0, ++count);
129 m_idDefault = count;
130 }
131
132 void
133 KRemoteEncodingPlugin::updateMenu()
134 {
135 if (!m_loaded)
136 loadSettings();
137
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);
141
142 QString charset = KIO::SlaveConfig::self()->configData(m_currentURL.protocol(), m_currentURL.host(),
143 DATA_KEY);
144 if (!charset.isEmpty())
145 {
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;
151
152 kDebug() << "URL=" << m_currentURL << " charset=" << charset;
153
154 if (it == m_encodingDescriptions.constEnd())
155 kWarning() << "could not find entry for charset=" << charset ;
156 else
157 m_menu->menu()->setItemChecked(id, true);
158 }
159 else
160 m_menu->menu()->setItemChecked(m_idDefault, true);
161 }
162
163 void
164 KRemoteEncodingPlugin::slotAboutToShow()
165 {
166 if (!m_loaded)
167 loadSettings();
168 updateMenu();
169 }
170
171 void
172 KRemoteEncodingPlugin::slotItemSelected(int id)
173 {
174 KConfig config(("kio_" + m_currentURL.protocol() + "rc").toLatin1());
175 QString host = m_currentURL.host();
176 if ( m_menu->menu()->isItemChecked(id) )
177 {
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();
184 }
185 }
186
187 void
188 KRemoteEncodingPlugin::slotDefault()
189 {
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());
193
194 QStringList partList = m_currentURL.host().split('.', QString::SkipEmptyParts);
195 if (!partList.isEmpty())
196 {
197 partList.erase(partList.begin());
198
199 QStringList domains;
200 // Remove the exact name match...
201 domains << m_currentURL.host();
202
203 while (partList.count())
204 {
205 if (partList.count() == 2)
206 if (partList[0].length() <= 2 && partList[1].length() == 2)
207 break;
208
209 if (partList.count() == 1)
210 break;
211
212 domains << partList.join(".");
213 partList.erase(partList.begin());
214 }
215
216 for (QStringList::const_iterator it = domains.constBegin(); it != domains.constEnd();
217 ++it)
218 {
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
224 }
225 }
226 config.sync();
227
228 // Update the io-slaves.
229 updateBrowser();
230 }
231
232 void
233 KRemoteEncodingPlugin::updateBrowser()
234 {
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);
241 }
242
243 bool KRemoteEncodingPlugin::eventFilter(QObject*obj, QEvent *ev)
244 {
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();
249 }
250 return KParts::Plugin::eventFilter(obj, ev);
251 }
252
253 typedef KGenericFactory < KRemoteEncodingPlugin > KRemoteEncodingPluginFactory;
254 K_EXPORT_COMPONENT_FACTORY(konq_remoteencoding,
255 KRemoteEncodingPluginFactory("kremoteencodingplugin"))
256
257 #include "kremoteencodingplugin.moc"