/usr/share/qt5/doc/qtcontacts/contactsusage.html is in qtpim5-doc-html 5.0~git20140515~29475884-0ubuntu24~7.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- contactsusage.qdoc -->
<title>Qt Contacts API Usage | Qt 5.0</title>
<link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
<script type="text/javascript">
document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
// loading style sheet breaks anchors that were jumped to before
// so force jumping to anchor again
setTimeout(function() {
var anchor = location.hash;
// need to jump to different anchor first (e.g. none)
location.hash = "#";
setTimeout(function() {
location.hash = anchor;
}, 0);
}, 0);
</script>
</head>
<body>
<div class="header" id="qtdocheader">
<div class="main">
<div class="main-rounded">
<div class="navigationbar">
<table><tr>
<td >Qt 5.0</td><td >Qt Contacts API Usage</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.0.0 Reference Documentation</td>
</tr></table>
</div>
</div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#retrieving-contact-details">Retrieving Contact Details</a></li>
<li class="level2"><a href="#instantiating-contact-manager">Instantiating Contact Manager</a></li>
<li class="level2"><a href="#retrieving-a-contact-from-the-manager">Retrieving a Contact from the Manager</a></li>
<li class="level2"><a href="#selecting-a-detail">Selecting a Detail</a></li>
<li class="level2"><a href="#retrieving-all-details">Retrieving All Details</a></li>
<li class="level2"><a href="#retrieving-details-of-a-type">Retrieving Details of a Type</a></li>
<li class="level1"><a href="#saving-contacts">Saving Contacts</a></li>
<li class="level1"><a href="#configuring-managers">Configuring Managers</a></li>
<li class="level2"><a href="#loading-the-default-manager-for-the-platform">Loading the Default Manager for the Platform</a></li>
<li class="level2"><a href="#querying-a-manager-for-capabilities">Querying a Manager for Capabilities</a></li>
<li class="level2"><a href="#loading-the-manager-for-a-specific-backend">Loading the Manager for a Specific Backend</a></li>
<li class="level2"><a href="#loading-a-manager-with-specific-parameters">Loading a Manager with Specific Parameters</a></li>
<li class="level1"><a href="#manipulating-contact-details">Manipulating Contact Details</a></li>
<li class="level2"><a href="#adding-details">Adding Details</a></li>
<li class="level2"><a href="#updating-details">Updating Details</a></li>
<li class="level2"><a href="#removing-details">Removing Details</a></li>
<li class="level2"><a href="#viewing-details">Viewing Details</a></li>
<li class="level2"><a href="#viewing-all-details-of-a-contact">Viewing All Details of a Contact</a></li>
<li class="level1"><a href="#persistent-contact-information">Persistent Contact Information</a></li>
<li class="level2"><a href="#creating-contacts">Creating Contacts</a></li>
<li class="level2"><a href="#retrieving-contacts">Retrieving Contacts</a></li>
<li class="level2"><a href="#updating-contacts">Updating Contacts</a></li>
<li class="level2"><a href="#removing-contacts">Removing Contacts</a></li>
<li class="level2"><a href="#creating-relationships">Creating Relationships</a></li>
<li class="level2"><a href="#retrieving-relationships">Retrieving Relationships</a></li>
<li class="level2"><a href="#removing-relationships">Removing Relationships</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Qt Contacts API Usage</h1>
<span class="subtitle"></span>
<!-- $$$contactsusage.html-description -->
<div class="descr"> <a name="details"></a>
<p>With the Qt Contacts API, typical use cases are:</p>
<ul>
<li>Access a list of personal contacts from the contact database supported by the selected backend.</li>
<li>Sort or filter contacts and access them as a list.</li>
<li>Import contacts in vCard format into the selected contacts database.</li>
<li>Export contacts to a vCard format to share elsewhere.</li>
</ul>
<p>This section provides some examples of common usage of the Qt Contacts API.</p>
<a name="retrieving-contact-details"></a>
<h2 id="retrieving-contact-details">Retrieving Contact Details</h2>
<p>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:</p>
<ul>
<li>A contact manager must be instantiated</li>
<li>The contact must be retrieved from the manager</li>
<li>The required details of the contact must be selected from the contact</li>
</ul>
<a name="instantiating-contact-manager"></a>
<h3 >Instantiating Contact Manager</h3>
<p>The first step is usually as simple as:</p>
<pre class="cpp">
<span class="type"><a href="qcontactmanager.html">QContactManager</a></span> cm; <span class="comment">// instantiate the default manager</span>
</pre>
<a name="retrieving-a-contact-from-the-manager"></a>
<h3 >Retrieving a Contact from the Manager</h3>
<p>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 <i>default filter</i> retrieve operation is used. The retrieval operations may either be <a href="contactssync.html">synchronous</a> or <a href="contactsasync.html">asynchronous</a>; 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:</p>
<pre class="cpp">
<span class="type">QList</span><span class="operator"><</span><span class="type"><a href="qcontact.html">QContact</a></span><span class="operator">></span> allContacts <span class="operator">=</span> cm<span class="operator">.</span>contacts();
</pre>
<a name="selecting-a-detail"></a>
<h3 >Selecting a Detail</h3>
<p>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:</p>
<pre class="cpp">
<span class="type"><a href="qcontact.html">QContact</a></span> firstContact <span class="operator">=</span> allContacts<span class="operator">.</span>first();
qDebug() <span class="operator"><</span><span class="operator"><</span> <span class="string">"The first contact has a phone number:"</span> <span class="operator"><</span><span class="operator"><</span> firstContact<span class="operator">.</span>detail<span class="operator"><</span><span class="type"><a href="qcontactphonenumber.html">QContactPhoneNumber</a></span><span class="operator">></span>()<span class="operator">.</span>number();
</pre>
<p>Alternatively, you can use the base <a href="qcontactdetail.html">QContactDetail</a> 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:</p>
<pre class="cpp">
qDebug() <span class="operator"><</span><span class="operator"><</span> <span class="string">"The first contact has a phone number:"</span> <span class="operator"><</span><span class="operator"><</span> firstContact<span class="operator">.</span>detail(<span class="type"><a href="qcontactphonenumber.html">QContactPhoneNumber</a></span><span class="operator">::</span>DefinitionName)<span class="operator">.</span>value(<span class="type"><a href="qcontactphonenumber.html">QContactPhoneNumber</a></span><span class="operator">::</span>FieldNumber);
</pre>
<p>Note that in each case, if the contact did not have a phone number detail, the return value of <a href="qcontact.html#detail-1">QContact::detail</a>() is an empty detail. Also note that in the first case, the return value will be of the <a href="qcontactphonenumber.html">QContactPhoneNumber</a> detail type, whereas in the second case, the return value will be of the <a href="qcontactdetail.html">QContactDetail</a> (base-class detail) type -- although the actual detail returned in both cases is exactly the same.</p>
<a name="retrieving-all-details"></a>
<h3 >Retrieving All Details</h3>
<p>If you wish to retrieve all of the details of a contact, you may do something similar to:</p>
<pre class="cpp">
<span class="type">QList</span><span class="operator"><</span><span class="type"><a href="qcontactdetail.html">QContactDetail</a></span><span class="operator">></span> allDetails <span class="operator">=</span> firstContact<span class="operator">.</span>details();
</pre>
<a name="retrieving-details-of-a-type"></a>
<h3 >Retrieving Details of a Type</h3>
<p>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:</p>
<pre class="cpp">
<span class="type">QList</span><span class="operator"><</span><span class="type"><a href="qcontactphonenumber.html">QContactPhoneNumber</a></span><span class="operator">></span> allPhoneNumbers <span class="operator">=</span> firstContact<span class="operator">.</span>details<span class="operator"><</span><span class="type"><a href="qcontactphonenumber.html">QContactPhoneNumber</a></span><span class="operator">></span>();
<span class="type">QList</span><span class="operator"><</span><span class="type"><a href="qcontactdetail.html">QContactDetail</a></span><span class="operator">></span> allPhoneNumbers2 <span class="operator">=</span> firstContact<span class="operator">.</span>details(<span class="type"><a href="qcontactphonenumber.html">QContactPhoneNumber</a></span><span class="operator">::</span>DefinitionName);
</pre>
<p>Note that in each case, if the contact did not have any phone number details, the return value of <a href="qcontact.html#details-1">QContact::details</a>() is an empty list. Also note that in the first case, the return value will be a list of the <a href="qcontactphonenumber.html">QContactPhoneNumber</a> detail type, whereas in the second case, the return value will be a list of the <a href="qcontactdetail.html">QContactDetail</a> (base-class detail) type -- although the actual details returned in both cases will be exactly the same.</p>
<a name="saving-contacts"></a>
<h2 id="saving-contacts">Saving Contacts</h2>
<p>The next most common use of the API is to save a contact. Such an operation consists of two steps:</p>
<ul>
<li>Saving a detail in a contact</li>
<li>Saving the contact in a manager</li>
</ul>
<p>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.</p>
<pre class="cpp">
<span class="type"><a href="qcontactphonenumber.html">QContactPhoneNumber</a></span> newPhoneNumber; <span class="comment">// create the detail to add</span>
newPhoneNumber<span class="operator">.</span>setNumber(<span class="string">"12345"</span>); <span class="comment">// set the value(s) to save</span>
firstContact<span class="operator">.</span>saveDetail(<span class="operator">&</span>newPhoneNumber); <span class="comment">// save the detail in the contact</span>
cm<span class="operator">.</span>saveContact(<span class="operator">&</span>firstContact); <span class="comment">// save the contact in the manager</span>
cm<span class="operator">.</span>removeContact(firstContact<span class="operator">.</span>id()); <span class="comment">// remove the contact from the manager</span>
</pre>
<p>That's it! For more in-depth discussion of usage of the API, see the sections below.</p>
<a name="configuring-managers"></a>
<h2 id="configuring-managers">Configuring Managers</h2>
<p>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 <a href="qcontactmanager.html">QContactManager</a> instance. Furthermore, some managers can be constructed with parameters which affect the operation of the backend.</p>
<a name="loading-the-default-manager-for-the-platform"></a>
<h3 >Loading the Default Manager for the Platform</h3>
<p>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.</p>
<p>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:</p>
<pre class="cpp">
<span class="type"><a href="qcontactmanager.html">QContactManager</a></span> stackDefaultContactManager;
</pre>
<p>or it can be used explicitly to create a manager on the heap, in which case the client must ensure that it deletes the manager when it is finished with it in order to avoid a memory leak:</p>
<pre class="cpp">
<span class="type"><a href="qcontactmanager.html">QContactManager</a></span> <span class="operator">*</span>heapDefaultContactManager <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qcontactmanager.html">QContactManager</a></span>;
<span class="comment">// ... perform contact manipulation</span>
<span class="keyword">delete</span> heapDefaultContactManager;
</pre>
<a name="querying-a-manager-for-capabilities"></a>
<h3 >Querying a Manager for Capabilities</h3>
<p>Different managers will support different capabilities and details. Clients can use the meta data reporting functions of <a href="qcontactmanager.html">QContactManager</a> to determine what the capabilities of the manager they have instantiated might be.</p>
<pre class="cpp">
<span class="type"><a href="qcontactmanager.html">QContactManager</a></span> cm;
qDebug() <span class="operator"><</span><span class="operator"><</span> <span class="string">"The default manager for the platform is:"</span> <span class="operator"><</span><span class="operator"><</span> cm<span class="operator">.</span>managerName();
qDebug() <span class="operator"><</span><span class="operator"><</span> <span class="string">"It"</span> <span class="operator"><</span><span class="operator"><</span> (cm<span class="operator">.</span>isRelationshipTypeSupported(<span class="type"><a href="qcontactrelationship.html">QContactRelationship</a></span><span class="operator">::</span>HasAssistant()) <span class="operator">?</span> <span class="string">"supports"</span> : <span class="string">"does not support"</span>) <span class="operator"><</span><span class="operator"><</span> <span class="string">"assistant relationships."</span>;
qDebug() <span class="operator"><</span><span class="operator"><</span> <span class="string">"It"</span> <span class="operator"><</span><span class="operator"><</span> (cm<span class="operator">.</span>supportedContactTypes()<span class="operator">.</span>contains(<span class="type">QContactType</span><span class="operator">::</span>TypeGroup) <span class="operator">?</span> <span class="string">"supports"</span> : <span class="string">"does not support"</span>) <span class="operator"><</span><span class="operator"><</span> <span class="string">"groups."</span>;
</pre>
<a name="loading-the-manager-for-a-specific-backend"></a>
<h3 >Loading the Manager for a Specific Backend</h3>
<p>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.</p>
<p>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).</p>
<pre class="cpp">
<span class="type"><a href="qcontactmanager.html">QContactManager</a></span> contactManager(<span class="string">"KABC"</span>);
</pre>
<a name="loading-a-manager-with-specific-parameters"></a>
<h3 >Loading a Manager with Specific Parameters</h3>
<p>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 <i>Settings</i> 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.</p>
<pre class="cpp">
<span class="type">QMap</span><span class="operator"><</span><span class="type">QString</span><span class="operator">,</span> <span class="type">QString</span><span class="operator">></span> parameters;
parameters<span class="operator">.</span>insert(<span class="string">"Settings"</span><span class="operator">,</span> <span class="string">"~/.qcontactmanager-kabc-settings.ini"</span>);
<span class="type"><a href="qcontactmanager.html">QContactManager</a></span> contactManager(<span class="string">"KABC"</span><span class="operator">,</span> parameters);
</pre>
<a name="manipulating-contact-details"></a>
<h2 id="manipulating-contact-details">Manipulating Contact Details</h2>
<p>Once a contact has been created (or retrieved from a manager), the client can retrieve, create, update or delete details from the contact. Since <a href="qcontact.html">QContact</a> and <a href="qcontactdetail.html">QContactDetail</a> are both container (value) classes, the API offered for these operations is purely synchronous.</p>
<p>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.</p>
<a name="adding-details"></a>
<h3 >Adding Details</h3>
<p>The client adds a name and a phone number to a contact.</p>
<pre class="cpp">
<span class="type"><a href="qcontact.html">QContact</a></span> exampleContact;
<span class="type"><a href="qcontactname.html">QContactName</a></span> nameDetail;
nameDetail<span class="operator">.</span>setFirstName(<span class="string">"Adam"</span>);
nameDetail<span class="operator">.</span>setLastName(<span class="string">"Unlikely"</span>);
<span class="type"><a href="qcontactphonenumber.html">QContactPhoneNumber</a></span> phoneNumberDetail;
phoneNumberDetail<span class="operator">.</span>setNumber(<span class="string">"+123 4567"</span>);
exampleContact<span class="operator">.</span>saveDetail(<span class="operator">&</span>nameDetail);
exampleContact<span class="operator">.</span>saveDetail(<span class="operator">&</span>phoneNumberDetail);
</pre>
<a name="updating-details"></a>
<h3 >Updating Details</h3>
<p>The client updates the phone number of a contact.</p>
<pre class="cpp">
phoneNumberDetail<span class="operator">.</span>setNumber(<span class="string">"+123 9876"</span>);
exampleContact<span class="operator">.</span>saveDetail(<span class="operator">&</span>phoneNumberDetail); <span class="comment">// overwrites old value on save</span>
</pre>
<a name="removing-details"></a>
<h3 >Removing Details</h3>
<p>The client removes the phone number of a contact.</p>
<pre class="cpp">
exampleContact<span class="operator">.</span>removeDetail(<span class="operator">&</span>phoneNumberDetail);
</pre>
<a name="viewing-details"></a>
<h3 >Viewing Details</h3>
<p>The client retrieves and displays the first phone number of a contact.</p>
<pre class="cpp">
<span class="type">void</span> viewSpecificDetail(<span class="type"><a href="qcontactmanager.html">QContactManager</a></span><span class="operator">*</span> cm)
{
<span class="type">QList</span><span class="operator"><</span><span class="type"><a href="qcontactid.html">QContactId</a></span><span class="operator">></span> contactIds <span class="operator">=</span> cm<span class="operator">-</span><span class="operator">></span>contactIds();
<span class="type"><a href="qcontact.html">QContact</a></span> exampleContact <span class="operator">=</span> cm<span class="operator">-</span><span class="operator">></span>contact(contactIds<span class="operator">.</span>first());
qDebug() <span class="operator"><</span><span class="operator"><</span> <span class="string">"The first phone number is"</span> <span class="operator"><</span><span class="operator"><</span> exampleContact<span class="operator">.</span>detail(<span class="type"><a href="qcontactphonenumber.html">QContactPhoneNumber</a></span><span class="operator">::</span>Type)<span class="operator">.</span>value(<span class="type"><a href="qcontactphonenumber.html">QContactPhoneNumber</a></span><span class="operator">::</span>FieldNumber);
}
</pre>
<a name="viewing-all-details-of-a-contact"></a>
<h3 >Viewing All Details of a Contact</h3>
<p>The client retrieves all of the details of a contact, and displays them.</p>
<pre class="cpp">
<span class="type">void</span> viewDetails(<span class="type"><a href="qcontactmanager.html">QContactManager</a></span><span class="operator">*</span> cm)
{
<span class="type">QList</span><span class="operator"><</span><span class="type"><a href="qcontactid.html">QContactId</a></span><span class="operator">></span> contactIds <span class="operator">=</span> cm<span class="operator">-</span><span class="operator">></span>contactIds();
<span class="type"><a href="qcontact.html">QContact</a></span> exampleContact <span class="operator">=</span> cm<span class="operator">-</span><span class="operator">></span>contact(contactIds<span class="operator">.</span>first());
<span class="type">QList</span><span class="operator"><</span><span class="type"><a href="qcontactdetail.html">QContactDetail</a></span><span class="operator">></span> allDetails <span class="operator">=</span> exampleContact<span class="operator">.</span>details();
<span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator"><</span> allDetails<span class="operator">.</span>size(); i<span class="operator">+</span><span class="operator">+</span>) {
<span class="type"><a href="qcontactdetail.html">QContactDetail</a></span> detail <span class="operator">=</span> allDetails<span class="operator">.</span>at(i);
<span class="type">QMap</span><span class="operator"><</span><span class="type">int</span><span class="operator">,</span> <span class="type">QVariant</span><span class="operator">></span> fields <span class="operator">=</span> detail<span class="operator">.</span>values();
qDebug(<span class="string">"\tDetail #%d (%d):"</span><span class="operator">,</span> i<span class="operator">,</span> detail<span class="operator">.</span>type());
foreach (<span class="keyword">const</span> <span class="type">int</span><span class="operator">&</span> fieldKey<span class="operator">,</span> fields<span class="operator">.</span>keys()) {
qDebug() <span class="operator"><</span><span class="operator"><</span> <span class="string">"\t\t"</span> <span class="operator"><</span><span class="operator"><</span> fieldKey <span class="operator"><</span><span class="operator"><</span> <span class="string">"("</span> <span class="operator"><</span><span class="operator"><</span> fields<span class="operator">.</span>value(fieldKey) <span class="operator"><</span><span class="operator"><</span> <span class="string">") ="</span> <span class="operator"><</span><span class="operator"><</span> detail<span class="operator">.</span>value(fieldKey);
}
qDebug();
}
}
</pre>
<p><b>Note: </b>Details are implicitly shared objects with particular semantics surrounding saving, removal and modification. The following example demonstrates these semantics.</p><pre class="cpp">
<span class="type">void</span> detailSharing(<span class="type"><a href="qcontactmanager.html">QContactManager</a></span><span class="operator">*</span> cm)
{
<span class="type">QList</span><span class="operator"><</span><span class="type"><a href="qcontactid.html">QContactId</a></span><span class="operator">></span> contactIds <span class="operator">=</span> cm<span class="operator">-</span><span class="operator">></span>contactIds();
<span class="type"><a href="qcontact.html">QContact</a></span> a <span class="operator">=</span> cm<span class="operator">-</span><span class="operator">></span>contact(contactIds<span class="operator">.</span>first());
<span class="comment">/* Create a new phone number detail. */</span>
<span class="type"><a href="qcontactphonenumber.html">QContactPhoneNumber</a></span> newNumber;
newNumber<span class="operator">.</span>setNumber(<span class="string">"123123123"</span>);
qDebug() <span class="operator"><</span><span class="operator"><</span> <span class="string">"\tThe new phone number is"</span> <span class="operator"><</span><span class="operator"><</span> newNumber<span class="operator">.</span>number();
<span class="comment">/*
* Create a copy of that detail. These will be implicitly shared;
* changes to nnCopy will not affect newNumber, and vice versa.
* However, attempting to save them will cause overwrite to occur.
* Removal is done purely via key() checking, also.
*/</span>
<span class="type"><a href="qcontactphonenumber.html">QContactPhoneNumber</a></span> nnCopy(newNumber);
nnCopy<span class="operator">.</span>setNumber(<span class="string">"456456456"</span>);
qDebug() <span class="operator"><</span><span class="operator"><</span> <span class="string">"\tThat number is still"</span> <span class="operator"><</span><span class="operator"><</span> newNumber<span class="operator">.</span>number() <span class="operator"><</span><span class="operator"><</span> <span class="string">", the copy is"</span> <span class="operator"><</span><span class="operator"><</span> nnCopy<span class="operator">.</span>number();
<span class="comment">/* Save the detail in the contact, then remove via the copy, then resave. */</span>
a<span class="operator">.</span>saveDetail(<span class="operator">&</span>newNumber);
a<span class="operator">.</span>removeDetail(<span class="operator">&</span>nnCopy); <span class="comment">// identical to a.removeDetail(&newNumber);</span>
a<span class="operator">.</span>saveDetail(<span class="operator">&</span>newNumber); <span class="comment">// since newNumber.key() == nnCopy.key();</span>
<span class="comment">/* Saving will cause overwrite */</span>
qDebug() <span class="operator"><</span><span class="operator"><</span> <span class="string">"\tPrior to saving nnCopy has"</span> <span class="operator"><</span><span class="operator"><</span> a<span class="operator">.</span>details()<span class="operator">.</span>count() <span class="operator"><</span><span class="operator"><</span> <span class="string">"details."</span>;
a<span class="operator">.</span>saveDetail(<span class="operator">&</span>nnCopy);
qDebug() <span class="operator"><</span><span class="operator"><</span> <span class="string">"\tAfter saving nnCopy still has"</span> <span class="operator"><</span><span class="operator"><</span> a<span class="operator">.</span>details()<span class="operator">.</span>count() <span class="operator"><</span><span class="operator"><</span> <span class="string">"details."</span>;
<span class="comment">/* In order to save nnCopy as a new detail, we must reset its key */</span>
nnCopy<span class="operator">.</span>resetKey();
qDebug() <span class="operator"><</span><span class="operator"><</span> <span class="string">"\tThe copy key is now"</span> <span class="operator"><</span><span class="operator"><</span> nnCopy<span class="operator">.</span>key() <span class="operator"><</span><span class="operator"><</span> <span class="string">", whereas the original key is"</span> <span class="operator"><</span><span class="operator"><</span> newNumber<span class="operator">.</span>key();
qDebug() <span class="operator"><</span><span class="operator"><</span> <span class="string">"\tPrior to saving (key reset) nnCopy has"</span> <span class="operator"><</span><span class="operator"><</span> a<span class="operator">.</span>details()<span class="operator">.</span>count() <span class="operator"><</span><span class="operator"><</span> <span class="string">"details."</span>;
a<span class="operator">.</span>saveDetail(<span class="operator">&</span>nnCopy);
qDebug() <span class="operator"><</span><span class="operator"><</span> <span class="string">"\tAfter saving (key reset) nnCopy still has"</span> <span class="operator"><</span><span class="operator"><</span> a<span class="operator">.</span>details()<span class="operator">.</span>count() <span class="operator"><</span><span class="operator"><</span> <span class="string">"details."</span>;
a<span class="operator">.</span>removeDetail(<span class="operator">&</span>nnCopy);
<span class="comment">/*
* Note that changes made to details are not
* propagated automatically to the contact.
* To persist changes to a detail, you must call saveDetail().
*/</span>
<span class="type">QList</span><span class="operator"><</span><span class="type"><a href="qcontactphonenumber.html">QContactPhoneNumber</a></span><span class="operator">></span> allNumbers <span class="operator">=</span> a<span class="operator">.</span>details<span class="operator"><</span><span class="type"><a href="qcontactphonenumber.html">QContactPhoneNumber</a></span><span class="operator">></span>();
foreach (<span class="keyword">const</span> <span class="type"><a href="qcontactphonenumber.html">QContactPhoneNumber</a></span><span class="operator">&</span> savedPhn<span class="operator">,</span> allNumbers) {
<span class="keyword">if</span> (savedPhn<span class="operator">.</span>key() <span class="operator">!</span><span class="operator">=</span> newNumber<span class="operator">.</span>key()) {
<span class="keyword">continue</span>;
}
<span class="comment">/*
* This phone number is the saved copy of the newNumber detail.
* It is detached from the newNumber detail, so changes to newNumber
* shouldn't affect savedPhn until saveDetail() is called again.
*/</span>
qDebug() <span class="operator"><</span><span class="operator"><</span> <span class="string">"\tCurrently, the (stack) newNumber is"</span> <span class="operator"><</span><span class="operator"><</span> newNumber<span class="operator">.</span>number()
<span class="operator"><</span><span class="operator"><</span> <span class="string">", and the saved newNumber is"</span> <span class="operator"><</span><span class="operator"><</span> savedPhn<span class="operator">.</span>number();
newNumber<span class="operator">.</span>setNumber(<span class="string">"678678678"</span>);
qDebug() <span class="operator"><</span><span class="operator"><</span> <span class="string">"\tNow, the (stack) newNumber is"</span> <span class="operator"><</span><span class="operator"><</span> newNumber<span class="operator">.</span>number()
<span class="operator"><</span><span class="operator"><</span> <span class="string">", but the saved newNumber is"</span> <span class="operator"><</span><span class="operator"><</span> savedPhn<span class="operator">.</span>number();
}
<span class="comment">/*
* Removal of the detail depends only on the key of the detail; the fact
* that the values differ is not taken into account by the remove operation.
*/</span>
a<span class="operator">.</span>removeDetail(<span class="operator">&</span>newNumber) <span class="operator">?</span> qDebug() <span class="operator"><</span><span class="operator"><</span> <span class="string">"\tSucceeded in removing the temporary detail."</span>
: qDebug() <span class="operator"><</span><span class="operator"><</span> <span class="string">"\tFailed to remove the temporary detail.\n"</span>;
}
</pre>
<a name="persistent-contact-information"></a>
<h2 id="persistent-contact-information">Persistent Contact Information</h2>
<p>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).</p>
<p>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:</p>
<pre class="cpp">
QTCONTACTS_USE_NAMESPACE
<span class="keyword">class</span> AsyncRequestExample : <span class="keyword">public</span> <span class="type">QObject</span>
{
Q_OBJECT
<span class="keyword">public</span>:
AsyncRequestExample();
<span class="operator">~</span>AsyncRequestExample();
<span class="keyword">public</span> <span class="keyword">slots</span>:
<span class="type">void</span> performRequests();
<span class="keyword">private</span> <span class="keyword">slots</span>:
<span class="type">void</span> contactFetchRequestStateChanged(<span class="type"><a href="qcontactabstractrequest.html">QContactAbstractRequest</a></span><span class="operator">::</span>State newState);
<span class="type">void</span> contactSaveRequestStateChanged(<span class="type"><a href="qcontactabstractrequest.html">QContactAbstractRequest</a></span><span class="operator">::</span>State newState);
<span class="type">void</span> contactRemoveRequestStateChanged(<span class="type"><a href="qcontactabstractrequest.html">QContactAbstractRequest</a></span><span class="operator">::</span>State newState);
<span class="type">void</span> relationshipFetchRequestStateChanged(<span class="type"><a href="qcontactabstractrequest.html">QContactAbstractRequest</a></span><span class="operator">::</span>State newState);
<span class="type">void</span> relationshipSaveRequestStateChanged(<span class="type"><a href="qcontactabstractrequest.html">QContactAbstractRequest</a></span><span class="operator">::</span>State newState);
<span class="type">void</span> relationshipRemoveRequestStateChanged(<span class="type"><a href="qcontactabstractrequest.html">QContactAbstractRequest</a></span><span class="operator">::</span>State newState);
<span class="keyword">private</span>:
<span class="type"><a href="qcontactmanager.html">QContactManager</a></span> <span class="operator">*</span>m_manager;
<span class="type"><a href="qcontactfetchrequest.html">QContactFetchRequest</a></span> m_contactFetchRequest;
<span class="type"><a href="qcontactsaverequest.html">QContactSaveRequest</a></span> m_contactSaveRequest;
<span class="type"><a href="qcontactremoverequest.html">QContactRemoveRequest</a></span> m_contactRemoveRequest;
<span class="type"><a href="qcontactrelationshipfetchrequest.html">QContactRelationshipFetchRequest</a></span> m_relationshipFetchRequest;
<span class="type"><a href="qcontactrelationshipsaverequest.html">QContactRelationshipSaveRequest</a></span> m_relationshipSaveRequest;
<span class="type"><a href="qcontactrelationshipremoverequest.html">QContactRelationshipRemoveRequest</a></span> m_relationshipRemoveRequest;
};
</pre>
<p>This allows them to define slots which deal with the data as required when the state of the request changes:</p>
<pre class="cpp">
<span class="type">void</span> AsyncRequestExample<span class="operator">::</span>contactFetchRequestStateChanged(<span class="type"><a href="qcontactabstractrequest.html">QContactAbstractRequest</a></span><span class="operator">::</span>State newState)
{
<span class="keyword">if</span> (newState <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qcontactabstractrequest.html">QContactAbstractRequest</a></span><span class="operator">::</span>FinishedState) {
<span class="type"><a href="qcontactfetchrequest.html">QContactFetchRequest</a></span> <span class="operator">*</span>request <span class="operator">=</span> qobject_cast<span class="operator"><</span><span class="type"><a href="qcontactfetchrequest.html">QContactFetchRequest</a></span><span class="operator">*</span><span class="operator">></span>(<span class="type">QObject</span><span class="operator">::</span>sender());
<span class="keyword">if</span> (request<span class="operator">-</span><span class="operator">></span>error() <span class="operator">!</span><span class="operator">=</span> <span class="type"><a href="qcontactmanager.html">QContactManager</a></span><span class="operator">::</span>NoError) {
qDebug() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Error"</span> <span class="operator"><</span><span class="operator"><</span> request<span class="operator">-</span><span class="operator">></span>error() <span class="operator"><</span><span class="operator"><</span> <span class="string">"occurred during fetch request!"</span>;
<span class="keyword">return</span>;
}
<span class="type">QList</span><span class="operator"><</span><span class="type"><a href="qcontact.html">QContact</a></span><span class="operator">></span> results <span class="operator">=</span> request<span class="operator">-</span><span class="operator">></span>contacts();
<span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator"><</span> results<span class="operator">.</span>size(); i<span class="operator">+</span><span class="operator">+</span>) {
qDebug() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Retrieved contact:"</span> <span class="operator"><</span><span class="operator"><</span> results<span class="operator">.</span>at(i);
}
} <span class="keyword">else</span> <span class="keyword">if</span> (newState <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qcontactabstractrequest.html">QContactAbstractRequest</a></span><span class="operator">::</span>CanceledState) {
qDebug() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Fetch operation canceled!"</span>;
}
}
</pre>
<p>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 <code>FinishedState</code>, for example), the client should instead connect the <a href="qcontactabstractrequest.html#resultsAvailable">QContactAbstractRequest::resultsAvailable</a>() signal to the slot which deals with the results.</p>
<a name="creating-contacts"></a>
<h3 >Creating Contacts</h3>
<p>The client creates a new contact and saves it in a manager.</p>
<pre class="cpp">
<span class="type"><a href="qcontact.html">QContact</a></span> exampleContact;
<span class="type"><a href="qcontactname.html">QContactName</a></span> nameDetail;
nameDetail<span class="operator">.</span>setFirstName(<span class="string">"Adam"</span>);
nameDetail<span class="operator">.</span>setLastName(<span class="string">"Unlikely"</span>);
<span class="type"><a href="qcontactphonenumber.html">QContactPhoneNumber</a></span> phoneNumberDetail;
phoneNumberDetail<span class="operator">.</span>setNumber(<span class="string">"+123 4567"</span>);
exampleContact<span class="operator">.</span>saveDetail(<span class="operator">&</span>nameDetail);
exampleContact<span class="operator">.</span>saveDetail(<span class="operator">&</span>phoneNumberDetail);
<span class="comment">// save the newly created contact in the manager</span>
connect(<span class="operator">&</span>m_contactSaveRequest<span class="operator">,</span> SIGNAL(stateChanged(<span class="type"><a href="qcontactabstractrequest.html">QContactAbstractRequest</a></span><span class="operator">::</span>State))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(contactSaveRequestStateChanged(<span class="type"><a href="qcontactabstractrequest.html">QContactAbstractRequest</a></span><span class="operator">::</span>State)));
m_contactSaveRequest<span class="operator">.</span>setManager(m_manager);
m_contactSaveRequest<span class="operator">.</span>setContacts(<span class="type">QList</span><span class="operator"><</span><span class="type"><a href="qcontact.html">QContact</a></span><span class="operator">></span>() <span class="operator"><</span><span class="operator"><</span> exampleContact);
m_contactSaveRequest<span class="operator">.</span>start();
</pre>
<p>Alternatively, the client can explicitly block execution until the request is complete, by doing something like:</p>
<pre class="cpp">
m_contactSaveRequest<span class="operator">.</span>setManager(m_manager);
m_contactSaveRequest<span class="operator">.</span>setContacts(<span class="type">QList</span><span class="operator"><</span><span class="type"><a href="qcontact.html">QContact</a></span><span class="operator">></span>() <span class="operator"><</span><span class="operator"><</span> exampleContact);
m_contactSaveRequest<span class="operator">.</span>start();
m_contactSaveRequest<span class="operator">.</span>waitForFinished();
<span class="type">QList</span><span class="operator"><</span><span class="type"><a href="qcontact.html">QContact</a></span><span class="operator">></span> savedContacts <span class="operator">=</span> m_contactSaveRequest<span class="operator">.</span>contacts();
</pre>
<p>The equivalent code using the synchronous API looks like:</p>
<pre class="cpp">
<span class="type"><a href="qcontact.html">QContact</a></span> exampleContact;
<span class="type"><a href="qcontactname.html">QContactName</a></span> nameDetail;
nameDetail<span class="operator">.</span>setFirstName(<span class="string">"Adam"</span>);
nameDetail<span class="operator">.</span>setLastName(<span class="string">"Unlikely"</span>);
<span class="type"><a href="qcontactphonenumber.html">QContactPhoneNumber</a></span> phoneNumberDetail;
phoneNumberDetail<span class="operator">.</span>setNumber(<span class="string">"+123 4567"</span>);
exampleContact<span class="operator">.</span>saveDetail(<span class="operator">&</span>nameDetail);
exampleContact<span class="operator">.</span>saveDetail(<span class="operator">&</span>phoneNumberDetail);
<span class="comment">// save the newly created contact in the manager</span>
<span class="keyword">if</span> (<span class="operator">!</span>m_manager<span class="operator">.</span>saveContact(<span class="operator">&</span>exampleContact))
qDebug() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Error"</span> <span class="operator"><</span><span class="operator"><</span> m_manager<span class="operator">.</span>error() <span class="operator"><</span><span class="operator"><</span> <span class="string">"occurred whilst saving contact!"</span>;
</pre>
<a name="retrieving-contacts"></a>
<h3 >Retrieving Contacts</h3>
<p>The client requests all contacts from the manager which match a particular filter.</p>
<pre class="cpp">
connect(<span class="operator">&</span>m_contactFetchRequest<span class="operator">,</span> SIGNAL(stateChanged(<span class="type"><a href="qcontactabstractrequest.html">QContactAbstractRequest</a></span><span class="operator">::</span>State))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(contactFetchRequestStateChanged(<span class="type"><a href="qcontactabstractrequest.html">QContactAbstractRequest</a></span><span class="operator">::</span>State)));
m_contactFetchRequest<span class="operator">.</span>setManager(m_manager);
m_contactFetchRequest<span class="operator">.</span>setFilter(<span class="type"><a href="qcontactphonenumber.html">QContactPhoneNumber</a></span><span class="operator">::</span>match(<span class="string">"+123 4567"</span>));
m_contactFetchRequest<span class="operator">.</span>start();
</pre>
<p>The equivalent code using the synchronous API looks like:</p>
<pre class="cpp">
<span class="type">QList</span><span class="operator"><</span><span class="type"><a href="qcontact.html">QContact</a></span><span class="operator">></span> results <span class="operator">=</span> m_manager<span class="operator">.</span>contacts(<span class="type"><a href="qcontactphonenumber.html">QContactPhoneNumber</a></span><span class="operator">::</span>match(<span class="string">"+123 4567"</span>));
</pre>
<p>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:</p>
<pre class="cpp">
<span class="type"><a href="qcontactidfilter.html">QContactIdFilter</a></span> idListFilter;
idListFilter<span class="operator">.</span>setIds(<span class="type">QList</span><span class="operator"><</span><span class="type"><a href="qcontactid.html">QContactId</a></span><span class="operator">></span>() <span class="operator"><</span><span class="operator"><</span> exampleContact<span class="operator">.</span>id());
m_contactFetchRequest<span class="operator">.</span>setManager(m_manager);
m_contactFetchRequest<span class="operator">.</span>setFilter(idListFilter);
m_contactFetchRequest<span class="operator">.</span>start();
</pre>
<p>The synchronous API provides a function specifically for this purpose:</p>
<pre class="cpp">
<span class="type"><a href="qcontact.html">QContact</a></span> existing <span class="operator">=</span> m_manager<span class="operator">.</span>contact(exampleContact<span class="operator">.</span>id());
</pre>
<a name="updating-contacts"></a>
<h3 >Updating Contacts</h3>
<p>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.</p>
<pre class="cpp">
phoneNumberDetail<span class="operator">.</span>setNumber(<span class="string">"+123 9876"</span>);
exampleContact<span class="operator">.</span>saveDetail(<span class="operator">&</span>phoneNumberDetail);
m_contactSaveRequest<span class="operator">.</span>setManager(m_manager);
m_contactSaveRequest<span class="operator">.</span>setContacts(<span class="type">QList</span><span class="operator"><</span><span class="type"><a href="qcontact.html">QContact</a></span><span class="operator">></span>() <span class="operator"><</span><span class="operator"><</span> exampleContact);
m_contactSaveRequest<span class="operator">.</span>start();
</pre>
<p>The equivalent code using the synchronous API looks like:</p>
<pre class="cpp">
phoneNumberDetail<span class="operator">.</span>setNumber(<span class="string">"+123 9876"</span>);
exampleContact<span class="operator">.</span>saveDetail(<span class="operator">&</span>phoneNumberDetail);
m_manager<span class="operator">.</span>saveContact(<span class="operator">&</span>exampleContact);
</pre>
<a name="removing-contacts"></a>
<h3 >Removing Contacts</h3>
<p>The client removes a contact from the manager by specifying its id.</p>
<pre class="cpp">
connect(<span class="operator">&</span>m_contactRemoveRequest<span class="operator">,</span> SIGNAL(stateChanged(<span class="type"><a href="qcontactabstractrequest.html">QContactAbstractRequest</a></span><span class="operator">::</span>State))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(contactRemoveRequestStateChanged(<span class="type"><a href="qcontactabstractrequest.html">QContactAbstractRequest</a></span><span class="operator">::</span>State)));
m_contactRemoveRequest<span class="operator">.</span>setManager(m_manager);
m_contactRemoveRequest<span class="operator">.</span>setContactIds(<span class="type">QList</span><span class="operator"><</span><span class="type"><a href="qcontactid.html">QContactId</a></span><span class="operator">></span>() <span class="operator"><</span><span class="operator"><</span> exampleContact<span class="operator">.</span>id());
m_contactRemoveRequest<span class="operator">.</span>start();
</pre>
<p>The equivalent code using the synchronous API looks like:</p>
<pre class="cpp">
m_manager<span class="operator">.</span>removeContact(exampleContact<span class="operator">.</span>id());
</pre>
<a name="creating-relationships"></a>
<h3 >Creating Relationships</h3>
<p>The client specifies a relationship between two contacts stored in the manager</p>
<pre class="cpp">
<span class="comment">// first, create the group and the group member</span>
<span class="type"><a href="qcontact.html">QContact</a></span> exampleGroup;
exampleGroup<span class="operator">.</span>setType(<span class="type">QContactType</span><span class="operator">::</span>TypeGroup);
<span class="type">QContactNickname</span> groupName;
groupName<span class="operator">.</span>setNickname(<span class="string">"Example Group"</span>);
exampleGroup<span class="operator">.</span>saveDetail(<span class="operator">&</span>groupName);
<span class="type"><a href="qcontact.html">QContact</a></span> exampleGroupMember;
<span class="type"><a href="qcontactname.html">QContactName</a></span> groupMemberName;
groupMemberName<span class="operator">.</span>setFirstName(<span class="string">"Member"</span>);
exampleGroupMember<span class="operator">.</span>saveDetail(<span class="operator">&</span>groupMemberName);
<span class="comment">// second, save those contacts in the manager</span>
<span class="type">QList</span><span class="operator"><</span><span class="type"><a href="qcontact.html">QContact</a></span><span class="operator">></span> saveList;
saveList <span class="operator"><</span><span class="operator"><</span> exampleGroup <span class="operator"><</span><span class="operator"><</span> exampleGroupMember;
m_contactSaveRequest<span class="operator">.</span>setContacts(saveList);
m_contactSaveRequest<span class="operator">.</span>start();
m_contactSaveRequest<span class="operator">.</span>waitForFinished();
<span class="comment">// third, create the relationship between those contacts</span>
<span class="type"><a href="qcontactrelationship.html">QContactRelationship</a></span> groupRelationship;
groupRelationship<span class="operator">.</span>setFirst(exampleGroup);
groupRelationship<span class="operator">.</span>setRelationshipType(<span class="type"><a href="qcontactrelationship.html">QContactRelationship</a></span><span class="operator">::</span>HasMember());
groupRelationship<span class="operator">.</span>setSecond(exampleGroupMember);
<span class="comment">// finally, save the relationship in the manager</span>
connect(<span class="operator">&</span>m_relationshipSaveRequest<span class="operator">,</span> SIGNAL(stateChanged(<span class="type"><a href="qcontactabstractrequest.html">QContactAbstractRequest</a></span><span class="operator">::</span>State))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(relationshipSaveRequestStateChanged(<span class="type"><a href="qcontactabstractrequest.html">QContactAbstractRequest</a></span><span class="operator">::</span>State)));
m_relationshipSaveRequest<span class="operator">.</span>setManager(m_manager);
m_relationshipSaveRequest<span class="operator">.</span>setRelationships(<span class="type">QList</span><span class="operator"><</span><span class="type"><a href="qcontactrelationship.html">QContactRelationship</a></span><span class="operator">></span>() <span class="operator"><</span><span class="operator"><</span> groupRelationship);
m_relationshipSaveRequest<span class="operator">.</span>start();
</pre>
<p>The equivalent code using the synchronous API looks like:</p>
<pre class="cpp">
<span class="comment">// first, create the group and the group member</span>
<span class="type"><a href="qcontact.html">QContact</a></span> exampleGroup;
exampleGroup<span class="operator">.</span>setType(<span class="type">QContactType</span><span class="operator">::</span>TypeGroup);
<span class="type">QContactNickname</span> groupName;
groupName<span class="operator">.</span>setNickname(<span class="string">"Example Group"</span>);
exampleGroup<span class="operator">.</span>saveDetail(<span class="operator">&</span>groupName);
<span class="type"><a href="qcontact.html">QContact</a></span> exampleGroupMember;
<span class="type"><a href="qcontactname.html">QContactName</a></span> groupMemberName;
groupMemberName<span class="operator">.</span>setFirstName(<span class="string">"Member"</span>);
exampleGroupMember<span class="operator">.</span>saveDetail(<span class="operator">&</span>groupMemberName);
<span class="comment">// second, save those contacts in the manager</span>
<span class="type">QMap</span><span class="operator"><</span><span class="type">int</span><span class="operator">,</span> <span class="type"><a href="qcontactmanager.html">QContactManager</a></span><span class="operator">::</span>Error<span class="operator">></span> errorMap;
<span class="type">QList</span><span class="operator"><</span><span class="type"><a href="qcontact.html">QContact</a></span><span class="operator">></span> saveList;
saveList <span class="operator"><</span><span class="operator"><</span> exampleGroup <span class="operator"><</span><span class="operator"><</span> exampleGroupMember;
m_manager<span class="operator">.</span>saveContacts(<span class="operator">&</span>saveList<span class="operator">,</span> <span class="operator">&</span>errorMap);
<span class="comment">// third, create the relationship between those contacts</span>
<span class="type"><a href="qcontactrelationship.html">QContactRelationship</a></span> groupRelationship;
groupRelationship<span class="operator">.</span>setFirst(exampleGroup);
groupRelationship<span class="operator">.</span>setRelationshipType(<span class="type"><a href="qcontactrelationship.html">QContactRelationship</a></span><span class="operator">::</span>HasMember());
groupRelationship<span class="operator">.</span>setSecond(exampleGroupMember);
<span class="comment">// finally, save the relationship in the manager</span>
m_manager<span class="operator">.</span>saveRelationship(<span class="operator">&</span>groupRelationship);
</pre>
<a name="retrieving-relationships"></a>
<h3 >Retrieving Relationships</h3>
<p>The client requests the relationships that a particular contact is involved in from the manager in which the contact is stored.</p>
<pre class="cpp">
connect(<span class="operator">&</span>m_relationshipFetchRequest<span class="operator">,</span> SIGNAL(stateChanged(<span class="type"><a href="qcontactabstractrequest.html">QContactAbstractRequest</a></span><span class="operator">::</span>State))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(relationshipFetchRequestStateChanged(<span class="type"><a href="qcontactabstractrequest.html">QContactAbstractRequest</a></span><span class="operator">::</span>State)));
m_relationshipFetchRequest<span class="operator">.</span>setManager(m_manager);
<span class="comment">// retrieve the list of relationships between the example group contact and the example member contact</span>
<span class="comment">// where the group contact is the first contact in the relationship, and the member contact is the</span>
<span class="comment">// second contact in the relationship. In order to fetch all relationships between them, another</span>
<span class="comment">// relationship fetch must be performed with their roles reversed, and the results added together.</span>
m_relationshipFetchRequest<span class="operator">.</span>setFirst(exampleGroup);
m_relationshipFetchRequest<span class="operator">.</span>setSecond(exampleGroupMember);
m_relationshipFetchRequest<span class="operator">.</span>start();
</pre>
<p>The equivalent code using the synchronous API looks like:</p>
<pre class="cpp">
<span class="type">QList</span><span class="operator"><</span><span class="type"><a href="qcontactrelationship.html">QContactRelationship</a></span><span class="operator">></span> groupRelationships <span class="operator">=</span> m_manager<span class="operator">.</span>relationships(exampleGroup<span class="operator">,</span> <span class="type"><a href="qcontactrelationship.html">QContactRelationship</a></span><span class="operator">::</span>First);
<span class="type">QList</span><span class="operator"><</span><span class="type"><a href="qcontactrelationship.html">QContactRelationship</a></span><span class="operator">></span> result;
<span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator"><</span> groupRelationships<span class="operator">.</span>size(); i<span class="operator">+</span><span class="operator">+</span>) {
<span class="keyword">if</span> (groupRelationships<span class="operator">.</span>at(i)<span class="operator">.</span>second() <span class="operator">=</span><span class="operator">=</span> exampleGroupMember) {
result<span class="operator">.</span>append(groupRelationships<span class="operator">.</span>at(i));
}
}
</pre>
<p>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.</p>
<pre class="cpp">
exampleGroup <span class="operator">=</span> m_manager<span class="operator">.</span>contact(exampleGroup<span class="operator">.</span>id()); <span class="comment">// refresh the group contact</span>
groupRelationships <span class="operator">=</span> exampleGroup<span class="operator">.</span>relationships(<span class="type"><a href="qcontactrelationship.html">QContactRelationship</a></span><span class="operator">::</span>HasMember());
<span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator"><</span> groupRelationships<span class="operator">.</span>size(); i<span class="operator">+</span><span class="operator">+</span>) {
<span class="keyword">if</span> (groupRelationships<span class="operator">.</span>at(i)<span class="operator">.</span>second() <span class="operator">=</span><span class="operator">=</span> exampleGroupMember) {
result<span class="operator">.</span>append(groupRelationships<span class="operator">.</span>at(i));
}
}
</pre>
<p>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 <code>HasMember</code> type (groups):</p>
<pre class="cpp">
<span class="type"><a href="qcontactfetchhint.html">QContactFetchHint</a></span> hasMemberRelationshipsOnly;
hasMemberRelationshipsOnly<span class="operator">.</span>setRelationshipTypesHint(<span class="type">QStringList</span>(<span class="type"><a href="qcontactrelationship.html">QContactRelationship</a></span><span class="operator">::</span>HasMember()));
m_contactFetchRequest<span class="operator">.</span>setManager(m_manager);
m_contactFetchRequest<span class="operator">.</span>setFilter(<span class="type"><a href="qcontactfilter.html">QContactFilter</a></span>()); <span class="comment">// all contacts</span>
m_contactFetchRequest<span class="operator">.</span>setFetchHint(hasMemberRelationshipsOnly);
m_contactFetchRequest<span class="operator">.</span>start();
</pre>
<p>The equivalent code using the synchronous API looks like:</p>
<pre class="cpp">
<span class="type"><a href="qcontactfetchhint.html">QContactFetchHint</a></span> hasMemberRelationshipsOnly;
hasMemberRelationshipsOnly<span class="operator">.</span>setRelationshipTypesHint(<span class="type">QStringList</span>(<span class="type"><a href="qcontactrelationship.html">QContactRelationship</a></span><span class="operator">::</span>HasMember()));
<span class="comment">// retrieve all contacts, with no specified sort order, requesting that</span>
<span class="comment">// HasMember relationships be included in the cache of result contacts</span>
<span class="type">QList</span><span class="operator"><</span><span class="type"><a href="qcontact.html">QContact</a></span><span class="operator">></span> allContacts <span class="operator">=</span> m_manager<span class="operator">.</span>contacts(<span class="type"><a href="qcontactfilter.html">QContactFilter</a></span>()<span class="operator">,</span> <span class="type">QList</span><span class="operator"><</span><span class="type"><a href="qcontactsortorder.html">QContactSortOrder</a></span><span class="operator">></span>()<span class="operator">,</span> hasMemberRelationshipsOnly);
</pre>
<a name="removing-relationships"></a>
<h3 >Removing Relationships</h3>
<p>The client can remove a relationship directly from a manager.</p>
<pre class="cpp">
connect(<span class="operator">&</span>m_relationshipRemoveRequest<span class="operator">,</span> SIGNAL(stateChanged(<span class="type"><a href="qcontactabstractrequest.html">QContactAbstractRequest</a></span><span class="operator">::</span>State))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(relationshipRemoveRequestStateChanged(<span class="type"><a href="qcontactabstractrequest.html">QContactAbstractRequest</a></span><span class="operator">::</span>State)));
m_relationshipRemoveRequest<span class="operator">.</span>setManager(m_manager);
m_relationshipRemoveRequest<span class="operator">.</span>setRelationships(<span class="type">QList</span><span class="operator"><</span><span class="type"><a href="qcontactrelationship.html">QContactRelationship</a></span><span class="operator">></span>() <span class="operator"><</span><span class="operator"><</span> groupRelationship);
m_relationshipRemoveRequest<span class="operator">.</span>start();
</pre>
<p>The equivalent code using the synchronous API looks like:</p>
<pre class="cpp">
m_manager<span class="operator">.</span>removeRelationship(groupRelationship);
</pre>
<p>Alternatively, when a contact which is involved in a relationship is removed, any relationships in which it is involved will be removed also.</p>
</div>
<!-- @@@contactsusage.html -->
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2017 The Qt Company Ltd.
Documentation contributions included herein are the copyrights of
their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. </p>
</div>
</body>
</html>
|