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