]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinremoteencoding.cpp
Merge branch 'Applications/18.08'
[dolphin.git] / src / views / dolphinremoteencoding.cpp
1 /***************************************************************************
2 * Copyright (C) 2009 by Rahman Duran <rahman.duran@gmail.com> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 /*
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.
24 */
25
26 #include "dolphinremoteencoding.h"
27
28 #include "dolphinviewactionhandler.h"
29 #include "dolphindebug.h"
30
31 #include <KActionCollection>
32 #include <KActionMenu>
33 #include <KCharsets>
34 #include <KConfig>
35 #include <KConfigGroup>
36 #include <KIO/Scheduler>
37 #include <KLocalizedString>
38 #include <KProtocolInfo>
39 #include <KProtocolManager>
40
41 #include <QMenu>
42
43 #define DATA_KEY QStringLiteral("Charset")
44
45 DolphinRemoteEncoding::DolphinRemoteEncoding(QObject* parent, DolphinViewActionHandler* actionHandler)
46 :QObject(parent),
47 m_actionHandler(actionHandler),
48 m_loaded(false),
49 m_idDefault(0)
50 {
51 m_menu = new KActionMenu(QIcon::fromTheme(QStringLiteral("character-set")), i18n("Select Remote Charset"), this);
52 m_actionHandler->actionCollection()->addAction(QStringLiteral("change_remote_encoding"), m_menu);
53 connect(m_menu->menu(), &QMenu::aboutToShow,
54 this, &DolphinRemoteEncoding::slotAboutToShow);
55
56 m_menu->setEnabled(false);
57 m_menu->setDelayed(false);
58 }
59
60 DolphinRemoteEncoding::~DolphinRemoteEncoding()
61 {
62 }
63
64 void DolphinRemoteEncoding::slotReload()
65 {
66 loadSettings();
67 }
68
69 void DolphinRemoteEncoding::loadSettings()
70 {
71 m_loaded = true;
72 m_encodingDescriptions = KCharsets::charsets()->descriptiveEncodingNames();
73
74 fillMenu();
75 }
76
77 void DolphinRemoteEncoding::slotAboutToOpenUrl()
78 {
79 QUrl oldURL = m_currentURL;
80 m_currentURL = m_actionHandler->currentView()->url();
81
82 if (m_currentURL.scheme() != oldURL.scheme()) {
83 // This plugin works on ftp, fish, etc.
84 // everything whose type is T_FILESYSTEM except for local files
85 if (!m_currentURL.isLocalFile() &&
86 KProtocolManager::outputType(m_currentURL) == KProtocolInfo::T_FILESYSTEM) {
87
88 m_menu->setEnabled(true);
89 loadSettings();
90 } else {
91 m_menu->setEnabled(false);
92 }
93 return;
94 }
95
96 if (m_currentURL.host() != oldURL.host()) {
97 updateMenu();
98 }
99 }
100
101 void DolphinRemoteEncoding::fillMenu()
102 {
103 QMenu* menu = m_menu->menu();
104 menu->clear();
105
106
107 for (int i = 0; i < m_encodingDescriptions.size();i++) {
108 QAction* action = new QAction(m_encodingDescriptions.at(i), this);
109 action->setCheckable(true);
110 action->setData(i);
111 menu->addAction(action);
112 }
113 menu->addSeparator();
114
115 menu->addAction(i18n("Reload"), this, SLOT(slotReload()), 0);
116 menu->addAction(i18n("Default"), this, SLOT(slotDefault()), 0)->setCheckable(true);
117 m_idDefault = m_encodingDescriptions.size() + 2;
118
119 connect(menu, &QMenu::triggered, this, &DolphinRemoteEncoding::slotItemSelected);
120 }
121
122 void DolphinRemoteEncoding::updateMenu()
123 {
124 if (!m_loaded) {
125 loadSettings();
126 }
127
128 // uncheck everything
129 for (int i = 0; i < m_menu->menu()->actions().count(); i++) {
130 m_menu->menu()->actions().at(i)->setChecked(false);
131 }
132
133 const QString charset = KCharsets::charsets()->descriptionForEncoding(KProtocolManager::charsetFor(m_currentURL));
134 if (!charset.isEmpty()) {
135 int id = 0;
136 bool isFound = false;
137 for (int i = 0; i < m_encodingDescriptions.size(); i++) {
138 if (m_encodingDescriptions.at(i) == charset) {
139 isFound = true;
140 id = i;
141 break;
142 }
143 }
144
145 qCDebug(DolphinDebug) << "URL=" << m_currentURL << " charset=" << charset;
146
147 if (!isFound) {
148 qCWarning(DolphinDebug) << "could not find entry for charset=" << charset ;
149 } else {
150 m_menu->menu()->actions().at(id)->setChecked(true);
151 }
152 } else {
153 m_menu->menu()->actions().at(m_idDefault)->setChecked(true);
154 }
155
156 }
157
158 void DolphinRemoteEncoding::slotAboutToShow()
159 {
160 if (!m_loaded) {
161 loadSettings();
162 }
163 updateMenu();
164 }
165
166 void DolphinRemoteEncoding::slotItemSelected(QAction* action)
167 {
168 if (action) {
169 int id = action->data().toInt();
170
171 KConfig config(("kio_" + m_currentURL.scheme() + "rc").toLatin1());
172 QString host = m_currentURL.host();
173 if (m_menu->menu()->actions().at(id)->isChecked()) {
174 QString charset = KCharsets::charsets()->encodingForName(m_encodingDescriptions.at(id));
175 KConfigGroup cg(&config, host);
176 cg.writeEntry(DATA_KEY, charset);
177 config.sync();
178
179 // Update the io-slaves...
180 updateView();
181 }
182 }
183 }
184
185 void DolphinRemoteEncoding::slotDefault()
186 {
187 // We have no choice but delete all higher domain level
188 // settings here since it affects what will be matched.
189 KConfig config(("kio_" + m_currentURL.scheme() + "rc").toLatin1());
190
191 QStringList partList = m_currentURL.host().split('.', QString::SkipEmptyParts);
192 if (!partList.isEmpty()) {
193 partList.erase(partList.begin());
194
195 QStringList domains;
196 // Remove the exact name match...
197 domains << m_currentURL.host();
198
199 while (!partList.isEmpty()) {
200 if (partList.count() == 2) {
201 if (partList[0].length() <= 2 && partList[1].length() == 2) {
202 break;
203 }
204 }
205
206 if (partList.count() == 1) {
207 break;
208 }
209
210 domains << partList.join(QLatin1Char('.'));
211 partList.erase(partList.begin());
212 }
213
214 for (QStringList::const_iterator it = domains.constBegin(); it != domains.constEnd();++it) {
215 qCDebug(DolphinDebug) << "Domain to remove: " << *it;
216 if (config.hasGroup(*it)) {
217 config.deleteGroup(*it);
218 } else if (config.group("").hasKey(*it)) {
219 config.group("").deleteEntry(*it); //don't know what group name is supposed to be XXX
220 }
221 }
222 }
223 config.sync();
224
225 // Update the io-slaves.
226 updateView();
227 }
228
229 void DolphinRemoteEncoding::updateView()
230 {
231 KIO::Scheduler::emitReparseSlaveConfiguration();
232 // Reload the page with the new charset
233 m_actionHandler->currentView()->setUrl(m_currentURL);
234 m_actionHandler->currentView()->reload();
235 }
236