src/versit/qversitcontactexporter_p.cpp
changeset 5 603d3f8b6302
parent 0 876b1a06bc25
equal deleted inserted replaced
3:e4ebb16b39ea 5:603d3f8b6302
    39 **
    39 **
    40 ****************************************************************************/
    40 ****************************************************************************/
    41 
    41 
    42 #include "qversitcontactexporter.h"
    42 #include "qversitcontactexporter.h"
    43 #include "qversitcontactexporter_p.h"
    43 #include "qversitcontactexporter_p.h"
    44 #include "qversitdefs_p.h"
    44 #include "qversitcontactsdefs_p.h"
    45 #include "versitutils_p.h"
    45 #include "versitutils_p.h"
    46 #include "qmobilityglobal.h"
    46 #include "qmobilityglobal.h"
    47 
    47 
    48 #include <qcontact.h>
    48 #include <qcontact.h>
    49 #include <qcontactdetail.h>
    49 #include <qcontactdetail.h>
    64 #include <qcontactanniversary.h>
    64 #include <qcontactanniversary.h>
    65 #include <qcontactonlineaccount.h>
    65 #include <qcontactonlineaccount.h>
    66 #include <qcontactfamily.h>
    66 #include <qcontactfamily.h>
    67 #include <qcontactdisplaylabel.h>
    67 #include <qcontactdisplaylabel.h>
    68 #include <qcontactthumbnail.h>
    68 #include <qcontactthumbnail.h>
       
    69 #include "qversitcontacthandler.h"
       
    70 #include "qversitcontactpluginloader_p.h"
    69 
    71 
    70 #include <QUrl>
    72 #include <QUrl>
    71 #include <QBuffer>
    73 #include <QBuffer>
    72 
    74 
    73 QTM_USE_NAMESPACE
    75 QTM_USE_NAMESPACE
    74 
    76 
    75 /*!
    77 /*!
    76  * Constructor.
    78  * Constructor.
    77  */
    79  */
    78 QVersitContactExporterPrivate::QVersitContactExporterPrivate() :
    80 QVersitContactExporterPrivate::QVersitContactExporterPrivate(const QString& profile) :
    79     mDetailHandler(NULL),
    81     mDetailHandler(NULL),
    80     mDetailHandler2(NULL),
    82     mDetailHandler2(NULL),
    81     mDetailHandlerVersion(0),
    83     mDetailHandlerVersion(0),
    82     mDefaultResourceHandler(new QVersitDefaultResourceHandler),
    84     mDefaultResourceHandler(new QVersitDefaultResourceHandler),
    83     mResourceHandler(mDefaultResourceHandler),
    85     mResourceHandler(mDefaultResourceHandler)
    84     mVersitType(QVersitDocument::InvalidType)
       
    85 {
    86 {
    86     // Detail mappings
    87     // Detail mappings
    87     int versitPropertyCount =
    88     int versitPropertyCount =
    88         sizeof(versitContactDetailMappings)/sizeof(VersitContactDetailMapping);
    89         sizeof(versitContactDetailMappings)/sizeof(VersitDetailMapping);
    89     for (int i=0; i < versitPropertyCount; i++) {
    90     for (int i=0; i < versitPropertyCount; i++) {
    90         mPropertyMappings.insert(
    91         mPropertyMappings.insert(
    91                 QLatin1String(versitContactDetailMappings[i].contactDetailDefinitionName),
    92                 QLatin1String(versitContactDetailMappings[i].detailDefinitionName),
    92                 QLatin1String(versitContactDetailMappings[i].versitPropertyName));
    93                 QLatin1String(versitContactDetailMappings[i].versitPropertyName));
    93     }
    94     }
    94 
    95 
    95     // Contexts mappings
    96     // Contexts mappings
    96     int contextCount = sizeof(versitContextMappings)/sizeof(VersitMapping);
    97     int contextCount = sizeof(versitContextMappings)/sizeof(VersitMapping);
   105     for (int i=0; i < subTypeCount; i++) {
   106     for (int i=0; i < subTypeCount; i++) {
   106         mParameterMappings.insert(
   107         mParameterMappings.insert(
   107                 QLatin1String(versitSubTypeMappings[i].contactString),
   108                 QLatin1String(versitSubTypeMappings[i].contactString),
   108                 QLatin1String(versitSubTypeMappings[i].versitString));
   109                 QLatin1String(versitSubTypeMappings[i].versitString));
   109     }
   110     }
       
   111 
       
   112     mPluginDetailHandlers = QVersitContactPluginLoader::instance()->createContactHandlers(profile);
   110 }
   113 }
   111 
   114 
   112 /*!
   115 /*!
   113  * Destructor.
   116  * Destructor.
   114  */
   117  */
   124 bool QVersitContactExporterPrivate::exportContact(
   127 bool QVersitContactExporterPrivate::exportContact(
   125     const QContact& contact,
   128     const QContact& contact,
   126     QVersitDocument& document,
   129     QVersitDocument& document,
   127     QVersitContactExporter::Error* error)
   130     QVersitContactExporter::Error* error)
   128 {
   131 {
   129     mVersitType = document.type();
       
   130     QList<QContactDetail> allDetails = contact.details();
   132     QList<QContactDetail> allDetails = contact.details();
   131     if (allDetails.isEmpty()) {
   133     if (allDetails.isEmpty()) {
   132         *error = QVersitContactExporter::EmptyContactError;
   134         *error = QVersitContactExporter::EmptyContactError;
   133         return false;
   135         return false;
   134     }
   136     }
   135     for (int i = 0; i < allDetails.size(); i++) {
   137     foreach (const QContactDetail& detail, allDetails) {
   136         QContactDetail detail = allDetails.at(i);
       
   137 
       
   138         // If the custom detail handler handles it, we don't have to.
   138         // If the custom detail handler handles it, we don't have to.
   139         if (mDetailHandler
   139         if (mDetailHandler
   140             && mDetailHandler->preProcessDetail(contact, detail, &document))
   140             && mDetailHandler->preProcessDetail(contact, detail, &document))
   141             continue;
   141             continue;
   142 
   142 
   186             encodeFamily(detail, &generatedProperties, &processedFields);
   186             encodeFamily(detail, &generatedProperties, &processedFields);
   187         } else if (detail.definitionName() == QContactDisplayLabel::DefinitionName) {
   187         } else if (detail.definitionName() == QContactDisplayLabel::DefinitionName) {
   188             encodeDisplayLabel(detail, document, &removedProperties, &generatedProperties, &processedFields);
   188             encodeDisplayLabel(detail, document, &removedProperties, &generatedProperties, &processedFields);
   189         }
   189         }
   190 
   190 
       
   191         // run plugin handlers
       
   192         foreach (QVersitContactExporterDetailHandlerV2* handler, mPluginDetailHandlers) {
       
   193             handler->detailProcessed(contact, detail, document,
       
   194                                      &processedFields, &removedProperties, &generatedProperties);
       
   195         }
       
   196         // run the v2 handler, if set
   191         if (mDetailHandler2 && mDetailHandlerVersion > 1) {
   197         if (mDetailHandler2 && mDetailHandlerVersion > 1) {
   192             mDetailHandler2->detailProcessed(contact, detail, processedFields, document,
   198             mDetailHandler2->detailProcessed(contact, detail, document,
   193                                              &removedProperties, &generatedProperties);
   199                                              &processedFields, &removedProperties, &generatedProperties);
   194         }
   200         }
   195 
   201 
   196         foreach(const QVersitProperty& property, removedProperties) {
   202         foreach(const QVersitProperty& property, removedProperties) {
   197             document.removeProperty(property);
   203             document.removeProperty(property);
   198         }
   204         }
   203         if (mDetailHandler && mDetailHandlerVersion == 1) {
   209         if (mDetailHandler && mDetailHandlerVersion == 1) {
   204             mDetailHandler->postProcessDetail(contact, detail, !processedFields.isEmpty(), &document);
   210             mDetailHandler->postProcessDetail(contact, detail, !processedFields.isEmpty(), &document);
   205         }
   211         }
   206     }
   212     }
   207 
   213 
       
   214     // run plugin handlers
       
   215     foreach (QVersitContactExporterDetailHandlerV2* handler, mPluginDetailHandlers) {
       
   216         handler->contactProcessed(contact, &document);
       
   217     }
       
   218     // run the v2 handler, if set
   208     if (mDetailHandler2 && mDetailHandlerVersion > 1) {
   219     if (mDetailHandler2 && mDetailHandlerVersion > 1) {
   209         mDetailHandler2->contactProcessed(contact, &document);
   220         mDetailHandler2->contactProcessed(contact, &document);
   210     }
   221     }
   211 
   222 
   212     // Search through the document for FN or N properties.  This will find it even if it was added
   223     // Search through the document for FN or N properties.  This will find it even if it was added
   259         *generatedProperties << property;
   270         *generatedProperties << property;
   260     }
   271     }
   261 
   272 
   262     // Generate an FN field if none is already there
   273     // Generate an FN field if none is already there
   263     // Don't override previously exported FN properties (eg. exported by a DisplayLabel detail)
   274     // Don't override previously exported FN properties (eg. exported by a DisplayLabel detail)
   264     QVersitProperty fnProperty = takeProperty(document, QLatin1String("FN"), removedProperties);
   275     QVersitProperty fnProperty =
       
   276         VersitUtils::takeProperty(document, QLatin1String("FN"), removedProperties);
   265     if (fnProperty.value().isEmpty()) {
   277     if (fnProperty.value().isEmpty()) {
   266         fnProperty.setName(QLatin1String("FN"));
   278         fnProperty.setName(QLatin1String("FN"));
   267         if (!contactName.customLabel().isEmpty()) {
   279         if (!contactName.customLabel().isEmpty()) {
   268             fnProperty.setValue(contactName.customLabel());
   280             fnProperty.setValue(contactName.customLabel());
   269         } else if (!contactName.firstName().isEmpty()) {
   281         } else if (!contactName.firstName().isEmpty()) {
   628     QList<QVersitProperty>* removedProperties,
   640     QList<QVersitProperty>* removedProperties,
   629     QList<QVersitProperty>* generatedProperties,
   641     QList<QVersitProperty>* generatedProperties,
   630     QSet<QString>* processedFields)
   642     QSet<QString>* processedFields)
   631 {
   643 {
   632     QContactNickname nicknameDetail = static_cast<QContactNickname>(detail);
   644     QContactNickname nicknameDetail = static_cast<QContactNickname>(detail);
   633     QVersitProperty property = takeProperty(document, QLatin1String("X-NICKNAME"), removedProperties);
   645     QVersitProperty property =
       
   646         VersitUtils::takeProperty(document, QLatin1String("X-NICKNAME"), removedProperties);
   634     property.setName(QLatin1String("X-NICKNAME"));
   647     property.setName(QLatin1String("X-NICKNAME"));
   635     QStringList value(property.variantValue().toStringList());
   648     QStringList value(property.variantValue().toStringList());
   636     value.append(nicknameDetail.nickname());
   649     value.append(nicknameDetail.nickname());
   637     property.setValue(value);
   650     property.setValue(value);
   638     property.setValueType(QVersitProperty::ListType);
   651     property.setValueType(QVersitProperty::ListType);
   649     QList<QVersitProperty>* removedProperties,
   662     QList<QVersitProperty>* removedProperties,
   650     QList<QVersitProperty>* generatedProperties,
   663     QList<QVersitProperty>* generatedProperties,
   651     QSet<QString>* processedFields)
   664     QSet<QString>* processedFields)
   652 {
   665 {
   653     QContactTag tagDetail = static_cast<QContactTag>(detail);
   666     QContactTag tagDetail = static_cast<QContactTag>(detail);
   654     QVersitProperty property = takeProperty(document, QLatin1String("CATEGORIES"), removedProperties);
   667     QVersitProperty property =
       
   668         VersitUtils::takeProperty(document, QLatin1String("CATEGORIES"), removedProperties);
   655     property.setName(QLatin1String("CATEGORIES"));
   669     property.setName(QLatin1String("CATEGORIES"));
   656     QStringList value(property.variantValue().toStringList());
   670     QStringList value(property.variantValue().toStringList());
   657     value.append(tagDetail.tag());
   671     value.append(tagDetail.tag());
   658     property.setValue(value);
   672     property.setValue(value);
   659     property.setValueType(QVersitProperty::ListType);
   673     property.setValueType(QVersitProperty::ListType);
   743     QList<QVersitProperty>* removedProperties,
   757     QList<QVersitProperty>* removedProperties,
   744     QList<QVersitProperty>* generatedProperties,
   758     QList<QVersitProperty>* generatedProperties,
   745     QSet<QString>* processedFields)
   759     QSet<QString>* processedFields)
   746 {
   760 {
   747     // Override any previous FN property
   761     // Override any previous FN property
   748     QVersitProperty property = takeProperty(document, QLatin1String("FN"), removedProperties);
   762     QVersitProperty property =
       
   763         VersitUtils::takeProperty(document, QLatin1String("FN"), removedProperties);
   749     property.setName(mPropertyMappings.value(detail.definitionName()));
   764     property.setName(mPropertyMappings.value(detail.definitionName()));
   750     QContactDisplayLabel displayLabel = static_cast<QContactDisplayLabel>(detail);
   765     QContactDisplayLabel displayLabel = static_cast<QContactDisplayLabel>(detail);
   751     if (!displayLabel.label().isEmpty()) {
   766     if (!displayLabel.label().isEmpty()) {
   752         property.setValue(displayLabel.label());
   767         property.setValue(displayLabel.label());
   753         *generatedProperties << property;
   768         *generatedProperties << property;
   754         *processedFields << QContactDisplayLabel::FieldLabel;
   769         *processedFields << QContactDisplayLabel::FieldLabel;
   755     }
   770     }
   756 }
       
   757 
       
   758 /*!
       
   759  * Finds a property in the \a document with the given \a propertyName, removes it and returns it.
       
   760  */
       
   761 QVersitProperty QVersitContactExporterPrivate::takeProperty(const QVersitDocument& document,
       
   762                                                             const QString& propertyName,
       
   763                                                             QList<QVersitProperty>* toBeRemoved) {
       
   764     foreach (const QVersitProperty& currentProperty, document.properties()) {
       
   765         if (currentProperty.name() == propertyName) {
       
   766             *toBeRemoved << currentProperty;
       
   767             return currentProperty;
       
   768         }
       
   769     }
       
   770     return QVersitProperty();
       
   771 }
   771 }
   772 
   772 
   773 /*!
   773 /*!
   774  * Check if \a resourceIdentifier represents a valid remote resource
   774  * Check if \a resourceIdentifier represents a valid remote resource
   775  */
   775  */