conversation.cpp 2.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
    Copyright (C) 2011  Lasath Fernando <kde@lasath.org>

    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 Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


#include "conversation.h"
21
#include "messages-model.h"
22

David Edmundson's avatar
David Edmundson committed
23
#include <TelepathyQt/TextChannel>
Lasath Fernando's avatar
Lasath Fernando committed
24
#include <KDebug>
25
#include "conversation-target.h"
26

27 28 29
#include "channel-delegator.h"


Lasath Fernando's avatar
Lasath Fernando committed
30 31
class Conversation::ConversationPrivate
{
32
public:
33
    MessagesModel *messages;
34
    ConversationTarget *target;
35
    bool valid;
36
    Tp::AccountPtr account;
37 38
};

39 40 41
Conversation::Conversation(const Tp::TextChannelPtr& channel,
                           const Tp::AccountPtr& account,
                           QObject *parent) :
42
        QObject(parent),
43
        d (new ConversationPrivate)
44
{
Lasath Fernando's avatar
Lasath Fernando committed
45 46
    kDebug();

47 48
    d->account = account;

49 50
    d->messages = new MessagesModel(this);
    d->messages->setTextChannel(channel);
51

52
    d->target = new ConversationTarget(channel->targetContact(), this);
53 54 55

    d->valid = channel->isValid();
    connect(channel.data(), SIGNAL(invalidated(Tp::DBusProxy*,QString,QString)),
56
            SLOT(onChannelInvalidated(Tp::DBusProxy*,QString,QString)));
Lasath Fernando's avatar
Lasath Fernando committed
57 58
}

59
Conversation::Conversation(QObject *parent) : QObject(parent)
Lasath Fernando's avatar
Lasath Fernando committed
60
{
61
    kError() << "Conversation should not be created directly. Use ConversationWatcher instead.";
62
    Q_ASSERT(false);
63 64
}

65
MessagesModel* Conversation::messages() const
66
{
67
    return d->messages;
68 69
}

70
ConversationTarget* Conversation::target() const
71
{
72
    return d->target;
73 74
}

75 76 77 78 79
bool Conversation::isValid()
{
    return d->valid;
}

80
void Conversation::onChannelInvalidated(Tp::DBusProxy* proxy, const QString& errorName, const QString& errorMessage)
81 82 83 84 85 86 87 88
{
    kDebug() << proxy << errorName << ":" << errorMessage;

    d->valid = false;

    Q_EMIT validityChanged(d->valid);
}

89 90 91 92 93
void Conversation::delegateToProperClient()
{
    ChannelDelegator::delegateChannel(d->account, d->messages->textChannel());
}

94 95 96
void Conversation::requestClose()
{
    kDebug();
97
    d->messages->textChannel()->requestClose();
98 99
}

100 101
Conversation::~Conversation()
{
Lasath Fernando's avatar
Lasath Fernando committed
102
    kDebug();
103
    delete d;
104
}
105