root / kadu-core / buddies / avatar-manager.cpp @ f38188d2
History | View | Annotate | Download (4.5 kB)
| 1 | /*
|
|---|---|
| 2 | * %kadu copyright begin% |
| 3 | * Copyright 2009, 2009, 2010 RafaĆ Malinowski (rafal.przemyslaw.malinowski@gmail.com) |
| 4 | * %kadu copyright end% |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; either version 2 of |
| 9 | * the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include <QtCore/QFile> |
| 21 | #include <QtCore/QTimer> |
| 22 | |
| 23 | #include "accounts/account.h" |
| 24 | #include "buddies/avatar.h" |
| 25 | #include "buddies/avatar-shared.h" |
| 26 | #include "configuration/configuration-manager.h" |
| 27 | #include "contacts/contact-manager.h" |
| 28 | #include "contacts/contact.h" |
| 29 | #include "misc/misc.h" |
| 30 | #include "protocols/protocol.h" |
| 31 | #include "protocols/services/avatar-service.h" |
| 32 | #include "debug.h" |
| 33 | |
| 34 | #include "avatar-manager.h" |
| 35 | |
| 36 | AvatarManager * AvatarManager::Instance = 0;
|
| 37 | |
| 38 | AvatarManager * AvatarManager::instance() |
| 39 | {
|
| 40 | if (!Instance)
|
| 41 | Instance = new AvatarManager();
|
| 42 | |
| 43 | return Instance;
|
| 44 | } |
| 45 | |
| 46 | AvatarManager::AvatarManager() |
| 47 | {
|
| 48 | triggerAllAccountsRegistered(); |
| 49 | |
| 50 | UpdateTimer = new QTimer(this); |
| 51 | UpdateTimer->setInterval(5 * 60 * 1000); // 5 minutes |
| 52 | connect(UpdateTimer, SIGNAL(timeout()), this, SLOT(updateAvatars()));
|
| 53 | } |
| 54 | |
| 55 | AvatarManager::~AvatarManager() |
| 56 | {
|
| 57 | triggerAllAccountsUnregistered(); |
| 58 | } |
| 59 | |
| 60 | void AvatarManager::itemAboutToBeAdded(Avatar item)
|
| 61 | {
|
| 62 | emit avatarAboutToBeAdded(item); |
| 63 | } |
| 64 | |
| 65 | void AvatarManager::itemAdded(Avatar item)
|
| 66 | {
|
| 67 | emit avatarAdded(item); |
| 68 | } |
| 69 | |
| 70 | void AvatarManager::itemAboutToBeRemoved(Avatar item)
|
| 71 | {
|
| 72 | emit avatarAboutToBeRemoved(item); |
| 73 | } |
| 74 | |
| 75 | void AvatarManager::itemRemoved(Avatar item)
|
| 76 | {
|
| 77 | emit avatarRemoved(item); |
| 78 | } |
| 79 | |
| 80 | AvatarService * AvatarManager::avatarService(Account account) |
| 81 | {
|
| 82 | Protocol *protocol = account.protocolHandler(); |
| 83 | if (!protocol)
|
| 84 | return 0; |
| 85 | |
| 86 | return protocol->avatarService();
|
| 87 | } |
| 88 | |
| 89 | AvatarService * AvatarManager::avatarService(Contact contact) |
| 90 | {
|
| 91 | Account account = contact.contactAccount(); |
| 92 | if (account.isNull())
|
| 93 | return 0; |
| 94 | |
| 95 | return avatarService(account);
|
| 96 | } |
| 97 | |
| 98 | void AvatarManager::accountRegistered(Account account)
|
| 99 | {
|
| 100 | connect(account, SIGNAL(connected()), this, SLOT(updateAccountAvatars()));
|
| 101 | |
| 102 | AvatarService *service = avatarService(account); |
| 103 | if (!service)
|
| 104 | return;
|
| 105 | |
| 106 | connect(service, SIGNAL(avatarFetched(Contact, const QByteArray &)),
|
| 107 | this, SLOT(avatarFetched(Contact, const QByteArray &))); |
| 108 | } |
| 109 | |
| 110 | void AvatarManager::accountUnregistered(Account account)
|
| 111 | {
|
| 112 | disconnect(account, SIGNAL(connected()), this, SLOT(updateAccountAvatars()));
|
| 113 | |
| 114 | AvatarService *service = avatarService(account); |
| 115 | if (!service)
|
| 116 | return;
|
| 117 | |
| 118 | disconnect(service, SIGNAL(avatarFetched(Contact, const QByteArray &)),
|
| 119 | this, SLOT(avatarFetched(Contact, const QByteArray &))); |
| 120 | } |
| 121 | |
| 122 | bool AvatarManager::needUpdate(Contact contact)
|
| 123 | {
|
| 124 | if (!contact.contactAvatar())
|
| 125 | return true; |
| 126 | |
| 127 | QDateTime lastUpdated = contact.contactAvatar().lastUpdated(); |
| 128 | if (!lastUpdated.isValid())
|
| 129 | return true; |
| 130 | // one hour passed
|
| 131 | if (lastUpdated.secsTo(QDateTime::currentDateTime()) > 60 * 60) |
| 132 | return true; |
| 133 | |
| 134 | QDateTime nextUpdate = contact.contactAvatar().nextUpdate(); |
| 135 | if (nextUpdate > QDateTime::currentDateTime())
|
| 136 | return true; |
| 137 | |
| 138 | return false; |
| 139 | } |
| 140 | #include <stdio.h> |
| 141 | void AvatarManager::updateAvatar(Contact contact, bool force) |
| 142 | {
|
| 143 | if (!force && !needUpdate(contact))
|
| 144 | return;
|
| 145 | |
| 146 | AvatarService *service = avatarService(contact); |
| 147 | if (!service)
|
| 148 | return;
|
| 149 | |
| 150 | service->fetchAvatar(contact); |
| 151 | } |
| 152 | |
| 153 | void AvatarManager::avatarFetched(Contact contact, const QByteArray &data) |
| 154 | {
|
| 155 | Avatar avatar = contact.contactAvatar(); |
| 156 | if (!avatar)
|
| 157 | {
|
| 158 | avatar = Avatar::create(); |
| 159 | contact.setContactAvatar(avatar); |
| 160 | } |
| 161 | |
| 162 | avatar.setLastUpdated(QDateTime::currentDateTime()); |
| 163 | |
| 164 | QPixmap pixmap; |
| 165 | if (!data.isEmpty())
|
| 166 | pixmap.loadFromData(data); |
| 167 | |
| 168 | avatar.setPixmap(pixmap); |
| 169 | |
| 170 | emit avatarUpdated(contact); |
| 171 | } |
| 172 | |
| 173 | void AvatarManager::updateAvatars()
|
| 174 | {
|
| 175 | foreach (Contact contact, ContactManager::instance()->items()) |
| 176 | if (!contact.ownerBuddy().isAnonymous())
|
| 177 | updateAvatar(contact); |
| 178 | } |
| 179 | |
| 180 | void AvatarManager::updateAccountAvatars()
|
| 181 | {
|
| 182 | Account account(sender()); |
| 183 | if (!account)
|
| 184 | return;
|
| 185 | |
| 186 | foreach (Contact contact, ContactManager::instance()->contacts(account)) |
| 187 | if (!contact.ownerBuddy().isAnonymous())
|
| 188 | updateAvatar(contact, true);
|
| 189 | } |