×
Namespaces

Variants
Actions
(Difference between revisions)

QML SMShelper Plugin

From Nokia Developer Wiki
Jump to: navigation, search
jahartik (Talk | contribs)
(Created page with 'Category:Qt MobilityCategory:Qt Quick __NOTOC__ __NOEDITSECTION__ {{CodeSnippet |id=... |platform=... |devices=... |category=... |subcategory=... |creationdate=... |keywo…')
 
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
(23 intermediate revisions by 5 users not shown)
Line 1: Line 1:
[[Category:Qt Mobility]][[Category:Qt Quick]]
+
[[Category:Qt]][[Category:Qt Mobility]][[Category:Qt Quick]][[Category:SMS]][[Category:Code Examples]][[Category:MeeGo Harmattan]]
__NOTOC__
+
{{Abstract|This article creates a C++ extension to QML ({{Icode|SMSplugin}}) to expose [http://doc.qt.nokia.com/qtmobility/qtmessaging.html Qt Messaging APIs] to QML environment.}}  Note it is also possible to launch the standard SMS editor from QML using [http://doc.qt.nokia.com/stable/qml-qt.html#openUrlExternally-method Qt.openUrlExternally], as described in [[How to use Qt.openUrlExternally in QML]].
__NOEDITSECTION__
+
 
{{CodeSnippet
+
{{ArticleMetaData <!-- v1.2 -->
|id=...
+
|sourcecode= [[Media:Smssender.zip]]
|platform=...
+
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|devices=...
+
|devices= N8, E7, C7
|category=...
+
|sdk= [http://qt.nokia.com/downloads Qt SDK] 4.7.1 or newer and [http://qt.nokia.com/products/qt-addons/mobility Qt mobility] 1.1.0 or newer
|subcategory=...
+
|platform= Symbian^3
|creationdate=...
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
|keywords=...
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= Self-Signed
 +
|capabilities= NetworkServices
 +
|keywords= QMessageService, QMessageManager
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20110429
 +
|author= [[User:Jahartik]]
 
}}
 
}}
  
 +
==Pro file==
  
 +
The following capabilities and libraries are required:
 +
<code cpp-qt>
 +
symbian:TARGET.CAPABILITY = NetworkServices
 +
CONFIG += mobility
 +
MOBILITY = messaging
 +
</code>
 +
==Header file==
  
 +
'''smshelper.h''' applying this macro to definitions of member functions to allow them to be invoked via the''' meta-object system'''
  
''Delete the guidance text written in italics.''
+
<code cpp-qt>
 +
#ifndef SMSHELPER_H
 +
#define SMSHELPER_H
  
==Overview==
+
#include <QObject>
 +
#include <qmessage.h>
 +
#include <qmessageservice.h>
  
{{Abstract|An abstract (line or paragraph) describing very briefly what the article delivers - for example "This article defines the standard boilerplate text for a new code example"}}
+
QTM_USE_NAMESPACE
  
''Delete the texts that do not apply.''
 
  
This snippet can be self-signed.
+
#ifdef Q_OS_SYMBIAN
 +
#  define SENDSMS_ENABLED
 +
#elif defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
 +
#  define SENDSMS_ENABLED
 +
#endif
  
[or]
+
class SMSHelper : public QObject
 +
{
 +
    Q_OBJECT
  
This snippet requires XXX capabilities. Self-signing is not possible because a Developer Certificate is needed.
+
public:
 +
    explicit SMSHelper(QObject *parent = 0);
 +
    ~SMSHelper();
  
[or]
+
    Q_INVOKABLE bool sendsms(QString phonenumber, QString message);
  
This snippets requires XXX capabilities. Self-signing is not possible because a Developer Certificate, Publisher ID, and device manufacturer approval are required.
+
signals:
 +
    void stateMsg(const QString &statemsg);
 +
    void errorMsg(const QString &errormsg);
 +
    void debugMsg(const QString &dbgmsg);
  
 +
private slots:
 +
    void messageStateChanged(QMessageService::State s);
 +
#ifndef SENDSMS_ENABLED
 +
    void signalFinishedState() { emit stateMsg("FinishedState");};
 +
#endif
 +
private:
 +
    QMessageService iMessageService;
 +
    QMessageManager iManager;
 +
    QMessageService::State state;
 +
};
  
==Preconditions==
+
#endif // SMSHELPER_H
 +
</code>
  
''In complex cases you may need to provide information on the framework needed in order to implement and use the code snippet. For example, a Bluetooth connection must be established in order to use the code snippet "send a file via Bluetooth OBEX". You can refer to an already existing code example.''
 
  
 +
==Source file==
  
==MMP file (optional)==
+
smshelper source code
 +
<code cpp-qt>
 +
#include "smshelper.h"
  
The following capabilities and libraries are required:
+
#ifndef SENDSMS_ENABLED
 +
#include <QTimer>
 +
#endif
  
CAPABILITY     
+
SMSHelper::SMSHelper(QObject *parent) :
 +
    QObject(parent)
 +
{
 +
    state = QMessageService::InactiveState;
 +
    connect(&iMessageService, SIGNAL(stateChanged(QMessageService::State)), this, SLOT(messageStateChanged(QMessageService::State)));
 +
}
  
 +
SMSHelper::~SMSHelper()
 +
{
 +
}
  
LIBRARY         
 
  
LIBRARY        
+
bool SMSHelper::sendsms(QString phonenumber, QString message)
 +
{
  
<code> code </code>
+
#ifdef SENDSMS_ENABLED
 +
    if (!QMessageAccount::defaultAccount(QMessage::Sms).isValid())
 +
    {
 +
        emit errorMsg("No messageaccount for sms sending.");
 +
        return false;
 +
    }
  
==Header file==
+
    if (state == QMessageService::InactiveState || state == QMessageService::FinishedState)
 +
    {
 +
        QMessage sms;
  
''Not the complete file but relevant code blocks.''
+
        sms.setType(QMessage::Sms);
 +
        sms.setParentAccountId(QMessageAccount::defaultAccount(QMessage::Sms));
 +
        sms.setTo(QMessageAddress(QMessageAddress::Phone, phonenumber));
 +
        sms.setBody(message);
  
<code> code </code>
+
        return iMessageService.send(sms);
 +
    }
 +
    else
 +
    {
 +
        return false;
 +
    }
  
 +
#else
 +
    QTimer::singleShot(1000,this,SLOT(signalFinishedState()));
 +
    return true;
 +
#endif
 +
}
  
==Source file==
+
void SMSHelper::messageStateChanged(QMessageService::State s)
 
+
{
''Not the complete file but relevant code blocks.''
+
    state = s;
  
''You do not need to provide complete error handling but it should be commented as TODO for developers.''
+
    if (s == QMessageService::InactiveState)
 +
    {
 +
        emit stateMsg("InactiveState");
 +
    }
 +
    else if (s == QMessageService::ActiveState)
 +
    {
 +
        emit stateMsg("ActiveState");
 +
    }
 +
    else if (s == QMessageService::CanceledState)
 +
    {
 +
        emit stateMsg("CanceledState");
 +
    }
 +
    else if (s == QMessageService::FinishedState)
 +
    {
 +
        emit stateMsg("FinishedState");
 +
    }
 +
    else
 +
    {
 +
        emit stateMsg(QString("QMessageService::%1").arg(s));
 +
    }
 +
}
 +
</code>
  
<code> code </code>
+
To register the C++ type in the QML system with the name SMSHelper include following code in your main.cpp file
  
 +
<code cpp-qt>
 +
qmlRegisterType<SMSHelper>("SMSHelper",1,0,"SMSHelper");
 +
</code>
  
==Postconditions==
+
Import sms plugin in QML
 +
<code cpp-qt>
 +
import SMSHelper 1.0
 +
</code>
  
''Explain what the code snippet is expected to do.''
+
Example use QML
 +
<code css>
 +
SMSHelper
 +
    {
 +
        id: smshelper
 +
}
 +
</code>
  
 +
== SMShelper developer APIs ==
  
==Test application and other attachments (optional)==
+
<code cpp-qt>
 +
//Constructor and destructor
 +
SMSHelper(QObject *parent = 0);
 +
~SMSHelper();
  
''The attachments should be provided with the visible info page (Categories need to be defined, the attached file must be uploaded to Wiki, and internal Wiki links must be used). Every attachment should have its own page (however, a ZIP package can be used for multiple source code files).''
+
public slots:
 +
sendsms(QString phonenumber, QString message);
  
[[Category:Code Examples]]
+
signals:
 +
void stateMsg(const QString &statemsg);
 +
void errorMsg(const QString &errormsg);
 +
void debugMsg(const QString &dbgmsg);
 +
</code>

Latest revision as of 04:18, 11 October 2012

This article creates a C++ extension to QML (SMSplugin) to expose Qt Messaging APIs to QML environment. Note it is also possible to launch the standard SMS editor from QML using Qt.openUrlExternally, as described in How to use Qt.openUrlExternally in QML.

Article Metadata
Code Example
Source file: Media:Smssender.zip
Tested with
SDK: Qt SDK 4.7.1 or newer and Qt mobility 1.1.0 or newer
Devices(s): N8, E7, C7
Compatibility
Platform(s): Symbian^3
Platform Security
Signing Required: Self-Signed
Capabilities: NetworkServices
Article
Keywords: QMessageService, QMessageManager
Created: jahartik (29 Apr 2011)
Last edited: hamishwillee (11 Oct 2012)

Contents

[edit] Pro file

The following capabilities and libraries are required:

symbian:TARGET.CAPABILITY = NetworkServices
CONFIG += mobility
MOBILITY = messaging

[edit] Header file

smshelper.h applying this macro to definitions of member functions to allow them to be invoked via the meta-object system

#ifndef SMSHELPER_H
#define SMSHELPER_H
 
#include <QObject>
#include <qmessage.h>
#include <qmessageservice.h>
 
QTM_USE_NAMESPACE
 
 
#ifdef Q_OS_SYMBIAN
# define SENDSMS_ENABLED
#elif defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
# define SENDSMS_ENABLED
#endif
 
class SMSHelper : public QObject
{
Q_OBJECT
 
public:
explicit SMSHelper(QObject *parent = 0);
~SMSHelper();
 
Q_INVOKABLE bool sendsms(QString phonenumber, QString message);
 
signals:
void stateMsg(const QString &statemsg);
void errorMsg(const QString &errormsg);
void debugMsg(const QString &dbgmsg);
 
private slots:
void messageStateChanged(QMessageService::State s);
#ifndef SENDSMS_ENABLED
void signalFinishedState() { emit stateMsg("FinishedState");};
#endif
private:
QMessageService iMessageService;
QMessageManager iManager;
QMessageService::State state;
};
 
#endif // SMSHELPER_H


[edit] Source file

smshelper source code

#include "smshelper.h"
 
#ifndef SENDSMS_ENABLED
#include <QTimer>
#endif
 
SMSHelper::SMSHelper(QObject *parent) :
QObject(parent)
{
state = QMessageService::InactiveState;
connect(&iMessageService, SIGNAL(stateChanged(QMessageService::State)), this, SLOT(messageStateChanged(QMessageService::State)));
}
 
SMSHelper::~SMSHelper()
{
}
 
 
bool SMSHelper::sendsms(QString phonenumber, QString message)
{
 
#ifdef SENDSMS_ENABLED
if (!QMessageAccount::defaultAccount(QMessage::Sms).isValid())
{
emit errorMsg("No messageaccount for sms sending.");
return false;
}
 
if (state == QMessageService::InactiveState || state == QMessageService::FinishedState)
{
QMessage sms;
 
sms.setType(QMessage::Sms);
sms.setParentAccountId(QMessageAccount::defaultAccount(QMessage::Sms));
sms.setTo(QMessageAddress(QMessageAddress::Phone, phonenumber));
sms.setBody(message);
 
return iMessageService.send(sms);
}
else
{
return false;
}
 
#else
QTimer::singleShot(1000,this,SLOT(signalFinishedState()));
return true;
#endif
}
 
void SMSHelper::messageStateChanged(QMessageService::State s)
{
state = s;
 
if (s == QMessageService::InactiveState)
{
emit stateMsg("InactiveState");
}
else if (s == QMessageService::ActiveState)
{
emit stateMsg("ActiveState");
}
else if (s == QMessageService::CanceledState)
{
emit stateMsg("CanceledState");
}
else if (s == QMessageService::FinishedState)
{
emit stateMsg("FinishedState");
}
else
{
emit stateMsg(QString("QMessageService::%1").arg(s));
}
}

To register the C++ type in the QML system with the name SMSHelper include following code in your main.cpp file

qmlRegisterType<SMSHelper>("SMSHelper",1,0,"SMSHelper");

Import sms plugin in QML

import SMSHelper 1.0

Example use QML

SMSHelper
{
id: smshelper
}

[edit] SMShelper developer APIs

//Constructor and destructor
SMSHelper(QObject *parent = 0);
~SMSHelper();
 
public slots:
sendsms(QString phonenumber, QString message);
 
signals:
void stateMsg(const QString &statemsg);
void errorMsg(const QString &errormsg);
void debugMsg(const QString &dbgmsg);
This page was last modified on 11 October 2012, at 04:18.
153 page views in the last 30 days.
×