┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/views/dolphinremoteencoding.cpp
blob: 8644f5cc2da9c5d72bb41d5509bef55ffa096c60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/***************************************************************************
 *   Copyright (C) 2009 by Rahman Duran <[email protected]>           *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
 ***************************************************************************/

 /*
 * This code is largely based on the kremoteencodingplugin
 * Copyright (c) 2003 Thiago Macieira <[email protected]>
 * Distributed under the same terms.
 */

#include "dolphinremoteencoding.h"
#include "dolphinviewactionhandler.h"

#include <KDebug>
#include <KActionMenu>
#include <KActionCollection>
#include <KIcon>
#include <KLocale>
#include <KGlobal>
#include <KMimeType>
#include <KConfig>
#include <KCharsets>
#include <KMenu>
#include <KProtocolInfo>
#include <KProtocolManager>
#include <KIO/SlaveConfig>
#include <KIO/Scheduler>
#include <KConfigGroup>

#define DATA_KEY        QLatin1String("Charset")

DolphinRemoteEncoding::DolphinRemoteEncoding(QObject* parent, DolphinViewActionHandler* actionHandler)
   :QObject(parent),
    m_actionHandler(actionHandler),
    m_loaded(false),
    m_idDefault(0)
{
    m_menu = new KActionMenu(KIcon("character-set"), i18n("Select Remote Charset"), this);
    m_actionHandler->actionCollection()->addAction("change_remote_encoding", m_menu);
    connect(m_menu->menu(), SIGNAL(aboutToShow()),
          this, SLOT(slotAboutToShow()));

    m_menu->setEnabled(false);
    m_menu->setDelayed(false);
}

DolphinRemoteEncoding::~DolphinRemoteEncoding()
{
}

void DolphinRemoteEncoding::slotReload()
{
    loadSettings();
}

void DolphinRemoteEncoding::loadSettings()
{
    m_loaded = true;
    m_encodingDescriptions = KGlobal::charsets()->descriptiveEncodingNames();

    fillMenu();
}

void DolphinRemoteEncoding::slotAboutToOpenUrl()
{
    KUrl oldURL = m_currentURL;
    m_currentURL = m_actionHandler->currentView()->url();

    if (m_currentURL.protocol() != oldURL.protocol()) {
        // This plugin works on ftp, fish, etc.
        // everything whose type is T_FILESYSTEM except for local files
        if (!m_currentURL.isLocalFile() &&
            KProtocolManager::outputType(m_currentURL) == KProtocolInfo::T_FILESYSTEM) {

            m_menu->setEnabled(true);
            loadSettings();
        } else {
            m_menu->setEnabled(false);
        }
        return;
    }

    if (m_currentURL.host() != oldURL.host()) {
        updateMenu();
    }
}

void DolphinRemoteEncoding::fillMenu()
{
    KMenu* menu = m_menu->menu();
    menu->clear();


    for (int i = 0; i < m_encodingDescriptions.size();i++) {
        QAction* action = new QAction(m_encodingDescriptions.at(i), this);
        action->setCheckable(true);
        action->setData(i);
        menu->addAction(action);
    }
    menu->addSeparator();

    menu->addAction(i18n("Reload"), this, SLOT(slotReload()), 0);
    menu->addAction(i18n("Default"), this, SLOT(slotDefault()), 0)->setCheckable(true);
    m_idDefault = m_encodingDescriptions.size() + 2;

    connect(menu, SIGNAL(triggered(QAction*)), this, SLOT(slotItemSelected(QAction*)));
}

void DolphinRemoteEncoding::updateMenu()
{
    if (!m_loaded) {
        loadSettings();
    }

    // uncheck everything
    for (int i =  0; i < m_menu->menu()->actions().count(); i++) {
        m_menu->menu()->actions().at(i)->setChecked(false);
    }

    QString charset = KIO::SlaveConfig::self()->configData(m_currentURL.protocol(),
                                                           m_currentURL.host(), DATA_KEY);

    if (!charset.isEmpty()) {
        int id = 0;
        bool isFound = false;
        for (int i = 0; i < m_encodingDescriptions.size(); i++) {
            if (m_encodingDescriptions.at(i).contains(charset)) {
                isFound = true;
                id = i;
                break;
            }
        }

        kDebug() << "URL=" << m_currentURL << " charset=" << charset;

        if (!isFound) {
            kWarning() << "could not find entry for charset=" << charset ;
        } else {
            m_menu->menu()->actions().at(id)->setChecked(true);
        }
    } else {
        m_menu->menu()->actions().at(m_idDefault)->setChecked(true);
    }

}

void DolphinRemoteEncoding::slotAboutToShow()
{
    if (!m_loaded) {
        loadSettings();
    }
    updateMenu();
}

void DolphinRemoteEncoding::slotItemSelected(QAction* action)
{
    if (action) {
        int id = action->data().toInt();

        KConfig config(("kio_" + m_currentURL.protocol() + "rc").toLatin1());
        QString host = m_currentURL.host();
        if (m_menu->menu()->actions().at(id)->isChecked()) {
            QString charset = KGlobal::charsets()->encodingForName(m_encodingDescriptions.at(id));
            KConfigGroup cg(&config, host);
            cg.writeEntry(DATA_KEY, charset);
            config.sync();

            // Update the io-slaves...
            updateView();
        }
    }
}

void DolphinRemoteEncoding::slotDefault()
{
    // We have no choice but delete all higher domain level
    // settings here since it affects what will be matched.
    KConfig config(("kio_" + m_currentURL.protocol() + "rc").toLatin1());

    QStringList partList = m_currentURL.host().split('.', QString::SkipEmptyParts);
    if (!partList.isEmpty()) {
        partList.erase(partList.begin());

        QStringList domains;
        // Remove the exact name match...
        domains << m_currentURL.host();

        while (!partList.isEmpty()) {
            if (partList.count() == 2) {
                if (partList[0].length() <= 2 && partList[1].length() == 2) {
                    break;
                }
            }

            if (partList.count() == 1) {
                break;
            }

            domains << partList.join(".");
            partList.erase(partList.begin());
        }

        for (QStringList::const_iterator it = domains.constBegin(); it != domains.constEnd();++it) {
            kDebug() << "Domain to remove: " << *it;
            if (config.hasGroup(*it)) {
                config.deleteGroup(*it);
            } else if (config.group("").hasKey(*it)) {
                config.group("").deleteEntry(*it); //don't know what group name is supposed to be XXX
            }
        }
    }
    config.sync();

    // Update the io-slaves.
    updateView();
}

void DolphinRemoteEncoding::updateView()
{
    KIO::Scheduler::emitReparseSlaveConfiguration();
    // Reload the page with the new charset
    m_actionHandler->currentView()->setUrl(m_currentURL);
    m_actionHandler->currentView()->reload();
}

#include "dolphinremoteencoding.moc"