]>
cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinremoteencoding.cpp
2 * SPDX-FileCopyrightText: 2009 Rahman Duran <rahman.duran@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
8 * This code is largely based on the kremoteencodingplugin
9 * SPDX-FileCopyrightText: 2003 Thiago Macieira <thiago.macieira@kdemail.net>
10 * Distributed under the same terms.
13 #include "dolphinremoteencoding.h"
15 #include "dolphindebug.h"
16 #include "dolphinviewactionhandler.h"
18 #include <KActionCollection>
19 #include <KActionMenu>
22 #include <KConfigGroup>
23 #include <KIO/Scheduler>
24 #include <KLocalizedString>
25 #include <KProtocolInfo>
26 #include <KProtocolManager>
30 #define DATA_KEY QStringLiteral("Charset")
32 DolphinRemoteEncoding::DolphinRemoteEncoding(QObject
*parent
, DolphinViewActionHandler
*actionHandler
)
34 , m_actionHandler(actionHandler
)
38 m_menu
= new KActionMenu(QIcon::fromTheme(QStringLiteral("character-set")), i18n("Select Remote Charset"), this);
39 m_actionHandler
->actionCollection()->addAction(QStringLiteral("change_remote_encoding"), m_menu
);
40 connect(m_menu
->menu(), &QMenu::aboutToShow
, this, &DolphinRemoteEncoding::slotAboutToShow
);
42 m_menu
->setEnabled(false);
43 m_menu
->setPopupMode(QToolButton::InstantPopup
);
46 DolphinRemoteEncoding::~DolphinRemoteEncoding()
50 void DolphinRemoteEncoding::slotReload()
55 void DolphinRemoteEncoding::loadSettings()
58 m_encodingDescriptions
= KCharsets::charsets()->descriptiveEncodingNames();
63 void DolphinRemoteEncoding::slotAboutToOpenUrl()
65 QUrl oldURL
= m_currentURL
;
66 m_currentURL
= m_actionHandler
->currentView()->url();
68 if (m_currentURL
.scheme() != oldURL
.scheme()) {
69 // This plugin works on ftp, fish, etc.
70 // everything whose type is T_FILESYSTEM except for local files
71 if (!m_currentURL
.isLocalFile() && KProtocolManager::outputType(m_currentURL
) == KProtocolInfo::T_FILESYSTEM
) {
72 m_menu
->setEnabled(true);
75 m_menu
->setEnabled(false);
80 if (m_currentURL
.host() != oldURL
.host()) {
85 void DolphinRemoteEncoding::fillMenu()
87 QMenu
*menu
= m_menu
->menu();
90 menu
->addAction(i18n("Default"), this, &DolphinRemoteEncoding::slotDefault
)->setCheckable(true);
91 for (int i
= 0; i
< m_encodingDescriptions
.size(); i
++) {
92 QAction
*action
= new QAction(m_encodingDescriptions
.at(i
), this);
93 action
->setCheckable(true);
95 menu
->addAction(action
);
99 menu
->addAction(i18n("Reload"), this, &DolphinRemoteEncoding::slotReload
);
100 m_idDefault
= m_encodingDescriptions
.size() + 2;
102 connect(menu
, &QMenu::triggered
, this, &DolphinRemoteEncoding::slotItemSelected
);
105 void DolphinRemoteEncoding::updateMenu()
111 // uncheck everything
112 for (int i
= 0; i
< m_menu
->menu()->actions().count(); i
++) {
113 m_menu
->menu()->actions().at(i
)->setChecked(false);
116 const QString charset
= KCharsets::charsets()->descriptionForEncoding(KProtocolManager::charsetFor(m_currentURL
));
117 if (!charset
.isEmpty()) {
119 bool isFound
= false;
120 for (int i
= 0; i
< m_encodingDescriptions
.size(); i
++) {
121 if (m_encodingDescriptions
.at(i
) == charset
) {
128 qCDebug(DolphinDebug
) << "URL=" << m_currentURL
<< " charset=" << charset
;
131 qCWarning(DolphinDebug
) << "could not find entry for charset=" << charset
;
133 m_menu
->menu()->actions().at(id
)->setChecked(true);
136 m_menu
->menu()->actions().at(m_idDefault
)->setChecked(true);
140 void DolphinRemoteEncoding::slotAboutToShow()
148 void DolphinRemoteEncoding::slotItemSelected(QAction
*action
)
151 int id
= action
->data().toInt();
153 KConfig
config(("kio_" + m_currentURL
.scheme() + "rc").toLatin1());
154 QString host
= m_currentURL
.host();
155 if (m_menu
->menu()->actions().at(id
)->isChecked()) {
156 QString charset
= KCharsets::charsets()->encodingForName(m_encodingDescriptions
.at(id
));
157 KConfigGroup
cg(&config
, host
);
158 cg
.writeEntry(DATA_KEY
, charset
);
161 // Update the io-slaves...
167 void DolphinRemoteEncoding::slotDefault()
169 // We have no choice but delete all higher domain level
170 // settings here since it affects what will be matched.
171 KConfig
config(("kio_" + m_currentURL
.scheme() + "rc").toLatin1());
173 QStringList partList
= m_currentURL
.host().split('.', Qt::SkipEmptyParts
);
174 if (!partList
.isEmpty()) {
175 partList
.erase(partList
.begin());
178 // Remove the exact name match...
179 domains
<< m_currentURL
.host();
181 while (!partList
.isEmpty()) {
182 if (partList
.count() == 2) {
183 if (partList
[0].length() <= 2 && partList
[1].length() == 2) {
188 if (partList
.count() == 1) {
192 domains
<< partList
.join(QLatin1Char('.'));
193 partList
.erase(partList
.begin());
196 for (QStringList::const_iterator it
= domains
.constBegin(); it
!= domains
.constEnd(); ++it
) {
197 qCDebug(DolphinDebug
) << "Domain to remove: " << *it
;
198 if (config
.hasGroup(*it
)) {
199 config
.deleteGroup(*it
);
200 } else if (config
.group("").hasKey(*it
)) {
201 config
.group("").deleteEntry(*it
); //don't know what group name is supposed to be XXX
207 // Update the io-slaves.
211 void DolphinRemoteEncoding::updateView()
213 KIO::Scheduler::emitReparseSlaveConfiguration();
214 // Reload the page with the new charset
215 m_actionHandler
->currentView()->setUrl(m_currentURL
);
216 m_actionHandler
->currentView()->reload();
219 #include "moc_dolphinremoteencoding.cpp"