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

0001 /*
0002   This file is part of KOrganizer.
0003   SPDX-FileCopyrightText: 2001 Cornelius Schumacher <schumacher@kde.org>
0004   SPDX-FileCopyrightText: 2007 Loïc Corbasson <loic.corbasson@gmail.com>
0005   SPDX-FileCopyrightText: 2021 Friedrich W. H. Kossebau <kossebau@kde.org>
0006 
0007   SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "picoftheday.h"
0011 #include "configdialog.h"
0012 #include "element.h"
0013 
0014 #include "korganizer_picoftheday_plugin_debug.h"
0015 
0016 #include <KConfig>
0017 #include <KConfigGroup>
0018 #include <KLocalizedString>
0019 #include <KPluginFactory>
0020 
0021 #include <QCache>
0022 
0023 K_PLUGIN_CLASS_WITH_JSON(Picoftheday, "picoftheday.json")
0024 
0025 // TODO: add also disc cache to avoid even more network traffic
0026 using Cache = QCache<QDate, ElementData>;
0027 constexpr int cacheElementMaxSize = 6 * 7; // rows by weekdays, a full gregorian month's view
0028 Q_GLOBAL_STATIC_WITH_ARGS(Cache, s_cache, (cacheElementMaxSize))
0029 
0030 // https://www.mediawiki.org/wiki/API:Picture_of_the_day_viewer
0031 Picoftheday::Picoftheday(QObject *parent, const QVariantList &args)
0032     : Decoration(parent, args)
0033 {
0034     KConfig _config(QStringLiteral("korganizerrc"));
0035     KConfigGroup config(&_config, QStringLiteral("Picture of the Day Plugin"));
0036     mThumbSize = config.readEntry("InitialThumbnailSize", QSize(120, 60));
0037 }
0038 
0039 void Picoftheday::configure(QWidget *parent)
0040 {
0041     ConfigDialog dlg(parent);
0042     dlg.exec();
0043 }
0044 
0045 QString Picoftheday::info() const
0046 {
0047     return i18n(
0048         "<qt>This plugin provides the Wikipedia "
0049         "<i>Picture of the Day</i>.</qt>");
0050 }
0051 
0052 Element::List Picoftheday::createDayElements(const QDate &date)
0053 {
0054     Element::List elements;
0055 
0056     auto data = s_cache->take(date);
0057     qCDebug(KORGANIZERPICOFTHEDAYPLUGIN_LOG) << date << ": taking from cache" << data;
0058     if (!data) {
0059         data = new ElementData;
0060         data->mThumbSize = mThumbSize;
0061     }
0062 
0063     auto element = new POTDElement(QStringLiteral("main element"), date, data);
0064     elements.append(element);
0065 
0066     return elements;
0067 }
0068 
0069 void Picoftheday::cacheData(QDate date, ElementData *data)
0070 {
0071     if (data->mState < DataLoaded) {
0072         delete data;
0073         return;
0074     }
0075     qCDebug(KORGANIZERPICOFTHEDAYPLUGIN_LOG) << date << ": adding to cache" << data;
0076     s_cache->insert(date, data);
0077 }
0078 
0079 #include "picoftheday.moc"