1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the demonstration applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of The Qt Company Ltd nor the names of its
21 **     contributors may be used to endorse or promote products derived
22 **     from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 #include "messagehandling.h"
42 #include <QMessageBox>
43 #include <QDebug>
44 #include <QTimerEvent>
45 #include <QTimer>
46 #include <QFile>
47 #include <QPixmap>
48 #include <QImageReader>
49 
Message(QObject * parent)50 Message::Message(QObject *parent) :
51     QObject(parent)
52 {
53     // QMessageService class provides the interface for requesting messaging service operations
54     m_service = new QMessageService(this);
55     //QObject::connect(m_service, SIGNAL(stateChanged(QMessageService::State)), this, SLOT(stateChanged(QMessageService::State)));
56     QObject::connect(m_service, SIGNAL(messagesFound(const QMessageIdList&)), this, SLOT(messagesFound(const QMessageIdList&)));
57 
58     // QMessageManager class represents the main interface for storage and
59     // retrieval of messages, folders and accounts in the system message store
60     m_manager = new QMessageManager(this);
61 
62     QObject::connect(m_manager, SIGNAL(messageAdded(const QMessageId&,const QMessageManager::NotificationFilterIdSet&)),
63     this, SLOT(messageAdded(const QMessageId&,const QMessageManager::NotificationFilterIdSet&)));
64 
65     // Register MMS in inbox (draft in emulator) folder notificationfilter
66 #ifdef Q_OS_SYMBIAN
67 #ifdef __WINS__
68     m_notifFilterSet.insert(m_manager->registerNotificationFilter(QMessageFilter::byStandardFolder(
69         QMessage::DraftsFolder)));
70 #else
71     m_notifFilterSet.insert(m_manager->registerNotificationFilter(QMessageFilter::byStandardFolder(
72         QMessage::InboxFolder)));
73 #endif
74 #else
75     m_notifFilterSet.insert(m_manager->registerNotificationFilter(QMessageFilter::byStandardFolder(
76         QMessage::InboxFolder)));
77 #endif
78 
79 }
80 
~Message()81 Message::~Message()
82 {
83 }
84 
messageAdded(const QMessageId & id,const QMessageManager::NotificationFilterIdSet & matchingFilterIds)85 void Message::messageAdded(const QMessageId& id,
86     const QMessageManager::NotificationFilterIdSet& matchingFilterIds)
87 {
88     if (matchingFilterIds.contains(m_notifFilterSet)) {
89         processIncomingMMS(id);
90     }
91 }
92 
93 
checkMessages()94 void Message::checkMessages()
95 {
96 #ifdef Q_OS_SYMBIAN
97 #ifdef __WINS__
98     QMessageFilter folderFilter(QMessageFilter::byStandardFolder(QMessage::DraftsFolder));
99 #else
100     QMessageFilter folderFilter(QMessageFilter::byStandardFolder(QMessage::InboxFolder));
101 #endif
102 #else
103     QMessageFilter folderFilter(QMessageFilter::byStandardFolder(QMessage::InboxFolder));
104 #endif
105 
106     m_service->queryMessages(folderFilter);
107     // Message::messagesFound() is called if MMS messages found
108 
109 }
110 
messagesFound(const QMessageIdList & ids)111 void Message::messagesFound(const QMessageIdList &ids)
112 {
113     foreach (const QMessageId& id, ids) {
114             processIncomingMMS(id);
115         }
116 }
117 
processIncomingMMS(const QMessageId & id)118 void Message::processIncomingMMS(const QMessageId& id)
119 {
120     QMessage message = m_manager->message(id);
121 
122     // Handle only MMS messages
123     if (message.type()!=QMessage::Mms)
124         return;
125 
126 
127     QMessageContentContainerIdList attachments = message.attachmentIds();
128     if (!attachments.isEmpty()) {
129         QMessageContentContainer messageContent = message.find(attachments[0]);
130         if (messageContent.isContentAvailable() && messageContent.contentType() == "image") {
131 
132             // Create QPixmap from the message image attachment
133             QPixmap pixmap;
134             pixmap.loadFromData(messageContent.content());
135 
136             QString from = message.from().addressee();
137             QString filename = messageContent.suggestedFileName();
138 
139             // Emit received MMS message info
140             emit messageReceived(from, filename, pixmap);
141         }
142     }
143 }
144 
sendMMS(QString picturePath,QString phoneNumber)145 bool Message::sendMMS(QString picturePath, QString phoneNumber)
146 {
147     QString tmpFileName = "c:/System/qcamera_mms.jpg";
148 
149     // Create temp image for MMS
150     // Delete previous temp image
151     QFile previousFile(tmpFileName);
152     if (previousFile.exists()) {
153         previousFile.remove();
154     }
155     // Create new temp image
156     QImageReader reader;
157     reader.setFileName(picturePath);
158     QSize imageSize = reader.size();
159     imageSize.scale(QSize(300,300), Qt::KeepAspectRatio);
160     reader.setScaledSize(imageSize);
161     QImage image = reader.read();
162     image.save(tmpFileName);
163 
164     // Use temp mms image
165     picturePath = tmpFileName;
166 
167     // Send MMS
168     QMessage message;
169     message.setType(QMessage::Mms);
170     message.setParentAccountId(QMessageAccount::defaultAccount(QMessage::Mms));
171     message.setTo(QMessageAddress(QMessageAddress::Phone, phoneNumber));
172 
173     QStringList paths;
174     paths << picturePath;
175     message.appendAttachments(paths);
176 
177     return m_service->send(message);
178 }
179 
stateChanged(QMessageService::State s)180 void Message::stateChanged(QMessageService::State s)
181 {
182     emit messageStateChanged(s);
183 }
184