qtcontactsmobility/src/versit/qversitwriter.cpp
changeset 24 0ba2181d7c28
child 25 76a2435edfd4
equal deleted inserted replaced
0:e686773b3f54 24:0ba2181d7c28
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 "qvcard21writer_p.h"
       
    44 #include "qvcard30writer_p.h"
       
    45 #include "versitutils_p.h"
       
    46 #include "qmobilityglobal.h"
       
    47 
       
    48 #include <QStringList>
       
    49 
       
    50 QTM_BEGIN_NAMESPACE
       
    51 
       
    52 /*!
       
    53   \class QVersitWriter
       
    54  
       
    55   \brief The QVersitWriter class provides an interface
       
    56   for writing a versit document such as a vCard to a text stream.
       
    57 
       
    58   \ingroup versit
       
    59  
       
    60   QVersitWriter converts a QVersitDocument into its textual representation.
       
    61   QVersitWriter supports writing to an abstract I/O device
       
    62   which can be for example a file or a memory buffer.
       
    63   The writing can be done synchronously or asynchronously.
       
    64  
       
    65   \code
       
    66   // An example of writing a simple vCard to a memory buffer:
       
    67   QBuffer vCardBuffer;
       
    68   vCardBuffer.open(QBuffer::ReadWrite);
       
    69   QVersitWriter writer;
       
    70   writer.setDevice(&vCardBuffer);
       
    71   QVersitDocument document;
       
    72   QVersitProperty property;
       
    73   property.setName("N");
       
    74   property.setValue("Citizen;John;Q;;");
       
    75   document.addProperty(property);
       
    76   writer.setVersitDocument(document);
       
    77   if (writer.writeAll()) {
       
    78       // Use the vCardBuffer...
       
    79   }
       
    80   \endcode
       
    81  
       
    82   \sa QVersitDocument, QVersitProperty
       
    83  */
       
    84 
       
    85 /*!
       
    86  * \fn QVersitWriter::writingDone()
       
    87  * The signal is emitted by the writer when the asynchronous writing has been completed.
       
    88  */
       
    89 
       
    90 /*! Constructs a new writer. */
       
    91 QVersitWriter::QVersitWriter() : d(new QVCard21Writer)
       
    92 {
       
    93     connect(d,SIGNAL(finished()),this,SIGNAL(writingDone()),Qt::DirectConnection);
       
    94 }
       
    95 
       
    96 /*! 
       
    97  * Frees the memory used by the writer. 
       
    98  * Waits until a pending asynchronous writing has been completed.
       
    99  */
       
   100 QVersitWriter::~QVersitWriter()
       
   101 {
       
   102     d->wait();
       
   103     delete d;
       
   104 }
       
   105 
       
   106 /*!
       
   107  * Set the versit document to be written to \a versitDocument and
       
   108  * selects the actual writer implementation based on the versit document type.
       
   109  */
       
   110 void QVersitWriter::setVersitDocument(const QVersitDocument& versitDocument)
       
   111 {
       
   112     QVersitWriterPrivate* updatedWriter = 0;
       
   113     switch (versitDocument.versitType()) {
       
   114         case QVersitDocument::VCard21:
       
   115             updatedWriter = new QVCard21Writer;
       
   116             break;
       
   117         case QVersitDocument::VCard30:
       
   118             updatedWriter = new QVCard30Writer;
       
   119             break;
       
   120         default:
       
   121             break;
       
   122     }
       
   123     if (updatedWriter) {
       
   124         updatedWriter->mIoDevice = d->mIoDevice;
       
   125         delete d;
       
   126         d = updatedWriter;
       
   127         connect(d,SIGNAL(finished()),this,SIGNAL(writingDone()),Qt::DirectConnection);
       
   128     }
       
   129     d->mVersitDocument = versitDocument;
       
   130 }
       
   131 
       
   132 /*!
       
   133  * Returns the current versit document.
       
   134  */
       
   135 QVersitDocument QVersitWriter::versitDocument() const
       
   136 {
       
   137     return d->mVersitDocument;
       
   138 }
       
   139 
       
   140 /*!
       
   141  * Sets the device used for writing to \a device.
       
   142  */
       
   143 void QVersitWriter::setDevice(QIODevice* device)
       
   144 {
       
   145     d->mIoDevice = device;
       
   146 }
       
   147 
       
   148 /*!
       
   149  * Returns the device used for writing.
       
   150  */
       
   151 QIODevice* QVersitWriter::device() const
       
   152 {
       
   153     return d->mIoDevice;
       
   154 }
       
   155 
       
   156 /*!
       
   157  * Starts writing the output asynchronously.
       
   158  * Returns false if the output device has not been set or opened or
       
   159  * if there is another asynchronous write operation already pending.
       
   160  * Signal \l writingDone() is emitted when the writing has finished.
       
   161  */
       
   162 bool QVersitWriter::startWriting()
       
   163 {
       
   164     bool started = false;
       
   165     if (d->isReady() && !d->isRunning()) {
       
   166         d->start();
       
   167         started = true;
       
   168     }
       
   169 
       
   170     return started;
       
   171 }
       
   172 
       
   173 /*!
       
   174  * Writes the output synchronously.
       
   175  * Returns false if the output device has not been set or opened or
       
   176  * if there is an asynchronous write operation pending.
       
   177  * Using this function may block the user thread for an undefined period.
       
   178  * In most cases asynchronous \l startWriting() should be used instead.
       
   179  */
       
   180 bool QVersitWriter::writeAll()
       
   181 {
       
   182     bool ok = false;
       
   183     if (!d->isRunning())
       
   184         ok = d->write();
       
   185     return ok;
       
   186 }
       
   187 
       
   188 #include "moc_qversitwriter.cpp"
       
   189 
       
   190 QTM_END_NAMESPACE