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 QtContacts 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 "qcontactactiontarget.h"
35#include "qcontactactiontarget_p.h"
36
37#ifndef QT_NO_DATASTREAM
38#include <QtCore/qdatastream.h>
39#endif
40#ifndef QT_NO_DEBUG_STREAM
41#include <QtCore/qdebug.h>
42#endif
43
44QT_BEGIN_NAMESPACE_CONTACTS
45
46/*!
47 \class QContactActionTarget
48 \brief The QContactActionTarget class provides information about the
49 target of an action. It may be either a contact, a contact and a detail
50 of that contact, or a contact and a list of the details of the contact,
51 which together should be used by the action.
52 \ingroup contacts-actions
53 \inmodule QtContacts
54*/
55
56/*!
57 \enum QContactActionTarget::Type
58 This enumerator defines the type of a QContactActionTarget.
59
60 \value Invalid The type is invalid.
61 \value WholeContact The type is a complete contact.
62 \value SingleDetail The type is only a single detail.
63 \value MultipleDetails The type contains multiple details.
64*/
65
66/*!
67 * Constructs a new action target from the given \a contact and the given list of that contact's \a details.
68 * If no \a contact is specified, the target will be invalid. If a \a contact but no \a details are specified,
69 * the target will be valid, but the action which operates on the target may fail (for example, if it requires
70 * a certain detail to be specified in order to perform the action).
71 */
72QContactActionTarget::QContactActionTarget(const QContact& contact, const QList<QContactDetail>& details)
73 : d(new QContactActionTargetPrivate(contact, details))
74{
75}
76
77/*!
78 * Constructs a new action target from the given \a contact and a specific \a detail of that contact
79 */
80QContactActionTarget::QContactActionTarget(const QContact& contact, const QContactDetail& detail)
81 : d(new QContactActionTargetPrivate(contact, QList<QContactDetail>() << detail))
82{
83}
84
85/*!
86 * Constructs a copy of the \a other action target
87 */
88QContactActionTarget::QContactActionTarget(const QContactActionTarget& other)
89 : d(other.d)
90{
91}
92
93/*!
94 * Assigns this action target to be equal to \a other
95 */
96QContactActionTarget& QContactActionTarget::operator=(const QContactActionTarget& other)
97{
98 d = other.d;
99 return *this;
100}
101
102/*!
103 * Cleans up any memory in use by the action target
104 */
105QContactActionTarget::~QContactActionTarget()
106{
107}
108
109/*!
110 * Returns the contact that this action target will operate on.
111 * \sa details()
112 */
113QContact QContactActionTarget::contact() const
114{
115 return d.constData()->m_contact;
116}
117
118/*!
119 * Returns the details that this action target will operate on.
120 * \sa contact()
121 */
122QList<QContactDetail> QContactActionTarget::details() const
123{
124 return d.constData()->m_details;
125}
126
127/*!
128 * Sets the contact that this action target will operate on to \a contact.
129 * \sa setDetails()
130 */
131void QContactActionTarget::setContact(const QContact& contact)
132{
133 d->m_contact = contact;
134}
135
136/*!
137 * Sets the details that this action target will operate on to \a details.
138 * \sa setContact()
139 */
140void QContactActionTarget::setDetails(const QList<QContactDetail>& details)
141{
142 d->m_details = details;
143}
144
145/*!
146 * Returns true if the target contact is not the default constructed contact.
147 * The validity of any specified details is not checked by this function.
148 */
149bool QContactActionTarget::isValid() const
150{
151 return (d.constData()->m_contact != QContact());
152}
153
154/*!
155 * Returns true if the contacts and details specified by this action target are equal to those specified by \a other
156 */
157bool QContactActionTarget::operator==(const QContactActionTarget& other) const
158{
159 return d.constData()->m_contact == other.d.constData()->m_contact
160 && d.constData()->m_details == other.d.constData()->m_details;
161}
162
163/*!
164 * Returns true if the contacts or details specified by this action target are different to that specified by \a other
165 */
166bool QContactActionTarget::operator!=(const QContactActionTarget& other) const
167{
168 return !(*this == other);
169}
170
171/*!
172 Returns the type of this action target.
173
174 The type is determined by the properties that have been set on this target.
175 */
176QContactActionTarget::Type QContactActionTarget::type() const
177{
178 if (d.constData()->m_contact.isEmpty())
179 return QContactActionTarget::Invalid;
180 switch (d.constData()->m_details.count()) {
181 case 0:
182 return QContactActionTarget::WholeContact;
183 case 1:
184 return QContactActionTarget::SingleDetail;
185 default:
186 return QContactActionTarget::MultipleDetails;
187 }
188}
189
190/*! Returns the hash value for \a key. */
191uint qHash(const QContactActionTarget& key)
192{
193 uint ret = qHash(key: key.contact());
194 foreach (const QContactDetail& det, key.details()) {
195 ret += qHash(key: det);
196 }
197 return ret;
198}
199
200#ifndef QT_NO_DATASTREAM
201/*! Streams the given \a target to the datastream \a out */
202QDataStream& operator<<(QDataStream& out, const QContactActionTarget& target)
203{
204 quint8 formatVersion = 1; // Version of QDataStream format for QContactActionTarget
205 out << formatVersion;
206 out << target.d.constData()->m_contact;
207 out << target.d.constData()->m_details;
208 return out;
209}
210
211/*! Streams \a target in from the datastream \a in */
212QDataStream& operator>>(QDataStream& in, QContactActionTarget& target)
213{
214 QContactActionTarget retn;
215 quint8 formatVersion;
216 in >> formatVersion;
217 if (formatVersion == 1) {
218 in >> retn.d->m_contact;
219 in >> retn.d->m_details;
220 } else {
221 in.setStatus(QDataStream::ReadCorruptData);
222 }
223 target = retn;
224 return in;
225}
226#endif
227
228#ifndef QT_NO_DEBUG_STREAM
229QDebug& operator<<(QDebug dbg, const QContactActionTarget& target)
230{
231 dbg.nospace() << "QContactActionTarget(" << target.contact() << target.details() << ')';
232 return dbg.maybeSpace();
233}
234#endif
235
236QT_END_NAMESPACE_CONTACTS
237

source code of qtpim/src/contacts/qcontactactiontarget.cpp