emailuis/nmailui/src/nmsendserviceinterface.cpp
branchRCL_3
changeset 63 d189ee25cf9d
equal deleted inserted replaced
61:dcf0eedfc1a3 63:d189ee25cf9d
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: NMail Application Launcher interface used for interfacing between
       
    15 *              QT highway and other applications
       
    16 *
       
    17 */
       
    18 
       
    19 //  INCLUDES
       
    20 #include "nmuiheaders.h"
       
    21 
       
    22 /*!
       
    23     \class NmStartParamDataHelper
       
    24     \brief A helper class for processing the data given to the actual service.
       
    25 */
       
    26 class NmStartParamDataHelper
       
    27 {
       
    28 public:
       
    29 
       
    30     /*!
       
    31         Class constructor.
       
    32     */
       
    33     inline NmStartParamDataHelper()
       
    34     : mSubject(0),
       
    35       mToAddresses(0),
       
    36       mCcAddresses(0),
       
    37       mBccAddresses(0),
       
    38       mAttachmentList(0),
       
    39       mEditorStartMode(NmUiEditorCreateNew)
       
    40     {
       
    41         NM_FUNCTION;
       
    42     }
       
    43 
       
    44     /*!
       
    45         Class destructor.
       
    46     */
       
    47     inline ~NmStartParamDataHelper()
       
    48     {
       
    49         NM_FUNCTION;
       
    50     }
       
    51 
       
    52     /*!
       
    53         Extracts the data from the given QVariant into the class members.
       
    54         \param data QVariant containing the data.
       
    55         \return True if success, false otherwise.
       
    56     */
       
    57     inline bool extractData(const QVariant &data)
       
    58     {
       
    59         NM_FUNCTION;
       
    60         
       
    61         bool success(false);
       
    62 
       
    63         if (data.canConvert(QVariant::Map)) {
       
    64             // The given data may contain a mail subject and recipient lists.
       
    65             QMap<QString, QVariant> map = data.toMap();
       
    66             success = processMap(map);
       
    67         }
       
    68         else if (data.canConvert(QVariant::StringList)) {
       
    69             QStringList attachmentList = data.toStringList();
       
    70             mAttachmentList = new QStringList(attachmentList);
       
    71             success = true;
       
    72         }
       
    73         else {
       
    74             // Data type not supported!
       
    75             NM_ERROR(1,"NmStartParamDataHelper::extractData(): data type not supported");
       
    76         }
       
    77 
       
    78         // Determine the editor start mode.
       
    79         if (mToAddresses || mCcAddresses || mBccAddresses) {
       
    80             mEditorStartMode = NmUiEditorMailto;
       
    81         }
       
    82         else if (mAttachmentList) {
       
    83             mEditorStartMode = NmUiEditorCreateNew;
       
    84         }
       
    85 
       
    86         return success;
       
    87     }
       
    88 
       
    89     /*!
       
    90         Deletes the class members. Must be used if NmUiStartParam does not
       
    91         take ownership of the members.
       
    92     */
       
    93     inline void deleteData()
       
    94     {
       
    95         NM_FUNCTION;
       
    96         
       
    97         delete mSubject;
       
    98         mSubject = 0;
       
    99 
       
   100         if (mToAddresses) {
       
   101             qDeleteAll(*mToAddresses);
       
   102             delete mToAddresses;
       
   103             mToAddresses = 0;
       
   104         }
       
   105 
       
   106         if (mCcAddresses) {
       
   107             qDeleteAll(*mCcAddresses);
       
   108             delete mCcAddresses;
       
   109             mCcAddresses = 0;
       
   110         }
       
   111 
       
   112         if (mBccAddresses) {
       
   113             qDeleteAll(*mBccAddresses);
       
   114             delete mBccAddresses;
       
   115             mBccAddresses = 0;
       
   116         }
       
   117 
       
   118         delete mAttachmentList;
       
   119         mAttachmentList = 0;
       
   120     }
       
   121 
       
   122 
       
   123 private:
       
   124 
       
   125     /*!
       
   126         Extracts the data from the given map into the class members.
       
   127         \param map The map to extract the data from.
       
   128         \return True if success, false otherwise.
       
   129     */
       
   130     inline bool processMap(const QMap<QString, QVariant> &map)
       
   131     {
       
   132         NM_FUNCTION;
       
   133         
       
   134         QMap<QString, QVariant>::const_iterator i = map.constBegin();
       
   135         QString key;
       
   136         QVariant value;
       
   137 
       
   138         while (i != map.constEnd()) {
       
   139             key = i.key();
       
   140             value = i.value();
       
   141 
       
   142             if (!key.compare(emailSendSubjectKey, Qt::CaseInsensitive) &&
       
   143                 value.type() == QVariant::String) {
       
   144                 // Extract the mail subject.
       
   145                 if (mSubject) {
       
   146                     // A subject string has already been extracted! Discard the
       
   147                     // old subject.
       
   148                     delete mSubject;
       
   149                 }
       
   150 
       
   151                 mSubject = new QString(value.toString());
       
   152             }
       
   153             else if (!key.compare(emailSendToKey, Qt::CaseInsensitive)) {
       
   154                 // Extract the "to" recipients.
       
   155                 addAddressesToList(value, &mToAddresses);
       
   156             }
       
   157             else if (!key.compare(emailSendCcKey, Qt::CaseInsensitive)) {
       
   158                 // Extract the "cc" recipients.
       
   159                 addAddressesToList(value, &mCcAddresses);
       
   160             }
       
   161             else if (!key.compare(emailSendBccKey, Qt::CaseInsensitive)) {
       
   162                 // Extract the "bcc" recipients.
       
   163                 addAddressesToList(value, &mBccAddresses);
       
   164             }
       
   165 
       
   166             ++i;
       
   167         }
       
   168 
       
   169         // In principle it is ok even if the map does not contain any data e.g.
       
   170         // in a case where the editor view needs to be opened without any
       
   171         // predefined data. Therefore, we always return true.
       
   172         return true;
       
   173     }
       
   174 
       
   175     /*!
       
   176         Appends the given addresses into the given list.
       
   177         \param address The addresses to append.
       
   178         \param list The list where the addresses are appended to.
       
   179     */
       
   180     inline void addAddressesToList(const QVariant &addresses,
       
   181                                    QList<NmAddress*> **list)
       
   182     {
       
   183         NM_FUNCTION;
       
   184         
       
   185         if (!list) {
       
   186             // Invalid argument!
       
   187             return;
       
   188         }
       
   189 
       
   190         QVariant::Type dataType = addresses.type();
       
   191         QList<NmAddress*> foundAddresses;
       
   192 
       
   193         switch (dataType) {
       
   194             case QVariant::String: {
       
   195                 // A single address.
       
   196                 NmAddress *address = new NmAddress(addresses.toString());
       
   197                 foundAddresses.append(address);
       
   198                 break;
       
   199             }
       
   200             case QVariant::StringList: {
       
   201                 // A list of addresses.
       
   202                 QStringList addressList = addresses.toStringList();
       
   203 
       
   204                 foreach (QString addressAsString, addressList) {
       
   205                     NmAddress *address = new NmAddress(addressAsString);
       
   206                     foundAddresses.append(address);
       
   207                 }
       
   208 
       
   209                 break;
       
   210             }
       
   211             default: {
       
   212                 break;
       
   213             }
       
   214         }
       
   215 
       
   216         if (foundAddresses.count()) {
       
   217             // Append the found addresses into the given list.
       
   218             if (!(*list)) {
       
   219                 *list = new QList<NmAddress*>();
       
   220             }
       
   221 
       
   222             (*list)->append(foundAddresses);
       
   223         }
       
   224     }
       
   225 
       
   226 
       
   227 public: // Data
       
   228 
       
   229     QString *mSubject; // Not owned.
       
   230     QList<NmAddress*> *mToAddresses; // Not owned.
       
   231     QList<NmAddress*> *mCcAddresses; // Not owned.
       
   232     QList<NmAddress*> *mBccAddresses; // Not owned.
       
   233     QStringList *mAttachmentList; // Not owned.
       
   234     NmUiEditorStartMode mEditorStartMode;
       
   235 };
       
   236 
       
   237 
       
   238 
       
   239 /*!
       
   240     \class NmSendServiceInterface
       
   241     \brief NMail application service interface which provides an email sending
       
   242            interface for other application using the Qt Highway.
       
   243 */
       
   244 
       
   245 /*!
       
   246     Class constructor.
       
   247 */
       
   248 NmSendServiceInterface::NmSendServiceInterface(QString interfaceName,
       
   249                                                QObject *parent,
       
   250                                                NmUiEngine &uiEngine,
       
   251                                                NmApplication *application)
       
   252     : XQServiceProvider(interfaceName, parent),
       
   253       mApplication(application),
       
   254       mUiEngine(uiEngine),
       
   255       mAsyncReqId(0),
       
   256       mStartParam(NULL),
       
   257       mSelectionDialog(NULL),
       
   258       mCurrentView(NULL)
       
   259 {
       
   260     publishAll();
       
   261 }
       
   262 
       
   263 
       
   264 /*!
       
   265     Class desctructor.
       
   266 */
       
   267 NmSendServiceInterface::~NmSendServiceInterface()
       
   268 {
       
   269     NM_FUNCTION;
       
   270     
       
   271     delete mStartParam;
       
   272     delete mSelectionDialog;
       
   273 }
       
   274 
       
   275 
       
   276 /*!
       
   277     Queries the user for a mailbox to use.
       
   278     \param mailboxId Where the ID of the selected mailbox is set.
       
   279     \return True if a mailbox was selected, false otherwise.
       
   280 */
       
   281 void NmSendServiceInterface::selectionDialogClosed(NmId &mailboxId)
       
   282 {
       
   283     NM_FUNCTION;
       
   284     
       
   285     if (mailboxId.id()) { // mailbox selected
       
   286         launchEditorView(mailboxId);
       
   287     }
       
   288     else {
       
   289         cancelService();
       
   290     }
       
   291 }
       
   292 
       
   293 
       
   294 /*!
       
   295     Used for sending email messages from external applications. Used by the
       
   296     Share UI. In addition, the interface can be used to send email to a contact
       
   297     from another application. The method sets the given data (e.g. recipients,
       
   298     attachments) to a mail composer (editor) view and displays it.
       
   299 
       
   300     \param data If used by Share UI, will contain the list of filenames to
       
   301                 attach. Can also contain a map with key value pairs containing
       
   302                 subject and recipient data.
       
   303 */
       
   304 void NmSendServiceInterface::send(QVariant data)
       
   305 {
       
   306     NM_FUNCTION;
       
   307     
       
   308     HbMainWindow *mainWindow(NULL);
       
   309     
       
   310     // Make sure that qmail stays background if user presses back in editorview
       
   311     if (mApplication) {
       
   312         mApplication->updateVisibilityState();
       
   313         
       
   314         mainWindow = mApplication->mainWindow();
       
   315         mCurrentView = mainWindow->currentView();
       
   316     
       
   317         // Hide the current view.
       
   318         if (mCurrentView) {
       
   319             mCurrentView->hide();
       
   320         }    
       
   321     }
       
   322 
       
   323     // Check the given data.
       
   324     NmStartParamDataHelper dataHelper;
       
   325     bool validData = dataHelper.extractData(data);
       
   326 
       
   327     NmMailboxListModel &mailboxListModel = mUiEngine.mailboxListModel();
       
   328     const int count = mailboxListModel.rowCount();
       
   329     NmId mailboxId(0);
       
   330 
       
   331     mAsyncReqId = setCurrentRequestAsync();
       
   332 
       
   333     if (!validData) {
       
   334         // Failed to extract the data!
       
   335         NM_ERROR(1,"NmSendServiceInterface::send(): failed to process the given data");
       
   336         cancelService();
       
   337     }
       
   338     else if (count == 0) {
       
   339         HbDeviceMessageBox note(hbTrId("txt_mail_dialog_no_mailboxes_defined"),
       
   340                           HbMessageBox::MessageTypeInformation);
       
   341         note.setTimeout(HbMessageBox::NoTimeout);
       
   342         note.show();
       
   343         cancelService();
       
   344     }
       
   345     else { // count > 0
       
   346         if (mainWindow) {
       
   347             mainWindow->show();        
       
   348         }
       
   349 
       
   350     	mStartParam = new NmUiStartParam(
       
   351         	NmUiViewMessageEditor,
       
   352 	        0, // account id
       
   353 	        0, // folder id
       
   354     	    0, // message id
       
   355         	dataHelper.mEditorStartMode, // editor start mode
       
   356 	        dataHelper.mToAddresses, // address list
       
   357     	    dataHelper.mAttachmentList, // attachment list
       
   358         	true, // start as service
       
   359 	        dataHelper.mSubject, // message subject
       
   360 	        dataHelper.mCcAddresses, // list containing cc recipient addresses
       
   361     	    dataHelper.mBccAddresses // list containing bcc recipient addresses
       
   362 	    );
       
   363 
       
   364         if (count == 1) {
       
   365             // A single mailbox exists.
       
   366             QModelIndex modelIndex = mailboxListModel.index(0, 0);
       
   367             QVariant mailbox(mailboxListModel.data(modelIndex));
       
   368             NmMailboxMetaData *mailboxMetaData = mailbox.value<NmMailboxMetaData*>();
       
   369             mailboxId = mailboxMetaData->id();
       
   370         	launchEditorView(mailboxId);
       
   371         }
       
   372         else { // count > 1
       
   373             if (!mSelectionDialog) {
       
   374                 mSelectionDialog =
       
   375                     new NmMailboxSelectionDialog(mUiEngine.mailboxListModel());
       
   376             }
       
   377 
       
   378             if (!XQServiceUtil::isEmbedded()) {
       
   379                 XQServiceUtil::toBackground(false);
       
   380             }
       
   381             
       
   382             connect(mSelectionDialog, SIGNAL(selectionDialogClosed(NmId&)),
       
   383                     this, SLOT(selectionDialogClosed(NmId&)));
       
   384             mSelectionDialog->open();
       
   385 
       
   386             // launch the editor when the dialog is closed
       
   387         }
       
   388     }
       
   389 }
       
   390 
       
   391 /*!
       
   392     Called when mailbox id is know and editor can be opened
       
   393     \param mailboxId mailbox using in editor
       
   394  */
       
   395 void NmSendServiceInterface::launchEditorView(NmId mailboxId)
       
   396 {
       
   397     NM_FUNCTION;
       
   398     NM_COMMENT(QString("NmSendServiceInterface::launchEditorView(): mailboxId=%1").arg(mailboxId.id()));
       
   399     
       
   400     // Make the previous view visible again.
       
   401     if (mCurrentView) {
       
   402         mCurrentView->show();
       
   403         mCurrentView = NULL;
       
   404     }
       
   405 
       
   406     if (mStartParam) {
       
   407         // Make sure the NMail application is in the foreground
       
   408         if (!XQServiceUtil::isEmbedded()) {
       
   409             XQServiceUtil::toBackground(false);    
       
   410         }
       
   411         
       
   412         mStartParam->setMailboxId(mailboxId);
       
   413         mApplication->enterNmUiView(mStartParam);
       
   414         mStartParam = NULL; // ownership passed
       
   415     }
       
   416     completeRequest(mAsyncReqId, 1);
       
   417     mAsyncReqId = 0;
       
   418 }
       
   419 
       
   420 void NmSendServiceInterface::cancelService()
       
   421 {
       
   422     NM_FUNCTION;
       
   423     
       
   424     delete mStartParam;
       
   425     mStartParam = NULL;
       
   426 
       
   427     // If the service was started as embedded, do not hide the app.
       
   428     if (!XQServiceUtil::isEmbedded()) {
       
   429         XQServiceUtil::toBackground(true);
       
   430     }
       
   431 
       
   432     completeRequest(mAsyncReqId, 0);
       
   433     mAsyncReqId = 0;
       
   434 
       
   435     // If started as service, the application must be closed now.
       
   436     if (XQServiceUtil::isService()) {
       
   437         connect(this, SIGNAL(returnValueDelivered()),
       
   438             mApplication, SLOT(delayedExitApplication()));
       
   439     }
       
   440     else {
       
   441         // Make the previous view visible again
       
   442         if (mCurrentView) {
       
   443             mCurrentView->show();
       
   444             mCurrentView = NULL;
       
   445         }
       
   446     }
       
   447 }
       
   448 
       
   449 // End of file.