Commit 4e2e6daf authored by Lasath Fernando's avatar Lasath Fernando Committed by David Edmundson
Browse files

Further work on chat plasmoid

parent 5e1d7f95
......@@ -22,6 +22,7 @@
#include <TelepathyQt4/AvatarData>
#include <TelepathyQt4/Presence>
#include <KDebug>
#include <KIconLoader>
class ConversationTarget::ConversationTargetPrivate
{
......@@ -50,8 +51,17 @@ void ConversationTarget::setupContactSignals(Tp::ContactPtr contact)
QIcon ConversationTarget::avatar() const
{
//FIXME: return KIcon("im-user") if avatar is unavailable
return QIcon(d->contact->avatarData().fileName);
QString path = d->contact->avatarData().fileName;
if(path.isEmpty()) {
return KIcon(QLatin1String("im-user"));
} else {
return QIcon(path);
}
}
QString ConversationTarget::id() const
{
return d->contact->id();
}
QString ConversationTarget::nick() const
......
......@@ -36,6 +36,7 @@ Q_PROPERTY(QIcon avatar READ avatar NOTIFY avatarChanged);
Q_PROPERTY(QString nick READ nick NOTIFY nickChanged);
Q_PROPERTY(QIcon presenceIcon READ presenceIcon NOTIFY presenceIconChanged);
Q_PROPERTY(QString presenceIconSource READ presenceIconSource NOTIFY presenceIconSourceChanged);
Q_PROPERTY(QString id READ id)
//turns out you can't have non QObjects as properties
// Q_PROPERTY(Tp::ContactPtr contact READ contact WRITE setContact NOTIFY contactChanged);
......@@ -45,6 +46,7 @@ public:
virtual ~ConversationTarget();
QIcon avatar() const;
QString id() const;
QString nick() const;
QIcon presenceIcon() const;
QString presenceIconSource() const;
......
......@@ -27,7 +27,6 @@
#include <TelepathyQt/TextChannel>
// #include "conversation-model.h"
#include <KIcon>
#include "conversation-target.h"
class ConversationTarget;
class MessagesModel;
......@@ -38,12 +37,12 @@ Q_OBJECT
Q_PROPERTY(MessagesModel* model READ model NOTIFY modelChanged);
Q_PROPERTY(ConversationTarget* target READ target NOTIFY targetChanged);
public:
Conversation(Tp::TextChannelPtr channel, Tp::AccountPtr account);
Conversation(QObject* parent = 0);
virtual ~Conversation();
//FIXME: rename model to messages
MessagesModel* model() const;
ConversationTarget* target() const;
......
......@@ -21,6 +21,8 @@
#include "conversation.h"
#include "telepathy-text-observer.h"
#include <KDebug>
#include "conversation-target.h"
#include "messages-model.h"
class ConversationsModel::ConversationsModelPrivate
{
......@@ -57,10 +59,19 @@ ConversationsModel::ConversationsModel() :
void ConversationsModel::onInconmingConversation ( Conversation* convo )
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
bool found = false;
Q_FOREACH(Conversation *con, d->data) {
if(con->target()->id() == convo->target()->id()) {
con->model()->setTextChannel(convo->model()->textChannel());
found = true;
}
}
d->data.append(convo);
endInsertRows();
if(!found) {
beginInsertRows(QModelIndex(), rowCount(), rowCount());
d->data.append(convo);
endInsertRows();
}
}
ConversationsModel::~ConversationsModel()
......
......@@ -27,6 +27,7 @@ public:
QString user;
QString text;
QDateTime time;
QString id;
//FIXME : replace with Tp::ChannelTextMessageType
enum MessageType {
......@@ -34,12 +35,21 @@ public:
Outgoing,
Status
} type;
MessageItem(QString user, QString text, QDateTime time, MessageType type, QString messageId)
: user(user), text(text), time(time), type(type), id(messageId)
{
if(this->text.endsWith(QLatin1String("\n"))) {
this->text.chop(1);
}
}
};
class MessagesModel::ConversationModelPrivate {
public:
Tp::TextChannelPtr textChannel;
QList<MessageItem> messages;
bool visible;
};
MessagesModel::MessagesModel(QObject* parent):
......@@ -54,6 +64,8 @@ MessagesModel::MessagesModel(QObject* parent):
roles[TimeRole] = "time";
roles[TypeRole] = "type";
setRoleNames(roles);
d->visible = false;
}
Tp::TextChannelPtr MessagesModel::textChannel()
......@@ -63,11 +75,11 @@ Tp::TextChannelPtr MessagesModel::textChannel()
bool MessagesModel::verifyPendingOperation ( Tp::PendingOperation* op )
{
bool success = !op->isError();
if(!success) {
if(op->isError()) {
kWarning() << op->errorName() << "+" << op->errorMessage();
return false;
}
return success;
return true;
}
void MessagesModel::setupChannelSignals(Tp::TextChannelPtr channel)
......@@ -91,41 +103,68 @@ void MessagesModel::setTextChannel(Tp::TextChannelPtr channel)
//FIXME: check messageQue for any lost messages
d->textChannel = channel;
textChannelChanged(channel);
Q_EMIT textChannelChanged(channel);
QList<Tp::ReceivedMessage> que = channel->messageQueue();
Q_FOREACH(Tp::ReceivedMessage message, que) {
bool messageAlreadyInModel = false;
Q_FOREACH(MessageItem current, d->messages) {
if(current.id == message.messageToken()) {
messageAlreadyInModel = true;
}
}
if(!messageAlreadyInModel) {
onMessageReceived(message);
}
}
}
void MessagesModel::onMessageReceived(Tp::ReceivedMessage message)
{
kDebug() << "unreadMessagesCount = " << d->textChannel->messageQueue().size();
int length = rowCount();
beginInsertRows(QModelIndex(), length, length);
MessageItem newMessage = {
message.sender()->alias(),
message.text(),
message.received(),
MessageItem::Incoming
};
int unreadCount = d->textChannel->messageQueue().size();
kDebug() << "unreadMessagesCount =" << unreadCount;
kDebug() << "text =" << message.text();
if(message.messageType() == Tp::ChannelTextMessageTypeNormal) {
int length = rowCount();
beginInsertRows(QModelIndex(), length, length);
d->messages.append(MessageItem(
message.sender()->alias(),
message.text(),
message.received(),
MessageItem::Incoming,
message.messageToken()
));
endInsertRows();
if(d->visible) {
acknowledgeAllMessages();
} else {
Q_EMIT unreadCountChanged(unreadCount);
}
}
d->messages.append(newMessage);
endInsertRows();
}
void MessagesModel::onMessageSent(Tp::Message message, Tp::MessageSendingFlags flags, QString token)
{
Q_UNUSED(flags);
Q_UNUSED(token);
int length = rowCount();
beginInsertRows(QModelIndex(), length, length);
kDebug() << "text =" << message.text();
MessageItem newMessage = {
d->messages.append(MessageItem(
tr("Me"), //FIXME : use actual nickname from Tp::AccountPtr
message.text(),
message.sent(),
MessageItem::Outgoing
};
MessageItem::Outgoing,
message.messageToken()
));
d->messages.append(newMessage);
endInsertRows();
}
......@@ -192,10 +231,51 @@ void MessagesModel::removeChannelSignals(Tp::TextChannelPtr channel)
);
}
int MessagesModel::unreadCount() const
{
return d->textChannel->messageQueue().size();
}
void MessagesModel::acknowledgeAllMessages()
{
QList<Tp::ReceivedMessage> que = d->textChannel->messageQueue();
kDebug() << "Conversation Visible, Acknowledging " << que.size() << " messages.";
d->textChannel->acknowledge(que);
Q_EMIT unreadCountChanged(que.size());
}
void MessagesModel::setVisibleToUser(bool visible)
{
kDebug() << visible;
if(d->visible != visible) {
d->visible = visible;
Q_EMIT visibleToUserChanged(visible);
}
if(visible) {
acknowledgeAllMessages();
}
}
bool MessagesModel::isVisibleToUser() const
{
return d->visible;
}
MessagesModel::~MessagesModel()
{
kDebug();
delete d;
}
void MessagesModel::printallmessages()
{
Q_FOREACH(MessageItem msg, d->messages) {
kDebug() << msg.text;
}
}
#include "moc_messages-model.cpp"
......@@ -29,6 +29,9 @@
class KDE_TELEPATHY_CHAT_EXPORT MessagesModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(bool visibleToUser READ isVisibleToUser WRITE setVisibleToUser NOTIFY visibleToUserChanged);
Q_PROPERTY(int unreadCount READ unreadCount NOTIFY unreadCountChanged);
//turns out you can't have a non QObject as a property
// Q_PROPERTY(Tp::TextChannelPtr textChannel
// READ textChannel
// WRITE setTextChannel
......@@ -52,8 +55,17 @@ public:
Tp::TextChannelPtr textChannel();
void setTextChannel(Tp::TextChannelPtr channel);
bool isVisibleToUser() const;
void setVisibleToUser(bool visible);
void acknowledgeAllMessages();
int unreadCount() const;
Q_INVOKABLE void printallmessages();
Q_SIGNALS:
void textChannelChanged(Tp::TextChannelPtr newChannel);
void textChannelChanged(Tp::TextChannelPtr newChannel);
void visibleToUserChanged(bool visible);
void unreadCountChanged(int unreadMesssagesCount);
public Q_SLOTS:
Tp::PendingSendMessage* sendNewMessage(QString message);
......
......@@ -67,6 +67,7 @@ public:
AbstractClientObserver(channelClassList()),
m_parent(parent)
{
}
TelepathyTextObserver *m_parent;
......@@ -79,42 +80,38 @@ TelepathyTextObserver::TelepathyTextObserver() :
kDebug();
Tp::registerTypes();
Tp::AccountFactoryPtr accountFactory = Tp::AccountFactory::create(QDBusConnection::sessionBus(),
Tp::Account::FeatureCore);
Tp::Account::FeatureCore);
Tp::ConnectionFactoryPtr connectionFactory = Tp::ConnectionFactory::create(
QDBusConnection::sessionBus(),
Tp::Features() << Tp::Connection::FeatureSelfContact
<< Tp::Connection::FeatureCore
<< Tp::Connection::FeatureCore
);
Tp::ChannelFactoryPtr channelFactory = Tp::ChannelFactory::create(QDBusConnection::sessionBus());
channelFactory->addCommonFeatures(Tp::Channel::FeatureCore);
Tp::Features textFeatures = Tp::Features() << Tp::TextChannel::FeatureMessageQueue
<< Tp::TextChannel::FeatureMessageSentSignal
<< Tp::TextChannel::FeatureChatState
<< Tp::TextChannel::FeatureMessageCapabilities;
<< Tp::TextChannel::FeatureMessageSentSignal
<< Tp::TextChannel::FeatureChatState
<< Tp::TextChannel::FeatureMessageCapabilities;
channelFactory->addFeaturesForTextChats(textFeatures);
channelFactory->addFeaturesForTextChatrooms(textFeatures);
Tp::ContactFactoryPtr contactFactory = Tp::ContactFactory::create(
Tp::Features() << Tp::Contact::FeatureAlias
<< Tp::Contact::FeatureAvatarToken
<< Tp::Contact::FeatureAvatarData
<< Tp::Contact::FeatureCapabilities
<< Tp::Contact::FeatureSimplePresence
<< Tp::Contact::FeatureAvatarToken
<< Tp::Contact::FeatureAvatarData
<< Tp::Contact::FeatureCapabilities
<< Tp::Contact::FeatureSimplePresence
);
//TODO: check these to make sure I'm only requesting features I actually use.
d->registrar = Tp::ClientRegistrar::create(accountFactory, connectionFactory,
channelFactory, contactFactory);
channelFactory, contactFactory);
d->registrar->registerClient(d, QLatin1String("KDE.TextUi.ConversationWatcher"));
}
TelepathyTextObserver::~TelepathyTextObserver()
{
}
// #include "moc_conversation-watcher.cpp"
}
\ No newline at end of file
......@@ -38,7 +38,7 @@ Q_SIGNALS:
void newConversation(Conversation *con);
private:
class ConversationClientObserver;
Tp::SharedPtr<ConversationClientObserver> d;
Tp::SharedPtr<ConversationClientObserver> d /*= Tp::SharedPtr<ConversationClientObserver>()*/;
};
#endif // CONVERSATION_WATCHER_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment