summaryrefslogtreecommitdiff
path: root/tubesmanager.cpp
blob: 770b13e92cd21ce4101f144648e1f3356c1edabd (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
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
/****************************************************************************
**
** Copyright (C) 2009 Collabora Ltd <http://www.collabora.co.uk>
** Copyright (C) 2009 Abner Silva <abner.silva@kdemail.net>
**
** This file is part of KDE.
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program 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 General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; see the file COPYING. If not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
** Boston, MA 02110-1301, USA.
**
****************************************************************************/

#include "tubesmanager.h"

#include <TelepathyQt4/Debug>

#include <KDebug>

using namespace Tp;

static inline Tp::ChannelClassList channelClassList()
{
    QMap<QString, QDBusVariant> filter0;
    filter0[TELEPATHY_INTERFACE_CHANNEL ".ChannelType"] =
            QDBusVariant(TELEPATHY_INTERFACE_CHANNEL_TYPE_STREAM_TUBE);
    filter0[TELEPATHY_INTERFACE_CHANNEL_TYPE_STREAM_TUBE ".Service"] = QDBusVariant("rfb");
    filter0[TELEPATHY_INTERFACE_CHANNEL ".Requested"] = QDBusVariant(false);

    return Tp::ChannelClassList() << Tp::ChannelClass(filter0);
}

TubesManager::TubesManager(QObject *parent)
    : QObject(parent),
      AbstractClientHandler(channelClassList(), false)
{
    kDebug() << "Initializing tubes manager";

    Tp::enableDebug(true);
    Tp::enableWarnings(true);

    /* Registering telepathy types */
    registerTypes();
}

TubesManager::~TubesManager()
{
    kDebug() << "Destroying tube manager";
}

bool TubesManager::bypassApproval() const
{
    kDebug() << "bypassing";
    return false;
}

void TubesManager::handleChannels(const Tp::MethodInvocationContextPtr<> & context,
        const Tp::AccountPtr & account,
        const Tp::ConnectionPtr & connection,
        const QList<Tp::ChannelPtr> & channels,
        const QList<Tp::ChannelRequestPtr> & requestsSatisfied,
        const QDateTime & userActionTime,
        const QVariantMap & handlerInfo)
{
    Q_UNUSED(account);
    Q_UNUSED(connection);
    Q_UNUSED(requestsSatisfied);
    Q_UNUSED(userActionTime);
    Q_UNUSED(handlerInfo);

    kDebug();

    foreach(const Tp::ChannelPtr &channel, channels) {
        kDebug() << "Handling new tube";

        QVariantMap properties = channel->immutableProperties();

        if (properties[TELEPATHY_INTERFACE_CHANNEL ".ChannelType"] ==
               TELEPATHY_INTERFACE_CHANNEL_TYPE_STREAM_TUBE) {

            kDebug() << "Handling:" << TELEPATHY_INTERFACE_CHANNEL_TYPE_STREAM_TUBE;

            /* New tube arrived */
            Tube *tube = new Tube(channel, this);

            /* Listening to tube status changed */
            connect(tube,
                    SIGNAL(statusChanged(Tube::Status)),
                    SLOT(onTubeStateChanged(Tube::Status)));

            m_tubes.append(tube);
        }
    }
    context->setFinished();
}

void TubesManager::closeTube(KUrl url)
{
    kDebug() << "Closing tube:" << url;

    foreach (Tube *tube, m_tubes)
        if (tube->url() == url) {
            m_tubes.removeOne(tube);
            tube->close();
        }
}

void TubesManager::onTubeStateChanged(Tube::Status status)
{
    kDebug() << "tube status changed:" << status;
    Tube *tube = qobject_cast<Tube *>(sender());

    if (status == Tube::Open) {
        KUrl url = tube->url();
        kDebug() << "newConnection:" << url;
        emit newConnection(url);
    }
    else if (status == Tube::Close) {
        kDebug() << "Tube:" << tube->url() << "closed";
        m_tubes.removeOne(tube);
        tube->deleteLater();
        kDebug() << m_tubes;
    }
}