~chris.gagnon/+junk/qtpim-coverage

« back to all changes in this revision

Viewing changes to src/contacts/doc/src/contactsusage.qdoc

  • Committer: chris.gagnon
  • Date: 2013-12-10 23:09:37 UTC
  • Revision ID: chris.gagnon@canonical.com-20131210230937-2akf1ft1edcttk87
first post

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the documentation of the Qt PIM Module.
 
7
**
 
8
** $QT_BEGIN_LICENSE:FDL$
 
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 Digia.  For licensing terms and
 
14
** conditions see http://qt.digia.com/licensing.  For further information
 
15
** use the contact form at http://qt.digia.com/contact-us.
 
16
**
 
17
** GNU Free Documentation License Usage
 
18
** Alternatively, this file may be used under the terms of the GNU Free
 
19
** Documentation License version 1.3 as published by the Free Software
 
20
** Foundation and appearing in the file included in the packaging of
 
21
** this file.  Please review the following information to ensure
 
22
** the GNU Free Documentation License version 1.3 requirements
 
23
** will be met: http://www.gnu.org/copyleft/fdl.html.
 
24
** $QT_END_LICENSE$
 
25
**
 
26
****************************************************************************/
 
27
 
 
28
/*!
 
29
 
 
30
\page contactsusage.html
 
31
 
 
32
\title Qt Contacts API Usage
 
33
 
 
34
\tableofcontents
 
35
 
 
36
    With the Qt Contacts API, typical use cases are:
 
37
 
 
38
        \list
 
39
            \li Access a list of personal contacts from the contact database
 
40
                supported by the selected backend.
 
41
            \li Sort or filter contacts and access them as a list.
 
42
            \li Import contacts in vCard format into the selected
 
43
                contacts database.
 
44
            \li Export contacts to a vCard format to share elsewhere.
 
45
        \endlist
 
46
 
 
47
This section provides some examples of common usage of the Qt Contacts API.
 
48
 
 
49
\section1 Retrieving Contact Details
 
50
 
 
51
The most common use of the API is to retrieve a contact and then display
 
52
certain details of that contact.  To do so, several steps must be taken:
 
53
\list
 
54
   \li A contact manager must be instantiated
 
55
   \li The contact must be retrieved from the manager
 
56
   \li The required details of the contact must be selected from the contact
 
57
\endlist
 
58
 
 
59
\section2 Instantiating Contact Manager
 
60
 
 
61
The first step is usually as simple as:
 
62
\code
 
63
    QContactManager cm; // instantiate the default manager
 
64
\endcode
 
65
 
 
66
\section2 Retrieving a Contact from the Manager
 
67
 
 
68
The second step requires either a filtering operation, or, if the id of the
 
69
contact is already known, a direct selection operation.  If you are interested
 
70
in all contacts, a \e {default filter} retrieve operation is used. The retrieval
 
71
operations may either be \l{Qt Contacts Synchronous API}{synchronous} or
 
72
\l{Qt Contacts Asynchronous API}{asynchronous}; we recommend using asynchronous
 
73
operations for applications which require a responsive user interface.
 
74
For simplicity, however, the example below uses the synchronous API to
 
75
retrieve all contacts:
 
76
\code
 
77
    QList<QContact> allContacts = cm.contacts();
 
78
\endcode
 
79
 
 
80
\section2 Selecting a Detail
 
81
 
 
82
The third step may be performed in several ways.  The recommended way is to
 
83
utilize the templated detail accessor, if you know which type of detail you
 
84
are interested in:
 
85
\code
 
86
    QContact firstContact = allContacts.first();
 
87
    qDebug() << "The first contact has a phone number:" << firstContact.detail<QContactPhoneNumber>().number();
 
88
\endcode
 
89
 
 
90
Alternatively, you can use the base \l QContactDetail class methods to select
 
91
the detail in which you are interested in, and the field keys specified in the
 
92
derived class to select the value which you are interested in:
 
93
\code
 
94
    qDebug() << "The first contact has a phone number:" << firstContact.detail(QContactPhoneNumber::DefinitionName).value(QContactPhoneNumber::FieldNumber);
 
95
\endcode
 
96
 
 
97
Note that in each case, if the contact did not have a phone number detail,
 
98
the return value of QContact::detail() is an empty detail.  Also note that in
 
99
the first case, the return value will be of the QContactPhoneNumber detail
 
100
type, whereas in the second case, the return value will be of the
 
101
QContactDetail (base-class detail) type -- although the actual detail returned
 
102
in both cases is exactly the same.
 
103
 
 
104
\section2 Retrieving All Details
 
105
 
 
106
If you wish to retrieve all of the details of a contact, you may do something
 
107
similar to:
 
108
\code
 
109
    QList<QContactDetail> allDetails = firstContact.details();
 
110
\endcode
 
111
 
 
112
\section2 Retrieving Details of a Type
 
113
 
 
114
Alternatively, if you wish only to retrieve the details which are of some
 
115
particular type, you can use either the templated or non-templated accessor:
 
116
\code
 
117
    QList<QContactPhoneNumber> allPhoneNumbers = firstContact.details<QContactPhoneNumber>();
 
118
    QList<QContactDetail> allPhoneNumbers2 = firstContact.details(QContactPhoneNumber::DefinitionName);
 
119
\endcode
 
120
 
 
121
Note that in each case, if the contact did not have any phone number details,
 
122
the return value of QContact::details() is an empty list.  Also note that in
 
123
the first case, the return value will be a list of the QContactPhoneNumber
 
124
detail type, whereas in the second case, the return value will be a list of
 
125
the QContactDetail (base-class detail) type -- although the actual details
 
126
returned in both cases will be exactly the same.
 
127
 
 
128
\section1 Saving Contacts
 
129
 
 
130
The next most common use of the API is to save a contact.  Such an operation
 
131
consists of two steps:
 
132
\list
 
133
   \li Saving a detail in a contact
 
134
   \li Saving the contact in a manager
 
135
\endlist
 
136
 
 
137
Removing a contact is done similarly to saving a contact.  An example of these
 
138
two operations is given below.  Note that it uses the synchronous API to save
 
139
and remove the contact, although in a real application we recommend using the
 
140
asynchronous API to perform such manager-related operations.
 
141
\code
 
142
    QContactPhoneNumber newPhoneNumber;       // create the detail to add
 
143
    newPhoneNumber.setNumber("12345");        // set the value(s) to save
 
144
    firstContact.saveDetail(&newPhoneNumber); // save the detail in the contact
 
145
    cm.saveContact(&firstContact);            // save the contact in the manager
 
146
    cm.removeContact(firstContact.id()); // remove the contact from the manager
 
147
\endcode
 
148
 
 
149
That's it!  For more in-depth discussion of usage of the API, see the sections
 
150
below.
 
151
 
 
152
\section1 Configuring Managers
 
153
 
 
154
Users of the contacts API can define which backend they wish to access if a
 
155
manager for that backend is available.  The list of available managers can be
 
156
queried programmatically at run-time, and the capabilities of different
 
157
managers can be ascertained by inspecting a QContactManager instance.
 
158
Furthermore, some managers can be constructed with parameters which affect the
 
159
operation of the backend.
 
160
 
 
161
\section2 Loading the Default Manager for the Platform
 
162
 
 
163
Most users of the API will want to use the default manager for the platform,
 
164
which provides access to the system address book.  Instantiating a manager by
 
165
using the default constructor will result in the default manager for that
 
166
platform being instantiated.
 
167
 
 
168
The default constructor can either be used to create a manager on the stack,
 
169
in which case it will be deleted automatically when it goes out of scope:
 
170
 
 
171
    \snippet qtcontactsdocsample/qtcontactsdocsample.cpp Loading the default manager for the platform
 
172
 
 
173
or it can be used explicitly to create a manager on the heap, in which case
 
174
the client must ensure that it deletes the manager when it is finished
 
175
with it in order to avoid a memory leak:
 
176
 
 
177
    \snippet qtcontactsdocsample/qtcontactsdocsample.cpp Loading the default manager for the platform on heap
 
178
 
 
179
\section2 Querying a Manager for Capabilities
 
180
 
 
181
Different managers will support different capabilities and details.  Clients
 
182
can use the meta data reporting functions of QContactManager to determine what
 
183
the capabilities of the manager they have instantiated might be.
 
184
 
 
185
    \snippet qtcontactsdocsample/qtcontactsdocsample.cpp Querying a manager for capabilities
 
186
 
 
187
\section2 Loading the Manager for a Specific Backend
 
188
 
 
189
In this example, the client loads a manager for a specific backend.  While
 
190
this could be found and retrieved using a more advanced plugin framework
 
191
(such as the Qt Service Framework), this code assumes that the client has
 
192
prior knowledge of the backend in question.
 
193
 
 
194
Clients may wish to use this feature of the API if they wish to store or
 
195
retrieve contact information to a particular manager (for example, one that
 
196
interfaces with a particular online service).
 
197
 
 
198
    \snippet qtcontactsdocsample/qtcontactsdocsample.cpp Loading a specific manager backend
 
199
 
 
200
\section2 Loading a Manager with Specific Parameters
 
201
 
 
202
The client loads a manager with specific parameters defined.  The
 
203
parameters which are available are backend specific, and so the client had
 
204
to know that the \e Settings parameter was valid for the particular backend,
 
205
and what argument it took. In this example, the client tells the backend to
 
206
load detail definitions saved in a particular settings file.
 
207
 
 
208
    \snippet qtcontactsdocsample/qtcontactsdocsample.cpp Loading a specific manager backend with parameters
 
209
 
 
210
\section1 Manipulating Contact Details
 
211
 
 
212
Once a contact has been created (or retrieved from a manager), the client can
 
213
retrieve, create, update or delete details from the contact.  Since QContact
 
214
and QContactDetail are both container (value) classes, the API offered for
 
215
these operations is purely synchronous.
 
216
 
 
217
A contact consists of the details it contains, as well as an id.  Some details
 
218
are read-only (such as the display label of a contact) or irremovable (like
 
219
the type of a contact), but most are freely modifiable by clients.
 
220
 
 
221
\section2 Adding Details
 
222
 
 
223
The client adds a name and a phone number to a contact.
 
224
 
 
225
    \snippet qtcontactsdocsample/qtcontactsdocsample.cpp Adding a detail to a contact
 
226
 
 
227
\section2 Updating Details
 
228
 
 
229
The client updates the phone number of a contact.
 
230
 
 
231
    \snippet qtcontactsdocsample/qtcontactsdocsample.cpp Updating a detail in a contact
 
232
 
 
233
\section2 Removing Details
 
234
 
 
235
The client removes the phone number of a contact.
 
236
 
 
237
    \snippet qtcontactsdocsample/qtcontactsdocsample.cpp Removing a detail from a contact
 
238
 
 
239
\section2 Viewing Details
 
240
 
 
241
The client retrieves and displays the first phone number of a contact.
 
242
 
 
243
    \snippet qtcontactsdocsample/qtcontactsdocsample.cpp Viewing a specific detail of a contact
 
244
 
 
245
\section2 Viewing All Details of a Contact
 
246
 
 
247
The client retrieves all of the details of a contact, and displays them.
 
248
 
 
249
    \snippet qtcontactsdocsample/qtcontactsdocsample.cpp Viewing the details of a contact
 
250
 
 
251
\note Details are implicitly shared objects with
 
252
particular semantics surrounding saving, removal and modification.  The
 
253
following example demonstrates these semantics.
 
254
 
 
255
    \snippet qtcontactsdocsample/qtcontactsdocsample.cpp Demonstration of detail sharing semantics
 
256
 
 
257
\section1 Persistent Contact Information
 
258
 
 
259
After instantiating a manager, clients will wish to retrieve or modify contact
 
260
information (including relationships and possibly detail definitions) which
 
261
is persistently stored in the manager (for example, in a database or online
 
262
cloud).
 
263
 
 
264
If the client wishes to use the asynchronous API, it is suggested that their
 
265
class uses member variables for the manager and requests, similarly to:
 
266
 
 
267
    \snippet qtcontactsdocsample/requestexample.h Class setup
 
268
 
 
269
This allows them to define slots which deal with the data as required when the
 
270
state of the request changes:
 
271
 
 
272
    \snippet qtcontactsdocsample/qtcontactsdocsampleasync.cpp Example of an asynchronous request slot
 
273
 
 
274
Note that if the client is interested in receiving the results of the request
 
275
as they become available, rather than only the final set of results once the
 
276
request changes state (to \c FinishedState, for example), the client should
 
277
instead connect the QContactAbstractRequest::resultsAvailable() signal to the
 
278
slot which deals with the results.
 
279
 
 
280
\section2 Creating Contacts
 
281
 
 
282
The client creates a new contact and saves it in a manager.
 
283
 
 
284
    \snippet qtcontactsdocsample/qtcontactsdocsampleasync.cpp Creating a new contact in a manager
 
285
 
 
286
Alternatively, the client can explicitly block execution until the request is
 
287
complete, by doing something like:
 
288
 
 
289
    \snippet qtcontactsdocsample/qtcontactsdocsampleasync.cpp Creating a new contact in a manager waiting until finished
 
290
 
 
291
The equivalent code using the synchronous API looks like:
 
292
 
 
293
    \snippet qtcontactsdocsample/qtcontactsdocsample.cpp Synchronously creating a new contact in a manager
 
294
 
 
295
\section2 Retrieving Contacts
 
296
 
 
297
The client requests all contacts from the manager which match a particular
 
298
filter.
 
299
 
 
300
    \snippet qtcontactsdocsample/qtcontactsdocsampleasync.cpp Filtering contacts from a manager
 
301
 
 
302
The equivalent code using the synchronous API looks like:
 
303
 
 
304
    \snippet qtcontactsdocsample/qtcontactsdocsample.cpp Synchronously filtering contacts from a manager
 
305
 
 
306
The client can also retrieve a particular existing contact from a manager, by
 
307
directly requesting the contact with a particular (previously known) id.
 
308
With the asynchronous API, this takes the form of another filter:
 
309
 
 
310
    \snippet qtcontactsdocsample/qtcontactsdocsampleasync.cpp Retrieving an existing contact from a manager
 
311
 
 
312
The synchronous API provides a function specifically for this purpose:
 
313
 
 
314
    \snippet qtcontactsdocsample/qtcontactsdocsample.cpp Synchronously retrieving an existing contact from a manager
 
315
 
 
316
\section2 Updating Contacts
 
317
 
 
318
The client updates a previously saved contact by saving the updated version of
 
319
the contact.  Any contact whose id is the same as that of the updated contact
 
320
will be overwritten as a result of the save request.
 
321
 
 
322
    \snippet qtcontactsdocsample/qtcontactsdocsampleasync.cpp Updating an existing contact in a manager
 
323
 
 
324
The equivalent code using the synchronous API looks like:
 
325
 
 
326
    \snippet qtcontactsdocsample/qtcontactsdocsample.cpp Synchronously updating an existing contact in a manager
 
327
 
 
328
\section2 Removing Contacts
 
329
 
 
330
The client removes a contact from the manager by specifying its id.
 
331
 
 
332
    \snippet qtcontactsdocsample/qtcontactsdocsampleasync.cpp Removing a contact from a manager
 
333
 
 
334
The equivalent code using the synchronous API looks like:
 
335
 
 
336
    \snippet qtcontactsdocsample/qtcontactsdocsample.cpp Synchronously removing a contact from a manager
 
337
 
 
338
\section2 Creating Relationships
 
339
 
 
340
The client specifies a relationship between two contacts stored in the manager
 
341
 
 
342
    \snippet qtcontactsdocsample/qtcontactsdocsampleasync.cpp Creating a new relationship between two contacts
 
343
 
 
344
The equivalent code using the synchronous API looks like:
 
345
 
 
346
    \snippet qtcontactsdocsample/qtcontactsdocsample.cpp Synchronously creating a new relationship between two contacts
 
347
 
 
348
\section2 Retrieving Relationships
 
349
 
 
350
The client requests the relationships that a particular contact is involved in
 
351
from the manager in which the contact is stored.
 
352
 
 
353
    \snippet qtcontactsdocsample/qtcontactsdocsampleasync.cpp Retrieving relationships between contacts
 
354
 
 
355
The equivalent code using the synchronous API looks like:
 
356
 
 
357
    \snippet qtcontactsdocsample/qtcontactsdocsample.cpp Synchronously retrieving relationships between contacts
 
358
 
 
359
When a contact is retrieved, it will contain a cache of the relationships in
 
360
which it is involved at the point in time at which it was retrieved.
 
361
This provides clients with a simple way to retrieve the relationships in which
 
362
a contact is involved, but carries the risk that the cache is stale.
 
363
 
 
364
    \snippet qtcontactsdocsample/qtcontactsdocsample.cpp Retrieving relationships from cache
 
365
 
 
366
Clients can inform the manager that they do not require this cache of
 
367
relationships to be populated when retrieving a contact, which can allow a
 
368
manager to optimize contact retrieval.  Other retrieval optimizations are also
 
369
possible to specify, for example that they do not require action preferences
 
370
to be returned, or that they are only interested in certain types of details.
 
371
The following code shows how the client can inform the manager that they are
 
372
only interested in relationships of the \c HasMember type (groups):
 
373
 
 
374
    \snippet qtcontactsdocsample/qtcontactsdocsampleasync.cpp Providing a fetch hint
 
375
 
 
376
The equivalent code using the synchronous API looks like:
 
377
 
 
378
    \snippet qtcontactsdocsample/qtcontactsdocsample.cpp Synchronously providing a fetch hint
 
379
 
 
380
\section2 Removing Relationships
 
381
 
 
382
The client can remove a relationship directly from a manager.
 
383
 
 
384
    \snippet qtcontactsdocsample/qtcontactsdocsampleasync.cpp Removing a relationship
 
385
 
 
386
The equivalent code using the synchronous API looks like:
 
387
 
 
388
    \snippet qtcontactsdocsample/qtcontactsdocsample.cpp Synchronously removing a relationship
 
389
 
 
390
Alternatively, when a contact which is involved in a relationship is removed,
 
391
any relationships in which it is involved will be removed also.
 
392
 
 
393
*/