~ubuntu-branches/ubuntu/trusty/telephony-service/trusty

« back to all changes in this revision

Viewing changes to handler/tests/approver.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release, Gustavo Pichorim Boiko, Timo Jyrinki, Tiago Salem Herrmann
  • Date: 2014-01-30 17:08:38 UTC
  • mfrom: (1.1.12)
  • Revision ID: package-import@ubuntu.com-20140130170838-42iekm3rlprvz8nq
Tags: 0.1+14.04.20140130-0ubuntu1
[ Gustavo Pichorim Boiko ]
* Add the infrastructure to write tests for the handler, and write
  tests for the most important bits of it.
* Make the dbus-based tests more robust. (LP: #1270778)
* Improve the support for handling multiple calls by: Using a
  different approach to detect background calls in the QML plugin.
  Make it possible to hangup the active call to answer an incoming
  one. Show notifications when calls are put on hold, when they are
  rejected and when they end. .

[ Timo Jyrinki ]
* Use QT_QPA_PLATFORM=minimal to fix test failures (LP: #1270770).
  (LP: #1270770)

[ Tiago Salem Herrmann ]
* Fix default action for missed calls. Add voicemail entries to the
  messaging menu.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright (C) 2013 Canonical, Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it under
 
5
 * the terms of the GNU General Public License version 3, as published by
 
6
 * the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
 
9
 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
 
10
 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
 * 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
 * Authors: Tiago Salem Herrmann <tiago.herrmann@canonical.com>
 
17
 */
 
18
 
 
19
#include "approver.h"
 
20
#include <QDebug>
 
21
 
 
22
#include <TelepathyQt/PendingReady>
 
23
#include <TelepathyQt/ChannelClassSpec>
 
24
#include <TelepathyQt/ClientRegistrar>
 
25
#include <TelepathyQt/CallChannel>
 
26
#include <TelepathyQt/TextChannel>
 
27
 
 
28
Approver::Approver(QObject* parent)
 
29
: QObject(parent), Tp::AbstractClientApprover(channelFilters())
 
30
{
 
31
}
 
32
 
 
33
Approver::~Approver()
 
34
{
 
35
}
 
36
 
 
37
Tp::ChannelClassSpecList Approver::channelFilters() const
 
38
{
 
39
    Tp::ChannelClassSpecList specList;
 
40
    specList << Tp::ChannelClassSpec::textChat()
 
41
             << Tp::ChannelClassSpec::audioCall();
 
42
 
 
43
    return specList;
 
44
}
 
45
 
 
46
void Approver::addDispatchOperation(const Tp::MethodInvocationContextPtr<> &context,
 
47
                                        const Tp::ChannelDispatchOperationPtr &dispatchOperation)
 
48
{
 
49
    bool willHandle = false;
 
50
 
 
51
    QList<Tp::ChannelPtr> channels = dispatchOperation->channels();
 
52
    Q_FOREACH (Tp::ChannelPtr channel, channels) {
 
53
        // Text Channel
 
54
        Tp::TextChannelPtr textChannel = Tp::TextChannelPtr::dynamicCast(channel);
 
55
        if (!textChannel.isNull()) {
 
56
            // right now we are not using any of the text channel's features in the approver
 
57
            // so no need to call becomeReady() on it.
 
58
            willHandle = true;
 
59
            continue;
 
60
        }
 
61
 
 
62
        // Call Channel
 
63
        Tp::CallChannelPtr callChannel = Tp::CallChannelPtr::dynamicCast(channel);
 
64
        if (!callChannel.isNull()) {
 
65
            Tp::PendingReady *pr = callChannel->becomeReady(Tp::Features()
 
66
                                  << Tp::CallChannel::FeatureCore
 
67
                                  << Tp::CallChannel::FeatureCallState
 
68
                                  << Tp::CallChannel::FeatureLocalHoldState);
 
69
            mChannels[pr] = callChannel;
 
70
 
 
71
            connect(pr, SIGNAL(finished(Tp::PendingOperation*)),
 
72
                    SLOT(onChannelReady(Tp::PendingOperation*)));
 
73
            callChannel->setProperty("accountId", QVariant(dispatchOperation->account()->uniqueIdentifier()));
 
74
            willHandle = true;
 
75
            continue;
 
76
        }
 
77
    }
 
78
 
 
79
    if (willHandle) {
 
80
        mDispatchOps.append(dispatchOperation);
 
81
    }
 
82
 
 
83
    context->setFinished();
 
84
 
 
85
    // check if we need to approve channels already or if we should wait.
 
86
    processChannels();
 
87
}
 
88
 
 
89
void Approver::processChannels()
 
90
{
 
91
    Q_FOREACH (Tp::ChannelDispatchOperationPtr dispatchOperation, mDispatchOps) {
 
92
        QList<Tp::ChannelPtr> channels = dispatchOperation->channels();
 
93
        Q_FOREACH (Tp::ChannelPtr channel, channels) {
 
94
            // approve only text channels
 
95
            Tp::TextChannelPtr textChannel = Tp::TextChannelPtr::dynamicCast(channel);
 
96
            if (textChannel.isNull()) {
 
97
                continue;
 
98
            }
 
99
 
 
100
            if (dispatchOperation->possibleHandlers().contains(TELEPHONY_SERVICE_HANDLER)) {
 
101
                dispatchOperation->handleWith(TELEPHONY_SERVICE_HANDLER);
 
102
                mDispatchOps.removeAll(dispatchOperation);
 
103
            }
 
104
            // FIXME: this shouldn't happen, but in any case, we need to check what to do when
 
105
            // the phone app client is not available
 
106
        }
 
107
    }
 
108
}
 
109
 
 
110
void Approver::acceptCall()
 
111
{
 
112
    Q_FOREACH (Tp::ChannelDispatchOperationPtr dispatchOperation, mDispatchOps) {
 
113
        QList<Tp::ChannelPtr> channels = dispatchOperation->channels();
 
114
        Q_FOREACH (Tp::ChannelPtr channel, channels) {
 
115
            if (dispatchOperation->possibleHandlers().contains(TELEPHONY_SERVICE_HANDLER)) {
 
116
                dispatchOperation->handleWith(TELEPHONY_SERVICE_HANDLER);
 
117
                mDispatchOps.removeAll(dispatchOperation);
 
118
            }
 
119
        }
 
120
    }
 
121
}
 
122
 
 
123
void Approver::rejectCall()
 
124
{
 
125
    Q_FOREACH (Tp::ChannelDispatchOperationPtr dispatchOperation, mDispatchOps) {
 
126
        QList<Tp::ChannelPtr> channels = dispatchOperation->channels();
 
127
        Q_FOREACH (Tp::ChannelPtr channel, channels) {
 
128
            if (dispatchOperation->possibleHandlers().contains(TELEPHONY_SERVICE_HANDLER)) {
 
129
                Tp::PendingOperation *claimop = dispatchOperation->claim();
 
130
                mChannels[claimop] = dispatchOperation->channels().first();
 
131
                connect(claimop, SIGNAL(finished(Tp::PendingOperation*)),
 
132
                    this, SLOT(onClaimFinished(Tp::PendingOperation*)));
 
133
            }
 
134
        }
 
135
    }
 
136
}
 
137
 
 
138
void Approver::onClaimFinished(Tp::PendingOperation* op)
 
139
{
 
140
    if(!op || op->isError()) {
 
141
        return;
 
142
    }
 
143
 
 
144
    Tp::CallChannelPtr callChannel = Tp::CallChannelPtr::dynamicCast(mChannels[op]);
 
145
    if (callChannel) {
 
146
        Tp::PendingOperation *hangupop = callChannel->hangup(Tp::CallStateChangeReasonUserRequested, TP_QT_ERROR_REJECTED, QString());
 
147
        mChannels[hangupop] = callChannel;
 
148
        connect(hangupop, SIGNAL(finished(Tp::PendingOperation*)),
 
149
                this, SLOT(onHangupFinished(Tp::PendingOperation*)));
 
150
    }
 
151
}
 
152
 
 
153
void Approver::onHangupFinished(Tp::PendingOperation* op)
 
154
{
 
155
    if(!op || op->isError()) {
 
156
        return;
 
157
    }
 
158
    mDispatchOps.removeAll(dispatchOperation(op));
 
159
    mChannels.remove(op);
 
160
}
 
161
 
 
162
Tp::ChannelDispatchOperationPtr Approver::dispatchOperation(Tp::PendingOperation *op)
 
163
{
 
164
    Tp::ChannelPtr channel = mChannels[op];
 
165
    QString accountId = channel->property("accountId").toString();
 
166
    Q_FOREACH (Tp::ChannelDispatchOperationPtr dispatchOperation, mDispatchOps) {
 
167
        if (dispatchOperation->account()->uniqueIdentifier() == accountId) {
 
168
            return dispatchOperation;
 
169
        }
 
170
    }
 
171
    return Tp::ChannelDispatchOperationPtr();
 
172
}
 
173
 
 
174
void Approver::onChannelReady(Tp::PendingOperation *op)
 
175
{
 
176
    Q_EMIT newCall();
 
177
}
 
178
 
 
179