emailuis/nmailui/src/nmsendserviceinterface.cpp
changeset 23 2dc6caa42ec3
parent 20 ecc8def7944a
child 27 9ba4404ef423
equal deleted inserted replaced
20:ecc8def7944a 23:2dc6caa42ec3
    17 */
    17 */
    18 
    18 
    19 //  INCLUDES
    19 //  INCLUDES
    20 #include "nmuiheaders.h"
    20 #include "nmuiheaders.h"
    21 
    21 
    22 //  CONSTANTS
       
    23 static const QString emailSendSubjectKey = "subject";
       
    24 static const QString emailSendToKey = "to";
       
    25 static const QString emailSendCcKey = "cc";
       
    26 static const QString emailSendBccKey = "bcc";
       
    27 
       
    28 
       
    29 /*!
    22 /*!
    30     \class NmStartParamDataHelper
    23     \class NmStartParamDataHelper
    31     \brief A helper class for processing the data given to the actual service.
    24     \brief A helper class for processing the data given to the actual service.
    32 */
    25 */
    33 class NmStartParamDataHelper
    26 class NmStartParamDataHelper
   145     inline bool processMap(const QMap<QString, QVariant> &map)
   138     inline bool processMap(const QMap<QString, QVariant> &map)
   146     {
   139     {
   147         QMap<QString, QVariant>::const_iterator i = map.constBegin();
   140         QMap<QString, QVariant>::const_iterator i = map.constBegin();
   148         QString key;
   141         QString key;
   149         QVariant value;
   142         QVariant value;
   150         
   143 
   151         while (i != map.constEnd()) {
   144         while (i != map.constEnd()) {
   152             key = i.key();
   145             key = i.key();
   153             value = i.value();
   146             value = i.value();
   154             
   147 
   155             if (!key.compare(emailSendSubjectKey, Qt::CaseInsensitive) &&
   148             if (!key.compare(emailSendSubjectKey, Qt::CaseInsensitive) &&
   156                 value.type() == QVariant::String) {
   149                 value.type() == QVariant::String) {
   157                 // Extract the mail subject.
   150                 // Extract the mail subject.
   158                 if (mSubject) {
   151                 if (mSubject) {
   159                     // A subject string has already been extracted! Discard the
   152                     // A subject string has already been extracted! Discard the
   191         \param list The list where the addresses are appended to.
   184         \param list The list where the addresses are appended to.
   192     */
   185     */
   193     inline void addAddressesToList(const QVariant &addresses,
   186     inline void addAddressesToList(const QVariant &addresses,
   194                                    QList<NmAddress*> **list)
   187                                    QList<NmAddress*> **list)
   195     {
   188     {
   196       
   189 
   197         if (!list) {
   190         if (!list) {
   198             // Invalid argument!
   191             // Invalid argument!
   199             return;
   192             return;
   200         }
   193         }
   201 
   194 
   249 
   242 
   250 
   243 
   251 /*!
   244 /*!
   252     \class NmSendServiceInterface
   245     \class NmSendServiceInterface
   253     \brief NMail application service interface which provides an email sending
   246     \brief NMail application service interface which provides an email sending
   254            interface for other application using the Qt Highway. 
   247            interface for other application using the Qt Highway.
   255 */
   248 */
   256 
   249 
   257 /*!
   250 /*!
   258     Class constructor.
   251     Class constructor.
   259 */
   252 */
   260 NmSendServiceInterface::NmSendServiceInterface(QString interfaceName, 
   253 NmSendServiceInterface::NmSendServiceInterface(QString interfaceName,
   261                                                QObject *parent,
   254                                                QObject *parent,
   262                                                NmUiEngine &uiEngine,
   255                                                NmUiEngine &uiEngine,
   263                                                NmApplication *application)
   256                                                NmApplication *application)
   264 #ifndef NM_WINS_ENV
   257 #ifndef NM_WINS_ENV
   265     : XQServiceProvider(interfaceName, parent),
   258     : XQServiceProvider(interfaceName, parent),
   266 #else
   259 #else
   267     : QObject(parent),
   260     : QObject(parent),
   268 #endif
   261 #endif
   269       mApplication(application),
   262       mApplication(application),
   270       mUiEngine(uiEngine),
   263       mUiEngine(uiEngine),
   271       mAsyncReqId(0)
   264       mAsyncReqId(0),
       
   265       mStartParam(NULL),
       
   266       mSelectionDialog(NULL),
       
   267       mCurrentView(NULL)
   272 {
   268 {
   273 #ifndef NM_WINS_ENV
   269 #ifndef NM_WINS_ENV
   274     publishAll();
   270     publishAll();
   275 #endif
   271 #endif
   276 }
   272 }
   279 /*!
   275 /*!
   280     Class desctructor.
   276     Class desctructor.
   281 */
   277 */
   282 NmSendServiceInterface::~NmSendServiceInterface()
   278 NmSendServiceInterface::~NmSendServiceInterface()
   283 {
   279 {
       
   280     delete mStartParam;
       
   281     delete mSelectionDialog;
   284 }
   282 }
   285 
   283 
   286 
   284 
   287 /*!
   285 /*!
   288     Queries the user for a mailbox to use.
   286     Queries the user for a mailbox to use.
   289     \param mailboxId Where the ID of the selected mailbox is set.
   287     \param mailboxId Where the ID of the selected mailbox is set.
   290     \return True if a mailbox was selected, false otherwise.
   288     \return True if a mailbox was selected, false otherwise.
   291 */
   289 */
   292 bool NmSendServiceInterface::selectMailbox(NmId& mailboxId)
   290 void NmSendServiceInterface::selectionDialogClosed(NmId &mailboxId)
   293 {
   291 {
   294 	NmMailboxSelectionDialog *dialog =
   292     NMLOG("NmSendServiceInterface::selectionDialogClosed");
   295 	    new NmMailboxSelectionDialog(mUiEngine.mailboxListModel());
   293     if (mailboxId.id()) { // mailbox selected
   296 	bool ok = dialog->exec(mailboxId);
   294         launchEditorView(mailboxId);
   297 	delete dialog;
   295     }
   298 	return ok;
   296     else {
       
   297         cancelService();
       
   298     }
   299 }
   299 }
   300 
   300 
   301 
   301 
   302 /*!
   302 /*!
   303     Used for sending email messages from external applications. Used by the
   303     Used for sending email messages from external applications. Used by the
   305     from another application. The method sets the given data (e.g. recipients,
   305     from another application. The method sets the given data (e.g. recipients,
   306     attachments) to a mail composer (editor) view and displays it.
   306     attachments) to a mail composer (editor) view and displays it.
   307 
   307 
   308     \param data If used by Share UI, will contain the list of filenames to
   308     \param data If used by Share UI, will contain the list of filenames to
   309                 attach. Can also contain a map with key value pairs containing
   309                 attach. Can also contain a map with key value pairs containing
   310                 subject and recipient data. 
   310                 subject and recipient data.
   311 */
   311 */
   312 void NmSendServiceInterface::send(QVariant data)
   312 void NmSendServiceInterface::send(QVariant data)
   313 {
   313 {
   314     NMLOG("NmSendServiceInterface::send()");
   314     NMLOG("NmSendServiceInterface::send()");
   315 
   315 
   316 #ifndef NM_WINS_ENV
   316 #ifndef NM_WINS_ENV
   317     // Make sure the NMail application is on the foreground.
       
   318     XQServiceUtil::toBackground(false);
       
   319     HbMainWindow *mainWindow = mApplication->mainWindow();
   317     HbMainWindow *mainWindow = mApplication->mainWindow();
   320     HbView *currentView = mainWindow->currentView();
   318     mCurrentView = mainWindow->currentView();
   321 
   319 
   322     // Hide the current view.
   320     // Hide the current view.
   323     if (currentView) {
   321     if (mCurrentView) {
   324         currentView->hide();
   322         mCurrentView->hide();
   325     }
   323     }
   326 
   324 
   327     // Check the given data.
   325     // Check the given data.
   328     NmStartParamDataHelper dataHelper;
   326     NmStartParamDataHelper dataHelper;
   329     bool validData = dataHelper.extractData(data);
   327     bool validData = dataHelper.extractData(data);
   330 
   328 
   331     NmMailboxListModel &mailboxListModel = mUiEngine.mailboxListModel();
   329     NmMailboxListModel &mailboxListModel = mUiEngine.mailboxListModel();
   332     const int count = mailboxListModel.rowCount();
   330     const int count = mailboxListModel.rowCount();
   333     NmId mailboxId(0);
   331     NmId mailboxId(0);
   334     bool launchEditor(true);
   332 
   335 
   333     mAsyncReqId = setCurrentRequestAsync();
       
   334     
   336     if (!validData) {
   335     if (!validData) {
   337         // Failed to extract the data!
   336         // Failed to extract the data!
   338         NMLOG("NmSendServiceInterface::send(): Failed to process the given data!");
   337         NMLOG("NmSendServiceInterface::send(): Failed to process the given data!");
   339         launchEditor = false;
   338         cancelService();
   340     }
   339     }
   341     else if (count == 0) {
   340     else if (count == 0) {
   342         // No mailboxes.
   341         // No mailboxes.
   343         HbMessageBox note(hbTrId("txt_mail_dialog_no_mailboxes_defined"),
   342         if (mainWindow) {
       
   343             mainWindow->hide();
       
   344         }
       
   345 
       
   346         // Hide the app if it not embedded
       
   347         if (!XQServiceUtil::isEmbedded()) {
       
   348             XQServiceUtil::toBackground(true);
       
   349         }
       
   350 
       
   351         HbDeviceMessageBox note(hbTrId("txt_mail_dialog_no_mailboxes_defined"),
   344                           HbMessageBox::MessageTypeInformation);
   352                           HbMessageBox::MessageTypeInformation);
   345         note.setTimeout(HbMessageBox::NoTimeout);
   353         note.setTimeout(HbMessageBox::NoTimeout);
   346         note.exec();
   354         note.exec();
   347         launchEditor = false;
   355         if (mainWindow) {
   348     }
   356             mainWindow->show();
   349     else if (count == 1) {
   357         }
   350         // A single mailbox exists.
   358         cancelService();
   351         QModelIndex modelIndex = mailboxListModel.index(0, 0);
   359     }
   352         QVariant mailbox(mailboxListModel.data(modelIndex));
   360     else { // count > 0
   353         NmMailboxMetaData *mailboxMetaData = mailbox.value<NmMailboxMetaData*>();
   361         // Make sure the NMail application is in the foreground.
   354         mailboxId = mailboxMetaData->id();
   362         XQServiceUtil::toBackground(false);
   355     }
   363 
   356     else if (count > 1 && !selectMailbox(mailboxId)) {
   364     	mStartParam = new NmUiStartParam(
   357         // More than one mailboxes exist but the user cancelled the mailbox
   365         	NmUiViewMessageEditor,
   358         // selection dialog.
   366 	        0, // account id
   359         launchEditor = false;
   367 	        0, // folder id
   360     }
   368     	    0, // message id
   361 
   369         	dataHelper.mEditorStartMode, // editor start mode
   362     mAsyncReqId = setCurrentRequestAsync();
   370 	        dataHelper.mToAddresses, // address list
       
   371     	    dataHelper.mAttachmentList, // attachment list
       
   372         	true, // start as service
       
   373 	        dataHelper.mSubject, // message subject
       
   374 	        dataHelper.mCcAddresses, // list containing cc recipient addresses
       
   375     	    dataHelper.mBccAddresses // list containing bcc recipient addresses
       
   376 	    );
       
   377 
       
   378         if (count == 1) {
       
   379             // A single mailbox exists.
       
   380             QModelIndex modelIndex = mailboxListModel.index(0, 0);
       
   381             QVariant mailbox(mailboxListModel.data(modelIndex));
       
   382             NmMailboxMetaData *mailboxMetaData = mailbox.value<NmMailboxMetaData*>();
       
   383             mailboxId = mailboxMetaData->id();
       
   384         	launchEditorView(mailboxId);
       
   385         }
       
   386         else { // count > 1
       
   387             if (!mSelectionDialog) {
       
   388                 mSelectionDialog =
       
   389                     new NmMailboxSelectionDialog(mUiEngine.mailboxListModel());
       
   390             }
       
   391             connect(mSelectionDialog,SIGNAL(selectionDialogClosed(NmId&)),
       
   392                 this,SLOT(selectionDialogClosed(NmId&)));
       
   393             mSelectionDialog->open();
       
   394 
       
   395             // launch the editor when the dialog is closed
       
   396         }
       
   397     }
       
   398 }
       
   399 
       
   400 /*!
       
   401     Called when mailbox id is know and editor can be opened
       
   402     \param mailboxId mailbox using in editor
       
   403  */
       
   404 void NmSendServiceInterface::launchEditorView(NmId mailboxId) 
       
   405 {
       
   406     NMLOG(QString("NmSendServiceInterface::launchEditorView %1").arg(mailboxId.id()));
       
   407     // Make the previous view visible again.
       
   408     if (mCurrentView) {
       
   409         mCurrentView->show();
       
   410         mCurrentView = NULL;        
       
   411     }
   363     
   412     
   364     if (launchEditor) {
   413     if (mStartParam) {
   365         // Make the previous view visible again.
   414         mStartParam->setMailboxId(mailboxId);
   366         if (currentView) {
   415         mApplication->enterNmUiView(mStartParam);
   367             currentView->show();
   416         mStartParam = NULL; // ownership passed
   368         }
   417     }
   369 
   418     completeRequest(mAsyncReqId, 1);
   370         NmUiStartParam *startParam = new NmUiStartParam(
   419 }
   371             NmUiViewMessageEditor,
   420 
   372             mailboxId,
   421 void NmSendServiceInterface::cancelService() 
   373             0, // folder id
   422 {
   374             0, // message id
   423     NMLOG("NmSendServiceInterface::cancelService");
   375             dataHelper.mEditorStartMode, // editor start mode
   424     delete mStartParam;
   376             dataHelper.mToAddresses, // address list
   425     mStartParam = NULL;
   377             dataHelper.mAttachmentList, // attachment list
   426 
   378             true, // start as service
   427     // If the service was started as embedded, do not hide the app.
   379             dataHelper.mSubject, // message subject
   428     if (!XQServiceUtil::isEmbedded()) {
   380             dataHelper.mCcAddresses, // list containing cc recipient addresses
   429         XQServiceUtil::toBackground(true);
   381             dataHelper.mBccAddresses // list containing bcc recipient addresses
   430     }
   382         );
   431 
   383 
   432     completeRequest(mAsyncReqId, 0);
   384         mApplication->enterNmUiView(startParam);
   433 
   385         completeRequest(mAsyncReqId, 1);
   434     // If started as service, the application must be closed now.
   386     } 
   435     if (XQServiceUtil::isService()) {
       
   436         connect(this, SIGNAL(returnValueDelivered()),
       
   437             mApplication, SLOT(delayedExitApplication()));
       
   438     }
   387     else {
   439     else {
   388         // Delete the extracted data since the editor was not launched and thus
   440         // Make the previous view visible again
   389         // NmUiStartParam did not take the ownership of the data.
   441         if (mCurrentView) {
   390         dataHelper.deleteData();
   442             mCurrentView->show();
   391 
   443             mCurrentView = NULL;
   392         // If the service was started as embedded, do not hide the app.
   444         }
   393 		if (!XQServiceUtil::isEmbedded()) {
   445     }
   394 			XQServiceUtil::toBackground(true);
   446 }
   395 		}
   447 
   396 
       
   397         completeRequest(mAsyncReqId, 0);
       
   398 
       
   399         // If started as service, the application must be closed now.
       
   400         if (XQServiceUtil::isService()) {
       
   401             connect(this, SIGNAL(returnValueDelivered()),
       
   402                     mApplication, SLOT(delayedExitApplication()));
       
   403         }
       
   404         else {
       
   405             // Make the previous view visible again.
       
   406             if (currentView) {
       
   407                 currentView->show();
       
   408             }
       
   409         }
       
   410     }
       
   411 #endif /* NM_WINS_ENV */
   448 #endif /* NM_WINS_ENV */
   412 }
   449 
   413 
   450 
   414 
   451 
   415 // End of file.
   452 // End of file.