Debian Codegraph @

src/imports/Lomiri/Contacts/ContactFetch.qml

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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 _ready: false
31
32 signal contactFetched(QtObject contact)
33 signal contactRemoved()
34 signal contactNotFound()
35
36 function fetchContact(contactId) {
37 if (root._ready) {
38 root._fetchContact(contactId)
39 } else {
40 root._pendingId = contactId
41 }
42 }
43
44 function _fetchContact(contactId) {
45 if (running) {
46 console.warn("Fetch already running!")
47 return
48 }
49
50 if (contact && !contactIsDirty && contact.contacId == contactId) {
51 contactFetched(contact)
52 } else if (model) {
53 contactIsDirty = true
54 running = true
55 if (model.manager === "memory") {
56 // memory backend emit contact fetched before return from "fetchContacts" we will use operation = "-2"
57 // to say that we are wainting for a operation from memory manager
58 connections.currentQueryId = -2
59 model.fetchContacts([contactId])
60 } else {
61 connections.currentQueryId = model.fetchContacts([contactId])
62 if (connections.currentQueryId === -1) {
63 running = false
64 }
65 }
66 }
67 }
68
69 onContactChanged: {
70 if (contact == null) {
71 contactRemoved()
72 }
73 }
74
75 Connections {
76 id: connections
77
78 property int currentQueryId: -1
79 property QtObject contact: null
80 property string contactId: contact ? contact.contactId : ""
81
82 ignoreUnknownSignals: true
83 function onContactsFetched(requestId, fetchedContacts) {
84 // currentQueryId == -2 is used during a fetch using "memory" manager
85 if ((currentQueryId == -2) || (requestId == currentQueryId)) {
86 root.contactIsDirty = false
87 root.running = false
88 currentQueryId = -1
89 if (fetchedContacts.length > 0) {
90 contact = fetchedContacts[0]
91 root.contactFetched(fetchedContacts[0])
92 } else {
93 contactNotFound()
94 }
95 }
96 }
97 }
98
99 Component.onCompleted: {
100 root._ready = true
101 if (root._pendingId != "") {
102 root._fetchContact(root._pendingId)
103 root._pendingId = ""
104 }
105 }
106 }