Commit 3dddbaa6 authored by Daniel Vrátil's avatar Daniel Vrátil 🤖

FB: small fixes and cleanup

parent ec919117
......@@ -46,15 +46,30 @@ QVector<Akonadi::Item> BirthdayListJob::items() const
return mItems;
}
KIO::StoredTransferJob *BirthdayListJob::createGetJob(const QUrl &url) const
{
auto job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo);
job->setMetaData({ QMap<QString,QString>{
{ QStringLiteral("cookies"), QStringLiteral("manual") },
{ QStringLiteral("setcookies"), mCookies } }
});
return job;
}
void BirthdayListJob::emitError(const QString& errorText)
{
setError(KJob::UserDefinedError);
setErrorText(errorText);
emitResult();
}
void BirthdayListJob::start()
{
auto tokenJob = new GetTokenJob(qobject_cast<FacebookResource*>(parent()));
connect(tokenJob, &GetTokenJob::result,
this, [this, tokenJob]() {
if (tokenJob->error()) {
setError(tokenJob->error());
setErrorText(tokenJob->errorText());
emitResult();
emitError(tokenJob->errorText());
return;
}
......@@ -71,23 +86,6 @@ void BirthdayListJob::start()
tokenJob->start();
}
KIO::StoredTransferJob *BirthdayListJob::createGetJob(const QUrl &url) const
{
auto job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo);
job->setMetaData({ QMap<QString,QString>{
{ QStringLiteral("cookies"), QStringLiteral("manual") },
{ QStringLiteral("setcookies"), mCookies } }
});
return job;
}
void BirthdayListJob::emitError(const QString& errorText)
{
setError(KJob::UserDefinedError);
setErrorText(errorText);
emitResult();
}
void BirthdayListJob::fetchFacebookEventsPage()
{
auto job = createGetJob(QUrl(QStringLiteral("https://www.facebook.com/events/birthdays")));
......@@ -113,11 +111,11 @@ void BirthdayListJob::fetchFacebookEventsPage()
QUrl BirthdayListJob::findBirthdayIcalLink(const QByteArray &data)
{
// QXmlStreamParser cannot deal with Facebook's broken HTML and refuses
// to parse it. But since we know very well what we are looking for and the
// address is very unique in the source code, using QBAMatcher is much more
// efficient than QXmlStreamParser anyway...
// to parse it. But we know very well what we are looking for and the
// address is very unique in the source code, so using QBAMatcher is much more
// efficient...
QByteArrayMatcher matcher("webcal://www.facebook.com/ical/b.php");
const QByteArrayMatcher matcher("webcal://www.facebook.com/ical/b.php");
const int start = matcher.indexIn(data);
if (start == -1) {
return {};
......@@ -128,7 +126,7 @@ QUrl BirthdayListJob::findBirthdayIcalLink(const QByteArray &data)
return {};
}
auto str = QString::fromUtf8(data.constData() + start, end - start);
const auto str = QString::fromUtf8(data.constData() + start, end - start);
return QUrl(KCharsets::resolveEntities(str));
}
......@@ -170,7 +168,7 @@ void BirthdayListJob::processEvent(const KCalCore::Event::Ptr &event)
}
const auto uid = event->uid(); // b123456789@facebook.com
const auto id = uid.mid(1, uid.indexOf(QLatin1Char('@')) - 2); // 123456789
const auto id = uid.mid(1, uid.indexOf(QLatin1Char('@')) - 1); // 123456789
event->setDescription(QStringLiteral("https://www.facebook.com/%1").arg(id));
......
......@@ -106,29 +106,29 @@ Akonadi::Item EventsListJob::handleResponse(const QJsonObject &data)
const auto place = placeIt->toObject();
QStringList locationStr;
const auto placeEnd = place.constEnd();
auto it = place.constFind(QLatin1String("name"));
if (it != placeEnd) {
locationStr << it->toString();
const auto name = place.constFind(QLatin1String("name"));
if (name != placeEnd) {
locationStr << name->toString();
}
it = place.constFind(QLatin1String("location"));
if (it != placeEnd) {
auto location = it->toObject();
const auto locationIt = place.constFind(QLatin1String("location"));
if (locationIt != placeEnd) {
const auto location = locationIt->toObject();
for (const auto &loc : { QLatin1String("street"), QLatin1String("city"),
QLatin1String("zip"), QLatin1String("country") }) {
it = place.constFind(loc);
const auto it = location.constFind(loc);
if (it != placeEnd) {
locationStr << it->toString();
}
}
// no name, no address, try GPS coordinates
if (locationStr.size() < 1) {
it = place.constFind(QLatin1String("longitude"));
if (it != placeEnd) {
event->setGeoLongitude(it->toDouble());
const auto longitude = place.constFind(QLatin1String("longitude"));
if (longitude != placeEnd) {
event->setGeoLongitude(longitude->toDouble());
}
it = place.constFind(QLatin1String("latitude"));
if (it != placeEnd) {
event->setGeoLatitude(it->toDouble());
const auto latitude = place.constFind(QLatin1String("latitude"));
if (latitude != placeEnd) {
event->setGeoLatitude(latitude->toDouble());
}
}
}
......@@ -140,16 +140,16 @@ Akonadi::Item EventsListJob::handleResponse(const QJsonObject &data)
const QString dtStart = data.value(QLatin1String("start_time")).toString();
event->setDtStart(KDateTime(parseDateTime(dtStart)));
auto it = data.constFind(QLatin1String("end_time"));
if (it != dataEnd) {
event->setDtEnd(KDateTime(parseDateTime(it->toString())));
const auto endTime = data.constFind(QLatin1String("end_time"));
if (endTime != dataEnd) {
event->setDtEnd(KDateTime(parseDateTime(endTime->toString())));
}
QString description = data.value(QLatin1String("description")).toString();
description += QStringLiteral("\n\nhttps://www.facebook.com/events/%1").arg(data.value(QLatin1String("id")).toString());
event->setDescription(description);
auto status = parseStatus(data);
const auto status = parseStatus(data);
event->setStatus(status);
if (status == KCalCore::Incidence::StatusCanceled) {
event->setTransparency(KCalCore::Event::Transparent);
......
......@@ -44,6 +44,12 @@ Akonadi::Collection ListJob::collection() const
return mCollection;
}
void ListJob::emitError(const QString &errorText)
{
setError(KJob::UserDefinedError);
setErrorText(errorText);
emitResult();
}
void ListJob::setRequest(const QString &endpoint, const QStringList &fields,
const QMap<QString,QString> &queries)
......@@ -64,9 +70,7 @@ void ListJob::tokenJobResult(KJob *job)
{
auto tokenJob = qobject_cast<GetTokenJob*>(job);
if (tokenJob->error()) {
setError(tokenJob->error());
setErrorText(tokenJob->errorText());
emitResult();
emitError(tokenJob->errorText());
return;
}
......@@ -84,9 +88,7 @@ void ListJob::sendRequest(const QUrl &url)
void ListJob::onGraphResponseReceived(KJob *job)
{
if (job->error()) {
setError(job->error());
setErrorText(job->errorText());
emitResult();
emitError(job->errorText());
return;
}
......@@ -97,9 +99,7 @@ void ListJob::onGraphResponseReceived(KJob *job)
auto json = QJsonDocument::fromJson(tjob->data(), &error);
if (error.error) {
qCWarning(RESOURCE_LOG) << "JSON parsing error" << error.error << ", offset" << error.offset;
setError(KJob::UserDefinedError);
setErrorText(i18n("Invalid response from server: JSON parsing error"));
emitResult();
emitError(i18n("Invalid response from server: JSON parsing error"));
return;
}
......@@ -115,9 +115,7 @@ void ListJob::onGraphResponseReceived(KJob *job)
connect(tokenJob, &LoginJob::result,
this, [this, tokenJob, url]() {
if (tokenJob->error()) {
setError(tokenJob->error());
setErrorText(tokenJob->errorText());
emitResult();
emitError(tokenJob->errorText());
return;
}
......@@ -127,12 +125,10 @@ void ListJob::onGraphResponseReceived(KJob *job)
sendRequest(url_);
});
tokenJob->start();
} else {
setError(KJob::UserDefinedError);
setErrorText(err.value(QLatin1String("message")).toString());
emitResult();
return;
}
emitError(err.value(QLatin1String("message")).toString());
return;
}
......@@ -149,9 +145,9 @@ void ListJob::onGraphResponseReceived(KJob *job)
Q_EMIT itemsAvailable(this, items, {});
const auto paging = obj.value(QLatin1String("paging")).toObject();
const auto nextIt = paging.constFind(QLatin1String("next"));
if (nextIt != paging.constEnd()) {
sendRequest(QUrl(nextIt->toString()));
const auto next = paging.constFind(QLatin1String("next"));
if (next != paging.constEnd()) {
sendRequest(QUrl(next->toString()));
} else {
emitResult();
}
......
......@@ -47,6 +47,8 @@ protected:
virtual Akonadi::Item handleResponse(const QJsonObject &data) = 0;
void emitError(const QString &errorString);
Q_SIGNALS:
void itemsAvailable(KJob *self, const Akonadi::Item::List &items, QPrivateSignal);
......
......@@ -76,7 +76,6 @@ public:
explicit WebView(QWidget *parent = nullptr)
: QWebEngineView(parent)
{
QWebEngineProfile::defaultProfile()->setPersistentCookiesPolicy(QWebEngineProfile::NoPersistentCookies);
}
void contextMenuEvent(QContextMenuEvent *e) override
......@@ -165,7 +164,7 @@ public:
cookieStore->deleteAllCookies(); // delete all cookies from it
const auto parsedCookies = QNetworkCookie::parseCookies(cookies);
for (const auto &parsedCookie : parsedCookies) {
cookieStore->setCookie(parsedCookie, QUrl(QStringLiteral(".facebook.com")));
cookieStore->setCookie(parsedCookie, QUrl(QStringLiteral("https://www.facebook.com")));
mCookies.insert(parsedCookie.name(), parsedCookie.toRawForm());
}
connect(cookieStore, &QWebEngineCookieStore::cookieAdded,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment