qtmobility/src/contacts/qcontact.cpp
changeset 5 453da2cfceef
parent 4 90517678cc4f
child 11 06b8e2af4411
equal deleted inserted replaced
4:90517678cc4f 5:453da2cfceef
    44 
    44 
    45 #include "qcontact.h"
    45 #include "qcontact.h"
    46 #include "qcontact_p.h"
    46 #include "qcontact_p.h"
    47 #include "qcontactdetail_p.h"
    47 #include "qcontactdetail_p.h"
    48 #include "qcontactmanager_p.h"
    48 #include "qcontactmanager_p.h"
    49 #include "qcontactaction.h"
       
    50 
    49 
    51 QTM_BEGIN_NAMESPACE
    50 QTM_BEGIN_NAMESPACE
    52 
    51 
    53 /*!
    52 /*!
    54   \class QContact
    53   \class QContact
    55  
    54  
    56   \brief The QContact class represents an addressbook contact.
    55   \brief The QContact class represents an addressbook contact.
    57 
    56 
    58   \ingroup contacts-main
    57   \ingroup contacts-main
       
    58 
       
    59   Individual contacts, groups, and other types of contacts are represented with
       
    60   a QContact object.  In addition to the type, a QContact consists of information
       
    61   that belongs to the contact and some information about the relationships that the
       
    62   contact has.
       
    63 
       
    64   A QContact object has a collection of details (like a name, phone numbers and
       
    65   email addresses).  Each detail (which can have multiple fields) is stored
       
    66   in an appropriate subclass of QContactDetail, and the QContact allows
       
    67   retrieving these details in various ways.
       
    68 
       
    69   A QContact may have zero or more relationships with other contacts.  For example,
       
    70   a group contact would have a \c "HasMember" relationship with the QContacts that
       
    71   are its members.  Spouses, managers and assistants can also be represented this
       
    72   way.
    59  
    73  
    60   A QContact consists of zero or more details.
    74   A QContact instance represents the in-memory version of an addressbook contact,
    61  
    75   and has no tie to a specific QContactManager.  It is possible for the contents
    62   An instance of the QContact class represents an in-memory contact,
    76   of a QContact to change independently of the contents that are stored persistently
    63   and may not reflect the state of that contact found in persistent
    77   in a QContactManager.  A QContact has an ID associated with it when it is first
    64   storage until the appropriate synchronization method is called
    78   retrieved from a QContactManager, or after it has been first saved, and this allows
    65   on the QContactManager (i.e., saveContact, removeContact).
    79   clients to track changes using the signals in QContactManager.
       
    80 
       
    81   A QContact has a number of mandatory details:
       
    82   \list
       
    83    \o A QContactType, with the type of the contact (individual contact, group etc)
       
    84    \o A QContactDisplayLabel, representing the text to display
       
    85   \endlist
       
    86 
       
    87   If you have edited the contents of a QContact (via saving or removing details),
       
    88   you will need to ask a specific QContactManager for the new display label for the
       
    89   contact, since system settings (like the order of first or last names) can vary
       
    90   between managers.
    66  
    91  
    67   \sa QContactManager, QContactDetail
    92   \sa QContactManager, QContactDetail
    68  */
    93  */
    69 
    94 
    70 /*!
    95 /*!
    71  * \fn QList<T> QContact::details() const
    96  * \fn QList<T> QContact::details() const
    72  * Returns a list of details of the template type
    97  * Returns a list of details of the template parameter type.  The type must be
       
    98  * a subclass of QContactDetail.
       
    99  *
       
   100  * For example:
       
   101  *  \snippet doc/src/snippets/qtcontactsdocsample/qtcontactsdocsample.cpp 3
    73  */
   102  */
    74 
   103 
    75 /*!
   104 /*!
    76  * \fn QList<T> QContact::details(const QString& fieldName, const QString& value) const
   105  * \fn QList<T> QContact::details(const QString& fieldName, const QString& value) const
    77  * Returns a list of details of the template type which match the \a fieldName and \a value criteria
   106  * Returns a list of details of the template parameter type which have field called \a fieldName, with matching \a value.
       
   107  * The type must be a subclass of QContactDetail.
       
   108  *
       
   109  * For example:
       
   110  *  \snippet doc/src/snippets/qtcontactsdocsample/qtcontactsdocsample.cpp 4
    78  */
   111  */
    79 
   112 
    80 /*!
   113 /*!
    81  * \fn T QContact::detail() const
   114  * \fn T QContact::detail() const
    82  * Returns the first detail of the template type
   115  * Returns the first detail of the template parameter type, as returned by the template details() function.
       
   116  * The type must be a subclass of QContactDetail.
    83  */
   117  */
    84 
   118 
    85 /*!
   119 /*!
    86  * \fn QContact::operator!=(const QContact &other) const
   120  * \fn QContact::operator!=(const QContact &other) const
    87  * Returns true if this contacts Id or details are different to those of the \a other contact
   121  * Returns true if this contacts id or details are different to those of the \a other contact.
    88  */
   122  */
    89 
   123 
    90 /*! Construct an empty contact. */
   124 /*!
       
   125     Construct an empty contact.
       
   126 
       
   127     The contact will have an empty display label, an empty id, and have type \l QContactType::TypeContact.
       
   128     The isEmpty() function will return true.
       
   129 */
    91 QContact::QContact()
   130 QContact::QContact()
    92     : d(new QContactData)
   131     : d(new QContactData)
    93 {
   132 {
    94     clearDetails();
   133     clearDetails();
    95 }
   134 }
   151 /*! Frees the memory used by this QContact */
   190 /*! Frees the memory used by this QContact */
   152 QContact::~QContact()
   191 QContact::~QContact()
   153 {
   192 {
   154 }
   193 }
   155 
   194 
   156 /*! Returns the QContactId that identifies this contact */
   195 /*!
       
   196     Returns the QContactId that identifies this contact.
       
   197 
       
   198     This may have been set when the contact was retrieved from
       
   199     a particular manager, or when the contact was first saved
       
   200     in a manager.  The QContactId is only valid with a specific
       
   201     manager.  See \l QContactManager::saveContact() for more
       
   202     information.
       
   203 
       
   204     \sa localId()
       
   205  */
   157 QContactId QContact::id() const
   206 QContactId QContact::id() const
   158 {
   207 {
   159     return d->m_id;
   208     return d->m_id;
   160 }
   209 }
   161 
   210 
   162 /*! Returns the QContactLocalId that identifies this contact within its manager */
   211 /*!
       
   212     Returns the QContactLocalId that identifies this contact within its manager
       
   213 
       
   214     This may have been set when the contact was retrieved from
       
   215     a particular manager, or when the contact was first saved
       
   216     in a manager.  The QContactLocalId is associated with a specific
       
   217     manager, but other contacts with the same local id might exist in
       
   218     different managers.
       
   219 
       
   220     See \l QContactManager::saveContact() for more
       
   221     information.
       
   222 
       
   223    \sa id()
       
   224 */
   163 QContactLocalId QContact::localId() const
   225 QContactLocalId QContact::localId() const
   164 {
   226 {
   165     return d->m_id.localId();
   227     return d->m_id.localId();
   166 }
   228 }
   167 
   229 
   193 
   255 
   194     d->m_details[1] = newType;
   256     d->m_details[1] = newType;
   195 }
   257 }
   196 
   258 
   197 /*!
   259 /*!
   198  * Sets the type of the contact to the given \a type.
   260  * Sets the type of the contact to the given \a type detail.
   199  */
   261  */
   200 void QContact::setType(const QContactType& type)
   262 void QContact::setType(const QContactType& type)
   201 {
   263 {
   202     // type is detail 1
   264     // type is detail 1
   203     d->m_details[1] = type;
   265     d->m_details[1] = type;
   204     d->m_details[1].d->m_access = QContactDetail::Irremovable;
   266     d->m_details[1].d->m_access = QContactDetail::Irremovable;
   205 }
   267 }
   206 
   268 
   207 /*!
   269 /*!
   208  * Returns the read-only display label of this contact.
   270  * Returns the display label of this contact.
   209  * A contact which has been retrieved from a manager will have a display label synthesized for it.
   271  *
       
   272  * A contact which has been retrieved from a manager will have a display label set when
       
   273  * the contact is retrieved.
       
   274  *
       
   275  * The display label is usually read-only, since some managers do not support arbitrary
       
   276  * labels (see also \l QContactName::setCustomLabel()).  If you modify the contact in a way
       
   277  * that would affect the display label, you can call QContactManager::synthesizeContactDisplayLabel() to get an
       
   278  * up-to-date display label.
       
   279  *
       
   280  * See the following example for more information:
       
   281  * \snippet doc/src/snippets/qtcontactsdocsample/qtcontactsdocsample.cpp Updating the display label of a contact
       
   282  *
       
   283  * \sa QContactManager::synthesizeContactDisplayLabel()
   210  */
   284  */
   211 QString QContact::displayLabel() const
   285 QString QContact::displayLabel() const
   212 {
   286 {
   213     return d->m_details.at(0).value(QContactDisplayLabel::FieldLabel);
   287     return d->m_details.at(0).value(QContactDisplayLabel::FieldLabel);
   214 }
   288 }
   215 
   289 
   216 /*!
   290 /*!
   217  * Sets the id of this contact to \a id.  Note that this only affects
   291  * Sets the id of this contact to \a id.
   218  * this structure, not any corresponding structures stored
   292  *
       
   293  * Note that this only affects this object, not any corresponding structures stored
   219  * by a QContactManager.
   294  * by a QContactManager.
   220  *
   295  *
   221  * If you change the id of a contact and save the contact
   296  * If you change the id of a contact and save the contact
   222  * in a manager, the previously existing contact will still
   297  * in a manager, the previously existing contact will still
   223  * exist.  You can do this to create copies (possibly modified)
   298  * exist.  You can do this to create copies (possibly modified)
   224  * of an existing contact.
   299  * of an existing contact, or to save a contact in a different manager.
   225  *
   300  *
   226  * Returns true if the \a id was set successfully, otherwise
   301  * \sa QContactManager::saveContact()
   227  * returns false.
       
   228  */
   302  */
   229 void QContact::setId(const QContactId& id)
   303 void QContact::setId(const QContactId& id)
   230 {
   304 {
   231     d->m_id = id;
   305     d->m_id = id;
   232 }
   306 }
   233 
   307 
   234 /*! Returns the first detail stored in the contact which is of the given \a definitionName */
   308 /*!
       
   309     \fn QContactDetail QContact::detail(const QLatin1Constant& definitionName) const
       
   310     Returns the first detail stored in the contact which with the given \a definitionName.
       
   311     The \a definitionName argument is typically the detail name constant provided by a
       
   312     specific subclass of QContactDetail.  For example:
       
   313 
       
   314     \snippet doc/src/snippets/qtcontactsdocsample/qtcontactsdocsample.cpp 0
       
   315 
       
   316     It would usually be more convenient to use the template version of this function, in
       
   317     the following manner:
       
   318 
       
   319     \snippet doc/src/snippets/qtcontactsdocsample/qtcontactsdocsample.cpp 1
       
   320 */
       
   321 
       
   322 /*!
       
   323     \fn QList<QContactDetail> QContact::details(const QLatin1Constant& definitionName) const
       
   324     Returns a list of details of the given \a definitionName.
       
   325 
       
   326     The \a definitionName argument is typically the detail name constant provided by a
       
   327     specific subclass of QContactDetail.  For example:
       
   328 
       
   329     \snippet doc/src/snippets/qtcontactsdocsample/qtcontactsdocsample.cpp 2
       
   330 
       
   331     It would usually be more convenient to use the template version of this function, in
       
   332     the following manner:
       
   333 
       
   334     \snippet doc/src/snippets/qtcontactsdocsample/qtcontactsdocsample.cpp 3
       
   335 */
       
   336 
       
   337 /*!
       
   338     \fn QList<QContactDetail> QContact::details(const QLatin1Constant& definitionName, const QLatin1Constant& fieldName, const QString& value)
       
   339     Returns a list of details of the given \a definitionName, with fields named \a fieldName and with value \a value.
       
   340 */
       
   341 
       
   342 /*!
       
   343     \fn QList<T> QContact::details(const char* fieldName, const QString& value) const
       
   344     \internal
       
   345 
       
   346     Returns a list of details of the template type which match the \a fieldName and \a value criteria
       
   347 */
       
   348 
       
   349 /*!
       
   350     Returns the first detail stored in the contact with the given \a definitionName
       
   351 */
   235 QContactDetail QContact::detail(const QString& definitionName) const
   352 QContactDetail QContact::detail(const QString& definitionName) const
   236 {
   353 {
   237     if (definitionName.isEmpty())
   354     if (definitionName.isEmpty())
   238         return d->m_details.first();
   355         return d->m_details.first();
   239 
   356 
   246     }
   363     }
   247 
   364 
   248     return QContactDetail();
   365     return QContactDetail();
   249 }
   366 }
   250 
   367 
   251 /*! Returns a list of details of the given \a definitionName */
   368 /*! Returns a list of details with the given \a definitionName
       
   369     The definitionName string can be determined by the DefinitionName attribute
       
   370     of defined objects (e.g. QContactPhoneNumber::DefinitionName) or by
       
   371     requesting a list of all the definition names using
       
   372     \l {QContactManager::detailDefinitions()}{detailDefinitions()} or the
       
   373     asynchronous \l
       
   374     {QContactDetailDefinitionFetchRequest::definitionNames()}{definitionNames()}.*/
   252 QList<QContactDetail> QContact::details(const QString& definitionName) const
   375 QList<QContactDetail> QContact::details(const QString& definitionName) const
   253 {
   376 {
   254     // build the sub-list of matching details.
   377     // build the sub-list of matching details.
   255     QList<QContactDetail> sublist;
   378     QList<QContactDetail> sublist;
   256 
   379 
   267     }
   390     }
   268 
   391 
   269     return sublist;
   392     return sublist;
   270 }
   393 }
   271 
   394 
   272 /*! Returns a list of details of the given \a definitionName, \a fieldName and field \a value*/
   395 /*!
       
   396     Returns a list of details of the given \a definitionName, with fields named \a fieldName and with value \a value.
       
   397     The definitionName string can be determined by the DefinitionName attribute
       
   398     of defined objects (e.g. QContactPhoneNumber::DefinitionName) or by
       
   399     requesting a list of all the definition names using
       
   400     \l {QContactManager::detailDefinitions()}{detailDefinitions()} or the
       
   401     asynchronous \l
       
   402     {QContactDetailDefinitionFetchRequest::definitionNames()}{definitionNames()}.*/
   273 QList<QContactDetail> QContact::details(const QString& definitionName, const QString& fieldName, const QString& value) const
   403 QList<QContactDetail> QContact::details(const QString& definitionName, const QString& fieldName, const QString& value) const
   274 {
   404 {
   275     // build the sub-list of matching details.
   405     // build the sub-list of matching details.
   276     QList<QContactDetail> sublist;
   406     QList<QContactDetail> sublist;
   277 
   407 
   289     }
   419     }
   290 
   420 
   291     return sublist;
   421     return sublist;
   292 }
   422 }
   293 
   423 
   294 /*! Returns the first detail stored in the contact which is of the given \a definitionName */
   424 /*!
       
   425     \internal
       
   426     Returns the first detail stored in the contact which with the given \a definitionName
       
   427 */
   295 QContactDetail QContact::detail(const char* definitionName) const
   428 QContactDetail QContact::detail(const char* definitionName) const
   296 {
   429 {
   297     if (definitionName == 0)
   430     if (definitionName == 0)
   298         return d->m_details.first();
   431         return d->m_details.first();
   299 
   432 
   306     }
   439     }
   307 
   440 
   308     return QContactDetail();
   441     return QContactDetail();
   309 }
   442 }
   310 
   443 
   311 /*! Returns a list of details of the given \a definitionName */
   444 /*!
       
   445     \internal
       
   446     Returns a list of details with the given \a definitionName
       
   447 */
   312 QList<QContactDetail> QContact::details(const char* definitionName) const
   448 QList<QContactDetail> QContact::details(const char* definitionName) const
   313 {
   449 {
   314     // build the sub-list of matching details.
   450     // build the sub-list of matching details.
   315     QList<QContactDetail> sublist;
   451     QList<QContactDetail> sublist;
   316 
   452 
   327     }
   463     }
   328 
   464 
   329     return sublist;
   465     return sublist;
   330 }
   466 }
   331 
   467 
   332 /*! Returns a list of details of the given \a definitionName, \a fieldName and field \a value*/
   468 /*!
       
   469     \internal
       
   470     Returns a list of details with the given \a definitionName, \a fieldName and field \a value
       
   471 */
   333 QList<QContactDetail> QContact::details(const char* definitionName, const char* fieldName, const QString& value) const
   472 QList<QContactDetail> QContact::details(const char* definitionName, const char* fieldName, const QString& value) const
   334 {
   473 {
   335     // build the sub-list of matching details.
   474     // build the sub-list of matching details.
   336     QList<QContactDetail> sublist;
   475     QList<QContactDetail> sublist;
   337 
   476 
   350 
   489 
   351     return sublist;
   490     return sublist;
   352 }
   491 }
   353 
   492 
   354 /*!
   493 /*!
   355  * Saves the given \a detail in the list of stored details, and sets its Id.
   494  * Saves the given \a detail in the list of stored details, and sets the detail's id.
   356  * If another detail of the same type and Id has been previously saved in
   495  * If another detail of the same type and id has been previously saved in
   357  * this contact, that detail is overwritten.  Otherwise, a new Id is generated
   496  * this contact, that detail is overwritten.  Otherwise, a new id is generated
   358  * and set in the detail, and the detail is added to the list.
   497  * and set in the detail, and the detail is added to the contact.
   359  *
   498  *
   360  * If the detail's access constraint includes \c QContactDetail::ReadOnly,
   499  * If the detail's access constraint includes \c QContactDetail::ReadOnly,
   361  * this function will return false.
   500  * this function will return true and save the detail in the contact,
   362  *
   501  * however attempting to save the contact in a manager may fail (if that manager
   363  * If \a detail is a contact type, the existing contact type will
   502  * decides that the read only detail should not be updated).
       
   503  * Details with the \c QContactDetail::ReadOnly constraint set are typically provided
       
   504  * in a contact by the manager, and are usually information that is either
       
   505  * synthesized, or not intended to be changed by the user (e.g. presence information
       
   506  * for other contacts).
       
   507  *
       
   508  * If \a detail is a QContactType, the existing contact type will
   364  * be overwritten with \a detail.  There is never more than one contact type
   509  * be overwritten with \a detail.  There is never more than one contact type
   365  * in a contact.  The supplied \a detail will have its accessConstraint set to
   510  * in a contact.
   366  * QContactDetail::Irremovable.
   511  *
   367  *
   512  * If \a detail is a QContactDisplayLabel, the contact will not be updated,
   368  * If \a detail is a display label, the supplied \a detail will have its
   513  * and the function will return false.  Since the display label formatting is specific
   369  * accessConstraint set to QContactDetail::Irremovable | QContactDetail::ReadOnly,
   514  * to each manager, use the QContactManager::synthesizeContactDisplayLabel() function
   370  * and the function will return false.
   515  * instead.
   371  *
   516  *
   372  * Returns true if the detail was saved successfully, otherwise returns false.
   517  * Returns true if the detail was saved successfully, otherwise returns false.
   373  *
   518  *
   374  * Note that the caller retains ownership of the detail.
   519  * Note that the caller retains ownership of the detail.
       
   520  * \sa QContactManager::synthesizeContactDisplayLabel()
   375  */
   521  */
   376 bool QContact::saveDetail(QContactDetail* detail)
   522 bool QContact::saveDetail(QContactDetail* detail)
   377 {
   523 {
   378     if (!detail)
   524     if (!detail)
   379         return false;
   525         return false;
   380 
   526 
   381     if (detail->accessConstraints() & QContactDetail::ReadOnly)
       
   382         return false;
       
   383 
       
   384     /* Also handle contact type specially - only one of them. */
   527     /* Also handle contact type specially - only one of them. */
   385     if (QContactDetailPrivate::detailPrivate(*detail)->m_definitionName == QContactType::DefinitionName.latin1()) {
   528     if (QContactDetailPrivate::detailPrivate(*detail)->m_definitionName == QContactType::DefinitionName.latin1()) {
   386         detail->d->m_access = QContactDetail::Irremovable;
   529         detail->d->m_access |= QContactDetail::Irremovable;
   387         d->m_details[1] = *detail;
   530         d->m_details[1] = *detail;
   388         return true;
   531         return true;
   389     }
   532     }
   390 
   533 
   391     /* And display label.. */
   534     /* And display label.. */
   392     if (QContactDetailPrivate::detailPrivate(*detail)->m_definitionName == QContactDisplayLabel::DefinitionName.latin1()) {
   535     if (QContactDetailPrivate::detailPrivate(*detail)->m_definitionName == QContactDisplayLabel::DefinitionName.latin1()) {
   393         detail->d->m_access = QContactDetail::Irremovable | QContactDetail::ReadOnly;
       
   394         return false;
   536         return false;
   395     }
   537     }
   396 
   538 
   397     // try to find the "old version" of this field
   539     // try to find the "old version" of this field
   398     // ie, the one with the same type and id, but different value or attributes.
   540     // ie, the one with the same type and id, but different value or attributes.
   414 
   556 
   415 /*!
   557 /*!
   416  * Removes the \a detail from the contact.
   558  * Removes the \a detail from the contact.
   417  *
   559  *
   418  * The detail in the contact which has the same key as that of the given \a detail
   560  * The detail in the contact which has the same key as that of the given \a detail
   419  * will be removed if it exists.  That is, the information in the detail may be different.
   561  * will be removed if it exists.  Only the key is used for comparison - that is, the
   420  * Any preference for the given field is also removed.
   562  * information in the detail may be different.
   421  *
   563  *
   422  * If the detail's access constraint includes \c QContactDetail::Irremovable,
   564  * If the detail's access constraint includes \c QContactDetail::Irremovable,
   423  * this function will return false.
   565  * this function will return false.
   424  *
   566  *
   425  * Returns true if the detail was removed successfully, false if an error occurred.
   567  * Returns true if the detail was removed successfully, false if an error occurred.
   448         return false;
   590         return false;
   449 
   591 
   450     if (!d->m_details.contains(*detail))
   592     if (!d->m_details.contains(*detail))
   451         return false;
   593         return false;
   452 
   594 
   453     // remove any preferences we may have stored for the detail.
   595     // then remove the detail.
   454     QStringList keys = d->m_preferences.keys();
       
   455     for (int i = 0; i < keys.size(); i++) {
       
   456         QString prefKey = keys.at(i);
       
   457         if (d->m_preferences.value(prefKey) == detail->d->m_id) {
       
   458             d->m_preferences.remove(prefKey);
       
   459         }
       
   460     }
       
   461 
       
   462     // then remove the detail.  // OLD BEHAVIOUR (24/12/2009): d->m_details.removeOne(*detail);
       
   463     d->m_details.removeAt(removeIndex);
   596     d->m_details.removeAt(removeIndex);
   464     return true;
   597     return true;
   465 }
   598 }
   466 
   599 
   467 /*! Returns true if this contact is equal to the \a other contact, false if either the id or stored details are not the same */
   600 /*! Returns true if this contact is equal to the \a other contact, false if either the id or stored details are not the same */
   469 {
   602 {
   470     return other.d->m_id == d->m_id &&
   603     return other.d->m_id == d->m_id &&
   471         other.d->m_details == d->m_details;
   604         other.d->m_details == d->m_details;
   472 }
   605 }
   473 
   606 
   474 /*! Returns the hash value for \a key. */
   607 /*!
       
   608     \relates QContact
       
   609     Returns the hash value for \a key.
       
   610 */
   475 uint qHash(const QContact &key)
   611 uint qHash(const QContact &key)
   476 {
   612 {
   477     uint hash = qHash(key.id());
   613     uint hash = qHash(key.id());
   478     foreach (const QContactDetail& detail, key.details()) {
   614     foreach (const QContactDetail& detail, key.details()) {
   479         hash += qHash(detail);
   615         hash += qHash(detail);
   488         dbg.space() << '\n' << detail;
   624         dbg.space() << '\n' << detail;
   489     }
   625     }
   490     return dbg.maybeSpace();
   626     return dbg.maybeSpace();
   491 }
   627 }
   492 
   628 
   493 /*! Retrieve the first detail for which the given \a actionName is available */
   629 /*!
   494 QContactDetail QContact::detailWithAction(const QString& actionName) const
   630     Returns a list of relationships of the given \a relationshipType in which this contact is a participant.
   495 {
   631 
   496     if (actionName.isEmpty())
   632     If \a relationshipType is empty, all relationships will be returned.
   497         return QContactDetail();
   633 
   498 
   634     \note This function only examines the relationships that were present when this contact
   499     QList<QContactDetail> dets = detailsWithAction(actionName);
   635     was retrieved from a manager.  You can also query the manager directly, if you require
   500     if (dets.isEmpty())
   636     the most up to date information.
   501         return QContactDetail();
   637 
   502 
   638     \snippet doc/src/snippets/qtcontactsdocsample/qtcontactsdocsample.cpp 5
   503     QContactDetail retn = dets.first();
   639 
   504     return retn;
   640     \sa QContactRelationshipFetchRequest, QContactManager::relationships()
   505 }
       
   506 
       
   507 /*! Retrieve any details for which the given \a actionName is available */
       
   508 QList<QContactDetail> QContact::detailsWithAction(const QString& actionName) const
       
   509 {
       
   510     if (actionName.isEmpty())
       
   511         return QList<QContactDetail>();
       
   512 
       
   513     // ascertain which details are supported by any implementation of the given action
       
   514     QList<QContactDetail> retn;
       
   515     QList<QContactActionDescriptor> descriptors = QContactManagerData::actionDescriptors(actionName);
       
   516     for (int i = 0; i < descriptors.size(); i++) {
       
   517         QContactAction *currImpl = QContactManagerData::action(descriptors.at(i));
       
   518         for (int i = 0; i < d->m_details.size(); i++) {
       
   519             QContactDetail detail = d->m_details.at(i);
       
   520             if (currImpl->isDetailSupported(detail, *this)) {
       
   521                 retn.append(detail);
       
   522                 break;
       
   523             }
       
   524         }
       
   525 
       
   526         // clean up
       
   527         delete currImpl;
       
   528     }
       
   529 
       
   530     return retn;
       
   531 }
       
   532 
       
   533 /*!
       
   534  * \preliminary
       
   535  * Returns a list of relationships of the given \a relationshipType in which the contact was a participant at the time that it was retrieved from the manager
       
   536  */
   641  */
   537 QList<QContactRelationship> QContact::relationships(const QString& relationshipType) const
   642 QList<QContactRelationship> QContact::relationships(const QString& relationshipType) const
   538 {
   643 {
   539     // if empty, then they want all relationships
   644     // if empty, then they want all relationships
   540     if (relationshipType.isEmpty())
   645     if (relationshipType.isEmpty())
   551 
   656 
   552     return retn;
   657     return retn;
   553 }
   658 }
   554 
   659 
   555 /*!
   660 /*!
   556   Returns a list of ids of contacts which are related to this contact in a relationship of the
   661     Returns a list of the ids of contacts which have a relationship of the given \a relationshipType with this contact.
   557   given \a relationshipType, where those other contacts participate in the relationship in the
   662     The \a role parameter describes the role that the related contacts have in the relationship.
   558   given \a role.
   663 
       
   664     If \a relationshipType is empty, relationships of all types will be considered.
       
   665 
       
   666     \note This function only examines the relationships that were present when this contact
       
   667     was retrieved from a manager.  You can also query the manager directly, if you require
       
   668     the most up to date information.
       
   669 
       
   670     \snippet doc/src/snippets/qtcontactsdocsample/qtcontactsdocsample.cpp 6
       
   671 
       
   672     \sa QContactRelationshipFetchRequest, QContactManager::relationships()
   559  */
   673  */
   560 QList<QContactId> QContact::relatedContacts(const QString& relationshipType, QContactRelationship::Role role) const
   674 QList<QContactId> QContact::relatedContacts(const QString& relationshipType, QContactRelationship::Role role) const
   561 {
   675 {
   562     QList<QContactId> retn;
   676     QList<QContactId> retn;
   563     for (int i = 0; i < d->m_relationshipsCache.size(); i++) {
   677     for (int i = 0; i < d->m_relationshipsCache.size(); i++) {
   591     }
   705     }
   592 
   706 
   593     return retn;
   707     return retn;
   594 }
   708 }
   595 
   709 
   596 /*!
       
   597  * Return a list of actions available to be performed on this contact which are offered
       
   598  * by the vendor whose name is the given \a vendorName, where the action instance has
       
   599  * the implementation version given by \a implementationVersion.
       
   600  * If \a vendorName is empty, actions from any vendor are supplied; if \a implementationVersion
       
   601  * is \c -1, action implementations of any version will be returned.
       
   602  */
       
   603 QList<QContactActionDescriptor> QContact::availableActions(const QString& vendorName, int implementationVersion) const
       
   604 {
       
   605     // check every action implementation to see if it supports me.
       
   606     QSet<QContactActionDescriptor> retn;
       
   607     QList<QContactActionDescriptor> descriptors = QContactManagerData::actionDescriptors();
       
   608     for (int i = 0; i < descriptors.size(); i++) {
       
   609         QContactActionDescriptor currDescriptor = descriptors.at(i);
       
   610         QContactAction *currImpl = QContactManagerData::action(currDescriptor);
       
   611         if (QContactManagerEngine::testFilter(currImpl->contactFilter(), *this)) {
       
   612             if ((vendorName.isEmpty() || currDescriptor.vendorName() == vendorName) &&
       
   613                     (implementationVersion == -1 || currDescriptor.implementationVersion() == implementationVersion)) {
       
   614                 retn.insert(currDescriptor);
       
   615             }
       
   616         }
       
   617 
       
   618         // clean up the implementation to avoid leak.
       
   619         delete currImpl;
       
   620     }
       
   621 
       
   622     return retn.toList();
       
   623 }
       
   624 
       
   625 /*!
       
   626  * \preliminary
       
   627  * Set a particular detail as the \a preferredDetail for a given \a actionName.  Returns
       
   628  * true if the detail exists in the contact and was successfully set as the preferred detail for the action
       
   629  * identified by \a actionName, otherwise returns false.
       
   630  * Note that since QContact is a value class, no error checking is done on the action name
       
   631  * (to ensure that an action of that name is available) in this function.
       
   632  */
       
   633 bool QContact::setPreferredDetail(const QString& actionName, const QContactDetail& preferredDetail)
       
   634 {
       
   635     // if the given action name is empty, bad argument.
       
   636     if (actionName.isEmpty())
       
   637         return false;
       
   638 
       
   639     // check to see whether the the given preferredDetail is saved in this contact
       
   640     if (!d->m_details.contains(preferredDetail))
       
   641         return false;
       
   642 
       
   643     // otherwise, save the preference.
       
   644     d->m_preferences.insert(actionName, preferredDetail.d->m_id);
       
   645     return true;
       
   646 }
       
   647 
       
   648 /*!
       
   649  * \preliminary
       
   650  *
       
   651  * Returns true if the given \a detail is a preferred detail for the given \a actionName, or for any action if the \a actionName is empty
       
   652  */
       
   653 bool QContact::isPreferredDetail(const QString& actionName, const QContactDetail& detail) const
       
   654 {
       
   655     if (!d->m_details.contains(detail))
       
   656         return false;
       
   657 
       
   658     if (actionName.isEmpty())
       
   659          return d->m_preferences.values().contains(detail.d->m_id);
       
   660 
       
   661     QMap<QString, int>::const_iterator it = d->m_preferences.find(actionName);
       
   662     if (it != d->m_preferences.end() && it.value() == detail.d->m_id)
       
   663         return true;
       
   664 
       
   665     return false;
       
   666 }
       
   667 
       
   668 /*!
       
   669  * \preliminary
       
   670  * Returns the preferred detail for a given \a actionName
       
   671  */
       
   672 QContactDetail QContact::preferredDetail(const QString& actionName) const
       
   673 {
       
   674     // if the given action name is empty, bad argument.
       
   675     if (actionName.isEmpty())
       
   676         return QContactDetail();
       
   677 
       
   678     if (!d->m_preferences.contains(actionName))
       
   679         return QContactDetail();
       
   680 
       
   681     QContactDetail retn;
       
   682     int detId = d->m_preferences.value(actionName);
       
   683     for (int i = 0; i < d->m_details.size(); i++) {
       
   684         QContactDetail det = d->m_details.at(i);
       
   685         if (det.d->m_id == detId) {
       
   686             // found it.
       
   687             retn = det;
       
   688             break;
       
   689         }
       
   690     }
       
   691 
       
   692     return retn;
       
   693 }
       
   694 
       
   695 
       
   696 
       
   697 /*!
       
   698  * \preliminary
       
   699  * Returns a map of action name to the preferred detail for the action of that name.
       
   700  */
       
   701 QMap<QString, QContactDetail> QContact::preferredDetails() const
       
   702 {
       
   703     QMap<QString, QContactDetail> ret;
       
   704     QMap<QString, int>::const_iterator it = d->m_preferences.constBegin();
       
   705     while (it != d->m_preferences.constEnd()) {
       
   706         ret.insert(it.key(), d->m_details.at(it.value()));
       
   707         ++it;
       
   708     }
       
   709 
       
   710     return ret;
       
   711 }
       
   712 
       
   713 QTM_END_NAMESPACE
   710 QTM_END_NAMESPACE