diff --git a/dataengines/potd/potd.h b/dataengines/potd/potd.h --- a/dataengines/potd/potd.h +++ b/dataengines/potd/potd.h @@ -31,9 +31,10 @@ * This class provides the Pictures of The Day from various online websites. * * The query keys have the following structure: - * \:\ + * \:\[:other_args] * e.g. * apod:2007-07-19 + * unsplash:12435322 * */ class PotdEngine : public Plasma::DataEngine diff --git a/dataengines/potd/potd.cpp b/dataengines/potd/potd.cpp --- a/dataengines/potd/potd.cpp +++ b/dataengines/potd/potd.cpp @@ -102,17 +102,11 @@ qDebug() << "invalid provider: " << parts[ 0 ]; return false; } - + QVariantList args; - args << providerName; - if ( parts.count() > 1 ) { - const QDate date = QDate::fromString( parts[ 1 ], Qt::ISODate ); - if ( !date.isValid() ) { - qDebug() << "invalid date:" << parts[1]; - return false; - } - args << date; + for (int i = 0; i < parts.count(); i++) { + args << parts[i]; } auto factory = KPluginLoader(mFactories[ providerName ].fileName()).factory(); diff --git a/dataengines/potd/potdprovider.cpp b/dataengines/potd/potdprovider.cpp --- a/dataengines/potd/potdprovider.cpp +++ b/dataengines/potd/potdprovider.cpp @@ -21,27 +21,41 @@ // Qt #include +#include class PotdProviderPrivate { public: QString name; QDate date; + QString identifier; }; PotdProvider::PotdProvider( QObject *parent, const QVariantList &args ) : QObject( parent ), d(new PotdProviderPrivate) { if ( args.count() > 0 ) { d->name = args[ 0 ].toString(); + + d->identifier = d->name; - if ( args.count() > 1 && args[ 1 ].canConvert( QVariant::Date ) ) { - d->date = args[ 1 ].toDate(); + QRegularExpression re(QStringLiteral("\\d{4}-\\d{2}-\\d{2}")); + if ( args.count() > 1 ) { + for (int i = 1; i < args.count(); i++) { + d->identifier += QStringLiteral(":") + args[i].toString(); + if (re.match(args[ i ].toString()).hasMatch()) { + QDate date = QDate::fromString(args[ i ].toString()); + if (date.isValid()) { + d->date = date; + } + } + } } } else { d->name = QStringLiteral("Unknown"); + d->identifier = d->name; } } @@ -66,11 +80,7 @@ QString PotdProvider::identifier() const { - if (isFixedDate()) { - return d->name + QLatin1Char(':') + d->date.toString(Qt::ISODate); - } - - return d->name; + return d->identifier; } diff --git a/dataengines/potd/unsplashprovider.h b/dataengines/potd/unsplashprovider.h --- a/dataengines/potd/unsplashprovider.h +++ b/dataengines/potd/unsplashprovider.h @@ -28,7 +28,7 @@ /** * This class provides random wallpapers from Unsplash Wallpapers - * Image urls are parsed from https://unsplash.com/wallpaper/1065396/desktop-wallpapers + * https://unsplash.com/wallpaper/ */ class UnsplashProvider : public PotdProvider { @@ -57,7 +57,6 @@ QImage image() const override; private: - void pageRequestFinished(KJob *job); void imageRequestFinished(KJob *job); private: diff --git a/dataengines/potd/unsplashprovider.cpp b/dataengines/potd/unsplashprovider.cpp --- a/dataengines/potd/unsplashprovider.cpp +++ b/dataengines/potd/unsplashprovider.cpp @@ -28,10 +28,18 @@ UnsplashProvider::UnsplashProvider(QObject* parent, const QVariantList& args) : PotdProvider(parent, args) { - const QUrl url(QStringLiteral("https://unsplash.com/wallpaper/1065396/desktop-wallpapers")); + QString collectionId = QStringLiteral("1065976"); + QRegularExpression re(QStringLiteral("^\\d+$")); + for (int i = 1; i < args.count(); i++) { + QString str = args[i].toString(); + if (re.match(str).hasMatch()) { + collectionId = str; + } + } + const QUrl url(QStringLiteral("https://source.unsplash.com/collection/%1/3840x2160/daily").arg(collectionId)); KIO::StoredTransferJob* job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo); - connect(job, &KIO::StoredTransferJob::finished, this, &UnsplashProvider::pageRequestFinished); + connect(job, &KIO::StoredTransferJob::finished, this, &UnsplashProvider::imageRequestFinished); } UnsplashProvider::~UnsplashProvider() = default; @@ -41,41 +49,6 @@ return mImage; } -void UnsplashProvider::pageRequestFinished(KJob* _job) -{ - KIO::StoredTransferJob* job = static_cast(_job); - if (job->error()) { - emit error(this); - return; - } - - const QString html = QString::fromUtf8(job->data()); - - // "?ixlib" will filter out the banner image which rarely change... - QRegularExpression re(QStringLiteral("src=\"(https://images\\.unsplash\\.com/photo-\\w+-\\w+)\\?ixlib")); - - QRegularExpressionMatchIterator i = re.globalMatch(html); - - QStringList urls; - - while (i.hasNext()) { - QRegularExpressionMatch match = i.next(); - QString url = match.captured(1); - urls << url; - } - - if (urls.size() > 0) { - // Pick a ramdom photo because the wallpaper page doesn't update every day - QUrl picUrl(urls.at(rand() % urls.size())); // url to full size photo (compressed) - KIO::StoredTransferJob* imageJob = KIO::storedGet(picUrl, KIO::NoReload, KIO::HideProgressInfo); - connect(imageJob, &KIO::StoredTransferJob::finished, this, &UnsplashProvider::imageRequestFinished); - return; - } else { - emit error(this); - return; - } -} - void UnsplashProvider::imageRequestFinished(KJob* _job) { KIO::StoredTransferJob* job = static_cast(_job); diff --git a/wallpapers/potd/contents/config/main.xml b/wallpapers/potd/contents/config/main.xml --- a/wallpapers/potd/contents/config/main.xml +++ b/wallpapers/potd/contents/config/main.xml @@ -10,6 +10,10 @@ apod + + + 1065976 + 0 diff --git a/wallpapers/potd/contents/ui/config.qml b/wallpapers/potd/contents/ui/config.qml --- a/wallpapers/potd/contents/ui/config.qml +++ b/wallpapers/potd/contents/ui/config.qml @@ -30,6 +30,7 @@ anchors.right: parent.right property string cfg_Provider + property string cfg_Category property int cfg_FillMode property alias cfg_Color: colorButton.color property alias formLayout: root @@ -80,6 +81,135 @@ cfg_Provider = providerModel.get(currentIndex)["id"] } } + + // TODO: port to QQC2 version once we've fixed https://bugs.kde.org/show_bug.cgi?id=403153 + QQC1.ComboBox { + id: categoryComboBox + visible: cfg_Provider === 'unsplash' + TextMetrics { + id: categoryTextMetrics + text: categoryComboBox.currentText + } + implicitWidth: Math.max(providerTextMetrics.width + Kirigami.Units.gridUnit * 2 + Kirigami.Units.smallSpacing * 2, pluginComboBox.width) //QQC1 Combobox default sizing is broken + Kirigami.FormData.label: i18ndc("plasma_wallpaper_org.kde.potd", "@label:listbox", "Category:") + model: [ + { + 'label': i18ndc("plasma_wallpaper_org.kde.potd", "@item:inlistbox", "All"), + 'value': '1065976' + }, + { + 'label': i18ndc("plasma_wallpaper_org.kde.potd", "@item:inlistbox", "1080p"), + 'value': '1339107' + }, + { + 'label': i18ndc("plasma_wallpaper_org.kde.potd", "@item:inlistbox", "4K"), + 'value': '1339090' + }, + { + 'label': i18ndc("plasma_wallpaper_org.kde.potd", "@item:inlistbox", "Ultra Wide"), + 'value': '1339089' + }, + { + 'label': i18ndc("plasma_wallpaper_org.kde.potd", "@item:inlistbox", "Background"), + 'value': '1339276' + }, + { + 'label': i18ndc("plasma_wallpaper_org.kde.potd", "@item:inlistbox", "Lock Screen"), + 'value': '1339070' + }, + { + 'label': i18ndc("plasma_wallpaper_org.kde.potd", "@item:inlistbox", "Nature"), + 'value': '1065376' + }, + { + 'label': i18ndc("plasma_wallpaper_org.kde.potd", "@item:inlistbox", "Tumblr"), + 'value': '1111644' + }, + { + 'label': i18ndc("plasma_wallpaper_org.kde.potd", "@item:inlistbox", "Black"), + 'value': '1101680' + }, + { + 'label': i18ndc("plasma_wallpaper_org.kde.potd", "@item:inlistbox", "Flower"), + 'value': '1100232' + }, + { + 'label': i18ndc("plasma_wallpaper_org.kde.potd", "@item:inlistbox", "Funny"), + 'value': '1111682' + }, + { + 'label': i18ndc("plasma_wallpaper_org.kde.potd", "@item:inlistbox", "Cute"), + 'value': '1111680' + }, + { + 'label': i18ndc("plasma_wallpaper_org.kde.potd", "@item:inlistbox", "Cool"), + 'value': '1111678' + }, + { + 'label': i18ndc("plasma_wallpaper_org.kde.potd", "@item:inlistbox", "Fall"), + 'value': '1100229' + }, + { + 'label': i18ndc("plasma_wallpaper_org.kde.potd", "@item:inlistbox", "Love"), + 'value': '1066280' + }, + { + 'label': i18ndc("plasma_wallpaper_org.kde.potd", "@item:inlistbox", "Design"), + 'value': '1066276' + }, + { + 'label': i18ndc("plasma_wallpaper_org.kde.potd", "@item:inlistbox", "Christmas"), + 'value': '1066273' + }, + { + 'label': i18ndc("plasma_wallpaper_org.kde.potd", "@item:inlistbox", "Travel"), + 'value': '1065428' + }, + { + 'label': i18ndc("plasma_wallpaper_org.kde.potd", "@item:inlistbox", "Beach"), + 'value': '1065423' + }, + { + 'label': i18ndc("plasma_wallpaper_org.kde.potd", "@item:inlistbox", "Car"), + 'value': '1065408' + }, + { + 'label': i18ndc("plasma_wallpaper_org.kde.potd", "@item:inlistbox", "Sports"), + 'value': '1065402' + }, + { + 'label': i18ndc("plasma_wallpaper_org.kde.potd", "@item:inlistbox", "Animal"), + 'value': '1065390' + }, + { + 'label': i18ndc("plasma_wallpaper_org.kde.potd", "@item:inlistbox", "People"), + 'value': '1065386' + }, + { + 'label': i18ndc("plasma_wallpaper_org.kde.potd", "@item:inlistbox", "Music"), + 'value': '1065384' + }, + { + 'label': i18ndc("plasma_wallpaper_org.kde.potd", "@item:inlistbox", "Summer"), + 'value': '1065380' + }, + { + 'label': i18ndc("plasma_wallpaper_org.kde.potd", "@item:inlistbox", "Galaxy"), + 'value': '1065374' + }, + ] + textRole: "label" + onCurrentIndexChanged: { + cfg_Category = model[currentIndex]["value"] + } + Component.onCompleted: { + for (var i = 0; i < model.length; i++) { + if (model[i]["value"] == wallpaper.configuration.Category) { + categoryComboBox.currentIndex = i; + } + } + } + } // TODO: port to QQC2 version once we've fixed https://bugs.kde.org/show_bug.cgi?id=403153 QQC1.ComboBox { diff --git a/wallpapers/potd/contents/ui/main.qml b/wallpapers/potd/contents/ui/main.qml --- a/wallpapers/potd/contents/ui/main.qml +++ b/wallpapers/potd/contents/ui/main.qml @@ -25,11 +25,13 @@ id: root readonly property string provider: wallpaper.configuration.Provider + readonly property string category: wallpaper.configuration.Category + readonly property string identifier: provider === 'unsplash' && category ? provider + ':' + category : provider PlasmaCore.DataSource { id: engine engine: "potd" - connectedSources: [provider] + connectedSources: [identifier] } Rectangle { @@ -43,7 +45,7 @@ QImageItem { anchors.fill: parent - image: engine.data[provider]["Image"] + image: engine.data[identifier].Image fillMode: wallpaper.configuration.FillMode smooth: true }