|
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 "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 |
|
54 QTM_USE_NAMESPACE |
|
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 /*! |
|
72 * Do the actual writing and set the error and state appropriately. |
|
73 */ |
|
74 void QVersitWriterPrivate::write() |
|
75 { |
|
76 bool canceled = false; |
|
77 foreach (QVersitDocument document, mInput) { |
|
78 if (isCanceling()) { |
|
79 canceled = true; |
|
80 break; |
|
81 } |
|
82 QScopedPointer<QVersitDocumentWriter> writer(writerForType(document.type())); |
|
83 QTextCodec* codec = mDefaultCodec; |
|
84 if (codec == NULL) { |
|
85 if (document.type() == QVersitDocument::VCard21Type) |
|
86 codec = QTextCodec::codecForName("ISO-8859-1"); |
|
87 else |
|
88 codec = QTextCodec::codecForName("UTF-8"); |
|
89 } |
|
90 writer->setCodec(codec); |
|
91 writer->setDevice(mIoDevice); |
|
92 writer->encodeVersitDocument(document); |
|
93 if (!writer->mSuccessful) { |
|
94 setError(QVersitWriter::IOError); |
|
95 break; |
|
96 } |
|
97 } |
|
98 if (canceled) |
|
99 setState(QVersitWriter::CanceledState); |
|
100 else |
|
101 setState(QVersitWriter::FinishedState); |
|
102 } |
|
103 |
|
104 /*! |
|
105 * Inherited from QThread, called by QThread when the thread has been started. |
|
106 */ |
|
107 void QVersitWriterPrivate::run() |
|
108 { |
|
109 write(); |
|
110 } |
|
111 |
|
112 void QVersitWriterPrivate::setState(QVersitWriter::State state) |
|
113 { |
|
114 mMutex.lock(); |
|
115 mState = state; |
|
116 mMutex.unlock(); |
|
117 emit stateChanged(state); |
|
118 } |
|
119 |
|
120 QVersitWriter::State QVersitWriterPrivate::state() const |
|
121 { |
|
122 QMutexLocker locker(&mMutex); |
|
123 return mState; |
|
124 } |
|
125 |
|
126 void QVersitWriterPrivate::setError(QVersitWriter::Error error) |
|
127 { |
|
128 QMutexLocker locker(&mMutex); |
|
129 mError = error; |
|
130 } |
|
131 |
|
132 QVersitWriter::Error QVersitWriterPrivate::error() const |
|
133 { |
|
134 QMutexLocker locker(&mMutex); |
|
135 return mError; |
|
136 } |
|
137 |
|
138 /*! |
|
139 * Returns a QVersitDocumentWriter that can encode a QVersitDocument of type \a type. |
|
140 * The caller is responsible for deleting the object. |
|
141 */ |
|
142 QVersitDocumentWriter* QVersitWriterPrivate::writerForType(QVersitDocument::VersitType type) |
|
143 { |
|
144 switch (type) { |
|
145 case QVersitDocument::VCard21Type: |
|
146 return new QVCard21Writer; |
|
147 case QVersitDocument::VCard30Type: |
|
148 return new QVCard30Writer; |
|
149 default: |
|
150 return new QVCard21Writer; |
|
151 } |
|
152 } |
|
153 |
|
154 void QVersitWriterPrivate::setCanceling(bool canceling) |
|
155 { |
|
156 QMutexLocker locker(&mMutex); |
|
157 mIsCanceling = canceling; |
|
158 } |
|
159 |
|
160 bool QVersitWriterPrivate::isCanceling() |
|
161 { |
|
162 QMutexLocker locker(&mMutex); |
|
163 return mIsCanceling; |
|
164 } |
|
165 |
|
166 #include "moc_qversitwriter_p.cpp" |