Skip to content

Commit

Permalink
Adapted to use ConstrPtr pattern with VKImageDatabase structs
Browse files Browse the repository at this point in the history
  • Loading branch information
Antti Seppälä committed Oct 12, 2015
1 parent 1b448be commit 00d540e
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 90 deletions.
8 changes: 4 additions & 4 deletions rpm/sociald.spec
Expand Up @@ -476,7 +476,7 @@ systemctl-user try-restart msyncd.service || :

%package vk-notifications
Summary: Provides notification synchronisation with VK
License: TBD
License: LGPLv2.1
Group: System/Libraries
BuildRequires: nemo-qml-plugin-notifications-qt5-devel
BuildRequires: qt5-qttools-linguist
Expand Down Expand Up @@ -504,7 +504,7 @@ systemctl-user restart msyncd.service || :

%package vk-calendars
Summary: Provides calendar synchronisation with VK
License: TBD
License: LGPLv2.1
Group: System/Libraries
BuildRequires: pkgconfig(libmkcal-qt5)
BuildRequires: pkgconfig(libkcalcoren-qt5)
Expand All @@ -531,7 +531,7 @@ systemctl-user restart msyncd.service || :

%package vk-contacts
Summary: Provides contact synchronisation with VK
License: TBD
License: LGPLv2.1
Group: System/Libraries
BuildRequires: pkgconfig(Qt5Contacts)
BuildRequires: pkgconfig(qtcontacts-sqlite-qt5-extensions)
Expand All @@ -558,7 +558,7 @@ systemctl-user restart msyncd.service || :

%package vk-images
Summary: Provides image synchronisation with VK
License: TBD
License: LGPLv2.1
Group: System/Libraries
Requires: %{name} = %{version}-%{release}

Expand Down
169 changes: 88 additions & 81 deletions src/vk/vk-images/vkimagesyncadaptor.cpp
Expand Up @@ -43,7 +43,6 @@ VKImageSyncAdaptor::VKImageSyncAdaptor(QObject *parent)
setInitialActive(m_db.isValid());
}


VKImageSyncAdaptor::~VKImageSyncAdaptor()
{
}
Expand Down Expand Up @@ -81,18 +80,18 @@ void VKImageSyncAdaptor::finalize(int accountId)
// Determine album delta.
QHash<QString, QSet<QString> > deletedAlbumIds; // user to deleted album ids
QHash<QString, QSet<QString> > emptiedAlbumIds; // user to empty album ids
QList<VKAlbum> deletedAlbums;
QList<VKAlbum> addedAlbums;
QList<VKAlbum> modifiedAlbums;
QList<VKAlbum> unmodifiedAlbums;
QList<VKAlbum::ConstPtr> deletedAlbums;
QList<VKAlbum::ConstPtr> addedAlbums;
QList<VKAlbum::ConstPtr> modifiedAlbums;
QList<VKAlbum::ConstPtr> unmodifiedAlbums;

// first, find the albums which are new and need to be added to the db.
bool found = false;
QList<VKAlbum> accountAlbums = m_db.albums(accountId, QString());
Q_FOREACH (const VKAlbum &receivedAlbum, m_receivedAlbums) {
QList<VKAlbum::ConstPtr> accountAlbums = m_db.albums(accountId, QString());
Q_FOREACH (const VKAlbum::ConstPtr &receivedAlbum, m_receivedAlbums) {
found = false;
Q_FOREACH (const VKAlbum &album, accountAlbums) {
if (album.id == receivedAlbum.id && album.owner_id == receivedAlbum.owner_id) {
Q_FOREACH (const VKAlbum::ConstPtr &album, accountAlbums) {
if (album->id() == receivedAlbum->id() && album->ownerId() == receivedAlbum->ownerId()) {
found = true;
}
}
Expand All @@ -102,12 +101,12 @@ void VKImageSyncAdaptor::finalize(int accountId)
}

// then, find the albums which need to be removed or updated in the db.
Q_FOREACH (const VKAlbum &album, accountAlbums) {
Q_FOREACH (const VKAlbum::ConstPtr &album, accountAlbums) {
found = false;
Q_FOREACH (const VKAlbum &receivedAlbum, m_receivedAlbums) {
if (album.id == receivedAlbum.id && album.owner_id == receivedAlbum.owner_id) {
Q_FOREACH (const VKAlbum::ConstPtr &receivedAlbum, m_receivedAlbums) {
if (album->id() == receivedAlbum->id() && album->ownerId() == receivedAlbum->ownerId()) {
found = true;
if (album != receivedAlbum) {
if (*album != *receivedAlbum) {
modifiedAlbums.append(receivedAlbum);
} else {
unmodifiedAlbums.append(receivedAlbum);
Expand All @@ -116,42 +115,42 @@ void VKImageSyncAdaptor::finalize(int accountId)
}
if (!found) {
deletedAlbums.append(album);
deletedAlbumIds[album.owner_id].insert(album.id);
deletedAlbumIds[album->ownerId()].insert(album->id());
}
}

// and find the albums which are empty server side.
// these will be unmodified but the photos will need to be removed.
Q_FOREACH (const VKAlbum &album, m_emptyAlbums) {
emptiedAlbumIds[album.owner_id].insert(album.id);
Q_FOREACH (const VKAlbum::ConstPtr &album, m_emptyAlbums) {
emptiedAlbumIds[album->ownerId()].insert(album->id());
}

// Determine photo delta.
QList<VKImage> deletedPhotos;
QList<VKImage> addedPhotos;
QList<VKImage> modifiedPhotos;
QList<VKImage> unmodifiedPhotos;
QList<VKImage::ConstPtr> deletedPhotos;
QList<VKImage::ConstPtr> addedPhotos;
QList<VKImage::ConstPtr> modifiedPhotos;
QList<VKImage::ConstPtr> unmodifiedPhotos;

// first, find the photos which need to be removed or updated in the db.
QList<VKImage> accountPhotos = m_db.images(accountId, QString(), QString());
Q_FOREACH (const VKImage &photo, accountPhotos) {
if (deletedAlbumIds[photo.owner_id].contains(photo.album_id)) {
QList<VKImage::ConstPtr> accountPhotos = m_db.images(accountId, QString(), QString());
Q_FOREACH (const VKImage::ConstPtr &photo, accountPhotos) {
if (deletedAlbumIds[photo->ownerId()].contains(photo->albumId())) {
// the entire album has been deleted. every photo in it needs to be deleted.
deletedPhotos.append(photo);
} else if (emptiedAlbumIds[photo.owner_id].contains(photo.album_id)) {
} else if (emptiedAlbumIds[photo->ownerId()].contains(photo->albumId())) {
// the album has been emptied server-side. every photo in it needs to be deleted.
deletedPhotos.append(photo);
} else if (!m_requestedPhotosForOwnerAndAlbum.contains(QStringLiteral("%1:%2").arg(photo.owner_id).arg(photo.album_id))) {
} else if (!m_requestedPhotosForOwnerAndAlbum.contains(QStringLiteral("%1:%2").arg(photo->ownerId()).arg(photo->albumId()))) {
// this album wasn't modified, so we didn't request photos from it.
// that is, every photo in it is unchanged.
unmodifiedPhotos.append(photo);
} else {
// this album was modified, so we need to perform delta detection.
found = false;
Q_FOREACH (const VKImage &receivedPhoto, m_receivedPhotos) {
if (photo.id == receivedPhoto.id && photo.owner_id == receivedPhoto.owner_id) {
Q_FOREACH (const VKImage::ConstPtr &receivedPhoto, m_receivedPhotos) {
if (photo->id() == receivedPhoto->id() && photo->ownerId() == receivedPhoto->ownerId()) {
found = true;
if (photo != receivedPhoto) {
if (*photo != *receivedPhoto) {
modifiedPhotos.append(receivedPhoto);
} else {
unmodifiedPhotos.append(receivedPhoto);
Expand All @@ -165,10 +164,10 @@ void VKImageSyncAdaptor::finalize(int accountId)
}

// then find the photos which are new and need to be added to the db.
Q_FOREACH (const VKImage &receivedPhoto, m_receivedPhotos) {
Q_FOREACH (const VKImage::ConstPtr &receivedPhoto, m_receivedPhotos) {
found = false;
Q_FOREACH (const VKImage &photo, accountPhotos) {
if (photo.id == receivedPhoto.id && photo.owner_id == receivedPhoto.owner_id) {
Q_FOREACH (const VKImage::ConstPtr &photo, accountPhotos) {
if (photo->id() == receivedPhoto->id() && photo->ownerId() == receivedPhoto->ownerId()) {
found = true;
}
}
Expand All @@ -183,7 +182,7 @@ void VKImageSyncAdaptor::finalize(int accountId)
LOG_DEBUG(" with Photos A/M/R/U:" << addedPhotos.size() << "/" << modifiedPhotos.size() << "/" << deletedPhotos.size() << "/" << unmodifiedPhotos.size());

// write changes to database.
Q_FOREACH (const VKUser &user, m_receivedUsers) { m_db.addUser(user); }
Q_FOREACH (const VKUser::ConstPtr &user, m_receivedUsers) { m_db.addUser(user); }
m_db.addAlbums(addedAlbums+modifiedAlbums);
m_db.removeAlbums(deletedAlbums);
m_db.addImages(addedPhotos + modifiedPhotos);
Expand Down Expand Up @@ -304,33 +303,36 @@ void VKImageSyncAdaptor::albumsFinishedHandler()
}

// parse the album info.
VKAlbum album;
album.accountId = accountId;
album.owner_id = QString::number(albumObject.value("owner_id").toDouble(), 'g', 13);
album.id = QString::number(albumObject.value("id").toDouble(), 'g', 13);
album.title = albumObject.value("title").toString();
album.description = albumObject.value("description").toString();
album.created = albumObject.value("created").toInt();
album.updated = albumObject.value("updated").toInt();
album.thumb_src = albumObject.value("thumb_src").toString();
album.size = albumObject.value("size").toInt();
m_receivedAlbums.append(album);
QString id = QString::number(albumObject.value("id").toDouble(), 'g', 13);
QString ownerId = QString::number(albumObject.value("owner_id").toDouble(), 'g', 13);
QString title = albumObject.value("title").toString();
QString description = albumObject.value("description").toString();
int created = albumObject.value("created").toInt();
int updated = albumObject.value("updated").toInt();
QString thumbSrc = albumObject.value("thumb_src").toString();
int size = albumObject.value("size").toInt();
m_receivedAlbums.append(VKAlbum::create(id, ownerId, title, description,
thumbSrc, QString(), size, created, updated,
accountId));

// request the photos from this album if necessary
VKAlbum dbAlbum = m_db.album(accountId, album.owner_id, album.id);
int lastSyncTimestampForAlbum = qMax(dbAlbum.created, dbAlbum.updated);
if (album.created > lastSyncTimestampForAlbum || album.updated > lastSyncTimestampForAlbum) {
SOCIALD_LOG_DEBUG("Need to request photos for album:" << album.id << album.title << "with timestamps:" <<
album.created << "+" << album.updated << ">" << lastSyncTimestampForAlbum);
m_requestedPhotosForOwnerAndAlbum.insert(QStringLiteral("%1:%2").arg(album.owner_id).arg(album.id));
requestData(accountId, accessToken, QString(), album.owner_id, album.id);
int lastSyncTimestampForAlbum = 0;
VKAlbum::ConstPtr dbAlbum = m_db.album(accountId, ownerId, id);
if (dbAlbum) {
lastSyncTimestampForAlbum = qMax(dbAlbum->created(), dbAlbum->updated());
}
if (created > lastSyncTimestampForAlbum || updated > lastSyncTimestampForAlbum) {
SOCIALD_LOG_DEBUG("Need to request photos for album:" << id << title << "with timestamps:" <<
created << "+" << updated << ">" << lastSyncTimestampForAlbum);
m_requestedPhotosForOwnerAndAlbum.insert(QStringLiteral("%1:%2").arg(ownerId).arg(id));
requestData(accountId, accessToken, QString(), ownerId, id);
} else {
SOCIALD_LOG_DEBUG("No need to request photos for album:" << album.id << album.title << "with timestamps:" <<
album.created << "+" << album.updated << "<=" << lastSyncTimestampForAlbum);
SOCIALD_LOG_DEBUG("No need to request photos for album:" << id << title << "with timestamps:" <<
created << "+" << updated << "<=" << lastSyncTimestampForAlbum);
}

// request the information for user who owns this album if necessary
possiblyAddNewUser(album.owner_id, accountId, accessToken);
possiblyAddNewUser(ownerId, accountId, accessToken);
}

// Finally, reduce our semaphore.
Expand Down Expand Up @@ -364,7 +366,7 @@ void VKImageSyncAdaptor::imagesFinishedHandler()
QJsonArray items = parsed.value(QLatin1String("response")).toObject().value(QLatin1String("items")).toArray();
if (items.size() == 0) {
SOCIALD_LOG_DEBUG("album with id" << vkAlbumId << "from VK account with id" << accountId << "has no photos");
VKAlbum emptyAlbum(vkAlbumId, vkUserId, QString(), QString(), QString(), QString(), 0, 0, 0, accountId);
VKAlbum::Ptr emptyAlbum = VKAlbum::create(vkAlbumId, vkUserId, QString(), QString(), QString(), QString(), 0, 0, 0, accountId);
m_emptyAlbums.append(emptyAlbum);
decrementSemaphore(accountId);
return;
Expand All @@ -380,38 +382,40 @@ void VKImageSyncAdaptor::imagesFinishedHandler()
}

// parse image info.
VKImage photo;
photo.accountId = accountId;
photo.owner_id = vkUserId;
photo.album_id = vkAlbumId;
photo.id = QString::number(imageObject.value("id").toDouble(), 'g', 13);
photo.text = imageObject.value("text").toString();
photo.date = imageObject.value("date").toInt();
photo.height = 0;
QString id = QString::number(imageObject.value("id").toDouble(), 'g', 13);
QString text = imageObject.value("text").toString();
int date = imageObject.value("date").toInt();
int height = 0;
int width = 0;
QString src;
QString thumbSrc;

QJsonArray sizedPhotos = imageObject.value("sizes").toArray();
for (int psi = 0; psi < sizedPhotos.size(); ++psi) {
const QJsonObject &sizedImage(sizedPhotos[psi].toObject());
int currHeight = sizedImage.value("height").toInt();
if (currHeight > photo.height) {
photo.height = currHeight;
photo.width = sizedImage.value("width").toInt();
photo.photo_src = sizedImage.value("src").toString();
if (currHeight > height) {
height = currHeight;
width = sizedImage.value("width").toInt();
src = sizedImage.value("src").toString();
}

if (photo.thumb_src.isEmpty() && sizedImage.value("type").toString() == QStringLiteral("s")) {
photo.thumb_src = sizedImage.value("src").toString();
if (thumbSrc.isEmpty() && sizedImage.value("type").toString() == QStringLiteral("s")) {
thumbSrc = sizedImage.value("src").toString();
} else if (sizedImage.value("type").toString() == QStringLiteral("m")) {
photo.thumb_src = sizedImage.value("src").toString();
thumbSrc = sizedImage.value("src").toString();
}
}

if (photo.thumb_src.isEmpty()) {
photo.thumb_src = photo.photo_src;
if (thumbSrc.isEmpty()) {
thumbSrc = src;
}

// append the photo to our internal list.
SOCIALD_LOG_DEBUG("have new photo:" << photo.id << photo.photo_src << photo.height << photo.width << photo.date);
m_receivedPhotos.append(photo);
SOCIALD_LOG_DEBUG("have new photo:" << id << src << height << width << date);
m_receivedPhotos.append(VKImage::create(id, vkAlbumId, vkUserId, text, thumbSrc,
src, QString(), QString(),
width, height, date, accountId));
requestImagesCount += 1;
}

Expand All @@ -436,7 +440,12 @@ void VKImageSyncAdaptor::imagesFinishedHandler()

void VKImageSyncAdaptor::possiblyAddNewUser(const QString &vkUserId, int accountId, const QString &accessToken)
{
if (m_requestedUsers.contains(vkUserId) || !m_db.user(accountId).id.isEmpty()) {
QString dbUserId;
VKUser::ConstPtr dbUser = m_db.user(accountId);
if (dbUser) {
dbUserId = dbUser->id();
}
if (m_requestedUsers.contains(vkUserId) || !dbUserId.isEmpty()) {
return; // already requested or db already contains the user, no need to request.
}

Expand Down Expand Up @@ -482,13 +491,11 @@ void VKImageSyncAdaptor::userFinishedHandler()
}

QJsonObject userObject = parsed.value(QLatin1String("response")).toArray().first().toObject();
VKUser user;
user.accountId = accountId;
user.id = QString::number(userObject.value(QLatin1String("id")).toDouble(), 'g', 13);
user.first_name = userObject.value(QLatin1String("first_name")).toString();
user.last_name = userObject.value(QLatin1String("last_name")).toString();
user.photo_src = userObject.value(QLatin1String("photo_medium")).toString();
m_receivedUsers.append(user);
QString id = QString::number(userObject.value(QLatin1String("id")).toDouble(), 'g', 13);
QString firstName = userObject.value(QLatin1String("first_name")).toString();
QString lastName = userObject.value(QLatin1String("last_name")).toString();
QString photoSrc = userObject.value(QLatin1String("photo_medium")).toString();
m_receivedUsers.append(VKUser::create(id, firstName, lastName, photoSrc, QString(), accountId));

decrementSemaphore(accountId);
}
8 changes: 4 additions & 4 deletions src/vk/vk-images/vkimagesyncadaptor.h
Expand Up @@ -64,12 +64,12 @@ private Q_SLOTS:
void userFinishedHandler();

private:
QList<VKAlbum> m_receivedAlbums;
QList<VKImage> m_receivedPhotos;
QList<VKUser> m_receivedUsers;
QList<VKAlbum::ConstPtr> m_receivedAlbums;
QList<VKImage::ConstPtr> m_receivedPhotos;
QList<VKUser::ConstPtr> m_receivedUsers;
QSet<QString> m_requestedUsers; // only want to request the user information once.
QSet<QString> m_requestedPhotosForOwnerAndAlbum; // owner_id:album_id
QList<VKAlbum> m_emptyAlbums;
QList<VKAlbum::ConstPtr> m_emptyAlbums;
VKImagesDatabase m_db;
bool m_syncError;
};
Expand Down
1 change: 0 additions & 1 deletion src/vk/vk-posts/vk-posts.pri
@@ -1,4 +1,3 @@
CONFIG += link_pkgconfig meegotouchevents-qt5
PKGCONFIG += nemonotifications-qt5 Qt5Contacts qtcontacts-sqlite-qt5-extensions
SOURCES += $$PWD/vkpostsyncadaptor.cpp
HEADERS += $$PWD/vkpostsyncadaptor.h
Expand Down

0 comments on commit 00d540e

Please sign in to comment.