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
|
/****************************************************************************
* Copyright (c) 2015 Anthony Vital <anthony.vital@gmail.com> *
* *
* This file is part of Gmail Feed. *
* *
* Gmail Feed 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 3 of the License, or *
* (at your option) any later version. *
* *
* Gmail Feed 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 General Public License *
* along with Gmail Feed. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************/
#include "account.h"
#include <QNetworkRequest>
#include <QNetworkReply>
#include <KAccounts/Core>
#include <KAccounts/GetCredentialsJob>
#include <Accounts/Manager>
Account::Account(QObject *parent)
: QObject(parent)
, m_id(0)
, m_isConfigured(false)
{
}
Account::~Account()
{
}
void Account::updateFeed()
{
if (!m_isConfigured) {
qWarning()<<"No Gmail account configured";
return;
}
auto job = new KAccounts::GetCredentialsJob(m_id, this);
connect(job, &KAccounts::GetCredentialsJob::result, this, &Account::credentialsReceived);
job->start();
}
void Account::credentialsReceived(KJob *job)
{
KAccounts::GetCredentialsJob* credentials = qobject_cast<KAccounts::GetCredentialsJob*>(job);
if (credentials->error()) {
qWarning() << "Couldn't fetch credentials";
return;
}
auto accessToken = credentials->credentialsData()[QStringLiteral("AccessToken")].toByteArray();
QNetworkRequest req(QUrl("https://mail.google.com/mail/feed/atom"));
req.setRawHeader("Authorization", "Bearer "+accessToken);
auto reply = m_networkManager.get(req);
connect(reply, &QNetworkReply::finished, this, &Account::newData);
}
void Account::newData()
{
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
if (reply->error()) {
qWarning() << "couldn't read data" << reply->readAll();
return;
}
m_feed = QString(reply->readAll());
Q_EMIT(feedChanged());
}
void Account::setId(int id)
{
if (id == 0) {
m_id = 0;
m_name.clear();
m_isConfigured = false;
Q_EMIT(isConfiguredChanged());
Q_EMIT(idChanged());
qWarning() << "account id set to 0.";
return;
}
auto account = KAccounts::accountsManager()->account(qint32(id));
if (!account) {
qWarning() << "account id " << qint32(id) << "doesn't exist";
return;
}
m_id = qint32(id);
m_name = account->displayName();
m_isConfigured = true;
Q_EMIT(isConfiguredChanged());
Q_EMIT(idChanged());
}
|