Changeset View
Changeset View
Standalone View
Standalone View
dataengines/potd/unsplashprovider.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2019 Guo Yunhe <i@guoyunhe.me> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU General Public License as published by | ||||
6 | * the Free Software Foundation; either version 2 of the License, or | ||||
7 | * (at your option) any later version. | ||||
8 | * | ||||
9 | * This program is distributed in the hope that it will be useful, | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | * GNU General Public License for more details | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU Library General Public | ||||
15 | * License along with this program; if not, write to the | ||||
16 | * Free Software Foundation, Inc., | ||||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #include "unsplashprovider.h" | ||||
21 | | ||||
22 | #include <QDebug> | ||||
23 | #include <QRegularExpression> | ||||
24 | | ||||
25 | #include <KPluginFactory> | ||||
26 | #include <KIO/Job> | ||||
27 | | ||||
28 | UnsplashProvider::UnsplashProvider(QObject* parent, const QVariantList& args) | ||||
29 | : PotdProvider(parent, args) | ||||
30 | { | ||||
31 | const QUrl url(QStringLiteral("https://unsplash.com/wallpaper/1065396/desktop-wallpapers")); | ||||
32 | | ||||
33 | KIO::StoredTransferJob* job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo); | ||||
34 | connect(job, &KIO::StoredTransferJob::finished, this, &UnsplashProvider::pageRequestFinished); | ||||
35 | } | ||||
36 | | ||||
37 | UnsplashProvider::~UnsplashProvider() = default; | ||||
38 | | ||||
39 | QImage UnsplashProvider::image() const | ||||
40 | { | ||||
41 | return mImage; | ||||
42 | } | ||||
43 | | ||||
44 | void UnsplashProvider::pageRequestFinished(KJob* _job) | ||||
45 | { | ||||
46 | KIO::StoredTransferJob* job = static_cast<KIO::StoredTransferJob*>(_job); | ||||
47 | if (job->error()) { | ||||
48 | emit error(this); | ||||
49 | return; | ||||
50 | } | ||||
51 | | ||||
52 | const QString html = QString::fromUtf8(job->data()); | ||||
53 | | ||||
54 | QRegularExpression re(QStringLiteral("src=\"(https://images\\.unsplash\\.com/photo-\\w+-\\w+)")); | ||||
55 | | ||||
56 | QRegularExpressionMatch match = re.match(html); | ||||
57 | | ||||
58 | if (match.hasMatch()) { | ||||
59 | QUrl picUrl(match.captured(1)); // url to full size photo (compressed) | ||||
60 | KIO::StoredTransferJob* imageJob = KIO::storedGet(picUrl, KIO::NoReload, KIO::HideProgressInfo); | ||||
61 | connect(imageJob, &KIO::StoredTransferJob::finished, this, &UnsplashProvider::imageRequestFinished); | ||||
62 | return; | ||||
63 | } else { | ||||
64 | emit error(this); | ||||
65 | return; | ||||
66 | } | ||||
67 | } | ||||
68 | | ||||
69 | void UnsplashProvider::imageRequestFinished(KJob* _job) | ||||
70 | { | ||||
71 | KIO::StoredTransferJob* job = static_cast<KIO::StoredTransferJob*>(_job); | ||||
72 | if (job->error()) { | ||||
73 | emit error(this); | ||||
74 | return; | ||||
75 | } | ||||
76 | QByteArray data = job->data(); | ||||
77 | mImage = QImage::fromData(data); | ||||
78 | emit finished(this); | ||||
79 | } | ||||
80 | | ||||
81 | K_PLUGIN_CLASS_WITH_JSON(UnsplashProvider, "unsplashprovider.json") | ||||
82 | | ||||
83 | #include "unsplashprovider.moc" |