File indexing completed on 2024-06-16 05:03:55
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 subLabel(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 return QString::fromUtf16(dateString.getBuffer(), dateString.length()); 0048 } 0049 0050 QCalendar::YearMonthDay IndianCalendarProviderPrivate::fromGregorian(const QDate &_date) 0051 { 0052 if (U_FAILURE(m_errorCode) || !_date.isValid() || !setDate(_date)) { 0053 return {}; 0054 } 0055 0056 return date(); 0057 } 0058 0059 CalendarEvents::CalendarEventsPlugin::SubLabel IndianCalendarProviderPrivate::subLabel(const QDate &date) 0060 { 0061 auto sublabel = CalendarEvents::CalendarEventsPlugin::SubLabel{}; 0062 0063 if (U_FAILURE(m_errorCode) || !date.isValid() || !setDate(date)) { 0064 return sublabel; 0065 } 0066 0067 sublabel.dayLabel = QString::number(day()); 0068 sublabel.label = i18ndc("plasma_calendar_alternatecalendar", 0069 "@label %1 day %2 month name in India National Calendar %3 year", 0070 "%1 %2, %3", 0071 sublabel.dayLabel, 0072 formattedDateStringInNativeLanguage("MMMM"), 0073 QString::number(year())); 0074 sublabel.priority = CalendarEvents::CalendarEventsPlugin::SubLabelPriority::Low; 0075 0076 return sublabel; 0077 } 0078 0079 IndianCalendarProvider::IndianCalendarProvider(QObject *parent, CalendarSystem::System calendarSystem, const QDate &startDate, const QDate &endDate) 0080 : AbstractCalendarProvider(parent, calendarSystem, startDate, endDate) 0081 , d(std::make_unique<IndianCalendarProviderPrivate>()) 0082 { 0083 Q_ASSERT(m_calendarSystem == CalendarSystem::Indian); 0084 } 0085 0086 IndianCalendarProvider::~IndianCalendarProvider() 0087 { 0088 } 0089 0090 QCalendar::YearMonthDay IndianCalendarProvider::fromGregorian(const QDate &date) const 0091 { 0092 return d->fromGregorian(date); 0093 } 0094 0095 CalendarEvents::CalendarEventsPlugin::SubLabel IndianCalendarProvider::subLabel(const QDate &date) const 0096 { 0097 return d->subLabel(date); 0098 }