MessageViewer.cpp Example File
messages/src/MessageViewer.cpp
#include "MessageViewer.hpp"
#include <bb/pim/account/Account>
#include <bb/pim/message/Message>
using namespace bb::pim::account;
using namespace bb::pim::message;
MessageViewer::MessageViewer(MessageService *service, QObject *parent)
: QObject(parent)
, m_messageService(service)
, m_messageId(-1)
, m_accountId(-1)
{
bool ok = connect(m_messageService, SIGNAL(messageUpdated(bb::pim::account::AccountKey, bb::pim::message::ConversationKey, bb::pim::message::MessageKey, bb::pim::message::MessageUpdate)),
SLOT(messageUpdated(bb::pim::account::AccountKey, bb::pim::message::ConversationKey, bb::pim::message::MessageKey, bb::pim::message::MessageUpdate)));
Q_ASSERT(ok);
Q_UNUSED(ok);
}
void MessageViewer::updateMessage()
{
const QString oldSubject = m_subject;
const QString oldSender = m_sender;
const QDateTime oldTime = m_time;
const QString oldBody = m_body;
const Message message = m_messageService->message(m_accountId, m_messageId);
m_subject = message.subject();
m_sender = message.sender().displayableName();
m_time = message.serverTimestamp();
m_body = message.body(MessageBody::PlainText).plainText();
if (m_body.isEmpty())
m_body = message.body(MessageBody::Html).plainText();
if (oldSubject != m_subject)
emit subjectChanged();
if (oldSender != m_sender)
emit senderChanged();
if (oldTime != m_time)
emit timeChanged();
if (oldBody != m_body)
emit bodyChanged();
}
void MessageViewer::messageUpdated(bb::pim::account::AccountKey accountId, bb::pim::message::ConversationKey, bb::pim::message::MessageKey messageId, const bb::pim::message::MessageUpdate&)
{
if (m_accountId == accountId && m_messageId == messageId)
updateMessage();
}
void MessageViewer::setMessage(AccountKey accountId, MessageKey messageId)
{
if (m_accountId != accountId || m_messageId != messageId) {
m_accountId = accountId;
m_messageId = messageId;
updateMessage();
}
}
QString MessageViewer::subject() const
{
return m_subject;
}
QString MessageViewer::senderContact() const
{
return m_sender;
}
QString MessageViewer::time() const
{
if (!m_time.isValid())
return tr("n/a");
else
return m_time.toString();
}
QString MessageViewer::body() const
{
return m_body;
}