×
Namespaces

Variants
Actions

Delete messages using Qt Mobility API

From Nokia Developer Wiki
Jump to: navigation, search

Needs-update.pngThis article needs to be updated: If you found this article useful, please fix the problems below then delete the {{ArticleNeedsUpdate}} template from the article to remove this warning.

Reasons: hamishwillee (16 Feb 2012)
This is outdated example and may not compile with new sdks; see this for new examples Qt Mobility API Basic Examples Part 1. Note also that the article has some Symbian specific guidance and has been tested on Symbian, but should work on all platforms.) The article uses the QWidget UI - it should be updated to use Qt Quick.

Article Metadata
Code ExampleTested with
Devices(s): Nokia 5800 and Nokia N97.
Compatibility
Platform(s): S60 3rd Edition, FP1, FP2
S60 5th Edition
Symbian
Article
Keywords: QMessageServiceAction, QMessageId
Created: Yashu (29 Mar 2010)
Last edited: hamishwillee (11 Oct 2012)

Contents

Overview

This code example shows how to use messaging API in Qt for symbian. Code demonstrate how to delete all messages of each folders as well as how to delete each item (message) of folder one by one. Code example uses Messaging APIs. Purpose of this code example is to show how to use Qt and QtMoblity APIs to delete messages.

On Symbian, this snippet requires capabilities: NetworkServices LocalServices ReadUserData WriteUserData UserEnvironment ReadDeviceData WriteDeviceData. Self-signing is not possible and at least Developer certificate is needed, or Open on-line signing an be used

Headers required

#include "qtmessaging.h"

.pro file

Modify your .pro file as follows.

CONFIG += mobility
MOBILITY = messaging
INCLUDEPATH += D:/QtMobility/src/messaging
INCLUDEPATH += D:/QtMobility/src/global
/* To show icon on list box */
RESOURCES += DeleteMessages.qrc
symbian:TARGET.CAPABILITY = NetworkServices \
LocalServices \
ReadUserData \
WriteUserData \
UserEnvironment \
ReadDeviceData \
WriteDeviceData

Add resource file

Add .qrc file to project to show icon in list widget.

<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file>rsc/Inbox.jpg</file>
</qresource>
</RCC>

Header files

/* DeleteMessages.h*/
#ifndef DELETEMESSAGES_H
#define DELETEMESSAGES_H
 
#include <QWidget>
#include "qtmessaging.h"
 
QT_BEGIN_NAMESPACE
class QStackedWidget;
QT_END_NAMESPACE
 
QTM_USE_NAMESPACE;
 
class MainPage;
class ListBox;
 
class DeleteMessages : public QWidget
{
Q_OBJECT
 
public:
DeleteMessages(QWidget *parent = 0);
~DeleteMessages();
 
public slots:
void activateMainPage();
void activateInbox();
void activateSentItem();
void activateDraft();
void activateOutbox();
void messageServiceStateChanged(QMessageServiceAction::State s);
 
 
private:
/* The stacked widget which will display the pages */
QStackedWidget *m_stackedWidget;
 
/*widget to show main screen */
MainPage *m_mainPage;
/* widget to show each item in folder */
ListBox *m_listBox;
 
QMessageId messageId;
QMessageServiceAction *messageService;
};
 
#endif // DELETEMESSAGES_H
/* MainPage.h */
#ifndef MAINPAGE_H
#define MAINPAGE_H
 
#include <QtGui/QMainWindow>
#include "qtmessaging.h"
 
QT_BEGIN_NAMESPACE
class QListWidget;
class QScrollArea;
class QListWidgetItem;
class QAction;
QT_END_NAMESPACE
 
QTM_USE_NAMESPACE;
 
class MainPage : public QMainWindow
{
Q_OBJECT
 
public:
MainPage(QWidget *parent = 0);
~MainPage();
 
public:
void initializeListWidget();
 
 
signals:
void createMessage();
void createInbox();
void createSentItem();
void createDraft();
void createOutbox();
 
public slots:
void getSelectedItem(QListWidgetItem* item);
void getCurrentRow(int a);
void deleteAllMessages();
 
private:
void countMessagesInFolders();
void deleteInBox();
void deleteDrafts();
void deleteOutbox();
void deleteSentItem();
 
private:
QListWidget *listWidget;
QScrollArea *scrollArea;
int selectedRow;
int previousRow;
 
//Actions for menu item
QAction* rightSoftKeyAction;
 
 
int countInboxSMS;
int countSentSMS;
int countDraftSMS;
int countOutboxSMS;
};
 
#endif // MAINPAGE_H


/* ListBox.h */
#ifndef LISTBOX_H
#define LISTBOX_H
 
#include <QtGui/QMainWindow>
#include <qmessagestore.h>
#include "qtmessaging.h"
 
class QListWidget;
class QScrollArea;
class QListWidgetItem;
 
QTM_USE_NAMESPACE;
 
class ListBox : public QMainWindow
{
Q_OBJECT
 
public:
ListBox(QWidget *parent = 0);
~ListBox();
 
signals:
void gotoMainPage();
 
public slots:
void getCurrentRow(int a);
void closeListBox();
void openItem();
 
public:
void activateMessagingFolder(QMessage::StandardFolder folder);
bool deleteMessage(const QMessageId & id);
 
private:
QAction *rightSoftKeyAction;
QAction *leftSoftKeyAction;
QListWidget *listWidget;
QScrollArea *scrollArea;
QStringList messageIdStringList;
int selectedRow;
int previousRow;
};
 
#endif // ListBox_H

Source files

/* DeleteMessages.cpp*/
#include "MainPage.h"
#include "ListBox.h"
#include "DeleteMessages.h"
#include <qmessagestore.h>
#include <QStackedWidget>
#include <QVBoxLayout>
#include <QMessageBox>
 
DeleteMessages::DeleteMessages(QWidget *parent)
: QWidget(parent)
{
setWindowTitle(tr("DeleteMessages"));
 
/* initialize main page widget*/
m_mainPage = new MainPage(this);
connect(m_mainPage, SIGNAL(createInbox()), this, SLOT(activateInbox()));
connect(m_mainPage, SIGNAL(createSentItem()), this, SLOT(activateSentItem()));
connect(m_mainPage, SIGNAL(createDraft()), this, SLOT(activateDraft()));
connect(m_mainPage, SIGNAL(createOutbox()), this, SLOT(activateOutbox()));
 
/* initialize list widget */
m_listBox = new ListBox(this);
 
connect(m_listBox, SIGNAL(gotoMainPage()), this, SLOT(activateMainPage()));
 
m_stackedWidget = new QStackedWidget(this);
m_stackedWidget->addWidget(m_mainPage);
m_stackedWidget->addWidget(m_listBox);
m_stackedWidget->setCurrentIndex(0);
 
 
QVBoxLayout *centralLayout = new QVBoxLayout;
centralLayout->addWidget(m_stackedWidget);
 
/* The QWidget will take ownership of centralLayout. So no need to delete centralLayout*/
setLayout(centralLayout);
 
messageService = new QMessageServiceAction(this);
connect(messageService, SIGNAL(stateChanged(QMessageServiceAction::State)), this, SLOT(messageServiceStateChanged(QMessageServiceAction::State)));
 
}
 
/*Activate main page widget */
void DeleteMessages::activateMainPage()
{
setWindowTitle(tr("DeleteMessages"));
m_mainPage->initializeListWidget();
m_stackedWidget->setCurrentIndex(0);
}
 
/* activate widget to show inbox */
void DeleteMessages::activateInbox()
{
setWindowTitle(tr("Inbox"));
m_listBox->activateMessagingFolder(QMessage::InboxFolder);
m_stackedWidget->setCurrentIndex(1);
}
 
/* activate widget to show sent item */
void DeleteMessages::activateSentItem()
{
setWindowTitle(tr("Sent"));
m_listBox->activateMessagingFolder(QMessage::SentFolder);
m_stackedWidget->setCurrentIndex(1);
}
 
/* activate widget to show draft */
void DeleteMessages::activateDraft()
{
setWindowTitle(tr("Draft"));
m_listBox->activateMessagingFolder(QMessage::DraftsFolder);
m_stackedWidget->setCurrentIndex(1);
}
 
/* activate widget to show sent outbox */
void DeleteMessages::activateOutbox()
{
setWindowTitle(tr("Outbox"));
m_listBox->activateMessagingFolder(QMessage::OutboxFolder);
m_stackedWidget->setCurrentIndex(1);
}
 
/* handle callback methods */
void DeleteMessages::messageServiceStateChanged(QMessageServiceAction::State s)
{
if (s == QMessageServiceAction::Successful)
{
QMessageBox::information(0, "Message sent", "Message sent successfully");
}
else if (s == QMessageServiceAction::Failed)
{
QMessageBox::warning(0, "Failed", "Unable to send message");
 
if (!QMessageStore::instance()->removeMessage(messageId))
{
QMessageBox::warning(0, "Unable to remove", "Unable to remove message: " + messageId.toString());
}
messageId = QMessageId();
}
}
 
DeleteMessages::~DeleteMessages()
{
}
/* MainPage.cpp */
#include "MainPage.h"
#include <QListWidget>
#include <QScrollArea>
#include <QMessageBox>
#include <QItemSelectionModel>
#include <QKeyEvent>
#include <QAction>
 
MainPage::MainPage(QWidget *parent)
: QMainWindow(parent)
{
/* To show items in list view */
listWidget = new QListWidget();
listWidget->setObjectName(QString::fromUtf8("listWidget"));
 
/* To make UI scrollable */
scrollArea = new QScrollArea();
 
/* scrollArea takes ownership of the listWidget, so no need to delete it */
scrollArea->setWidget(listWidget);
scrollArea->setAlignment(Qt::AlignLeft);
scrollArea->setWidgetResizable(true);
 
/* QMainWindow takes ownership of the scrollArea, so no need to delete it */
setCentralWidget(scrollArea);
 
connect(listWidget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(getSelectedItem(QListWidgetItem*)));
connect(listWidget, SIGNAL(currentRowChanged (int)), this, SLOT(getCurrentRow(int)));
 
initializeListWidget();
 
selectedRow = previousRow = 0;
 
rightSoftKeyAction = new QAction(tr("Delete all"),this);
rightSoftKeyAction->setSoftKeyRole(QAction::PositiveSoftKey);
addAction(rightSoftKeyAction);
 
connect(rightSoftKeyAction, SIGNAL(triggered()), this, SLOT(deleteAllMessages()));
}
 
void MainPage::initializeListWidget()
{
countMessagesInFolders();
 
int curIndex = 0;
 
if(listWidget->count() > 0)
curIndex = listWidget->currentRow();
 
/* set font and icon size of list Widget */
QFont listWidgetFont("Courier", 8, QFont::Normal);
listWidget->setIconSize(QSize(60 ,70));
 
listWidget->clear();
 
QString listItemText;
 
/* The listWidget will take ownership of the each QListWidgetItem item, so no need to delete it. */
QListWidgetItem* listItemInbox = new QListWidgetItem;
listItemInbox->setFont(listWidgetFont);
listItemText.setNum(countInboxSMS);
listItemText.prepend("Inbox ");
listItemText.append(" messages");
listItemInbox->setText(listItemText);
/* set icon to list widget item */
listItemInbox->setIcon(QIcon(":/rsc/Inbox.jpg"));
listItemInbox->setSizeHint(QSize(size().width() - 200 ,70));
listWidget->addItem(listItemInbox);
 
QListWidgetItem* listItemSent = new QListWidgetItem;
listItemSent->setFont(listWidgetFont);
listItemText.clear();
listItemText.setNum(countSentSMS);
listItemText.prepend("Sent ");
listItemText.append(" messages");
listItemSent->setText(listItemText);
listItemSent->setIcon(QIcon(":/rsc/Inbox.jpg"));
listItemSent->setSizeHint(QSize(size().width() - 200 ,70));
listWidget->addItem(listItemSent);
 
QListWidgetItem* listItemDraft = new QListWidgetItem;
listItemDraft->setFont(listWidgetFont);
listItemText.clear();
listItemText.setNum(countDraftSMS);
listItemText.prepend("Draft ");
listItemText.append(" messages");
listItemDraft->setText(listItemText);
listItemDraft->setIcon(QIcon(":/rsc/Inbox.jpg"));
listItemDraft->setSizeHint(QSize(size().width() - 200 ,70));
listWidget->addItem(listItemDraft);
 
QListWidgetItem* listItemOutbox = new QListWidgetItem;
listItemOutbox->setFont(listWidgetFont);
listItemText.clear();
listItemText.setNum(countOutboxSMS);
listItemText.prepend("Outbox ");
listItemText.append(" messages");
listItemOutbox->setText(listItemText);
listItemOutbox->setIcon(QIcon(":/rsc/Inbox.jpg"));
listItemOutbox->setSizeHint(QSize(size().width() - 200 ,70));
listWidget->addItem(listItemOutbox);
 
listWidget->setCurrentRow(curIndex,QItemSelectionModel::Select);
}
 
void MainPage::getSelectedItem(QListWidgetItem* item)
{
if(selectedRow == previousRow)
{
if(!item->text().compare("New message"))
createMessage();
else if(!item->text().left(5).compare("Inbox"))
createInbox();
else if(!item->text().left(4).compare("Sent"))
createSentItem();
else if(!item->text().left(5).compare("Draft"))
createDraft();
else if(!item->text().left(6).compare("Outbox"))
createOutbox();
}
else
{
previousRow = selectedRow;
}
}
 
 
void MainPage::getCurrentRow(int row)
{
previousRow = selectedRow;
selectedRow = row;
}
 
void MainPage::deleteAllMessages()
{
if(!listWidget->currentItem()->text().left(5).compare("Inbox"))
deleteInBox();
else if(!listWidget->currentItem()->text().left(4).compare("Sent"))
deleteSentItem();
else if(!listWidget->currentItem()->text().left(5).compare("Draft"))
deleteDrafts();
else if(!listWidget->currentItem()->text().left(6).compare("Outbox"))
deleteOutbox();
 
initializeListWidget();
}
 
void MainPage::countMessagesInFolders()
{
 
/* Count SMS in inbox */
QMessageFilter messageFilter(QMessageFilter::byStandardFolder (QMessage::InboxFolder));
QMessageOrdering messageOrdering(QMessageOrdering::byReceptionTimeStamp(Qt::DescendingOrder));
QMessageIdList messageIdList(QMessageStore::instance()->queryMessages(messageFilter, messageOrdering));
 
countInboxSMS = messageIdList.count();
 
/* Count SMS in sent item folder */
messageFilter = QMessageFilter::byStandardFolder (QMessage::SentFolder);
messageOrdering = QMessageOrdering::byReceptionTimeStamp(Qt::DescendingOrder);
messageIdList = QMessageStore::instance()->queryMessages(messageFilter, messageOrdering);
 
countSentSMS = messageIdList.count();
 
/* Count SMS in draft folder */
messageFilter = QMessageFilter::byStandardFolder (QMessage::DraftsFolder);
messageOrdering = QMessageOrdering::byReceptionTimeStamp(Qt::DescendingOrder);
messageIdList = QMessageStore::instance()->queryMessages(messageFilter, messageOrdering);
 
countDraftSMS = messageIdList.count();
 
 
/* Count SMS in outbox folder */
messageFilter = QMessageFilter::byStandardFolder (QMessage::OutboxFolder);
messageOrdering = QMessageOrdering::byReceptionTimeStamp(Qt::DescendingOrder);
messageIdList = QMessageStore::instance()->queryMessages(messageFilter, messageOrdering);
 
countOutboxSMS = messageIdList.count();
 
}
 
/* delete messages from inbox */
void MainPage::deleteInBox()
{
QMessageFilter messageFilter(QMessageFilter::byStandardFolder (QMessage::InboxFolder));
QMessageOrdering messageOrdering(QMessageOrdering::byReceptionTimeStamp(Qt::DescendingOrder));
 
const QMessageIdList matchingIds(QMessageStore::instance()->queryMessages(messageFilter, messageOrdering));
 
if(QMessageBox::Yes == QMessageBox::warning(0, "Inbox", "Do you want to delete all messages from Inbox.",QMessageBox::Yes,QMessageBox::No))
{
if(!QMessageStore::instance()->removeMessages(messageFilter))
{
QMessageBox::information(0, "Inbox", "Unable to delete message.");
}
else
{
QMessageBox::information(0, "Inbox", "Message deleted successfully.");
}
}
}
 
/* delete messages from draft */
void MainPage::deleteDrafts()
{
QMessageFilter messageFilter(QMessageFilter::byStandardFolder (QMessage::DraftsFolder));
QMessageOrdering messageOrdering(QMessageOrdering::byReceptionTimeStamp(Qt::DescendingOrder));
 
const QMessageIdList matchingIds(QMessageStore::instance()->queryMessages(messageFilter, messageOrdering));
 
if(QMessageBox::Yes == QMessageBox::warning(0, "Draft", "Do you want to delete all messages from Draft.",QMessageBox::Yes,QMessageBox::No))
{
if(!QMessageStore::instance()->removeMessages(messageFilter))
{
QMessageBox::information(0, "Draft", "Unable to delete message.");
}
else
{
QMessageBox::information(0, "Draft", "Message deleted successfully.");
}
}
}
 
/* delete messages from outbox */
void MainPage::deleteOutbox()
{
QMessageFilter messageFilter(QMessageFilter::byStandardFolder (QMessage::OutboxFolder));
QMessageOrdering messageOrdering(QMessageOrdering::byReceptionTimeStamp(Qt::DescendingOrder));
 
const QMessageIdList matchingIds(QMessageStore::instance()->queryMessages(messageFilter, messageOrdering));
 
if(QMessageBox::Yes == QMessageBox::warning(0, "Outbox", "Do you want to delete all messages from Outbox.",QMessageBox::Yes,QMessageBox::No))
{
if(!QMessageStore::instance()->removeMessages(messageFilter))
{
QMessageBox::information(0, "Outbox", "Unable to delete message.");
}
else
{
QMessageBox::information(0, "Outbox", "Message deleted successfully.");
}
}
}
 
/* delete messages from sentitem */
void MainPage::deleteSentItem()
{
QMessageFilter messageFilter(QMessageFilter::byStandardFolder (QMessage::SentFolder));
QMessageOrdering messageOrdering(QMessageOrdering::byReceptionTimeStamp(Qt::DescendingOrder));
 
const QMessageIdList matchingIds(QMessageStore::instance()->queryMessages(messageFilter, messageOrdering));
 
if(QMessageBox::Yes == QMessageBox::warning(0, "Sent", "Do you want to delete all messages from Sent item folder.",QMessageBox::Yes,QMessageBox::No))
{
if(!QMessageStore::instance()->removeMessages(messageFilter))
{
QMessageBox::information(0, "Sent", "Unable to delete message.");
}
else
{
QMessageBox::information(0, "Sent", "Message deleted successfully.");
}
}
}
 
MainPage::~MainPage()
{
}


/* ListBox.cpp */
#include "ListBox.h"
#include <QListWidget>
#include <QScrollArea>
#include <QMessageBox>
#include <QAction>
 
ListBox::ListBox(QWidget *parent)
: QMainWindow(parent)
{
rightSoftKeyAction = new QAction(tr("Delete message"),this);
rightSoftKeyAction->setSoftKeyRole(QAction::PositiveSoftKey);
addAction(rightSoftKeyAction);
 
connect(rightSoftKeyAction, SIGNAL(triggered()), this, SLOT(openItem()));
 
leftSoftKeyAction = new QAction(tr("Back"),this);
leftSoftKeyAction->setSoftKeyRole(QAction::NegativeSoftKey);
addAction(leftSoftKeyAction);
 
connect(leftSoftKeyAction, SIGNAL(triggered()), this, SLOT(closeListBox()));
 
/* To show items in list view */
listWidget = new QListWidget();
listWidget->setObjectName(QString::fromUtf8("listWidget"));
 
/* To make UI scrollable */
scrollArea = new QScrollArea();
 
/* scrollArea takes ownership of the listWidget, so no need to delete it */
scrollArea->setWidget(listWidget);
scrollArea->setAlignment(Qt::AlignLeft);
scrollArea->setWidgetResizable(true);
 
/* QMainWindow takes ownership of the scrollArea, so no need to delete it */
setCentralWidget(scrollArea);
 
//connect(listWidget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(getSelectedItem(QListWidgetItem*)));
connect(listWidget, SIGNAL(currentRowChanged (int)), this, SLOT(getCurrentRow(int)));
 
}
 
 
void ListBox::closeListBox()
{
gotoMainPage();
}
 
void ListBox::openItem()
{
int currRow = listWidget->currentRow();
QString messageIdText = messageIdStringList.at(currRow);
QMessageId messageId(messageIdText);
if(deleteMessage(messageId))
{
listWidget->takeItem(currRow);
listWidget->reset();
}
}
 
void ListBox::getCurrentRow(int row)
{
selectedRow = row;
}
 
void ListBox::activateMessagingFolder(QMessage::StandardFolder folder)
{
listWidget->clear();
messageIdStringList.clear();
QMessageFilter messageFilter(QMessageFilter::byStandardFolder (folder));
QMessageOrdering messageOrdering(QMessageOrdering::byReceptionTimeStamp(Qt::DescendingOrder));
 
const QMessageIdList messageIdList(QMessageStore::instance()->queryMessages(messageFilter, messageOrdering));
 
QMessage message;
 
for(int i = 0; i < messageIdList.count(); ++i)
{
message = QMessageStore::instance()->message(messageIdList.at(i));
QMessageAddress from = message.from();
QString number(from.recipient());
QString text;
text.append( number + ": " + message.find(message.bodyId()).textContent());
 
/* set font of listWiget */
QFont newFont("Courier", 6, QFont::Normal);
 
/* The listWidget will take ownership of the each QListWidgetItem item, so no need to delete it. */
QListWidgetItem* listItem = new QListWidgetItem;
listItem->setFont(newFont);
listItem->setText(text);
listItem->setSizeHint(QSize(size().width() - 200 ,70));
listWidget->addItem(listItem);
messageIdStringList.append(messageIdList.at(i).toString());
}
 
if(messageIdStringList.count() <= 0)
{
listWidget->addItem("No messages");
messageIdStringList.append(QString("no messaegs"));
listWidget->setCurrentRow(0);
}
else
listWidget->setCurrentRow(0,QItemSelectionModel::Select);
 
selectedRow = previousRow = 0;
}
 
/* delete single message with message id */
bool ListBox::deleteMessage(const QMessageId & id)
{
bool ret = false;
if(QMessageBox::Yes == QMessageBox::warning(0, "Delete message", "Do you want to delete this message.",QMessageBox::Yes,QMessageBox::No))
{
if(!QMessageStore::instance()->removeMessage(id))
{
QMessageBox::information(0, "Delete message", "Unable to delete message.");
}
else
{
QMessageBox::information(0, "Delete message", "Message deleted successfully.");
ret = true;
}
}
return ret;
}
 
ListBox::~ListBox()
{
}

Post-conditions

The code snippet is expected to delete all messages from selected folder as well as delete selected message from a particular folder.

Main screen
QtDeleteMsgMainSCR.jpg

Delete a Massage from inbox folder.
QtDeleteMsgSCR1.jpg

Selected message is deleted successfully
QtDeleteMsgSCR2.jpg

Delete all Massages from sent item folder.
QtDeleteMsgSCR3.jpg

Download Code Example

This page was last modified on 11 October 2012, at 04:13.
106 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×