1
/***************************************************************************
2
krremoteencodingmenu.cpp - description
4
copyright : (C) 2005 + by Csaba Karai
5
based on : KRemoteEncodingPlugin from Dawit Alemayehu
6
e-mail : krusader@users.sourceforge.net
7
web site : http://krusader.sourceforge.net
8
---------------------------------------------------------------------------
10
***************************************************************************
14
db dD d8888b. db db .d8888. .d8b. d8888b. d88888b d8888b.
15
88 ,8P' 88 `8D 88 88 88' YP d8' `8b 88 `8D 88' 88 `8D
16
88,8P 88oobY' 88 88 `8bo. 88ooo88 88 88 88ooooo 88oobY'
17
88`8b 88`8b 88 88 `Y8b. 88~~~88 88 88 88~~~~~ 88`8b
18
88 `88. 88 `88. 88b d88 db 8D 88 88 88 .8D 88. 88 `88.
19
YP YD 88 YD ~Y8888P' `8888Y' YP YP Y8888D' Y88888P 88 YD
23
***************************************************************************
25
* This program is free software; you can redistribute it and/or modify *
26
* it under the terms of the GNU General Public License as published by *
27
* the Free Software Foundation; either version 2 of the License, or *
28
* (at your option) any later version. *
30
***************************************************************************/
1
/*****************************************************************************
2
* Copyright (C) 2005 Csaba Karai <cskarai@freemail.hu> *
3
* based on KRemoteEncodingPlugin from Dawit Alemayehu <adawit@kde.org> *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 2 of the License, or *
8
* (at your option) any later version. *
10
* This package is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this package; if not, write to the Free Software *
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
18
*****************************************************************************/
32
20
#include "krremoteencodingmenu.h"
46
34
#define DATA_KEY QString::fromLatin1("Charset")
48
36
KrRemoteEncodingMenu::KrRemoteEncodingMenu(const QString &text, const QString &icon, KActionCollection *parent) :
49
KActionMenu( KIcon( icon, krLoader ), text, parent ), settingsLoaded( false )
37
KActionMenu(KIcon(icon, krLoader), text, parent), settingsLoaded(false)
51
connect(menu(), SIGNAL(aboutToShow()), this, SLOT(slotAboutToShow()));
39
connect(menu(), SIGNAL(aboutToShow()), this, SLOT(slotAboutToShow()));
53
parent->addAction("changeremoteencoding", this);
41
parent->addAction("changeremoteencoding", this);
56
44
void KrRemoteEncodingMenu::slotAboutToShow()
62
QList<QAction *> acts = menu()->actions();
63
foreach( QAction *act, acts )
64
act->setChecked( false );
66
KUrl currentURL = ACTIVE_PANEL->virtualPath();
68
QString charset = KIO::SlaveConfig::self()->configData(currentURL.protocol(), currentURL.host(), DATA_KEY);
69
if (!charset.isEmpty())
72
QStringList::Iterator it;
73
for (it = encodingNames.begin(); it != encodingNames.end(); ++it, ++id)
74
if ((*it).indexOf(charset) != -1)
79
foreach( QAction *act, acts ) {
80
if( act->data().canConvert<int> () ) {
81
int idr = act->data().toInt();
84
act->setChecked( found = true );
91
kWarning() << k_funcinfo << "could not find entry for charset=" << charset << endl;
94
foreach( QAction *act, acts ) {
95
if( act->data().canConvert<int> () ) {
96
int idr = act->data().toInt();
99
act->setChecked( true );
50
QList<QAction *> acts = menu()->actions();
51
foreach(QAction *act, acts)
52
act->setChecked(false);
54
QString charset = currentCharacterSet();
55
if (!charset.isEmpty()) {
57
QStringList::Iterator it;
58
for (it = encodingNames.begin(); it != encodingNames.end(); ++it, ++id)
59
if ((*it).indexOf(charset) != -1)
64
foreach(QAction *act, acts) {
65
if (act->data().canConvert<int> ()) {
66
int idr = act->data().toInt();
69
act->setChecked(found = true);
76
kWarning() << k_funcinfo << "could not find entry for charset=" << charset << endl;
78
foreach(QAction *act, acts) {
79
if (act->data().canConvert<int> ()) {
80
int idr = act->data().toInt();
83
act->setChecked(true);
91
QString KrRemoteEncodingMenu::currentCharacterSet()
93
KUrl currentURL = ACTIVE_PANEL->virtualPath();
94
return KIO::SlaveConfig::self()->configData(currentURL.protocol(), currentURL.host(), DATA_KEY);
107
97
void KrRemoteEncodingMenu::loadSettings()
109
settingsLoaded = true;
110
encodingNames = KGlobal::charsets()->descriptiveEncodingNames();
112
KMenu *kmenu = menu();
113
disconnect( kmenu, SIGNAL( triggered ( QAction * ) ), this, SLOT( slotTriggered ( QAction * ) ) );
114
connect( kmenu, SIGNAL( triggered ( QAction * ) ), this, SLOT( slotTriggered ( QAction * ) ) );
117
QStringList::ConstIterator it;
121
for (it = encodingNames.begin(); it != encodingNames.end(); ++it) {
122
act = kmenu->addAction( *it );
123
act->setData( QVariant( ++count) );
124
act->setCheckable( true );
126
kmenu->addSeparator();
128
act = kmenu->addAction(i18n("Reload"));
129
act->setCheckable( true );
130
act->setData( QVariant( -1 ) );
132
act = kmenu->addAction(i18n("Default"));
133
act->setCheckable( true );
134
act->setData( QVariant( -2 ) );
137
void KrRemoteEncodingMenu::slotTriggered( QAction * act )
139
if( !act || !act->data().canConvert<int> () )
142
int id = act->data().toInt();
154
KUrl currentURL = ACTIVE_PANEL->virtualPath();
156
KConfig config(("kio_" + currentURL.protocol() + "rc").toLatin1());
157
QString host = currentURL.host();
159
QString charset = KGlobal::charsets()->encodingForName( encodingNames[id - 1] );
161
KConfigGroup group( &config, host);
162
group.writeEntry(DATA_KEY, charset);
165
// Update the io-slaves...
99
settingsLoaded = true;
100
encodingNames = KGlobal::charsets()->descriptiveEncodingNames();
102
KMenu *kmenu = menu();
103
disconnect(kmenu, SIGNAL(triggered(QAction *)), this, SLOT(slotTriggered(QAction *)));
104
connect(kmenu, SIGNAL(triggered(QAction *)), this, SLOT(slotTriggered(QAction *)));
107
QStringList::ConstIterator it;
111
for (it = encodingNames.constBegin(); it != encodingNames.constEnd(); ++it) {
112
act = kmenu->addAction(*it);
113
act->setData(QVariant(++count));
114
act->setCheckable(true);
116
kmenu->addSeparator();
118
act = kmenu->addAction(i18n("Reload"));
119
act->setCheckable(true);
120
act->setData(QVariant(-1));
122
act = kmenu->addAction(i18n("Default"));
123
act->setCheckable(true);
124
act->setData(QVariant(-2));
127
void KrRemoteEncodingMenu::slotTriggered(QAction * act)
129
if (!act || !act->data().canConvert<int> ())
132
int id = act->data().toInt();
142
chooseEncoding(encodingNames[id - 1]);
146
void KrRemoteEncodingMenu::chooseEncoding(QString encoding)
148
KUrl currentURL = ACTIVE_PANEL->virtualPath();
150
KConfig config(("kio_" + currentURL.protocol() + "rc").toLatin1());
151
QString host = currentURL.host();
153
QString charset = KGlobal::charsets()->encodingForName(encoding);
155
KConfigGroup group(&config, host);
156
group.writeEntry(DATA_KEY, charset);
159
// Update the io-slaves...
171
163
void KrRemoteEncodingMenu::slotReload()
176
void KrRemoteEncodingMenu::slotDefault()
168
void KrRemoteEncodingMenu::chooseDefault()
178
KUrl currentURL = ACTIVE_PANEL->virtualPath();
180
// We have no choice but delete all higher domain level
181
// settings here since it affects what will be matched.
182
KConfig config ( ( "kio_" + currentURL.protocol() + "rc" ).toLatin1() );
184
QStringList partList = currentURL.host().split ( '.', QString::SkipEmptyParts );
185
if ( !partList.isEmpty() )
187
partList.erase ( partList.begin() );
190
// Remove the exact name match...
191
domains << currentURL.host();
193
while ( partList.count() )
195
if ( partList.count() == 2 )
196
if ( partList[0].length() <= 2 && partList[1].length() == 2 )
199
if ( partList.count() == 1 )
202
domains << partList.join ( "." );
203
partList.erase ( partList.begin() );
206
for ( QStringList::Iterator it = domains.begin(); it != domains.end(); ++it )
208
kDebug() << "Domain to remove: " << *it;
209
if ( config.hasGroup ( *it ) )
210
config.deleteGroup ( *it );
211
else if ( config.group ( "" ).hasKey ( *it ) )
212
config.group ( "" ).deleteEntry ( *it ); //don't know what group name is supposed to be XXX
170
KUrl currentURL = ACTIVE_PANEL->virtualPath();
172
// We have no choice but delete all higher domain level
173
// settings here since it affects what will be matched.
174
KConfig config(("kio_" + currentURL.protocol() + "rc").toLatin1());
176
QStringList partList = currentURL.host().split('.', QString::SkipEmptyParts);
177
if (!partList.isEmpty()) {
178
partList.erase(partList.begin());
181
// Remove the exact name match...
182
domains << currentURL.host();
184
while (partList.count()) {
185
if (partList.count() == 2)
186
if (partList[0].length() <= 2 && partList[1].length() == 2)
189
if (partList.count() == 1)
192
domains << partList.join(".");
193
partList.erase(partList.begin());
196
for (QStringList::Iterator it = domains.begin(); it != domains.end(); ++it) {
197
kDebug() << "Domain to remove: " << *it;
198
if (config.hasGroup(*it))
199
config.deleteGroup(*it);
200
else if (config.group("").hasKey(*it))
201
config.group("").deleteEntry(*it); //don't know what group name is supposed to be XXX
221
210
void KrRemoteEncodingMenu::updateKIOSlaves()
223
KIO::Scheduler::emitReparseSlaveConfiguration();
212
KIO::Scheduler::emitReparseSlaveConfiguration();
225
// Reload the page with the new charset
226
QTimer::singleShot( 500, ACTIVE_FUNC, SLOT( refresh() ) );
214
// Reload the page with the new charset
215
QTimer::singleShot(500, ACTIVE_FUNC, SLOT(refresh()));
229
218
#include "krremoteencodingmenu.moc"