~chris.gagnon/+junk/qtpim-coverage

« back to all changes in this revision

Viewing changes to src/versit/qversitwriter_p.cpp

  • Committer: chris.gagnon
  • Date: 2013-12-10 23:09:37 UTC
  • Revision ID: chris.gagnon@canonical.com-20131210230937-2akf1ft1edcttk87
first post

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the QtVersit module of the Qt Toolkit.
 
7
**
 
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.
 
16
**
 
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.
 
24
**
 
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.
 
28
**
 
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.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
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"
 
47
 
 
48
#include <QStringList>
 
49
#include <QMutexLocker>
 
50
#include <QScopedPointer>
 
51
#include <QTextCodec>
 
52
#include <QBuffer>
 
53
 
 
54
QT_BEGIN_NAMESPACE_VERSIT
 
55
 
 
56
/*! Constructs a writer. */
 
57
QVersitWriterPrivate::QVersitWriterPrivate()
 
58
    : mIoDevice(0),
 
59
    mState(QVersitWriter::InactiveState),
 
60
    mError(QVersitWriter::NoError),
 
61
    mIsCanceling(false),
 
62
    mDefaultCodec(0)
 
63
{
 
64
}
 
65
 
 
66
/*! Destroys a writer. */
 
67
QVersitWriterPrivate::~QVersitWriterPrivate()
 
68
{
 
69
}
 
70
 
 
71
/*! Links the signals from this to the signals of \a writer. */
 
72
void QVersitWriterPrivate::init(QVersitWriter* writer)
 
73
{
 
74
    qRegisterMetaType<QVersitWriter::State>("QVersitWriter::State");
 
75
    connect(this, SIGNAL(stateChanged(QVersitWriter::State)),
 
76
            writer, SIGNAL(stateChanged(QVersitWriter::State)), Qt::DirectConnection);
 
77
}
 
78
 
 
79
/*!
 
80
 * Do the actual writing and set the error and state appropriately.
 
81
 */
 
82
void QVersitWriterPrivate::write()
 
83
{
 
84
    bool canceled = false;
 
85
 
 
86
    // Try to get the type from the parameter to startWriting...
 
87
    QVersitDocument::VersitType type = documentType();
 
88
 
 
89
    foreach (const QVersitDocument& document, mInput) {
 
90
        if (isCanceling()) {
 
91
            canceled = true;
 
92
            break;
 
93
        }
 
94
 
 
95
        // Get type from the document if not specified in startWriting
 
96
        if (type == QVersitDocument::InvalidType)
 
97
            type = document.type();
 
98
 
 
99
        QScopedPointer<QVersitDocumentWriter> writer(writerForType(type, document));
 
100
        QTextCodec* codec = mDefaultCodec;
 
101
        if (codec == NULL) {
 
102
            if (type == QVersitDocument::VCard21Type) {
 
103
                codec = QTextCodec::codecForName("ISO-8859-1");
 
104
                writer->setAsciiCodec();
 
105
            } else {
 
106
                codec = QTextCodec::codecForName("UTF-8");
 
107
            }
 
108
        }
 
109
        writer->setCodec(codec);
 
110
        writer->setDevice(mIoDevice);
 
111
        if (!writer->encodeVersitDocument(document)) {
 
112
            setError(QVersitWriter::IOError);
 
113
            break;
 
114
        }
 
115
    }
 
116
    if (canceled)
 
117
        setState(QVersitWriter::CanceledState);
 
118
    else
 
119
        setState(QVersitWriter::FinishedState);
 
120
}
 
121
 
 
122
/*!
 
123
 * Inherited from QThread, called by QThread when the thread has been started.
 
124
 */
 
125
void QVersitWriterPrivate::run()
 
126
{
 
127
    write();
 
128
}
 
129
 
 
130
void QVersitWriterPrivate::setState(QVersitWriter::State state)
 
131
{
 
132
    mMutex.lock();
 
133
    mState = state;
 
134
    mMutex.unlock();
 
135
    emit stateChanged(state);
 
136
}
 
137
 
 
138
QVersitWriter::State QVersitWriterPrivate::state() const
 
139
{
 
140
    QMutexLocker locker(&mMutex);
 
141
    return mState;
 
142
}
 
143
 
 
144
void QVersitWriterPrivate::setError(QVersitWriter::Error error)
 
145
{
 
146
    QMutexLocker locker(&mMutex);
 
147
    mError = error;
 
148
}
 
149
 
 
150
QVersitWriter::Error QVersitWriterPrivate::error() const
 
151
{
 
152
    QMutexLocker locker(&mMutex);
 
153
    return mError;
 
154
}
 
155
 
 
156
/*!
 
157
 * Returns a QVersitDocumentWriter that can encode a QVersitDocument of type \a type.
 
158
 * The caller is responsible for deleting the object.
 
159
 */
 
160
QVersitDocumentWriter* QVersitWriterPrivate::writerForType(QVersitDocument::VersitType type, const QVersitDocument& document)
 
161
{
 
162
    switch (type) {
 
163
        case QVersitDocument::InvalidType:
 
164
        {
 
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);
 
177
            } else {
 
178
                return new QVCard30Writer(QVersitDocument::VCard30Type);
 
179
            }
 
180
        }
 
181
        case QVersitDocument::VCard21Type:
 
182
            return new QVCard21Writer(type);
 
183
        case QVersitDocument::VCard30Type:
 
184
            return new QVCard30Writer(type);
 
185
        default:
 
186
            return new QVCard30Writer(type);
 
187
    }
 
188
}
 
189
 
 
190
void QVersitWriterPrivate::setCanceling(bool canceling)
 
191
{
 
192
    QMutexLocker locker(&mMutex);
 
193
    mIsCanceling = canceling;
 
194
}
 
195
 
 
196
bool QVersitWriterPrivate::isCanceling() const
 
197
{
 
198
    QMutexLocker locker(&mMutex);
 
199
    return mIsCanceling;
 
200
}
 
201
 
 
202
void QVersitWriterPrivate::setDocumentType(QVersitDocument::VersitType type)
 
203
{
 
204
    QMutexLocker locker(&mMutex);
 
205
    mType = type;
 
206
}
 
207
 
 
208
QVersitDocument::VersitType QVersitWriterPrivate::documentType() const
 
209
{
 
210
    QMutexLocker locker(&mMutex);
 
211
    return mType;
 
212
}
 
213
 
 
214
#include "moc_qversitwriter_p.cpp"
 
215
QT_END_NAMESPACE_VERSIT