]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinremoteencoding.cpp
Merge branch 'master' into kf6
[dolphin.git] / src / views / dolphinremoteencoding.cpp
1 /*
2 * SPDX-FileCopyrightText: 2009 Rahman Duran <rahman.duran@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 /*
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.
11 */
12
13 #include "dolphinremoteencoding.h"
14
15 #include "dolphindebug.h"
16 #include "dolphinviewactionhandler.h"
17
18 #include <KActionCollection>
19 #include <KActionMenu>
20 #include <KCharsets>
21 #include <KConfig>
22 #include <KConfigGroup>
23 #include <KIO/Scheduler>
24 #include <KLocalizedString>
25 #include <KProtocolInfo>
26 #include <KProtocolManager>
27
28 #include <QMenu>
29
30 #define DATA_KEY QStringLiteral("Charset")
31
32 DolphinRemoteEncoding::DolphinRemoteEncoding(QObject *parent, DolphinViewActionHandler *actionHandler)
33 : QObject(parent)
34 , m_actionHandler(actionHandler)
35 , m_loaded(false)
36 , m_idDefault(0)
37 {
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);
41
42 m_menu->setEnabled(false);
43 m_menu->setPopupMode(QToolButton::InstantPopup);
44 }
45
46 DolphinRemoteEncoding::~DolphinRemoteEncoding()
47 {
48 }
49
50 void DolphinRemoteEncoding::slotReload()
51 {
52 loadSettings();
53 }
54
55 void DolphinRemoteEncoding::loadSettings()
56 {
57 m_loaded = true;
58 m_encodingDescriptions = KCharsets::charsets()->descriptiveEncodingNames();
59
60 fillMenu();
61 }
62
63 void DolphinRemoteEncoding::slotAboutToOpenUrl()
64 {
65 QUrl oldURL = m_currentURL;
66 m_currentURL = m_actionHandler->currentView()->url();
67
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);
73 loadSettings();
74 } else {
75 m_menu->setEnabled(false);
76 }
77 return;
78 }
79
80 if (m_currentURL.host() != oldURL.host()) {
81 updateMenu();
82 }
83 }
84
85 void DolphinRemoteEncoding::fillMenu()
86 {
87 QMenu *menu = m_menu->menu();
88 menu->clear();
89
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);
94 action->setData(i);
95 menu->addAction(action);
96 }
97 menu->addSeparator();
98
99 menu->addAction(i18n("Reload"), this, &DolphinRemoteEncoding::slotReload);
100 m_idDefault = m_encodingDescriptions.size() + 2;
101
102 connect(menu, &QMenu::triggered, this, &DolphinRemoteEncoding::slotItemSelected);
103 }
104
105 void DolphinRemoteEncoding::updateMenu()
106 {
107 if (!m_loaded) {
108 loadSettings();
109 }
110
111 // uncheck everything
112 for (int i = 0; i < m_menu->menu()->actions().count(); i++) {
113 m_menu->menu()->actions().at(i)->setChecked(false);
114 }
115
116 const QString charset = KCharsets::charsets()->descriptionForEncoding(KProtocolManager::charsetFor(m_currentURL));
117 if (!charset.isEmpty()) {
118 int id = 0;
119 bool isFound = false;
120 for (int i = 0; i < m_encodingDescriptions.size(); i++) {
121 if (m_encodingDescriptions.at(i) == charset) {
122 isFound = true;
123 id = i;
124 break;
125 }
126 }
127
128 qCDebug(DolphinDebug) << "URL=" << m_currentURL << " charset=" << charset;
129
130 if (!isFound) {
131 qCWarning(DolphinDebug) << "could not find entry for charset=" << charset;
132 } else {
133 m_menu->menu()->actions().at(id)->setChecked(true);
134 }
135 } else {
136 m_menu->menu()->actions().at(m_idDefault)->setChecked(true);
137 }
138 }
139
140 void DolphinRemoteEncoding::slotAboutToShow()
141 {
142 if (!m_loaded) {
143 loadSettings();
144 }
145 updateMenu();
146 }
147
148 void DolphinRemoteEncoding::slotItemSelected(QAction *action)
149 {
150 if (action) {
151 int id = action->data().toInt();
152
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);
159 config.sync();
160
161 // Update the io-slaves...
162 updateView();
163 }
164 }
165 }
166
167 void DolphinRemoteEncoding::slotDefault()
168 {
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());
172
173 QStringList partList = m_currentURL.host().split('.', Qt::SkipEmptyParts);
174 if (!partList.isEmpty()) {
175 partList.erase(partList.begin());
176
177 QStringList domains;
178 // Remove the exact name match...
179 domains << m_currentURL.host();
180
181 while (!partList.isEmpty()) {
182 if (partList.count() == 2) {
183 if (partList[0].length() <= 2 && partList[1].length() == 2) {
184 break;
185 }
186 }
187
188 if (partList.count() == 1) {
189 break;
190 }
191
192 domains << partList.join(QLatin1Char('.'));
193 partList.erase(partList.begin());
194 }
195
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
202 }
203 }
204 }
205 config.sync();
206
207 // Update the io-slaves.
208 updateView();
209 }
210
211 void DolphinRemoteEncoding::updateView()
212 {
213 KIO::Scheduler::emitReparseSlaveConfiguration();
214 // Reload the page with the new charset
215 m_actionHandler->currentView()->setUrl(m_currentURL);
216 m_actionHandler->currentView()->reload();
217 }