src/versit/qversitwriter_p.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 "qmobilityglobal.h"
       
    43 #include "qversitwriter_p.h"
       
    44 #include "qversitdocumentwriter_p.h"
       
    45 #include "qvcard21writer_p.h"
       
    46 #include "qvcard30writer_p.h"
       
    47 #include "versitutils_p.h"
       
    48 
       
    49 #include <QStringList>
       
    50 #include <QMutexLocker>
       
    51 #include <QScopedPointer>
       
    52 #include <QTextCodec>
       
    53 #include <QBuffer>
       
    54 
       
    55 QTM_USE_NAMESPACE
       
    56 
       
    57 /*! Constructs a writer. */
       
    58 QVersitWriterPrivate::QVersitWriterPrivate()
       
    59     : mIoDevice(0),
       
    60     mState(QVersitWriter::InactiveState),
       
    61     mError(QVersitWriter::NoError),
       
    62     mIsCanceling(false),
       
    63     mDefaultCodec(0)
       
    64 {
       
    65 }
       
    66 
       
    67 /*! Destroys a writer. */
       
    68 QVersitWriterPrivate::~QVersitWriterPrivate()
       
    69 {
       
    70 }
       
    71 
       
    72 /*! Links the signals from this to the signals of \a writer. */
       
    73 void QVersitWriterPrivate::init(QVersitWriter* writer)
       
    74 {
       
    75     qRegisterMetaType<QVersitWriter::State>("QVersitWriter::State");
       
    76     connect(this, SIGNAL(stateChanged(QVersitWriter::State)),
       
    77             writer, SIGNAL(stateChanged(QVersitWriter::State)), Qt::DirectConnection);
       
    78 }
       
    79 
       
    80 /*!
       
    81  * Do the actual writing and set the error and state appropriately.
       
    82  */
       
    83 void QVersitWriterPrivate::write()
       
    84 {
       
    85     bool canceled = false;
       
    86     foreach (const QVersitDocument& document, mInput) {
       
    87         if (isCanceling()) {
       
    88             canceled = true;
       
    89             break;
       
    90         }
       
    91         QScopedPointer<QVersitDocumentWriter> writer(writerForType(document.type()));
       
    92         QTextCodec* codec = mDefaultCodec;
       
    93         if (codec == NULL) {
       
    94             if (document.type() == QVersitDocument::VCard21Type)
       
    95                 codec = QTextCodec::codecForName("ISO-8859-1");
       
    96             else
       
    97                 codec = QTextCodec::codecForName("UTF-8");
       
    98         }
       
    99         writer->setCodec(codec);
       
   100         writer->setDevice(mIoDevice);
       
   101         writer->encodeVersitDocument(document);
       
   102         if (!writer->mSuccessful) {
       
   103             setError(QVersitWriter::IOError);
       
   104             break;
       
   105         }
       
   106     }
       
   107     if (canceled)
       
   108         setState(QVersitWriter::CanceledState);
       
   109     else
       
   110         setState(QVersitWriter::FinishedState);
       
   111 }
       
   112 
       
   113 /*!
       
   114  * Inherited from QThread, called by QThread when the thread has been started.
       
   115  */
       
   116 void QVersitWriterPrivate::run()
       
   117 {
       
   118     write();
       
   119 }
       
   120 
       
   121 void QVersitWriterPrivate::setState(QVersitWriter::State state)
       
   122 {
       
   123     mMutex.lock();
       
   124     mState = state;
       
   125     mMutex.unlock();
       
   126     emit stateChanged(state);
       
   127 }
       
   128 
       
   129 QVersitWriter::State QVersitWriterPrivate::state() const
       
   130 {
       
   131     QMutexLocker locker(&mMutex);
       
   132     return mState;
       
   133 }
       
   134 
       
   135 void QVersitWriterPrivate::setError(QVersitWriter::Error error)
       
   136 {
       
   137     QMutexLocker locker(&mMutex);
       
   138     mError = error;
       
   139 }
       
   140 
       
   141 QVersitWriter::Error QVersitWriterPrivate::error() const
       
   142 {
       
   143     QMutexLocker locker(&mMutex);
       
   144     return mError;
       
   145 }
       
   146 
       
   147 /*!
       
   148  * Returns a QVersitDocumentWriter that can encode a QVersitDocument of type \a type.
       
   149  * The caller is responsible for deleting the object.
       
   150  */
       
   151 QVersitDocumentWriter* QVersitWriterPrivate::writerForType(QVersitDocument::VersitType type)
       
   152 {
       
   153     switch (type) {
       
   154         case QVersitDocument::VCard21Type:
       
   155             return new QVCard21Writer;
       
   156         case QVersitDocument::VCard30Type:
       
   157             return new QVCard30Writer;
       
   158         default:
       
   159             return new QVCard21Writer;
       
   160     }
       
   161 }
       
   162 
       
   163 void QVersitWriterPrivate::setCanceling(bool canceling)
       
   164 {
       
   165     QMutexLocker locker(&mMutex);
       
   166     mIsCanceling = canceling;
       
   167 }
       
   168 
       
   169 bool QVersitWriterPrivate::isCanceling()
       
   170 {
       
   171     QMutexLocker locker(&mMutex);
       
   172     return mIsCanceling;
       
   173 }
       
   174 
       
   175 #include "moc_qversitwriter_p.cpp"