]>
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 <KLocalizedString>
24 #include <KProtocolInfo>
25 #include <KProtocolManager>
27 #include <QDBusConnection>
28 #include <QDBusMessage>
31 #define DATA_KEY QStringLiteral("Charset")
33 DolphinRemoteEncoding::DolphinRemoteEncoding(QObject
*parent
, DolphinViewActionHandler
*actionHandler
)
35 , m_actionHandler(actionHandler
)
39 m_menu
= new KActionMenu(QIcon::fromTheme(QStringLiteral("character-set")), i18n("Select Remote Charset"), this);
40 m_actionHandler
->actionCollection()->addAction(QStringLiteral("change_remote_encoding"), m_menu
);
41 connect(m_menu
->menu(), &QMenu::aboutToShow
, this, &DolphinRemoteEncoding::slotAboutToShow
);
43 m_menu
->setEnabled(false);
44 m_menu
->setPopupMode(QToolButton::InstantPopup
);
47 DolphinRemoteEncoding::~DolphinRemoteEncoding()
51 void DolphinRemoteEncoding::slotReload()
56 void DolphinRemoteEncoding::loadSettings()
59 m_encodingDescriptions
= KCharsets::charsets()->descriptiveEncodingNames();
64 void DolphinRemoteEncoding::slotAboutToOpenUrl()
66 QUrl oldURL
= m_currentURL
;
67 m_currentURL
= m_actionHandler
->currentView()->url();
69 if (m_currentURL
.scheme() != oldURL
.scheme()) {
70 // This plugin works on ftp, fish, etc.
71 // everything whose type is T_FILESYSTEM except for local files
72 if (!m_currentURL
.isLocalFile() && KProtocolManager::outputType(m_currentURL
) == KProtocolInfo::T_FILESYSTEM
) {
73 m_menu
->setEnabled(true);
76 m_menu
->setEnabled(false);
81 if (m_currentURL
.host() != oldURL
.host()) {
86 void DolphinRemoteEncoding::fillMenu()
88 QMenu
*menu
= m_menu
->menu();
91 menu
->addAction(i18n("Default"), this, &DolphinRemoteEncoding::slotDefault
)->setCheckable(true);
92 for (int i
= 0; i
< m_encodingDescriptions
.size(); i
++) {
93 QAction
*action
= new QAction(m_encodingDescriptions
.at(i
), this);
94 action
->setCheckable(true);
96 menu
->addAction(action
);
100 menu
->addAction(i18n("Reload"), this, &DolphinRemoteEncoding::slotReload
);
101 m_idDefault
= m_encodingDescriptions
.size() + 2;
103 connect(menu
, &QMenu::triggered
, this, &DolphinRemoteEncoding::slotItemSelected
);
106 void DolphinRemoteEncoding::updateMenu()
112 // uncheck everything
113 for (int i
= 0; i
< m_menu
->menu()->actions().count(); i
++) {
114 m_menu
->menu()->actions().at(i
)->setChecked(false);
117 const QString charset
= KCharsets::charsets()->descriptionForEncoding(KProtocolManager::charsetFor(m_currentURL
));
118 if (!charset
.isEmpty()) {
120 bool isFound
= false;
121 for (int i
= 0; i
< m_encodingDescriptions
.size(); i
++) {
122 if (m_encodingDescriptions
.at(i
) == charset
) {
129 qCDebug(DolphinDebug
) << "URL=" << m_currentURL
<< " charset=" << charset
;
132 qCWarning(DolphinDebug
) << "could not find entry for charset=" << charset
;
134 m_menu
->menu()->actions().at(id
)->setChecked(true);
137 m_menu
->menu()->actions().at(m_idDefault
)->setChecked(true);
141 void DolphinRemoteEncoding::slotAboutToShow()
149 void DolphinRemoteEncoding::slotItemSelected(QAction
*action
)
152 int id
= action
->data().toInt();
154 KConfig
config(QLatin1String("kio_%1rc").arg(m_currentURL
.scheme()));
155 QString host
= m_currentURL
.host();
156 if (m_menu
->menu()->actions().at(id
)->isChecked()) {
157 QString charset
= KCharsets::charsets()->encodingForName(m_encodingDescriptions
.at(id
));
158 KConfigGroup
cg(&config
, host
);
159 cg
.writeEntry(DATA_KEY
, charset
);
162 // Update the io-slaves...
168 void DolphinRemoteEncoding::slotDefault()
170 // We have no choice but delete all higher domain level
171 // settings here since it affects what will be matched.
172 KConfig
config(QLatin1String("kio_%1rc").arg(m_currentURL
.scheme()));
174 QStringList partList
= m_currentURL
.host().split(QLatin1Char('.'), Qt::SkipEmptyParts
);
175 if (!partList
.isEmpty()) {
176 partList
.erase(partList
.begin());
179 // Remove the exact name match...
180 domains
<< m_currentURL
.host();
182 while (!partList
.isEmpty()) {
183 if (partList
.count() == 2) {
184 if (partList
[0].length() <= 2 && partList
[1].length() == 2) {
189 if (partList
.count() == 1) {
193 domains
<< partList
.join(QLatin1Char('.'));
194 partList
.erase(partList
.begin());
197 for (QStringList::const_iterator it
= domains
.constBegin(); it
!= domains
.constEnd(); ++it
) {
198 qCDebug(DolphinDebug
) << "Domain to remove: " << *it
;
199 if (config
.hasGroup(*it
)) {
200 config
.deleteGroup(*it
);
201 } else if (config
.group(QString()).hasKey(*it
)) {
202 config
.group(QString()).deleteEntry(*it
); // don't know what group name is supposed to be XXX
208 // Update the io-slaves.
212 void DolphinRemoteEncoding::updateView()
214 QDBusMessage message
=
215 QDBusMessage::createSignal(QStringLiteral("/KIO/Scheduler"), QStringLiteral("org.kde.KIO.Scheduler"), QStringLiteral("reparseSlaveConfiguration"));
216 message
<< QString();
217 QDBusConnection::sessionBus().send(message
);
219 // Reload the page with the new charset
220 m_actionHandler
->currentView()->setUrl(m_currentURL
);
221 m_actionHandler
->currentView()->reload();
224 #include "moc_dolphinremoteencoding.cpp"