File indexing completed on 2024-05-12 16:40:52
0001 /* This file is part of the KDE project 0002 Copyright (C) 2005-2012 Jarosław Staniek <staniek@kde.org> 0003 0004 This program is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License as published by the Free Software Foundation; either 0007 version 2 of the License, or (at your option) any later version. 0008 0009 This program is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this program; see the file COPYING. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "kexicharencodingcombobox.h" 0021 #include <kexiutils/utils.h> 0022 0023 #include <KCharsets> 0024 #include <KLocalizedString> 0025 0026 #include <QTextCodec> 0027 #include <QLocale> 0028 #include <QDebug> 0029 0030 class Q_DECL_HIDDEN KexiCharacterEncodingComboBox::Private { 0031 public: 0032 Private(); 0033 0034 QHash<QString, QString> encodingDescriptionForName; 0035 bool defaultEncodingAdded; 0036 }; 0037 0038 KexiCharacterEncodingComboBox::KexiCharacterEncodingComboBox( 0039 QWidget* parent, const QString& selectedEncoding) 0040 : KComboBox(parent), d(new Private) 0041 { 0042 QString defaultEncoding(QString::fromLatin1(KexiUtils::encoding())); 0043 QString defaultEncodingDescriptiveName; 0044 0045 QString _selectedEncoding = selectedEncoding; 0046 if (_selectedEncoding.isEmpty()) 0047 _selectedEncoding = defaultEncoding; 0048 0049 QStringList descEncodings(KCharsets::charsets()->descriptiveEncodingNames()); 0050 0051 int id = 0; 0052 foreach(const QString &descEncoding, descEncodings) { 0053 bool found = false; 0054 QString name(KCharsets::charsets()->encodingForName(descEncoding)); 0055 QTextCodec *codecForEnc = KCharsets::charsets()->codecForName(name, found); 0056 if (found) { 0057 addItem(descEncoding); 0058 if (codecForEnc->name() == defaultEncoding || name == defaultEncoding) { 0059 defaultEncodingDescriptiveName = descEncoding; 0060 //remember, do not add, will be prepended later 0061 } else { 0062 d->encodingDescriptionForName.insert(name, descEncoding); 0063 } 0064 if (codecForEnc->name() == _selectedEncoding || name == _selectedEncoding) { 0065 setCurrentIndex(id); 0066 } 0067 id++; 0068 } 0069 } 0070 0071 //prepend default encoding, if present 0072 if (!defaultEncodingDescriptiveName.isEmpty()) { 0073 d->defaultEncodingAdded = true; 0074 QString desc = xi18nc("Text encoding: Default", "Default: %1", 0075 defaultEncodingDescriptiveName); 0076 insertItem(0, desc); 0077 if (_selectedEncoding == defaultEncoding) { 0078 setCurrentIndex(0); 0079 } 0080 d->encodingDescriptionForName.insert(defaultEncoding, desc); 0081 } 0082 } 0083 0084 KexiCharacterEncodingComboBox::~KexiCharacterEncodingComboBox() 0085 { 0086 delete d; 0087 } 0088 0089 QString KexiCharacterEncodingComboBox::selectedEncoding() const 0090 { 0091 if (defaultEncodingSelected()) { 0092 return QString::fromLatin1(KexiUtils::encoding()); 0093 } else { 0094 return KCharsets::charsets()->encodingForName(currentText()); 0095 } 0096 } 0097 0098 void KexiCharacterEncodingComboBox::setSelectedEncoding(const QString& encodingName) 0099 { 0100 QString desc = d->encodingDescriptionForName[encodingName]; 0101 if (desc.isEmpty()) { 0102 qWarning() << "no such encoding" << encodingName; 0103 return; 0104 } 0105 setCurrentIndex(findText(desc)); 0106 } 0107 0108 bool KexiCharacterEncodingComboBox::defaultEncodingSelected() const 0109 { 0110 return d->defaultEncodingAdded && 0 == currentIndex(); 0111 } 0112 0113 void KexiCharacterEncodingComboBox::selectDefaultEncoding() 0114 { 0115 if (d->defaultEncodingAdded) 0116 setCurrentIndex(0); 0117 } 0118 0119 KexiCharacterEncodingComboBox::Private::Private() 0120 : defaultEncodingAdded(false) 0121 { 0122 }