MessageComposer.cpp Example File
messages/src/MessageComposer.cpp
#include "MessageComposer.hpp"
#include <bb/pim/account/Account>
#include <bb/pim/message/Message>
#include <bb/pim/message/MessageBuilder>
using namespace bb::pim::account;
using namespace bb::pim::message;
MessageComposer::MessageComposer(MessageService *service, QObject *parent)
: QObject(parent)
, m_messageService(service)
, m_messageId(-1)
, m_accountId(-1)
, m_mode(CreateMode)
{
}
void MessageComposer::setAccountId(AccountKey accountId)
{
m_accountId = accountId;
}
void MessageComposer::loadMessage(MessageKey messageId)
{
Q_ASSERT(m_accountId != -1);
m_messageId = messageId;
const Message message = m_messageService->message(m_accountId, m_messageId);
m_subject = message.subject();
m_recipient = message.sender().address();
m_body = message.body(MessageBody::PlainText).plainText();
if (m_body.isEmpty())
m_body = message.body(MessageBody::Html).plainText();
m_subject = QString::fromLatin1("Re: %1").arg(m_subject);
m_body.replace("\n", "\n> ");
m_body = QString::fromLatin1("> %1").arg(m_body);
emit subjectChanged();
emit recipientChanged();
emit bodyChanged();
}
void MessageComposer::composeMessage()
{
const MessageContact recipient = MessageContact(-1, MessageContact::To, QString(), m_recipient);
const QByteArray bodyData = m_body.toUtf8();
MessageBuilder *builder = (m_mode == CreateMode ? MessageBuilder::create(m_accountId)
: MessageBuilder::create(m_accountId, m_messageService->message(m_accountId, m_messageId)));
builder->subject(m_subject);
builder->removeAllRecipients();
builder->addRecipient(recipient);
builder->body(MessageBody::PlainText, bodyData);
m_messageService->send(m_accountId, *builder);
}
void MessageComposer::reset()
{
m_accountId = -1;
m_messageId = -1;
m_subject.clear();
m_recipient.clear();
m_body.clear();
emit subjectChanged();
emit recipientChanged();
emit bodyChanged();
}
void MessageComposer::setSubject(const QString &subject)
{
if (m_subject == subject)
return;
m_subject = subject;
emit subjectChanged();
}
QString MessageComposer::subject() const
{
return m_subject;
}
void MessageComposer::setRecipient(const QString &recipient)
{
if (m_recipient == recipient)
return;
m_recipient = recipient;
emit recipientChanged();
}
QString MessageComposer::recipient() const
{
return m_recipient;
}
void MessageComposer::setBody(const QString &body)
{
if (m_body == body)
return;
m_body = body;
emit bodyChanged();
}
QString MessageComposer::body() const
{
return m_body;
}
void MessageComposer::setMode(Mode mode)
{
if (m_mode == mode)
return;
m_mode = mode;
emit modeChanged();
}
MessageComposer::Mode MessageComposer::mode() const
{
return m_mode;
}