qtcontactsmobility/src/versit/qversitproperty.cpp
changeset 25 76a2435edfd4
parent 24 0ba2181d7c28
child 27 de1630741fbe
equal deleted inserted replaced
24:0ba2181d7c28 25:76a2435edfd4
    42 #include "qversitproperty.h"
    42 #include "qversitproperty.h"
    43 #include "qversitproperty_p.h"
    43 #include "qversitproperty_p.h"
    44 #include "qmobilityglobal.h"
    44 #include "qmobilityglobal.h"
    45 
    45 
    46 #include <QStringList>
    46 #include <QStringList>
    47 
    47 #include <QTextCodec>
    48 QTM_BEGIN_NAMESPACE
    48 
       
    49 QTM_USE_NAMESPACE
    49 
    50 
    50 /*!
    51 /*!
    51   \class QVersitProperty
    52   \class QVersitProperty
    52  
    53   \preliminary 
    53   \brief The QVersitProperty class stores the name, value and parameters of a versit property.
    54   \brief The QVersitProperty class stores the name, value, groups and parameters of a Versit property.
    54 
       
    55   \ingroup versit
    55   \ingroup versit
    56  
    56 
    57   For example a vCard can be presented as a QVersitDocument that
    57   For example a vCard can be presented as a QVersitDocument that consists of a number of properties
    58   consists of 0..n properties such as a name (N),
    58   such as a name (N), a telephone number (TEL) and an email address (EMAIL) to name a few.
    59   a telephone number (TEL) and an email address (EMAIL) to name a few.
    59   Each of these properties is stored as an instance of a QVersitProperty in a QVersitDocument.
    60   Each of these properties is stored as
       
    61   an instance of a QVersitProperty in a QVersitDocument.
       
    62  
    60  
    63   QVersitProperty supports implicit sharing.
    61   QVersitProperty supports implicit sharing.
    64   The property name and parameters of a QVersitProperty are converted
    62   The property name and parameters of a QVersitProperty are converted to upper-case when they are
    65   to upper-case when they are stored to a QVersitProperty.
    63   stored to a QVersitProperty.
    66   The value of a QVersitProperty is raw data and it is case-sensitive.
    64 
       
    65   The value of a QVersitProperty is stored as a QVariant and should always be one of three types:
       
    66   QString for textual values, QByteArray for binary data (eg. images), or QVersitDocument for
       
    67   nested documents.  The \l QVersitReader will parse Versit properties and assign the correct type
       
    68   of object to the property value.  The \l QVersitWriter will serialise objects of these types
       
    69   correctly into the (text-based) Versit format.
    67  
    70  
    68   \sa QVersitDocument
    71   \sa QVersitDocument
    69  */
    72  */
    70 
    73 
    71 
    74 
    88 QVersitProperty& QVersitProperty::operator=(const QVersitProperty& other)
    91 QVersitProperty& QVersitProperty::operator=(const QVersitProperty& other)
    89 {
    92 {
    90     if (this != &other)
    93     if (this != &other)
    91         d = other.d;
    94         d = other.d;
    92     return *this;    
    95     return *this;    
       
    96 }
       
    97 
       
    98 /*! Returns true if this is equal to \a other; false otherwise. */
       
    99 bool QVersitProperty::operator==(const QVersitProperty& other) const
       
   100 {
       
   101     return d->mGroups == other.d->mGroups &&
       
   102             d->mName == other.d->mName &&
       
   103             d->mParameters == other.d->mParameters &&
       
   104             d->mValue == other.d->mValue;
       
   105 }
       
   106 
       
   107 /*! Returns true if this is not equal to \a other; false otherwise. */
       
   108 bool QVersitProperty::operator!=(const QVersitProperty& other) const
       
   109 {
       
   110     return !(*this == other);
    93 }
   111 }
    94 
   112 
    95 /*!
   113 /*!
    96  * Sets the groups in the property to the given list of \a groups.
   114  * Sets the groups in the property to the given list of \a groups.
    97  */
   115  */
   102         d->mGroups.append(group);
   120         d->mGroups.append(group);
   103     }
   121     }
   104 }
   122 }
   105 
   123 
   106 /*!
   124 /*!
   107  * Gets the groups part of the property.
   125  * Gets the groups of the property.
   108  */
   126  */
   109 QStringList QVersitProperty::groups() const
   127 QStringList QVersitProperty::groups() const
   110 {
   128 {
   111     return d->mGroups;
   129     return d->mGroups;
   112 }
   130 }
   141     for (int i=keys.count()-1; i >= 0; i--) {
   159     for (int i=keys.count()-1; i >= 0; i--) {
   142         QString key = keys.at(i);
   160         QString key = keys.at(i);
   143         QList<QString> values = parameters.values(key);
   161         QList<QString> values = parameters.values(key);
   144         for (int j=values.count()-1; j >= 0; j--) {
   162         for (int j=values.count()-1; j >= 0; j--) {
   145             // Convert all the parameter names and values to upper case
   163             // Convert all the parameter names and values to upper case
   146             addParameter(key,values.at(j));
   164             insertParameter(key,values.at(j));
   147         }
   165         }
   148     }
   166     }
   149 }
   167 }
   150 
   168 
   151 /*!
   169 /*!
   152  * Adds a new parameter with \a name and \a value.
   170  * Adds a new parameter with \a name and \a value.
   153  * Both the name and the value are converted to upper-case.
   171  * Both the name and the value are converted to upper-case.
   154  */
   172  */
   155 void QVersitProperty::addParameter(const QString& name, const QString& value)
   173 void QVersitProperty::insertParameter(const QString& name, const QString& value)
   156 {
   174 {
   157     d->mParameters.insert(name.toUpper(),value.toUpper());
   175     d->mParameters.insert(name.toUpper(), value.toUpper());
   158 }
   176 }
   159 
   177 
   160 /*!
   178 /*!
   161  * Removes a parameter with \a name and \a value.
   179  * Removes a parameter with \a name and \a value.
       
   180  *
       
   181  * \sa removeParameters()
   162  */
   182  */
   163 void QVersitProperty::removeParameter(const QString& name, const QString& value)
   183 void QVersitProperty::removeParameter(const QString& name, const QString& value)
   164 {
   184 {
   165     d->mParameters.remove(name.toUpper(),value.toUpper());
   185     d->mParameters.remove(name.toUpper(), value.toUpper());
       
   186 }
       
   187 
       
   188 /*!
       
   189  * Removes all parameters with the given \a name.
       
   190  *
       
   191  * \sa removeParameter()
       
   192  */
       
   193 void QVersitProperty::removeParameters(const QString& name)
       
   194 {
       
   195     d->mParameters.remove(name.toUpper());
   166 }
   196 }
   167 
   197 
   168 /*!
   198 /*!
   169  * Return a copy of the contained list of parameters.
   199  * Return a copy of the contained list of parameters.
   170  * Note that actual the parameters cannot be modified using the copy.
   200  * Note that actual the parameters cannot be modified using the copy.
   173 {
   203 {
   174     return d->mParameters;
   204     return d->mParameters;
   175 }
   205 }
   176 
   206 
   177 /*!
   207 /*!
   178  * Sets the \a value of the property.
   208  * Sets the property value to \a value.
   179  */
   209  */
   180 void QVersitProperty::setValue(const QByteArray& value)
   210 void QVersitProperty::setValue(const QVariant& value)
   181 {
   211 {
   182     d->mValue = value;
   212     d->mValue = value;
   183 }
   213 }
   184 
   214 
   185 /*!
   215 /*!
   186  * Returns the value of the property.
   216  * Returns the value of the property.
   187  */
   217  */
   188 QByteArray QVersitProperty::value() const
   218 QVariant QVersitProperty::variantValue() const
   189 {
   219 {
   190     return d->mValue;
   220     return d->mValue;
   191 }
   221 }
   192 
   222 
   193 /*!
   223 /*!
   194  * Sets the embedded \a document of the property
   224  * \fn T QVersitProperty::value() const
   195  */
   225  * \overload
       
   226  * Returns the value of the property as a \tt T.
       
   227  */
       
   228 
       
   229 /*!
       
   230  * Returns the value of the property as a string if possible, otherwise return an empty string.
       
   231  * If the property is stored as a QByteArray, it is decoded using the charset specified in the
       
   232  * property's parameters.
       
   233  * \sa QVariant::toString()
       
   234  */
       
   235 QString QVersitProperty::value() const
       
   236 {
       
   237     if (d->mValue.type() == QVariant::ByteArray) {
       
   238         if (d->mParameters.contains(QLatin1String("CHARSET"))) {
       
   239             QTextCodec* codec = QTextCodec::codecForName(
       
   240                     d->mParameters.value(QLatin1String("CHARSET")).toAscii());
       
   241             if (codec != NULL) {
       
   242                 return codec->toUnicode(d->mValue.toByteArray());
       
   243             }
       
   244         }
       
   245         return QString();
       
   246     } else {
       
   247         return d->mValue.toString();
       
   248     }
       
   249 }
       
   250 
       
   251 /*!
       
   252  * Returns true if the property is empty.
       
   253  */
       
   254 bool QVersitProperty::isEmpty() const
       
   255 {
       
   256     return d->mGroups.isEmpty()
       
   257             && d->mName.isEmpty()
       
   258             && d->mParameters.isEmpty()
       
   259             && !d->mValue.isValid();
       
   260 }
       
   261 
       
   262 /*!
       
   263  * Clears the contents of this property.
       
   264  */
       
   265 void QVersitProperty::clear()
       
   266 {
       
   267     d->mGroups.clear();
       
   268     d->mName.clear();
       
   269     d->mValue.clear();
       
   270     d->mParameters.clear();
       
   271 }
       
   272 
       
   273 /*! \internal */
       
   274 void QVersitProperty::addParameter(const QString& name, const QString& value)
       
   275 {
       
   276     Q_UNUSED(name)
       
   277     Q_UNUSED(value)
       
   278 }
       
   279 
       
   280 /*! \internal */
   196 void QVersitProperty::setEmbeddedDocument(const QVersitDocument& document)
   281 void QVersitProperty::setEmbeddedDocument(const QVersitDocument& document)
   197 {
   282 {
   198     d->mDocument = document;
   283     setValue(QVariant::fromValue(document));
   199 }
   284 }
   200 
   285 
   201 /*!
   286 /*! \internal */
   202  * Returns the embedded document of the property.
       
   203  * If the embedded document has not been set, an empty document is returned.
       
   204  */
       
   205 QVersitDocument QVersitProperty::embeddedDocument() const
   287 QVersitDocument QVersitProperty::embeddedDocument() const
   206 {
   288 {
   207     return d->mDocument;
   289     return value<QVersitDocument>();
   208 }
   290 }
   209 
       
   210 QTM_END_NAMESPACE