Changeset View
Changeset View
Standalone View
Standalone View
dataengines/potd/unsplashprovider.cpp
Show All 22 Lines | |||||
23 | #include <QRegularExpression> | 23 | #include <QRegularExpression> | ||
24 | 24 | | |||
25 | #include <KPluginFactory> | 25 | #include <KPluginFactory> | ||
26 | #include <KIO/Job> | 26 | #include <KIO/Job> | ||
27 | 27 | | |||
28 | UnsplashProvider::UnsplashProvider(QObject* parent, const QVariantList& args) | 28 | UnsplashProvider::UnsplashProvider(QObject* parent, const QVariantList& args) | ||
29 | : PotdProvider(parent, args) | 29 | : PotdProvider(parent, args) | ||
30 | { | 30 | { | ||
31 | const QUrl url(QStringLiteral("https://unsplash.com/wallpaper/1065396/desktop-wallpapers")); | 31 | qDebug() << args; | ||
32 | const QUrl url(QStringLiteral("https://source.unsplash.com/collection/%1/3840x2160/daily").arg(args[1].toString())); | ||||
32 | 33 | | |||
33 | KIO::StoredTransferJob* job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo); | 34 | KIO::StoredTransferJob* job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo); | ||
34 | connect(job, &KIO::StoredTransferJob::finished, this, &UnsplashProvider::pageRequestFinished); | 35 | connect(job, &KIO::StoredTransferJob::finished, this, &UnsplashProvider::imageRequestFinished); | ||
35 | } | 36 | } | ||
36 | 37 | | |||
37 | UnsplashProvider::~UnsplashProvider() = default; | 38 | UnsplashProvider::~UnsplashProvider() = default; | ||
38 | 39 | | |||
39 | QImage UnsplashProvider::image() const | 40 | QImage UnsplashProvider::image() const | ||
40 | { | 41 | { | ||
41 | return mImage; | 42 | return mImage; | ||
42 | } | 43 | } | ||
43 | 44 | | |||
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 | // "?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) | 45 | void UnsplashProvider::imageRequestFinished(KJob* _job) | ||
80 | { | 46 | { | ||
81 | KIO::StoredTransferJob* job = static_cast<KIO::StoredTransferJob*>(_job); | 47 | KIO::StoredTransferJob* job = static_cast<KIO::StoredTransferJob*>(_job); | ||
82 | if (job->error()) { | 48 | if (job->error()) { | ||
83 | emit error(this); | 49 | emit error(this); | ||
84 | return; | 50 | return; | ||
85 | } | 51 | } | ||
86 | QByteArray data = job->data(); | 52 | QByteArray data = job->data(); | ||
87 | mImage = QImage::fromData(data); | 53 | mImage = QImage::fromData(data); | ||
88 | emit finished(this); | 54 | emit finished(this); | ||
89 | } | 55 | } | ||
90 | 56 | | |||
91 | K_PLUGIN_CLASS_WITH_JSON(UnsplashProvider, "unsplashprovider.json") | 57 | K_PLUGIN_CLASS_WITH_JSON(UnsplashProvider, "unsplashprovider.json") | ||
92 | 58 | | |||
93 | #include "unsplashprovider.moc" | 59 | #include "unsplashprovider.moc" |