]>
cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinremoteencoding.cpp
397eaccc098babbe5819ca3b81c78ce345b93af0
1 /***************************************************************************
2 * Copyright (C) 2009 by Rahman Duran <rahman.duran@gmail.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
21 * This code is largely based on the kremoteencodingplugin
22 * Copyright (c) 2003 Thiago Macieira <thiago.macieira@kdemail.net>
23 * Distributed under the same terms.
26 #include "dolphinremoteencoding.h"
27 #include "dolphinviewactionhandler.h"
30 #include <kactionmenu.h>
31 #include <kactioncollection.h>
35 #include <kmimetype.h>
37 #include <kcharsets.h>
39 #include <kprotocolinfo.h>
40 #include <kprotocolmanager.h>
41 #include <kio/slaveconfig.h>
42 #include <kio/scheduler.h>
43 #include <kconfiggroup.h>
45 #define DATA_KEY QLatin1String("Charset")
47 DolphinRemoteEncoding::DolphinRemoteEncoding(QObject
* parent
, DolphinViewActionHandler
* actionHandler
)
49 m_actionHandler(actionHandler
),
53 m_menu
= new KActionMenu(KIcon("character-set"), i18n("Select Remote Charset"), this);
54 m_actionHandler
->actionCollection()->addAction("change_remote_encoding", m_menu
);
55 connect(m_menu
->menu(), SIGNAL(aboutToShow()),
56 this, SLOT(slotAboutToShow()));
58 m_menu
->setEnabled(false);
59 m_menu
->setDelayed(false);
62 DolphinRemoteEncoding::~DolphinRemoteEncoding()
66 void DolphinRemoteEncoding::slotReload()
71 void DolphinRemoteEncoding::loadSettings()
74 m_encodingDescriptions
= KGlobal::charsets()->descriptiveEncodingNames();
79 void DolphinRemoteEncoding::slotAboutToOpenUrl()
81 KUrl oldURL
= m_currentURL
;
82 m_currentURL
= m_actionHandler
->currentView()->url();
84 if (m_currentURL
.protocol() != oldURL
.protocol()) {
85 // This plugin works on ftp, fish, etc.
86 // everything whose type is T_FILESYSTEM except for local files
87 if (!m_currentURL
.isLocalFile() &&
88 KProtocolManager::outputType(m_currentURL
) == KProtocolInfo::T_FILESYSTEM
) {
90 m_menu
->setEnabled(true);
93 m_menu
->setEnabled(false);
98 if (m_currentURL
.host() != oldURL
.host()) {
103 void DolphinRemoteEncoding::fillMenu()
105 KMenu
* menu
= m_menu
->menu();
109 for (int i
= 0; i
< m_encodingDescriptions
.size();i
++) {
110 QAction
* action
= new QAction(m_encodingDescriptions
.at(i
), this);
111 action
->setCheckable(true);
113 menu
->addAction(action
);
115 menu
->addSeparator();
117 menu
->addAction(i18n("Reload"), this, SLOT(slotReload()), 0);
118 menu
->addAction(i18n("Default"), this, SLOT(slotDefault()), 0)->setCheckable(true);
119 m_idDefault
= m_encodingDescriptions
.size() + 2;
121 connect(menu
, SIGNAL(triggered(QAction
*)), this, SLOT(slotItemSelected(QAction
*)));
124 void DolphinRemoteEncoding::updateMenu()
130 // uncheck everything
131 for (int i
= 0; i
< m_menu
->menu()->actions().count(); i
++) {
132 m_menu
->menu()->actions().at(i
)->setChecked(false);
135 QString charset
= KIO::SlaveConfig::self()->configData(m_currentURL
.protocol(),
136 m_currentURL
.host(), DATA_KEY
);
138 if (!charset
.isEmpty()) {
140 bool isFound
= false;
141 for (int i
= 0; i
< m_encodingDescriptions
.size(); i
++) {
142 if (m_encodingDescriptions
.at(i
).contains(charset
)) {
149 kDebug() << "URL=" << m_currentURL
<< " charset=" << charset
;
152 kWarning() << "could not find entry for charset=" << charset
;
154 m_menu
->menu()->actions().at(id
)->setChecked(true);
157 m_menu
->menu()->actions().at(m_idDefault
)->setChecked(true);
162 void DolphinRemoteEncoding::slotAboutToShow()
170 void DolphinRemoteEncoding::slotItemSelected(QAction
* action
)
173 int id
= action
->data().toInt();
175 KConfig
config(("kio_" + m_currentURL
.protocol() + "rc").toLatin1());
176 QString host
= m_currentURL
.host();
177 if (m_menu
->menu()->actions().at(id
)->isChecked()) {
178 QString charset
= KGlobal::charsets()->encodingForName(m_encodingDescriptions
.at(id
));
179 KConfigGroup
cg(&config
, host
);
180 cg
.writeEntry(DATA_KEY
, charset
);
183 // Update the io-slaves...
189 void DolphinRemoteEncoding::slotDefault()
191 // We have no choice but delete all higher domain level
192 // settings here since it affects what will be matched.
193 KConfig
config(("kio_" + m_currentURL
.protocol() + "rc").toLatin1());
195 QStringList partList
= m_currentURL
.host().split('.', QString::SkipEmptyParts
);
196 if (!partList
.isEmpty()) {
197 partList
.erase(partList
.begin());
200 // Remove the exact name match...
201 domains
<< m_currentURL
.host();
203 while (partList
.count()) {
204 if (partList
.count() == 2) {
205 if (partList
[0].length() <= 2 && partList
[1].length() == 2) {
210 if (partList
.count() == 1) {
214 domains
<< partList
.join(".");
215 partList
.erase(partList
.begin());
218 for (QStringList::const_iterator it
= domains
.constBegin(); it
!= domains
.constEnd();++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
229 // Update the io-slaves.
233 void DolphinRemoteEncoding::updateView()
235 KIO::Scheduler::emitReparseSlaveConfiguration();
236 // Reload the page with the new charset
237 m_actionHandler
->currentView()->setUrl(m_currentURL
);
238 m_actionHandler
->currentView()->reload();
241 #include "dolphinremoteencoding.moc"