qtmobility/src/versit/qversitcontactimporter.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 8 71781823f776
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
    37 **
    37 **
    38 ** $QT_END_LICENSE$
    38 ** $QT_END_LICENSE$
    39 **
    39 **
    40 ****************************************************************************/
    40 ****************************************************************************/
    41 
    41 
       
    42 #include "qcontactmanagerengine.h"
    42 #include "qversitcontactimporter.h"
    43 #include "qversitcontactimporter.h"
    43 #include "qversitcontactimporter_p.h"
    44 #include "qversitcontactimporter_p.h"
    44 #include "qversitdocument.h"
    45 #include "qversitdocument.h"
    45 #include "qversitproperty.h"
    46 #include "qversitproperty.h"
    46 #include "qmobilityglobal.h"
    47 #include "qmobilityglobal.h"
    66 /*!
    67 /*!
    67  * \fn virtual bool QVersitContactImporterPropertyHandler::preProcessProperty(const QVersitDocument& document, const QVersitProperty& property, int contactIndex, QContact* contact) = 0;
    68  * \fn virtual bool QVersitContactImporterPropertyHandler::preProcessProperty(const QVersitDocument& document, const QVersitProperty& property, int contactIndex, QContact* contact) = 0;
    68  * Process \a property and update \a contact with the corresponding QContactDetail(s).
    69  * Process \a property and update \a contact with the corresponding QContactDetail(s).
    69  * \a document provides the context within which the property was found.
    70  * \a document provides the context within which the property was found.
    70  * \a contactIndex specifies the position that \a contact will take in the list returned by
    71  * \a contactIndex specifies the position that \a contact will take in the list returned by
    71  * \l QVersitContactImporter::importContacts().
    72  * \l QVersitContactImporter::importDocuments().
    72  *
    73  *
    73  * Returns true if the property has been handled and requires no further processing, false
    74  * Returns true if the property has been handled and requires no further processing, false
    74  * otherwise.
    75  * otherwise.
    75  *
    76  *
    76  * This function is called on every QVersitProperty encountered during an import.  Supply this
    77  * This function is called on every QVersitProperty encountered during an import.  Supply this
    80 /*!
    81 /*!
    81  * \fn virtual bool QVersitContactImporterPropertyHandler::postProcessProperty(const QVersitDocument& document, const QVersitProperty& property, bool alreadyProcessed, int contactIndex, QContact* contact) = 0;
    82  * \fn virtual bool QVersitContactImporterPropertyHandler::postProcessProperty(const QVersitDocument& document, const QVersitProperty& property, bool alreadyProcessed, int contactIndex, QContact* contact) = 0;
    82  * Process \a property and update \a contact with the corresponding QContactDetail(s).
    83  * Process \a property and update \a contact with the corresponding QContactDetail(s).
    83  * \a document provides the context within which the property was found.
    84  * \a document provides the context within which the property was found.
    84  * \a contactIndex specifies the position that \a contact will take in the list returned by
    85  * \a contactIndex specifies the position that \a contact will take in the list returned by
    85  * \l QVersitContactImporter::importContacts().
    86  * \l QVersitContactImporter::importDocuments().
    86  * \a alreadyProcessed is true if the detail has already been processed either by
    87  * \a alreadyProcessed is true if the detail has already been processed either by
    87  * \l preProcessProperty() or by QVersitContactImporter itself.
    88  * \l preProcessProperty() or by QVersitContactImporter itself.
    88  *
    89  *
    89  * Returns true if the property has been handled, false otherwise.
    90  * Returns true if the property has been handled, false otherwise.
    90  *
    91  *
   114  * \snippet ../../doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp Property handler
   115  * \snippet ../../doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp Property handler
   115  *
   116  *
   116  * An example usage of QVersitContactImporter
   117  * An example usage of QVersitContactImporter
   117  * \snippet ../../doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp Import example
   118  * \snippet ../../doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp Import example
   118  *
   119  *
       
   120  * \section1 Importing categories
       
   121  * The importer imports the vCard CATEGORIES property by converting each category to a QContactTag.
       
   122  * Some managers may not have support for QContactTag, but instead support categorization using the
       
   123  * \l{QContactRelationship::HasMember}{HasMember} QContactRelationship along with contacts of type
       
   124  * \l{QContactType::TypeGroup}{TypeGroup}.  For these backends, if the categorization information
       
   125  * needs to be retained through group relationships, extra work needs to be done to do the
       
   126  * conversion.  Below is some example code that does this translation.
       
   127  *
       
   128  * \snippet ../../doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp Import relationship example
       
   129  *
   119  * \sa QVersitDocument, QVersitReader, QVersitContactImporterPropertyHandler
   130  * \sa QVersitDocument, QVersitReader, QVersitContactImporterPropertyHandler
   120  */
   131  */
       
   132 
       
   133 /*!
       
   134   \enum QVersitContactImporter::Error
       
   135   This enum specifies an error that occurred during the most recent call to importDocuments()
       
   136   \value NoError The most recent operation was successful
       
   137   \value InvalidDocumentError One of the documents is not a vCard
       
   138   \value EmptyDocumentError One of the documents is empty
       
   139   */
   121 
   140 
   122 /*! Constructs a new importer */
   141 /*! Constructs a new importer */
   123 QVersitContactImporter::QVersitContactImporter()
   142 QVersitContactImporter::QVersitContactImporter()
   124     : d(new QVersitContactImporterPrivate)
   143     : d(new QVersitContactImporterPrivate)
   125 {
   144 {
   130 {
   149 {
   131     delete d;
   150     delete d;
   132 }
   151 }
   133 
   152 
   134 /*!
   153 /*!
   135  * Converts \a documents into a corresponding list of QContacts.
   154  * Converts \a documents into a corresponding list of QContacts.  After calling this, the converted
   136  */
   155  * contacts can be retrieved by calling contacts().
   137 QList<QContact> QVersitContactImporter::importContacts(const QList<QVersitDocument>& documents)
   156  * Returns true on success.  If any of the documents cannot be imported as contacts (eg. they aren't
   138 {
   157  * vCards), false is returned and errors() will return a list describing the errors that occurred.
   139     QList<QContact> list;
   158  * The successfully imported documents will still be available via contacts().
   140     int i = 0;
   159  *
   141     foreach (QVersitDocument document, documents) {
   160  * \sa contacts(), errors()
   142         list.append(d->importContact(document, i));
   161  */
   143         i++;
   162 bool QVersitContactImporter::importDocuments(const QList<QVersitDocument>& documents)
       
   163 {
       
   164     int documentIndex = 0;
       
   165     int contactIndex = 0;
       
   166     d->mContacts.clear();
       
   167     d->mErrors.clear();
       
   168     bool ok = true;
       
   169     foreach (const QVersitDocument& document, documents) {
       
   170         QContact contact;
       
   171         QVersitContactImporter::Error error;
       
   172         if (d->importContact(document, contactIndex, &contact, &error)) {
       
   173             d->mContacts.append(contact);
       
   174             contactIndex++;
       
   175         } else {
       
   176             d->mErrors.insert(documentIndex, error);
       
   177             ok = false;
       
   178         }
       
   179         documentIndex++;
   144     }
   180     }
   145 
   181 
   146     return list;
   182     return ok;
       
   183 }
       
   184 
       
   185 /*!
       
   186  * Returns the contacts imported in the most recent call to importDocuments().
       
   187  *
       
   188  * \sa importDocuments()
       
   189  */
       
   190 QList<QContact> QVersitContactImporter::contacts() const
       
   191 {
       
   192     return d->mContacts;
       
   193 }
       
   194 
       
   195 /*!
       
   196  * Returns the map of errors encountered in the most recent call to importDocuments().  The key is
       
   197  * the index into the input list of documents and the value is the error that occurred on that
       
   198  * document.
       
   199  *
       
   200  * \sa importDocuments()
       
   201  */
       
   202 QMap<int, QVersitContactImporter::Error> QVersitContactImporter::errors() const
       
   203 {
       
   204     return d->mErrors;
   147 }
   205 }
   148 
   206 
   149 /*!
   207 /*!
   150  * Sets \a handler to be the handler for processing QVersitProperties, or 0 to have no handler.
   208  * Sets \a handler to be the handler for processing QVersitProperties, or 0 to have no handler.
   151  *
   209  *