phonebookui/pbkcommonui/src/cnteditviewlistmodel.cpp
changeset 46 efe85016a067
parent 40 b46a585f6909
child 47 7cbcb2896f0e
equal deleted inserted replaced
40:b46a585f6909 46:efe85016a067
    58     refresh();
    58     refresh();
    59 }
    59 }
    60 
    60 
    61 CntEditViewListModel::~CntEditViewListModel()
    61 CntEditViewListModel::~CntEditViewListModel()
    62 {
    62 {
       
    63     // Delete the separator item separately if it hasn't been added to the list
       
    64     if (mSeparator && mItemList.indexOf(mSeparator) == -1)
       
    65     {
       
    66         delete mSeparator;
       
    67         mSeparator = NULL;
       
    68     }
       
    69     
    63     qDeleteAll( mItemList );
    70     qDeleteAll( mItemList );
    64     
    71     
    65     delete mBuilder;
    72     delete mBuilder;
    66     delete mManager;
    73     delete mManager;
    67 }
    74 }
   104     int index = mItemList.indexOf( aItem );
   111     int index = mItemList.indexOf( aItem );
   105     if ( index >= 0 )
   112     if ( index >= 0 )
   106     {
   113     {
   107         beginRemoveRows( aIndex.parent(), index, index );
   114         beginRemoveRows( aIndex.parent(), index, index );
   108         // remove item from item list
   115         // remove item from item list
   109         int count = mItemList.count();
       
   110         CntEditViewItem* item = mItemList.takeAt( index );
   116         CntEditViewItem* item = mItemList.takeAt( index );
   111         count = mItemList.count();
       
   112         
   117         
   113         // get detailed information
   118         // get detailed information
   114         QContactDetail detail = item->data(ERoleContactDetail).value<QContactDetail>();
   119         QContactDetail detail = item->data(ERoleContactDetail).value<QContactDetail>();
   115         QStringList fields = item->data(ERoleContactDetailFields).toStringList();
   120         QStringList fields = item->data(ERoleContactDetailFields).toStringList();
   116         
   121         
   119         
   124         
   120         // Update lookup table. Note, in case of QContactAddress,
   125         // Update lookup table. Note, in case of QContactAddress,
   121         // we can't remove address template, so the mapping for address always points to address detail
   126         // we can't remove address template, so the mapping for address always points to address detail
   122         KLookupKey lookupKey = mLookupMap.value( detail.definitionName() );
   127         KLookupKey lookupKey = mLookupMap.value( detail.definitionName() );
   123         removeItem( lookupKey );
   128         removeItem( lookupKey );
       
   129         
       
   130         delete item;
       
   131         
   124         endRemoveRows();
   132         endRemoveRows();
   125         
   133         
   126         // Remove separator item if needed
   134         // Remove separator item if needed
   127         if (mItemList.last()->data( Hb::ItemTypeRole ) == QVariant(Hb::SeparatorItem))
   135         if (mItemList.last()->data( Hb::ItemTypeRole ) == QVariant(Hb::SeparatorItem))
   128         {
   136         {
   129             int separatorIndex = mItemList.indexOf( mSeparator );
   137             int separatorIndex = mItemList.indexOf( mSeparator );
   130             beginRemoveRows( aIndex.parent(), separatorIndex, separatorIndex );
   138             beginRemoveRows( aIndex.parent(), separatorIndex, separatorIndex );
   131             mItemList.removeAt( mItemList.indexOf(mSeparator) );
   139             mItemList.removeAt( separatorIndex );
   132             removeItem( ESeparator  );
   140             removeItem( ESeparator  );
       
   141             
       
   142             delete mSeparator;
       
   143             mSeparator = NULL;
       
   144             
   133             endRemoveRows();
   145             endRemoveRows();
   134         }
   146         }
   135         
       
   136         
   147         
   137         // Check if the removed item is -1 in lookuptable and if it needs a template
   148         // Check if the removed item is -1 in lookuptable and if it needs a template
   138         int lookupValue = mLookupTable.value( lookupKey );
   149         int lookupValue = mLookupTable.value( lookupKey );
   139         if ( lookupValue == -1 )
   150         if ( lookupValue == -1 )
   140         {
   151         {
   141             beginResetModel();
       
   142             
       
   143             if ( detail.definitionName() == QContactPhoneNumber::DefinitionName )
   152             if ( detail.definitionName() == QContactPhoneNumber::DefinitionName )
       
   153             {
       
   154                 beginInsertRows(aIndex.parent(), index, index);
   144                 insertItem( EPhonenumber, mBuilder->phoneNumberItems(*mContact) );
   155                 insertItem( EPhonenumber, mBuilder->phoneNumberItems(*mContact) );
   145             
   156                 endInsertRows();
       
   157             }
   146             else if ( detail.definitionName() == QContactEmailAddress::DefinitionName )
   158             else if ( detail.definitionName() == QContactEmailAddress::DefinitionName )
       
   159             {
       
   160                 beginInsertRows(aIndex.parent(), index, index);
   147                 insertItem( EEmailAddress, mBuilder->emailAddressItems(*mContact) );
   161                 insertItem( EEmailAddress, mBuilder->emailAddressItems(*mContact) );
   148             
   162                 endInsertRows();
       
   163             }
   149             else if ( detail.definitionName() == QContactAddress::DefinitionName )
   164             else if ( detail.definitionName() == QContactAddress::DefinitionName )
       
   165             {
       
   166                 // special case: unlike the others, address template isn't in the same index as the last deleted detail
       
   167                 int emailIndex = mLookupTable.value( EEmailAddress );
       
   168                 beginInsertRows(aIndex.parent(), emailIndex + 1, emailIndex + 1);
   150                 insertItem( EAddressTemplate, mBuilder->addressItems(*mContact) );
   169                 insertItem( EAddressTemplate, mBuilder->addressItems(*mContact) );
   151             
   170                 endInsertRows();
       
   171             }
   152             else if ( detail.definitionName() == QContactUrl::DefinitionName )
   172             else if ( detail.definitionName() == QContactUrl::DefinitionName )
       
   173             {
       
   174                 beginInsertRows(aIndex.parent(), index, index);
   153                 insertItem( EUrl, mBuilder->urlItems(*mContact) );
   175                 insertItem( EUrl, mBuilder->urlItems(*mContact) );
   154             
   176                 endInsertRows();
   155             endResetModel();
   177             }
   156         }
   178         }
   157         delete item;
       
   158     }
   179     }
   159 }
   180 }
   160 
   181 
   161 void CntEditViewListModel::refreshExtensionItems()
   182 void CntEditViewListModel::refreshExtensionItems()
   162 {
   183 {
   290     endResetModel();
   311     endResetModel();
   291 }
   312 }
   292 
   313 
   293 bool CntEditViewListModel::isEmptyItem( CntEditViewItem* aItem )
   314 bool CntEditViewListModel::isEmptyItem( CntEditViewItem* aItem )
   294 {
   315 {
   295     QContactDetail d = aItem->data( ERoleContactDetail ).value<QContactDetail>();
   316     if ( aItem )
   296     QStringList fields = aItem->data( ERoleContactDetailFields ).toStringList();
   317     {
   297     
   318         QContactDetail d = aItem->data( ERoleContactDetail ).value<QContactDetail>();
   298     foreach ( QString field, fields )
   319         QStringList fields = aItem->data( ERoleContactDetailFields ).toStringList();
   299     {
   320         
   300         if ( !d.value(field).isEmpty() )
   321         foreach ( QString field, fields )
   301         {
   322         {
   302             return false;
   323             if ( !d.value(field).isEmpty() )
       
   324             {
       
   325                 return false;
       
   326             }
   303         }
   327         }
   304     }
   328     }
   305     return true;
   329     return true;
   306 }
   330 }
   307 
   331