Skip to content
This repository has been archived by the owner on Sep 4, 2021. It is now read-only.

Commit

Permalink
Merge pull request #61 from matthewvogt/demo-im-accounts
Browse files Browse the repository at this point in the history
[libcontacts] Allow online accounts to be imported for demo purposes
  • Loading branch information
matthewvogt committed Dec 4, 2013
2 parents 1ed988d + c1c2e0a commit 380fa72
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/seasideimport.cpp
Expand Up @@ -32,7 +32,7 @@
#include "seasideimport.h"

#include "seasidecache.h"
#include "seasidephotohandler.h"
#include "seasidepropertyhandler.h"

#include <QContactDetailFilter>
#include <QContactFetchHint>
Expand Down Expand Up @@ -312,9 +312,9 @@ QList<QContact> SeasideImport::buildImportContacts(const QList<QVersitDocument>
*updatedCount = 0;

// Read the contacts from the import details
SeasidePhotoHandler photoHandler;
SeasidePropertyHandler propertyHandler;
QVersitContactImporter importer;
importer.setPropertyHandler(&photoHandler);
importer.setPropertyHandler(&propertyHandler);
importer.importDocuments(details);

QList<QContact> importedContacts(importer.contacts());
Expand Down
65 changes: 58 additions & 7 deletions src/seasidephotohandler.cpp → src/seasidepropertyhandler.cpp
Expand Up @@ -30,36 +30,40 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
*/

#include "seasidephotohandler.h"
#include "seasidepropertyhandler.h"

#ifndef QT_VERSION_5
#include <QDesktopServices>
#include <QContactThumbnail>
#endif
#include <QContactAvatar>
#include <QContactOnlineAccount>
#include <QContactPresence>
#include <QCryptographicHash>
#include <QDir>
#include <QImage>

SeasidePhotoHandler::SeasidePhotoHandler()
#include <qtcontacts-extensions.h>

SeasidePropertyHandler::SeasidePropertyHandler()
{
}

SeasidePhotoHandler::~SeasidePhotoHandler()
SeasidePropertyHandler::~SeasidePropertyHandler()
{
}

void SeasidePhotoHandler::documentProcessed(const QVersitDocument &, QContact *)
void SeasidePropertyHandler::documentProcessed(const QVersitDocument &, QContact *)
{
// do nothing, have no state to clean.
}

void SeasidePhotoHandler::propertyProcessed(const QVersitDocument &, const QVersitProperty &property, const QContact &, bool *alreadyProcessed, QList<QContactDetail> * updatedDetails)
namespace {

void processPhoto(const QVersitProperty &property, bool *alreadyProcessed, QList<QContactDetail> * updatedDetails)
{
// if the property is a PHOTO property, store the data to disk
// and then create an avatar detail which points to it.
if (property.name().toLower() != QLatin1String("photo"))
return;

#ifndef QT_VERSION_5
// The Qt4 / QtMobility version has QContactThumbnail support.
Expand Down Expand Up @@ -165,3 +169,50 @@ void SeasidePhotoHandler::propertyProcessed(const QVersitDocument &, const QVers
*alreadyProcessed = true;
}

void processOnlineAccount(const QVersitProperty &property, bool *alreadyProcessed, QList<QContactDetail> * updatedDetails)
{
// Create an online account instance for demo purposes; it will not be connected
// to a registered telepathy account, so it won't actually be able to converse

// Try to interpret the data as a stringlist
const QString detail(property.variantValue().toString());

// The format is: URI/path/display-name/icon-path/service-provider/service-provider-display-name
const QStringList details(detail.split(QLatin1Char(';'), QString::KeepEmptyParts));
if (details.count() == 6) {
QContactOnlineAccount qcoa;

qcoa.setValue(QContactOnlineAccount::FieldAccountUri, details.at(0));
qcoa.setValue(QContactOnlineAccount__FieldAccountPath, details.at(1));
qcoa.setValue(QContactOnlineAccount__FieldAccountDisplayName, details.at(2));
qcoa.setValue(QContactOnlineAccount__FieldAccountIconPath, details.at(3));
qcoa.setValue(QContactOnlineAccount::FieldServiceProvider, details.at(4));
qcoa.setValue(QContactOnlineAccount__FieldServiceProviderDisplayName, details.at(5));
qcoa.setDetailUri(QString::fromLatin1("%1:%2").arg(details.at(1)).arg(details.at(0)));

updatedDetails->append(qcoa);

// Since it is a demo account, give it a random presence state
const int state = (qrand() % 4);
QContactPresence presence;
presence.setPresenceState(state == 3 ? QContactPresence::PresenceBusy : (state == 2 ? QContactPresence::PresenceAway : QContactPresence::PresenceAvailable));
presence.setLinkedDetailUris(QStringList() << qcoa.detailUri());
updatedDetails->append(presence);

*alreadyProcessed = true;
} else {
qWarning() << "Invalid online account details:" << details;
}
}

}

void SeasidePropertyHandler::propertyProcessed(const QVersitDocument &, const QVersitProperty &property, const QContact &, bool *alreadyProcessed, QList<QContactDetail> * updatedDetails)
{
if (property.name().toLower() == QLatin1String("photo")) {
processPhoto(property, alreadyProcessed, updatedDetails);
} else if (property.name().toLower() == QLatin1String("x-nemomobile-onlineaccount-demo")) {
processOnlineAccount(property, alreadyProcessed, updatedDetails);
}
}

17 changes: 10 additions & 7 deletions src/seasidephotohandler.h → src/seasidepropertyhandler.h
Expand Up @@ -30,8 +30,8 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
*/

#ifndef PHOTOHANDLER_H
#define PHOTOHANDLER_H
#ifndef PROPERTYHANDLER_H
#define PROPERTYHANDLER_H

#include "contactcacheexport.h"

Expand All @@ -58,23 +58,26 @@ QTM_USE_NAMESPACE
#endif

/*
SeasidePhotoHandler
SeasidePropertyHandler
Some backends don't support saving PHOTO data directly.
Instead, the PHOTO data needs to be extracted, saved to
a file, and then the path to the file needs to be saved
to the backend as a contact avatar url detail.
Also support the X-NEMOMOBILE-ONLINEACCOUNT-DEMO property
for loading demo online account data.
*/
class CONTACTCACHE_EXPORT SeasidePhotoHandler : public QVersitContactImporterPropertyHandlerV2
class CONTACTCACHE_EXPORT SeasidePropertyHandler : public QVersitContactImporterPropertyHandlerV2
{
public:
SeasidePhotoHandler();
~SeasidePhotoHandler();
SeasidePropertyHandler();
~SeasidePropertyHandler();

// QVersitContactImporterPropertyHandlerV2
void documentProcessed(const QVersitDocument &, QContact *);
void propertyProcessed(const QVersitDocument &, const QVersitProperty &property,
const QContact &, bool *alreadyProcessed, QList<QContactDetail> * updatedDetails);
};

#endif // PHOTOHANDLER_H
#endif // PROPERTYHANDLER_H
6 changes: 3 additions & 3 deletions src/src.pro
Expand Up @@ -39,22 +39,22 @@ DEFINES += CONTACTCACHE_BUILD
SOURCES += \
$$PWD/seasidecache.cpp \
$$PWD/seasideimport.cpp \
$$PWD/seasidephotohandler.cpp
$$PWD/seasidepropertyhandler.cpp

HEADERS += \
$$PWD/contactcacheexport.h \
$$PWD/seasidecache.h \
$$PWD/seasideimport.h \
$$PWD/synchronizelists.h \
$$PWD/seasidenamegrouper.h \
$$PWD/seasidephotohandler.h
$$PWD/seasidepropertyhandler.h

headers.files = \
$$PWD/contactcacheexport.h \
$$PWD/seasidecache.h \
$$PWD/seasideimport.h \
$$PWD/synchronizelists.h \
$$PWD/seasidenamegrouper.h \
$$PWD/seasidephotohandler.h
$$PWD/seasidepropertyhandler.h
headers.path = $$PREFIX/include/$$TARGET
INSTALLS += headers

0 comments on commit 380fa72

Please sign in to comment.