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