~ubuntu-branches/ubuntu/vivid/krusader/vivid-proposed

« back to all changes in this revision

Viewing changes to krusader/GUI/krremoteencodingmenu.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-05-05 22:26:37 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100505222637-ydv3cwjwy365on2r
Tags: 1:2.1.0~beta1-1ubuntu1
* Merge from Debian Unstable.  Remaining changes:
  - Retain Kubuntu doc path

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
                   krremoteencodingmenu.cpp  -  description
3
 
                             -------------------
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
 
 ---------------------------------------------------------------------------
9
 
  Description
10
 
 ***************************************************************************
11
 
 
12
 
  A
13
 
 
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
20
 
 
21
 
                                                     S o u r c e    F i l e
22
 
 
23
 
 ***************************************************************************
24
 
 *                                                                         *
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.                                   *
29
 
 *                                                                         *
30
 
 ***************************************************************************/
 
1
/*****************************************************************************
 
2
 * Copyright (C) 2005 Csaba Karai <cskarai@freemail.hu>                      *
 
3
 * based on KRemoteEncodingPlugin from Dawit Alemayehu <adawit@kde.org>      *
 
4
 *                                                                           *
 
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.                                       *
 
9
 *                                                                           *
 
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.                              *
 
14
 *                                                                           *
 
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
 *****************************************************************************/
31
19
 
32
20
#include "krremoteencodingmenu.h"
33
21
 
46
34
#define DATA_KEY    QString::fromLatin1("Charset")
47
35
 
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)
50
38
{
51
 
  connect(menu(), SIGNAL(aboutToShow()), this, SLOT(slotAboutToShow()));
 
39
    connect(menu(), SIGNAL(aboutToShow()), this, SLOT(slotAboutToShow()));
52
40
 
53
 
  parent->addAction("changeremoteencoding", this);
 
41
    parent->addAction("changeremoteencoding", this);
54
42
}
55
43
 
56
44
void KrRemoteEncodingMenu::slotAboutToShow()
57
45
{
58
 
  if (!settingsLoaded)
59
 
    loadSettings();
60
 
 
61
 
  // uncheck everything
62
 
  QList<QAction *> acts = menu()->actions();
63
 
  foreach( QAction *act, acts )
64
 
    act->setChecked( false );
65
 
 
66
 
  KUrl currentURL = ACTIVE_PANEL->virtualPath();
67
 
 
68
 
  QString charset = KIO::SlaveConfig::self()->configData(currentURL.protocol(), currentURL.host(), DATA_KEY);
69
 
  if (!charset.isEmpty())
70
 
  {
71
 
    int id = 1;
72
 
    QStringList::Iterator it;
73
 
    for (it = encodingNames.begin(); it != encodingNames.end(); ++it, ++id)
74
 
      if ((*it).indexOf(charset) != -1)
75
 
        break;
76
 
 
77
 
    bool found = false;
78
 
 
79
 
    foreach( QAction *act, acts ) {
80
 
      if( act->data().canConvert<int> () ) {
81
 
        int idr = act->data().toInt();
82
 
 
83
 
        if( idr == id ) {
84
 
          act->setChecked( found = true );
85
 
          break;
86
 
        }
87
 
      }
88
 
    }
89
 
 
90
 
    if( !found )
91
 
      kWarning() << k_funcinfo << "could not find entry for charset=" << charset << endl;
92
 
  }
93
 
  else {
94
 
    foreach( QAction *act, acts ) {
95
 
      if( act->data().canConvert<int> () ) {
96
 
        int idr = act->data().toInt();
97
 
 
98
 
        if( idr == -2 ) {
99
 
          act->setChecked( true );
100
 
          break;
101
 
        }
102
 
      }
103
 
    }
104
 
  }
 
46
    if (!settingsLoaded)
 
47
        loadSettings();
 
48
 
 
49
    // uncheck everything
 
50
    QList<QAction *> acts = menu()->actions();
 
51
    foreach(QAction *act, acts)
 
52
    act->setChecked(false);
 
53
 
 
54
    QString charset = currentCharacterSet();
 
55
    if (!charset.isEmpty()) {
 
56
        int id = 1;
 
57
        QStringList::Iterator it;
 
58
        for (it = encodingNames.begin(); it != encodingNames.end(); ++it, ++id)
 
59
            if ((*it).indexOf(charset) != -1)
 
60
                break;
 
61
 
 
62
        bool found = false;
 
63
 
 
64
        foreach(QAction *act, acts) {
 
65
            if (act->data().canConvert<int> ()) {
 
66
                int idr = act->data().toInt();
 
67
 
 
68
                if (idr == id) {
 
69
                    act->setChecked(found = true);
 
70
                    break;
 
71
                }
 
72
            }
 
73
        }
 
74
 
 
75
        if (!found)
 
76
            kWarning() << k_funcinfo << "could not find entry for charset=" << charset << endl;
 
77
    } else {
 
78
        foreach(QAction *act, acts) {
 
79
            if (act->data().canConvert<int> ()) {
 
80
                int idr = act->data().toInt();
 
81
 
 
82
                if (idr == -2) {
 
83
                    act->setChecked(true);
 
84
                    break;
 
85
                }
 
86
            }
 
87
        }
 
88
    }
 
89
}
 
90
 
 
91
QString KrRemoteEncodingMenu::currentCharacterSet()
 
92
{
 
93
    KUrl currentURL = ACTIVE_PANEL->virtualPath();
 
94
    return KIO::SlaveConfig::self()->configData(currentURL.protocol(), currentURL.host(), DATA_KEY);
105
95
}
106
96
 
107
97
void KrRemoteEncodingMenu::loadSettings()
108
98
{
109
 
  settingsLoaded = true;
110
 
  encodingNames = KGlobal::charsets()->descriptiveEncodingNames();
111
 
 
112
 
  KMenu *kmenu = menu();
113
 
  disconnect( kmenu, SIGNAL( triggered ( QAction * ) ), this, SLOT( slotTriggered ( QAction * ) ) );
114
 
  connect( kmenu, SIGNAL( triggered ( QAction * ) ), this, SLOT( slotTriggered ( QAction * ) ) );
115
 
  kmenu->clear();
116
 
 
117
 
  QStringList::ConstIterator it;
118
 
  int count = 0;
119
 
  QAction *act;
120
 
 
121
 
  for (it = encodingNames.begin(); it != encodingNames.end(); ++it) {
122
 
    act = kmenu->addAction( *it );
123
 
    act->setData( QVariant( ++count) );
124
 
    act->setCheckable( true );
125
 
  }
126
 
  kmenu->addSeparator();
127
 
 
128
 
  act = kmenu->addAction(i18n("Reload"));
129
 
  act->setCheckable( true );
130
 
  act->setData( QVariant( -1 ) );
131
 
 
132
 
  act = kmenu->addAction(i18n("Default"));
133
 
  act->setCheckable( true );
134
 
  act->setData( QVariant( -2 ) );
135
 
}
136
 
 
137
 
void KrRemoteEncodingMenu::slotTriggered( QAction * act )
138
 
{
139
 
  if( !act || !act->data().canConvert<int> () )
140
 
    return;
141
 
 
142
 
  int id = act->data().toInt();
143
 
 
144
 
  switch( id )
145
 
  {
146
 
  case -1:
147
 
    slotReload();
148
 
    return;
149
 
  case -2:
150
 
    slotDefault();
151
 
    return;
152
 
  default:
153
 
    {
154
 
      KUrl currentURL = ACTIVE_PANEL->virtualPath();
155
 
 
156
 
      KConfig config(("kio_" + currentURL.protocol() + "rc").toLatin1());
157
 
      QString host = currentURL.host();
158
 
 
159
 
      QString charset = KGlobal::charsets()->encodingForName( encodingNames[id - 1] );
160
 
 
161
 
      KConfigGroup group( &config, host);
162
 
      group.writeEntry(DATA_KEY, charset);
163
 
      config.sync();
164
 
 
165
 
      // Update the io-slaves...
166
 
      updateKIOSlaves();
167
 
    }
168
 
  }
 
99
    settingsLoaded = true;
 
100
    encodingNames = KGlobal::charsets()->descriptiveEncodingNames();
 
101
 
 
102
    KMenu *kmenu = menu();
 
103
    disconnect(kmenu, SIGNAL(triggered(QAction *)), this, SLOT(slotTriggered(QAction *)));
 
104
    connect(kmenu, SIGNAL(triggered(QAction *)), this, SLOT(slotTriggered(QAction *)));
 
105
    kmenu->clear();
 
106
 
 
107
    QStringList::ConstIterator it;
 
108
    int count = 0;
 
109
    QAction *act;
 
110
 
 
111
    for (it = encodingNames.constBegin(); it != encodingNames.constEnd(); ++it) {
 
112
        act = kmenu->addAction(*it);
 
113
        act->setData(QVariant(++count));
 
114
        act->setCheckable(true);
 
115
    }
 
116
    kmenu->addSeparator();
 
117
 
 
118
    act = kmenu->addAction(i18n("Reload"));
 
119
    act->setCheckable(true);
 
120
    act->setData(QVariant(-1));
 
121
 
 
122
    act = kmenu->addAction(i18n("Default"));
 
123
    act->setCheckable(true);
 
124
    act->setData(QVariant(-2));
 
125
}
 
126
 
 
127
void KrRemoteEncodingMenu::slotTriggered(QAction * act)
 
128
{
 
129
    if (!act || !act->data().canConvert<int> ())
 
130
        return;
 
131
 
 
132
    int id = act->data().toInt();
 
133
 
 
134
    switch (id) {
 
135
    case -1:
 
136
        slotReload();
 
137
        return;
 
138
    case -2:
 
139
        chooseDefault();
 
140
        return;
 
141
    default:
 
142
        chooseEncoding(encodingNames[id - 1]);
 
143
    }
 
144
}
 
145
 
 
146
void KrRemoteEncodingMenu::chooseEncoding(QString encoding)
 
147
{
 
148
    KUrl currentURL = ACTIVE_PANEL->virtualPath();
 
149
 
 
150
    KConfig config(("kio_" + currentURL.protocol() + "rc").toLatin1());
 
151
    QString host = currentURL.host();
 
152
 
 
153
    QString charset = KGlobal::charsets()->encodingForName(encoding);
 
154
 
 
155
    KConfigGroup group(&config, host);
 
156
    group.writeEntry(DATA_KEY, charset);
 
157
    config.sync();
 
158
 
 
159
    // Update the io-slaves...
 
160
    updateKIOSlaves();
169
161
}
170
162
 
171
163
void KrRemoteEncodingMenu::slotReload()
172
164
{
173
 
  loadSettings();
 
165
    loadSettings();
174
166
}
175
167
 
176
 
void KrRemoteEncodingMenu::slotDefault()
 
168
void KrRemoteEncodingMenu::chooseDefault()
177
169
{
178
 
  KUrl currentURL = ACTIVE_PANEL->virtualPath();
179
 
 
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() );
183
 
 
184
 
  QStringList partList = currentURL.host().split ( '.', QString::SkipEmptyParts );
185
 
  if ( !partList.isEmpty() )
186
 
  {
187
 
    partList.erase ( partList.begin() );
188
 
 
189
 
    QStringList domains;
190
 
    // Remove the exact name match...
191
 
    domains << currentURL.host();
192
 
 
193
 
    while ( partList.count() )
194
 
    {
195
 
      if ( partList.count() == 2 )
196
 
        if ( partList[0].length() <= 2 && partList[1].length() == 2 )
197
 
          break;
198
 
 
199
 
      if ( partList.count() == 1 )
200
 
        break;
201
 
 
202
 
      domains << partList.join ( "." );
203
 
      partList.erase ( partList.begin() );
204
 
    }
205
 
 
206
 
    for ( QStringList::Iterator it = domains.begin(); it != domains.end(); ++it )
207
 
    {
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
213
 
    }
214
 
  }
215
 
  config.sync();
216
 
 
217
 
  updateKIOSlaves();
 
170
    KUrl currentURL = ACTIVE_PANEL->virtualPath();
 
171
 
 
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());
 
175
 
 
176
    QStringList partList = currentURL.host().split('.', QString::SkipEmptyParts);
 
177
    if (!partList.isEmpty()) {
 
178
        partList.erase(partList.begin());
 
179
 
 
180
        QStringList domains;
 
181
        // Remove the exact name match...
 
182
        domains << currentURL.host();
 
183
 
 
184
        while (partList.count()) {
 
185
            if (partList.count() == 2)
 
186
                if (partList[0].length() <= 2 && partList[1].length() == 2)
 
187
                    break;
 
188
 
 
189
            if (partList.count() == 1)
 
190
                break;
 
191
 
 
192
            domains << partList.join(".");
 
193
            partList.erase(partList.begin());
 
194
        }
 
195
 
 
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
 
202
        }
 
203
    }
 
204
    config.sync();
 
205
 
 
206
    updateKIOSlaves();
218
207
}
219
208
 
220
209
 
221
210
void KrRemoteEncodingMenu::updateKIOSlaves()
222
211
{
223
 
  KIO::Scheduler::emitReparseSlaveConfiguration();
 
212
    KIO::Scheduler::emitReparseSlaveConfiguration();
224
213
 
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()));
227
216
}
228
217
 
229
218
#include "krremoteencodingmenu.moc"