File indexing completed on 2024-05-19 04:55:55

0001 /**
0002  * \file generalconfig.cpp
0003  * General configuration.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 17 Sep 2003
0008  *
0009  * Copyright (C) 2003-2024  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #include "generalconfig.h"
0028 #include <QStringList>
0029 
0030 namespace {
0031 
0032 /** Index of latin-1 entry in getTextCodecNames(). */
0033 #if QT_VERSION >= 0x060000
0034 enum { TextEncodingLatin1Index = 7 };
0035 #else
0036 enum { TextEncodingLatin1Index = 13 };
0037 #endif
0038 
0039 }
0040 
0041 /**
0042  * Constructor.
0043  * Set default configuration.
0044  *
0045  * @param grp configuration group
0046  */
0047 GeneralConfig::GeneralConfig(const QString& grp) : m_group(grp) {}
0048 
0049 /**
0050  * Convert list of integers to list of strings.
0051  * @param intList list of integers
0052  * @return list of strings.
0053  */
0054 QStringList GeneralConfig::intListToStringList(const QList<int>& intList)
0055 {
0056   QStringList result;
0057   result.reserve(intList.size());
0058   for (int value : intList) {
0059     result.append(QString::number(value));
0060   }
0061   return result;
0062 }
0063 
0064 /**
0065  * Convert list of strings to list of integers.
0066  * @param strList list of strings
0067  * @return list of integers.
0068  */
0069 QList<int> GeneralConfig::stringListToIntList(const QStringList& strList)
0070 {
0071   QList<int> result;
0072   result.reserve(strList.size());
0073   for (const QString& value : strList) {
0074     result.append(value.toInt());
0075   }
0076   return result;
0077 }
0078 
0079 /**
0080  * String list of available text codecs.
0081  *
0082  * @return list of codec names.
0083  */
0084 QStringList GeneralConfig::getTextCodecNames()
0085 {
0086   static QStringList textEncodingList;
0087   if (textEncodingList.isEmpty()) {
0088 #if QT_VERSION >= 0x060000
0089     static const char* const codecs[] = {
0090       "UTF-8",
0091       "UTF-16",
0092       "UTF-16LE",
0093       "UTF-16BE",
0094       "UTF-32",
0095       "UTF-32LE",
0096       "UTF-32BE",
0097       "ISO-8859-1",
0098       "Locale",
0099       nullptr
0100     };
0101     Q_ASSERT(qstrcmp(codecs[TextEncodingLatin1Index], "ISO-8859-1") == 0);
0102 #else
0103     static const char* const codecs[] = {
0104       "Apple Roman (macintosh)",
0105       "Big5",
0106       "big5-0",
0107       "Big5-HKSCS",
0108       "big5hkscs-0",
0109       "EUC-JP",
0110       "EUC-KR",
0111       "GB18030",
0112       "GBK (windows-936)",
0113       "hp-roman8",
0114       "IBM850",
0115       "IBM866",
0116       "ISO-2022-JP (JIS7)",
0117       "ISO-8859-1 (latin1)",
0118       "ISO-8859-2 (latin2)",
0119       "ISO-8859-3 (latin3)",
0120       "ISO-8859-4 (latin4)",
0121       "ISO-8859-5 (cyrillic)",
0122       "ISO-8859-6 (arabic)",
0123       "ISO-8859-7 (greek)",
0124       "ISO-8859-8 (hebrew)",
0125       "ISO-8859-9 (latin5)",
0126       "ISO-8859-10 (latin6)",
0127       "ISO-8859-13 (baltic)",
0128       "ISO-8859-14 (latin8, iso-celtic)",
0129       "ISO-8859-15 (latin9)",
0130       "ISO-8859-16 (latin10)",
0131       "ISO-10646-UCS-2 (UTF-16)",
0132       "Iscii-Bng",
0133       "Iscii-Dev",
0134       "Iscii-Gjr",
0135       "Iscii-Knd",
0136       "Iscii-Mlm",
0137       "Iscii-Ori",
0138       "Iscii-Pnj",
0139       "Iscii-Tlg",
0140       "Iscii-Tml",
0141       "jisx0201*-0",
0142       "KOI8-R",
0143       "KOI8-U",
0144       "ksc5601.1987-0",
0145       "mulelao-1",
0146       "Shift_JIS (SJIS, MS_Kanji)",
0147       "System",
0148       "TIS-620 (ISO 8859-11)",
0149       "TSCII",
0150       "UTF-8",
0151       "windows-1250",
0152       "windows-1251",
0153       "windows-1252",
0154       "windows-1253",
0155       "windows-1254",
0156       "windows-1255",
0157       "windows-1256",
0158       "windows-1257",
0159       "windows-1258",
0160       "WINSAMI2 (WS2)",
0161       nullptr
0162     };
0163     Q_ASSERT(qstrcmp(codecs[TextEncodingLatin1Index], "ISO-8859-1 (latin1)") == 0);
0164 #endif
0165     const char* const* str = codecs;
0166     while (*str) {
0167       textEncodingList += QString::fromLatin1(*str++);
0168     }
0169   }
0170   return textEncodingList;
0171 }
0172 
0173 /**
0174  * Remove aliases in braces from text encoding name.
0175  *
0176  * @param comboEntry text encoding name
0177  *
0178  * @return codec name.
0179  */
0180 QString GeneralConfig::getTextCodecName(const QString& comboEntry)
0181 {
0182   int braceIdx = comboEntry.indexOf(QLatin1String(" ("));
0183   return braceIdx == -1 ? comboEntry : comboEntry.left(braceIdx);
0184 }
0185 
0186 /**
0187  * Get index of text encoding in getTextCodecNames().
0188  * @param textEncoding text encoding name
0189  * @return index of encoding.
0190  */
0191 int GeneralConfig::indexFromTextCodecName(const QString& textEncoding)
0192 {
0193   int index = 0;
0194   QStringList textEncodingList = getTextCodecNames();
0195   for (auto it = textEncodingList.constBegin();
0196        it != textEncodingList.constEnd();
0197        ++it) {
0198     if (getTextCodecName(*it) == textEncoding) {
0199       return index;
0200     }
0201     ++index;
0202   }
0203   return TextEncodingLatin1Index;
0204 }
0205 
0206 /**
0207  * Get text encoding name from index in getTextCodecNames().
0208  * @param index index of encoding
0209  * @return text encoding name, null if index invalid.
0210  */
0211 QString GeneralConfig::indexToTextCodecName(int index)
0212 {
0213   QStringList textEncodingList = getTextCodecNames();
0214   return index >= 0 && index < textEncodingList.size()
0215       ? getTextCodecName(textEncodingList.at(index))
0216       : QString();
0217 }