]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinremoteencoding.cpp
Add support for recentlyused: kio to view properties.
[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 "dolphinviewactionhandler.h"
16 #include "dolphindebug.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,
41 this, &DolphinRemoteEncoding::slotAboutToShow);
42
43 m_menu->setEnabled(false);
44 m_menu->setPopupMode(QToolButton::InstantPopup);
45 }
46
47 DolphinRemoteEncoding::~DolphinRemoteEncoding()
48 {
49 }
50
51 void DolphinRemoteEncoding::slotReload()
52 {
53 loadSettings();
54 }
55
56 void DolphinRemoteEncoding::loadSettings()
57 {
58 m_loaded = true;
59 m_encodingDescriptions = KCharsets::charsets()->descriptiveEncodingNames();
60
61 fillMenu();
62 }
63
64 void DolphinRemoteEncoding::slotAboutToOpenUrl()
65 {
66 QUrl oldURL = m_currentURL;
67 m_currentURL = m_actionHandler->currentView()->url();
68
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() &&
73 KProtocolManager::outputType(m_currentURL) == KProtocolInfo::T_FILESYSTEM) {
74
75 m_menu->setEnabled(true);
76 loadSettings();
77 } else {
78 m_menu->setEnabled(false);
79 }
80 return;
81 }
82
83 if (m_currentURL.host() != oldURL.host()) {
84 updateMenu();
85 }
86 }
87
88 void DolphinRemoteEncoding::fillMenu()
89 {
90 QMenu* menu = m_menu->menu();
91 menu->clear();
92
93
94 for (int i = 0; i < m_encodingDescriptions.size();i++) {
95 QAction* action = new QAction(m_encodingDescriptions.at(i), this);
96 action->setCheckable(true);
97 action->setData(i);
98 menu->addAction(action);
99 }
100 menu->addSeparator();
101
102 menu->addAction(i18n("Reload"), this, SLOT(slotReload()), 0);
103 menu->addAction(i18n("Default"), this, SLOT(slotDefault()), 0)->setCheckable(true);
104 m_idDefault = m_encodingDescriptions.size() + 2;
105
106 connect(menu, &QMenu::triggered, this, &DolphinRemoteEncoding::slotItemSelected);
107 }
108
109 void DolphinRemoteEncoding::updateMenu()
110 {
111 if (!m_loaded) {
112 loadSettings();
113 }
114
115 // uncheck everything
116 for (int i = 0; i < m_menu->menu()->actions().count(); i++) {
117 m_menu->menu()->actions().at(i)->setChecked(false);
118 }
119
120 const QString charset = KCharsets::charsets()->descriptionForEncoding(KProtocolManager::charsetFor(m_currentURL));
121 if (!charset.isEmpty()) {
122 int id = 0;
123 bool isFound = false;
124 for (int i = 0; i < m_encodingDescriptions.size(); i++) {
125 if (m_encodingDescriptions.at(i) == charset) {
126 isFound = true;
127 id = i;
128 break;
129 }
130 }
131
132 qCDebug(DolphinDebug) << "URL=" << m_currentURL << " charset=" << charset;
133
134 if (!isFound) {
135 qCWarning(DolphinDebug) << "could not find entry for charset=" << charset ;
136 } else {
137 m_menu->menu()->actions().at(id)->setChecked(true);
138 }
139 } else {
140 m_menu->menu()->actions().at(m_idDefault)->setChecked(true);
141 }
142
143 }
144
145 void DolphinRemoteEncoding::slotAboutToShow()
146 {
147 if (!m_loaded) {
148 loadSettings();
149 }
150 updateMenu();
151 }
152
153 void DolphinRemoteEncoding::slotItemSelected(QAction* action)
154 {
155 if (action) {
156 int id = action->data().toInt();
157
158 KConfig config(("kio_" + m_currentURL.scheme() + "rc").toLatin1());
159 QString host = m_currentURL.host();
160 if (m_menu->menu()->actions().at(id)->isChecked()) {
161 QString charset = KCharsets::charsets()->encodingForName(m_encodingDescriptions.at(id));
162 KConfigGroup cg(&config, host);
163 cg.writeEntry(DATA_KEY, charset);
164 config.sync();
165
166 // Update the io-slaves...
167 updateView();
168 }
169 }
170 }
171
172 void DolphinRemoteEncoding::slotDefault()
173 {
174 // We have no choice but delete all higher domain level
175 // settings here since it affects what will be matched.
176 KConfig config(("kio_" + m_currentURL.scheme() + "rc").toLatin1());
177
178 QStringList partList = m_currentURL.host().split('.', Qt::SkipEmptyParts);
179 if (!partList.isEmpty()) {
180 partList.erase(partList.begin());
181
182 QStringList domains;
183 // Remove the exact name match...
184 domains << m_currentURL.host();
185
186 while (!partList.isEmpty()) {
187 if (partList.count() == 2) {
188 if (partList[0].length() <= 2 && partList[1].length() == 2) {
189 break;
190 }
191 }
192
193 if (partList.count() == 1) {
194 break;
195 }
196
197 domains << partList.join(QLatin1Char('.'));
198 partList.erase(partList.begin());
199 }
200
201 for (QStringList::const_iterator it = domains.constBegin(); it != domains.constEnd();++it) {
202 qCDebug(DolphinDebug) << "Domain to remove: " << *it;
203 if (config.hasGroup(*it)) {
204 config.deleteGroup(*it);
205 } else if (config.group("").hasKey(*it)) {
206 config.group("").deleteEntry(*it); //don't know what group name is supposed to be XXX
207 }
208 }
209 }
210 config.sync();
211
212 // Update the io-slaves.
213 updateView();
214 }
215
216 void DolphinRemoteEncoding::updateView()
217 {
218 KIO::Scheduler::emitReparseSlaveConfiguration();
219 // Reload the page with the new charset
220 m_actionHandler->currentView()->setUrl(m_currentURL);
221 m_actionHandler->currentView()->reload();
222 }
223