File indexing completed on 2024-05-19 05:21:40

0001 /*
0002   This file is part of KOrganizer.
0003 
0004   SPDX-FileCopyrightText: 2002, 2003 Cornelius Schumacher <schumacher@kde.org>
0005 
0006   SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
0007 */
0008 
0009 #include "koglobals.h"
0010 #include "prefs/koprefs.h"
0011 #include <KHolidays/HolidayRegion>
0012 
0013 #include <QApplication>
0014 
0015 class KOGlobalsSingletonPrivate
0016 {
0017 public:
0018     KOGlobals instance;
0019 };
0020 
0021 Q_GLOBAL_STATIC(KOGlobalsSingletonPrivate, sKOGlobalsSingletonPrivate)
0022 
0023 KOGlobals *KOGlobals::self()
0024 {
0025     return &sKOGlobalsSingletonPrivate->instance;
0026 }
0027 
0028 KOGlobals::KOGlobals() = default;
0029 
0030 KOGlobals::~KOGlobals()
0031 {
0032     qDeleteAll(mHolidayRegions);
0033 }
0034 
0035 bool KOGlobals::reverseLayout()
0036 {
0037     return QApplication::isRightToLeft();
0038 }
0039 
0040 QMap<QDate, QStringList> KOGlobals::holiday(const QDate &start, const QDate &end) const
0041 {
0042     QMap<QDate, QStringList> holidaysByDate;
0043 
0044     if (mHolidayRegions.isEmpty()) {
0045         return holidaysByDate;
0046     }
0047 
0048     for (const KHolidays::HolidayRegion *region : std::as_const(mHolidayRegions)) {
0049         if (region && region->isValid()) {
0050             const KHolidays::Holiday::List list = region->rawHolidaysWithAstroSeasons(start, end);
0051             const int listCount(list.count());
0052             for (int i = 0; i < listCount; ++i) {
0053                 const KHolidays::Holiday &h = list.at(i);
0054                 // dedupe, since we support multiple holiday regions which may have similar holidays
0055                 if (!holidaysByDate[h.observedStartDate()].contains(h.name())) {
0056                     holidaysByDate[h.observedStartDate()].append(h.name());
0057                 }
0058             }
0059         }
0060     }
0061 
0062     return holidaysByDate;
0063 }
0064 
0065 int KOGlobals::firstDayOfWeek() const
0066 {
0067     return KOPrefs::instance()->mWeekStartDay + 1;
0068 }
0069 
0070 int KOGlobals::getWorkWeekMask()
0071 {
0072     return KOPrefs::instance()->mWorkWeekMask;
0073 }
0074 
0075 void KOGlobals::setHolidays(const QStringList &regions)
0076 {
0077     qDeleteAll(mHolidayRegions);
0078     mHolidayRegions.clear();
0079     for (const QString &regionStr : regions) {
0080         auto region = new KHolidays::HolidayRegion(regionStr);
0081         if (region->isValid()) {
0082             mHolidayRegions.append(region);
0083         } else {
0084             delete region;
0085         }
0086     }
0087 }
0088 
0089 QList<KHolidays::HolidayRegion *> KOGlobals::holidays() const
0090 {
0091     return mHolidayRegions;
0092 }