File indexing completed on 2025-01-05 04:49:35

0001 /*
0002   This file is part of KOrganizer.
0003 
0004   SPDX-FileCopyrightText: 2001 Cornelius Schumacher <schumacher@kde.org>
0005   SPDX-FileCopyrightText: 2007 Loïc Corbasson <loic.corbasson@gmail.com>
0006 
0007   SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "datenums.h"
0011 #include "configdialog.h"
0012 
0013 #include <KConfig>
0014 #include <KConfigGroup>
0015 #include <KLocalizedString>
0016 #include <KPluginFactory>
0017 
0018 K_PLUGIN_CLASS_WITH_JSON(Datenums, "datenums.json")
0019 
0020 Datenums::Datenums(QObject *parent, const QVariantList &args)
0021     : Decoration(parent, args)
0022 {
0023     KConfig _config(QStringLiteral("korganizerrc"), KConfig::NoGlobals);
0024     KConfigGroup config(&_config, QStringLiteral("Calendar/Datenums Plugin"));
0025     mDisplayedInfo = (DayNumbers)config.readEntry("ShowDayNumbers", int(DayOfYear | DaysRemaining));
0026 }
0027 
0028 void Datenums::configure(QWidget *parent)
0029 {
0030     ConfigDialog dlg(parent);
0031     dlg.exec();
0032 }
0033 
0034 QString Datenums::info() const
0035 {
0036     return i18n("This plugin shows information on a day's position in the year.");
0037 }
0038 
0039 Element::List Datenums::createDayElements(const QDate &date)
0040 {
0041     Element::List result;
0042 
0043     int dayOfYear = date.dayOfYear();
0044     int remainingDays = date.daysInYear() - dayOfYear;
0045 
0046     StoredElement *e = nullptr;
0047     switch (mDisplayedInfo) {
0048     case DayOfYear: // only day of year
0049         e = new StoredElement(QStringLiteral("main element"), QString::number(dayOfYear));
0050         break;
0051     case DaysRemaining: // only days until end of year
0052         e = new StoredElement(QStringLiteral("main element"),
0053                               QString::number(remainingDays),
0054                               i18np("1 day before the end of the year", "%1 days before the end of the year", remainingDays));
0055         break;
0056     default:
0057         e = new StoredElement(QStringLiteral("main element"),
0058                               QString::number(dayOfYear),
0059                               i18nc("dayOfYear / daysTillEndOfYear", "%1 / %2", dayOfYear, remainingDays),
0060                               i18np("1 day since the beginning of the year,\n", "%1 days since the beginning of the year,\n", dayOfYear)
0061                                   + i18np("1 day until the end of the year", "%1 days until the end of the year", remainingDays));
0062         break;
0063     }
0064     result.append(e);
0065 
0066     return result;
0067 }
0068 
0069 static int weeksInYear(int year)
0070 {
0071     QDate date(year, 12, 31);
0072     for (int i = 0; i < 6; ++i) {
0073         if (date.weekNumber() > 1) {
0074             break;
0075         }
0076         date = date.addDays(-1);
0077     }
0078     return date.weekNumber();
0079 }
0080 
0081 Element::List Datenums::createWeekElements(const QDate &date)
0082 {
0083     Element::List result;
0084 
0085     int *yearOfTheWeek = nullptr;
0086     yearOfTheWeek = nullptr;
0087     int remainingWeeks;
0088     const int weekOfYear = date.weekNumber(yearOfTheWeek);
0089 
0090     QString weekOfYearShort;
0091     QString weekOfYearLong;
0092     QString weekOfYearExtensive;
0093     QString remainingWeeksShort;
0094     QString remainingWeeksLong;
0095     QString remainingWeeksExtensive;
0096     QString weekOfYearAndRemainingWeeksShort;
0097 
0098     // Usual case: the week belongs to this year
0099     remainingWeeks = weeksInYear(date.year()) - weekOfYear;
0100 
0101     weekOfYearShort = QString::number(weekOfYear);
0102     weekOfYearLong = i18nc("Week weekOfYear", "Week %1", weekOfYear);
0103     weekOfYearExtensive = i18np("1 week since the beginning of the year", "%1 weeks since the beginning of the year", weekOfYear);
0104 
0105     if (yearOfTheWeek) { // The week does not belong to this year
0106         weekOfYearShort = i18nc("weekOfYear (year)", "%1 (%2)", weekOfYear, *yearOfTheWeek);
0107         weekOfYearLong = i18nc("Week weekOfYear (year)", "Week %1 (%2)", weekOfYear, *yearOfTheWeek);
0108 
0109         if (*yearOfTheWeek == date.year() + 1) {
0110             // The week belongs to next year
0111             remainingWeeks = 0;
0112 
0113             weekOfYearExtensive = i18np("1 week since the beginning of the year", "%1 weeks since the beginning of the year", weekOfYear);
0114         } else {
0115             // The week belongs to last year
0116             remainingWeeks = weeksInYear(date.year());
0117 
0118             weekOfYearExtensive = i18np("1 week since the beginning of the year", "%1 weeks since the beginning of the year", 0);
0119         }
0120     }
0121 
0122     remainingWeeksShort = i18np("1 week remaining", "%1 weeks remaining", remainingWeeks);
0123     remainingWeeksExtensive = i18np("1 week until the end of the year", "%1 weeks until the end of the year", remainingWeeks);
0124     weekOfYearAndRemainingWeeksShort = i18nc("weekOfYear / weeksTillEndOfYear", "%1 / %2", weekOfYear, remainingWeeks);
0125 
0126     StoredElement *e = nullptr;
0127     switch (mDisplayedInfo) {
0128     case DayOfYear: // only week of year
0129         e = new StoredElement(QStringLiteral("main element"), weekOfYearShort, weekOfYearLong, weekOfYearExtensive);
0130         break;
0131     case DaysRemaining: // only weeks until end of year
0132         e = new StoredElement(QStringLiteral("main element"), remainingWeeksShort, remainingWeeksLong, remainingWeeksExtensive);
0133         break;
0134     default:
0135         e = new StoredElement(QStringLiteral("main element"),
0136                               weekOfYearShort,
0137                               weekOfYearAndRemainingWeeksShort,
0138                               i18nc("n weeks since the beginning of the year\n"
0139                                     "n weeks until the end of the year",
0140                                     "%1\n%2",
0141                                     weekOfYearExtensive,
0142                                     remainingWeeksExtensive));
0143         break;
0144     }
0145     result.append(e);
0146 
0147     return result;
0148 }
0149 
0150 #include "datenums.moc"