qtmobility/plugins/contacts/maemo5/qcontactmaemo5backend.cpp
changeset 4 90517678cc4f
child 5 453da2cfceef
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qcontactmaemo5backend_p.h"
       
    43 
       
    44 #include <QSharedData>
       
    45 
       
    46 #include "qcontactmaemo5debug_p.h"
       
    47 
       
    48 DEFINE_GLOBAL_DEBUG_VAR
       
    49 
       
    50 QContactManagerEngine* ContactMaemo5Factory::engine(const QMap<QString, QString>& parameters, QContactManager::Error* error)
       
    51 {
       
    52     Q_UNUSED(parameters);
       
    53     Q_UNUSED(error);
       
    54 
       
    55     initDebugLogger();
       
    56     return new QContactMaemo5Engine(); //FIXME Wonderfull memory leak :D
       
    57 }
       
    58 
       
    59 QString ContactMaemo5Factory::managerName() const
       
    60 {
       
    61     return QString("maemo5");
       
    62 }
       
    63 
       
    64 Q_EXPORT_PLUGIN2(qtcontacts_maemo5, ContactMaemo5Factory);
       
    65 
       
    66 /*!
       
    67   \class QContactMaemo5Engine
       
    68   \brief The QContactMaemo5Engine class provides an implementation of
       
    69   QContactManagerEngine whose functions always return an error.
       
    70   
       
    71   The Maemo5 engine.
       
    72  */
       
    73 
       
    74 /*! Constructs a new invalid contacts backend. */
       
    75 QContactMaemo5Engine::QContactMaemo5Engine() : d(new QContactMaemo5EngineData)
       
    76 {
       
    77   QContactABook *abook = d->m_abook;
       
    78   connect(abook, SIGNAL(contactsAdded(const QList<QContactLocalId>&)), SIGNAL(contactsAdded(const QList<QContactLocalId>&)));
       
    79   connect(abook, SIGNAL(contactsChanged(const QList<QContactLocalId>&)), SIGNAL(contactsChanged(const QList<QContactLocalId>&)));
       
    80   connect(abook, SIGNAL(contactsRemoved(const QList<QContactLocalId>&)), SIGNAL(contactsRemoved(const QList<QContactLocalId>&)));
       
    81 }
       
    82 
       
    83 /*! \reimp */
       
    84 QContactMaemo5Engine& QContactMaemo5Engine::operator=(const QContactMaemo5Engine& other)
       
    85 {
       
    86     d = other.d;
       
    87     return *this;
       
    88 }
       
    89 
       
    90 /*! \reimp */
       
    91 QString QContactMaemo5Engine::managerName() const
       
    92 {
       
    93     return QString(QLatin1String("maemo5"));
       
    94 }
       
    95 
       
    96 /* Synthesise the display label of a contact */
       
    97 QString QContactMaemo5Engine::synthesizedDisplayLabel(const QContact& contact, QContactManager::Error* error) const
       
    98 {
       
    99   Q_UNUSED(error)
       
   100   QString label = QContactManagerEngine::synthesizedDisplayLabel(contact, error);
       
   101   
       
   102   if (label.isEmpty()) {
       
   103     QContactNickname n = contact.detail(QContactNickname::DefinitionName);
       
   104     label = n.nickname();
       
   105   }
       
   106   
       
   107   if (label.isEmpty())
       
   108     label = "No name";
       
   109   
       
   110   return label;
       
   111 }
       
   112 
       
   113 bool QContactMaemo5Engine::validateContact(const QContact& contact, QContactManager::Error* error) const
       
   114 {
       
   115     return QContactManagerEngine::validateContact(contact, error);
       
   116 }
       
   117 
       
   118 bool QContactMaemo5Engine::validateDefinition(const QContactDetailDefinition& definition, QContactManager::Error* error) const
       
   119 {
       
   120     QContactDetailDefinition existing = detailDefinition(definition.name(), QContactType::TypeContact, error);
       
   121     if (existing == definition) {
       
   122         *error = QContactManager::NoError;
       
   123         return true;
       
   124     }
       
   125 
       
   126     *error = QContactManager::NotSupportedError; // XXX TODO: mutable definitions?
       
   127     return false;
       
   128 }
       
   129 
       
   130 QContactLocalId QContactMaemo5Engine::selfContactId(QContactManager::Error* error) const
       
   131 {
       
   132   Q_CHECK_PTR(d->m_abook);
       
   133 
       
   134   return d->m_abook->selfContactId(error);
       
   135 }
       
   136 
       
   137 QList<QContactLocalId> QContactMaemo5Engine::contactIds(const QContactFilter& filter, const QList<QContactSortOrder>& sortOrders, QContactManager::Error* error) const
       
   138 {
       
   139   Q_CHECK_PTR(d->m_abook);
       
   140   
       
   141   //return QContactManagerEngine::contactIds(filter, sortOrders, error);
       
   142   return d->m_abook->contactIds(filter, sortOrders, error);
       
   143 }
       
   144 
       
   145 QList<QContact> QContactMaemo5Engine::contacts(const QContactFilter & filter, const QList<QContactSortOrder> & sortOrders, const QContactFetchHint & fetchHint,
       
   146 			  QContactManager::Error* error ) const
       
   147 {
       
   148   Q_UNUSED(fetchHint); // no optimisations currently, ignore the fetchhint.
       
   149   Q_CHECK_PTR(d->m_abook);
       
   150   QList<QContact> rtn;
       
   151   
       
   152   QList<QContactLocalId> ids = contactIds(filter, sortOrders,error);
       
   153   foreach (QContactLocalId id, ids)
       
   154     rtn << contact(id, QContactFetchHint(), error);
       
   155   return rtn;
       
   156 }
       
   157 
       
   158 QContact QContactMaemo5Engine::contact(const QContactLocalId& contactId, const QContactFetchHint& fetchHint, QContactManager::Error* error) const
       
   159 {
       
   160   Q_UNUSED(fetchHint); //TODO
       
   161   Q_CHECK_PTR(d->m_abook);
       
   162   
       
   163   QContact *contact = d->m_abook->getQContact(contactId, error);
       
   164   QContact rtn(*contact);
       
   165   delete contact;
       
   166   if (*error == QContactManager::NoError) {
       
   167     setContactDisplayLabel(&rtn, synthesizedDisplayLabel(rtn, error));
       
   168     QContactId cid;
       
   169     cid.setLocalId(contactId);
       
   170     cid.setManagerUri(managerUri());
       
   171     rtn.setId(cid);
       
   172   }
       
   173   return rtn;
       
   174 }
       
   175 
       
   176 bool QContactMaemo5Engine::saveContacts(QList<QContact>* contacts, QMap<int, QContactManager::Error>* errorMap, QContactManager::Error* error)
       
   177 {
       
   178     *error = QContactManager::NoError;
       
   179     QContactManager::Error tempError = QContactManager::NoError;
       
   180     QContact curr;
       
   181     for (int i = 0; i < contacts->size(); i++) {
       
   182         curr = contacts->at(i);
       
   183         if (!saveContact(&curr, &tempError)) {
       
   184             errorMap->insert(i, tempError);
       
   185             *error = tempError;
       
   186         } else {
       
   187             contacts->replace(i, curr);
       
   188         }
       
   189     }
       
   190 
       
   191     return (*error == QContactManager::NoError);
       
   192 }
       
   193 
       
   194 bool QContactMaemo5Engine::removeContacts(const QList<QContactLocalId>& ids, QMap<int, QContactManager::Error>* errorMap, QContactManager::Error* error)
       
   195 {
       
   196     *error = QContactManager::NoError;
       
   197     QContactManager::Error tempError = QContactManager::NoError;
       
   198     QContact curr;
       
   199     for (int i = 0; i < ids.size(); i++) {
       
   200         if (!removeContact(ids.at(i), &tempError)) {
       
   201             errorMap->insert(i, tempError);
       
   202             *error = tempError;
       
   203         }
       
   204     }
       
   205 
       
   206     return (*error == QContactManager::NoError);
       
   207 }
       
   208 
       
   209 bool QContactMaemo5Engine::saveContact(QContact* contact, QContactManager::Error* error)
       
   210 {
       
   211   Q_CHECK_PTR(d->m_abook);
       
   212   
       
   213   if (!contact) {
       
   214     *error = QContactManager::BadArgumentError;
       
   215     return false;
       
   216   }
       
   217 
       
   218   // synthesize the display label for the contact
       
   219   setContactDisplayLabel(contact, synthesizedDisplayLabel(*contact, error));
       
   220 
       
   221   // ensure that the contact's details conform to their definitions
       
   222   if (!validateContact(*contact, error)) {
       
   223     QCM5_DEBUG << "Validate Contact failed";
       
   224     return false;
       
   225   }
       
   226 
       
   227   bool retn = d->m_abook->saveContact(contact, error);
       
   228   QContactId cId = contact->id();
       
   229   cId.setManagerUri(managerUri());
       
   230   contact->setId(cId);
       
   231   return retn;
       
   232 }
       
   233 
       
   234 bool QContactMaemo5Engine::removeContact(const QContactLocalId& contactId, QContactManager::Error* error)
       
   235 {
       
   236   Q_CHECK_PTR(d->m_abook);
       
   237   return d->m_abook->removeContact(contactId, error);
       
   238 }
       
   239 
       
   240 QMap<QString, QContactDetailDefinition> QContactMaemo5Engine::detailDefinitions(const QString& contactType, QContactManager::Error* error) const
       
   241 {
       
   242 
       
   243     QMap<QString, QMap<QString, QContactDetailDefinition> > defns = QContactManagerEngine::schemaDefinitions();
       
   244     
       
   245     QMap<QString, QContactDetailFieldDefinition> fields;
       
   246     
       
   247     QContactDetailFieldDefinition gsfd; //Generic string field definition
       
   248     gsfd.setDataType(QVariant::String);
       
   249     
       
   250     // QContactAddress
       
   251     fields = defns[contactType][QContactAddress::DefinitionName].fields();
       
   252     //fields.remove(QContactAddress::FieldSubTypes);
       
   253     fields.insert("Estension", gsfd);
       
   254     fields.insert(QContactDetail::FieldDetailUri, gsfd);
       
   255     defns[contactType][QContactAddress::DefinitionName].setFields(fields);
       
   256     
       
   257     // QContactAnniversary
       
   258     defns[contactType].remove(QContactAnniversary::DefinitionName);
       
   259     
       
   260     // QContactAvatar
       
   261     // TODO setUnique(true);
       
   262     // QContactBirthday
       
   263     // QContactDisplayLabel
       
   264     
       
   265     // QContactEmailAddress
       
   266     fields = defns[contactType][QContactEmailAddress::DefinitionName].fields();
       
   267     fields.insert(QContactDetail::FieldDetailUri, gsfd);
       
   268     defns[contactType][QContactEmailAddress::DefinitionName].setFields(fields);
       
   269     
       
   270     // QContactFamily
       
   271     // QContactGender
       
   272     // QContactGeoLocation
       
   273     defns[contactType].remove(QContactGeoLocation::DefinitionName);
       
   274     
       
   275     // QContactGuid
       
   276     // QContactName
       
   277     fields = defns[contactType][QContactName::DefinitionName].fields();
       
   278     fields.remove(QContactName::FieldCustomLabel);
       
   279     fields.remove(QContactName::FieldPrefix);
       
   280     fields.remove(QContactName::FieldSuffix);
       
   281     defns[contactType][QContactName::DefinitionName].setFields(fields);
       
   282     
       
   283     // QContactNickname
       
   284     // QContactNote
       
   285     // QContactOnlineAccount
       
   286     fields = defns[contactType][QContactOnlineAccount::DefinitionName].fields();
       
   287     fields.remove(QContactOnlineAccount::FieldAccountUri);
       
   288     fields.remove(QContactOnlineAccount::FieldSubTypes);
       
   289     fields.insert("AccountPath", gsfd);
       
   290     defns[contactType][QContactOnlineAccount::DefinitionName].setFields(fields);
       
   291     
       
   292     // QContactOrganization
       
   293     fields = defns[contactType][QContactOrganization::DefinitionName].fields();
       
   294     fields.remove(QContactOrganization::FieldAssistantName);
       
   295     fields.remove(QContactOrganization::FieldDepartment);
       
   296     fields.remove(QContactOrganization::FieldLocation);
       
   297     fields.remove(QContactOrganization::FieldLogoUrl);
       
   298     fields.remove(QContactOrganization::FieldName);
       
   299     fields.remove(QContactOrganization::FieldRole);
       
   300     defns[contactType][QContactOrganization::DefinitionName].setFields(fields);
       
   301     
       
   302     // QContactPhoneNumber
       
   303     fields = defns[contactType][QContactPhoneNumber::DefinitionName].fields();
       
   304     fields.insert(QContactDetail::FieldDetailUri, gsfd);
       
   305     defns[contactType][QContactPhoneNumber::DefinitionName].setFields(fields);
       
   306     
       
   307     // QContactSyncTarget
       
   308     defns[contactType].remove(QContactSyncTarget::DefinitionName);
       
   309     
       
   310     // QContactTimestamp
       
   311     // QContactType
       
   312     // QContactUrl
       
   313     fields = defns[contactType][QContactUrl::DefinitionName].fields();
       
   314     fields.remove(QContactUrl::FieldSubType);
       
   315     defns[contactType][QContactUrl::DefinitionName].setFields(fields);
       
   316   
       
   317     //Still unmanaged: GlobalPresence, Presence, Ringtone, Tag
       
   318     
       
   319     QCM5_DEBUG << "Contact type" << contactType << "Keys" <<  defns.keys();
       
   320     
       
   321     *error = QContactManager::NoError;
       
   322     return defns[contactType];
       
   323 }
       
   324 
       
   325 QContactDetailDefinition QContactMaemo5Engine::detailDefinition(const QString& definitionName, const QString& contactType, QContactManager::Error* error) const
       
   326 {
       
   327     return QContactManagerEngine::detailDefinition(definitionName, contactType, error);
       
   328 }
       
   329 
       
   330 bool QContactMaemo5Engine::hasFeature(QContactManager::ManagerFeature feature, const QString& contactType) const {
       
   331   Q_UNUSED(contactType);
       
   332   if (feature == QContactManager::Anonymous)
       
   333     return true;
       
   334   
       
   335   return false;
       
   336 }
       
   337 
       
   338 bool QContactMaemo5Engine::isFilterSupported(const QContactFilter& filter) const {
       
   339   switch (filter.type()) {
       
   340     case QContactFilter::InvalidFilter:
       
   341     case QContactFilter::DefaultFilter:
       
   342     case QContactFilter::LocalIdFilter:
       
   343     case QContactFilter::ContactDetailFilter:
       
   344     case QContactFilter::ActionFilter:
       
   345     case QContactFilter::IntersectionFilter:
       
   346     case QContactFilter::UnionFilter:
       
   347       return true;
       
   348     default:
       
   349       return false;
       
   350   }
       
   351 }
       
   352 
       
   353 QList<QVariant::Type> QContactMaemo5Engine::supportedDataTypes() const {
       
   354   QList<QVariant::Type> st;
       
   355   st.append(QVariant::String);
       
   356   st.append(QVariant::Int);
       
   357   st.append(QVariant::UInt);
       
   358   st.append(QVariant::Double);
       
   359   st.append(QVariant::Date);
       
   360   st.append(QVariant::DateTime);
       
   361 
       
   362   return st;   
       
   363 }