src/versit/qversitwriter.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 "qversitwriter.h"
       
    43 #include "qversitwriter_p.h"
       
    44 #include "versitutils_p.h"
       
    45 #include "qmobilityglobal.h"
       
    46 
       
    47 #include <QStringList>
       
    48 #include <QTextCodec>
       
    49 #include <QBuffer>
       
    50 
       
    51 QTM_USE_NAMESPACE
       
    52 
       
    53 /*!
       
    54   \class QVersitWriter
       
    55   \brief The QVersitWriter class writes Versit documents such as vCards to a device.
       
    56   \ingroup versit
       
    57 
       
    58   QVersitWriter converts a QVersitDocument into its textual representation.
       
    59   QVersitWriter supports writing to an abstract I/O device
       
    60   which can be for example a file or a memory buffer.
       
    61   The writing can be done asynchronously and the waitForFinished()
       
    62   function can be used to implement a blocking write.
       
    63 
       
    64   The serialization of the document is done in accordance with the type of the QVersitDocument
       
    65   being written.  The value of each QVersitProperty is encoded according to the type of object:
       
    66   \list
       
    67   \o \l{QString}{QStrings} are serialized verbatim, unless the default codec of the writer cannot
       
    68   encode the string: in this case, UTF-8 is used to encode it (and the CHARSET parameter added to
       
    69   the property, as per the vCard 2.1 specification).  If the document type is vCard 2.1,
       
    70   quoted-printable encoding may also be performed.
       
    71   \o \l{QByteArray}{QByteArrays} are assumed to be binary data and are serialized as base-64 encoded
       
    72   values.
       
    73   \o \l{QVersitDocument}{QVersitDocuments} are serialized as a nested document (eg. as per the
       
    74   AGENT property in vCard).
       
    75   \endlist
       
    76 
       
    77   \sa QVersitDocument, QVersitProperty
       
    78  */
       
    79 
       
    80 
       
    81 /*!
       
    82  * \enum QVersitWriter::Error
       
    83  * This enum specifies an error that occurred during the most recent operation:
       
    84  * \value NoError The most recent operation was successful
       
    85  * \value UnspecifiedError The most recent operation failed for an undocumented reason
       
    86  * \value IOError The most recent operation failed because of a problem with the device
       
    87  * \value OutOfMemoryError The most recent operation failed due to running out of memory
       
    88  * \value NotReadyError The most recent operation failed because there is an operation in progress
       
    89  */
       
    90 
       
    91 /*!
       
    92  * \enum QVersitWriter::State
       
    93  * Enumerates the various states that a reader may be in at any given time
       
    94  * \value InactiveState Write operation not yet started
       
    95  * \value ActiveState Write operation started, not yet finished
       
    96  * \value CanceledState Write operation is finished due to cancelation
       
    97  * \value FinishedState Write operation successfully completed
       
    98  */
       
    99 
       
   100 /*!
       
   101  * \fn QVersitWriter::stateChanged(QVersitWriter::State state)
       
   102  * The signal is emitted by the writer when its state has changed (eg. when it has finished
       
   103  * writing to the device).
       
   104  * \a state is the new state of the writer.
       
   105  */
       
   106 
       
   107 /*! Constructs a new writer. */
       
   108 QVersitWriter::QVersitWriter() : d(new QVersitWriterPrivate)
       
   109 {
       
   110     d->init(this);
       
   111 }
       
   112 
       
   113 /*! Constructs a new writer that writes to \a outputDevice. */
       
   114 QVersitWriter::QVersitWriter(QIODevice *outputDevice) : d(new QVersitWriterPrivate)
       
   115 {
       
   116     d->init(this);
       
   117     d->mIoDevice = outputDevice;
       
   118 }
       
   119 
       
   120 /*! Constructs a new writer that appends to \a outputBytes. */
       
   121 QVersitWriter::QVersitWriter(QByteArray *outputBytes) : d(new QVersitWriterPrivate)
       
   122 {
       
   123     d->init(this);
       
   124     d->mOutputBytes.reset(new QBuffer);
       
   125     d->mOutputBytes->setBuffer(outputBytes);
       
   126     d->mOutputBytes->open(QIODevice::WriteOnly);
       
   127     d->mIoDevice = d->mOutputBytes.data();
       
   128 }
       
   129 
       
   130 /*!
       
   131  * Frees the memory used by the writer.
       
   132  * Waits until a pending asynchronous writing has been completed.
       
   133  */
       
   134 QVersitWriter::~QVersitWriter()
       
   135 {
       
   136     d->wait();
       
   137     delete d;
       
   138 }
       
   139 
       
   140 /*!
       
   141  * Sets the device used for writing to \a device.
       
   142  * Does not take ownership of the device.
       
   143  */
       
   144 void QVersitWriter::setDevice(QIODevice* device)
       
   145 {
       
   146     d->mOutputBytes.reset(0);
       
   147     d->mIoDevice = device;
       
   148 }
       
   149 
       
   150 /*!
       
   151  * Returns the device used for writing, or 0 if no device has been set.
       
   152  */
       
   153 QIODevice* QVersitWriter::device() const
       
   154 {
       
   155     if (d->mOutputBytes.isNull())
       
   156         return d->mIoDevice;
       
   157     else
       
   158         return 0;
       
   159 }
       
   160 
       
   161 /*!
       
   162  * Sets the default codec for the writer to use for writing the entire output.
       
   163  *
       
   164  * If \a codec is NULL, the writer uses the codec according to the specification prescribed default.
       
   165  * (for vCard 2.1, ASCII; for vCard 3.0, UTF-8).
       
   166  */
       
   167 void QVersitWriter::setDefaultCodec(QTextCodec *codec)
       
   168 {
       
   169     d->mDefaultCodec = codec;
       
   170 }
       
   171 
       
   172 /*!
       
   173  * Returns the document's codec.
       
   174  */
       
   175 QTextCodec* QVersitWriter::defaultCodec() const
       
   176 {
       
   177     return d->mDefaultCodec;
       
   178 }
       
   179 
       
   180 /*!
       
   181  * Returns the state of the writer.
       
   182  */
       
   183 QVersitWriter::State QVersitWriter::state() const
       
   184 {
       
   185     return d->state();
       
   186 }
       
   187 
       
   188 /*!
       
   189  * Returns the error encountered by the last operation.
       
   190  */
       
   191 QVersitWriter::Error QVersitWriter::error() const
       
   192 {
       
   193     return d->error();
       
   194 }
       
   195 
       
   196 /*!
       
   197  * Starts writing \a input to device() asynchronously.
       
   198  * Returns false if the output device has not been set or opened or
       
   199  * if there is another asynchronous write operation already pending.
       
   200  * Signal \l stateChanged() is emitted with parameter FinishedState
       
   201  * when the writing has finished.
       
   202  */
       
   203 bool QVersitWriter::startWriting(const QList<QVersitDocument>& input)
       
   204 {
       
   205     d->mInput = input;
       
   206     if (d->state() == ActiveState || d->isRunning()) {
       
   207         d->setError(QVersitWriter::NotReadyError);
       
   208         return false;
       
   209     } else if (!d->mIoDevice || !d->mIoDevice->isWritable()) {
       
   210         d->setError(QVersitWriter::IOError);
       
   211         return false;
       
   212     } else {
       
   213         d->setState(ActiveState);
       
   214         d->setError(NoError);
       
   215         d->start();
       
   216         return true;
       
   217     }
       
   218 }
       
   219 
       
   220 /*!
       
   221  * Attempts to asynchronously cancel the write request.
       
   222  */
       
   223 void QVersitWriter::cancel()
       
   224 {
       
   225     d->setCanceling(true);
       
   226 }
       
   227 
       
   228 /*!
       
   229  * If the state is ActiveState, blocks until the writer has finished writing or \a msec milliseconds
       
   230  * has elapsed, returning true if it successfully finishes or is cancelled by the user.
       
   231  * If the state is FinishedState, returns true immediately.
       
   232  * Otherwise, returns false immediately.
       
   233  */
       
   234 bool QVersitWriter::waitForFinished(int msec)
       
   235 {
       
   236     State state = d->state();
       
   237     if (state == ActiveState) {
       
   238         return d->wait(msec);
       
   239     } else if (state == FinishedState) {
       
   240         return true;
       
   241     } else {
       
   242         return false;
       
   243     }
       
   244 }
       
   245 
       
   246 #include "moc_qversitwriter.cpp"