File: request-temporary-handler-internal.cpp

package info (click to toggle)
telepathy-qt 0.9.1-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 38,764 kB
  • sloc: cpp: 59,914; xml: 34,543; ansic: 21,194; python: 3,370; sh: 118; makefile: 20
file content (135 lines) | stat: -rw-r--r-- 4,465 bytes parent folder | download | duplicates (5)
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
/**
 * This file is part of TelepathyQt
 *
 * @copyright Copyright (C) 2011 Collabora Ltd. <http://www.collabora.co.uk/>
 * @copyright Copyright (C) 2011 Nokia Corporation
 * @license LGPL 2.1
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "TelepathyQt/request-temporary-handler-internal.h"

#include "TelepathyQt/_gen/request-temporary-handler-internal.moc.hpp"

#include "TelepathyQt/debug-internal.h"

#include <TelepathyQt/ChannelClassSpecList>

namespace Tp
{

SharedPtr<RequestTemporaryHandler> RequestTemporaryHandler::create(const AccountPtr &account)
{
    return SharedPtr<RequestTemporaryHandler>(new RequestTemporaryHandler(account));
}

RequestTemporaryHandler::RequestTemporaryHandler(const AccountPtr &account)
    : AbstractClient(),
      QObject(),
      AbstractClientHandler(ChannelClassSpecList(), AbstractClientHandler::Capabilities(), false),
      mAccount(account),
      mQueueChannelReceived(true),
      dbusHandlerInvoked(false)
{
}

RequestTemporaryHandler::~RequestTemporaryHandler()
{
}

void RequestTemporaryHandler::handleChannels(
        const MethodInvocationContextPtr<> &context,
        const AccountPtr &account,
        const ConnectionPtr &connection,
        const QList<ChannelPtr> &channels,
        const QList<ChannelRequestPtr> &requestsSatisfied,
        const QDateTime &userActionTime,
        const HandlerInfo &handlerInfo)
{
    Q_ASSERT(dbusHandlerInvoked);

    QString errorMessage;

    ChannelPtr oldChannel = channel();
    if (channels.size() != 1 || requestsSatisfied.size() != 1) {
        errorMessage = QLatin1String("Only one channel and one channel request should be given "
                "to HandleChannels");
    } else if (account != mAccount) {
        errorMessage = QLatin1String("Account received is not the same as the account which made "
                "the request");
    } else if (oldChannel && oldChannel != channels.first()) {
        errorMessage = QLatin1String("Received a channel that is not the same as the first "
                "one received");
    }

    if (!errorMessage.isEmpty()) {
        warning() << "Handling channel failed with" << TP_QT_ERROR_SERVICE_CONFUSED << ":" <<
            errorMessage;

        // Only emit error if we didn't receive any channel yet.
        if (!oldChannel) {
            emit error(TP_QT_ERROR_SERVICE_CONFUSED, errorMessage);
        }
        context->setFinishedWithError(TP_QT_ERROR_SERVICE_CONFUSED, errorMessage);
        return;
    }

    ChannelRequestPtr channelRequest = requestsSatisfied.first();

    if (!oldChannel) {
        mChannel = WeakPtr<Channel>(channels.first());
        emit channelReceived(channel(), userActionTime, channelRequest->hints());
    } else {
        if (mQueueChannelReceived) {
            mChannelReceivedQueue.enqueue(qMakePair(userActionTime, channelRequest->hints()));
        } else {
            emit channelReceived(oldChannel, userActionTime, channelRequest->hints());
        }
    }

    context->setFinished();
}

void RequestTemporaryHandler::setQueueChannelReceived(bool queue)
{
    mQueueChannelReceived = queue;
    if (!queue) {
        processChannelReceivedQueue();
    }
}

void RequestTemporaryHandler::setDBusHandlerInvoked()
{
    dbusHandlerInvoked = true;
}

void RequestTemporaryHandler::setDBusHandlerErrored(const QString &errorName, const QString &errorMessage)
{
    Q_ASSERT(dbusHandlerInvoked);
    if (!channel()) {
        emit error(errorName, errorMessage);
    }
}

void RequestTemporaryHandler::processChannelReceivedQueue()
{
    while (!mChannelReceivedQueue.isEmpty()) {
        QPair<QDateTime, ChannelRequestHints> info = mChannelReceivedQueue.dequeue();
        emit channelReceived(channel(), info.first, info.second);
    }
}

} // Tp