Changeset View
Changeset View
Standalone View
Standalone View
dataengines/potd/unsplashprovider.cpp
Show All 13 Lines | |||||
14 | * You should have received a copy of the GNU Library General Public | 14 | * You should have received a copy of the GNU Library General Public | ||
15 | * License along with this program; if not, write to the | 15 | * License along with this program; if not, write to the | ||
16 | * Free Software Foundation, Inc., | 16 | * Free Software Foundation, Inc., | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
18 | */ | 18 | */ | ||
19 | 19 | | |||
20 | #include "unsplashprovider.h" | 20 | #include "unsplashprovider.h" | ||
21 | 21 | | |||
22 | #include <QDebug> | | |||
23 | #include <QRegularExpression> | | |||
24 | | ||||
25 | #include <KPluginFactory> | 22 | #include <KPluginFactory> | ||
26 | #include <KIO/Job> | | |||
27 | 23 | | |||
28 | UnsplashProvider::UnsplashProvider(QObject* parent, const QVariantList& args) | 24 | UnsplashProvider::UnsplashProvider(QObject* parent, const QVariantList& args) | ||
29 | : PotdProvider(parent, args) | 25 | : UnsplashBaseProvider(1065976, 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 | { | 26 | { | ||
41 | return mImage; | | |||
42 | } | 27 | } | ||
43 | 28 | | |||
44 | void UnsplashProvider::pageRequestFinished(KJob* _job) | 29 | UnsplashProvider::~UnsplashProvider() | ||
45 | { | 30 | { | ||
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 | // "?ixlib" will filter out the banner image which rarely change... | | |||
55 | QRegularExpression re(QStringLiteral("src=\"(https://images\\.unsplash\\.com/photo-\\w+-\\w+)\\?ixlib")); | | |||
56 | | ||||
57 | QRegularExpressionMatchIterator i = re.globalMatch(html); | | |||
58 | | ||||
59 | QStringList urls; | | |||
60 | | ||||
61 | while (i.hasNext()) { | | |||
62 | QRegularExpressionMatch match = i.next(); | | |||
63 | QString url = match.captured(1); | | |||
64 | urls << url; | | |||
65 | } | | |||
66 | | ||||
67 | if (urls.size() > 0) { | | |||
68 | // Pick a ramdom photo because the wallpaper page doesn't update every day | | |||
69 | QUrl picUrl(urls.at(rand() % urls.size())); // url to full size photo (compressed) | | |||
70 | KIO::StoredTransferJob* imageJob = KIO::storedGet(picUrl, KIO::NoReload, KIO::HideProgressInfo); | | |||
71 | connect(imageJob, &KIO::StoredTransferJob::finished, this, &UnsplashProvider::imageRequestFinished); | | |||
72 | return; | | |||
73 | } else { | | |||
74 | emit error(this); | | |||
75 | return; | | |||
76 | } | | |||
77 | } | | |||
78 | | ||||
79 | void UnsplashProvider::imageRequestFinished(KJob* _job) | | |||
80 | { | | |||
81 | KIO::StoredTransferJob* job = static_cast<KIO::StoredTransferJob*>(_job); | | |||
82 | if (job->error()) { | | |||
83 | emit error(this); | | |||
84 | return; | | |||
85 | } | | |||
86 | QByteArray data = job->data(); | | |||
87 | mImage = QImage::fromData(data); | | |||
88 | emit finished(this); | | |||
89 | } | 31 | } | ||
90 | 32 | | |||
91 | K_PLUGIN_CLASS_WITH_JSON(UnsplashProvider, "unsplashprovider.json") | 33 | K_PLUGIN_CLASS_WITH_JSON(UnsplashProvider, "unsplashprovider.json") | ||
92 | 34 | | |||
93 | #include "unsplashprovider.moc" | 35 | #include "unsplashprovider.moc" |