qlocale_mac: make sure that helper functions return QVariant

QLocale treats a null QVariant returned from the QSystemLocale::query()
as a signal to fall back to CLDR implementation.
In Qt 5 QVariant(QString()).isNull() was returning true, so we could
easily return an empty QString() to fall back to CLDR.
In Qt 6 the QVariant() behavior has changed.
This patch makes sure that all the helper methods in macOS system locale
implementation return a null QVariant() when they fail to provide any
reasonable value.

Task-number: QTBUG-84877
Pick-to: 6.2
Change-Id: I85be3b1463b1366f737e912c99bc11e37af98c62
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
This commit is contained in:
Ivan Solovev 2021-09-03 12:35:11 +02:00
parent cc22857d1e
commit 797039eb20

View File

@ -61,11 +61,11 @@ static QString getMacLocaleName()
return QString::fromCFString(locale); return QString::fromCFString(locale);
} }
static QString macMonthName(int month, QSystemLocale::QueryType type) static QVariant macMonthName(int month, QSystemLocale::QueryType type)
{ {
month -= 1; month -= 1;
if (month < 0 || month > 11) if (month < 0 || month > 11)
return QString(); return {};
QCFType<CFDateFormatterRef> formatter QCFType<CFDateFormatterRef> formatter
= CFDateFormatterCreate(0, QCFType<CFLocaleRef>(CFLocaleCopyCurrent()), = CFDateFormatterCreate(0, QCFType<CFLocaleRef>(CFLocaleCopyCurrent()),
@ -93,7 +93,7 @@ static QString macMonthName(int month, QSystemLocale::QueryType type)
break; break;
default: default:
qWarning("macMonthName: Unsupported query type %d", type); qWarning("macMonthName: Unsupported query type %d", type);
return QString(); return {};
} }
QCFType<CFArrayRef> values QCFType<CFArrayRef> values
= static_cast<CFArrayRef>(CFDateFormatterCopyProperty(formatter, formatterType)); = static_cast<CFArrayRef>(CFDateFormatterCopyProperty(formatter, formatterType));
@ -102,13 +102,13 @@ static QString macMonthName(int month, QSystemLocale::QueryType type)
CFStringRef cfstring = static_cast<CFStringRef>(CFArrayGetValueAtIndex(values, month)); CFStringRef cfstring = static_cast<CFStringRef>(CFArrayGetValueAtIndex(values, month));
return QString::fromCFString(cfstring); return QString::fromCFString(cfstring);
} }
return QString(); return {};
} }
static QString macDayName(int day, QSystemLocale::QueryType type) static QVariant macDayName(int day, QSystemLocale::QueryType type)
{ {
if (day < 1 || day > 7) if (day < 1 || day > 7)
return QString(); return {};
QCFType<CFDateFormatterRef> formatter QCFType<CFDateFormatterRef> formatter
= CFDateFormatterCreate(0, QCFType<CFLocaleRef>(CFLocaleCopyCurrent()), = CFDateFormatterCreate(0, QCFType<CFLocaleRef>(CFLocaleCopyCurrent()),
@ -136,7 +136,7 @@ static QString macDayName(int day, QSystemLocale::QueryType type)
break; break;
default: default:
qWarning("macDayName: Unsupported query type %d", type); qWarning("macDayName: Unsupported query type %d", type);
return QString(); return {};
} }
QCFType<CFArrayRef> values = QCFType<CFArrayRef> values =
static_cast<CFArrayRef>(CFDateFormatterCopyProperty(formatter, formatterType)); static_cast<CFArrayRef>(CFDateFormatterCopyProperty(formatter, formatterType));
@ -145,10 +145,10 @@ static QString macDayName(int day, QSystemLocale::QueryType type)
CFStringRef cfstring = static_cast<CFStringRef>(CFArrayGetValueAtIndex(values, day % 7)); CFStringRef cfstring = static_cast<CFStringRef>(CFArrayGetValueAtIndex(values, day % 7));
return QString::fromCFString(cfstring); return QString::fromCFString(cfstring);
} }
return QString(); return {};
} }
static QString macDateToString(QDate date, bool short_format) static QVariant macDateToString(QDate date, bool short_format)
{ {
QCFType<CFDateRef> myDate = QDateTime(date, QTime()).toCFDate(); QCFType<CFDateRef> myDate = QDateTime(date, QTime()).toCFDate();
QCFType<CFLocaleRef> mylocale = CFLocaleCopyCurrent(); QCFType<CFLocaleRef> mylocale = CFLocaleCopyCurrent();
@ -157,10 +157,10 @@ static QString macDateToString(QDate date, bool short_format)
= CFDateFormatterCreate(kCFAllocatorDefault, = CFDateFormatterCreate(kCFAllocatorDefault,
mylocale, style, mylocale, style,
kCFDateFormatterNoStyle); kCFDateFormatterNoStyle);
return QCFString(CFDateFormatterCreateStringWithDate(0, myFormatter, myDate)); return QString::fromCFString(CFDateFormatterCreateStringWithDate(0, myFormatter, myDate));
} }
static QString macTimeToString(QTime time, bool short_format) static QVariant macTimeToString(QTime time, bool short_format)
{ {
QCFType<CFDateRef> myDate = QDateTime(QDate::currentDate(), time).toCFDate(); QCFType<CFDateRef> myDate = QDateTime(QDate::currentDate(), time).toCFDate();
QCFType<CFLocaleRef> mylocale = CFLocaleCopyCurrent(); QCFType<CFLocaleRef> mylocale = CFLocaleCopyCurrent();
@ -169,7 +169,7 @@ static QString macTimeToString(QTime time, bool short_format)
mylocale, mylocale,
kCFDateFormatterNoStyle, kCFDateFormatterNoStyle,
style); style);
return QCFString(CFDateFormatterCreateStringWithDate(0, myFormatter, myDate)); return QString::fromCFString(CFDateFormatterCreateStringWithDate(0, myFormatter, myDate));
} }
// Mac uses the Unicode CLDR format codes // Mac uses the Unicode CLDR format codes
@ -177,7 +177,7 @@ static QString macTimeToString(QTime time, bool short_format)
// See also qtbase/util/locale_database/dateconverter.py // See also qtbase/util/locale_database/dateconverter.py
// Makes the assumption that input formats are always well formed and consecutive letters // Makes the assumption that input formats are always well formed and consecutive letters
// never exceed the maximum for the format code. // never exceed the maximum for the format code.
static QString macToQtFormat(QStringView sys_fmt) static QVariant macToQtFormat(QStringView sys_fmt)
{ {
QString result; QString result;
int i = 0; int i = 0;
@ -293,10 +293,10 @@ static QString macToQtFormat(QStringView sys_fmt)
i += repeat; i += repeat;
} }
return result; return !result.isEmpty() ? QVariant::fromValue(result) : QVariant();
} }
QString getMacDateFormat(CFDateFormatterStyle style) static QVariant getMacDateFormat(CFDateFormatterStyle style)
{ {
QCFType<CFLocaleRef> l = CFLocaleCopyCurrent(); QCFType<CFLocaleRef> l = CFLocaleCopyCurrent();
QCFType<CFDateFormatterRef> formatter = CFDateFormatterCreate(kCFAllocatorDefault, QCFType<CFDateFormatterRef> formatter = CFDateFormatterCreate(kCFAllocatorDefault,
@ -304,7 +304,7 @@ QString getMacDateFormat(CFDateFormatterStyle style)
return macToQtFormat(QString::fromCFString(CFDateFormatterGetFormat(formatter))); return macToQtFormat(QString::fromCFString(CFDateFormatterGetFormat(formatter)));
} }
static QString getMacTimeFormat(CFDateFormatterStyle style) static QVariant getMacTimeFormat(CFDateFormatterStyle style)
{ {
QCFType<CFLocaleRef> l = CFLocaleCopyCurrent(); QCFType<CFLocaleRef> l = CFLocaleCopyCurrent();
QCFType<CFDateFormatterRef> formatter = CFDateFormatterCreate(kCFAllocatorDefault, QCFType<CFDateFormatterRef> formatter = CFDateFormatterCreate(kCFAllocatorDefault,
@ -321,7 +321,7 @@ static QVariant getCFLocaleValue(CFStringRef key)
return QString::fromCFString(CFStringRef(static_cast<CFTypeRef>(value))); return QString::fromCFString(CFStringRef(static_cast<CFTypeRef>(value)));
} }
static QLocale::MeasurementSystem macMeasurementSystem() static QVariant macMeasurementSystem()
{ {
QCFType<CFLocaleRef> locale = CFLocaleCopyCurrent(); QCFType<CFLocaleRef> locale = CFLocaleCopyCurrent();
CFStringRef system = static_cast<CFStringRef>(CFLocaleGetValue(locale, kCFLocaleMeasurementSystem)); CFStringRef system = static_cast<CFStringRef>(CFLocaleGetValue(locale, kCFLocaleMeasurementSystem));
@ -342,7 +342,7 @@ static quint8 macFirstDayOfWeek()
return day; return day;
} }
static QString macCurrencySymbol(QLocale::CurrencySymbolFormat format) static QVariant macCurrencySymbol(QLocale::CurrencySymbolFormat format)
{ {
QCFType<CFLocaleRef> locale = CFLocaleCopyCurrent(); QCFType<CFLocaleRef> locale = CFLocaleCopyCurrent();
switch (format) { switch (format) {
@ -358,10 +358,10 @@ static QString macCurrencySymbol(QLocale::CurrencySymbolFormat format)
default: default:
break; break;
} }
return QString(); return {};
} }
static QString macZeroDigit() static QVariant macZeroDigit()
{ {
QCFType<CFLocaleRef> locale = CFLocaleCopyCurrent(); QCFType<CFLocaleRef> locale = CFLocaleCopyCurrent();
QCFType<CFNumberFormatterRef> numberFormatter = QCFType<CFNumberFormatterRef> numberFormatter =
@ -373,7 +373,7 @@ static QString macZeroDigit()
} }
#ifndef QT_NO_SYSTEMLOCALE #ifndef QT_NO_SYSTEMLOCALE
static QString macFormatCurrency(const QSystemLocale::CurrencyToStringArgument &arg) static QVariant macFormatCurrency(const QSystemLocale::CurrencyToStringArgument &arg)
{ {
QCFType<CFNumberRef> value; QCFType<CFNumberRef> value;
switch (arg.value.metaType().id()) { switch (arg.value.metaType().id()) {
@ -395,7 +395,7 @@ static QString macFormatCurrency(const QSystemLocale::CurrencyToStringArgument &
break; break;
} }
default: default:
return QString(); return {};
} }
QCFType<CFLocaleRef> locale = CFLocaleCopyCurrent(); QCFType<CFLocaleRef> locale = CFLocaleCopyCurrent();
@ -500,10 +500,10 @@ QVariant QSystemLocale::query(QueryType type, QVariant in) const
case PositiveSign: case PositiveSign:
break; break;
case ZeroDigit: case ZeroDigit:
return QVariant(macZeroDigit()); return macZeroDigit();
case MeasurementSystem: case MeasurementSystem:
return QVariant(static_cast<int>(macMeasurementSystem())); return macMeasurementSystem();
case AMText: case AMText:
case PMText: { case PMText: {
@ -516,7 +516,7 @@ QVariant QSystemLocale::query(QueryType type, QVariant in) const
case FirstDayOfWeek: case FirstDayOfWeek:
return QVariant(macFirstDayOfWeek()); return QVariant(macFirstDayOfWeek());
case CurrencySymbol: case CurrencySymbol:
return QVariant(macCurrencySymbol(QLocale::CurrencySymbolFormat(in.toUInt()))); return macCurrencySymbol(QLocale::CurrencySymbolFormat(in.toUInt()));
case CurrencyToString: case CurrencyToString:
return macFormatCurrency(in.value<QSystemLocale::CurrencyToStringArgument>()); return macFormatCurrency(in.value<QSystemLocale::CurrencyToStringArgument>());
case UILanguages: { case UILanguages: {