Commit 12219a7c authored by Rafał Malinowski's avatar Rafał Malinowski

facebook: download contact avatars

parent f3b8bd27
......@@ -61,7 +61,9 @@ set (SOURCES
qthrift/qthrift-writer.cpp
qthrift/qthrift.cpp
services/facebook-avatar-downloader.cpp
services/facebook-chat-service.cpp
services/facebook-contact-avatar-service.cpp
services/facebook-roster-service.cpp
widgets/facebook-add-account-widget.cpp
......
......@@ -22,8 +22,10 @@
#include "qfacebook/session/qfacebook-session.h"
#include "services/facebook-chat-service.h"
#include "services/facebook-contact-avatar-service.h"
#include "services/facebook-roster-service.h"
#include "avatars/aggregated-contact-avatar-service.h"
#include "chat/chat-service-repository.h"
#include "contacts/contact-manager.h"
#include "plugin/plugin-injected-factory.h"
......@@ -35,9 +37,15 @@ FacebookServices::FacebookServices(Account account, std::unique_ptr<QFacebookSes
FacebookServices::~FacebookServices()
{
m_aggregatedContactAvatarService->remove(m_contactAvatarService.get());
m_chatServiceRepository->removeChatService(m_chatService.get());
}
void FacebookServices::setAggregatedContactAvatarService(AggregatedContactAvatarService *aggregatedContactAvatarService)
{
m_aggregatedContactAvatarService = aggregatedContactAvatarService;
}
void FacebookServices::setChatServiceRepository(ChatServiceRepository *chatServiceRepository)
{
m_chatServiceRepository = chatServiceRepository;
......@@ -55,10 +63,15 @@ void FacebookServices::setPluginInjectedFactory(PluginInjectedFactory *pluginInj
void FacebookServices::init()
{
m_contactAvatarService = m_pluginInjectedFactory->makeUnique<FacebookContactAvatarService>(m_account);
m_aggregatedContactAvatarService->add(m_contactAvatarService.get());
m_chatService = m_pluginInjectedFactory->makeUnique<FacebookChatService>(m_account, *m_session);
m_chatServiceRepository->addChatService(m_chatService.get());
auto contacts = m_contactManager->contacts(m_account, ContactManager::ExcludeAnonymous);
m_rosterService = m_pluginInjectedFactory->makeUnique<FacebookRosterService>(
std::move(contacts), *m_account.protocolHandler(), *m_session);
connect(m_rosterService.get(), &FacebookRosterService::added, m_contactAvatarService.get(), &FacebookContactAvatarService::contactAdded);
}
......@@ -28,9 +28,11 @@
#include <memory>
class FacebookChatService;
class FacebookContactAvatarService;
class FacebookRosterService;
class QFacebookSession;
class AggregatedContactAvatarService;
class ChatServiceRepository;
class ContactManager;
class PluginInjectedFactory;
......@@ -44,6 +46,7 @@ public:
virtual ~FacebookServices();
private:
QPointer<AggregatedContactAvatarService> m_aggregatedContactAvatarService;
QPointer<ChatServiceRepository> m_chatServiceRepository;
QPointer<ContactManager> m_contactManager;
QPointer<PluginInjectedFactory> m_pluginInjectedFactory;
......@@ -51,9 +54,11 @@ private:
Account m_account;
std::unique_ptr<QFacebookSession> m_session;
std::unique_ptr<FacebookChatService> m_chatService;
std::unique_ptr<FacebookContactAvatarService> m_contactAvatarService;
std::unique_ptr<FacebookRosterService> m_rosterService;
private slots:
INJEQT_SET void setAggregatedContactAvatarService(AggregatedContactAvatarService *aggregatedContactAvatarService);
INJEQT_SET void setChatServiceRepository(ChatServiceRepository *chatServiceRepository);
INJEQT_SET void setContactManager(ContactManager *contactManager);
INJEQT_SET void setPluginInjectedFactory(PluginInjectedFactory *pluginInjectedFactory);
......
/*
* %kadu copyright begin%
* Copyright 2017 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com)
* %kadu copyright end%
*
* 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 General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "facebook-avatar-downloader.h"
#include "facebook-avatar-downloader.moc"
#include <QtGui/QImage>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkRequest>
FacebookAvatarDownloader::FacebookAvatarDownloader(ContactAvatarId id, QObject *parent)
: QObject{parent}, m_reply{}, m_redirectCount{0}, m_id{std::move(id)}
{
m_nam = make_owned<QNetworkAccessManager>(this);
fetch(m_id.id);
}
FacebookAvatarDownloader::~FacebookAvatarDownloader()
{
}
void FacebookAvatarDownloader::done(QByteArray avatar)
{
emit downloaded(m_id, std::move(avatar));
deleteLater();
}
void FacebookAvatarDownloader::failed()
{
deleteLater();
}
void FacebookAvatarDownloader::requestFinished()
{
auto redirect = m_reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
m_reply->deleteLater();
if (redirect.isNull())
{
parseReply();
return;
}
if (m_redirectCount > 5)
{
failed();
return;
}
m_redirectCount++;
fetch(redirect.toString());
}
void FacebookAvatarDownloader::fetch(const QString &url)
{
QNetworkRequest request;
request.setUrl(url);
m_reply = m_nam->get(request);
connect(m_reply, SIGNAL(finished()), this, SLOT(requestFinished()));
}
void FacebookAvatarDownloader::parseReply()
{
done(m_reply->readAll());
}
/*
* %kadu copyright begin%
* Copyright 2017 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com)
* %kadu copyright end%
*
* 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 General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "avatars/contact-avatar-id.h"
#include "misc/memory.h"
#include <QtCore/QObject>
class QNetworkAccessManager;
class QNetworkReply;
class FacebookAvatarDownloader : public QObject
{
Q_OBJECT
public:
explicit FacebookAvatarDownloader(ContactAvatarId id, QObject *parent = nullptr);
virtual ~FacebookAvatarDownloader();
signals:
void downloaded(const ContactAvatarId &id, const QByteArray &content);
private:
owned_qptr<QNetworkAccessManager> m_nam;
owned_qptr<QNetworkReply> m_reply;
int m_redirectCount;
ContactAvatarId m_id;
void done(QByteArray avatar);
void failed();
void fetch(const QString &url);
void parseReply();
private slots:
void requestFinished();
};
/*
* %kadu copyright begin%
* Copyright 2017 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com)
* %kadu copyright end%
*
* 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 General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "facebook-contact-avatar-service.h"
#include "facebook-contact-avatar-service.moc"
#include "qfacebook/qfacebook-contact.h"
#include "services/facebook-avatar-downloader.h"
#include "avatars/contact-avatar-id.h"
#include "contacts/contact-id.h"
FacebookContactAvatarService::FacebookContactAvatarService(Account account, QObject *parent)
: ContactAvatarService{account, parent}
{
}
FacebookContactAvatarService::~FacebookContactAvatarService() = default;
void FacebookContactAvatarService::download(const ContactAvatarId &id)
{
auto avatarDownloader = make_owned<FacebookAvatarDownloader>(id, this);
connect(avatarDownloader, &FacebookAvatarDownloader::downloaded, this, &FacebookContactAvatarService::downloaded);
}
void FacebookContactAvatarService::contactAdded(const QFacebookContact &c)
{
printf("contact added!\n");
emit available({{c.id()}, c.avatarUrl()});
}
/*
* %kadu copyright begin%
* Copyright 2017 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com)
* %kadu copyright end%
*
* 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 General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "avatars/contact-avatar-service.h"
class QFacebookContact;
class FacebookContactAvatarService : public ContactAvatarService
{
Q_OBJECT
public:
explicit FacebookContactAvatarService(Account account, QObject *parent = nullptr);
virtual ~FacebookContactAvatarService();
virtual void download(const ContactAvatarId &id) override;
void contactAdded(const QFacebookContact &c);
};
......@@ -160,6 +160,7 @@ void FacebookRosterService::addContact(const QFacebookContact &c)
contact.rosterEntry()->setSynchronized();
RosterService::addContact(contact);
emit added(c);
}
void FacebookRosterService::removeContact(const QString &id)
......
......@@ -43,6 +43,9 @@ public:
explicit FacebookRosterService(QVector<Contact> contacts, Protocol &protocol, QFacebookSession &facebookSession);
virtual ~FacebookRosterService();
signals:
void added(const QFacebookContact &c);
private:
QPointer<BuddyManager> m_buddyManager;
QPointer<ContactManager> m_contactManager;
......
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