<commit_before>/*
 * This file is part of TelepathyQt4
 *
 * Copyright (C) 2009 Collabora Ltd. <http://www.collabora.co.uk/>
 * Copyright (C) 2009 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 <TelepathyQt4/ClientRegistrar>
#include "TelepathyQt4/client-registrar-internal.h"

#include "TelepathyQt4/_gen/client-registrar.moc.hpp"
#include "TelepathyQt4/_gen/client-registrar-internal.moc.hpp"

#include "TelepathyQt4/channel-factory.h"
#include "TelepathyQt4/debug-internal.h"

#include <TelepathyQt4/Account>
#include <TelepathyQt4/Channel>
#include <TelepathyQt4/ChannelDispatchOperation>
#include <TelepathyQt4/ChannelRequest>
#include <TelepathyQt4/Connection>
#include <TelepathyQt4/MethodInvocationContext>
#include <TelepathyQt4/PendingReady>

namespace Tp
{

class HandleChannelsInvocationContext : public MethodInvocationContext<>
{
    Q_DISABLE_COPY(HandleChannelsInvocationContext)

public:
    typedef void (*FinishedCb)(const MethodInvocationContextPtr<> &context,
                               const QList<ChannelPtr> &channels,
                               void *data);

    static MethodInvocationContextPtr<> create(const QDBusConnection &bus,
            const QDBusMessage &message, const QList<ChannelPtr> &channels,
            FinishedCb finishedCb, void *finishedCbData)
    {
        return MethodInvocationContextPtr<>::dynamicCast(
                SharedPtr<HandleChannelsInvocationContext>(
                    new HandleChannelsInvocationContext(bus, message, channels,
                        finishedCb, finishedCbData)));
    }

private:
    HandleChannelsInvocationContext(const QDBusConnection &connection,
            const QDBusMessage &message, const QList<ChannelPtr> &channels,
            FinishedCb finishedCb, void *finishedCbData)
        : MethodInvocationContext<>(connection, message),
          mChannels(channels),
          mFinishedCb(finishedCb),
          mFinishedCbData(finishedCbData)
    {
    }

    void onFinished()
    {
        if (mFinishedCb) {
            mFinishedCb(MethodInvocationContextPtr<>(this), mChannels, mFinishedCbData);
        }
    }

    QList<ChannelPtr> mChannels;
    FinishedCb mFinishedCb;
    void *mFinishedCbData;
};

ClientAdaptor::ClientAdaptor(const QStringList &interfaces,
        QObject *parent)
    : QDBusAbstractAdaptor(parent),
      mInterfaces(interfaces)
{
}

ClientAdaptor::~ClientAdaptor()
{
}

ClientObserverAdaptor::ClientObserverAdaptor(const QDBusConnection &bus,
        AbstractClientObserver *client,
        QObject *parent)
    : QDBusAbstractAdaptor(parent),
      mBus(bus),
      mClient(client)
{
}

ClientObserverAdaptor::~ClientObserverAdaptor()
{
}

void ClientObserverAdaptor::ObserveChannels(const QDBusObjectPath &accountPath,
        const QDBusObjectPath &connectionPath,
        const Tp::ChannelDetailsList &channelDetailsList,
        const QDBusObjectPath &dispatchOperationPath,
        const Tp::ObjectPathList &requestsSatisfied,
        const QVariantMap &observerInfo,
        const QDBusMessage &message)
{
    debug() << "ObserveChannels: account:" << accountPath.path() <<
        ", connection:" << connectionPath.path();

    AccountPtr account = Account::create(mBus,
            TELEPATHY_ACCOUNT_MANAGER_BUS_NAME,
            accountPath.path());

    QString connectionBusName = connectionPath.path().mid(1).replace('/', '.');
    ConnectionPtr connection = Connection::create(mBus, connectionBusName,
            connectionPath.path());

    QList<ChannelPtr> channels;
    ChannelPtr channel;
    foreach (const ChannelDetails &channelDetails, channelDetailsList) {
        channel = ChannelFactory::create(connection, channelDetails.channel.path(),
                channelDetails.properties);
        channels.append(channel);
    }

    ChannelDispatchOperationPtr channelDispatchOperation =
        ChannelDispatchOperation::create(mBus,
                dispatchOperationPath.path(), QVariantMap());

    QList<ChannelRequestPtr> channelRequests;
    ChannelRequestPtr channelRequest;
    foreach (const QDBusObjectPath &path, requestsSatisfied) {
        channelRequest = ChannelRequest::create(mBus,
                path.path(), QVariantMap());
        channelRequests.append(channelRequest);
    }

    MethodInvocationContextPtr<> context =
        MethodInvocationContextPtr<>(
                new MethodInvocationContext<>(mBus, message));

    mClient->observeChannels(context, account, connection, channels,
            channelDispatchOperation, channelRequests, observerInfo);
}

ClientApproverAdaptor::ClientApproverAdaptor(const QDBusConnection &bus,
        AbstractClientApprover *client,
        QObject *parent)
    : QDBusAbstractAdaptor(parent),
      mBus(bus),
      mClient(client)
{
}

ClientApproverAdaptor::~ClientApproverAdaptor()
{
}

void ClientApproverAdaptor::AddDispatchOperation(const Tp::ChannelDetailsList &channelDetailsList,
        const QDBusObjectPath &dispatchOperationPath,
        const QVariantMap &properties,
        const QDBusMessage &message)
{
    QDBusObjectPath connectionPath = qdbus_cast<QDBusObjectPath>(
            properties.value("Connection"));
    debug() << "addDispatchOperation: connection:" << connectionPath.path();
    QString connectionBusName = connectionPath.path().mid(1).replace('/', '.');
    ConnectionPtr connection = Connection::create(mBus, connectionBusName,
            connectionPath.path());

    QList<ChannelPtr> channels;
    ChannelPtr channel;
    foreach (const ChannelDetails &channelDetails, channelDetailsList) {
        channel = Channel::create(connection, channelDetails.channel.path(),
                channelDetails.properties);
        channels.append(channel);
    }

    ChannelDispatchOperationPtr channelDispatchOperation =<commit_msg>ClientApproverAdaptor: Use the dbus qualified name to get the connection property.<commit_after>/*
 * This file is part of TelepathyQt4
 *
 * Copyright (C) 2009 Collabora Ltd. <http://www.collabora.co.uk/>
 * Copyright (C) 2009 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 <TelepathyQt4/ClientRegistrar>
#include "TelepathyQt4/client-registrar-internal.h"

#include "TelepathyQt4/_gen/client-registrar.moc.hpp"
#include "TelepathyQt4/_gen/client-registrar-internal.moc.hpp"

#include "TelepathyQt4/channel-factory.h"
#include "TelepathyQt4/debug-internal.h"

#include <TelepathyQt4/Account>
#include <TelepathyQt4/Channel>
#include <TelepathyQt4/ChannelDispatchOperation>
#include <TelepathyQt4/ChannelRequest>
#include <TelepathyQt4/Connection>
#include <TelepathyQt4/MethodInvocationContext>
#include <TelepathyQt4/PendingReady>

namespace Tp
{

class HandleChannelsInvocationContext : public MethodInvocationContext<>
{
    Q_DISABLE_COPY(HandleChannelsInvocationContext)

public:
    typedef void (*FinishedCb)(const MethodInvocationContextPtr<> &context,
                               const QList<ChannelPtr> &channels,
                               void *data);

    static MethodInvocationContextPtr<> create(const QDBusConnection &bus,
            const QDBusMessage &message, const QList<ChannelPtr> &channels,
            FinishedCb finishedCb, void *finishedCbData)
    {
        return MethodInvocationContextPtr<>::dynamicCast(
                SharedPtr<HandleChannelsInvocationContext>(
                    new HandleChannelsInvocationContext(bus, message, channels,
                        finishedCb, finishedCbData)));
    }

private:
    HandleChannelsInvocationContext(const QDBusConnection &connection,
            const QDBusMessage &message, const QList<ChannelPtr> &channels,
            FinishedCb finishedCb, void *finishedCbData)
        : MethodInvocationContext<>(connection, message),
          mChannels(channels),
          mFinishedCb(finishedCb),
          mFinishedCbData(finishedCbData)
    {
    }

    void onFinished()
    {
        if (mFinishedCb) {
            mFinishedCb(MethodInvocationContextPtr<>(this), mChannels, mFinishedCbData);
        }
    }

    QList<ChannelPtr> mChannels;
    FinishedCb mFinishedCb;
    void *mFinishedCbData;
};

ClientAdaptor::ClientAdaptor(const QStringList &interfaces,
        QObject *parent)
    : QDBusAbstractAdaptor(parent),
      mInterfaces(interfaces)
{
}

ClientAdaptor::~ClientAdaptor()
{
}

ClientObserverAdaptor::ClientObserverAdaptor(const QDBusConnection &bus,
        AbstractClientObserver *client,
        QObject *parent)
    : QDBusAbstractAdaptor(parent),
      mBus(bus),
      mClient(client)
{
}

ClientObserverAdaptor::~ClientObserverAdaptor()
{
}

void ClientObserverAdaptor::ObserveChannels(const QDBusObjectPath &accountPath,
        const QDBusObjectPath &connectionPath,
        const Tp::ChannelDetailsList &channelDetailsList,
        const QDBusObjectPath &dispatchOperationPath,
        const Tp::ObjectPathList &requestsSatisfied,
        const QVariantMap &observerInfo,
        const QDBusMessage &message)
{
    debug() << "ObserveChannels: account:" << accountPath.path() <<
        ", connection:" << connectionPath.path();

    AccountPtr account = Account::create(mBus,
            TELEPATHY_ACCOUNT_MANAGER_BUS_NAME,
            accountPath.path());

    QString connectionBusName = connectionPath.path().mid(1).replace('/', '.');
    ConnectionPtr connection = Connection::create(mBus, connectionBusName,
            connectionPath.path());

    QList<ChannelPtr> channels;
    ChannelPtr channel;
    foreach (const ChannelDetails &channelDetails, channelDetailsList) {
        channel = ChannelFactory::create(connection, channelDetails.channel.path(),
                channelDetails.properties);
        channels.append(channel);
    }

    ChannelDispatchOperationPtr channelDispatchOperation =
        ChannelDispatchOperation::create(mBus,
                dispatchOperationPath.path(), QVariantMap());

    QList<ChannelRequestPtr> channelRequests;
    ChannelRequestPtr channelRequest;
    foreach (const QDBusObjectPath &path, requestsSatisfied) {
        channelRequest = ChannelRequest::create(mBus,
                path.path(), QVariantMap());
        channelRequests.append(channelRequest);
    }

    MethodInvocationContextPtr<> context =
        MethodInvocationContextPtr<>(
                new MethodInvocationContext<>(mBus, message));

    mClient->observeChannels(context, account, connection, channels,
            channelDispatchOperation, channelRequests, observerInfo);
}

ClientApproverAdaptor::ClientApproverAdaptor(const QDBusConnection &bus,
        AbstractClientApprover *client,
        QObject *parent)
    : QDBusAbstractAdaptor(parent),
      mBus(bus),
      mClient(client)
{
}

ClientApproverAdaptor::~ClientApproverAdaptor()
{
}

void ClientApproverAdaptor::AddDispatchOperation(const Tp::ChannelDetailsList &channelDetailsList,
        const QDBusObjectPath &dispatchOperationPath,
        const QVariantMap &properties,
        const QDBusMessage &message)
{
    QDBusObjectPath connectionPath = qdbus_cast<QDBusObjectPath>(
            properties.value(TELEPATHY_INTERFACE_CHANNEL_DISPATCH_OPERATION ".Connection"));
    debug() << "addDispatchOperation: connection:" << connectionPath.path();
    QString connectionBusName = connectionPath.path().mid(1).replace('/', '.');
    ConnectionPtr connection = Connection::create(mBus, connectionBusName,
            connectionPath.path());

    QList<ChannelPtr> channels;
    ChannelPtr channel;
    foreach (const ChannelDetails &channelDetails, channelDetailsList) {
        channel = Channel::create(connection, channelDetails.channel.path(),
                channelDetails.properties);
        channels.append(channel);
    }

    ChannelDispatchOperationPtr channelDispatchOperation =
{'fraction_non_alphanumeric': 0.09005100098957144, 'fraction_numerical': 0.005632945116845551, 'mean_word_length': 3.7705156136528686, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3846724', 'n_tokens_mistral': 3625, 'n_tokens_neox': 3566, 'n_words': 803}