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

719.1.3 by Tiago Salem Herrmann
implement other functions
1
/*
2
 * Copyright (C) 2013 Canonical, Ltd.
3
 *
4
 * Authors:
5
 *  Tiago Salem Herrmann <tiago.herrmann@canonical.com>
6
 *
7
 * This file is part of telephony-service.
8
 *
9
 * telephony-service is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; version 3.
12
 *
13
 * telephony-service is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
719.1.1 by Tiago Salem Herrmann
add initial contact watcher implementation
22
#include "contactwatcher.h"
722.2.2 by Gustavo Pichorim Boiko
Use the manager from the ContactUtils.
23
#include "contactutils.h"
726.1.1 by Tiago Salem Herrmann
make ContactWatcher provide subTypes and contexts of the matched phone number
24
#include "phoneutils.h"
719.1.1 by Tiago Salem Herrmann
add initial contact watcher implementation
25
#include <QContactManager>
26
#include <QContactFetchByIdRequest>
27
#include <QContactFetchRequest>
28
#include <QContactAvatar>
29
#include <QContactDetailFilter>
719.1.3 by Tiago Salem Herrmann
implement other functions
30
#include <QContactPhoneNumber>
719.1.1 by Tiago Salem Herrmann
add initial contact watcher implementation
31
759.1.1 by Tiago Salem Herrmann
add support for unknown and private numbers
32
namespace C {
33
#include <libintl.h>
34
}
35
719.1.1 by Tiago Salem Herrmann
add initial contact watcher implementation
36
ContactWatcher::ContactWatcher(QObject *parent) :
759.1.4 by Tiago Salem Herrmann
initialize mInteractive
37
    QObject(parent), mInteractive(true)
719.1.1 by Tiago Salem Herrmann
add initial contact watcher implementation
38
{
722.2.2 by Gustavo Pichorim Boiko
Use the manager from the ContactUtils.
39
    connect(ContactUtils::sharedManager(),
719.1.1 by Tiago Salem Herrmann
add initial contact watcher implementation
40
            SIGNAL(contactsAdded(QList<QContactId>)),
41
            SLOT(onContactsAdded(QList<QContactId>)));
722.2.2 by Gustavo Pichorim Boiko
Use the manager from the ContactUtils.
42
    connect(ContactUtils::sharedManager(),
719.1.1 by Tiago Salem Herrmann
add initial contact watcher implementation
43
            SIGNAL(contactsChanged(QList<QContactId>)),
44
            SLOT(onContactsChanged(QList<QContactId>)));
722.2.2 by Gustavo Pichorim Boiko
Use the manager from the ContactUtils.
45
    connect(ContactUtils::sharedManager(),
719.1.1 by Tiago Salem Herrmann
add initial contact watcher implementation
46
            SIGNAL(contactsRemoved(QList<QContactId>)),
47
            SLOT(onContactsRemoved(QList<QContactId>)));
48
}
49
719.1.3 by Tiago Salem Herrmann
implement other functions
50
void ContactWatcher::searchByPhoneNumber(const QString &phoneNumber)
719.1.1 by Tiago Salem Herrmann
add initial contact watcher implementation
51
{
719.1.3 by Tiago Salem Herrmann
implement other functions
52
    QContactFetchRequest *request = new QContactFetchRequest(this);
53
    request->setFilter(QContactPhoneNumber::match(phoneNumber));
719.1.1 by Tiago Salem Herrmann
add initial contact watcher implementation
54
    connect(request, SIGNAL(stateChanged(QContactAbstractRequest::State)), SLOT(onRequestStateChanged(QContactAbstractRequest::State)));
719.1.3 by Tiago Salem Herrmann
implement other functions
55
    connect(request, SIGNAL(resultsAvailable()), SLOT(resultsAvailable()));
722.2.2 by Gustavo Pichorim Boiko
Use the manager from the ContactUtils.
56
    request->setManager(ContactUtils::sharedManager());
719.1.1 by Tiago Salem Herrmann
add initial contact watcher implementation
57
    request->start();
58
}
59
719.1.2 by Tiago Salem Herrmann
use resultsAvailable() signal
60
QString ContactWatcher::contactId() const
719.1.1 by Tiago Salem Herrmann
add initial contact watcher implementation
61
{
719.1.2 by Tiago Salem Herrmann
use resultsAvailable() signal
62
    return mContactId;
719.1.1 by Tiago Salem Herrmann
add initial contact watcher implementation
63
}
64
65
QString ContactWatcher::avatar() const
66
{
67
    return mAvatar;
68
}
69
70
QString ContactWatcher::alias() const
71
{
72
    return mAlias;
73
}
74
75
QString ContactWatcher::phoneNumber() const
76
{
77
    return mPhoneNumber;
78
}
79
726.1.1 by Tiago Salem Herrmann
make ContactWatcher provide subTypes and contexts of the matched phone number
80
QList<int> ContactWatcher::phoneNumberSubTypes() const
81
{
82
    return mPhoneNumberSubTypes;
83
}
84
85
QList<int> ContactWatcher::phoneNumberContexts() const
86
{
87
    return mPhoneNumberContexts;
88
}
89
719.1.1 by Tiago Salem Herrmann
add initial contact watcher implementation
90
void ContactWatcher::setPhoneNumber(const QString &phoneNumber)
91
{
759.1.1 by Tiago Salem Herrmann
add support for unknown and private numbers
92
    const bool isPrivate = phoneNumber.startsWith("x-ofono-private");
93
    const bool isUnknown = phoneNumber.startsWith("x-ofono-unknown");
94
719.1.1 by Tiago Salem Herrmann
add initial contact watcher implementation
95
    mPhoneNumber = phoneNumber;
759.1.3 by Tiago Salem Herrmann
fix mInteractive attribution
96
    mInteractive = true;
719.1.7 by Tiago Salem Herrmann
do not keep copy of engineInstance() pointer
97
    Q_EMIT phoneNumberChanged();
759.1.1 by Tiago Salem Herrmann
add support for unknown and private numbers
98
    if (mPhoneNumber.isEmpty() || isPrivate || isUnknown) {
719.1.3 by Tiago Salem Herrmann
implement other functions
99
        mAlias.clear();
100
        mContactId.clear();
101
        mAvatar.clear();
726.1.1 by Tiago Salem Herrmann
make ContactWatcher provide subTypes and contexts of the matched phone number
102
        mPhoneNumberSubTypes.clear();
103
        mPhoneNumberContexts.clear();
759.1.3 by Tiago Salem Herrmann
fix mInteractive attribution
104
        mInteractive = false;
759.1.1 by Tiago Salem Herrmann
add support for unknown and private numbers
105
106
        if (isPrivate) {
107
            mAlias = C::gettext("Private Number");
108
        } else if (isUnknown) {
109
            mAlias = C::gettext("Unknown Number");
110
        }
111
719.1.3 by Tiago Salem Herrmann
implement other functions
112
        Q_EMIT contactIdChanged();
113
        Q_EMIT avatarChanged();
114
        Q_EMIT aliasChanged();
726.1.1 by Tiago Salem Herrmann
make ContactWatcher provide subTypes and contexts of the matched phone number
115
        Q_EMIT phoneNumberSubTypesChanged();
116
        Q_EMIT phoneNumberContextsChanged();
719.1.3 by Tiago Salem Herrmann
implement other functions
117
        Q_EMIT isUnknownChanged();
759.1.1 by Tiago Salem Herrmann
add support for unknown and private numbers
118
        Q_EMIT interactiveChanged();
719.1.3 by Tiago Salem Herrmann
implement other functions
119
        return;
120
    }
719.1.1 by Tiago Salem Herrmann
add initial contact watcher implementation
121
759.1.3 by Tiago Salem Herrmann
fix mInteractive attribution
122
    Q_EMIT interactiveChanged();
719.1.3 by Tiago Salem Herrmann
implement other functions
123
    searchByPhoneNumber(mPhoneNumber);
719.1.1 by Tiago Salem Herrmann
add initial contact watcher implementation
124
}
125
126
bool ContactWatcher::isUnknown() const
127
{
719.1.3 by Tiago Salem Herrmann
implement other functions
128
    return mContactId.isEmpty();
719.1.1 by Tiago Salem Herrmann
add initial contact watcher implementation
129
}
130
759.1.1 by Tiago Salem Herrmann
add support for unknown and private numbers
131
bool ContactWatcher::interactive() const
132
{
133
    return mInteractive;
134
}
719.1.1 by Tiago Salem Herrmann
add initial contact watcher implementation
135
136
void ContactWatcher::onContactsAdded(QList<QContactId> ids)
137
{
719.1.3 by Tiago Salem Herrmann
implement other functions
138
    // ignore this signal if we have a contact already
139
    // or if we have no phone number set
719.1.4 by Tiago Salem Herrmann
fix comments, identation and debugs
140
    if (!mContactId.isEmpty() || mPhoneNumber.isEmpty()) {
719.1.3 by Tiago Salem Herrmann
implement other functions
141
        return;
719.1.4 by Tiago Salem Herrmann
fix comments, identation and debugs
142
    }
719.1.1 by Tiago Salem Herrmann
add initial contact watcher implementation
143
719.1.3 by Tiago Salem Herrmann
implement other functions
144
    searchByPhoneNumber(mPhoneNumber);
719.1.1 by Tiago Salem Herrmann
add initial contact watcher implementation
145
}
146
147
void ContactWatcher::onContactsChanged(QList<QContactId> ids)
148
{
726.1.1 by Tiago Salem Herrmann
make ContactWatcher provide subTypes and contexts of the matched phone number
149
    // check for changes even if we have this contact already,
150
    // as the number might have changed, thus invalidating the current contact
151
    if (!mPhoneNumber.isEmpty()) {
719.1.3 by Tiago Salem Herrmann
implement other functions
152
        searchByPhoneNumber(mPhoneNumber);
153
    }
719.1.1 by Tiago Salem Herrmann
add initial contact watcher implementation
154
}
155
156
void ContactWatcher::onContactsRemoved(QList<QContactId> ids)
157
{
726.1.1 by Tiago Salem Herrmann
make ContactWatcher provide subTypes and contexts of the matched phone number
158
    bool currentContactRemoved =  false;
159
    Q_FOREACH (const QContactId &contactId, ids) {
160
        if(contactId.toString() == mContactId) {
161
            currentContactRemoved = true;
162
            break;
163
        }
164
    }
165
727.1.2 by Gustavo Pichorim Boiko
Fix the logic for clearing contact data in the contact watcher.
166
    // if the current contact got removed, clear it before trying to search for a new one
167
    if (currentContactRemoved) {
719.1.3 by Tiago Salem Herrmann
implement other functions
168
        mAlias.clear();
169
        mContactId.clear();
170
        mAvatar.clear();
726.1.1 by Tiago Salem Herrmann
make ContactWatcher provide subTypes and contexts of the matched phone number
171
        mPhoneNumberSubTypes.clear();
172
        mPhoneNumberContexts.clear();
719.1.3 by Tiago Salem Herrmann
implement other functions
173
        Q_EMIT contactIdChanged();
174
        Q_EMIT avatarChanged();
175
        Q_EMIT aliasChanged();
726.1.1 by Tiago Salem Herrmann
make ContactWatcher provide subTypes and contexts of the matched phone number
176
        Q_EMIT phoneNumberSubTypesChanged();
177
        Q_EMIT phoneNumberContextsChanged();
719.1.3 by Tiago Salem Herrmann
implement other functions
178
        Q_EMIT isUnknownChanged();
727.1.2 by Gustavo Pichorim Boiko
Fix the logic for clearing contact data in the contact watcher.
179
180
        if (!mPhoneNumber.isEmpty()) {
181
            searchByPhoneNumber(mPhoneNumber);
182
        }
719.1.3 by Tiago Salem Herrmann
implement other functions
183
    }
184
}
719.1.1 by Tiago Salem Herrmann
add initial contact watcher implementation
185
186
void ContactWatcher::resultsAvailable()
187
{
719.1.2 by Tiago Salem Herrmann
use resultsAvailable() signal
188
    QContactFetchRequest *request = qobject_cast<QContactFetchRequest*>(sender());
719.1.3 by Tiago Salem Herrmann
implement other functions
189
    if (request && request->contacts().size() > 0) {
719.1.4 by Tiago Salem Herrmann
fix comments, identation and debugs
190
        // use the first match
719.1.2 by Tiago Salem Herrmann
use resultsAvailable() signal
191
        QContact contact = request->contacts().at(0);
192
        mContactId = contact.id().toString();
193
        mAvatar = QContactAvatar(contact.detail(QContactDetail::TypeAvatar)).imageUrl().toString();
722.2.3 by Gustavo Pichorim Boiko
Use the contact name formatted in the form FirstName + LastName.
194
        mAlias = ContactUtils::formatContactName(contact);
726.1.1 by Tiago Salem Herrmann
make ContactWatcher provide subTypes and contexts of the matched phone number
195
        Q_FOREACH(const QContactPhoneNumber phoneNumber, contact.details(QContactDetail::TypePhoneNumber)) {
758.1.4 by Tiago Salem Herrmann
revert back to comparePhoneNumbers()
196
            if (PhoneUtils::comparePhoneNumbers(phoneNumber.number(), mPhoneNumber)) {
726.1.1 by Tiago Salem Herrmann
make ContactWatcher provide subTypes and contexts of the matched phone number
197
                mPhoneNumberSubTypes = phoneNumber.subTypes();
198
                mPhoneNumberContexts = phoneNumber.contexts();
199
            }
200
        }
727.1.5 by Gustavo Pichorim Boiko
Clear the contact data from ContactWatcher when the match is not valid anymore.
201
202
        Q_EMIT contactIdChanged();
203
        Q_EMIT avatarChanged();
204
        Q_EMIT aliasChanged();
205
        Q_EMIT phoneNumberSubTypesChanged();
206
        Q_EMIT phoneNumberContextsChanged();
207
        Q_EMIT isUnknownChanged();
719.1.2 by Tiago Salem Herrmann
use resultsAvailable() signal
208
    }
719.1.1 by Tiago Salem Herrmann
add initial contact watcher implementation
209
}
210
211
void ContactWatcher::onRequestStateChanged(QContactAbstractRequest::State state)
212
{
213
    QContactFetchRequest *request = qobject_cast<QContactFetchRequest*>(sender());
719.1.3 by Tiago Salem Herrmann
implement other functions
214
    if (request && state == QContactAbstractRequest::FinishedState) {
719.1.1 by Tiago Salem Herrmann
add initial contact watcher implementation
215
        request->deleteLater();
727.1.5 by Gustavo Pichorim Boiko
Clear the contact data from ContactWatcher when the match is not valid anymore.
216
217
        // if we got no results and we had a contact previously, we need to clear the data
218
        if (request->contacts().isEmpty() && !mContactId.isEmpty()) {
219
            mAlias.clear();
220
            mContactId.clear();
221
            mAvatar.clear();
222
            mPhoneNumberSubTypes.clear();
223
            mPhoneNumberContexts.clear();
224
225
            Q_EMIT contactIdChanged();
226
            Q_EMIT avatarChanged();
227
            Q_EMIT aliasChanged();
228
            Q_EMIT phoneNumberSubTypesChanged();
229
            Q_EMIT phoneNumberContextsChanged();
230
            Q_EMIT isUnknownChanged();
231
        }
719.1.1 by Tiago Salem Herrmann
add initial contact watcher implementation
232
    }
233
}