1 /*
2   This file is part of KOrganizer.
3   SPDX-FileCopyrightText: 2001 Cornelius Schumacher <schumacher@kde.org>
4   SPDX-FileCopyrightText: 2007 Loïc Corbasson <loic.corbasson@gmail.com>
5   SPDX-FileCopyrightText: 2021 Friedrich W. H. Kossebau <kossebau@kde.org>
6 
7   SPDX-License-Identifier: GPL-2.0-or-later
8 */
9 
10 #include "picoftheday.h"
11 #include "configdialog.h"
12 #include "element.h"
13 
14 #include "korganizer_picoftheday_plugin_debug.h"
15 
16 #include <KConfig>
17 #include <KConfigGroup>
18 #include <KLocalizedString>
19 #include <KPluginFactory>
20 
21 #include <QCache>
22 #include <QGlobalStatic>
23 
24 K_PLUGIN_FACTORY(PicofthedayFactory, registerPlugin<Picoftheday>();)
25 
26 // TODO: add also disc cache to avoid even more network traffic
27 using Cache = QCache<QDate, ElementData>;
28 constexpr int cacheElementMaxSize = 6 * 7; // rows by weekdays, a full gregorian month's view
29 Q_GLOBAL_STATIC_WITH_ARGS(Cache, s_cache, (cacheElementMaxSize))
30 
31 // https://www.mediawiki.org/wiki/API:Picture_of_the_day_viewer
Picoftheday(QObject * parent,const QVariantList & args)32 Picoftheday::Picoftheday(QObject *parent, const QVariantList &args)
33     : Decoration(parent, args)
34 {
35     KConfig _config(QStringLiteral("korganizerrc"));
36     KConfigGroup config(&_config, "Picture of the Day Plugin");
37     mThumbSize = config.readEntry("InitialThumbnailSize", QSize(120, 60));
38 }
39 
configure(QWidget * parent)40 void Picoftheday::configure(QWidget *parent)
41 {
42     ConfigDialog dlg(parent);
43     dlg.exec();
44 }
45 
info() const46 QString Picoftheday::info() const
47 {
48     return i18n(
49         "<qt>This plugin provides the Wikipedia "
50         "<i>Picture of the Day</i>.</qt>");
51 }
52 
createDayElements(const QDate & date)53 Element::List Picoftheday::createDayElements(const QDate &date)
54 {
55     Element::List elements;
56 
57     auto data = s_cache->take(date);
58     qCDebug(KORGANIZERPICOFTHEDAYPLUGIN_LOG) << date << ": taking from cache" << data;
59     if (!data) {
60         data = new ElementData;
61         data->mThumbSize = mThumbSize;
62     }
63 
64     auto element = new POTDElement(QStringLiteral("main element"), date, data);
65     elements.append(element);
66 
67     return elements;
68 }
69 
cacheData(QDate date,ElementData * data)70 void Picoftheday::cacheData(QDate date, ElementData *data)
71 {
72     if (data->mState < DataLoaded) {
73         delete data;
74         return;
75     }
76     qCDebug(KORGANIZERPICOFTHEDAYPLUGIN_LOG) << date << ": adding to cache" << data;
77     s_cache->insert(date, data);
78 }
79 
80 #include "picoftheday.moc"
81