conversation.cpp 2.64 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
#include "channel-delegator.h"

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

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

46 47
    d->account = account;

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

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

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

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

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

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

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

Martin Klapetek's avatar
Martin Klapetek committed
79
void Conversation::onChannelInvalidated(Tp::DBusProxy *proxy, const QString &errorName, const QString &errorMessage)
80 81 82 83 84 85 86 87
{
    kDebug() << proxy << errorName << ":" << errorMessage;

    d->valid = false;

    Q_EMIT validityChanged(d->valid);
}

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

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

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