summaryrefslogtreecommitdiff
path: root/qt4/examples/file-transfer/file-receiver-handler.cpp
blob: 15e5ca937d15f8373a39f63473927e5228d0af48 (plain)
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
/*
 * This file is part of TelepathyQt4
 *
 * Copyright (C) 2011 Collabora Ltd. <http://www.collabora.co.uk/>
 * Copyright (C) 2011 Nokia Corporation
 *
 * 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 "file-receiver-handler.h"
#include "_gen/file-receiver-handler.moc.hpp"

#include "pending-file-receive.h"

#include <TelepathyQt4/Channel>
#include <TelepathyQt4/ChannelClassSpec>
#include <TelepathyQt4/ChannelClassSpecList>
#include <TelepathyQt4/ChannelRequest>
#include <TelepathyQt4/Connection>
#include <TelepathyQt4/MethodInvocationContext>
#include <TelepathyQt4/IncomingFileTransferChannel>

#include <QDateTime>
#include <QDebug>

FileReceiverHandler::FileReceiverHandler()
    : QObject(),
      AbstractClientHandler(ChannelClassSpecList() << ChannelClassSpec::incomingFileTransfer(),
              AbstractClientHandler::Capabilities(), false)
{
}

FileReceiverHandler::~FileReceiverHandler()
{
}

bool FileReceiverHandler::bypassApproval() const
{
    return false;
}

void FileReceiverHandler::handleChannels(const MethodInvocationContextPtr<> &context,
        const AccountPtr &account,
        const ConnectionPtr &connection,
        const QList<ChannelPtr> &channels,
        const QList<ChannelRequestPtr> &requestsSatisfied,
        const QDateTime &userActionTime,
        const HandlerInfo &handlerInfo)
{
    // We should always receive one channel to handle,
    // otherwise either MC or tp-qt4 itself is bogus, so let's assert in case they are
    Q_ASSERT(channels.size() == 1);
    ChannelPtr chan = channels.first();

    if (!chan->isValid()) {
        qWarning() << "Channel received to handle is invalid, ignoring channel";
        context->setFinishedWithError(TP_QT4_ERROR_INVALID_ARGUMENT,
                QLatin1String("Channel received to handle is invalid"));
        return;
    }

    // We should always receive incoming channels of type FileTransfer, as set by our filter,
    // otherwise either MC or tp-qt4 itself is bogus, so let's assert in case they are
    Q_ASSERT(chan->channelType() == TP_QT4_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
    Q_ASSERT(!chan->isRequested());

    IncomingFileTransferChannelPtr transferChannel = IncomingFileTransferChannelPtr::qObjectCast(chan);
    Q_ASSERT(transferChannel);

    context->setFinished();

    PendingFileReceive *receiveOperation = new PendingFileReceive(transferChannel,
            SharedPtr<RefCounted>::dynamicCast(AbstractClientPtr(this)));
    connect(receiveOperation,
            SIGNAL(finished(Tp::PendingOperation*)),
            SLOT(onReceiveFinished(Tp::PendingOperation*)));
}

void FileReceiverHandler::onReceiveFinished(PendingOperation *op)
{
    PendingFileReceive *receiveOperation = qobject_cast<PendingFileReceive*>(op);
    qDebug() << "Closing channel";
    receiveOperation->channel()->requestClose();
}