phonebookui/pbkcommonui/src/cntnoteeditormodel.cpp
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Wed, 23 Jun 2010 18:02:44 +0300
changeset 46 efe85016a067
parent 40 b46a585f6909
permissions -rw-r--r--
Revision: 201023 Kit: 2010125

/*
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:
*
*/
#include "cntnoteeditormodel.h"
#include "cntdetailmodelitem.h"
#include <qcontactnote.h>

CntNoteEditorModel::CntNoteEditorModel( QContact* aContact ) :
CntDetailEditorModel( aContact )
    {
    HbDataFormModelItem* root = invisibleRootItem();
    foreach ( QContactNote note, mContact->details<QContactNote>() )
        {
        CntDetailModelItem* item = new CntDetailModelItem(note);
        appendDataFormItem( item, root );
        }
    }

CntNoteEditorModel::~CntNoteEditorModel()
    {
    }

void CntNoteEditorModel::insertDetailField()
{
    QContactNote emptyNote;
    mNoteList.append( emptyNote );
    appendDataFormItem( new CntDetailModelItem(emptyNote), invisibleRootItem() );
}

void CntNoteEditorModel::saveContactDetails()
{
    HbDataFormModelItem* root = invisibleRootItem();
      
    int count( root->childCount() );
    for ( int i(0); i < count; i++ ) {
        CntDetailModelItem* detail = static_cast<CntDetailModelItem*>( root->childAt(i) );
        QContactNote note = detail->detail();
        
        if ( !mNoteList.contains(note) )
        {
            mContact->saveDetail( &note );
        }
        
        if ( note.note().isEmpty() )
        {
            mContact->removeDetail( &note );
        }
    }
}

QContactDetail CntNoteEditorModel::detail() const
{
    QListIterator<QContactNote> noteList(mContact->details<QContactNote>());
    noteList.toBack(); // go through backwards, so the newest item will be returned
    while ( noteList.hasPrevious() )
    {
        QContactNote note = noteList.previous();
        if ( !note.value(QContactNote::FieldNote).isEmpty() )
        {
            return note;
        }
    }
    return QContactNote(); // return empty address if none found
}
// End of File