summaryrefslogtreecommitdiffstats
path: root/src/contacts/qcontactactiontarget.cpp
blob: 4fe41a54986027fbf320d3de4cb40365089af0ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtContacts module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qcontactactiontarget.h"
#include "qcontactactiontarget_p.h"

#ifndef QT_NO_DATASTREAM
#include <QtCore/qdatastream.h>
#endif
#ifndef QT_NO_DEBUG_STREAM
#include <QtCore/qdebug.h>
#endif

QT_BEGIN_NAMESPACE_CONTACTS

/*!
  \class QContactActionTarget
  \brief The QContactActionTarget class provides information about the
  target of an action.  It may be either a contact, a contact and a detail
  of that contact, or a contact and a list of the details of the contact,
  which together should be used by the action.
  \ingroup contacts-actions
  \inmodule QtContacts
*/

/*!
  \enum QContactActionTarget::Type
  This enumerator defines the type of a QContactActionTarget.

  \value Invalid The type is invalid.
  \value WholeContact The type is a complete contact.
  \value SingleDetail The type is only a single detail.
  \value MultipleDetails The type contains multiple details.
*/

/*!
 * Constructs a new action target from the given \a contact and the given list of that contact's \a details.
 * If no \a contact is specified, the target will be invalid.  If a \a contact but no \a details are specified,
 * the target will be valid, but the action which operates on the target may fail (for example, if it requires
 * a certain detail to be specified in order to perform the action).
 */
QContactActionTarget::QContactActionTarget(const QContact& contact, const QList<QContactDetail>& details)
        : d(new QContactActionTargetPrivate(contact, details))
{
}

/*!
 * Constructs a new action target from the given \a contact and a specific \a detail of that contact
 */
QContactActionTarget::QContactActionTarget(const QContact& contact, const QContactDetail& detail)
        : d(new QContactActionTargetPrivate(contact, QList<QContactDetail>() << detail))
{
}

/*!
 * Constructs a copy of the \a other action target
 */
QContactActionTarget::QContactActionTarget(const QContactActionTarget& other)
        : d(other.d)
{
}

/*!
 * Assigns this action target to be equal to \a other
 */
QContactActionTarget& QContactActionTarget::operator=(const QContactActionTarget& other)
{
    d = other.d;
    return *this;
}

/*!
 * Cleans up any memory in use by the action target
 */
QContactActionTarget::~QContactActionTarget()
{
}

/*!
 * Returns the contact that this action target will operate on.
 * \sa details()
 */
QContact QContactActionTarget::contact() const
{
    return d.constData()->m_contact;
}

/*!
 * Returns the details that this action target will operate on.
 * \sa contact()
 */
QList<QContactDetail> QContactActionTarget::details() const
{
    return d.constData()->m_details;
}

/*!
 * Sets the contact that this action target will operate on to \a contact.
 * \sa setDetails()
 */
void QContactActionTarget::setContact(const QContact& contact)
{
    d->m_contact = contact;
}

/*!
 * Sets the details that this action target will operate on to \a details.
 * \sa setContact()
 */
void QContactActionTarget::setDetails(const QList<QContactDetail>& details)
{
    d->m_details = details;
}

/*!
 * Returns true if the target contact is not the default constructed contact.
 * The validity of any specified details is not checked by this function.
 */
bool QContactActionTarget::isValid() const
{
    return (d.constData()->m_contact != QContact());
}

/*!
 * Returns true if the contacts and details specified by this action target are equal to those specified by \a other
 */
bool QContactActionTarget::operator==(const QContactActionTarget& other) const
{
    return d.constData()->m_contact == other.d.constData()->m_contact
            && d.constData()->m_details == other.d.constData()->m_details;
}

/*!
 * Returns true if the contacts or details specified by this action target are different to that specified by \a other
 */
bool QContactActionTarget::operator!=(const QContactActionTarget& other) const
{
    return !(*this == other);
}

/*!
  Returns the type of this action target.

  The type is determined by the properties that have been set on this target.
 */
QContactActionTarget::Type QContactActionTarget::type() const
{
    if (d.constData()->m_contact.isEmpty())
        return QContactActionTarget::Invalid;
    switch (d.constData()->m_details.count()) {
        case 0:
            return QContactActionTarget::WholeContact;
        case 1:
            return QContactActionTarget::SingleDetail;
        default:
            return QContactActionTarget::MultipleDetails;
    }
}

/*! Returns the hash value for \a key. */
size_t qHash(const QContactActionTarget& key)
{
    size_t ret = qHash(key.contact());
    foreach (const QContactDetail& det, key.details()) {
        ret += qHash(det);
    }
    return ret;
}

#ifndef QT_NO_DATASTREAM
/*! Streams the given \a target to the datastream \a out */
QDataStream& operator<<(QDataStream& out, const QContactActionTarget& target)
{
    quint8 formatVersion = 1; // Version of QDataStream format for QContactActionTarget
    out << formatVersion;
    out << target.d.constData()->m_contact;
    out << target.d.constData()->m_details;
    return out;
}

/*! Streams \a target in from the datastream \a in */
QDataStream& operator>>(QDataStream& in, QContactActionTarget& target)
{
    QContactActionTarget retn;
    quint8 formatVersion;
    in >> formatVersion;
    if (formatVersion == 1) {
        in >> retn.d->m_contact;
        in >> retn.d->m_details;
    } else {
        in.setStatus(QDataStream::ReadCorruptData);
    }
    target = retn;
    return in;
}
#endif

#ifndef QT_NO_DEBUG_STREAM
QDebug& operator<<(QDebug dbg, const QContactActionTarget& target)
{
    dbg.nospace() << "QContactActionTarget(" << target.contact() << target.details() << ')';
    return dbg.maybeSpace();
}
#endif

QT_END_NAMESPACE_CONTACTS