~phablet-team/telephony-service/trunk

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
import QtQuick 1.1
import QtMobility.contacts 1.1
import TelephonyApp 0.1

/*
 * ContactWatcher in an element used to track changes to a specific
 * contact, based on its customId or phone number.
 * Pieces of code interested in a specific contact should create
 * an instance of ContactWatcher and set either "phoneNumber" or "customId".
 * If the contact is not available yet, this element will track the
 * contacts model events and populate the local "contact" property
 * when it becomes available.
 */

Item {
    property variant contact: null
    property string phoneNumber 
    property string customId
    property bool __unknownContact: false

    Component.onCompleted: __checkContact()

    function __checkContact() {
        if (customId && customId != "") {
            contact = contactModel.contactFromCustomId(customId);
            return;
        }
        // try to fill the customId and avoid future queries.
        // in some cases only phoneNumber is set, but this contact
        // has a customId, so in order to avoid calling contactFromPhoneNumber()
        // many times, we cache the customId and wait for this contact to
        // appear in the model.
        if (phoneNumber && (!customId || customId == "") && !__unknownContact) {
            customId = contactModel.customIdFromPhoneNumber(phoneNumber);
            if(customId && customId != "") {
                return;
            } else {
                __unknownContact = true;
                contact = null;
                return;
            }
        }
        // if this contact does not exist on database, 
        // dont waste time asking the backend about it.
        if (phoneNumber && !__unknownContact) {
            contact = contactModel.contactFromPhoneNumber(phoneNumber);
        }
    }

    function __checkContactAdded(newContact) {
        // check if we hold an instance of a contact already
        if (contact || __unknownContact) {
            return;
        }
        
        if (customId && customId != "" && newContact.customId == customId) {
            contact = newContact;
            return;
        }
        __checkContact()
    }
 
    function __checkContactRemoved(removedCustomId) {
        // check if we hold an instance of a contact already
        if (!contact) {
            return;
        }

        // check if we got removed
        if (customId == removedCustomId) {
            contact = null
            return;
        }
    }
   
    Connections {
        target: contactModel
        onContactAdded: __checkContactAdded(contact)
        onContactRemoved: __checkContactRemoved(customId)
    }

    onPhoneNumberChanged: {
        customId = ""; 
        __unknownContact = false; 
        __checkContact();
    }

    onCustomIdChanged: {
        __unknownContact = false; 
        __checkContact();
    }
}