File indexing completed on 2024-05-12 16:59:38
0001 /* 0002 SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "indiancalendar.h" 0008 0009 class IndianCalendarProviderPrivate : public ICUCalendarPrivate 0010 { 0011 public: 0012 explicit IndianCalendarProviderPrivate(); 0013 0014 /** 0015 * For formatting, see the documentation of SimpleDateFormat: 0016 * https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/classicu_1_1SimpleDateFormat.html#details 0017 */ 0018 QString formattedDateStringInNativeLanguage(const icu::UnicodeString &str) const; 0019 0020 QCalendar::YearMonthDay fromGregorian(const QDate &_date); 0021 CalendarEvents::CalendarEventsPlugin::SubLabel subLabels(const QDate &date); 0022 0023 private: 0024 // Translated month names are available in https://github.com/unicode-org/icu/tree/main/icu4c/source/data/locales 0025 icu::Locale m_nativeLocale; 0026 }; 0027 0028 IndianCalendarProviderPrivate::IndianCalendarProviderPrivate() 0029 : ICUCalendarPrivate() 0030 , m_nativeLocale(icu::Locale(QLocale::system().name().toLatin1(), 0, 0, "calendar=indian;")) 0031 { 0032 if (U_FAILURE(m_errorCode)) { 0033 return; // Failed to create m_GregorianCalendar 0034 } 0035 0036 m_calendar.reset(icu::Calendar::createInstance("en_US@calendar=indian", m_errorCode)); 0037 } 0038 0039 QString IndianCalendarProviderPrivate::formattedDateStringInNativeLanguage(const icu::UnicodeString &str) const 0040 { 0041 UErrorCode errorCode = U_ZERO_ERROR; 0042 icu::UnicodeString dateString; 0043 icu::SimpleDateFormat formatter(str, m_nativeLocale, errorCode); 0044 formatter.setCalendar(*m_calendar); 0045 formatter.format(m_calendar->getTime(errorCode), dateString); 0046 0047 std::string utf8Str; 0048 dateString.toUTF8String<std::string>(utf8Str); 0049 0050 return QString::fromStdString(utf8Str); 0051 } 0052 0053 QCalendar::YearMonthDay IndianCalendarProviderPrivate::fromGregorian(const QDate &_date) 0054 { 0055 if (U_FAILURE(m_errorCode) || !_date.isValid() || !setDate(_date)) { 0056 return {}; 0057 } 0058 0059 return date(); 0060 } 0061 0062 CalendarEvents::CalendarEventsPlugin::SubLabel IndianCalendarProviderPrivate::subLabels(const QDate &date) 0063 { 0064 auto sublabel = CalendarEvents::CalendarEventsPlugin::SubLabel{}; 0065 0066 if (U_FAILURE(m_errorCode) || !date.isValid() || !setDate(date)) { 0067 return sublabel; 0068 } 0069 0070 sublabel.dayLabel = QString::number(day()); 0071 sublabel.label = i18ndc("plasma_calendar_alternatecalendar", 0072 "@label %1 day %2 month name in India National Calendar %3 year", 0073 "%1 %2, %3", 0074 sublabel.dayLabel, 0075 formattedDateStringInNativeLanguage("MMMM"), 0076 QString::number(year())); 0077 sublabel.priority = CalendarEvents::CalendarEventsPlugin::SubLabelPriority::Low; 0078 0079 return sublabel; 0080 } 0081 0082 IndianCalendarProvider::IndianCalendarProvider(QObject *parent, CalendarSystem::System calendarSystem) 0083 : AbstractCalendarProvider(parent, calendarSystem) 0084 , d(std::make_unique<IndianCalendarProviderPrivate>()) 0085 { 0086 Q_ASSERT(m_calendarSystem == CalendarSystem::Indian); 0087 } 0088 0089 IndianCalendarProvider::~IndianCalendarProvider() 0090 { 0091 } 0092 0093 QCalendar::YearMonthDay IndianCalendarProvider::fromGregorian(const QDate &date) const 0094 { 0095 return d->fromGregorian(date); 0096 } 0097 0098 CalendarEvents::CalendarEventsPlugin::SubLabel IndianCalendarProvider::subLabels(const QDate &date) const 0099 { 0100 return d->subLabels(date); 0101 }