conversation.cpp 3.35 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
    setTextChannel(channel);
53
    d->target = new ConversationTarget(account, KTp::ContactPtr::qObjectCast(channel->targetContact()), this);
54

55 56
    d->delegated = false;

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 66 67 68 69 70 71 72 73 74 75 76 77 78
void Conversation::setTextChannel(const Tp::TextChannelPtr& channel)
{
    d->messages->setTextChannel(channel);
    d->valid = channel->isValid();
    connect(channel.data(), SIGNAL(invalidated(Tp::DBusProxy*,QString,QString)),
            SLOT(onChannelInvalidated(Tp::DBusProxy*,QString,QString)));
    Q_EMIT validityChanged(d->valid);
}

Tp::TextChannelPtr Conversation::textChannel() const
{
    return d->messages->textChannel();
}

79
MessagesModel* Conversation::messages() const
80
{
81
    return d->messages;
82 83
}

84
ConversationTarget* Conversation::target() const
85
{
86
    return d->target;
87 88
}

89 90 91 92 93
bool Conversation::isValid()
{
    return d->valid;
}

Martin Klapetek's avatar
Martin Klapetek committed
94
void Conversation::onChannelInvalidated(Tp::DBusProxy *proxy, const QString &errorName, const QString &errorMessage)
95 96 97 98 99 100 101 102
{
    kDebug() << proxy << errorName << ":" << errorMessage;

    d->valid = false;

    Q_EMIT validityChanged(d->valid);
}

103 104 105
void Conversation::delegateToProperClient()
{
    ChannelDelegator::delegateChannel(d->account, d->messages->textChannel());
106
    d->delegated = true;
107
    Q_EMIT conversationCloseRequested();
108 109
}

110 111 112
void Conversation::requestClose()
{
    kDebug();
113 114 115

    //removing from the model will delete this object closing the channel
    Q_EMIT conversationCloseRequested();
116 117
}

118 119
Conversation::~Conversation()
{
Lasath Fernando's avatar
Lasath Fernando committed
120
    kDebug();
121 122 123 124
    //if we are not handling the channel do nothing.
    if (!d->delegated) {
        d->messages->textChannel()->requestClose();
    }
125
    delete d;
126
}