Skip to content

Commit

Permalink
Merge branch 'jb22610-avatars' into 'master'
Browse files Browse the repository at this point in the history
[buteo-sync-plugins-social] Delay downloading of avatars. JB#22610

See merge request mer-core/buteo-sync-plugins-social!74
  • Loading branch information
blam committed Oct 29, 2020
2 parents 16ac498 + c9be2d2 commit 8b6c9fa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/google/google-contacts/googletwowaycontactsyncadaptor.cpp
Expand Up @@ -41,6 +41,7 @@
#include <QtCore/QJsonArray>
#include <QtCore/QSettings>
#include <QtGui/QImageReader>
#include <QtCore/QTimer>

#include <QtContacts/QContactCollectionFilter>
#include <QtContacts/QContactIntersectionFilter>
Expand Down Expand Up @@ -610,6 +611,8 @@ void GoogleTwoWayContactSyncAdaptor::contactsFinishedHandler()

void GoogleTwoWayContactSyncAdaptor::continueSync(int accountId, const QString &accessToken, ContactChangeNotifier contactChangeNotifier)
{
Q_UNUSED(accessToken)

// early out in case we lost connectivity
if (syncAborted()) {
SOCIALD_LOG_ERROR("aborting sync of account" << accountId);
Expand All @@ -618,10 +621,6 @@ void GoogleTwoWayContactSyncAdaptor::continueSync(int accountId, const QString &
return;
}

// download avatars for new and modified contacts
transformContactAvatars(m_remoteAdds[accountId], accountId, accessToken);
transformContactAvatars(m_remoteMods[accountId], accountId, accessToken);

// now store the changes locally
SOCIALD_LOG_TRACE("storing remote changes locally for account" << accountId);

Expand All @@ -634,6 +633,10 @@ void GoogleTwoWayContactSyncAdaptor::continueSync(int accountId, const QString &
} else {
sqliteSync->remoteContactsDetermined(sqliteSync->m_collection, m_remoteAdds[accountId] + m_remoteMods[accountId]);
}

m_pendingAvatarRequests.append(accountId);
QTimer::singleShot(0, this, &GoogleTwoWayContactSyncAdaptor::delayedTransformContactAvatars);
incrementSemaphore(accountId);
}

void GoogleTwoWayContactSyncAdaptor::upsyncLocalChanges(const QDateTime &localSince,
Expand Down Expand Up @@ -923,6 +926,18 @@ bool GoogleTwoWayContactSyncAdaptor::queueAvatarForDownload(int accountId, const
return false;
}

void GoogleTwoWayContactSyncAdaptor::delayedTransformContactAvatars()
{
// download avatars for new and modified contacts
if (m_pendingAvatarRequests.count()) {
const int accountId = m_pendingAvatarRequests.takeLast();
transformContactAvatars(m_remoteAdds[accountId], accountId, m_accessTokens[accountId]);
transformContactAvatars(m_remoteMods[accountId], accountId, m_accessTokens[accountId]);

decrementSemaphore(accountId);
}
}

void GoogleTwoWayContactSyncAdaptor::transformContactAvatars(QList<QContact> &remoteContacts, int accountId, const QString &accessToken)
{
// The avatar detail from the remote contact will be of the form:
Expand Down
2 changes: 2 additions & 0 deletions src/google/google-contacts/googletwowaycontactsyncadaptor.h
Expand Up @@ -148,6 +148,7 @@ class GoogleTwoWayContactSyncAdaptor : public GoogleDataTypeSyncAdaptor
void downloadContactAvatarImage(int accountId, const QString &accessToken, const QUrl &imageUrl, const QString &filename);
void imageDownloaded(const QString &url, const QString &path, const QVariantMap &metadata);

void delayedTransformContactAvatars();
void loadCollection(const QContactCollection &collection);

void purgeAccount(int pid);
Expand Down Expand Up @@ -183,6 +184,7 @@ class GoogleTwoWayContactSyncAdaptor : public GoogleDataTypeSyncAdaptor

QMap<int, int> m_apiRequestsRemaining;
QMap<int, QMap<QString, QString> > m_queuedAvatarsForDownload; // contact guid -> remote avatar path
QList<int> m_pendingAvatarRequests;

bool m_allowFinalCleanup = false;
};
Expand Down

0 comments on commit 8b6c9fa

Please sign in to comment.