|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:BSD$ |
|
10 ** You may use this file under the terms of the BSD license as follows: |
|
11 ** |
|
12 ** "Redistribution and use in source and binary forms, with or without |
|
13 ** modification, are permitted provided that the following conditions are |
|
14 ** met: |
|
15 ** * Redistributions of source code must retain the above copyright |
|
16 ** notice, this list of conditions and the following disclaimer. |
|
17 ** * Redistributions in binary form must reproduce the above copyright |
|
18 ** notice, this list of conditions and the following disclaimer in |
|
19 ** the documentation and/or other materials provided with the |
|
20 ** distribution. |
|
21 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
|
22 ** the names of its contributors may be used to endorse or promote |
|
23 ** products derived from this software without specific prior written |
|
24 ** permission. |
|
25 ** |
|
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
|
37 ** $QT_END_LICENSE$ |
|
38 ** |
|
39 ****************************************************************************/ |
|
40 |
|
41 #if defined(Q_OS_SYMBIAN) |
|
42 // Maximized windows are resizing correctly on S60 w/ Qt 4.6.0 |
|
43 // Use tabs to reduce the minimal height required |
|
44 #define USE_TABBED_LAYOUT |
|
45 #endif |
|
46 |
|
47 #include "messagesender.h" |
|
48 |
|
49 #include <QComboBox> |
|
50 #include <QCoreApplication> |
|
51 #include <QDateTime> |
|
52 #include <QFileDialog> |
|
53 #include <QGroupBox> |
|
54 #include <QLabel> |
|
55 #include <QLayout> |
|
56 #include <QLineEdit> |
|
57 #include <QListWidget> |
|
58 #include <QMessageBox> |
|
59 #include <QPushButton> |
|
60 #include <QTextEdit> |
|
61 #include <QTimer> |
|
62 #include <QDebug> |
|
63 #include <QScrollArea> |
|
64 |
|
65 #ifdef USE_TABBED_LAYOUT |
|
66 #include <QTabWidget> |
|
67 #endif |
|
68 |
|
69 MessageSender::MessageSender(QWidget *parent, Qt::WindowFlags flags) |
|
70 : QWidget(parent, flags), |
|
71 accountCombo(0), |
|
72 toEdit(0), |
|
73 subjectEdit(0), |
|
74 textEdit(0), |
|
75 sendButton(0), |
|
76 removeButton(0), |
|
77 addButton(0), |
|
78 attachmentsList(0) |
|
79 { |
|
80 QVBoxLayout* vbl = new QVBoxLayout(this); |
|
81 vbl->setSpacing(0); |
|
82 vbl->setContentsMargins(0,0,0,0); |
|
83 |
|
84 QWidget* mainWidget = new QWidget(this); |
|
85 |
|
86 QScrollArea* sa = new QScrollArea(this); |
|
87 vbl->addWidget(sa); |
|
88 sa->setWidget(mainWidget); |
|
89 sa->setWidgetResizable(true); |
|
90 |
|
91 setWindowTitle(tr("Write Message")); |
|
92 |
|
93 accountCombo = new QComboBox; |
|
94 connect(accountCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(accountSelected(int))); |
|
95 |
|
96 toEdit = new QLineEdit; |
|
97 |
|
98 subjectEdit = new QLineEdit; |
|
99 |
|
100 QLabel *accountLabel = new QLabel(tr("Account")); |
|
101 accountLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
|
102 |
|
103 QLabel *toLabel = new QLabel(tr("To")); |
|
104 toLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
|
105 |
|
106 QLabel *subjectLabel = new QLabel(tr("Subject")); |
|
107 subjectLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
|
108 |
|
109 QGridLayout *metaDataLayout = new QGridLayout; |
|
110 metaDataLayout->setContentsMargins(0, 0, 0, 0); |
|
111 metaDataLayout->addWidget(accountLabel, 0, 0); |
|
112 metaDataLayout->setAlignment(accountLabel, Qt::AlignRight); |
|
113 metaDataLayout->addWidget(toLabel, 1, 0); |
|
114 metaDataLayout->setAlignment(toLabel, Qt::AlignRight); |
|
115 metaDataLayout->addWidget(subjectLabel, 2, 0); |
|
116 metaDataLayout->setAlignment(subjectLabel, Qt::AlignRight); |
|
117 metaDataLayout->addWidget(accountCombo, 0, 1); |
|
118 metaDataLayout->addWidget(toEdit, 1, 1); |
|
119 metaDataLayout->addWidget(subjectEdit, 2, 1); |
|
120 |
|
121 removeButton = new QPushButton(tr("Remove")); |
|
122 removeButton->setEnabled(false); |
|
123 |
|
124 connect(removeButton, SIGNAL(clicked()), this, SLOT(removeAttachment())); |
|
125 |
|
126 addButton = new QPushButton(tr("Add")); |
|
127 |
|
128 connect(addButton, SIGNAL(clicked()), this, SLOT(addAttachment())); |
|
129 |
|
130 QHBoxLayout *buttonLayout = new QHBoxLayout; |
|
131 buttonLayout->setContentsMargins(0, 0, 0, 0); |
|
132 buttonLayout->addWidget(addButton); |
|
133 buttonLayout->addWidget(removeButton); |
|
134 |
|
135 attachmentsList = new QListWidget; |
|
136 attachmentsList->setSelectionMode(QAbstractItemView::SingleSelection); |
|
137 |
|
138 connect(attachmentsList, SIGNAL(currentRowChanged(int)), this, SLOT(attachmentSelected(int))); |
|
139 |
|
140 QVBoxLayout *attachmentsLayout = new QVBoxLayout; |
|
141 attachmentsLayout->setContentsMargins(0, 0, 0, 0); |
|
142 attachmentsLayout->addWidget(attachmentsList); |
|
143 attachmentsLayout->addLayout(buttonLayout); |
|
144 |
|
145 QGroupBox *attachmentsGroup = new QGroupBox(tr("Attachments")); |
|
146 attachmentsGroup->setLayout(attachmentsLayout); |
|
147 #ifdef Q_WS_MAEMO_5 |
|
148 // Maemo 5 style doesn't take group box titles into account. |
|
149 int spacingHack = QFontMetrics(QFont()).height(); |
|
150 attachmentsLayout->setContentsMargins(0, spacingHack, 0, 0); |
|
151 #endif |
|
152 |
|
153 textEdit = new QTextEdit; |
|
154 |
|
155 sendButton = new QPushButton(tr("Send")); |
|
156 |
|
157 connect(sendButton, SIGNAL(clicked()), this, SLOT(send()), Qt::QueuedConnection); |
|
158 |
|
159 #ifdef USE_TABBED_LAYOUT |
|
160 QTabWidget *tabWidget = new QTabWidget; |
|
161 tabWidget->addTab(textEdit, tr("Text")); |
|
162 tabWidget->addTab(attachmentsGroup, tr("Attachments")); |
|
163 #endif |
|
164 |
|
165 QVBoxLayout *mainLayout = new QVBoxLayout(mainWidget); |
|
166 mainLayout->setContentsMargins(0, 0, 0, 0); |
|
167 mainLayout->addLayout(metaDataLayout, 0); |
|
168 #ifdef USE_TABBED_LAYOUT |
|
169 mainLayout->addWidget(tabWidget, 0); |
|
170 #else |
|
171 mainLayout->addWidget(textEdit, 2); |
|
172 mainLayout->addWidget(attachmentsGroup, 1); |
|
173 #endif |
|
174 mainLayout->addWidget(sendButton, 0); |
|
175 mainLayout->setAlignment(sendButton, Qt::AlignRight); |
|
176 |
|
177 connect(&service, SIGNAL(stateChanged(QMessageService::State)), this, SLOT(stateChanged(QMessageService::State))); |
|
178 |
|
179 QTimer::singleShot(0, this, SLOT(populateAccounts())); |
|
180 |
|
181 QWidgetList focusableWidgets; |
|
182 focusableWidgets << accountCombo |
|
183 << toEdit |
|
184 << subjectEdit |
|
185 #ifdef USE_TABBED_LAYOUT |
|
186 << tabWidget |
|
187 #else |
|
188 << textEdit |
|
189 << attachmentsList |
|
190 #endif |
|
191 << addButton |
|
192 << removeButton |
|
193 << sendButton; |
|
194 |
|
195 foreach(QWidget* w, focusableWidgets) |
|
196 w->setContextMenuPolicy(Qt::NoContextMenu); |
|
197 |
|
198 accountCombo->setFocus(); |
|
199 } |
|
200 |
|
201 MessageSender::~MessageSender() |
|
202 { |
|
203 } |
|
204 |
|
205 void MessageSender::populateAccounts() |
|
206 { |
|
207 #ifndef _WIN32_WCE |
|
208 // How long can the accounts query take? |
|
209 setCursor(Qt::BusyCursor); |
|
210 #endif |
|
211 |
|
212 //! [accounts-listing] |
|
213 // Find the list of available accounts and add them to combo box |
|
214 foreach (const QMessageAccountId &id, manager.queryAccounts()) { |
|
215 QMessageAccount account(id); |
|
216 |
|
217 // What is the most capable message type supported by this account? |
|
218 QMessage::Type type(QMessage::NoType); |
|
219 if (account.messageTypes() & QMessage::Email) { |
|
220 type = QMessage::Email; |
|
221 } else if (account.messageTypes() & QMessage::Mms) { |
|
222 type = QMessage::Mms; |
|
223 } else if (account.messageTypes() & QMessage::Sms) { |
|
224 type = QMessage::Sms; |
|
225 } |
|
226 |
|
227 if (type != QMessage::NoType) { |
|
228 QString name(account.name()); |
|
229 accountDetails.insert(name, qMakePair(type, account.id())); |
|
230 accountCombo->addItem(name); |
|
231 } |
|
232 } |
|
233 //! [accounts-listing] |
|
234 |
|
235 if (accountDetails.isEmpty()) { |
|
236 QMessageBox::warning(0, tr("Cannot send"), tr("No accounts are available to send with!")); |
|
237 QCoreApplication::instance()->quit(); |
|
238 } else { |
|
239 accountCombo->setCurrentIndex(0); |
|
240 } |
|
241 |
|
242 #ifndef _WIN32_WCE |
|
243 setCursor(Qt::ArrowCursor); |
|
244 #endif |
|
245 } |
|
246 |
|
247 void MessageSender::removeAttachment() |
|
248 { |
|
249 delete attachmentsList->takeItem(attachmentsList->currentRow()); |
|
250 if (attachmentsList->count() == 0) { |
|
251 removeButton->setEnabled(false); |
|
252 } |
|
253 } |
|
254 |
|
255 void MessageSender::addAttachment() |
|
256 { |
|
257 QString path(QFileDialog::getOpenFileName()); |
|
258 if (!path.isEmpty()) { |
|
259 attachmentsList->addItem(new QListWidgetItem(path)); |
|
260 } |
|
261 } |
|
262 |
|
263 //! [account-selected] |
|
264 void MessageSender::accountSelected(int index) |
|
265 { |
|
266 QString name(accountCombo->itemText(index)); |
|
267 if (!name.isEmpty()) { |
|
268 QPair<QMessage::Type, QMessageAccountId> details = accountDetails[name]; |
|
269 |
|
270 // Enable subject only for email |
|
271 subjectEdit->setEnabled(details.first == QMessage::Email); |
|
272 |
|
273 // Disable attachments for SMS |
|
274 const bool smsOnly(details.first == QMessage::Sms); |
|
275 addButton->setEnabled(!smsOnly); |
|
276 removeButton->setEnabled(!smsOnly); |
|
277 } |
|
278 } |
|
279 //! [account-selected] |
|
280 |
|
281 void MessageSender::attachmentSelected(int index) |
|
282 { |
|
283 removeButton->setEnabled(index != -1); |
|
284 } |
|
285 |
|
286 void MessageSender::send() |
|
287 { |
|
288 //! [associate-account] |
|
289 QString accountName(accountCombo->currentText()); |
|
290 if (accountName.isEmpty()) { |
|
291 QMessageBox::warning(0, tr("Missing information"), tr("No account is selected for transmission")); |
|
292 return; |
|
293 } |
|
294 |
|
295 QMessage message; |
|
296 |
|
297 QPair<QMessage::Type, QMessageAccountId> details = accountDetails[accountName]; |
|
298 message.setType(details.first); |
|
299 message.setParentAccountId(details.second); |
|
300 //! [associate-account] |
|
301 |
|
302 //! [set-recipients] |
|
303 QString to(toEdit->text()); |
|
304 if (to.isEmpty()) { |
|
305 QMessageBox::warning(0, tr("Missing information"), tr("Please enter a recipient address")); |
|
306 return; |
|
307 } |
|
308 |
|
309 // Find the address type to use for this message |
|
310 QMessageAddress::Type addrType(message.type() == QMessage::Email ? QMessageAddress::Email : QMessageAddress::Phone); |
|
311 |
|
312 QMessageAddressList toList; |
|
313 foreach (const QString &item, to.split(QRegExp("\\s"), QString::SkipEmptyParts)) { |
|
314 toList.append(QMessageAddress(addrType, item)); |
|
315 } |
|
316 message.setTo(toList); |
|
317 //! [set-recipients] |
|
318 |
|
319 //! [set-properties] |
|
320 if (message.type() == QMessage::Email) { |
|
321 QString subject(subjectEdit->text()); |
|
322 if (subject.isEmpty()) { |
|
323 QMessageBox::warning(0, tr("Missing information"), tr("Please enter a subject")); |
|
324 return; |
|
325 } |
|
326 message.setSubject(subject); |
|
327 } |
|
328 |
|
329 QString text(textEdit->toPlainText()); |
|
330 if (text.isEmpty()) { |
|
331 QMessageBox::warning(0, tr("Missing information"), tr("Please enter a message")); |
|
332 return; |
|
333 } |
|
334 message.setBody(text); |
|
335 //! [set-properties] |
|
336 |
|
337 //! [add-attachments] |
|
338 if (message.type() != QMessage::Sms) { |
|
339 if (attachmentsList->count()) { |
|
340 QStringList paths; |
|
341 for (int i = 0; i < attachmentsList->count(); ++i) { |
|
342 paths.append(attachmentsList->item(i)->text()); |
|
343 } |
|
344 |
|
345 message.appendAttachments(paths); |
|
346 } |
|
347 } |
|
348 //! [add-attachments] |
|
349 |
|
350 //! [send-message] |
|
351 sendButton->setEnabled(false); |
|
352 if (service.send(message)) { |
|
353 sendId = message.id(); |
|
354 } else { |
|
355 sendButton->setEnabled(true); |
|
356 QMessageBox::warning(0, tr("Failed"), tr("Unable to send message")); |
|
357 } |
|
358 //! [send-message] |
|
359 } |
|
360 |
|
361 //! [handle-response] |
|
362 void MessageSender::stateChanged(QMessageService::State newState) |
|
363 { |
|
364 if (newState == QMessageService::FinishedState) { |
|
365 if (service.error() == QMessageManager::NoError) { |
|
366 QMessageBox::information(0, tr("Success"), tr("Message sent successfully")); |
|
367 } else { |
|
368 QMessageBox::warning(0, tr("Failed"), tr("Unable to send message")); |
|
369 |
|
370 // Try to delete the failed message |
|
371 if (!manager.removeMessage(sendId)) { |
|
372 qWarning() << "Unable to remove failed message:" << sendId.toString(); |
|
373 } |
|
374 } |
|
375 |
|
376 sendId = QMessageId(); |
|
377 sendButton->setEnabled(true); |
|
378 } |
|
379 } |
|
380 //! [handle-response] |
|
381 |