]>
cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinremoteencoding.cpp
059b898e5297a1b7f2732904069ed0e472aa78d4
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>
31 #include <KActionCollection>
33 #include <KLocalizedString>
39 #include <KProtocolInfo>
40 #include <KProtocolManager>
41 #include <KIO/Scheduler>
42 #include <KConfigGroup>
44 #define DATA_KEY QLatin1String("Charset")
46 DolphinRemoteEncoding::DolphinRemoteEncoding(QObject
* parent
, DolphinViewActionHandler
* actionHandler
)
48 m_actionHandler(actionHandler
),
52 m_menu
= new KActionMenu(QIcon::fromTheme("character-set"), i18n("Select Remote Charset"), this);
53 m_actionHandler
->actionCollection()->addAction("change_remote_encoding", m_menu
);
54 connect(m_menu
->menu(), &QMenu::aboutToShow
,
55 this, &DolphinRemoteEncoding::slotAboutToShow
);
57 m_menu
->setEnabled(false);
58 m_menu
->setDelayed(false);
61 DolphinRemoteEncoding::~DolphinRemoteEncoding()
65 void DolphinRemoteEncoding::slotReload()
70 void DolphinRemoteEncoding::loadSettings()
73 m_encodingDescriptions
= KCharsets::charsets()->descriptiveEncodingNames();
78 void DolphinRemoteEncoding::slotAboutToOpenUrl()
80 KUrl oldURL
= m_currentURL
;
81 m_currentURL
= m_actionHandler
->currentView()->url();
83 if (m_currentURL
.protocol() != oldURL
.protocol()) {
84 // This plugin works on ftp, fish, etc.
85 // everything whose type is T_FILESYSTEM except for local files
86 if (!m_currentURL
.isLocalFile() &&
87 KProtocolManager::outputType(m_currentURL
) == KProtocolInfo::T_FILESYSTEM
) {
89 m_menu
->setEnabled(true);
92 m_menu
->setEnabled(false);
97 if (m_currentURL
.host() != oldURL
.host()) {
102 void DolphinRemoteEncoding::fillMenu()
104 QMenu
* menu
= m_menu
->menu();
108 for (int i
= 0; i
< m_encodingDescriptions
.size();i
++) {
109 QAction
* action
= new QAction(m_encodingDescriptions
.at(i
), this);
110 action
->setCheckable(true);
112 menu
->addAction(action
);
114 menu
->addSeparator();
116 menu
->addAction(i18n("Reload"), this, SLOT(slotReload()), 0);
117 menu
->addAction(i18n("Default"), this, SLOT(slotDefault()), 0)->setCheckable(true);
118 m_idDefault
= m_encodingDescriptions
.size() + 2;
120 connect(menu
, &QMenu::triggered
, this, &DolphinRemoteEncoding::slotItemSelected
);
123 void DolphinRemoteEncoding::updateMenu()
129 // uncheck everything
130 for (int i
= 0; i
< m_menu
->menu()->actions().count(); i
++) {
131 m_menu
->menu()->actions().at(i
)->setChecked(false);
134 const QString charset
= KCharsets::charsets()->descriptionForEncoding(KProtocolManager::charsetFor(m_currentURL
));
135 if (!charset
.isEmpty()) {
137 bool isFound
= false;
138 for (int i
= 0; i
< m_encodingDescriptions
.size(); i
++) {
139 if (m_encodingDescriptions
.at(i
) == charset
) {
146 kDebug() << "URL=" << m_currentURL
<< " charset=" << charset
;
149 kWarning() << "could not find entry for charset=" << charset
;
151 m_menu
->menu()->actions().at(id
)->setChecked(true);
154 m_menu
->menu()->actions().at(m_idDefault
)->setChecked(true);
159 void DolphinRemoteEncoding::slotAboutToShow()
167 void DolphinRemoteEncoding::slotItemSelected(QAction
* action
)
170 int id
= action
->data().toInt();
172 KConfig
config(("kio_" + m_currentURL
.protocol() + "rc").toLatin1());
173 QString host
= m_currentURL
.host();
174 if (m_menu
->menu()->actions().at(id
)->isChecked()) {
175 QString charset
= KCharsets::charsets()->encodingForName(m_encodingDescriptions
.at(id
));
176 KConfigGroup
cg(&config
, host
);
177 cg
.writeEntry(DATA_KEY
, charset
);
180 // Update the io-slaves...
186 void DolphinRemoteEncoding::slotDefault()
188 // We have no choice but delete all higher domain level
189 // settings here since it affects what will be matched.
190 KConfig
config(("kio_" + m_currentURL
.protocol() + "rc").toLatin1());
192 QStringList partList
= m_currentURL
.host().split('.', QString::SkipEmptyParts
);
193 if (!partList
.isEmpty()) {
194 partList
.erase(partList
.begin());
197 // Remove the exact name match...
198 domains
<< m_currentURL
.host();
200 while (!partList
.isEmpty()) {
201 if (partList
.count() == 2) {
202 if (partList
[0].length() <= 2 && partList
[1].length() == 2) {
207 if (partList
.count() == 1) {
211 domains
<< partList
.join(".");
212 partList
.erase(partList
.begin());
215 for (QStringList::const_iterator it
= domains
.constBegin(); it
!= domains
.constEnd();++it
) {
216 kDebug() << "Domain to remove: " << *it
;
217 if (config
.hasGroup(*it
)) {
218 config
.deleteGroup(*it
);
219 } else if (config
.group("").hasKey(*it
)) {
220 config
.group("").deleteEntry(*it
); //don't know what group name is supposed to be XXX
226 // Update the io-slaves.
230 void DolphinRemoteEncoding::updateView()
232 KIO::Scheduler::emitReparseSlaveConfiguration();
233 // Reload the page with the new charset
234 m_actionHandler
->currentView()->setUrl(m_currentURL
);
235 m_actionHandler
->currentView()->reload();