| 1 |
/* |
| 2 |
* Copyright (C) 2012-2015 Canonical, Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; version 3. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
import QtQuick 2.4 |
| 18 |
|
| 19 |
Item { |
| 20 |
id: root |
| 21 |
|
| 22 |
readonly property alias contact: connections.contact |
| 23 |
readonly property alias contactId: connections.contactId |
| 24 |
property alias model: connections.target |
| 25 |
|
| 26 |
property bool running: false |
| 27 |
property bool contactIsDirty: false |
| 28 |
|
| •29 |
property string pendingId: "" |
| 30 |
property bool withConstituents: false |
| 31 |
property bool completed: false |
| 32 |
property bool ready: completed && model !== null |
| 33 |
|
| •34 |
signal contactFetched(QtObject contact, var constituents, |
| 35 |
QtObject mainConstituent) |
| 36 |
signal contactRemoved() |
| 37 |
signal contactNotFound() |
| 38 |
|
| •39 |
onReadyChanged: { |
| 40 |
if (ready && (root.pendingId != "")) { |
| 41 |
root._fetchContact(root.pendingId) |
| 42 |
root.pendingId = "" |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
function fetchContact(contactId) { |
| •48 |
root.withConstituents = false |
| 49 |
if (root.ready) { |
| 50 |
root._fetchContact(contactId) |
| 51 |
} else { |
| 52 |
root.pendingId = contactId |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
function fetchContactFull(contactId) { |
| 57 |
root.withConstituents = true |
| 58 |
if (root.ready) { |
| 59 |
root._fetchContact(contactId) |
| 60 |
} else { |
| •61 |
root.pendingId = contactId |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
function _fetchContact(contactId) { |
| 66 |
if (running) { |
| 67 |
console.warn("Fetch already running!") |
| 68 |
return |
| 69 |
} |
| •70 |
if (contact && !contactIsDirty && contact.contactId == contactId) { |
| 71 |
if (root.withConstituents) { |
| 72 |
root._fetchConstituents(contact) |
| 73 |
} else { |
| 74 |
running = false |
| 75 |
contactFetched(contact, [], null) |
| 76 |
} |
| 77 |
} else if (model) { |
| 78 |
contactIsDirty = true |
| 79 |
running = true |
| 80 |
if (model.manager === "memory") { |
| 81 |
// memory backend emit contact fetched before return from "fetchContacts" we will use operation = "-2" |
| 82 |
// to say that we are wainting for a operation from memory manager |
| 83 |
connections.currentQueryId = -2 |
| 84 |
model.fetchContacts([contactId]) |
| 85 |
} else { |
| 86 |
connections.currentQueryId = model.fetchContacts([contactId]) |
| 87 |
if (connections.currentQueryId === -1) { |
| 88 |
running = false |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| •94 |
function _fetchConstituents(contact) { |
| 95 |
model.enumerateConstituents(contact, function(contacts) { |
| 96 |
var mainConstituent = null |
| 97 |
for (var i = 0; i < contacts.length; i++) { |
| 98 |
/* TODO: add support for the case when there are multiple |
| 99 |
* constituents */ |
| 100 |
mainConstituent = contacts[i] |
| 101 |
} |
| 102 |
running = false |
| 103 |
root.contactFetched(root.contact, contacts, mainConstituent) |
| 104 |
}) |
| 105 |
} |
| 106 |
|
| 107 |
onContactChanged: { |
| 108 |
if (contact == null) { |
| 109 |
contactRemoved() |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
Connections { |
| 114 |
id: connections |
| 115 |
|
| 116 |
property int currentQueryId: -1 |
| 117 |
property QtObject contact: null |
| 118 |
property string contactId: contact ? contact.contactId : "" |
| 119 |
|
| 120 |
ignoreUnknownSignals: true |
| 121 |
function onContactsFetched(requestId, fetchedContacts) { |
| 122 |
// currentQueryId == -2 is used during a fetch using "memory" manager |
| 123 |
if ((currentQueryId == -2) || (requestId == currentQueryId)) { |
| 124 |
root.contactIsDirty = false |
| 125 |
root.running = false |
| 126 |
currentQueryId = -1 |
| 127 |
if (fetchedContacts.length > 0) { |
| 128 |
contact = fetchedContacts[0] |
| •129 |
if (root.withConstituents) { |
| 130 |
root._fetchConstituents(contact) |
| 131 |
} else { |
| 132 |
root.contactFetched(fetchedContacts[0], [], null) |
| 133 |
} |
| 134 |
} else { |
| 135 |
contactNotFound() |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
Component.onCompleted: { |
| •142 |
root.completed = true |
| 143 |
} |
| 144 |
} |