src/versit/qversitdocument.cpp
changeset 0 876b1a06bc25
child 5 603d3f8b6302
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 "qversitdocument.h"
       
    43 #include "qversitdocument_p.h"
       
    44 #include "qmobilityglobal.h"
       
    45 
       
    46 #include <QTextCodec>
       
    47 
       
    48 QTM_BEGIN_NAMESPACE
       
    49 
       
    50 /*!
       
    51   \class QVersitDocument
       
    52   \brief The QVersitDocument class is a container for a list of versit properties.
       
    53   \ingroup versit
       
    54 
       
    55   A vCard is represented in abstract form as a QVersitDocument that consists of a number of
       
    56   properties such as a name (N), a telephone number (TEL) and an email address (EMAIL), for
       
    57   instance.  Each of these properties is stored as an instance of a QVersitProperty in a
       
    58   QVersitDocument.
       
    59 
       
    60   In addition to the list of properties, QVersitDocument also records the type of the Versit
       
    61   document via the VersitType field.  This enum describes the format in which the document is to be
       
    62   serialized by QVersitWriter (or the format from which it was read by QVersitReader), and should
       
    63   not be used to infer any semantics about the document data.
       
    64 
       
    65   QVersitDocument supports implicit sharing.
       
    66 
       
    67   \sa QVersitProperty
       
    68  */
       
    69 
       
    70 /*!
       
    71   \enum QVersitDocument::VersitType
       
    72   This enum describes a versit document type and version.
       
    73   \value InvalidType No type specified or a document with an invalid type was parsed
       
    74   \value VCard21Type vCard version 2.1
       
    75   \value VCard30Type vCard version 3.0
       
    76  */
       
    77 
       
    78 /*! Constructs a new empty document */
       
    79 QVersitDocument::QVersitDocument() : d(new QVersitDocumentPrivate())
       
    80 {
       
    81 }
       
    82 
       
    83 /*! Constructs a new empty document with the type set to \a type */
       
    84 QVersitDocument::QVersitDocument(VersitType type) : d(new QVersitDocumentPrivate())
       
    85 {
       
    86     d->mVersitType = type;
       
    87 }
       
    88 
       
    89 
       
    90 /*! Constructs a document that is a copy of \a other */
       
    91 QVersitDocument::QVersitDocument(const QVersitDocument& other) : d(other.d)
       
    92 {
       
    93 }
       
    94 
       
    95 /*! Frees the memory used by the document */
       
    96 QVersitDocument::~QVersitDocument()
       
    97 {
       
    98 }
       
    99 
       
   100 /*! Assigns this document to \a other */
       
   101 QVersitDocument& QVersitDocument::operator=(const QVersitDocument& other)
       
   102 {
       
   103     if (this != &other)
       
   104         d = other.d;
       
   105     return *this;
       
   106 }
       
   107 
       
   108 /*! Returns true if this is equal to \a other; false otherwise. */
       
   109 bool QVersitDocument::operator==(const QVersitDocument& other) const
       
   110 {
       
   111     return d->mVersitType == other.d->mVersitType &&
       
   112             d->mProperties == other.d->mProperties;
       
   113 }
       
   114 
       
   115 /*! Returns true if this is not equal to \a other; false otherwise. */
       
   116 bool QVersitDocument::operator!=(const QVersitDocument& other) const
       
   117 {
       
   118     return !(*this == other);
       
   119 }
       
   120 
       
   121 /*! Returns the hash value for \a key. */
       
   122 uint qHash(const QVersitDocument &key)
       
   123 {
       
   124     int hash = QT_PREPEND_NAMESPACE(qHash)(key.type());
       
   125     foreach (const QVersitProperty& property, key.properties()) {
       
   126         hash += qHash(property);
       
   127     }
       
   128     return hash;
       
   129 }
       
   130 
       
   131 #ifndef QT_NO_DEBUG_STREAM
       
   132 QDebug operator<<(QDebug dbg, const QVersitDocument& document)
       
   133 {
       
   134     dbg.nospace() << "QVersitDocument(" << document.type() << ')';
       
   135     foreach (const QVersitProperty& property, document.properties()) {
       
   136         dbg.space() << '\n' << property;
       
   137     }
       
   138     return dbg.maybeSpace();
       
   139 }
       
   140 #endif
       
   141 
       
   142 /*!
       
   143  * Sets the versit document type to \a type.
       
   144  */
       
   145 void QVersitDocument::setType(VersitType type)
       
   146 {
       
   147     d->mVersitType = type;
       
   148 }
       
   149 
       
   150 /*!
       
   151  * Gets the versit document type.
       
   152  */
       
   153 QVersitDocument::VersitType QVersitDocument::type() const
       
   154 {
       
   155     return d->mVersitType;
       
   156 }
       
   157 
       
   158 /*!
       
   159  * Add \a property to the list of contained versit properties.
       
   160  * The property is appended as the last property of the list.
       
   161  */
       
   162 void QVersitDocument::addProperty(const QVersitProperty& property)
       
   163 {
       
   164     d->mProperties.append(property);
       
   165 }
       
   166 
       
   167 /*!
       
   168  * Removes the property \a property from the versit document.
       
   169  */
       
   170 void QVersitDocument::removeProperty(const QVersitProperty& property)
       
   171 {
       
   172     d->mProperties.removeAll(property);
       
   173 }
       
   174 
       
   175 /*!
       
   176  * Removes all the properties with the given \a name from the versit document.
       
   177  */
       
   178 void QVersitDocument::removeProperties(const QString& name)
       
   179 {
       
   180     for (int i=d->mProperties.count()-1; i >=0; i--) {
       
   181         if (d->mProperties[i].name() == name) {
       
   182             d->mProperties.removeAt(i);
       
   183         }
       
   184     }
       
   185 }
       
   186 
       
   187 /*!
       
   188  * Clears the document, removing all properties and metadata
       
   189  * and resetting the codec to the default.
       
   190  */
       
   191 void QVersitDocument::clear()
       
   192 {
       
   193     d->mProperties.clear();
       
   194     d->mVersitType = QVersitDocument::InvalidType;
       
   195 }
       
   196 
       
   197 /*!
       
   198  * Gets the list of the contained versit properties.
       
   199  * Note that the actual properties cannot be modified using the copy.
       
   200  */
       
   201 QList<QVersitProperty> QVersitDocument::properties() const
       
   202 {
       
   203     return d->mProperties;
       
   204 }
       
   205 
       
   206 /*!
       
   207  * Returns true if the document is empty.
       
   208  */
       
   209 bool QVersitDocument::isEmpty() const
       
   210 {
       
   211     return d->mProperties.count() == 0 && d->mVersitType == QVersitDocument::InvalidType;
       
   212 }
       
   213 
       
   214 QTM_END_NAMESPACE