phonebookui/pbkcommonui/src/cnteditview_p.cpp
changeset 59 a642906a277a
parent 47 7cbcb2896f0e
child 65 ae724a111993
--- a/phonebookui/pbkcommonui/src/cnteditview_p.cpp	Tue Jul 06 14:05:47 2010 +0300
+++ b/phonebookui/pbkcommonui/src/cnteditview_p.cpp	Wed Aug 18 09:39:00 2010 +0300
@@ -21,7 +21,9 @@
 #include "cntdetailpopup.h"
 #include "cnteditviewheadingitem.h"
 #include "cntimagelabel.h"
+#include "cntsavemanager.h"
 #include "cntglobal.h"
+#include "cntdebug.h"
 
 #include <qtcontacts.h>
 #include <hbdocumentloader.h>
@@ -39,17 +41,22 @@
 #include <hbdevicenotificationdialog.h>
 #include <xqaiwrequest.h>
 #include <xqaiwdecl.h>
-#include <QDir>
+#include <cntservicescontact.h>
+
+#include <QFileInfo>
+#include <QApplication>
+#include <QTimer>
 
 const char *CNT_EDIT_XML = ":/xml/contacts_ev.docml";
 
 CntEditViewPrivate::CntEditViewPrivate() :
-mModel( NULL ),
-mImageLabel( NULL ),
-mThumbnailManager( NULL ),
-mContact( NULL ),
-mReq(0),
-mMenu(NULL)
+    mModel( NULL ),
+    mImageLabel( NULL ),
+    mThumbnailManager( NULL ),
+    mContact( NULL ),
+    mReq(NULL),
+    mMenu(NULL),
+    mSaveManager(NULL)
 {
     mDocument = new CntDocumentLoader;
     
@@ -93,19 +100,30 @@
     connect( mHeading, SIGNAL(iconClicked()), this, SLOT(openImageEditor()) );
     connect( mListView, SIGNAL(activated(const QModelIndex&)), this, SLOT(activated(const QModelIndex&)) );
     connect( mListView, SIGNAL(longPressed(HbAbstractViewItem*,const QPointF&)), this, SLOT(longPressed(HbAbstractViewItem*,const QPointF&)) );
+    
+    // closing the application from task swapper or end key will cause the contact to be saved
+    connect( qApp, SIGNAL(aboutToQuit()), this, SLOT(saveChanges()));
 }
 
 CntEditViewPrivate::~CntEditViewPrivate()
 {
     mView->deleteLater();
+    
     delete mDocument;
+    mDocument = NULL;
     delete mModel;
+    mModel = NULL;
     delete mContact;
+    mContact = NULL;
     if (mMenu) 
     {
         delete mMenu;
+        mMenu = NULL;
     }
     delete mThumbnailManager;
+    mThumbnailManager = NULL;
+    delete mSaveManager;
+    mSaveManager = NULL;
 }
 
 void CntEditViewPrivate::setOrientation(Qt::Orientation orientation)
@@ -124,6 +142,8 @@
 
 void CntEditViewPrivate::activate( CntAbstractViewManager* aMgr, const CntViewParameters aArgs )
 {
+    CNT_ENTRY
+    
     mMgr = aMgr;
     mArgs = aArgs;
     
@@ -150,7 +170,16 @@
     QContactLocalId localId = mContact->localId();
     QContactManager* cm = mMgr->contactManager(SYMBIAN_BACKEND);
     QContactLocalId selfContactId = cm->selfContactId();
-    mIsMyCard = ( localId == selfContactId && localId != 0 ) || myCard == "myCard";
+    mIsMyCard = ( localId == selfContactId && localId != 0 ) || !myCard.isEmpty();
+    
+    if (mIsMyCard)
+    {
+        mSaveManager = new CntSaveManager(CntSaveManager::EMyCard);
+    }
+    else
+    {
+        mSaveManager = new CntSaveManager();
+    }
     
     if ( mHeading )
         mHeading->setDetails( mContact, mIsMyCard );
@@ -195,6 +224,8 @@
             this, SLOT(thumbnailReady(QPixmap, void*, int, int)) );
 
     loadAvatar();
+    
+    CNT_EXIT
 }
 
 void CntEditViewPrivate::deactivate()
@@ -321,10 +352,12 @@
 {
     CntDetailPopup *popup = static_cast<CntDetailPopup*>(sender());
     
-    if (popup && aAction != popup->actions().first())
+    if (popup && aAction != popup->actions().first() && popup->selectedItems().count())
     {
-        int id = popup->selectedDetail();
-        
+        int row = popup->selectedItems().first().toInt();
+        QModelIndex index = popup->model()->index(row, 0);
+        int id = popup->model()->data(index, Qt::UserRole).toInt();
+
         if (id != noView )
         {
             mArgs.insert(EViewId, id );
@@ -423,31 +456,36 @@
     if ( mIsMyCard )
     {
         HbMessageBox::question(hbTrId("txt_phob_dialog_remove_all_personal_data_from_my_c"), this, 
-                SLOT(handleDeleteContact(HbAction*)), 
-                hbTrId("txt_common_button_yes"), 
-                hbTrId("txt_common_button_no"));
+                SLOT(handleDeleteContact(int)), 
+                HbMessageBox::Yes | HbMessageBox::No);
     }
     else
     {
         QContactManager* cm = mMgr->contactManager( SYMBIAN_BACKEND );
-        QString name = cm->synthesizedDisplayLabel( *mContact );
-        HbMessageBox::question(HbParameterLengthLimiter(hbTrId("txt_phob_info_delete_1")).arg(name), this, SLOT(handleDeleteContact(HbAction*)), 
-                hbTrId("txt_common_button_delete"), hbTrId("txt_common_button_cancel"));
+        QString name = cm->synthesizedContactDisplayLabel( *mContact );
+        if (name.isEmpty())
+        {
+            name = hbTrId("txt_phob_list_unnamed");
+        }
+        
+        HbMessageBox::question(HbParameterLengthLimiter(hbTrId("txt_phob_info_delete_1")).arg(name), this, SLOT(handleDeleteContact(int)), 
+                HbMessageBox::Delete | HbMessageBox::Cancel);
     }
 }
 
-void CntEditViewPrivate::handleDeleteContact(HbAction *action)
+void CntEditViewPrivate::handleDeleteContact(int action)
 {
     Q_Q(CntEditView);
-    
-    HbMessageBox *dlg = static_cast<HbMessageBox*>(sender());
-    if(dlg && action == dlg->actions().first())
+
+    if(action == HbMessageBox::Yes || action == HbMessageBox::Delete)
     {
         QContactManager* cm = mMgr->contactManager( SYMBIAN_BACKEND );
 
+        disconnect(cm, SIGNAL(contactsRemoved(const QList<QContactLocalId>&)),
+                this, SLOT(contactDeletedFromOtherSource(const QList<QContactLocalId>&)));
         emit q->contactRemoved(cm->removeContact( mContact->localId() ));
         
-        mMgr->back( mArgs );
+        mMgr->back( mArgs, true );
     }
 }
 
@@ -459,155 +497,79 @@
     
     // get a fresh one from backend.
     QContactManager* mgr = mMgr->contactManager(SYMBIAN_BACKEND);
-    mContact = new QContact(mgr->contact( mContact->localId() ));
+    setSelectedContact( mgr->contact( mContact->localId()) );
     
     QVariant var;
     var.setValue(*mContact);
     
     CntViewParameters viewParameters;
     viewParameters.insert(ESelectedContact, var);
-            
-    mMgr->back( viewParameters );
-}
+    
+    if ( mArgs.value( EExtraAction ).toString() == CNT_ROOT_ACTION )
+    {
+        mMgr->back( viewParameters, true );
+    }
+    else
+    {
+        mMgr->back( viewParameters );
+    }
+       
+}   
 
 void CntEditViewPrivate::saveChanges()
 {
     Q_Q(CntEditView);
-
-    QContactManager* mgr = mMgr->contactManager( SYMBIAN_BACKEND );
-    bool isSavedContact = mContact->localId() > 0;
     
-    // if the contact is really changed or a new one
-    if ( (*mContact) != mgr->contact(mContact->localId()) || !isSavedContact )
+    QString name = mMgr->contactManager(SYMBIAN_BACKEND)->synthesizedContactDisplayLabel(*mContact);
+    
+    if (name.isEmpty())
     {
-        int detailCount = mContact->details().count();
-         
-        setPreferredDetails( mContact );
-        
-        // If its a new contact
-        if ( !isSavedContact )
-        {
-            if ( detailCount > 2 )
-            {
-                bool success = mgr->saveContact( mContact );
-                if ( success && mIsMyCard )
-                {
-                    mgr->setSelfContactId( mContact->localId() );
-                }
-                
-                QString name = mgr->synthesizedDisplayLabel( *mContact );
-                if (name.isEmpty())
-                {
-                    name = hbTrId("txt_phob_dblist_unnamed");
-                }
-                
-                if ( success )
-                {
-                    emit q->contactUpdated(1);
-                    HbDeviceNotificationDialog::notification(QString(),HbParameterLengthLimiter(hbTrId("txt_phob_dpophead_contact_1_saved")).arg(name));
-                }
-                else
-                {
-                    emit q->contactUpdated(0);
-                    //TODO: localization is missing
-                    HbDeviceNotificationDialog::notification(QString(),qtTrId("SAVING FAILED!"));
-                }
-                
-                QVariant var;
-                var.setValue(*mContact);
-                mArgs.insert(ESelectedContact, var);
-                mArgs.insert(ESelectedAction, CNT_CREATE_ACTION);
-            }
-            else
-            {
-                // nothing happened to the contact. Flags should be used
-                emit q->contactUpdated(-2);
-            }
-        }
-        else
-        {
-            // contact details has been cleared out.
-            if ( detailCount <= 4 )
-            {
-                // get the contact from database, it should have the name still in it,
-                // and show the delete notification to user
-                QContact c = mgr->contact( mContact->localId() );
-                
-                bool success = mgr->removeContact( mContact->localId() );
-                emit q->contactRemoved(success);
-            }
-            else
-            {
-                bool success = mgr->saveContact(mContact);
-                
-                QString name = mgr->synthesizedDisplayLabel( *mContact );
-                if (name.isEmpty())
-                {
-                    name = hbTrId("txt_phob_dblist_unnamed");
-                }
-                
-                if ( success )
-                {   
-                    emit q->contactUpdated(1);
-                    HbDeviceNotificationDialog::notification(QString(),HbParameterLengthLimiter(hbTrId("txt_phob_dpophead_contacts_1_updated")).arg(name));
-                    mArgs.insert(ESelectedAction, CNT_EDIT_ACTION);           
-                }
-                else
-                {
-                    emit q->contactUpdated(0);
-                    //TODO: localization is missing
-                    HbDeviceNotificationDialog::notification(QString(),qtTrId("SAVING FAILED!"));
-                }
-                
-                QVariant var;
-                var.setValue(*mContact);
-                mArgs.insert(ESelectedContact, var);           
-            }
-        }
-    }
-    else
-    {
-        emit q->changesDiscarded();
+        name = hbTrId("txt_phob_list_unnamed");
     }
     
-    mMgr->back( mArgs );
-}
-
-void CntEditViewPrivate::setPreferredDetails( QContact* aContact )
-{
-    QList<QContactPhoneNumber> numberList( aContact->details<QContactPhoneNumber>() );
-    //set preferred number for call if there is only one phone number
-    if ( aContact->preferredDetail("call").isEmpty() && numberList.count() == 1 )
-    {
-        aContact->setPreferredDetail( "call", numberList.first() );
-    }
-    //set preferred number for message if there is only one mobile phone number
-    if ( aContact->preferredDetail("message").isEmpty() && numberList.count() >= 1 )
+    CntSaveManager::CntSaveResult result = mSaveManager->saveContact(mContact, mMgr->contactManager(SYMBIAN_BACKEND));
+    
+    QVariant var;
+    bool backToRoot(false);
+    
+    switch (result)
     {
-        int mobileNumbers = 0;
-        int mobileNumberIndex = -1;
-        for (int i = 0; i < numberList.count(); i++)
-        {
-            if (numberList.at(i).subTypes().count() && numberList.at(i).subTypes().first() == QContactPhoneNumber::SubTypeMobile)
-            {
-                mobileNumbers++;
-                mobileNumberIndex = i;
-            }      
-        }
-        if ( mobileNumbers == 1 )
-        {
-            aContact->setPreferredDetail( "message", numberList.at(mobileNumberIndex) );
-        }
+    case CntSaveManager::ESaved:
+        emit q->contactUpdated(KCntServicesReturnValueContactSaved);
+        HbDeviceNotificationDialog::notification(QString(),HbParameterLengthLimiter(hbTrId("txt_phob_dpophead_contact_1_saved")).arg(name));
+        var.setValue(*mContact);
+        mArgs.insert(ESelectedContact, var);
+        mArgs.insert(ESelectedAction, CNT_CREATE_ACTION);
+        break;
+    case CntSaveManager::EUpdated:
+        emit q->contactUpdated(KCntServicesReturnValueContactSaved);
+        HbDeviceNotificationDialog::notification(QString(),HbParameterLengthLimiter(hbTrId("txt_phob_dpophead_contacts_1_updated")).arg(name));
+        var.setValue(*mContact);
+        mArgs.insert(ESelectedContact, var);
+        mArgs.insert(ESelectedAction, CNT_EDIT_ACTION);
+        break;
+    case CntSaveManager::EFailed:
+        emit q->contactUpdated(KCntServicesReturnValueContactNotModified);
+        HbDeviceNotificationDialog::notification(QString(),hbTrId("SAVING FAILED!"));
+        break;
+    case CntSaveManager::EDeleted:
+        emit q->contactRemoved(true);
+        backToRoot = true;
+        break;
+    case CntSaveManager::ENothingDone:
+    default:
+        emit q->contactUpdated(KCntServicesReturnValueContactNotModified);
+        break;
     }
-    QList<QContactEmailAddress> emailList( aContact->details<QContactEmailAddress>() );
-    //set preferred number for email if there is only one email address
-    if ( aContact->preferredDetail("email").isEmpty() && emailList.count() == 1 )
+
+    if ( mArgs.value( EExtraAction ).toString() == CNT_ROOT_ACTION )
     {
-        aContact->setPreferredDetail( "email", emailList.first() );
-    }   
+        backToRoot = true;
+    }
+    
+    mMgr->back( mArgs, backToRoot );
 }
 
-
 void CntEditViewPrivate::openNameEditor()
 {
     QVariant var;
@@ -670,7 +632,7 @@
         mHeading->setIcon(icon);
         
         mImageLabel->clear();
-        mImageLabel->setIcon(icon);
+        mImageLabel->setIcon(pixmap);
     }
 }
 
@@ -744,14 +706,15 @@
 void CntEditViewPrivate::addDetail( CntEditViewItem* aDetail )
 {
     QVariant id = aDetail->data( ERoleEditorViewId );
-    CntViewParameters viewParameters;
-    viewParameters.insert(EViewId, id.toInt());
+    
+    mArgs.insert(EViewId, id.toInt());
+    mArgs.insert(ESelectedAction, CNT_ADD_ACTION );
     QVariant var;
     var.setValue(*mContact);
-    viewParameters.insert(ESelectedContact, var);
-    viewParameters.insert(ESelectedAction, CNT_ADD_ACTION );
-                                            
-    mMgr->changeView( viewParameters );
+    
+    mArgs.insert(ESelectedContact, var);
+                                        
+    mMgr->changeView( mArgs );
 }
 
 void CntEditViewPrivate::editDetail( CntEditViewItem* aDetail )
@@ -783,19 +746,37 @@
         mContact = NULL;
     }
     mContact = new QContact( aContact );
-    /*
-    mListView->setModel( NULL );
     
-    if ( mModel )
+    QContactManager* cm = mMgr->contactManager( SYMBIAN_BACKEND );
+    connect(cm, SIGNAL(contactsRemoved(const QList<QContactLocalId>&)), 
+        this, SLOT(contactDeletedFromOtherSource(const QList<QContactLocalId>&)), Qt::UniqueConnection);
+}
+
+void CntEditViewPrivate::contactDeletedFromOtherSource(const QList<QContactLocalId>& contactIds)
+{
+    CNT_ENTRY
+    
+    if ( contactIds.contains(mContact->localId()) )
     {
-        delete mModel;
-        mModel = NULL;
+        // Do not switch to the previous view immediately. List views are
+        // not updated properly if this is not done in the event loop
+        QTimer::singleShot(0, this, SLOT(showRootView()));
     }
-    mModel = new CntEditViewListModel( mContact );
     
-    if ( mListView )
-        mListView->setModel( mModel );
-        */
+    CNT_EXIT
 }
+
+void CntEditViewPrivate::showRootView()
+{
+    CNT_ENTRY
+    
+    Q_Q(CntEditView);
+    
+    emit q->contactRemoved(true);
+    mMgr->back( mArgs, true );
+    
+    CNT_EXIT
+}
+
 // End of File