~renatofilho/telephony-service/fix-phone-number-field-cursor

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
/*
 * Copyright (C) 2013 Canonical, Ltd.
 *
 * Authors:
 *  Tiago Salem Herrmann <tiago.herrmann@canonical.com>
 *
 * This file is part of telephony-service.
 *
 * telephony-service is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * telephony-service is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "contactwatcher.h"
#include "contactutils.h"
#include "phoneutils.h"
#include <QContactManager>
#include <QContactFetchByIdRequest>
#include <QContactFetchRequest>
#include <QContactAvatar>
#include <QContactDetailFilter>
#include <QContactPhoneNumber>

namespace C {
#include <libintl.h>
}

ContactWatcher::ContactWatcher(QObject *parent) :
    QObject(parent), mInteractive(true)
{
    connect(ContactUtils::sharedManager(),
            SIGNAL(contactsAdded(QList<QContactId>)),
            SLOT(onContactsAdded(QList<QContactId>)));
    connect(ContactUtils::sharedManager(),
            SIGNAL(contactsChanged(QList<QContactId>)),
            SLOT(onContactsChanged(QList<QContactId>)));
    connect(ContactUtils::sharedManager(),
            SIGNAL(contactsRemoved(QList<QContactId>)),
            SLOT(onContactsRemoved(QList<QContactId>)));
}

void ContactWatcher::searchByPhoneNumber(const QString &phoneNumber)
{
    QContactFetchRequest *request = new QContactFetchRequest(this);
    request->setFilter(QContactPhoneNumber::match(phoneNumber));
    connect(request, SIGNAL(stateChanged(QContactAbstractRequest::State)), SLOT(onRequestStateChanged(QContactAbstractRequest::State)));
    connect(request, SIGNAL(resultsAvailable()), SLOT(resultsAvailable()));
    request->setManager(ContactUtils::sharedManager());
    request->start();
}

QString ContactWatcher::contactId() const
{
    return mContactId;
}

QString ContactWatcher::avatar() const
{
    return mAvatar;
}

QString ContactWatcher::alias() const
{
    return mAlias;
}

QString ContactWatcher::phoneNumber() const
{
    return mPhoneNumber;
}

QList<int> ContactWatcher::phoneNumberSubTypes() const
{
    return mPhoneNumberSubTypes;
}

QList<int> ContactWatcher::phoneNumberContexts() const
{
    return mPhoneNumberContexts;
}

void ContactWatcher::setPhoneNumber(const QString &phoneNumber)
{
    const bool isPrivate = phoneNumber.startsWith("x-ofono-private");
    const bool isUnknown = phoneNumber.startsWith("x-ofono-unknown");

    mPhoneNumber = phoneNumber;
    mInteractive = true;
    Q_EMIT phoneNumberChanged();
    if (mPhoneNumber.isEmpty() || isPrivate || isUnknown) {
        mAlias.clear();
        mContactId.clear();
        mAvatar.clear();
        mPhoneNumberSubTypes.clear();
        mPhoneNumberContexts.clear();
        mInteractive = false;

        if (isPrivate) {
            mAlias = C::gettext("Private Number");
        } else if (isUnknown) {
            mAlias = C::gettext("Unknown Number");
        }

        Q_EMIT contactIdChanged();
        Q_EMIT avatarChanged();
        Q_EMIT aliasChanged();
        Q_EMIT phoneNumberSubTypesChanged();
        Q_EMIT phoneNumberContextsChanged();
        Q_EMIT isUnknownChanged();
        Q_EMIT interactiveChanged();
        return;
    }

    Q_EMIT interactiveChanged();
    searchByPhoneNumber(mPhoneNumber);
}

bool ContactWatcher::isUnknown() const
{
    return mContactId.isEmpty();
}

bool ContactWatcher::interactive() const
{
    return mInteractive;
}

void ContactWatcher::onContactsAdded(QList<QContactId> ids)
{
    // ignore this signal if we have a contact already
    // or if we have no phone number set
    if (!mContactId.isEmpty() || mPhoneNumber.isEmpty()) {
        return;
    }

    searchByPhoneNumber(mPhoneNumber);
}

void ContactWatcher::onContactsChanged(QList<QContactId> ids)
{
    // check for changes even if we have this contact already,
    // as the number might have changed, thus invalidating the current contact
    if (!mPhoneNumber.isEmpty()) {
        searchByPhoneNumber(mPhoneNumber);
    }
}

void ContactWatcher::onContactsRemoved(QList<QContactId> ids)
{
    bool currentContactRemoved =  false;
    Q_FOREACH (const QContactId &contactId, ids) {
        if(contactId.toString() == mContactId) {
            currentContactRemoved = true;
            break;
        }
    }

    // if the current contact got removed, clear it before trying to search for a new one
    if (currentContactRemoved) {
        mAlias.clear();
        mContactId.clear();
        mAvatar.clear();
        mPhoneNumberSubTypes.clear();
        mPhoneNumberContexts.clear();
        Q_EMIT contactIdChanged();
        Q_EMIT avatarChanged();
        Q_EMIT aliasChanged();
        Q_EMIT phoneNumberSubTypesChanged();
        Q_EMIT phoneNumberContextsChanged();
        Q_EMIT isUnknownChanged();

        if (!mPhoneNumber.isEmpty()) {
            searchByPhoneNumber(mPhoneNumber);
        }
    }
}

void ContactWatcher::resultsAvailable()
{
    QContactFetchRequest *request = qobject_cast<QContactFetchRequest*>(sender());
    if (request && request->contacts().size() > 0) {
        // use the first match
        QContact contact = request->contacts().at(0);
        mContactId = contact.id().toString();
        mAvatar = QContactAvatar(contact.detail(QContactDetail::TypeAvatar)).imageUrl().toString();
        mAlias = ContactUtils::formatContactName(contact);
        Q_FOREACH(const QContactPhoneNumber phoneNumber, contact.details(QContactDetail::TypePhoneNumber)) {
            if (PhoneUtils::comparePhoneNumbers(phoneNumber.number(), mPhoneNumber)) {
                mPhoneNumberSubTypes = phoneNumber.subTypes();
                mPhoneNumberContexts = phoneNumber.contexts();
            }
        }

        Q_EMIT contactIdChanged();
        Q_EMIT avatarChanged();
        Q_EMIT aliasChanged();
        Q_EMIT phoneNumberSubTypesChanged();
        Q_EMIT phoneNumberContextsChanged();
        Q_EMIT isUnknownChanged();
    }
}

void ContactWatcher::onRequestStateChanged(QContactAbstractRequest::State state)
{
    QContactFetchRequest *request = qobject_cast<QContactFetchRequest*>(sender());
    if (request && state == QContactAbstractRequest::FinishedState) {
        request->deleteLater();

        // if we got no results and we had a contact previously, we need to clear the data
        if (request->contacts().isEmpty() && !mContactId.isEmpty()) {
            mAlias.clear();
            mContactId.clear();
            mAvatar.clear();
            mPhoneNumberSubTypes.clear();
            mPhoneNumberContexts.clear();

            Q_EMIT contactIdChanged();
            Q_EMIT avatarChanged();
            Q_EMIT aliasChanged();
            Q_EMIT phoneNumberSubTypesChanged();
            Q_EMIT phoneNumberContextsChanged();
            Q_EMIT isUnknownChanged();
        }
    }
}