1
/* This file is part of the KDE project
2
Copyright (C) 2005 Jaroslaw Staniek <js@iidea.pl>
4
This program is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License as published by the Free Software Foundation; either
7
version 2 of the License, or (at your option) any later version.
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 GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this program; see the file COPYING. If not, write to
16
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
* Boston, MA 02110-1301, USA.
20
#include "kexicharencodingcombobox.h"
22
#include <qtextcodec.h>
27
#include <kcharsets.h>
29
KexiCharacterEncodingComboBox::KexiCharacterEncodingComboBox(
30
QWidget* parent, const QString& selectedEncoding )
31
: KComboBox( parent, "KexiCharacterEncodingComboBox" )
32
, m_defaultEncodingAdded(false)
34
QString defaultEncoding(QString::fromLatin1(KGlobal::locale()->encoding()));
35
QString defaultEncodingDescriptiveName;
37
QString _selectedEncoding = selectedEncoding;
38
if (_selectedEncoding.isEmpty())
39
_selectedEncoding = QString::fromLatin1(KGlobal::locale()->encoding());
41
QStringList descEncodings(KGlobal::charsets()->descriptiveEncodingNames());
42
QStringList::ConstIterator it = descEncodings.constBegin();
44
for (uint id = 0; it!=descEncodings.constEnd(); ++it)
47
QString name( KGlobal::charsets()->encodingForName( *it ) );
48
QTextCodec *codecForEnc = KGlobal::charsets()->codecForName(name, found);
51
if (codecForEnc->name() == defaultEncoding || name == defaultEncoding) {
52
defaultEncodingDescriptiveName = *it;
53
//remember, do not add, will be prepended later
56
m_encodingDescriptionForName.insert(name, *it);
58
if (codecForEnc->name() == _selectedEncoding || name == _selectedEncoding) {
65
//prepend default encoding, if present
66
if (!defaultEncodingDescriptiveName.isEmpty()) {
67
m_defaultEncodingAdded = true;
68
QString desc = i18n("Text encoding: Default", "Default: %1")
69
.arg(defaultEncodingDescriptiveName);
70
insertItem( desc, 0 );
71
if (_selectedEncoding==defaultEncoding) {
75
setCurrentItem(currentItem()+1);
76
m_encodingDescriptionForName.insert(defaultEncoding, desc);
80
KexiCharacterEncodingComboBox::~KexiCharacterEncodingComboBox()
84
QString KexiCharacterEncodingComboBox::selectedEncoding() const
86
if (defaultEncodingSelected()) {
87
return QString::fromLatin1(KGlobal::locale()->encoding());
90
return KGlobal::charsets()->encodingForName( currentText() );
94
void KexiCharacterEncodingComboBox::setSelectedEncoding(const QString& encodingName)
96
QString desc = m_encodingDescriptionForName[encodingName];
98
kdWarning() << "KexiCharacterEncodingComboBox::setSelectedEncoding(): "
99
"no such encoding \"" << encodingName << "\"" << endl;
102
setCurrentText(desc);
105
bool KexiCharacterEncodingComboBox::defaultEncodingSelected() const
107
return m_defaultEncodingAdded && 0==currentItem();
110
void KexiCharacterEncodingComboBox::selectDefaultEncoding()
112
if (m_defaultEncodingAdded)