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 QtVersit 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 "vcardpreserver.h" |
35 | |
36 | #include <QtCore/qdatetime.h> |
37 | #include <QtCore/qvariant.h> |
38 | #include <QtCore/qurl.h> |
39 | |
40 | #include <QtContacts/qcontact.h> |
41 | #include <QtContacts/qcontactdetail.h> |
42 | #include <QtContacts/qcontactextendeddetail.h> |
43 | |
44 | #include <QtVersit/qversitdocument.h> |
45 | #include <QtVersit/qversitproperty.h> |
46 | |
47 | /* |
48 | When these conditions are satisfied, QStringLiteral is implemented by |
49 | gcc's statement-expression extension. However, in this file it will |
50 | not work, because "statement-expressions are not allowed outside functions |
51 | nor in template-argument lists". |
52 | |
53 | Fall back to the less-performant QLatin1String in this case. |
54 | */ |
55 | #if defined(QStringLiteral) && defined(QT_UNICODE_LITERAL_II) && defined(Q_CC_GNU) && !defined(Q_COMPILER_LAMBDA) |
56 | # undef QStringLiteral |
57 | # define QStringLiteral QLatin1String |
58 | #endif |
59 | |
60 | QTCONTACTS_USE_NAMESPACE |
61 | |
62 | QT_BEGIN_NAMESPACE_VERSIT |
63 | |
64 | class VCardPreserver : public QVersitContactHandler |
65 | { |
66 | public: |
67 | VCardPreserver(); |
68 | void propertyProcessed(const QVersitDocument& document, |
69 | const QVersitProperty& property, |
70 | const QContact& contact, |
71 | bool* alreadyProcessed, |
72 | QList<QContactDetail>* updatedDetails); |
73 | void documentProcessed(const QVersitDocument& document, |
74 | QContact* contact); |
75 | void detailProcessed(const QContact& contact, |
76 | const QContactDetail& detail, |
77 | const QVersitDocument& document, |
78 | QSet<int>* processedFields, |
79 | QList<QVersitProperty>* toBeRemoved, |
80 | QList<QVersitProperty>* toBeAdded); |
81 | void contactProcessed(const QContact& contact, |
82 | QVersitDocument* document); |
83 | }; |
84 | |
85 | const QContactDetail::DetailType DetailType(QContactDetail::TypeExtendedDetail); |
86 | const QContactExtendedDetail::ExtendedDetailField KeyField(QContactExtendedDetail::FieldName); |
87 | const QContactExtendedDetail::ExtendedDetailField ValueField(QContactExtendedDetail::FieldData); |
88 | |
89 | |
90 | QSet<QString> VCardPreserverFactory::profiles() const |
91 | { |
92 | QSet<QString> retval; |
93 | // TODO: use this line in 1.2 when the constant is enabled |
94 | //retval.insert(QVersitContactHandlerFactory::ProfilePreserve); |
95 | retval.insert(QStringLiteral("Preserve" )); |
96 | return retval; |
97 | } |
98 | |
99 | QString VCardPreserverFactory::name() const |
100 | { |
101 | return QStringLiteral("org.qt-project.Qt.VCardPreserverFactory" ); |
102 | } |
103 | |
104 | int VCardPreserverFactory::index() const |
105 | { |
106 | // Prefer to run this plugin last, but before the backup handler. |
107 | return -2; |
108 | } |
109 | |
110 | QVersitContactHandler* VCardPreserverFactory::createHandler() const |
111 | { |
112 | return new VCardPreserver(); |
113 | } |
114 | |
115 | QStringList VCardPreserverFactory::keys() const |
116 | { |
117 | return QStringList() << name(); |
118 | } |
119 | |
120 | VCardPreserver::VCardPreserver() |
121 | { |
122 | } |
123 | |
124 | void VCardPreserver::propertyProcessed(const QVersitDocument& document, |
125 | const QVersitProperty& property, |
126 | const QContact& contact, |
127 | bool* alreadyProcessed, |
128 | QList<QContactDetail>* updatedDetails) |
129 | { |
130 | Q_UNUSED(contact) |
131 | Q_UNUSED(document) |
132 | if (!updatedDetails->isEmpty() || *alreadyProcessed) { |
133 | return; |
134 | } |
135 | QContactDetail detail(DetailType); |
136 | detail.setValue(field: KeyField, value: property.name()); |
137 | detail.setValue(field: ValueField, value: property.value()); |
138 | updatedDetails->append(t: detail); |
139 | *alreadyProcessed = true; |
140 | } |
141 | |
142 | void VCardPreserver::documentProcessed(const QVersitDocument& document, |
143 | QContact* contact) |
144 | { |
145 | Q_UNUSED(document) |
146 | Q_UNUSED(contact) |
147 | } |
148 | |
149 | void VCardPreserver::detailProcessed(const QContact& contact, |
150 | const QContactDetail& detail, |
151 | const QVersitDocument& document, |
152 | QSet<int>* processedFields, |
153 | QList<QVersitProperty>* toBeRemoved, |
154 | QList<QVersitProperty>* toBeAdded) |
155 | { |
156 | Q_UNUSED(contact) |
157 | Q_UNUSED(document) |
158 | Q_UNUSED(toBeRemoved) |
159 | if (detail.type() == DetailType |
160 | && processedFields->isEmpty()) { |
161 | QString key(detail.value(field: KeyField).toString()); |
162 | QString value(detail.value(field: ValueField).toString()); |
163 | if (!key.isEmpty() && !value.isEmpty()) { |
164 | QVersitProperty property; |
165 | property.setName(key); |
166 | property.setValue(value); |
167 | toBeAdded->append(t: property); |
168 | } |
169 | processedFields->insert(value: KeyField); |
170 | processedFields->insert(value: ValueField); |
171 | } |
172 | } |
173 | |
174 | void VCardPreserver::contactProcessed(const QContact& contact, |
175 | QVersitDocument* document) |
176 | { |
177 | Q_UNUSED(contact) |
178 | Q_UNUSED(document) |
179 | } |
180 | |
181 | #include "moc_vcardpreserver.cpp" |
182 | |
183 | QT_END_NAMESPACE_VERSIT |
184 | |