~tiagosh/telepathy-qt/group-chat2

« back to all changes in this revision

Viewing changes to TelepathyQt/request-temporary-handler-internal.cpp

  • Committer: Package Import Robot
  • Author(s): Ricardo Salveti de Araujo
  • Date: 2013-06-06 04:56:14 UTC
  • Revision ID: package-import@ubuntu.com-20130606045614-inpxexo6765rnmp1
Tags: upstream-0.9.3
ImportĀ upstreamĀ versionĀ 0.9.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * This file is part of TelepathyQt
 
3
 *
 
4
 * @copyright Copyright (C) 2011 Collabora Ltd. <http://www.collabora.co.uk/>
 
5
 * @copyright Copyright (C) 2011 Nokia Corporation
 
6
 * @license LGPL 2.1
 
7
 *
 
8
 * This library is free software; you can redistribute it and/or
 
9
 * modify it under the terms of the GNU Lesser General Public
 
10
 * License as published by the Free Software Foundation; either
 
11
 * version 2.1 of the License, or (at your option) any later version.
 
12
 *
 
13
 * This library 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 GNU
 
16
 * Lesser General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU Lesser General Public
 
19
 * License along with this library; if not, write to the Free Software
 
20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
21
 */
 
22
 
 
23
#include "TelepathyQt/request-temporary-handler-internal.h"
 
24
 
 
25
#include "TelepathyQt/_gen/request-temporary-handler-internal.moc.hpp"
 
26
 
 
27
#include "TelepathyQt/debug-internal.h"
 
28
 
 
29
#include <TelepathyQt/ChannelClassSpecList>
 
30
 
 
31
namespace Tp
 
32
{
 
33
 
 
34
SharedPtr<RequestTemporaryHandler> RequestTemporaryHandler::create(const AccountPtr &account)
 
35
{
 
36
    return SharedPtr<RequestTemporaryHandler>(new RequestTemporaryHandler(account));
 
37
}
 
38
 
 
39
RequestTemporaryHandler::RequestTemporaryHandler(const AccountPtr &account)
 
40
    : AbstractClient(),
 
41
      QObject(),
 
42
      AbstractClientHandler(ChannelClassSpecList(), AbstractClientHandler::Capabilities(), false),
 
43
      mAccount(account),
 
44
      mQueueChannelReceived(true),
 
45
      dbusHandlerInvoked(false)
 
46
{
 
47
}
 
48
 
 
49
RequestTemporaryHandler::~RequestTemporaryHandler()
 
50
{
 
51
}
 
52
 
 
53
void RequestTemporaryHandler::handleChannels(
 
54
        const MethodInvocationContextPtr<> &context,
 
55
        const AccountPtr &account,
 
56
        const ConnectionPtr &connection,
 
57
        const QList<ChannelPtr> &channels,
 
58
        const QList<ChannelRequestPtr> &requestsSatisfied,
 
59
        const QDateTime &userActionTime,
 
60
        const HandlerInfo &handlerInfo)
 
61
{
 
62
    Q_ASSERT(dbusHandlerInvoked);
 
63
 
 
64
    QString errorMessage;
 
65
 
 
66
    ChannelPtr oldChannel = channel();
 
67
    if (channels.size() != 1 || requestsSatisfied.size() != 1) {
 
68
        errorMessage = QLatin1String("Only one channel and one channel request should be given "
 
69
                "to HandleChannels");
 
70
    } else if (account != mAccount) {
 
71
        errorMessage = QLatin1String("Account received is not the same as the account which made "
 
72
                "the request");
 
73
    } else if (oldChannel && oldChannel != channels.first()) {
 
74
        errorMessage = QLatin1String("Received a channel that is not the same as the first "
 
75
                "one received");
 
76
    }
 
77
 
 
78
    if (!errorMessage.isEmpty()) {
 
79
        warning() << "Handling channel failed with" << TP_QT_ERROR_SERVICE_CONFUSED << ":" <<
 
80
            errorMessage;
 
81
 
 
82
        // Only emit error if we didn't receive any channel yet.
 
83
        if (!oldChannel) {
 
84
            emit error(TP_QT_ERROR_SERVICE_CONFUSED, errorMessage);
 
85
        }
 
86
        context->setFinishedWithError(TP_QT_ERROR_SERVICE_CONFUSED, errorMessage);
 
87
        return;
 
88
    }
 
89
 
 
90
    ChannelRequestPtr channelRequest = requestsSatisfied.first();
 
91
 
 
92
    if (!oldChannel) {
 
93
        mChannel = WeakPtr<Channel>(channels.first());
 
94
        emit channelReceived(channel(), userActionTime, channelRequest->hints());
 
95
    } else {
 
96
        if (mQueueChannelReceived) {
 
97
            mChannelReceivedQueue.enqueue(qMakePair(userActionTime, channelRequest->hints()));
 
98
        } else {
 
99
            emit channelReceived(oldChannel, userActionTime, channelRequest->hints());
 
100
        }
 
101
    }
 
102
 
 
103
    context->setFinished();
 
104
}
 
105
 
 
106
void RequestTemporaryHandler::setQueueChannelReceived(bool queue)
 
107
{
 
108
    mQueueChannelReceived = queue;
 
109
    if (!queue) {
 
110
        processChannelReceivedQueue();
 
111
    }
 
112
}
 
113
 
 
114
void RequestTemporaryHandler::setDBusHandlerInvoked()
 
115
{
 
116
    dbusHandlerInvoked = true;
 
117
}
 
118
 
 
119
void RequestTemporaryHandler::setDBusHandlerErrored(const QString &errorName, const QString &errorMessage)
 
120
{
 
121
    Q_ASSERT(dbusHandlerInvoked);
 
122
    if (!channel()) {
 
123
        emit error(errorName, errorMessage);
 
124
    }
 
125
}
 
126
 
 
127
void RequestTemporaryHandler::processChannelReceivedQueue()
 
128
{
 
129
    while (!mChannelReceivedQueue.isEmpty()) {
 
130
        QPair<QDateTime, ChannelRequestHints> info = mChannelReceivedQueue.dequeue();
 
131
        emit channelReceived(channel(), info.first, info.second);
 
132
    }
 
133
}
 
134
 
 
135
} // Tp