1/****************************************************************************
2**
3** Copyright (C) 2015 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the QtVersitOrganizer module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL21$
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 The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/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 or version 3 as published by the Free
20** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22** following information to ensure the GNU Lesser General Public License
23** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25**
26** As a special exception, The Qt Company gives you certain additional
27** rights. These rights are described in The Qt Company LGPL Exception
28** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29**
30** $QT_END_LICENSE$
31**
32****************************************************************************/
33
34#include "qversitorganizerimporter.h"
35#include "qversitorganizerimporter_p.h"
36
37#include <QtVersit/qversitdocument.h>
38#include <QtVersit/qversitproperty.h>
39
40QTVERSIT_USE_NAMESPACE
41
42QT_BEGIN_NAMESPACE_VERSITORGANIZER
43
44/*!
45 \class QVersitOrganizerImporter
46 \brief The QVersitOrganizerImporter class converts \l{QVersitDocument}{QVersitDocuments} to
47 \l{QOrganizerItem}{QOrganizerItems}.
48
49 \ingroup versit
50 \inmodule QtVersit
51
52 This class is used to convert a \l QVersitDocument (which may be produced by a
53 QVersitReader) to lists of \l{QOrganizerItem}{QOrganizerItems} (which may be saved into a
54 QOrganizerManager. Unless there is an error, there is a one-to-one mapping between
55 sub-documents of the input Versit document and QOrganizerItems.
56 */
57
58/*!
59 \class QVersitOrganizerImporterPropertyHandler
60 \brief The QVersitOrganizerImporterPropertyHandler class is an interface for specifying
61 custom import behaviour for vCard properties.
62 \ingroup versit-extension
63 \inmodule QtVersit
64
65 For general information on extending Qt Versit, see the document on \l{Qt Versit Plugins}.
66
67 \sa QVersitOrganizerImporter
68 */
69
70/*!
71 \fn QVersitOrganizerImporterPropertyHandler::~QVersitOrganizerImporterPropertyHandler()
72 Frees any memory in use by this handler.
73 */
74
75/*!
76 \fn void QVersitOrganizerImporterPropertyHandler::propertyProcessed(const QVersitDocument& document, const QVersitProperty& property, const QOrganizerItem& item, bool* alreadyProcessed, QList<QOrganizerItemDetail>* updatedDetails)
77 Process \a property and provide a list of updated details by adding them to \a updatedDetails.
78
79 This function is called on every QVersitProperty encountered during an import, after the property
80 has been processed by the QVersitOrganizerImporter. An implementation of this function can be
81 made to provide support for vCard properties not supported by QVersitOrganizerImporter.
82
83 The supplied \a document is the container for the \a property. \a alreadyProcessed is true if the
84 QVersitOrganizerImporter or another handler was successful in processing the property. If it is
85 false and the handler processes the property, it should be set to true to inform later handlers
86 that the property requires no further processing. \a item holds the state of the item before the
87 property was processed by the importer. \a updatedDetails is initially filled with a list of
88 details that the importer will update, and can be modified (by removing, modifying or
89 adding details to the list)
90 */
91
92/*!
93 \fn void QVersitOrganizerImporterPropertyHandler::subDocumentProcessed(const QVersitDocument& topLevel, const QVersitDocument& subDocument, QOrganizerItem* item)
94 Perform any final processing on the \a item generated by the \a subDocument. \a topLevel is the
95 container within which \a subDocument was found. This can be implemented by the handler to clear
96 any internal state before moving onto the next document.
97
98 This function is called after all QVersitProperties have been handled by the
99 QVersitOrganizerImporter.
100*/
101
102/*!
103 \enum QVersitOrganizerImporter::Error
104 This enum specifies an error that occurred during the most recent call to importDocument()
105 \value NoError The most recent operation was successful
106 \value InvalidDocumentError One of the documents is not an iCalendar file
107 \value EmptyDocumentError One of the documents is empty
108 */
109
110/*! Constructs a new importer */
111QVersitOrganizerImporter::QVersitOrganizerImporter()
112 : d(new QVersitOrganizerImporterPrivate)
113{
114}
115
116/*!
117 * Constructs a new importer for the given \a profile. The profile strings should be one of those
118 * defined by QVersitOrganizerHandlerFactory, or a value otherwise agreed to by a \l{Qt Versit
119 * Plugins}{Versit plugin}.
120 *
121 * The profile determines which plugins will be loaded to supplement the importer.
122 */
123QVersitOrganizerImporter::QVersitOrganizerImporter(const QString& profile)
124 : d(new QVersitOrganizerImporterPrivate(profile))
125{
126}
127
128/*! Frees the memory used by the importer */
129QVersitOrganizerImporter::~QVersitOrganizerImporter()
130{
131 delete d;
132}
133
134/*!
135 * Converts \a document into a corresponding list of QOrganizerItems. After calling this, the
136 * converted organizer items can be retrieved by calling items().
137 *
138 * Returns true on success. The document should contain at least one subdocument. In the
139 * importing process, each subdocument roughly corresponds to a QOrganizerItem. If any of the
140 * subdocuments cannot be imported as organizer items (eg. they don't conform to the iCalendar
141 * format), false is returned and errorMap() will return a list describing the errors that occurred.
142 * The successfully imported items will still be available via items().
143 *
144 * \sa items(), errorMap()
145 */
146bool QVersitOrganizerImporter::importDocument(const QVersitDocument& document)
147{
148 d->mItems.clear();
149 d->mErrors.clear();
150 bool ok = true;
151 if (document.type() != QVersitDocument::ICalendar20Type
152 || document.componentType() != QStringLiteral("VCALENDAR")) {
153 d->mErrors.insert(akey: -1, avalue: QVersitOrganizerImporter::InvalidDocumentError);
154 return false;
155 }
156 const QList<QVersitDocument> subDocuments = document.subDocuments();
157 if (subDocuments.isEmpty()) {
158 d->mErrors.insert(akey: -1, avalue: QVersitOrganizerImporter::EmptyDocumentError);
159 return false;
160 }
161
162 int documentIndex = 0;
163 foreach (const QVersitDocument& subDocument, subDocuments) {
164 QOrganizerItem item;
165 QVersitOrganizerImporter::Error error;
166 if (d->importDocument(topLevel: document, subDocument, item: &item, error: &error)) {
167 d->mItems.append(t: item);
168 } else {
169 // importDocument can return false with no error if it's a non-document component
170 if (error != QVersitOrganizerImporter::NoError) {
171 d->mErrors.insert(akey: documentIndex, avalue: error);
172 ok = false;
173 }
174 }
175 documentIndex++;
176 }
177
178 return ok;
179}
180
181/*!
182 * Returns the organizer items imported in the most recent call to importDocument().
183 *
184 * \sa importDocument()
185 */
186QList<QOrganizerItem> QVersitOrganizerImporter::items() const
187{
188 return d->mItems;
189}
190
191/*!
192 * Returns the map of errors encountered in the most recent call to importDocument().
193 *
194 * The key is the zero based index of the sub document within the container document, or -1 for an error
195 * with the container document itself. The value is the error that occurred on that document.
196 *
197 * \sa importDocument()
198 */
199QMap<int, QVersitOrganizerImporter::Error> QVersitOrganizerImporter::errorMap() const
200{
201 return d->mErrors;
202}
203
204/*!
205 * Sets \a handler to be the handler for processing QVersitProperties, or 0 to have no handler.
206 *
207 * Does not take ownership of the handler. The client should ensure the handler remains valid for
208 * the lifetime of the importer.
209 *
210 * Only one property handler can be set. If another property handler was previously set, it will no
211 * longer be associated with the importer.
212 */
213void QVersitOrganizerImporter::setPropertyHandler(QVersitOrganizerImporterPropertyHandler* handler)
214{
215 d->mPropertyHandler = handler;
216}
217
218QT_END_NAMESPACE_VERSITORGANIZER
219

source code of qtpim/src/versitorganizer/qversitorganizerimporter.cpp