phoneapp/phoneuiqtviewadapter/src/phonenotecontroller.cpp
branchRCL_3
changeset 61 41a7f70b3818
equal deleted inserted replaced
58:40a3f856b14d 61:41a7f70b3818
       
     1 /*!
       
     2 * Copyright (c) 2009 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:  Handles phone notes.
       
    15 */
       
    16 
       
    17 #include "phonenotecontroller.h"
       
    18 #include "tphonecmdparamglobalnote.h"
       
    19 #include "tphonecmdparamquery.h"
       
    20 #include "phoneresourceadapter.h"
       
    21 #include "qtphonelog.h"
       
    22 #include <QSignalMapper>
       
    23 #include <QTimer>
       
    24 #include <hbdevicemessagebox.h>
       
    25 #include <hbdevicenotificationdialog.h>
       
    26 #include <hbdeviceprogressdialog.h>
       
    27 #include <hbmessagebox.h>
       
    28 #include <hbprogressdialog.h>
       
    29 #include <hbaction.h>
       
    30 #include <phoneappcommands.hrh>
       
    31 #include <hbstringutil.h>
       
    32 
       
    33 PhoneNoteController::PhoneNoteController(QObject *parent) : 
       
    34     QObject(parent), m_timer(0), m_progressDialog(0), m_dtmfNote(0), 
       
    35     m_queryNote(0), m_queryCanceledCommand(-1), m_timeoutCommand(-1)
       
    36 {
       
    37     PHONE_DEBUG("PhoneNoteController::PhoneNoteController");
       
    38     m_signalMapper = new QSignalMapper(this);
       
    39     connect(m_signalMapper, SIGNAL(mapped(int)), this, SIGNAL(command (int)));
       
    40 }
       
    41 
       
    42 PhoneNoteController::~PhoneNoteController()
       
    43 {
       
    44     PHONE_DEBUG("PhoneNoteController::~PhoneNoteController");
       
    45     if (m_timer) {
       
    46         delete m_timer;
       
    47     }
       
    48 }
       
    49 
       
    50 void PhoneNoteController::showGlobalNote(TPhoneCommandParam *commandParam)
       
    51 {
       
    52     PHONE_DEBUG("PhoneNoteController::showGlobalNote");
       
    53     Q_ASSERT (commandParam->ParamId () == TPhoneCommandParam::EPhoneParamIdGlobalNote);
       
    54 
       
    55     TPhoneCmdParamGlobalNote* globalNoteParam = 
       
    56         static_cast<TPhoneCmdParamGlobalNote*>( commandParam );
       
    57     
       
    58     if (globalNoteParam->NotificationDialog()) {
       
    59         showDeviceNotificationDialog(globalNoteParam);
       
    60     } else {
       
    61         showDeviceMessageBox(globalNoteParam);
       
    62     }
       
    63     
       
    64 }
       
    65 
       
    66 void PhoneNoteController::showNote(TPhoneCommandParam *commandParam)
       
    67 {
       
    68     PHONE_DEBUG("PhoneNoteController::showNote");
       
    69 
       
    70     TPhoneCmdParamNote* noteParam = static_cast<TPhoneCmdParamNote*>(
       
    71             commandParam );
       
    72     
       
    73     if ( noteParam->Type() == EPhoneNoteDtmfSending ) {
       
    74         showDtmfNote(noteParam);
       
    75     }
       
    76         
       
    77 }
       
    78 
       
    79 void PhoneNoteController::showQuery(TPhoneCommandParam *commandParam)
       
    80 {
       
    81     PHONE_DEBUG("PhoneNoteController::showQuery");
       
    82     TPhoneCmdParamQuery& params = *static_cast<TPhoneCmdParamQuery*>( commandParam );
       
    83 
       
    84     if ( EPhoneQueryDialog == params.QueryType() && 
       
    85          params.QueryPrompt().Length() ) {
       
    86         showDefaultQuery(&params);
       
    87        
       
    88     } else if ( EPhoneGlobalWaitNote == params.QueryType() ) {
       
    89         showGlobalWaitNote(&params);
       
    90         
       
    91     }
       
    92 }
       
    93 
       
    94 void PhoneNoteController::removeDtmfNote()
       
    95 {
       
    96     PHONE_DEBUG("PhoneNoteController::removeDtmfNote"); 
       
    97     if (m_dtmfNote) {
       
    98         m_dtmfNote->close();
       
    99     }
       
   100 }
       
   101 
       
   102 void PhoneNoteController::removeNote()
       
   103 {
       
   104     PHONE_DEBUG("PhoneNoteController::removeNote"); 
       
   105     removeDtmfNote();
       
   106 }
       
   107 
       
   108 void PhoneNoteController::removeQuery()
       
   109 {
       
   110     PHONE_DEBUG("PhoneNoteController::removeQuery"); 
       
   111     if (m_queryNote) {
       
   112         m_queryNote->close();
       
   113     }
       
   114 }
       
   115 
       
   116 void PhoneNoteController::removeGlobalWaitNote()
       
   117 {
       
   118     PHONE_DEBUG("PhoneNoteController::removeGlobalWaitNote"); 
       
   119     if (m_timer) {
       
   120         m_timeoutCommand = -1;
       
   121         m_timer->stop();
       
   122     }
       
   123     
       
   124     if (m_progressDialog) {
       
   125         m_queryCanceledCommand = -1;
       
   126         m_progressDialog->close();
       
   127     }
       
   128 }
       
   129 
       
   130 void PhoneNoteController::destroyDialog()
       
   131 {
       
   132     PHONE_DEBUG("PhoneNoteController::destroyDialog"); 
       
   133     HbDeviceMessageBox *messageBox = m_messageBoxList.takeFirst();
       
   134     messageBox->deleteLater();
       
   135     messageBox = 0;
       
   136     
       
   137     if ( 0 < m_messageBoxList.size() ) {
       
   138         PHONE_DEBUG("PhoneNoteController::show pending note");
       
   139         HbDeviceMessageBox *messageBoxTemp = m_messageBoxList[0];
       
   140         QObject::connect(messageBoxTemp, SIGNAL(aboutToClose()), 
       
   141                          this, SLOT(destroyDialog()));
       
   142         messageBoxTemp->show();
       
   143     }
       
   144 }
       
   145 
       
   146 void PhoneNoteController::destroyNotification()
       
   147 {
       
   148     PHONE_DEBUG("PhoneNoteController::destroyDialog"); 
       
   149     HbDeviceNotificationDialog *notification = m_notificationList.takeFirst();
       
   150     notification->deleteLater();
       
   151     notification = 0;
       
   152     
       
   153     if ( 0 < m_notificationList.size() ) {
       
   154         PHONE_DEBUG("PhoneNoteController::show pending note");
       
   155         HbDeviceNotificationDialog *notificationTemp = m_notificationList[0];
       
   156         QObject::connect(notificationTemp, SIGNAL(aboutToClose()), 
       
   157                          this, SLOT(destroyNotification()));
       
   158         notificationTemp->show();
       
   159     }
       
   160 }
       
   161 
       
   162 void PhoneNoteController::removeMappings()
       
   163 {
       
   164     foreach (HbAction *action, m_actions ) {
       
   165         m_signalMapper->removeMappings(action);
       
   166     }
       
   167     m_actions.clear();
       
   168     
       
   169     if (m_dtmfNote) {
       
   170         m_dtmfNote->deleteLater();
       
   171         m_dtmfNote = 0;
       
   172     }
       
   173     if (m_queryNote) {
       
   174         m_queryNote->deleteLater();
       
   175         m_queryNote = 0;
       
   176     }
       
   177     if (m_progressDialog) {
       
   178         m_progressDialog->deleteLater();
       
   179         m_progressDialog = 0;
       
   180     }
       
   181 }
       
   182 
       
   183 void PhoneNoteController::queryCancelled()
       
   184 {
       
   185     if (m_timer) {
       
   186         m_timer->stop();
       
   187     }
       
   188     
       
   189     if (m_queryCanceledCommand != -1) {
       
   190         emit command(m_queryCanceledCommand);
       
   191     }
       
   192     m_queryCanceledCommand = -1;
       
   193     m_timeoutCommand = -1;
       
   194 }
       
   195 
       
   196 void PhoneNoteController::queryTimeout()
       
   197 {
       
   198     int sendCommand = m_timeoutCommand;
       
   199     if (m_progressDialog) {
       
   200         m_queryCanceledCommand = -1;
       
   201         m_progressDialog->close();
       
   202     }
       
   203     if (sendCommand != -1) {        
       
   204         emit command(sendCommand);
       
   205     }
       
   206 }
       
   207 
       
   208 QString PhoneNoteController::globalNoteText(
       
   209         TPhoneCommandParam *commandParam)
       
   210 {
       
   211     TPhoneCmdParamGlobalNote* globalNoteParam = 
       
   212         static_cast<TPhoneCmdParamGlobalNote*>( commandParam );
       
   213     
       
   214     QString ret;    
       
   215     
       
   216     if ( globalNoteParam->TextResourceId() && 
       
   217          KErrNone != globalNoteParam->Text().Compare( KNullDesC() ) ) {
       
   218          // resource and text exists
       
   219          ret = PhoneResourceAdapter::Instance()->convertToStringWithParam(
       
   220                      globalNoteParam->TextResourceId(),
       
   221                      QString::fromUtf16(globalNoteParam->Text().Ptr(), 
       
   222                                      globalNoteParam->Text().Length()) );         
       
   223     } else if ( globalNoteParam->TextResourceId() ) {
       
   224         // resource exists
       
   225         QString causeCode;
       
   226         if (-1 != globalNoteParam->CauseCode()) {
       
   227             causeCode.setNum(globalNoteParam->CauseCode());
       
   228             causeCode = HbStringUtil::convertDigits(causeCode);
       
   229         }
       
   230         
       
   231         ret = PhoneResourceAdapter::Instance()->convertToString(
       
   232                     globalNoteParam->TextResourceId(), causeCode);
       
   233 
       
   234     } else if ( KErrNone != globalNoteParam->Text().Compare( KNullDesC() ) ) {
       
   235         // text exists
       
   236         ret = QString::fromUtf16(globalNoteParam->Text().Ptr(), 
       
   237                 globalNoteParam->Text().Length());
       
   238     }
       
   239     
       
   240     return ret;
       
   241 }
       
   242 
       
   243 void PhoneNoteController::showDtmfNote(TPhoneCmdParamNote* noteParam)
       
   244 {    
       
   245     if (m_dtmfNote) {
       
   246         m_dtmfNote->setText( QString::fromUtf16(noteParam->Text().Ptr(), 
       
   247                 noteParam->Text().Length()) );
       
   248         m_dtmfNote->update();
       
   249     } else {
       
   250         QList<HbAction*> hbactions = PhoneResourceAdapter::Instance()->
       
   251             convertToHbActions(noteParam->ResourceId());
       
   252 
       
   253         if (hbactions.count() > 0) {
       
   254             m_dtmfNote = new HbProgressDialog(HbProgressDialog::ProgressDialog);
       
   255             m_dtmfNote->setText( QString::fromUtf16(noteParam->Text().Ptr(), 
       
   256                     noteParam->Text().Length()) ); 
       
   257                   
       
   258             connect(hbactions.at(0), SIGNAL(triggered()), m_signalMapper, SLOT(map()));
       
   259 
       
   260             int count = m_dtmfNote->actions().count();
       
   261             for (int i=count;0<i;i--) {
       
   262                 QAction *action = m_dtmfNote->actions().at(i-1);
       
   263                 m_dtmfNote->removeAction(action);
       
   264                 //TODO
       
   265                 //delete action;
       
   266             }
       
   267             m_dtmfNote->addAction(hbactions.at(0));
       
   268             m_signalMapper->setMapping(hbactions.at(0), hbactions.at(0)->data().toInt());
       
   269             
       
   270             QObject::connect(m_dtmfNote, SIGNAL(aboutToClose()), 
       
   271                              this, SLOT(removeMappings())); 
       
   272             
       
   273             m_actions.append(hbactions.at(0));
       
   274       
       
   275             m_dtmfNote->show();
       
   276         }
       
   277     }
       
   278 }
       
   279 
       
   280 void PhoneNoteController::showDefaultQuery(TPhoneCmdParamQuery* params)
       
   281 {    
       
   282     if (!m_queryNote) {
       
   283         QList<HbAction*> hbactions = PhoneResourceAdapter::Instance()->
       
   284             convertToHbActions(params->QueryResourceId());
       
   285         
       
   286         if (hbactions.count() > 0) {
       
   287             m_queryNote = new HbMessageBox(HbMessageBox::MessageTypeQuestion);
       
   288             m_queryNote->setTimeout(HbPopup::NoTimeout);
       
   289             m_queryNote->setDismissPolicy(HbPopup::NoDismiss);
       
   290             m_queryNote->setText(QString::fromUtf16(params->QueryPrompt().Ptr(), 
       
   291                     params->QueryPrompt().Length()));
       
   292             
       
   293             QObject::connect(m_queryNote, SIGNAL(aboutToClose()), 
       
   294                              this, SLOT(removeMappings())); 
       
   295                             
       
   296             for (int i = 0; i < hbactions.count(); ++i) {
       
   297                 connect(hbactions.at(i), SIGNAL(triggered()), m_signalMapper, SLOT(map()));
       
   298                 m_signalMapper->setMapping(hbactions.at(i), hbactions.at(i)->data().toInt());
       
   299                 m_actions.append(hbactions.at(i));
       
   300             }
       
   301             
       
   302             int count = m_queryNote->actions().count();
       
   303             for (int i=count;0<i;i--) {
       
   304                 QAction *action = m_queryNote->actions().at(i-1);
       
   305                 m_queryNote->removeAction(action);
       
   306                 //TODO
       
   307                 //delete action;
       
   308             }
       
   309             
       
   310             for (int i=0;i<hbactions.count();i++) {
       
   311                 m_queryNote->addAction(hbactions.at(i));
       
   312             }
       
   313             
       
   314             m_queryNote->show();
       
   315         }
       
   316     }
       
   317 }
       
   318 
       
   319 void PhoneNoteController::showGlobalWaitNote(TPhoneCmdParamQuery* params)
       
   320 {    
       
   321     if (!m_progressDialog) {        
       
   322         m_queryCanceledCommand = params->CbaCommandMapping(EAknSoftkeyCancel);
       
   323 
       
   324         m_progressDialog = new HbDeviceProgressDialog(HbProgressDialog::WaitDialog);
       
   325         
       
   326         if (params->QueryPrompt().Length()) {
       
   327             m_progressDialog->setText(QString::fromUtf16(params->QueryPrompt().Ptr(), 
       
   328                     params->QueryPrompt().Length()));
       
   329         } else if (0 != params->DataText()) {
       
   330             m_progressDialog->setText(QString::fromUtf16(params->DataText()->Ptr(), 
       
   331                     params->DataText()->Length()));
       
   332         }
       
   333         
       
   334         QObject::connect(m_progressDialog, SIGNAL(aboutToClose()), 
       
   335                          this, SLOT(removeMappings())); 
       
   336         
       
   337         QObject::connect(m_progressDialog, SIGNAL(cancelled()), 
       
   338                          this, SLOT(queryCancelled())); 
       
   339         
       
   340         if (params->TimeOut() != 0) {
       
   341             if (!m_timer) {
       
   342                 m_timer = new QTimer();
       
   343                 m_timer->setSingleShot(true);
       
   344                 connect(m_timer, SIGNAL(timeout()), SLOT(queryTimeout()));
       
   345             }           
       
   346             
       
   347             m_timeoutCommand = -1;
       
   348             int customCommand;
       
   349             if (-1 != params->GetCustomCommandForTimeOut(customCommand)) {
       
   350                 m_timeoutCommand = customCommand;
       
   351             }
       
   352             
       
   353             m_timer->start(params->TimeOut());
       
   354         }
       
   355         
       
   356         m_progressDialog->show();
       
   357     }
       
   358 }
       
   359 
       
   360 void PhoneNoteController::showDeviceMessageBox(
       
   361         TPhoneCmdParamGlobalNote* params)
       
   362 {
       
   363     PHONE_DEBUG("PhoneNoteController::showDeviceMessageBox");
       
   364 
       
   365     HbMessageBox::MessageBoxType type;
       
   366         
       
   367     switch( params->Type() ) {
       
   368     case EPhoneMessageBoxInformation:
       
   369         type = HbMessageBox::MessageTypeInformation;
       
   370         break;
       
   371     case EPhoneMessageBoxWarning:
       
   372     default:
       
   373         type = HbMessageBox::MessageTypeWarning;
       
   374         break;
       
   375     }
       
   376     
       
   377     QString noteString = globalNoteText(params);
       
   378     
       
   379     if (false == noteString.isNull()) {
       
   380         bool showNote(true);
       
   381         for (int i = 0; i < m_messageBoxList.count(); ++i) {
       
   382             // Do not show same note/text several times, e.g when user hits
       
   383             // the end button several times we should show only one "not allowed"
       
   384             // note.
       
   385             if (noteString == m_messageBoxList.at(i)->text()) {
       
   386                 showNote = false;
       
   387                 break;
       
   388             }
       
   389         }
       
   390         
       
   391         if (showNote) {
       
   392             QScopedPointer<HbDeviceMessageBox> messageBox( 
       
   393                 new HbDeviceMessageBox(noteString, type));
       
   394             
       
   395             int timeout = params->Timeout();
       
   396             if (timeout <= 0) {
       
   397                 messageBox->setTimeout(HbDialog::StandardTimeout);
       
   398             } else {
       
   399                 messageBox->setTimeout(timeout);
       
   400             }
       
   401             
       
   402             HbDeviceMessageBox *messageBoxPtr = messageBox.data();
       
   403             m_messageBoxList.append(messageBoxPtr);
       
   404             messageBox.take();
       
   405             
       
   406             if (1 == m_messageBoxList.size()) {
       
   407                 QObject::connect(messageBoxPtr, SIGNAL(aboutToClose()), 
       
   408                                  this, SLOT(destroyDialog()));
       
   409                 messageBoxPtr->show();
       
   410             }
       
   411         }
       
   412     }    
       
   413 }
       
   414 
       
   415 void PhoneNoteController::showDeviceNotificationDialog(
       
   416         TPhoneCmdParamGlobalNote* params)
       
   417 {
       
   418     PHONE_DEBUG("PhoneNoteController::showDeviceNotificationDialog");
       
   419     
       
   420     QString noteString = globalNoteText(params);
       
   421     
       
   422     if (false == noteString.isNull()) {
       
   423         bool showNote(true);
       
   424         for (int i = 0; i < m_notificationList.count(); ++i) {
       
   425             // Do not show same note/text several times, e.g when user hits
       
   426             // the end button several times we should show only one "not allowed"
       
   427             // note.
       
   428             if (noteString == m_notificationList.at(i)->title()) {
       
   429                 showNote = false;
       
   430                 break;
       
   431             }
       
   432         }
       
   433         
       
   434         if (showNote) {
       
   435             QScopedPointer<HbDeviceNotificationDialog> notification( 
       
   436                 new HbDeviceNotificationDialog());
       
   437             
       
   438             notification->setTitle(noteString);
       
   439             
       
   440             int timeout = params->Timeout();
       
   441             if (timeout > 0) {
       
   442                 // If timeout not set we use default timeout. 
       
   443                 // Default value is HbPopup::StandardTimeout (3000 ms)
       
   444                 notification->setTimeout(timeout);
       
   445             }
       
   446             
       
   447             HbDeviceNotificationDialog *notificationPtr = notification.data();
       
   448             m_notificationList.append(notificationPtr);
       
   449             notification.take();
       
   450             
       
   451             if (1 == m_notificationList.size()) {
       
   452                 QObject::connect(notificationPtr, SIGNAL(aboutToClose()), 
       
   453                                  this, SLOT(destroyNotification()));
       
   454                 notificationPtr->show();
       
   455             }
       
   456         }
       
   457     }        
       
   458 }
       
   459 
       
   460