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 | QString collectionId = QStringLiteral("1065976"); | ||
32 | QRegularExpression re(QStringLiteral("^\\d+$")); | ||||
33 | for (int i = 1; i < args.count(); i++) { | ||||
34 | QString str = args[i].toString(); | ||||
35 | if (re.match(str).hasMatch()) { | ||||
36 | collectionId = str; | ||||
37 | } | ||||
38 | } | ||||
39 | const QUrl url(QStringLiteral("https://source.unsplash.com/collection/%1/3840x2160/daily").arg(collectionId)); | ||||
32 | 40 | | |||
33 | KIO::StoredTransferJob* job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo); | 41 | KIO::StoredTransferJob* job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo); | ||
34 | connect(job, &KIO::StoredTransferJob::finished, this, &UnsplashProvider::pageRequestFinished); | 42 | connect(job, &KIO::StoredTransferJob::finished, this, &UnsplashProvider::imageRequestFinished); | ||
35 | } | 43 | } | ||
36 | 44 | | |||
37 | UnsplashProvider::~UnsplashProvider() = default; | 45 | UnsplashProvider::~UnsplashProvider() = default; | ||
38 | 46 | | |||
39 | QImage UnsplashProvider::image() const | 47 | QImage UnsplashProvider::image() const | ||
40 | { | 48 | { | ||
41 | return mImage; | 49 | return mImage; | ||
42 | } | 50 | } | ||
43 | 51 | | |||
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) | 52 | void UnsplashProvider::imageRequestFinished(KJob* _job) | ||
80 | { | 53 | { | ||
81 | KIO::StoredTransferJob* job = static_cast<KIO::StoredTransferJob*>(_job); | 54 | KIO::StoredTransferJob* job = static_cast<KIO::StoredTransferJob*>(_job); | ||
82 | if (job->error()) { | 55 | if (job->error()) { | ||
83 | emit error(this); | 56 | emit error(this); | ||
84 | return; | 57 | return; | ||
85 | } | 58 | } | ||
86 | QByteArray data = job->data(); | 59 | QByteArray data = job->data(); | ||
87 | mImage = QImage::fromData(data); | 60 | mImage = QImage::fromData(data); | ||
88 | emit finished(this); | 61 | emit finished(this); | ||
89 | } | 62 | } | ||
90 | 63 | | |||
91 | K_PLUGIN_CLASS_WITH_JSON(UnsplashProvider, "unsplashprovider.json") | 64 | K_PLUGIN_CLASS_WITH_JSON(UnsplashProvider, "unsplashprovider.json") | ||
92 | 65 | | |||
93 | #include "unsplashprovider.moc" | 66 | #include "unsplashprovider.moc" |