/
bingprovider.cpp
116 lines (99 loc) · 3.3 KB
/
bingprovider.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Copyright 2017 Weng Xuetian <wengxt@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "bingprovider.h"
#include <QRegExp>
#include <QImage>
#include <QJsonDocument>
#include <QJsonArray>
#include <QDebug>
#include <kio/job.h>
class BingProvider::Private
{
public:
Private(BingProvider* parent)
: mParent(parent)
{
}
void pageRequestFinished(KJob*);
void imageRequestFinished(KJob*);
void parsePage();
BingProvider* mParent;
QImage mImage;
};
void BingProvider::Private::pageRequestFinished(KJob* _job)
{
KIO::StoredTransferJob* job = static_cast<KIO::StoredTransferJob*>(_job);
if (job->error()) {
emit mParent->error(mParent);
return;
}
auto json = QJsonDocument::fromJson(job->data());
do {
if (json.isNull()) {
break;
}
auto imagesArray = json.object().value("images");
if (!imagesArray.isArray() || imagesArray.toArray().size() <= 0) {
break;
}
auto imageObj = imagesArray.toArray().at(0);
if (!imageObj.isObject()) {
break;
}
auto url = imageObj.toObject().value("url");
if (!url.isString() || url.toString().isEmpty()) {
break;
}
QUrl picUrl(QString(QLatin1String("https://www.bing.com/%1")).arg(url.toString()));
KIO::StoredTransferJob* imageJob = KIO::storedGet(picUrl, KIO::NoReload, KIO::HideProgressInfo);
mParent->connect(imageJob, SIGNAL(finished(KJob*)), SLOT(imageRequestFinished(KJob*)));
return;
} while (0);
emit mParent->error(mParent);
return;
}
void BingProvider::Private::imageRequestFinished(KJob* _job)
{
KIO::StoredTransferJob* job = static_cast<KIO::StoredTransferJob*>(_job);
if (job->error()) {
emit mParent->error(mParent);
return;
}
QByteArray data = job->data();
mImage = QImage::fromData(data);
emit mParent->finished(mParent);
}
BingProvider::BingProvider(QObject* parent, const QVariantList& args)
: PotdProvider(parent, args), d(new Private(this))
{
QUrl url(QLatin1String("https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1"));
KIO::StoredTransferJob* job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo);
connect(job, SIGNAL(finished(KJob*)), SLOT(pageRequestFinished(KJob*)));
}
BingProvider::~BingProvider()
{
delete d;
}
QImage BingProvider::image() const
{
return d->mImage;
}
K_PLUGIN_FACTORY_WITH_JSON(BingProviderFactory, "bingprovider.json", registerPlugin<BingProvider>();)
#include "bingprovider.moc"
#include "moc_bingprovider.cpp"