1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the QtVersit module of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
17
** GNU Lesser General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU Lesser
19
** General Public License version 2.1 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 2.1 requirements
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
40
****************************************************************************/
42
#include "qversitwriter_p.h"
43
#include "qversitdocumentwriter_p.h"
44
#include "qvcard21writer_p.h"
45
#include "qvcard30writer_p.h"
46
#include "qversitutils_p.h"
48
#include <QStringList>
49
#include <QMutexLocker>
50
#include <QScopedPointer>
54
QT_BEGIN_NAMESPACE_VERSIT
56
/*! Constructs a writer. */
57
QVersitWriterPrivate::QVersitWriterPrivate()
59
mState(QVersitWriter::InactiveState),
60
mError(QVersitWriter::NoError),
66
/*! Destroys a writer. */
67
QVersitWriterPrivate::~QVersitWriterPrivate()
71
/*! Links the signals from this to the signals of \a writer. */
72
void QVersitWriterPrivate::init(QVersitWriter* writer)
74
qRegisterMetaType<QVersitWriter::State>("QVersitWriter::State");
75
connect(this, SIGNAL(stateChanged(QVersitWriter::State)),
76
writer, SIGNAL(stateChanged(QVersitWriter::State)), Qt::DirectConnection);
80
* Do the actual writing and set the error and state appropriately.
82
void QVersitWriterPrivate::write()
84
bool canceled = false;
86
// Try to get the type from the parameter to startWriting...
87
QVersitDocument::VersitType type = documentType();
89
foreach (const QVersitDocument& document, mInput) {
95
// Get type from the document if not specified in startWriting
96
if (type == QVersitDocument::InvalidType)
97
type = document.type();
99
QScopedPointer<QVersitDocumentWriter> writer(writerForType(type, document));
100
QTextCodec* codec = mDefaultCodec;
102
if (type == QVersitDocument::VCard21Type) {
103
codec = QTextCodec::codecForName("ISO-8859-1");
104
writer->setAsciiCodec();
106
codec = QTextCodec::codecForName("UTF-8");
109
writer->setCodec(codec);
110
writer->setDevice(mIoDevice);
111
if (!writer->encodeVersitDocument(document)) {
112
setError(QVersitWriter::IOError);
117
setState(QVersitWriter::CanceledState);
119
setState(QVersitWriter::FinishedState);
123
* Inherited from QThread, called by QThread when the thread has been started.
125
void QVersitWriterPrivate::run()
130
void QVersitWriterPrivate::setState(QVersitWriter::State state)
135
emit stateChanged(state);
138
QVersitWriter::State QVersitWriterPrivate::state() const
140
QMutexLocker locker(&mMutex);
144
void QVersitWriterPrivate::setError(QVersitWriter::Error error)
146
QMutexLocker locker(&mMutex);
150
QVersitWriter::Error QVersitWriterPrivate::error() const
152
QMutexLocker locker(&mMutex);
157
* Returns a QVersitDocumentWriter that can encode a QVersitDocument of type \a type.
158
* The caller is responsible for deleting the object.
160
QVersitDocumentWriter* QVersitWriterPrivate::writerForType(QVersitDocument::VersitType type, const QVersitDocument& document)
163
case QVersitDocument::InvalidType:
165
// Neither startWriting or the document provided the type.
166
// Need to infer the type from the document's componentType
167
QString componentType(document.componentType());
168
if (componentType == QStringLiteral("VCARD")) {
169
return new QVCard30Writer(QVersitDocument::VCard30Type);
170
} else if (componentType == QStringLiteral("VCALENDAR")
171
|| componentType == QStringLiteral("VEVENT")
172
|| componentType == QStringLiteral("VTODO")
173
|| componentType == QStringLiteral("VJOURNAL")
174
|| componentType == QStringLiteral("VTIMEZONE")
175
|| componentType == QStringLiteral("VALARM")) {
176
return new QVCard30Writer(QVersitDocument::ICalendar20Type);
178
return new QVCard30Writer(QVersitDocument::VCard30Type);
181
case QVersitDocument::VCard21Type:
182
return new QVCard21Writer(type);
183
case QVersitDocument::VCard30Type:
184
return new QVCard30Writer(type);
186
return new QVCard30Writer(type);
190
void QVersitWriterPrivate::setCanceling(bool canceling)
192
QMutexLocker locker(&mMutex);
193
mIsCanceling = canceling;
196
bool QVersitWriterPrivate::isCanceling() const
198
QMutexLocker locker(&mMutex);
202
void QVersitWriterPrivate::setDocumentType(QVersitDocument::VersitType type)
204
QMutexLocker locker(&mMutex);
208
QVersitDocument::VersitType QVersitWriterPrivate::documentType() const
210
QMutexLocker locker(&mMutex);
214
#include "moc_qversitwriter_p.cpp"
215
QT_END_NAMESPACE_VERSIT