qtmobility/examples/writemessage/messagesender.cpp
changeset 1 2b40d63a9c3d
child 4 90517678cc4f
equal deleted inserted replaced
0:cfcbf08528c4 1:2b40d63a9c3d
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #if defined(Q_OS_SYMBIAN)
       
    43 // Maximized windows are resizing correctly on S60 w/ Qt 4.6.0
       
    44 // Use tabs to reduce the minimal height required
       
    45 #define USE_TABBED_LAYOUT
       
    46 #endif
       
    47 
       
    48 #include "messagesender.h"
       
    49 
       
    50 #include <QComboBox>
       
    51 #include <QCoreApplication>
       
    52 #include <QDateTime>
       
    53 #include <QFileDialog>
       
    54 #include <QGroupBox>
       
    55 #include <QLabel>
       
    56 #include <QLayout>
       
    57 #include <QLineEdit>
       
    58 #include <QListWidget>
       
    59 #include <QMessageBox>
       
    60 #include <QPushButton>
       
    61 #include <QTextEdit>
       
    62 #include <QTimer>
       
    63 #include <QDebug>
       
    64 #include <QScrollArea>
       
    65 
       
    66 #ifdef USE_TABBED_LAYOUT
       
    67 #include <QTabWidget>
       
    68 #endif
       
    69 
       
    70 MessageSender::MessageSender(QWidget *parent, Qt::WindowFlags flags)
       
    71     : QWidget(parent, flags),
       
    72       accountCombo(0),
       
    73       toEdit(0),
       
    74       subjectEdit(0),
       
    75       textEdit(0),
       
    76       sendButton(0),
       
    77       removeButton(0),
       
    78       addButton(0),
       
    79       attachmentsList(0)
       
    80 {
       
    81     QVBoxLayout* vbl = new QVBoxLayout(this);
       
    82     vbl->setSpacing(0);
       
    83     vbl->setContentsMargins(0,0,0,0);
       
    84 
       
    85     QWidget* mainWidget = new QWidget(this);
       
    86 
       
    87     QScrollArea* sa = new QScrollArea(this);
       
    88     vbl->addWidget(sa);
       
    89     sa->setWidget(mainWidget);
       
    90     sa->setWidgetResizable(true);
       
    91 
       
    92     setWindowTitle(tr("Write Message"));
       
    93 
       
    94     accountCombo = new QComboBox;
       
    95     connect(accountCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(accountSelected(int)));
       
    96 
       
    97     toEdit = new QLineEdit;
       
    98 
       
    99     subjectEdit = new QLineEdit;
       
   100 
       
   101     QLabel *accountLabel = new QLabel(tr("Account"));
       
   102     accountLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
       
   103 
       
   104     QLabel *toLabel = new QLabel(tr("To"));
       
   105     toLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
       
   106 
       
   107     QLabel *subjectLabel = new QLabel(tr("Subject"));
       
   108     subjectLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
       
   109 
       
   110     QGridLayout *metaDataLayout = new QGridLayout;
       
   111     metaDataLayout->setContentsMargins(0, 0, 0, 0);
       
   112     metaDataLayout->addWidget(accountLabel, 0, 0);
       
   113     metaDataLayout->setAlignment(accountLabel, Qt::AlignRight);
       
   114     metaDataLayout->addWidget(toLabel, 1, 0);
       
   115     metaDataLayout->setAlignment(toLabel, Qt::AlignRight);
       
   116     metaDataLayout->addWidget(subjectLabel, 2, 0);
       
   117     metaDataLayout->setAlignment(subjectLabel, Qt::AlignRight);
       
   118     metaDataLayout->addWidget(accountCombo, 0, 1);
       
   119     metaDataLayout->addWidget(toEdit, 1, 1);
       
   120     metaDataLayout->addWidget(subjectEdit, 2, 1);
       
   121 
       
   122     removeButton = new QPushButton(tr("Remove"));
       
   123     removeButton->setEnabled(false);
       
   124 
       
   125     connect(removeButton, SIGNAL(clicked()), this, SLOT(removeAttachment()));
       
   126 
       
   127     addButton = new QPushButton(tr("Add"));
       
   128 
       
   129     connect(addButton, SIGNAL(clicked()), this, SLOT(addAttachment()));
       
   130 
       
   131     QHBoxLayout *buttonLayout = new QHBoxLayout;
       
   132     buttonLayout->setContentsMargins(0, 0, 0, 0);
       
   133     buttonLayout->addWidget(addButton);
       
   134     buttonLayout->addWidget(removeButton);
       
   135 
       
   136     attachmentsList = new QListWidget;
       
   137     attachmentsList->setSelectionMode(QAbstractItemView::SingleSelection);
       
   138 
       
   139     connect(attachmentsList, SIGNAL(currentRowChanged(int)), this, SLOT(attachmentSelected(int)));
       
   140 
       
   141     QVBoxLayout *attachmentsLayout = new QVBoxLayout;
       
   142     attachmentsLayout->setContentsMargins(0, 0, 0, 0);
       
   143     attachmentsLayout->addWidget(attachmentsList);
       
   144     attachmentsLayout->addLayout(buttonLayout);
       
   145 
       
   146     QGroupBox *attachmentsGroup = new QGroupBox(tr("Attachments"));
       
   147     attachmentsGroup->setLayout(attachmentsLayout);
       
   148 
       
   149     textEdit = new QTextEdit;
       
   150 
       
   151     sendButton = new QPushButton(tr("Send"));
       
   152 
       
   153     connect(sendButton, SIGNAL(clicked()), this, SLOT(send()), Qt::QueuedConnection);
       
   154 
       
   155 #ifdef USE_TABBED_LAYOUT
       
   156     QTabWidget *tabWidget = new QTabWidget;
       
   157     tabWidget->addTab(textEdit, tr("Text"));
       
   158     tabWidget->addTab(attachmentsGroup, tr("Attachments"));
       
   159 #endif
       
   160 
       
   161     QVBoxLayout *mainLayout = new QVBoxLayout(mainWidget);
       
   162     mainLayout->setContentsMargins(0, 0, 0, 0);
       
   163     mainLayout->addLayout(metaDataLayout, 0);
       
   164 #ifdef USE_TABBED_LAYOUT
       
   165     mainLayout->addWidget(tabWidget, 0);
       
   166 #else
       
   167     mainLayout->addWidget(textEdit, 2);
       
   168     mainLayout->addWidget(attachmentsGroup, 1);
       
   169 #endif
       
   170     mainLayout->addWidget(sendButton, 0);
       
   171     mainLayout->setAlignment(sendButton, Qt::AlignRight);
       
   172 
       
   173     connect(&service, SIGNAL(stateChanged(QMessageService::State)), this, SLOT(stateChanged(QMessageService::State)));
       
   174 
       
   175     QTimer::singleShot(0, this, SLOT(populateAccounts()));
       
   176 }
       
   177 
       
   178 MessageSender::~MessageSender()
       
   179 {
       
   180 }
       
   181 
       
   182 void MessageSender::populateAccounts()
       
   183 {
       
   184 #ifndef _WIN32_WCE
       
   185     // How long can the accounts query take?
       
   186     setCursor(Qt::BusyCursor);
       
   187 #endif
       
   188 
       
   189 //! [accounts-listing]
       
   190     // Find the list of available accounts and add them to combo box
       
   191     foreach (const QMessageAccountId &id, manager.queryAccounts()) {
       
   192         QMessageAccount account(id);
       
   193 
       
   194         // What is the most capable message type supported by this account?
       
   195         QMessage::Type type(QMessage::NoType);
       
   196         if (account.messageTypes() & QMessage::Email) {
       
   197             type = QMessage::Email;
       
   198         } else if (account.messageTypes() & QMessage::Mms) {
       
   199             type = QMessage::Mms;
       
   200         } else if (account.messageTypes() & QMessage::Sms) {
       
   201             type = QMessage::Sms;
       
   202         }
       
   203 
       
   204         if (type != QMessage::NoType) {
       
   205             QString name(account.name());
       
   206             accountDetails.insert(name, qMakePair(type, account.id()));
       
   207             accountCombo->addItem(name);
       
   208         }
       
   209     }
       
   210 //! [accounts-listing]
       
   211 
       
   212     if (accountDetails.isEmpty()) {
       
   213         QMessageBox::warning(0, tr("Cannot send"), tr("No accounts are available to send with!"));
       
   214         QCoreApplication::instance()->quit();
       
   215     } else {
       
   216         accountCombo->setCurrentIndex(0);
       
   217     }
       
   218 
       
   219 #ifndef _WIN32_WCE
       
   220     setCursor(Qt::ArrowCursor);
       
   221 #endif
       
   222 }
       
   223 
       
   224 void MessageSender::removeAttachment()
       
   225 {
       
   226     delete attachmentsList->takeItem(attachmentsList->currentRow());
       
   227     if (attachmentsList->count() == 0) {
       
   228         removeButton->setEnabled(false);
       
   229     }
       
   230 }
       
   231 
       
   232 void MessageSender::addAttachment()
       
   233 {
       
   234     QString path(QFileDialog::getOpenFileName());
       
   235     if (!path.isEmpty()) {
       
   236         attachmentsList->addItem(new QListWidgetItem(path));
       
   237     }
       
   238 }
       
   239 
       
   240 //! [account-selected]
       
   241 void MessageSender::accountSelected(int index)
       
   242 {
       
   243     QString name(accountCombo->itemText(index));
       
   244     if (!name.isEmpty()) {
       
   245         QPair<QMessage::Type, QMessageAccountId> details = accountDetails[name];
       
   246 
       
   247         // Enable subject only for email
       
   248         subjectEdit->setEnabled(details.first == QMessage::Email);
       
   249 
       
   250         // Disable attachments for SMS
       
   251         const bool smsOnly(details.first == QMessage::Sms);
       
   252         addButton->setEnabled(!smsOnly);
       
   253         removeButton->setEnabled(!smsOnly);
       
   254     }
       
   255 }
       
   256 //! [account-selected]
       
   257 
       
   258 void MessageSender::attachmentSelected(int index)
       
   259 {
       
   260     removeButton->setEnabled(index != -1);
       
   261 }
       
   262 
       
   263 void MessageSender::send()
       
   264 {
       
   265 //! [associate-account]
       
   266     QString accountName(accountCombo->currentText());
       
   267     if (accountName.isEmpty()) {
       
   268         QMessageBox::warning(0, tr("Missing information"), tr("No account is selected for transmission"));
       
   269         return;
       
   270     }
       
   271 
       
   272     QMessage message;
       
   273 
       
   274     QPair<QMessage::Type, QMessageAccountId> details = accountDetails[accountName];
       
   275     message.setType(details.first);
       
   276     message.setParentAccountId(details.second);
       
   277 //! [associate-account]
       
   278 
       
   279 //! [set-recipients]
       
   280     QString to(toEdit->text());
       
   281     if (to.isEmpty()) {
       
   282         QMessageBox::warning(0, tr("Missing information"), tr("Please enter a recipient address"));
       
   283         return;
       
   284     }
       
   285 
       
   286     // Find the address type to use for this message
       
   287     QMessageAddress::Type addrType(message.type() == QMessage::Email ? QMessageAddress::Email : QMessageAddress::Phone);
       
   288 
       
   289     QMessageAddressList toList;
       
   290     foreach (const QString &item, to.split(QRegExp("\\s"), QString::SkipEmptyParts)) {
       
   291         toList.append(QMessageAddress(addrType, item));
       
   292     }
       
   293     message.setTo(toList);
       
   294 //! [set-recipients]
       
   295 
       
   296 //! [set-properties]
       
   297     if (message.type() == QMessage::Email) {
       
   298         QString subject(subjectEdit->text());
       
   299         if (subject.isEmpty()) {
       
   300             QMessageBox::warning(0, tr("Missing information"), tr("Please enter a subject"));
       
   301             return;
       
   302         }
       
   303         message.setSubject(subject);
       
   304     }
       
   305 
       
   306     QString text(textEdit->toPlainText());
       
   307     if (text.isEmpty()) {
       
   308         QMessageBox::warning(0, tr("Missing information"), tr("Please enter a message"));
       
   309         return;
       
   310     }
       
   311     message.setBody(text);
       
   312 //! [set-properties]
       
   313 
       
   314 //! [add-attachments]
       
   315     if (message.type() != QMessage::Sms) {
       
   316         if (attachmentsList->count()) {
       
   317             QStringList paths;
       
   318             for (int i = 0; i < attachmentsList->count(); ++i) {
       
   319                 paths.append(attachmentsList->item(i)->text());
       
   320             }
       
   321 
       
   322             message.appendAttachments(paths);
       
   323         }
       
   324     }
       
   325 //! [add-attachments]
       
   326 
       
   327 //! [send-message]
       
   328     if (service.send(message)) {
       
   329         sendButton->setEnabled(false);
       
   330         sendId = message.id();
       
   331     } else {
       
   332         QMessageBox::warning(0, tr("Failed"), tr("Unable to send message"));
       
   333     }
       
   334 //! [send-message]
       
   335 }
       
   336 
       
   337 //! [handle-response]
       
   338 void MessageSender::stateChanged(QMessageService::State newState)
       
   339 {
       
   340     if (newState == QMessageService::FinishedState) {
       
   341         if (service.error() == QMessageManager::NoError) {
       
   342             QMessageBox::information(0, tr("Success"), tr("Message sent successfully"));
       
   343         } else {
       
   344             QMessageBox::warning(0, tr("Failed"), tr("Unable to send message"));
       
   345 
       
   346             // Try to delete the failed message
       
   347             if (!manager.removeMessage(sendId)) {
       
   348                 qWarning() << "Unable to remove failed message:" << sendId.toString();
       
   349             }
       
   350         }
       
   351 
       
   352         sendId = QMessageId();
       
   353         sendButton->setEnabled(true);
       
   354     }
       
   355 }
       
   356 //! [handle-response]
       
   357