Sophie

Sophie

distrib > Mageia > 5 > i586 > media > core-release > by-pkgid > 50facae208d4a6f280e44a513b104320 > files > 1964

qt-mobility-doc-1.2.0-13.mga5.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- writemessage.qdoc -->
  <title>Qt Mobility 1.2: 'Write Message' Example</title>
  <link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="content"> 
    <a href="index.html" class="qtref"><span>QtMobility Reference Documentation</span></a>
  </div>
  <div class="breadcrumb toolblock">
    <ul>
      <li class="first"><a href="index.html">Home</a></li>
      <!--  Breadcrumbs go here -->
<li><a href="http://qt.nokia.com/doc/4.7/all-examples.html">Examples</a></li>
<li>'Write Message' Example</li>
    </ul>
  </div>
</div>
<div class="content mainContent">
<h1 class="title">'Write Message' Example</h1>
<span class="subtitle"></span>
<!-- $$$writemessage-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="writemessage-messagesender-cpp.html">writemessage/messagesender.cpp</a></li>
<li><a href="writemessage-messagesender-h.html">writemessage/messagesender.h</a></li>
<li><a href="writemessage-main-cpp.html">writemessage/main.cpp</a></li>
<li><a href="writemessage-writemessage-pro.html">writemessage/writemessage.pro</a></li>
</ul>
<p>This example demonstrates using the QtMobility Messaging API to create and send a simple message.</p>
<p class="centerAlign"><img src="images/writemessage-example.png" alt="" /></p><p>The writemessage application provides an interface allowing the user to compose a simple message and send that message to one or more recipients. The type of message that is sent will depend on the messaging account that the user selects for transmission.</p>
<p>In order to know what type of message to create, our application requires that the user select an account to transmit with. We create a listing to present the user a choice, by populating a combo box with the names of the available accounts:</p>
<pre class="cpp">     <span class="comment">// Find the list of available accounts and add them to combo box</span>
     foreach (<span class="keyword">const</span> <span class="type"><a href="qmessageaccountid.html">QMessageAccountId</a></span> <span class="operator">&amp;</span>id<span class="operator">,</span> manager<span class="operator">.</span>queryAccounts()) {
         <span class="type"><a href="qmessageaccount.html">QMessageAccount</a></span> account(id);

         <span class="comment">// What is the most capable message type supported by this account?</span>
         <span class="type"><a href="qmessage.html">QMessage</a></span><span class="operator">::</span>Type type(<span class="type"><a href="qmessage.html">QMessage</a></span><span class="operator">::</span>NoType);
         <span class="keyword">if</span> (account<span class="operator">.</span>messageTypes() <span class="operator">&amp;</span> <span class="type"><a href="qmessage.html">QMessage</a></span><span class="operator">::</span>Email) {
             type <span class="operator">=</span> <span class="type"><a href="qmessage.html">QMessage</a></span><span class="operator">::</span>Email;
         } <span class="keyword">else</span> <span class="keyword">if</span> (account<span class="operator">.</span>messageTypes() <span class="operator">&amp;</span> <span class="type"><a href="qmessage.html">QMessage</a></span><span class="operator">::</span>Mms) {
             type <span class="operator">=</span> <span class="type"><a href="qmessage.html">QMessage</a></span><span class="operator">::</span>Mms;
         } <span class="keyword">else</span> <span class="keyword">if</span> (account<span class="operator">.</span>messageTypes() <span class="operator">&amp;</span> <span class="type"><a href="qmessage.html">QMessage</a></span><span class="operator">::</span>Sms) {
             type <span class="operator">=</span> <span class="type"><a href="qmessage.html">QMessage</a></span><span class="operator">::</span>Sms;
         }

         <span class="keyword">if</span> (type <span class="operator">!</span><span class="operator">=</span> <span class="type"><a href="qmessage.html">QMessage</a></span><span class="operator">::</span>NoType) {
             <span class="type"><a href="http://qt.nokia.com/doc/4.7/qstring.html">QString</a></span> name(account<span class="operator">.</span>name());
             accountDetails<span class="operator">.</span>insert(name<span class="operator">,</span> <a href="http://qt.nokia.com/doc/4.7/qpair.html#qMakePair">qMakePair</a>(type<span class="operator">,</span> account<span class="operator">.</span>id()));
             accountCombo<span class="operator">-</span><span class="operator">&gt;</span>addItem(name);
         }
     }</pre>
<p>We create a mapping of the account name to the most capable type of message that can be transmitted via that account. When an account is selected, we will adjust our UI to disable the message elements that cannot be transmitted using the associated message type. For example, an SMS message cannot transmit attachments or a subject line.</p>
<pre class="cpp"> <span class="type">void</span> MessageSender<span class="operator">::</span>accountSelected(<span class="type">int</span> index)
 {
     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qstring.html">QString</a></span> name(accountCombo<span class="operator">-</span><span class="operator">&gt;</span>itemText(index));
     <span class="keyword">if</span> (<span class="operator">!</span>name<span class="operator">.</span>isEmpty()) {
         <span class="type"><a href="http://qt.nokia.com/doc/4.7/qpair.html">QPair</a></span><span class="operator">&lt;</span><span class="type"><a href="qmessage.html">QMessage</a></span><span class="operator">::</span>Type<span class="operator">,</span> <span class="type"><a href="qmessageaccountid.html">QMessageAccountId</a></span><span class="operator">&gt;</span> details <span class="operator">=</span> accountDetails<span class="operator">[</span>name<span class="operator">]</span>;

         <span class="comment">// Enable subject only for email</span>
         subjectEdit<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(details<span class="operator">.</span>first <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qmessage.html">QMessage</a></span><span class="operator">::</span>Email);

         <span class="comment">// Disable attachments for SMS</span>
         <span class="keyword">const</span> <span class="type">bool</span> smsOnly(details<span class="operator">.</span>first <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qmessage.html">QMessage</a></span><span class="operator">::</span>Sms);
         addButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="operator">!</span>smsOnly);
         removeButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="operator">!</span>smsOnly);
     }
 }</pre>
<p>When the user presses the Send button, we create a message that contains the information they have provided. First of all, we create a message object to transmit, and associate it with the account and message type that correspond to the user's account selection:</p>
<pre class="cpp">     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qstring.html">QString</a></span> accountName(accountCombo<span class="operator">-</span><span class="operator">&gt;</span>currentText());
     <span class="keyword">if</span> (accountName<span class="operator">.</span>isEmpty()) {
         <span class="type"><a href="http://qt.nokia.com/doc/4.7/qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>warning(<span class="number">0</span><span class="operator">,</span> tr(<span class="string">&quot;Missing information&quot;</span>)<span class="operator">,</span> tr(<span class="string">&quot;No account is selected for transmission&quot;</span>));
         <span class="keyword">return</span>;
     }

     <span class="type"><a href="qmessage.html">QMessage</a></span> message;

     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qpair.html">QPair</a></span><span class="operator">&lt;</span><span class="type"><a href="qmessage.html">QMessage</a></span><span class="operator">::</span>Type<span class="operator">,</span> <span class="type"><a href="qmessageaccountid.html">QMessageAccountId</a></span><span class="operator">&gt;</span> details <span class="operator">=</span> accountDetails<span class="operator">[</span>accountName<span class="operator">]</span>;
     message<span class="operator">.</span>setType(details<span class="operator">.</span>first);
     message<span class="operator">.</span>setParentAccountId(details<span class="operator">.</span>second);</pre>
<p>Next, we process the recipient address or addresses that were provided. For each whitespace-separated element, we create an address object that has that element as the recipient, with the address type determined by the type of message we are constructing.</p>
<pre class="cpp">     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qstring.html">QString</a></span> to(toEdit<span class="operator">-</span><span class="operator">&gt;</span>text());
     <span class="keyword">if</span> (to<span class="operator">.</span>isEmpty()) {
         <span class="type"><a href="http://qt.nokia.com/doc/4.7/qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>warning(<span class="number">0</span><span class="operator">,</span> tr(<span class="string">&quot;Missing information&quot;</span>)<span class="operator">,</span> tr(<span class="string">&quot;Please enter a recipient address&quot;</span>));
         <span class="keyword">return</span>;
     }

     <span class="comment">// Find the address type to use for this message</span>
     <span class="type"><a href="qmessageaddress.html">QMessageAddress</a></span><span class="operator">::</span>Type addrType(message<span class="operator">.</span>type() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qmessage.html">QMessage</a></span><span class="operator">::</span>Email <span class="operator">?</span> <span class="type"><a href="qmessageaddress.html">QMessageAddress</a></span><span class="operator">::</span>Email : <span class="type"><a href="qmessageaddress.html">QMessageAddress</a></span><span class="operator">::</span>Phone);

     <span class="type">QMessageAddressList</span> toList;
     foreach (<span class="keyword">const</span> <span class="type"><a href="http://qt.nokia.com/doc/4.7/qstring.html">QString</a></span> <span class="operator">&amp;</span>item<span class="operator">,</span> to<span class="operator">.</span>split(<span class="type"><a href="http://qt.nokia.com/doc/4.7/qregexp.html">QRegExp</a></span>(<span class="string">&quot;\\s&quot;</span>)<span class="operator">,</span> <span class="type"><a href="http://qt.nokia.com/doc/4.7/qstring.html">QString</a></span><span class="operator">::</span>SkipEmptyParts)) {
         toList<span class="operator">.</span>append(<span class="type"><a href="qmessageaddress.html">QMessageAddress</a></span>(addrType<span class="operator">,</span> item));
     }
     message<span class="operator">.</span>setTo(toList);</pre>
<p>Next, we set the subject of the message (if the message can contain a subject) and set the text of the message.</p>
<pre class="cpp">     <span class="keyword">if</span> (message<span class="operator">.</span>type() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qmessage.html">QMessage</a></span><span class="operator">::</span>Email) {
         <span class="type"><a href="http://qt.nokia.com/doc/4.7/qstring.html">QString</a></span> subject(subjectEdit<span class="operator">-</span><span class="operator">&gt;</span>text());
         <span class="keyword">if</span> (subject<span class="operator">.</span>isEmpty()) {
             <span class="type"><a href="http://qt.nokia.com/doc/4.7/qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>warning(<span class="number">0</span><span class="operator">,</span> tr(<span class="string">&quot;Missing information&quot;</span>)<span class="operator">,</span> tr(<span class="string">&quot;Please enter a subject&quot;</span>));
             <span class="keyword">return</span>;
         }
         message<span class="operator">.</span>setSubject(subject);
     }

     <span class="type"><a href="http://qt.nokia.com/doc/4.7/qstring.html">QString</a></span> text(textEdit<span class="operator">-</span><span class="operator">&gt;</span>toPlainText());
     <span class="keyword">if</span> (text<span class="operator">.</span>isEmpty()) {
         <span class="type"><a href="http://qt.nokia.com/doc/4.7/qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>warning(<span class="number">0</span><span class="operator">,</span> tr(<span class="string">&quot;Missing information&quot;</span>)<span class="operator">,</span> tr(<span class="string">&quot;Please enter a message&quot;</span>));
         <span class="keyword">return</span>;
     }
     message<span class="operator">.</span>setBody(text);</pre>
<p>If the message can contain attachments, we add them as a list of file paths to be appended to the message:</p>
<pre class="cpp">     <span class="keyword">if</span> (message<span class="operator">.</span>type() <span class="operator">!</span><span class="operator">=</span> <span class="type"><a href="qmessage.html">QMessage</a></span><span class="operator">::</span>Sms) {
         <span class="keyword">if</span> (attachmentsList<span class="operator">-</span><span class="operator">&gt;</span>count()) {
             <span class="type"><a href="http://qt.nokia.com/doc/4.7/qstringlist.html">QStringList</a></span> paths;
             <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator">&lt;</span> attachmentsList<span class="operator">-</span><span class="operator">&gt;</span>count(); <span class="operator">+</span><span class="operator">+</span>i) {
                 paths<span class="operator">.</span>append(attachmentsList<span class="operator">-</span><span class="operator">&gt;</span>item(i)<span class="operator">-</span><span class="operator">&gt;</span>text());
             }

             message<span class="operator">.</span>appendAttachments(paths);
         }
     }</pre>
<p>Now that we have created our message, we use the <a href="qmessageservice.html#send">QMessageService::send</a>() operation to request that the message be stored in the system and transmitted:</p>
<pre class="cpp">     sendButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);
     <span class="keyword">if</span> (service<span class="operator">.</span>send(message)) {
         sendId <span class="operator">=</span> message<span class="operator">.</span>id();
     } <span class="keyword">else</span> {
         sendButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">true</span>);
         <span class="type"><a href="http://qt.nokia.com/doc/4.7/qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>warning(<span class="number">0</span><span class="operator">,</span> tr(<span class="string">&quot;Failed&quot;</span>)<span class="operator">,</span> tr(<span class="string">&quot;Unable to send message&quot;</span>));
     }</pre>
<p>To determine whether our transmission is successful, we wait for changes to the <a href="qmessageservice.html#state">state</a> of the <a href="qmessageservice.html">QMessageService</a> object. When the state changes to the FinishedState, we need to check whether the request was successful or not:</p>
<pre class="cpp"> <span class="type">void</span> MessageSender<span class="operator">::</span>stateChanged(<span class="type"><a href="qmessageservice.html">QMessageService</a></span><span class="operator">::</span>State newState)
 {
     <span class="keyword">if</span> (newState <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qmessageservice.html">QMessageService</a></span><span class="operator">::</span>FinishedState) {
         <span class="keyword">if</span> (service<span class="operator">.</span>error() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qmessagemanager.html">QMessageManager</a></span><span class="operator">::</span>NoError) {
             <span class="type"><a href="http://qt.nokia.com/doc/4.7/qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>information(<span class="number">0</span><span class="operator">,</span> tr(<span class="string">&quot;Success&quot;</span>)<span class="operator">,</span> tr(<span class="string">&quot;Message sent successfully&quot;</span>));
         } <span class="keyword">else</span> {
             <span class="type"><a href="http://qt.nokia.com/doc/4.7/qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>warning(<span class="number">0</span><span class="operator">,</span> tr(<span class="string">&quot;Failed&quot;</span>)<span class="operator">,</span> tr(<span class="string">&quot;Unable to send message&quot;</span>));

             <span class="comment">// Try to delete the failed message</span>
             <span class="keyword">if</span> (<span class="operator">!</span>manager<span class="operator">.</span>removeMessage(sendId)) {
                 <a href="http://qt.nokia.com/doc/4.7/qtglobal.html#qWarning">qWarning</a>() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Unable to remove failed message:&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> sendId<span class="operator">.</span>toString();
             }
         }

         sendId <span class="operator">=</span> <span class="type"><a href="qmessageid.html">QMessageId</a></span>();
         sendButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">true</span>);
     }
 }</pre>
</div>
<!-- @@@writemessage -->
  <div class="ft">
    <span></span>
  </div>
</div> 
<div class="footer">
  <p>
     <acronym title="Copyright">&copy;</acronym> 2008-2011 Nokia Corporation and/or its
     subsidiaries. Nokia, Qt and their respective logos are trademarks of Nokia Corporation 
     in Finland and/or other countries worldwide.</p>
  <p>
     All other trademarks are property of their respective owners. <a title="Privacy Policy"
     href="http://qt.nokia.com/about/privacy-policy">Privacy Policy</a></p>
  <br />
  <p>
    Licensees holding valid Qt Commercial licenses may use this document in accordance with the    Qt Commercial License Agreement provided with the Software or, alternatively, in accordance    with the terms contained in a written agreement between you and Nokia.</p>
  <p>
    Alternatively, this document may be used under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU
    Free Documentation License version 1.3</a>
    as published by the Free Software Foundation.</p>
</div>
</body>
</html>