conversation.cpp 3.02 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 35 36
    //stores if the conversation has been delegated to another client and we are only observing the channel
    //and not handling it.
    bool delegated;
37
    bool valid;
38
    Tp::AccountPtr account;
39 40
};

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

49 50
    d->account = account;

51
    d->messages = new MessagesModel(account, this);
52
    d->messages->setTextChannel(channel);
53

54
    d->target = new ConversationTarget(account, KTp::ContactPtr::qObjectCast(channel->targetContact()), this);
55 56

    d->valid = channel->isValid();
57 58
    d->delegated = false;

59
    connect(channel.data(), SIGNAL(invalidated(Tp::DBusProxy*,QString,QString)),
60
            SLOT(onChannelInvalidated(Tp::DBusProxy*,QString,QString)));
Lasath Fernando's avatar
Lasath Fernando committed
61 62
}

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

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

74
ConversationTarget* Conversation::target() const
75
{
76
    return d->target;
77 78
}

79 80 81 82 83
bool Conversation::isValid()
{
    return d->valid;
}

Martin Klapetek's avatar
Martin Klapetek committed
84
void Conversation::onChannelInvalidated(Tp::DBusProxy *proxy, const QString &errorName, const QString &errorMessage)
85 86 87 88 89 90 91 92
{
    kDebug() << proxy << errorName << ":" << errorMessage;

    d->valid = false;

    Q_EMIT validityChanged(d->valid);
}

93 94 95
void Conversation::delegateToProperClient()
{
    ChannelDelegator::delegateChannel(d->account, d->messages->textChannel());
96
    d->delegated = true;
97
    Q_EMIT conversationDelegated();
98 99
}

100 101 102
void Conversation::requestClose()
{
    kDebug();
103 104 105 106
    //if we are not handling the channel do nothing.
    if (!d->delegated) {
        d->messages->textChannel()->requestClose();
    }
107 108
}

109 110
Conversation::~Conversation()
{
Lasath Fernando's avatar
Lasath Fernando committed
111
    kDebug();
112
    requestClose();
113
    delete d;
114
}