File indexing completed on 2024-04-21 14:55:37

0001 /*  This file is part of the KDE libraries
0002  *  Copyright 2005, 2008 Jaroslaw Staniek <staniek@kde.org>
0003  *  Copyright 2010 John Layt <john@layt.net>
0004  *
0005  *  This library is free software; you can redistribute it and/or
0006  *  modify it under the terms of the GNU Library General Public
0007  *  License as published by the Free Software Foundation; either
0008  *  version 2 of the License, or (at your option) any later version.
0009  *
0010  *  This library is distributed in the hope that it will be useful,
0011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  *  Library General Public License for more details.
0014  *
0015  *  You should have received a copy of the GNU Library General Public License
0016  *  along with this library; see the file COPYING.LIB.  If not, write to
0017  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  *  Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #include "klocale_win_p.h"
0022 
0023 #include <QLocale>
0024 #include <QTextCodec>
0025 
0026 KLocaleWindowsPrivate::KLocaleWindowsPrivate(KLocale *q_ptr, KSharedConfig::Ptr config)
0027     : KLocalePrivate(q_ptr)
0028 {
0029     // Lock in the current Windows Locale ID
0030     // Can we also lock in the actual settings like we do for Mac?
0031     m_winLocaleId = GetUserDefaultLCID();
0032     init(QString(), QString(), config, 0);
0033 }
0034 
0035 KLocaleWindowsPrivate::KLocaleWindowsPrivate(KLocale *q_ptr,
0036         const QString &language, const QString &country, KConfig *config)
0037     : KLocalePrivate(q_ptr)
0038 {
0039     // Lock in the current Windows Locale ID
0040     // Can we also lock in the actual settings like we do for Mac?
0041     m_winLocaleId = GetUserDefaultLCID();
0042     init(language, country, KSharedConfig::Ptr(), config);
0043 }
0044 
0045 KLocaleWindowsPrivate::KLocaleWindowsPrivate(const KLocaleWindowsPrivate &rhs)
0046     : KLocalePrivate(rhs)
0047 {
0048     KLocalePrivate::copy(rhs);
0049     m_winLocaleId = rhs.m_winLocaleId;
0050     strcpy(m_win32SystemEncoding, rhs.m_win32SystemEncoding);
0051 }
0052 
0053 KLocaleWindowsPrivate &KLocaleWindowsPrivate::operator=(const KLocaleWindowsPrivate &rhs)
0054 {
0055     KLocalePrivate::copy(rhs);
0056     m_winLocaleId = rhs.m_winLocaleId;
0057     strcpy(m_win32SystemEncoding, rhs.m_win32SystemEncoding);
0058     return *this;
0059 }
0060 
0061 KLocaleWindowsPrivate::~KLocaleWindowsPrivate()
0062 {
0063 }
0064 
0065 QString KLocaleWindowsPrivate::windowsLocaleValue(LCTYPE key) const
0066 {
0067     // Find out how big the buffer needs to be
0068     int size = GetLocaleInfoW(m_winLocaleId, key, 0, 0);
0069 
0070     QString result;
0071     if (size) {
0072         wchar_t *buffer = new wchar_t[size];
0073         if (GetLocaleInfoW(m_winLocaleId, key, buffer, size)) {
0074             result = QString::fromWCharArray(buffer);
0075         }
0076         delete[] buffer;
0077     }
0078     return result;
0079 }
0080 
0081 QString KLocaleWindowsPrivate::systemCountry() const
0082 {
0083     return windowsLocaleValue(LOCALE_SISO3166CTRYNAME);
0084 }
0085 
0086 QStringList KLocaleWindowsPrivate::systemLanguageList() const
0087 {
0088     QStringList list;
0089     getLanguagesFromVariable(list, QLocale::system().name().toLocal8Bit().data());
0090     return list;
0091 }
0092 
0093 QByteArray KLocaleWindowsPrivate::systemCodeset() const
0094 {
0095     return QByteArray();
0096 }
0097 
0098 const QByteArray KLocaleWindowsPrivate::encoding()
0099 {
0100     if (qstrcmp(codecForEncoding()->name(), "System") == 0) {
0101         //win32 returns "System" codec name here but KDE apps expect a real name:
0102         strcpy(m_win32SystemEncoding, "cp ");
0103         // MSDN says the returned string for LOCALE_IDEFAULTANSICODEPAGE is max 6 char including '\0'
0104         char buffer[6];
0105         if (GetLocaleInfoA(MAKELCID(MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), SORT_DEFAULT),
0106                            LOCALE_IDEFAULTANSICODEPAGE, buffer, sizeof(buffer))) {
0107             strcpy(m_win32SystemEncoding + 3, buffer);
0108             return m_win32SystemEncoding;
0109         }
0110     }
0111     return KLocalePrivate::encoding();
0112 }
0113 
0114 /*
0115 These functions are commented out for now until all required Date/Time functions are implemented
0116 to ensure consistent behaviour, i.e. all KDE format or all Windows format, not some invalid mixture
0117 
0118 void KLocaleMacPrivate::initDayPeriods(const KConfigGroup &cg)
0119 {
0120     QString amText = windowsLocaleValue(LOCALE_S1159);
0121     QString pmText = windowsLocaleValue(LOCALE_S2359);
0122 
0123     m_dayPeriods.clear();
0124     m_dayPeriods.append(KDayPeriod("am", amText, amText, amText
0125                                    QTime(0, 0, 0), QTime(11, 59, 59, 999), 0, 12));
0126     m_dayPeriods.append(KDayPeriod("pm", pmText, pmText, pmText,
0127                                    QTime(12, 0, 0), QTime(23, 59, 59, 999), 0, 12));
0128 }
0129 
0130 */