Advertisement
Guest User

QMessageService use from QML

a guest
May 11th, 2012
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.99 KB | None | 0 0
  1. // message.h
  2.  
  3. /*
  4.  * Copyright (C) Xabier Rodriguez Calvar <calvaris@igalia.com>
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public License
  8.  * as published by the Free Software Foundation; version 3 of the
  9.  * License, or (at your option) any later version.
  10.  *
  11.  * This library is distributed in the hope that it will be useful, but
  12.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14.  * Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with this library; if not, write to the Free Software
  18.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  19.  * 02110-1301 USA
  20.  *
  21.  */
  22.  
  23. #ifndef MESSAGE_H
  24. #define MESSAGE_H
  25.  
  26. #include <QDeclarativeItem>
  27. #include <QMessageService>
  28. #include <QMessage>
  29.  
  30. QTM_USE_NAMESPACE
  31.  
  32. class Message : public QDeclarativeItem
  33. {
  34.     Q_OBJECT
  35.  
  36. public:
  37.     Q_PROPERTY(QStringList to
  38.                READ to
  39.                WRITE setTo)
  40.     Q_PROPERTY(QString body
  41.                READ body
  42.                WRITE setBody)
  43.     Q_PROPERTY(QString from
  44.                READ from
  45.                WRITE setFrom)
  46.     Q_PROPERTY(QStringList attachments
  47.                READ attachments
  48.                WRITE setAttachments)
  49.     Q_PROPERTY(QString subject
  50.                READ subject
  51.                WRITE setSubject)
  52.     Message(QDeclarativeItem *parent = 0);
  53.     virtual ~Message();
  54.  
  55.     Q_INVOKABLE bool send();
  56.     Q_INVOKABLE bool compose();
  57.  
  58.     QStringList to() { return m_to; };
  59.     QString body() { return m_body; };
  60.     QString from() { return m_from; };
  61.     QStringList attachments() { return m_attachments; };
  62.     QString subject() { return m_subject; };
  63.  
  64. public slots:
  65.     void setTo(const QStringList& to) { m_to = to; };
  66.     void setBody(const QString& body) { m_body = body; };
  67.     void setFrom(const QString& from) { m_from = from; };
  68.     void setAttachments(const QStringList& attachments) {
  69.       m_attachments = attachments; };
  70.     void setSubject(const QString& subject) { m_subject = subject; };
  71.  
  72. private slots:
  73.     void stateChanged (QMessageService::State newState);
  74.  
  75. private:
  76.     const QMessage createMessage();
  77.  
  78.     QStringList m_to;
  79.     QString m_body;
  80.     QString m_from;
  81.     QStringList m_attachments;
  82.     QString m_subject;
  83.     QMessageService service;
  84. };
  85.  
  86. #endif // MESSAGE_H
  87.  
  88.  
  89. // message.cpp
  90.  
  91. /*
  92.  * Copyright (C) Xabier Rodriguez Calvar <calvaris@igalia.com>
  93.  *
  94.  * This library is free software; you can redistribute it and/or
  95.  * modify it under the terms of the GNU Lesser General Public License
  96.  * as published by the Free Software Foundation; version 3 of the
  97.  * License, or (at your option) any later version.
  98.  *
  99.  * This library is distributed in the hope that it will be useful, but
  100.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  101.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  102.  * Lesser General Public License for more details.
  103.  *
  104.  * You should have received a copy of the GNU Lesser General Public
  105.  * License along with this library; if not, write to the Free Software
  106.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  107.  * 02110-1301 USA
  108.  *
  109.  */
  110.  
  111. #include "message.h"
  112.  
  113. Message::Message(QDeclarativeItem *parent) :
  114.     QDeclarativeItem(parent)
  115. {
  116.     connect(&service, SIGNAL(stateChanged (QMessageService::State)), this,
  117.             SLOT(stateChanged (QMessageService::State)));
  118. }
  119.  
  120. Message::~Message()
  121. {
  122. }
  123.  
  124. const QMessage Message::createMessage()
  125. {
  126.     QMessage message;
  127.  
  128.     message.setFrom(QMessageAddress(QMessageAddress::Email, m_from));
  129.     QMessageAddressList toList;
  130.     foreach (const QString &item, m_to) {
  131.         toList.append(QMessageAddress(QMessageAddress::Email, item));
  132.     }
  133.     message.setTo(toList);
  134.     message.setSubject(m_subject);
  135.     message.setBody(m_body);
  136.     message.appendAttachments(m_attachments);
  137.  
  138.     return message;
  139. }
  140.  
  141. bool Message::send()
  142. {
  143.     QMessage message = createMessage();
  144.     qDebug("%s: from: <%s> to: <%s> body: \"%s\" attachment: %s", Q_FUNC_INFO,
  145.            m_from.toUtf8().constData(), m_to[0].toUtf8().constData(),
  146.            m_body.toUtf8().constData(), m_attachments[0].toUtf8().constData());
  147.  
  148.     service.send(message);
  149.  
  150.     return true;
  151. }
  152.  
  153. bool Message::compose()
  154. {
  155.     QMessage message = createMessage();
  156.     qDebug("%s: from: <%s> to: <%s> body: \"%s\" attachment: %s", Q_FUNC_INFO,
  157.            m_from.toUtf8().constData(), m_to[0].toUtf8().constData(),
  158.            m_body.toUtf8().constData(), m_attachments[0].toUtf8().constData());
  159.  
  160.     service.compose(message);
  161.  
  162.     return true;
  163. }
  164.  
  165. void Message::stateChanged (QMessageService::State newState)
  166. {
  167.     qDebug() << "state changed to" << newState;
  168.  
  169.     if (newState == QMessageService::FinishedState) {
  170.         QMessageManager::Error error = service.error();
  171.         qDebug() << "error:" << error;
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement