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 "qcontactsortorder.h"
35#include "qcontactsortorder_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 QContactSortOrder
48 \brief The QContactSortOrder class defines how a list of contacts should be ordered according to some criteria
49
50 \inmodule QtContacts
51
52 */
53
54/*!
55 * \enum QContactSortOrder::BlankPolicy
56 * Enumerates the ways in which the sort order interprets blanks when sorting contacts
57 * \value BlanksFirst Considers blank values to evaluate to less than all other values in comparisons
58 * \value BlanksLast Considers blank values to evaluate to greater than all other values in comparisons
59 */
60
61/*!
62 * \fn QContactSortOrder::operator QList<QContactSortOrder>() const
63 * Constructs a new list of sort orders containing only the current sort order
64 */
65
66/*!
67 * \fn QContactSortOrder::operator!=(const QContactSortOrder& other) const
68 * Returns true if this sort order is not identical to the \a other sort order
69 * \sa operator==()
70 */
71
72/*!
73 * Constructs a new sort order
74 */
75QContactSortOrder::QContactSortOrder()
76 : d(new QContactSortOrderPrivate())
77{
78}
79
80/*!
81 * Frees any memory in use by this sort order
82 */
83QContactSortOrder::~QContactSortOrder()
84{
85}
86
87/*!
88 * Constructs a copy of the \a other sort order
89 */
90QContactSortOrder::QContactSortOrder(const QContactSortOrder& other)
91 : d(other.d)
92{
93
94}
95
96/*!
97 * Assigns this sort order to be equal to \a other
98 */
99QContactSortOrder& QContactSortOrder::operator=(const QContactSortOrder& other)
100{
101 if (this != &other) {
102 d = other.d;
103 }
104 return *this;
105}
106
107/*!
108 * Returns true if the sort order is able to be used to sort a list of contacts; otherwise, returns false
109 */
110bool QContactSortOrder::isValid() const
111{
112 return d->m_type != QContactDetail::TypeUndefined;
113}
114
115/*!
116 * Returns true if this sort order is identical to the \a other sort order
117 * \sa operator!=()
118 */
119bool QContactSortOrder::operator ==(const QContactSortOrder& other) const
120{
121 if (d.constData()->m_blankPolicy == other.d.constData()->m_blankPolicy &&
122 d.constData()->m_direction == other.d.constData()->m_direction &&
123 d.constData()->m_sensitivity == other.d.constData()->m_sensitivity &&
124 d.constData()->m_type == other.d.constData()->m_type &&
125 d.constData()->m_field == other.d.constData()->m_field)
126 return true;
127 return false;
128}
129
130
131#ifndef QT_NO_DATASTREAM
132/*!
133 * Writes \a sortOrder to the stream \a out.
134 */
135QDataStream& operator<<(QDataStream& out, const QContactSortOrder& sortOrder)
136{
137 quint8 formatVersion = 1; // Version of QDataStream format for QContactSortOrder
138 return out << formatVersion
139 << static_cast<quint32>(sortOrder.detailType())
140 << sortOrder.detailField()
141 << static_cast<quint32>(sortOrder.blankPolicy())
142 << static_cast<quint32>(sortOrder.direction())
143 << static_cast<quint32>(sortOrder.caseSensitivity());
144}
145
146/*!
147 * Reads a sort order from stream \a in into \a sortOrder.
148 */
149QDataStream& operator>>(QDataStream& in, QContactSortOrder& sortOrder)
150{
151 sortOrder = QContactSortOrder();
152 quint8 formatVersion;
153 in >> formatVersion;
154 if (formatVersion == 1) {
155 quint32 type;
156 int field;
157 quint32 blankPolicy;
158 quint32 direction;
159 quint32 caseSensitivity;
160 in >> type >> field >> blankPolicy >> direction >> caseSensitivity;
161 sortOrder.setDetailType(type: QContactDetail::DetailType(type), field);
162 sortOrder.setBlankPolicy(QContactSortOrder::BlankPolicy(blankPolicy));
163 sortOrder.setDirection(Qt::SortOrder(direction));
164 sortOrder.setCaseSensitivity(Qt::CaseSensitivity(caseSensitivity));
165 } else {
166 in.setStatus(QDataStream::ReadCorruptData);
167 }
168 return in;
169}
170#endif
171
172#ifndef QT_NO_DEBUG_STREAM
173/*!
174 Outputs \a sortOrder to the debug stream \a dbg
175 */
176QDebug operator<<(QDebug dbg, const QContactSortOrder& sortOrder)
177{
178 dbg.nospace() << "QContactSortOrder("
179 << "detailType=" << static_cast<quint32>(sortOrder.detailType()) << ","
180 << "detailField=" << sortOrder.detailField() << ","
181 << "blankPolicy=" << static_cast<quint32>(sortOrder.blankPolicy()) << ","
182 << "direction=" << static_cast<quint32>(sortOrder.direction()) << ","
183 << "caseSensitivity=" << static_cast<quint32>(sortOrder.caseSensitivity())
184 << ")";
185 return dbg.maybeSpace();
186}
187#endif
188
189/*!
190 * Sets the type of the details which will be inspected to perform sorting to \a type
191 * and the name of those details' fields which contains the value which contacts will be sorted by to \a field
192 *
193 * If \a field is not specified, or equal to -1, the contact with a detail of the specified type
194 * would appear before or after the contact that lacks a detail of the specified type,
195 * according to blankPolicy().
196 *
197 * \sa detailType(), detailField()
198 */
199void QContactSortOrder::setDetailType(QContactDetail::DetailType type, int field)
200{
201 d->m_type = type;
202 d->m_field = field;
203}
204
205/*!
206 * Sets the sort order's policy on blank values with respect to sorting to \a blankPolicy
207 * \sa blankPolicy()
208 */
209void QContactSortOrder::setBlankPolicy(BlankPolicy blankPolicy)
210{
211 d->m_blankPolicy = blankPolicy;
212}
213
214/*!
215 * Sets the sort order direction to \a direction
216 * \sa direction()
217 */
218void QContactSortOrder::setDirection(Qt::SortOrder direction)
219{
220 d->m_direction = direction;
221}
222
223/*!
224 * Returns the type of the details which will be inspected to perform sorting.
225 * Note that if a contact has multiple details of the definition, the result of the sorting
226 * is undefined.
227 * \sa setDetailType()
228 */
229QContactDetail::DetailType QContactSortOrder::detailType() const
230{
231 return d.constData()->m_type;
232}
233
234/*!
235 * Returns the detail field which the sorting order will be based on.
236 * \sa setDetailType()
237 */
238int QContactSortOrder::detailField() const
239{
240 return d.constData()->m_field;
241}
242
243
244/*!
245 * Returns the blank policy of the sort order
246 * \sa setBlankPolicy()
247 */
248QContactSortOrder::BlankPolicy QContactSortOrder::blankPolicy() const
249{
250 return d.constData()->m_blankPolicy;
251}
252
253/*!
254 * Returns the direction of the sort order
255 * \sa setDirection()
256 */
257Qt::SortOrder QContactSortOrder::direction() const
258{
259 return d.constData()->m_direction;
260}
261
262/*!
263 * Returns the case sensitivity of the sort order
264 * \sa setCaseSensitivity()
265 */
266Qt::CaseSensitivity QContactSortOrder::caseSensitivity() const
267{
268 return d.constData()->m_sensitivity;
269}
270
271/*!
272 * Sets the case sensitivity of the sort order to \a sensitivity
273 * \sa caseSensitivity()
274 */
275void QContactSortOrder::setCaseSensitivity(Qt::CaseSensitivity sensitivity)
276{
277 d->m_sensitivity = sensitivity;
278}
279
280QT_END_NAMESPACE_CONTACTS
281

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