summaryrefslogtreecommitdiffstats
path: root/doc/src/contactsusage.qdoc
blob: 81e8308bf35c313752239670f41901a0a3765961 (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** 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 Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Free Documentation License Usage
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file.  Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: http://www.gnu.org/copyleft/fdl.html.
** $QT_END_LICENSE$
**
****************************************************************************/

/*!

\page contactsusage.html

\title Contacts API Usage

\tableofcontents

\section1 Introduction

This section provides some examples of common usage of the Qt Contacts API.
The most common use of the API is to retrieve a contact and then display
certain details of that contact.  To do so, several steps must be taken:
\list
   \o A contact manager must be instantiated
   \o The contact must be retrieved from the manager
   \o The required details of the contact must be selected from the contact
\endlist

The first step is usually as simple as:
\code
    QContactManager cm; // instantiate the default manager
\endcode

The second step requires either a filtering operation, or, if the id of the
contact is already known, a direct selection operation.  If you are interested
in all contacts, a "default filter" retrieve operation is used.  The retrieval
operations may either be \l{Contacts Synchronous API}{synchronous} or
\l{Contacts Asynchronous API}{asynchronous}; we recommend using asynchronous
operations for applications which require a responsive user interface.
For simplicity, however, the example below uses the synchronous API to
retrieve all contacts:
\code
    QList<QContact> allContacts = cm.contacts();
\endcode

The third step may be performed in several ways.  The recommended way is to
utilize the templated detail accessor, if you know which type of detail you
are interested in:
\code
    QContact firstContact = allContacts.first();
    qDebug() << "The first contact has a phone number:" << firstContact.detail<QContactPhoneNumber>().number();
\endcode

Alternatively, you can use the base \l QContactDetail class methods to select
the detail in which you are interested in, and the field keys specified in the
derived class to select the value which you are interested in:
\code
    qDebug() << "The first contact has a phone number:" << firstContact.detail(QContactPhoneNumber::DefinitionName).value(QContactPhoneNumber::FieldNumber);
\endcode

Note that in each case, if the contact did not have a phone number detail,
the return value of QContact::detail() is an empty detail.  Also note that in
the first case, the return value will be of the QContactPhoneNumber detail
type, whereas in the second case, the return value will be of the
QContactDetail (base-class detail) type -- although the actual detail returned
in both cases is exactly the same.

If you wish to retrieve all of the details of a contact, you may do something
similar to:
\code
    QList<QContactDetail> allDetails = firstContact.details();
\endcode

Alternatively, if you wish only to retrieve the details which are of some
particular type, you can use either the templated or non-templated accessor:
\code
    QList<QContactPhoneNumber> allPhoneNumbers = firstContact.details<QContactPhoneNumber>();
    QList<QContactDetail> allPhoneNumbers2 = firstContact.details(QContactPhoneNumber::DefinitionName);
\endcode

Note that in each case, if the contact did not have any phone number details,
the return value of QContact::details() is an empty list.  Also note that in
the first case, the return value will be a list of the QContactPhoneNumber
detail type, whereas in the second case, the return value will be a list of
the QContactDetail (base-class detail) type -- although the actual details
returned in both cases will be exactly the same.

The next most common use of the API is to save a contact.  Such an operation
consists of two steps:
\list
   \o Saving a detail in a contact
   \o Saving the contact in a manager
\endlist

Removing a contact is done similarly to saving a contact.  An example of these
two operations is given below.  Note that it uses the synchronous API to save
and remove the contact, although in a real application we recommend using the
asynchronous API to perform such manager-related operations.
\code
    QContactPhoneNumber newPhoneNumber;       // create the detail to add
    newPhoneNumber.setNumber("12345");        // set the value(s) to save
    firstContact.saveDetail(&newPhoneNumber); // save the detail in the contact
    cm.saveContact(&firstContact);            // save the contact in the manager
    cm.removeContact(firstContact.localId()); // remove the contact from the manager
\endcode

That's it!  For more in-depth discussion of usage of the API, see the sections
below.

\section1 Manager Settings And Configuration

Users of the contacts API can define which backend they wish to access if a
manager for that backend is available.  The list of available managers can be
queried programmatically at run-time, and the capabilities of different
managers can be ascertained by inspecting a QContactManager instance.
Furthermore, some managers can be constructed with parameters which affect the
operation of the backend.

\section2 Loading the default manager for the platform

Most users of the API will want to use the default manager for the platform,
which provides access to the system address book.  Instantiating a manager by
using the default constructor will result in the default manager for that
platform being instantiated.

The default constructor can either be used to create a manager on the stack,
in which case it will be deleted automatically when it goes out of scope:

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsample.cpp Loading the default manager for the platform

or it can be used explicitly to create a manager on the heap, in which case
the client must ensure that they delete the manager when they are finished
with it in order to avoid a memory leak:

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsample.cpp Loading the default manager for the platform on heap

\section2 Querying a manager for capabilities

Different managers will support different capabilities and details.  Clients
can use the meta data reporting functions of QContactManager to determine what
the capabilities of the manager they have instantiated might be.

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsample.cpp Querying a manager for capabilities

\section2 Loading the manager for a specific backend

In this example, the client loads a manager for a specific backend.  While
this could be found and retrieved using a more advanced plugin framework
(such as the Qt Service Framework), this code assumes that the client has
prior knowledge of the backend in question.

Clients may wish to use this feature of the API if they wish to store or
retrieve contact information to a particular manager (for example, one that
interfaces with a particular online service).

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsample.cpp Loading a specific manager backend

\section2 Loading a manager with specific parameters

The client loads a manager with specific parameters defined.  The
parameters which are available are backend specific, and so the client had
to know that the "Settings" parameter was valid for the particular backend,
and what argument it took. In this example, the client tells the backend to
load detail definitions saved in a particular settings file.

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsample.cpp Loading a specific manager backend with parameters

\section1 Contact Detail Manipulation

Once a contact has been created (or retrieved from a manager), the client can
retrieve, create, update or delete details from the contact.  Since QContact
and QContactDetail are both container (value) classes, the API offered for
these operations is purely synchronous.

A contact consists of the details it contains, as well as an id.  Some details
are read-only (such as the display label of a contact) or irremovable (like
the type of a contact), but most are freely modifiable by clients.

\section2 Adding a detail to a contact

The client adds a name and a phone number to a contact.

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsample.cpp Adding a detail to a contact

\section2 Updating a detail in a contact

The client updates the phone number of a contact.

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsample.cpp Updating a detail in a contact

\section2 Removing a detail from a contact

The client removes the phone number of a contact.

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsample.cpp Removing a detail from a contact

\section2 Viewing a specific detail of a contact

The client retrieves and displays the first phone number of a contact

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsample.cpp Viewing a specific detail of a contact

\section2 Viewing all of the details of a contact

The client retrieves all of the details of a contact, and displays them

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsample.cpp Viewing the details of a contact

It is important to note that details are implicitly shared objects with
particular semantics surrounding saving, removal and modification.  The
following example demonstrates these semantics

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsample.cpp Demonstration of detail sharing semantics

\section1 Persistent Contact Information

After instantiating a manager, clients will wish to retrieve or modify contact
information (including relationships and possibly detail definitions) which
is persistently stored in the manager (for example, in a database or online
cloud).

If the client wishes to use the asynchronous API, it is suggested that their
class uses member variables for the manager and requests, similarly to:

    \snippet snippets/qtcontactsdocsample/requestexample.h Class setup

This allows them to define slots which deal with the data as required when the
state of the request changes:

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsampleasync.cpp Example of an asynchronous request slot

Note that if the client is interested in receiving the results of the request
as they become available, rather than only the final set of results once the
request changes state (to \c FinishedState, for example), the client should
instead connect the QContactAbstractRequest::resultsAvailable() signal to the
slot which deals with the results.

\section2 Creating a new contact in a manager

The client creates a new contact and saves it in a manager

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsampleasync.cpp Creating a new contact in a manager

Alternatively, the client can explicitly block execution until the request is
complete, by doing something like:

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsampleasync.cpp Creating a new contact in a manager waiting until finished

The equivalent code using the synchronous API looks like:

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsample.cpp Synchronously creating a new contact in a manager

\section2 Retrieving contacts from a manager

The client requests all contacts from the manager which match a particular
filter.

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsampleasync.cpp Filtering contacts from a manager

The equivalent code using the synchronous API looks like:

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsample.cpp Synchronously filtering contacts from a manager

The client can also retrieve a particular existing contact from a manager, by
directly requesting the contact with a particular (previously known) id.
With the asynchronous API, this takes the form of another filter:

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsampleasync.cpp Retrieving an existing contact from a manager

The synchronous API provides a function specifically for this purpose:

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsample.cpp Synchronously retrieving an existing contact from a manager

\section2 Updating an existing contact in a manager

The client updates a previously saved contact by saving the updated version of
the contact.  Any contact whose id is the same as that of the updated contact
will be overwritten as a result of the save request.

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsampleasync.cpp Updating an existing contact in a manager

The equivalent code using the synchronous API looks like:

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsample.cpp Synchronously updating an existing contact in a manager

\section2 Removing a contact from a manager

The client removes a contact from the manager by specifying its local id.

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsampleasync.cpp Removing a contact from a manager

The equivalent code using the synchronous API looks like:

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsample.cpp Synchronously removing a contact from a manager

\section2 Creating a new relationship between two contacts

The client specifies a relationship between two contacts stored in the manager

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsampleasync.cpp Creating a new relationship between two contacts

The equivalent code using the synchronous API looks like:

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsample.cpp Synchronously creating a new relationship between two contacts

\section2 Retrieving relationships between contacts

The client requests the relationships that a particular contact is involved in
from the manager in which the contact is stored.

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsampleasync.cpp Retrieving relationships between contacts

The equivalent code using the synchronous API looks like:

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsample.cpp Synchronously retrieving relationships between contacts

When a contact is retrieved, it will contain a cache of the relationships in
which it is involved at the point in time at which it was retrieved.
This provides clients with a simple way to retrieve the relationships in which
a contact is involved, but carries the risk that the cache is stale.

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsample.cpp Retrieving relationships from cache

Clients can inform the manager that they do not require this cache of
relationships to be populated when retrieving a contact, which can allow a
manager to optimize contact retrieval.  Other retrieval optimizations are also
possible to specify, for example that they do not require action preferences
to be returned, or that they are only interested in certain types of details.
The following code shows how the client can inform the manager that they are
only interested in relationships of the \c HasMember type (groups):

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsampleasync.cpp Providing a fetch hint

The equivalent code using the synchronous API looks like:

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsample.cpp Synchronously providing a fetch hint

\section2 Removing a relationship between two contacts

The client can remove a relationship directly from a manager.

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsampleasync.cpp Removing a relationship

The equivalent code using the synchronous API looks like:

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsample.cpp Synchronously removing a relationship

Alternatively, when a contact which is involved in a relationship is removed,
any relationships in which it is involved will be removed also.

\section2 Querying the schema supported by a manager

The client queries the schema supported by a manager, and checks to see if a
particular detail definition supports a certain field.

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsampleasync.cpp Querying the schema supported by a manager

The equivalent code using the synchronous API looks like:

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsample.cpp Synchronously querying the schema supported by a manager

\section2 Modifying the schema supported by a manager

The client attempts to modify a particular detail definition by extending it
so that it supports an extra field.

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsampleasync.cpp Modifying the schema supported by a manager

The equivalent code using the synchronous API looks like:

    \snippet snippets/qtcontactsdocsample/qtcontactsdocsample.cpp Synchronously modifying the schema supported by a manager

Note that some managers do not support mutable definitions, and hence
attempting to modify or remove detail definitions in those managers will fail.

*/