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