]>
cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinremoteencoding.cpp
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"
28 #include "dolphinviewactionhandler.h"
29 #include "dolphindebug.h"
31 #include <KActionCollection>
32 #include <KActionMenu>
35 #include <KConfigGroup>
36 #include <KIO/Scheduler>
37 #include <KLocalizedString>
38 #include <KProtocolInfo>
39 #include <KProtocolManager>
43 #define DATA_KEY QStringLiteral("Charset")
45 DolphinRemoteEncoding::DolphinRemoteEncoding(QObject
* parent
, DolphinViewActionHandler
* actionHandler
)
47 m_actionHandler(actionHandler
),
51 m_menu
= new KActionMenu(QIcon::fromTheme(QStringLiteral("character-set")), i18n("Select Remote Charset"), this);
52 m_actionHandler
->actionCollection()->addAction(QStringLiteral("change_remote_encoding"), m_menu
);
53 connect(m_menu
->menu(), &QMenu::aboutToShow
,
54 this, &DolphinRemoteEncoding::slotAboutToShow
);
56 m_menu
->setEnabled(false);
57 m_menu
->setDelayed(false);
60 DolphinRemoteEncoding::~DolphinRemoteEncoding()
64 void DolphinRemoteEncoding::slotReload()
69 void DolphinRemoteEncoding::loadSettings()
72 m_encodingDescriptions
= KCharsets::charsets()->descriptiveEncodingNames();
77 void DolphinRemoteEncoding::slotAboutToOpenUrl()
79 QUrl oldURL
= m_currentURL
;
80 m_currentURL
= m_actionHandler
->currentView()->url();
82 if (m_currentURL
.scheme() != oldURL
.scheme()) {
83 // This plugin works on ftp, fish, etc.
84 // everything whose type is T_FILESYSTEM except for local files
85 if (!m_currentURL
.isLocalFile() &&
86 KProtocolManager::outputType(m_currentURL
) == KProtocolInfo::T_FILESYSTEM
) {
88 m_menu
->setEnabled(true);
91 m_menu
->setEnabled(false);
96 if (m_currentURL
.host() != oldURL
.host()) {
101 void DolphinRemoteEncoding::fillMenu()
103 QMenu
* menu
= m_menu
->menu();
107 for (int i
= 0; i
< m_encodingDescriptions
.size();i
++) {
108 QAction
* action
= new QAction(m_encodingDescriptions
.at(i
), this);
109 action
->setCheckable(true);
111 menu
->addAction(action
);
113 menu
->addSeparator();
115 menu
->addAction(i18n("Reload"), this, SLOT(slotReload()), 0);
116 menu
->addAction(i18n("Default"), this, SLOT(slotDefault()), 0)->setCheckable(true);
117 m_idDefault
= m_encodingDescriptions
.size() + 2;
119 connect(menu
, &QMenu::triggered
, this, &DolphinRemoteEncoding::slotItemSelected
);
122 void DolphinRemoteEncoding::updateMenu()
128 // uncheck everything
129 for (int i
= 0; i
< m_menu
->menu()->actions().count(); i
++) {
130 m_menu
->menu()->actions().at(i
)->setChecked(false);
133 const QString charset
= KCharsets::charsets()->descriptionForEncoding(KProtocolManager::charsetFor(m_currentURL
));
134 if (!charset
.isEmpty()) {
136 bool isFound
= false;
137 for (int i
= 0; i
< m_encodingDescriptions
.size(); i
++) {
138 if (m_encodingDescriptions
.at(i
) == charset
) {
145 qCDebug(DolphinDebug
) << "URL=" << m_currentURL
<< " charset=" << charset
;
148 qCWarning(DolphinDebug
) << "could not find entry for charset=" << charset
;
150 m_menu
->menu()->actions().at(id
)->setChecked(true);
153 m_menu
->menu()->actions().at(m_idDefault
)->setChecked(true);
158 void DolphinRemoteEncoding::slotAboutToShow()
166 void DolphinRemoteEncoding::slotItemSelected(QAction
* action
)
169 int id
= action
->data().toInt();
171 KConfig
config(("kio_" + m_currentURL
.scheme() + "rc").toLatin1());
172 QString host
= m_currentURL
.host();
173 if (m_menu
->menu()->actions().at(id
)->isChecked()) {
174 QString charset
= KCharsets::charsets()->encodingForName(m_encodingDescriptions
.at(id
));
175 KConfigGroup
cg(&config
, host
);
176 cg
.writeEntry(DATA_KEY
, charset
);
179 // Update the io-slaves...
185 void DolphinRemoteEncoding::slotDefault()
187 // We have no choice but delete all higher domain level
188 // settings here since it affects what will be matched.
189 KConfig
config(("kio_" + m_currentURL
.scheme() + "rc").toLatin1());
191 QStringList partList
= m_currentURL
.host().split('.', QString::SkipEmptyParts
);
192 if (!partList
.isEmpty()) {
193 partList
.erase(partList
.begin());
196 // Remove the exact name match...
197 domains
<< m_currentURL
.host();
199 while (!partList
.isEmpty()) {
200 if (partList
.count() == 2) {
201 if (partList
[0].length() <= 2 && partList
[1].length() == 2) {
206 if (partList
.count() == 1) {
210 domains
<< partList
.join(QLatin1Char('.'));
211 partList
.erase(partList
.begin());
214 for (QStringList::const_iterator it
= domains
.constBegin(); it
!= domains
.constEnd();++it
) {
215 qCDebug(DolphinDebug
) << "Domain to remove: " << *it
;
216 if (config
.hasGroup(*it
)) {
217 config
.deleteGroup(*it
);
218 } else if (config
.group("").hasKey(*it
)) {
219 config
.group("").deleteEntry(*it
); //don't know what group name is supposed to be XXX
225 // Update the io-slaves.
229 void DolphinRemoteEncoding::updateView()
231 KIO::Scheduler::emitReparseSlaveConfiguration();
232 // Reload the page with the new charset
233 m_actionHandler
->currentView()->setUrl(m_currentURL
);
234 m_actionHandler
->currentView()->reload();