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