messagemodel.cpp Example File
messageviewer/messagemodel.cpp
#include "messagemodel.h"
#include "messagedelegate.h"
#include <QIcon>
#include <QMailMessageId>
#include <QMailMessageMetaData>
#include <QMailMessageKey>
#include <QMailMessageSortKey>
#include <QMailStore>
#include <QPhoneNumber>
#include <QStandardItem>
#include <QTimeString>
#include <QtopiaApplication>
class MessageItem : public QStandardItem
{
public:
explicit MessageItem(const QMailMessageId& id);
virtual ~MessageItem();
QMailMessageId messageId() const;
private:
QMailMessageId id;
};
MessageItem::MessageItem(const QMailMessageId& id)
: QStandardItem(), id(id)
{
static QIcon sentMessageIcon(":icon/qtmail/sendmail");
static QIcon receivedMessageIcon(":icon/qtmail/getmail");
static QIcon smsIcon(":icon/txt");
static QIcon mmsIcon(":icon/multimedia");
static QIcon emailIcon(":icon/email");
static QIcon instantIcon(":icon/im");
QMailMessageMetaData message(id);
QIcon* messageIcon = &smsIcon;
if (message.messageType() == QMailMessage::Mms)
messageIcon = &mmsIcon;
if (message.messageType() == QMailMessage::Email)
messageIcon = &emailIcon;
if (message.messageType() == QMailMessage::Instant)
messageIcon = &instantIcon;
bool sent(message.status() & QMailMessage::Outgoing);
QDateTime messageTime(message.date().toLocalTime());
QString action(qApp->translate("MessageViewer", sent ? "Sent" : "Received"));
QString date(QTimeString::localMD(messageTime.date()));
QString time(QTimeString::localHM(messageTime.time(), QTimeString::Short));
QString sublabel(QString("%1 %2 %3").arg(action).arg(date).arg(time));
setIcon(sent ? sentMessageIcon : receivedMessageIcon);
setText(message.subject());
setData(sublabel, MessageDelegate::SubLabelRole);
setData(*messageIcon, MessageDelegate::SecondaryDecorationRole);
}
MessageItem::~MessageItem()
{
}
QMailMessageId MessageItem::messageId() const
{
return id;
}
MessageModel::MessageModel(QObject *parent)
: QStandardItemModel(parent)
{
}
MessageModel::~MessageModel()
{
}
void MessageModel::setContact(const QContact& contact)
{
clear();
if (contact.phoneNumbers().isEmpty() && contact.emailList().isEmpty()) {
return;
}
QMailMessageKey msgsFrom;
QMailMessageKey msgsTo;
foreach(const QString& number, contact.phoneNumbers().values()) {
msgsFrom |= QMailMessageKey(QMailMessageKey::Sender, number);
msgsTo |= QMailMessageKey(QMailMessageKey::Recipients, number, QMailDataComparator::Includes);
}
foreach(const QString& address, contact.emailList()) {
msgsFrom |= QMailMessageKey(QMailMessageKey::Sender, address);
msgsTo |= QMailMessageKey(QMailMessageKey::Recipients, address, QMailDataComparator::Includes);
}
QMailMessageSortKey sort(QMailMessageSortKey::TimeStamp, Qt::DescendingOrder);
QMailMessageIdList matches(QMailStore::instance()->queryMessages(msgsFrom | msgsTo, sort));
foreach (const QMailMessageId& id, matches)
appendRow(new MessageItem(id));
}
bool MessageModel::isEmpty() const
{
return (rowCount() == 0);
}
QMailMessageId MessageModel::messageId(const QModelIndex& index)
{
if (index.isValid())
if (MessageItem* item = static_cast<MessageItem*>(itemFromIndex(index)))
return item->messageId();
return QMailMessageId();
}
Copyright © 2009 Trolltech |
Trademarks |
Qt Extended 4.4.3 |