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 "qcontactrelationship.h" |
35 | #include "qcontactrelationship_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 | |
44 | QT_BEGIN_NAMESPACE_CONTACTS |
45 | |
46 | /*! |
47 | \class QContactRelationship |
48 | \brief The QContactRelationship class describes a one-to-one relationship |
49 | between a locally-stored contact and another (possibly remote) contact. |
50 | |
51 | \inmodule QtContacts |
52 | |
53 | \ingroup contacts-main |
54 | |
55 | Each relationship is uniquely identified by the combination of the first |
56 | contact ID, second contact ID, and the relationship type. |
57 | |
58 | A relationship should not contain a second contact which is the same as |
59 | the first contact. |
60 | Any local contacts which are referenced in the relationship (that is, any |
61 | source contact, or any second contact whose manager URI is left empty or |
62 | whose manager URI references the manager that stores the source contact, |
63 | and in which the relationship will be saved) should exist. |
64 | |
65 | If any of these requirements are not met, validation of the relationship |
66 | may fail when attempting to save the relationship in a QContactManager. |
67 | |
68 | \sa QContactRelationshipFilter |
69 | */ |
70 | |
71 | /*! |
72 | \enum QContactRelationship::Role |
73 | Describes the roles that a contact may take in a relationship. |
74 | \value First The contact is the first contact in the relationship |
75 | \value Second The contact is the second contact in the relationship |
76 | \value Either The contact is either the first or second contact in the relationship |
77 | */ |
78 | |
79 | /*! |
80 | * \fn QContactRelationship::HasMember() |
81 | * The relationship type which identifies the first contact as being a group which includes the second contact |
82 | */ |
83 | |
84 | /*! |
85 | * \fn QContactRelationship::Aggregates() |
86 | * The relationship type which identifies the first contact as aggregating the second contact into a metacontact |
87 | */ |
88 | |
89 | /*! |
90 | * \fn QContactRelationship::IsSameAs() |
91 | * The relationship type which identifies the first contact as being the same contact as the second contact |
92 | */ |
93 | |
94 | /*! |
95 | * \fn QContactRelationship::HasAssistant() |
96 | * The relationship type which identifies the second contact as being the assistant of the first contact |
97 | */ |
98 | |
99 | /*! |
100 | * \fn QContactRelationship::HasManager() |
101 | * The relationship type which identifies the second contact as being the manager of the first contact |
102 | */ |
103 | |
104 | /*! |
105 | * \fn QContactRelationship::HasSpouse() |
106 | * The relationship type which identifies the second contact as being the spouse of the first contact |
107 | */ |
108 | |
109 | /*! |
110 | * Constructs a new relationship |
111 | */ |
112 | QContactRelationship::QContactRelationship() |
113 | : d(new QContactRelationshipPrivate) |
114 | { |
115 | } |
116 | |
117 | /*! |
118 | * Frees the memory in use by the relationship |
119 | */ |
120 | QContactRelationship::~QContactRelationship() |
121 | { |
122 | } |
123 | |
124 | /*! |
125 | * Creates a copy of the \a other relationship |
126 | */ |
127 | QContactRelationship::QContactRelationship(const QContactRelationship& other) |
128 | : d(other.d) |
129 | { |
130 | } |
131 | |
132 | /*! |
133 | * Assigns this relationship to be equal to \a other |
134 | */ |
135 | QContactRelationship& QContactRelationship::operator=(const QContactRelationship& other) |
136 | { |
137 | d = other.d; |
138 | return *this; |
139 | } |
140 | |
141 | /*! |
142 | * Returns true if this relationship is equal to the \a other relationship, otherwise returns false. |
143 | */ |
144 | bool QContactRelationship::operator==(const QContactRelationship &other) const |
145 | { |
146 | if (d.constData()->m_first != other.d.constData()->m_first) |
147 | return false; |
148 | if (d.constData()->m_second != other.d.constData()->m_second) |
149 | return false; |
150 | if (d.constData()->m_relationshipType != other.d.constData()->m_relationshipType) |
151 | return false; |
152 | return true; |
153 | } |
154 | |
155 | /*! |
156 | * Returns the hash value for \a key. |
157 | */ |
158 | uint qHash(const QContactRelationship &key) |
159 | { |
160 | return qHash(id: key.first()) + qHash(id: key.second()) |
161 | + QT_PREPEND_NAMESPACE(qHash)(key: key.relationshipType()); |
162 | } |
163 | |
164 | #ifndef QT_NO_DEBUG_STREAM |
165 | QDebug operator<<(QDebug dbg, const QContactRelationship& rel) |
166 | { |
167 | dbg.nospace() << "QContactRelationship(" << rel.first() << ' ' << rel.relationshipType() |
168 | << ' ' << rel.second() << ')'; |
169 | return dbg.maybeSpace(); |
170 | } |
171 | #endif |
172 | |
173 | #ifndef QT_NO_DATASTREAM |
174 | /*! |
175 | * Writes \a rel to the stream \a out. |
176 | */ |
177 | QDataStream& operator<<(QDataStream& out, const QContactRelationship& rel) |
178 | { |
179 | quint8 formatVersion = 1; // Version of QDataStream format for QContactRelationship |
180 | return out << formatVersion << rel.first() << rel.relationshipType() << rel.second(); |
181 | } |
182 | |
183 | /*! |
184 | * Reads a contact relationship from stream \a in into \a rel. |
185 | */ |
186 | QDataStream& operator>>(QDataStream& in, QContactRelationship& rel) |
187 | { |
188 | rel = QContactRelationship(); |
189 | quint8 formatVersion; |
190 | in >> formatVersion; |
191 | if (formatVersion == 1) { |
192 | QContactId first; |
193 | QString relationshipType; |
194 | QContactId second; |
195 | in >> first >> relationshipType >> second; |
196 | rel.setFirst(first); |
197 | rel.setRelationshipType(relationshipType); |
198 | rel.setSecond(second); |
199 | } else { |
200 | in.setStatus(QDataStream::ReadCorruptData); |
201 | } |
202 | return in; |
203 | } |
204 | #endif |
205 | |
206 | /*! |
207 | * \fn QContactRelationship::operator!=(const QContactRelationship& other) const |
208 | * Returns true if this relationship is not equal to \a other, otherwise returns false. |
209 | */ |
210 | |
211 | /*! |
212 | * Returns the ID of the locally-stored contact which has a relationship of the given type with the second contact |
213 | * \sa relationshipType(), second(), setFirst() |
214 | */ |
215 | QContactId QContactRelationship::first() const |
216 | { |
217 | return d.constData()->m_first; |
218 | } |
219 | |
220 | /*! |
221 | * Returns the ID of the contact with which the first contact has a relationship of the given type |
222 | * \sa relationshipType(), first() |
223 | */ |
224 | QContactId QContactRelationship::second() const |
225 | { |
226 | return d.constData()->m_second; |
227 | } |
228 | |
229 | /*! |
230 | * Returns the type of relationship which the source contact has with the destination contacts |
231 | * \sa setRelationshipType() |
232 | */ |
233 | QString QContactRelationship::relationshipType() const |
234 | { |
235 | return d.constData()->m_relationshipType; |
236 | } |
237 | |
238 | /*! |
239 | * Sets the ID of the first contact in the relationship to \a firstContactId. This contact |
240 | * must be stored in the manager in which the relationship is stored, and has |
241 | * a relationship of the specified type with the second contact. |
242 | * \sa first() |
243 | */ |
244 | void QContactRelationship::setFirst(const QContactId& firstContactId) |
245 | { |
246 | d->m_first = firstContactId; |
247 | } |
248 | |
249 | /*! |
250 | * Sets the second contact in the relationship to \a secondContact. The first contact |
251 | * has a relationship of the specified type with this contact. |
252 | * \sa second() |
253 | */ |
254 | void QContactRelationship::setSecond(const QContactId& secondContactId) |
255 | { |
256 | d->m_second = secondContactId; |
257 | } |
258 | |
259 | /*! |
260 | * Sets the type of relationship that the source contact has with the destination contacts |
261 | * to \a relationshipType. |
262 | * \sa relationshipType() |
263 | */ |
264 | void QContactRelationship::setRelationshipType(const QString& relationshipType) |
265 | { |
266 | d->m_relationshipType = relationshipType; |
267 | } |
268 | |
269 | QT_END_NAMESPACE_CONTACTS |
270 | |