phonebookui/pbkcommonui/src/cntdetaileditor.cpp
changeset 59 a642906a277a
parent 47 7cbcb2896f0e
child 65 ae724a111993
equal deleted inserted replaced
47:7cbcb2896f0e 59:a642906a277a
    15 *
    15 *
    16 */
    16 */
    17 #include "cntdetaileditor.h"
    17 #include "cntdetaileditor.h"
    18 #include "cnteditorfactory.h"
    18 #include "cnteditorfactory.h"
    19 #include "cntgroupeditormodel.h"
    19 #include "cntgroupeditormodel.h"
       
    20 #include "cntsavemanager.h"
    20 #include "cntglobal.h"
    21 #include "cntglobal.h"
       
    22 
    21 #include <cntviewparams.h>
    23 #include <cntviewparams.h>
    22 #include <hbmenu.h>
    24 #include <hbmenu.h>
    23 #include <hbaction.h>
    25 #include <hbaction.h>
    24 #include <hbmainwindow.h>
    26 #include <hbmainwindow.h>
    25 #include <hbview.h>
    27 #include <hbview.h>
    27 #include <hbaction.h>
    29 #include <hbaction.h>
    28 #include <hblineedit.h>
    30 #include <hblineedit.h>
    29 #include <hbinputeditorinterface.h>
    31 #include <hbinputeditorinterface.h>
    30 #include <hbinputstandardfilters.h>
    32 #include <hbinputstandardfilters.h>
    31 #include <cntdebug.h>
    33 #include <cntdebug.h>
       
    34 #include <hbscrollarea.h>
       
    35 #include <hbscrollbar.h>
       
    36 #include <hbdevicenotificationdialog.h>
       
    37 #include <hbparameterlengthlimiter.h>
       
    38 
       
    39 #include <QApplication>
       
    40 #include <QTimer>
    32 
    41 
    33 const char *CNT_DETAILEDITOR_XML = ":/xml/contacts_detail_editor.docml";
    42 const char *CNT_DETAILEDITOR_XML = ":/xml/contacts_detail_editor.docml";
    34 
    43 
    35 CntDetailEditor::CntDetailEditor( int aId ) :
    44 CntDetailEditor::CntDetailEditor( int aId ) :
    36     QObject(),
    45     QObject(),
    39     mId(aId),
    48     mId(aId),
    40     mView(NULL),
    49     mView(NULL),
    41     mLoader(NULL),   
    50     mLoader(NULL),   
    42     mViewManager(NULL),
    51     mViewManager(NULL),
    43     mEditorFactory(NULL),
    52     mEditorFactory(NULL),
    44     mCancel(NULL)
    53     mCancel(NULL),
       
    54     mSaveManager(NULL)
    45 {
    55 {
    46     bool ok;
    56     bool ok;
    47     document()->load(CNT_DETAILEDITOR_XML, &ok);
    57     document()->load(CNT_DETAILEDITOR_XML, &ok);
    48     if (!ok) {
    58     if (!ok) {
    49         qFatal("Unable to read %S", CNT_DETAILEDITOR_XML);
    59         qFatal("Unable to read %S", CNT_DETAILEDITOR_XML);
    55  
    65  
    56     mEditorFactory = new CntEditorFactory();
    66     mEditorFactory = new CntEditorFactory();
    57     
    67     
    58     mSoftkey = new HbAction(Hb::BackNaviAction, mView);
    68     mSoftkey = new HbAction(Hb::BackNaviAction, mView);
    59     connect( mSoftkey, SIGNAL(triggered()), this, SLOT(saveChanges()) );
    69     connect( mSoftkey, SIGNAL(triggered()), this, SLOT(saveChanges()) );
       
    70     
       
    71     // closing the application from task swapper or end key will cause the contact to be saved
       
    72     connect( qApp, SIGNAL(aboutToQuit()), this, SLOT(saveContact()));
    60 }
    73 }
    61 
    74 
    62 CntDetailEditor::~CntDetailEditor()
    75 CntDetailEditor::~CntDetailEditor()
    63 {
    76 {
    64     mView->deleteLater();
    77     mView->deleteLater();
       
    78     
    65     delete mDataForm;
    79     delete mDataForm;
       
    80     mDataForm = NULL;
    66     delete mDataFormModel;
    81     delete mDataFormModel;
       
    82     mDataFormModel = NULL;
    67     delete mHeader;
    83     delete mHeader;
       
    84     mHeader = NULL;
    68     delete mLoader;
    85     delete mLoader;
       
    86     mLoader = NULL;
    69     delete mEditorFactory;
    87     delete mEditorFactory;
       
    88     mEditorFactory = NULL;
       
    89     delete mSaveManager;
       
    90     mSaveManager = NULL;
    70 }
    91 }
    71 
    92 
    72 void CntDetailEditor::setViewId( int aId )
    93 void CntDetailEditor::setViewId( int aId )
    73 {
    94 {
    74     mId = aId;
    95     mId = aId;
   104     else
   125     else
   105     {
   126     {
   106         selectedContact = aArgs.value(ESelectedContact).value<QContact>();
   127         selectedContact = aArgs.value(ESelectedContact).value<QContact>();
   107         connect( mDataForm, SIGNAL(itemShown(const QModelIndex&)), this, SLOT(handleItemShown(const QModelIndex&)) );
   128         connect( mDataForm, SIGNAL(itemShown(const QModelIndex&)), this, SLOT(handleItemShown(const QModelIndex&)) );
   108     }
   129     }
       
   130     
       
   131     QContactManager* cm = mViewManager->contactManager(SYMBIAN_BACKEND);
       
   132     connect(cm, SIGNAL(contactsRemoved(const QList<QContactLocalId>&)), 
       
   133         this, SLOT(contactDeletedFromOtherSource(const QList<QContactLocalId>&)));
       
   134     
   109     mEditorFactory->setupEditorView(*this, selectedContact);
   135     mEditorFactory->setupEditorView(*this, selectedContact);
       
   136     
       
   137     QString myCard = mArgs.value( EMyCard ).toString();
       
   138     QContactLocalId localId = selectedContact.localId();
       
   139     QContactLocalId selfContactId = mViewManager->contactManager(SYMBIAN_BACKEND)->selfContactId();
       
   140     bool isMyCard = ( localId == selfContactId && localId != 0 ) || !myCard.isEmpty();
       
   141     
       
   142     if (isMyCard)
       
   143     {
       
   144         mSaveManager = new CntSaveManager(CntSaveManager::EMyCard);
       
   145     }
       
   146     else if ( mId == groupEditorView )
       
   147     {
       
   148         mSaveManager = new CntSaveManager(CntSaveManager::EGroup);
       
   149     }
       
   150     else
       
   151     {
       
   152         mSaveManager = new CntSaveManager();
       
   153     }
   110     
   154     
   111     mDataForm->setItemRecycling(true);
   155     mDataForm->setItemRecycling(true);
   112 
   156 
   113     // add new field if required
   157     // add new field if required
   114     if ( aArgs.value(ESelectedAction).toString() == CNT_ADD_ACTION )
   158     if ( aArgs.value(ESelectedAction).toString() == CNT_ADD_ACTION )
   115     {
   159     {
   116         mDataFormModel->insertDetailField();
   160         mDataFormModel->insertDetailField();
   117     }
   161     }
       
   162     mDataForm->setVerticalScrollBarPolicy(HbScrollArea::ScrollBarAsNeeded); 
       
   163     mDataForm->setScrollingStyle(HbScrollArea::PanWithFollowOn);
       
   164     mDataForm->verticalScrollBar()->setInteractive(true);
   118 }
   165 }
   119 
   166 
   120 void CntDetailEditor::deactivate()
   167 void CntDetailEditor::deactivate()
   121 {
   168 {
   122     if( mId == groupEditorView) {
   169     if( mId == groupEditorView) {
   123         QContactManager* mgr = mViewManager->contactManager(SYMBIAN_BACKEND);
   170         QContactManager* mgr = mViewManager->contactManager(SYMBIAN_BACKEND);
   124         mgr->saveContact( mDataFormModel->contact() );
   171         mgr->saveContact( mDataFormModel->contact() );
   125     }
   172     }
       
   173     
       
   174     QContactManager *cm = mViewManager->contactManager(SYMBIAN_BACKEND);
       
   175     disconnect(cm, SIGNAL(contactsRemoved(const QList<QContactLocalId>&)),
       
   176             this, SLOT(contactDeletedFromOtherSource(const QList<QContactLocalId>&)));
   126 }
   177 }
   127     
   178     
   128 bool CntDetailEditor::isDefault() const
   179 bool CntDetailEditor::isDefault() const
   129 {
   180 {
   130     return false;
   181     return false;
   149     mDataFormModel->insertDetailField();
   200     mDataFormModel->insertDetailField();
   150 }
   201 }
   151 
   202 
   152 void CntDetailEditor::handleItemShown(const QModelIndex& aIndex )
   203 void CntDetailEditor::handleItemShown(const QModelIndex& aIndex )
   153 {
   204 {
   154     if ( mId == groupEditorView )
   205     HbDataFormModelItem* modelItem = mDataFormModel->itemFromIndex( aIndex );
   155     {
   206     
   156         CntGroupEditorModel* groupModel = static_cast<CntGroupEditorModel*>( mDataFormModel );    
   207     if ( modelItem->type() == HbDataFormModelItem::TextItem ) 
   157         if ( groupModel->isConferenceNumber(aIndex) )
       
   158         {
       
   159             HbDataFormViewItem* viewItem = static_cast<HbDataFormViewItem*>(mDataForm->itemByIndex( aIndex ));
       
   160             HbLineEdit* edit = static_cast<HbLineEdit*>( viewItem->dataItemContentWidget() );
       
   161             edit->setInputMethodHints( Qt::ImhDialableCharactersOnly );
       
   162         }
       
   163     }
       
   164     else
       
   165     {
   208     {
   166         HbDataFormViewItem* viewItem = static_cast<HbDataFormViewItem*>(mDataForm->itemByIndex( aIndex ));
   209         HbDataFormViewItem* viewItem = static_cast<HbDataFormViewItem*>(mDataForm->itemByIndex( aIndex ));
   167         HbLineEdit* edit = static_cast<HbLineEdit*>( viewItem->dataItemContentWidget() );
   210         HbLineEdit* edit = static_cast<HbLineEdit*>( viewItem->dataItemContentWidget() );
   168         edit->setInputMethodHints( Qt::ImhNoPredictiveText );
   211         QString objName;
   169         
   212         
   170         HbDataFormModelItem* modelItem = mDataFormModel->itemFromIndex( aIndex );
   213         if ( mId == groupEditorView )
   171         if (modelItem->contentWidgetData( "preferDigits" ).toBool())
       
   172         {
   214         {
   173             edit->setInputMethodHints( Qt::ImhPreferNumbers );
   215             CntGroupEditorModel* groupModel = static_cast<CntGroupEditorModel*>( mDataFormModel );    
       
   216             if ( groupModel->isConferenceNumber(aIndex) )
       
   217             {
       
   218                 edit->setInputMethodHints( Qt::ImhDialableCharactersOnly );
       
   219                 objName = "Conference number line edit %1";
       
   220             }
       
   221             else
       
   222             {
       
   223                 edit->setInputMethodHints( Qt::ImhNoPredictiveText );
       
   224                 objName = "Group name line edit %1";
       
   225             }
   174         }
   226         }
       
   227         else
       
   228         {
       
   229             edit->setInputMethodHints( Qt::ImhNoPredictiveText );
       
   230             
       
   231             if (modelItem->contentWidgetData( "preferDigits" ).toBool())
       
   232             {
       
   233                 edit->setInputMethodHints( Qt::ImhPreferNumbers );
       
   234             }
       
   235             
       
   236             objName = mDataFormModel->detail().definitionName() + " line edit %1";
       
   237         }
       
   238         
       
   239         // Naming UI components for automation testability
       
   240         edit->setObjectName(objName.arg(aIndex.row()));
   175     }
   241     }
   176 }
   242 }
   177 
   243 
   178 void CntDetailEditor::discardChanges()
   244 void CntDetailEditor::discardChanges()
   179 {
   245 {
   182     var.setValue(selected);
   248     var.setValue(selected);
   183     mArgs.insert(ESelectedContact, var);
   249     mArgs.insert(ESelectedContact, var);
   184     mViewManager->back( mArgs );
   250     mViewManager->back( mArgs );
   185 }
   251 }
   186 
   252 
       
   253 /*!
       
   254     Saves all changes made to details (type of details depends on
       
   255     in which editor we are) of a contact. Doesn't save the contact.
       
   256 */
   187 void CntDetailEditor::saveChanges()
   257 void CntDetailEditor::saveChanges()
   188 {
   258 {
   189     mDataFormModel->saveContactDetails();
   259     mDataFormModel->saveContactDetails();
   190     
   260     
   191     QContact selected( *mDataFormModel->contact() );
   261     QContact selected( *mDataFormModel->contact() );
   206     }
   276     }
   207     
   277     
   208     mViewManager->back( mArgs );
   278     mViewManager->back( mArgs );
   209 }
   279 }
   210 
   280 
       
   281 /*!
       
   282     First saves all changes made to details (type of details depends on
       
   283     in which editor we are) of a contact and then saves the contact. This
       
   284     is only called when phonebook is being closed either from task swapper
       
   285     or by end key.
       
   286 */
       
   287 void CntDetailEditor::saveContact()
       
   288 {
       
   289     mDataFormModel->saveContactDetails();
       
   290     
       
   291     QString name = mViewManager->contactManager(SYMBIAN_BACKEND)->synthesizedContactDisplayLabel(*mDataFormModel->contact());
       
   292     
       
   293     if (name.isEmpty())
       
   294     {
       
   295         name = hbTrId("txt_phob_list_unnamed");
       
   296     }
       
   297     
       
   298     CntSaveManager::CntSaveResult result = mSaveManager->saveContact(mDataFormModel->contact(), mViewManager->contactManager(SYMBIAN_BACKEND));
       
   299     
       
   300     if (mId != groupEditorView)
       
   301     {
       
   302         switch (result)
       
   303         {
       
   304         case CntSaveManager::ESaved:
       
   305             HbDeviceNotificationDialog::notification(QString(),HbParameterLengthLimiter(hbTrId("txt_phob_dpophead_contact_1_saved")).arg(name));
       
   306             break;
       
   307         case CntSaveManager::EUpdated:
       
   308             HbDeviceNotificationDialog::notification(QString(),HbParameterLengthLimiter(hbTrId("txt_phob_dpophead_contacts_1_updated")).arg(name));
       
   309             break;
       
   310         case CntSaveManager::EFailed:
       
   311             HbDeviceNotificationDialog::notification(QString(),hbTrId("SAVING FAILED!"));
       
   312             break;
       
   313         case CntSaveManager::EDeleted:
       
   314         case CntSaveManager::ENothingDone:
       
   315         default:
       
   316             break;
       
   317         }
       
   318     }
       
   319 }
       
   320 
   211 void CntDetailEditor::setHeader(QString aHeader)
   321 void CntDetailEditor::setHeader(QString aHeader)
   212 {
   322 {
   213     if (!mHeader) {
   323     if (!mHeader) {
   214         mHeader = static_cast<HbGroupBox*> (document()->findWidget(QString("headerBox")));
   324         mHeader = static_cast<HbGroupBox*> (document()->findWidget(QString("headerBox")));
   215     }
   325     }
   234 int CntDetailEditor::viewId() const
   344 int CntDetailEditor::viewId() const
   235 {
   345 {
   236     return mId;
   346     return mId;
   237 }
   347 }
   238 
   348 
       
   349 /*!
       
   350 Go back to the root view
       
   351 */
       
   352 void CntDetailEditor::showRootView()
       
   353 {   
       
   354     mViewManager->back( mArgs, true );
       
   355 }
       
   356 
       
   357 
       
   358 void CntDetailEditor::contactDeletedFromOtherSource(const QList<QContactLocalId>& contactIds)
       
   359 {
       
   360     CNT_ENTRY
       
   361     
       
   362     QContact normalContact = mArgs.value(ESelectedContact).value<QContact>();
       
   363     QContact groupContact = mArgs.value(ESelectedGroupContact).value<QContact>();
       
   364     if ( contactIds.contains(normalContact.localId()) || contactIds.contains(groupContact.localId()) )
       
   365     {
       
   366         // Do not switch to the previous view immediately. List views are
       
   367         // not updated properly if this is not done in the event loop
       
   368         QTimer::singleShot(0, this, SLOT(showRootView()));
       
   369     }
       
   370     CNT_EXIT
       
   371 }
       
   372 
   239 // End of File
   373 // End of File