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
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Mobility Components.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qtcontacts.h"
#include "requestexample.h"
#include <QDebug>
#include <QCoreApplication>
#include <QObject>
#include <QTimer>
static void addContact(QContactManager*);
static void callContact(QContactManager*);
static void matchCall(QContactManager*, const QString&);
static void viewSpecificDetail(QContactManager*);
static void viewDetails(QContactManager*);
static void addPlugin(QContactManager*);
static void editView(QContactManager*);
static void loadManager();
static void loadManagerWithParameters();
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
// manager configuration examples
loadManager();
loadManagerWithParameters();
// synchronous API examples
QContactManager* cm = new QContactManager();
addContact(cm);
callContact(cm);
matchCall(cm, "111-222-333"); // unknown number.
matchCall(cm, "12345678"); // alice's number.
viewSpecificDetail(cm);
viewDetails(cm);
addPlugin(cm);
editView(cm);
// asynchronous API example
RequestExample re;
re.setManager(cm);
QTimer::singleShot(10, &re, SLOT(performRequest()));
app.exec();
delete cm;
return 0;
}
//! [Creating a new contact]
void addContact(QContactManager* cm)
{
QContact alice;
/* Set the contact's name */
QContactName aliceName;
aliceName.setFirst("Alice");
aliceName.setLast("Jones");
alice.saveDetail(&aliceName);
QContactDisplayLabel aliceDisplay;
aliceDisplay.setLabel("Ally Jones");
alice.saveDetail(&aliceDisplay);
/* Add a phone number */
QContactPhoneNumber number;
number.setContexts(QContactDetail::ContextHome);
number.setSubTypes(QContactPhoneNumber::SubTypeMobile);
number.setNumber("12345678");
alice.saveDetail(&number);
alice.setPreferredDetail("DialAction", number);
/* Add a second phone number */
QContactPhoneNumber number2;
number2.setContexts(QContactDetail::ContextWork);
number2.setSubTypes(QContactPhoneNumber::SubTypeLandline);
number2.setNumber("555-4444");
alice.saveDetail(&number2);
/* Save the contact */
cm->saveContact(&alice);
}
//! [Creating a new contact]
//! [Calling an existing contact]
void callContact(QContactManager* cm)
{
QList<QContactLocalId> contactIds = cm->contacts();
QContact a = cm->contact(contactIds.first());
/* Get this contact's first phone number */
QContactPhoneNumber phn = a.detail<QContactPhoneNumber>();
if (!phn.isEmpty()) {
// First, we need some way of retrieving the QObject which provides the action.
// This may be through the (previously announced) Qt Service Framework:
//QServiceManager* manager = new QServiceManager();
//QObject* dialer = manager->loadInterface("com.nokia.qt.mobility.contacts.Dialer");
//QContactAbstractAction* dialerImpl = static_cast<QContactAbstractAction*>dialer;
//dialerImpl->performAction("DialActionId", a, phn);
}
}
//! [Calling an existing contact]
//! [Filtering by definition and value]
void matchCall(QContactManager* cm, const QString& incomingCallNbr)
{
QContactDetailFilter phoneFilter;
phoneFilter.setDetailDefinitionName(QContactPhoneNumber::DefinitionName, QContactPhoneNumber::FieldNumber);
phoneFilter.setValue(incomingCallNbr);
phoneFilter.setMatchFlags(Qt::MatchExactly);
QList<QContactLocalId> matchingContacts = cm->contacts(phoneFilter);
if (matchingContacts.size() == 0) {
qDebug() << "Incoming call from unknown contact (" << incomingCallNbr << ")";
} else {
QContact match = cm->contact(matchingContacts.at(0));
QContactDisplayLabel cdl = match.detail(QContactDisplayLabel::DefinitionName);
if (cdl.isEmpty())
cdl.setLabel(cm->synthesizeDisplayLabel(match));
qDebug() << "Incoming call from"
<< cdl.label()
<< "(" << incomingCallNbr << ")";
}
}
//! [Filtering by definition and value]
//! [Viewing a specific detail of a contact]
void viewSpecificDetail(QContactManager* cm)
{
QList<QContactLocalId> contactIds = cm->contacts();
QContact a = cm->contact(contactIds.first());
QContactDisplayLabel cdl = a.detail(QContactDisplayLabel::DefinitionName);
if (cdl.isEmpty())
cdl.setLabel(cm->synthesizeDisplayLabel(a));
qDebug() << "The first phone number of" << cdl.label()
<< "is" << a.detail(QContactPhoneNumber::DefinitionName).value(QContactPhoneNumber::FieldNumber);
}
//! [Viewing a specific detail of a contact]
//! [Viewing the details of a contact]
void viewDetails(QContactManager* cm)
{
QList<QContactLocalId> contactIds = cm->contacts();
QContact a = cm->contact(contactIds.first());
QContactDisplayLabel cdl = a.detail(QContactDisplayLabel::DefinitionName);
if (cdl.isEmpty())
cdl.setLabel(cm->synthesizeDisplayLabel(a));
qDebug() << "Viewing the details of" << cdl.label();
QList<QContactDetail> allDetails = a.details();
for (int i = 0; i < allDetails.size(); i++) {
QContactDetail detail = allDetails.at(i);
QContactDetailDefinition currentDefinition = cm->detailDefinition(detail.definitionName());
QMap<QString, QContactDetailDefinition::Field> fields = currentDefinition.fields();
qDebug("\tDetail #%d (%s):", i, detail.definitionName().toAscii().constData());
foreach (const QString& fieldKey, fields.keys()) {
qDebug() << "\t\t" << fieldKey << "(" << fields.value(fieldKey).dataType << ") =" << detail.value(fieldKey);
}
qDebug();
}
}
//! [Viewing the details of a contact]
//! [Installing a plugin which modifies a definition]
void addPlugin(QContactManager* cm)
{
/* Find the definition that we are modifying */
QMap<QString, QContactDetailDefinition> definitions = cm->detailDefinitions();
QContactDetailDefinition modified = definitions.value(QContactEmailAddress::DefinitionName);
/* Make our modifications: we add a "Label" field to email addresses */
QContactDetailDefinition::Field newField;
newField.dataType = QVariant::String;
QMap<QString, QContactDetailDefinition::Field> fields = modified.fields();
fields.insert("Label", newField);
/* Update the definition with the new field included */
modified.setFields(fields);
/* Save the definition back to the manager */
if (cm->saveDetailDefinition(modified))
qDebug() << "Successfully modified the detail definition!";
else
qDebug() << "This backend could not support our modifications!";
}
//! [Installing a plugin which modifies a definition]
//! [Modifying an existing contact]
void editView(QContactManager* cm)
{
QList<QContactLocalId> contactIds = cm->contacts();
QContact a = cm->contact(contactIds.first());
QContactDisplayLabel cdl = a.detail(QContactDisplayLabel::DefinitionName);
if (cdl.isEmpty())
cdl.setLabel(cm->synthesizeDisplayLabel(a));
qDebug() << "Modifying the details of" << cdl.label();
/* Change the first phone number */
QList<QContactDetail> numbers = a.details(QContactPhoneNumber::DefinitionName);
QContactPhoneNumber phone = numbers.value(0);
phone.setNumber("123-4445");
/* Add an email address */
QContactEmailAddress email;
email.setEmailAddress("alice.jones@example");
email.setContexts(QContactDetail::ContextHome);
email.setValue("Label", "Alice's Work Email Address");
/* Save the updated details to the contact. */
a.saveDetail(&phone);
a.saveDetail(&email);
/* Now we must save the updated contact back to the database. */
cm->saveContact(&a);
viewDetails(cm);
}
//! [Modifying an existing contact]
//! [Asynchronous contact request]
void RequestExample::performRequest()
{
// retrieve any contact whose first name is "Alice"
QContactDetailFilter dfil;
dfil.setDetailDefinitionName(QContactName::DefinitionName, QContactName::FieldFirst);
dfil.setValue("Alice");
dfil.setMatchFlags(Qt::MatchExactly);
m_fetchRequest->setFilter(dfil);
connect(m_fetchRequest, SIGNAL(progress(QContactFetchRequest*,bool)), this, SLOT(printContacts(QContactFetchRequest*,bool)));
if (!m_fetchRequest->start()) {
qDebug() << "Unable to request contacts!";
QCoreApplication::exit(0);
} else {
qDebug() << "Requested contacts; awaiting results...";
}
}
void RequestExample::printContacts(QContactFetchRequest* request, bool appendOnly)
{
QList<QContact> results = request->contacts();
if (appendOnly) {
// we know that the results are still in the same order; just display the new results.
for (m_previousLastIndex += 1; m_previousLastIndex < results.size(); m_previousLastIndex++) {
qDebug() << "Found another Alice:" << results.at(m_previousLastIndex).displayLabel().label();
}
} else {
// the order of results has changed; display them all.
for (m_previousLastIndex = 0; m_previousLastIndex < results.size(); m_previousLastIndex++) {
qDebug() << "Found another Alice:" << results.at(m_previousLastIndex).displayLabel().label();
}
}
// once we've finished retrieving results, stop processing events.
if (request->status() == QContactAbstractRequest::Finished || request->status() == QContactAbstractRequest::Cancelled) {
QCoreApplication::exit(0);
}
}
//! [Asynchronous contact request]
//! [Loading a specific manager backend]
void loadManager()
{
QContactManager* cm = new QContactManager("KABC");
QList<QContactLocalId> contactIds = cm->contacts();
if (!contactIds.isEmpty()) {
QContact a = cm->contact(contactIds.first());
QContactDisplayLabel cdl = a.detail(QContactDisplayLabel::DefinitionName);
if (cdl.isEmpty())
cdl.setLabel(cm->synthesizeDisplayLabel(a));
qDebug() << "This manager contains" << cdl.label();
} else {
qDebug() << "This manager contains no contacts";
}
delete cm;
}
//! [Loading a specific manager backend]
//! [Loading a specific manager backend with parameters]
void loadManagerWithParameters()
{
QMap<QString, QString> parameters;
parameters.insert("Settings", "~/.qcontactmanager-kabc-settings.ini");
QContactManager* cm = new QContactManager("KABC", parameters);
QMap<QString, QContactDetailDefinition> definitions = cm->detailDefinitions();
qDebug() << "This backend currently supports the following detail definitions:";
QList<QContactDetailDefinition> allDefinitions = definitions.values();
foreach (const QContactDetailDefinition& defn, allDefinitions) {
QMap<QString, QContactDetailDefinition::Field> fields = defn.fields();
foreach (const QString& fieldKey, fields.keys()) {
QList<QVariant> allowableValues = fields.value(fieldKey).allowableValues;
qDebug() << "\t" << fieldKey << "(" << fields.value(fieldKey).dataType << "):";
if (allowableValues.isEmpty()) {
qDebug() << "\t\tAny Value Permitted";
} else {
qDebug() << allowableValues;
}
}
}
delete cm;
}
//! [Loading a specific manager backend with parameters]
|