]>
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"
27 #include "dolphinviewactionhandler.h"
29 #include "dolphindebug.h"
30 #include <KActionMenu>
31 #include <KActionCollection>
33 #include <KLocalizedString>
37 #include <KProtocolInfo>
38 #include <KProtocolManager>
39 #include <KIO/Scheduler>
40 #include <KConfigGroup>
42 #define DATA_KEY QLatin1String("Charset")
44 DolphinRemoteEncoding::DolphinRemoteEncoding(QObject
* parent
, DolphinViewActionHandler
* actionHandler
)
46 m_actionHandler(actionHandler
),
50 m_menu
= new KActionMenu(QIcon::fromTheme("character-set"), i18n("Select Remote Charset"), this);
51 m_actionHandler
->actionCollection()->addAction("change_remote_encoding", m_menu
);
52 connect(m_menu
->menu(), &QMenu::aboutToShow
,
53 this, &DolphinRemoteEncoding::slotAboutToShow
);
55 m_menu
->setEnabled(false);
56 m_menu
->setDelayed(false);
59 DolphinRemoteEncoding::~DolphinRemoteEncoding()
63 void DolphinRemoteEncoding::slotReload()
68 void DolphinRemoteEncoding::loadSettings()
71 m_encodingDescriptions
= KCharsets::charsets()->descriptiveEncodingNames();
76 void DolphinRemoteEncoding::slotAboutToOpenUrl()
78 QUrl oldURL
= m_currentURL
;
79 m_currentURL
= m_actionHandler
->currentView()->url();
81 if (m_currentURL
.scheme() != oldURL
.scheme()) {
82 // This plugin works on ftp, fish, etc.
83 // everything whose type is T_FILESYSTEM except for local files
84 if (!m_currentURL
.isLocalFile() &&
85 KProtocolManager::outputType(m_currentURL
) == KProtocolInfo::T_FILESYSTEM
) {
87 m_menu
->setEnabled(true);
90 m_menu
->setEnabled(false);
95 if (m_currentURL
.host() != oldURL
.host()) {
100 void DolphinRemoteEncoding::fillMenu()
102 QMenu
* menu
= m_menu
->menu();
106 for (int i
= 0; i
< m_encodingDescriptions
.size();i
++) {
107 QAction
* action
= new QAction(m_encodingDescriptions
.at(i
), this);
108 action
->setCheckable(true);
110 menu
->addAction(action
);
112 menu
->addSeparator();
114 menu
->addAction(i18n("Reload"), this, SLOT(slotReload()), 0);
115 menu
->addAction(i18n("Default"), this, SLOT(slotDefault()), 0)->setCheckable(true);
116 m_idDefault
= m_encodingDescriptions
.size() + 2;
118 connect(menu
, &QMenu::triggered
, this, &DolphinRemoteEncoding::slotItemSelected
);
121 void DolphinRemoteEncoding::updateMenu()
127 // uncheck everything
128 for (int i
= 0; i
< m_menu
->menu()->actions().count(); i
++) {
129 m_menu
->menu()->actions().at(i
)->setChecked(false);
132 const QString charset
= KCharsets::charsets()->descriptionForEncoding(KProtocolManager::charsetFor(m_currentURL
));
133 if (!charset
.isEmpty()) {
135 bool isFound
= false;
136 for (int i
= 0; i
< m_encodingDescriptions
.size(); i
++) {
137 if (m_encodingDescriptions
.at(i
) == charset
) {
144 qCDebug(DolphinDebug
) << "URL=" << m_currentURL
<< " charset=" << charset
;
147 qCWarning(DolphinDebug
) << "could not find entry for charset=" << charset
;
149 m_menu
->menu()->actions().at(id
)->setChecked(true);
152 m_menu
->menu()->actions().at(m_idDefault
)->setChecked(true);
157 void DolphinRemoteEncoding::slotAboutToShow()
165 void DolphinRemoteEncoding::slotItemSelected(QAction
* action
)
168 int id
= action
->data().toInt();
170 KConfig
config(("kio_" + m_currentURL
.scheme() + "rc").toLatin1());
171 QString host
= m_currentURL
.host();
172 if (m_menu
->menu()->actions().at(id
)->isChecked()) {
173 QString charset
= KCharsets::charsets()->encodingForName(m_encodingDescriptions
.at(id
));
174 KConfigGroup
cg(&config
, host
);
175 cg
.writeEntry(DATA_KEY
, charset
);
178 // Update the io-slaves...
184 void DolphinRemoteEncoding::slotDefault()
186 // We have no choice but delete all higher domain level
187 // settings here since it affects what will be matched.
188 KConfig
config(("kio_" + m_currentURL
.scheme() + "rc").toLatin1());
190 QStringList partList
= m_currentURL
.host().split('.', QString::SkipEmptyParts
);
191 if (!partList
.isEmpty()) {
192 partList
.erase(partList
.begin());
195 // Remove the exact name match...
196 domains
<< m_currentURL
.host();
198 while (!partList
.isEmpty()) {
199 if (partList
.count() == 2) {
200 if (partList
[0].length() <= 2 && partList
[1].length() == 2) {
205 if (partList
.count() == 1) {
209 domains
<< partList
.join(".");
210 partList
.erase(partList
.begin());
213 for (QStringList::const_iterator it
= domains
.constBegin(); it
!= domains
.constEnd();++it
) {
214 qCDebug(DolphinDebug
) << "Domain to remove: " << *it
;
215 if (config
.hasGroup(*it
)) {
216 config
.deleteGroup(*it
);
217 } else if (config
.group("").hasKey(*it
)) {
218 config
.group("").deleteEntry(*it
); //don't know what group name is supposed to be XXX
224 // Update the io-slaves.
228 void DolphinRemoteEncoding::updateView()
230 KIO::Scheduler::emitReparseSlaveConfiguration();
231 // Reload the page with the new charset
232 m_actionHandler
->currentView()->setUrl(m_currentURL
);
233 m_actionHandler
->currentView()->reload();