Skip to content

Commit

Permalink
Merged in JB7262 (pull request #24)
Browse files Browse the repository at this point in the history
[buteo-social] Port accounts to use new accounts API. Fixes JB#7262
  • Loading branch information
Raine Makelainen committed Jun 28, 2013
2 parents 882c5ca + c305396 commit 0be7b19
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 233 deletions.
1 change: 1 addition & 0 deletions rpm/sociald.spec
Expand Up @@ -15,6 +15,7 @@ BuildRequires: pkgconfig(mlite5)
BuildRequires: pkgconfig(libsignon-qt5)
BuildRequires: pkgconfig(accounts-qt5)
BuildRequires: pkgconfig(libsailfishkeyprovider)
BuildRequires: pkgconfig(sailfishaccounts)
BuildRequires: nemo-qml-plugin-notifications-qt5-devel
BuildRequires: eventfeed-devel
BuildRequires: libmeegotouchevents-qt5-devel
Expand Down
165 changes: 75 additions & 90 deletions src/facebook/facebookdatatypesyncadaptor.cpp
Expand Up @@ -16,24 +16,22 @@

#include <QJsonDocument>

//libaccounts-qt
#include <Accounts/Manager>
#include <Accounts/Service>
#include <Accounts/Account>
#include <Accounts/AccountService>
#include <Accounts/AuthData>

//libsignon-qt
#include <SignOn/Identity>
#include <SignOn/SessionData>
#include <SignOn/AuthSession>
// sailfish-components-accounts-qt5
#include <accountmanager.h>
#include <account.h>
#include <signinparameters.h>

//libsailfishkeyprovider
#include <sailfishkeyprovider.h>

Q_DECLARE_METATYPE(SignOn::Identity*)
//libsignon-qt: SignOn::NoUserInteractionPolicy
#include <SignOn/SessionData>

FacebookDataTypeSyncAdaptor::FacebookDataTypeSyncAdaptor(SyncService *syncService, SyncService::DataType dataType, QObject *parent)
: SocialNetworkSyncAdaptor("facebook", syncService, parent)
, m_dataType(dataType)
{
m_validClientId = initializeClientId();
}

FacebookDataTypeSyncAdaptor::~FacebookDataTypeSyncAdaptor()
Expand All @@ -46,6 +44,13 @@ void FacebookDataTypeSyncAdaptor::sync(const QString &dataType)
TRACE(SOCIALD_ERROR,
QString(QLatin1String("error: facebook %1 sync adaptor was asked to sync %2"))
.arg(SyncService::dataType(m_dataType)).arg(dataType));
changeStatus(SocialNetworkSyncAdaptor::Error);
return;
}

if (!m_validClientId) {
TRACE(SOCIALD_ERROR, QString(QLatin1String("error: client id couldn't be retrieved for facebook")));
changeStatus(SocialNetworkSyncAdaptor::Error);
return;
}

Expand All @@ -69,96 +74,47 @@ void FacebookDataTypeSyncAdaptor::sync(const QString &dataType)
void FacebookDataTypeSyncAdaptor::updateDataForAccounts(const QList<int> &accountIds)
{
foreach (int accountId, accountIds) {
Accounts::Account *act = m_accountManager->account(accountId);
if (!act) {
Account *account = m_accountManager->account(accountId);
if (!account) {
TRACE(SOCIALD_ERROR,
QString(QLatin1String("error: existing account with id %1 couldn't be retrieved"))
.arg(accountId));
QString(QLatin1String("error: existing account with id %1 couldn't be retrieved"))
.arg(accountId));
continue;
}

// grab out a valid identity for the sync service.
Accounts::ServiceList enabledSrvs = act->enabledServices();
if (!enabledSrvs.size()) {
TRACE(SOCIALD_INFORMATION,
QString(QLatin1String("account with id %1 has no enabled sync service"))
.arg(accountId));
continue;
}

quint32 identityId = 0;
Accounts::AccountService *asrv = 0;
for (int i = 0; i < enabledSrvs.size(); ++i) {
asrv = new Accounts::AccountService(act, enabledSrvs.at(i));
if (!asrv) {
continue;
}
identityId = asrv->authData().credentialsId();
if (identityId != 0) {
break;
}

asrv->deleteLater();
asrv = 0;
}

if (identityId == 0) {
TRACE(SOCIALD_INFORMATION,
QString(QLatin1String("account with id %1 has no valid credentials"))
.arg(accountId));
continue;
}

SignOn::Identity *ident = SignOn::Identity::existingIdentity(identityId);
if (!ident) {
TRACE(SOCIALD_ERROR,
QString(QLatin1String("error: credentials for account with id %1 couldn't be retrieved"))
.arg(accountId));
continue;
if (account->status() == Account::Initialized || account->status() == Account::Synced) {
signIn(account);
} else {
connect(account, SIGNAL(statusChanged()), this, SLOT(accountStatusChangeHandler()));
}
}
}

// we need the access token to perform requests.
// set UiPolicy to NO_USER_INTERACTION because we don't want
// to show any UI if we don't already have a token.
Accounts::AuthData authData(asrv->authData());
asrv->deleteLater();
SignOn::AuthSession *session = ident->createSession(authData.method());
QVariantMap sessionData = authData.parameters();
sessionData.insert(QLatin1String("UiPolicy"), SignOn::NoUserInteractionPolicy);
QVariant identVar = QVariant::fromValue<SignOn::Identity*>(ident);
session->setProperty("ident", identVar);
session->setProperty("accountId", accountId);
connect(session, SIGNAL(error(SignOn::Error)), this, SLOT(signOnError(SignOn::Error)));
connect(session, SIGNAL(response(SignOn::SessionData)), this, SLOT(signOnResponse(SignOn::SessionData)));
session->process(sessionData, authData.mechanism());
void FacebookDataTypeSyncAdaptor::accountStatusChangeHandler()
{
Account *account = qobject_cast<Account*>(sender());
if (account->status() == Account::Initialized || account->status() == Account::Synced)
{
// Not anymore interested about status changes of this account instance
account->disconnect(this);
signIn(account);
}
}

void FacebookDataTypeSyncAdaptor::signOnError(const SignOn::Error &err)
void FacebookDataTypeSyncAdaptor::signOnError(const QString &err)
{
SignOn::AuthSession *session = qobject_cast<SignOn::AuthSession *>(sender());
Account *account = qobject_cast<Account*>(sender());
TRACE(SOCIALD_ERROR,
QString(QLatin1String("error: credentials for account with id %1 couldn't be retrieved:"))
.arg(session->property("accountId").toInt()) << err.message());
SignOn::Identity *ident = session->property("ident").value<SignOn::Identity*>();
ident->destroySession(session); // XXX: is this safe? Does it deleteLater()?
ident->deleteLater();

.arg(account->identifier()) << err);
account->disconnect(this);
changeStatus(SocialNetworkSyncAdaptor::Error);
}

void FacebookDataTypeSyncAdaptor::signOnResponse(const SignOn::SessionData &sdata)
void FacebookDataTypeSyncAdaptor::signOnResponse(const QVariantMap &data)
{
QVariantMap data;
QStringList sdpns = sdata.propertyNames();
foreach (const QString &sdpn, sdpns) {
data.insert(sdpn, sdata.getProperty(sdpn));
}

QString accessToken;
SignOn::AuthSession *session = qobject_cast<SignOn::AuthSession *>(sender());
int accountId = static_cast<int>(session->property("accountId").toUInt());

Account *account = qobject_cast<Account*>(sender());
int accountId = account->identifier();
if (data.contains(QLatin1String("AccessToken"))) {
accessToken = data.value(QLatin1String("AccessToken")).toString();
TRACE(SOCIALD_DEBUG,
Expand All @@ -170,10 +126,7 @@ void FacebookDataTypeSyncAdaptor::signOnResponse(const SignOn::SessionData &sdat
.arg(accountId));
}

SignOn::Identity *ident = session->property("ident").value<SignOn::Identity*>();
ident->destroySession(session); // XXX: is this safe? Does it deleteLater()?
ident->deleteLater();

account->disconnect(this);
if (!accessToken.isEmpty()) {
beginSync(accountId, accessToken); // call the derived-class sync entrypoint.
}
Expand Down Expand Up @@ -216,3 +169,35 @@ QVariantMap FacebookDataTypeSyncAdaptor::parseReplyData(const QByteArray &replyD
*ok = false;
return QVariantMap();
}

bool FacebookDataTypeSyncAdaptor::initializeClientId()
{
char *cClientId = NULL;
int cSuccess = SailfishKeyProvider_storedKey("facebook", "facebook-sync", "client_id", &cClientId);
if (cSuccess != 0 || cClientId == NULL) {
return false;
}

m_clientId = QLatin1String(cClientId);
free(cClientId);
return true;
}

void FacebookDataTypeSyncAdaptor::signIn(Account *account)
{
// grab out a valid identity for the sync service.
if (!account->isEnabledWithService("facebook-sync")) {
TRACE(SOCIALD_INFORMATION,
QString(QLatin1String("account with id %1 has no enabled facebook sync service"))
.arg(account->identifier()));
return;
}

SignInParameters *sip = account->signInParameters("facebook-sync");
sip->setParameter(QLatin1String("ClientId"), m_clientId);
sip->setParameter(QLatin1String("UiPolicy"), SignOn::NoUserInteractionPolicy);

connect(account, SIGNAL(signInError(QString)), this, SLOT(signOnError(QString)));
connect(account, SIGNAL(signInResponse(QVariantMap)), this, SLOT(signOnResponse(QVariantMap)));
account->signIn("Jolla", "Jolla", sip);
}
20 changes: 12 additions & 8 deletions src/facebook/facebookdatatypesyncadaptor.h
Expand Up @@ -16,12 +16,7 @@
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QSslError>

//libaccounts-qt
#include <Accounts/Manager>

//libsignon-qt
#include <SignOn/SessionData>
#include <SignOn/Error>
class Account;

/*
Abstract interface for all of the data-specific sync adaptors
Expand Down Expand Up @@ -49,8 +44,17 @@ protected Q_SLOTS:
virtual void sslErrorsHandler(const QList<QSslError> &errs);

private Q_SLOTS:
void signOnError(const SignOn::Error &err);
void signOnResponse(const SignOn::SessionData &sdata);
void accountStatusChangeHandler();
void signOnError(const QString &message);
void signOnResponse(const QVariantMap &data);

private:
bool initializeClientId();
void signIn(Account *account);

private:
QString m_clientId;
bool m_validClientId;
};

#endif // FACEBOOKDATATYPESYNCADAPTOR_H
4 changes: 0 additions & 4 deletions src/facebook/facebooknotificationsyncadaptor.h
Expand Up @@ -24,10 +24,6 @@
#include <QtContacts/QContactFetchHint>
#include <QtContacts/QContact>

//libsignon-qt
#include <SignOn/SessionData>
#include <SignOn/Error>

class MEventFeed;

USE_CONTACTS_NAMESPACE
Expand Down
4 changes: 0 additions & 4 deletions src/facebook/facebookpostsyncadaptor.h
Expand Up @@ -22,10 +22,6 @@
#include <QtContacts/QContact>
#include <QtContacts/QContactFetchRequest>

//libsignon-qt
#include <SignOn/SessionData>
#include <SignOn/Error>

class MEventFeed;

USE_CONTACTS_NAMESPACE
Expand Down
14 changes: 8 additions & 6 deletions src/socialnetworksyncadaptor.cpp
Expand Up @@ -15,14 +15,15 @@

#include <QtNetwork/QNetworkAccessManager>

//libaccounts-qt
#include <Accounts/Manager>
// sailfish-components-accounts-qt5
#include <accountmanager.h>
#include <account.h>

SocialNetworkSyncAdaptor::SocialNetworkSyncAdaptor(QString serviceName, SyncService *syncService, QObject *parent)
: QObject(parent)
, m_status(SocialNetworkSyncAdaptor::Invalid)
, m_serviceName(serviceName)
, m_accountManager(new Accounts::Manager(QLatin1String("sync"), this))
, m_accountManager(new AccountManager(this))
, m_qnam(new QNetworkAccessManager(this))
, q(syncService)
{
Expand Down Expand Up @@ -69,14 +70,15 @@ void SocialNetworkSyncAdaptor::checkAccounts(SyncService::DataType dataType, QLi
}
}

Accounts::AccountIdList currentIds = m_accountManager->accountList();
QList<int> currentIds = m_accountManager->accountIdentifiers();
TRACE(SOCIALD_DEBUG,
QString(QLatin1String("have found %1 accounts which support a sync service; determining old/new/update sets..."))
.arg(currentIds.size()));
for (int i = 0; i < currentIds.size(); ++i) {
int currId = currentIds.at(i);
Accounts::Account *act = m_accountManager->account(currId);
if (!act || act->providerName() != m_serviceName) {
Account *act = m_accountManager->account(currId);
if (!act || !(act->supportedServiceNames().size() > 0 &&
act->supportedServiceNames().at(0).startsWith(m_serviceName))) {
continue; // not same account as m_serviceName. Ignore it.
}

Expand Down
4 changes: 2 additions & 2 deletions src/socialnetworksyncadaptor.h
Expand Up @@ -17,7 +17,7 @@
class QSqlDatabase;
class SyncService;
class QNetworkAccessManager;
namespace Accounts { class Manager; }
class AccountManager;

class SocialNetworkSyncAdaptor : public QObject
{
Expand Down Expand Up @@ -63,7 +63,7 @@ class SocialNetworkSyncAdaptor : public QObject
Status m_status;
bool m_enabled;
QString m_serviceName;
Accounts::Manager *m_accountManager;
AccountManager *m_accountManager;
QNetworkAccessManager *m_qnam;

private:
Expand Down
8 changes: 6 additions & 2 deletions src/src.pro
Expand Up @@ -5,15 +5,19 @@ VERSION = 0.0.1
CONFIG += link_pkgconfig plugin

CONFIG += meegotouchevents-qt5 eventfeed-qt5
PKGCONFIG += Qt5Contacts accounts-qt5 libsignon-qt5 nemonotifications-qt5 buteosyncfw5
PKGCONFIG += Qt5Contacts \
libsailfishkeyprovider \
sailfishaccounts \
libsignon-qt5 \
nemonotifications-qt5 \
buteosyncfw5

DEFINES *= USING_QTPIM
DEFINES *= BEGIN_CONTACTS_NAMESPACE=QT_BEGIN_NAMESPACE_CONTACTS
DEFINES *= END_CONTACTS_NAMESPACE=QT_END_NAMESPACE_CONTACTS
DEFINES *= USE_CONTACTS_NAMESPACE=QTCONTACTS_USE_NAMESPACE

HEADERS += constants_p.h
PKGCONFIG += libsailfishkeyprovider

target.path += /usr/lib/buteo-plugins-qt5

Expand Down

0 comments on commit 0be7b19

Please sign in to comment.