fep/aknfep/UiPlugins/AknFepUiInterface/AvkonImpl/src/AknFepPredOwnWordsModel.cpp
changeset 40 2cb9bae34d17
parent 31 f1bdd6b078d1
child 49 37f5d84451bd
equal deleted inserted replaced
31:f1bdd6b078d1 40:2cb9bae34d17
     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:          
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 
       
    26 
       
    27 #include "AknFepPredOwnWordsModel.h"
       
    28 
       
    29 
       
    30 // ============================ MEMBER FUNCTIONS ===============================
       
    31 
       
    32 // -----------------------------------------------------------------------------
       
    33 // CAknFepPredOwnWordsModel::NewL()
       
    34 // Two-phased constructor.
       
    35 // -----------------------------------------------------------------------------
       
    36 //
       
    37 CAknFepPredOwnWordsModel* CAknFepPredOwnWordsModel::NewL( MDesCArray* aTextArray )
       
    38     {
       
    39     CAknFepPredOwnWordsModel* self = CAknFepPredOwnWordsModel::NewLC( aTextArray );
       
    40     CleanupStack::Pop(self);
       
    41     return self;
       
    42     }
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // CAknFepPredOwnWordsModel::NewLC()
       
    46 // Two-phased constructor.
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 CAknFepPredOwnWordsModel* CAknFepPredOwnWordsModel::NewLC( MDesCArray* aTextArray )
       
    50     {
       
    51     CAknFepPredOwnWordsModel* self = new ( ELeave ) CAknFepPredOwnWordsModel();
       
    52     CleanupStack::PushL(self);
       
    53     self->ConstructL( aTextArray );
       
    54     return self;
       
    55     }
       
    56 // -----------------------------------------------------------------------------
       
    57 // CAknFepPredOwnWordsModel::ConstructL()
       
    58 // Symbian 2nd phase constructor can leave.
       
    59 // -----------------------------------------------------------------------------
       
    60 //
       
    61 void CAknFepPredOwnWordsModel::ConstructL( MDesCArray* aTextArray )
       
    62     {
       
    63     iPtiEngine = CPtiEngine::NewL(ETrue); 
       
    64     //for our purposes(adding/removing UDB entries), 
       
    65     //we are not dependent on language. Hence using English here
       
    66     //as it should be available on all variants/devices.
       
    67     iPtiEngine->ActivateLanguageL( ELangEnglish, EPtiEnginePredictive );
       
    68     
       
    69     //Now read the UDB entires into the text array
       
    70     UpdateItemsL( aTextArray );
       
    71     }
       
    72 // -----------------------------------------------------------------------------
       
    73 // CAknFepPredOwnWordsModel::~CAknFepPredOwnWordsModel()
       
    74 // C++ default constructor can NOT contain any code, that might leave.
       
    75 // -----------------------------------------------------------------------------
       
    76 //
       
    77 CAknFepPredOwnWordsModel::CAknFepPredOwnWordsModel()
       
    78     {
       
    79     // No implementation required
       
    80     }
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // CAknFepPredOwnWordsModel::~CAknFepPredOwnWordsModel()
       
    84 // Destructor.
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 CAknFepPredOwnWordsModel::~CAknFepPredOwnWordsModel()
       
    88     {
       
    89     delete iPtiEngine;
       
    90     }
       
    91 
       
    92 // -----------------------------------------------------------------------------
       
    93 // CAknFepPredOwnWordsModel::UpdateItemsL()
       
    94 // Destructor.
       
    95 // -----------------------------------------------------------------------------
       
    96 //
       
    97 void CAknFepPredOwnWordsModel::UpdateItemsL( MDesCArray* aTextArray )
       
    98     {
       
    99     iUDBWords = (CDesCArrayFlat*)aTextArray;
       
   100     ReadUDBEntriesL();
       
   101     }
       
   102 
       
   103 // -----------------------------------------------------------------------------
       
   104 //  Read all the user dictionary words through PtiEngine
       
   105 // -----------------------------------------------------------------------------
       
   106 //
       
   107 void CAknFepPredOwnWordsModel::ReadUDBEntriesL()
       
   108     {
       
   109     if (NULL == iUDBWords)
       
   110         {
       
   111         //iUDBWords is not created here. We take referrence of item array
       
   112         //from listbox parameter passed to the MAknFepPredUserDictEditingModel
       
   113         //methods. So this is an error condition - list box is not 
       
   114         //completely/properly initialized
       
   115         User::Leave(KErrCorrupt);
       
   116         }
       
   117     
       
   118     TInt numOfUDBEntries = iPtiEngine->NumberOfEntriesInUserDictionary();
       
   119     for (TInt i(0) ; i < numOfUDBEntries ; ++i)
       
   120         {
       
   121         TPtiUserDictionaryEntry udbEntry;
       
   122         TInt getEntryResult = iPtiEngine->GetUserDictionaryEntry( i, udbEntry);
       
   123         if ( KErrNone == getEntryResult )
       
   124             {
       
   125             HBufC *listEntry = FormListBoxEntryFromWordLC( udbEntry.Word() ); 
       
   126             iUDBWords->AppendL( *listEntry );
       
   127             CleanupStack::PopAndDestroy( listEntry );
       
   128             }
       
   129         else if ( KErrNotFound != getEntryResult )
       
   130             {
       
   131             //As this is not an empty slot, this could be an error condition
       
   132             User::Leave(KErrCorrupt);
       
   133             }   
       
   134         }
       
   135     iUDBWords->Sort();
       
   136     }
       
   137 
       
   138 // -----------------------------------------------------------------------------
       
   139 //  Helper method for listbox string formatting
       
   140 // -----------------------------------------------------------------------------
       
   141 //
       
   142 HBufC* CAknFepPredOwnWordsModel::FormListBoxEntryFromWordLC( const TDesC& aWord )
       
   143     {
       
   144     _LIT( KListItemPrefix, "\t" );
       
   145     TInt length = aWord.Length();
       
   146     //add 2 to length of word to store the tab seperator
       
   147     HBufC* listItem = HBufC::NewLC( length + 2 );
       
   148     listItem->Des().Copy( KListItemPrefix() ); 
       
   149     listItem->Des().Append( aWord );
       
   150     return ( listItem );
       
   151     }
       
   152 
       
   153 
       
   154 //Adding new word
       
   155 TInt CAknFepPredOwnWordsModel::AddNewWordL( TDesC& aNewWord )
       
   156     {
       
   157     TPtiUserDictionaryEntry udbEntry;
       
   158     udbEntry.SetWord( aNewWord );
       
   159     TInt status = iPtiEngine->AddUserDictionaryEntry( udbEntry );
       
   160     if (KErrNone == status || KErrAlreadyExists == status)
       
   161         {
       
   162         HBufC *listEntry = FormListBoxEntryFromWordLC( aNewWord ); 
       
   163         //If the word already exists, just move focus to it.
       
   164         //If it is a new word, add it to the list box items too.
       
   165         if ( KErrNone == status )
       
   166             {
       
   167             iUDBWords->AppendL( *listEntry );
       
   168             iUDBWords->Sort();
       
   169             }
       
   170         TInt selectedWordIndex(-1);
       
   171         iUDBWords->Find( *listEntry, selectedWordIndex, ECmpNormal );
       
   172         CleanupStack::PopAndDestroy( listEntry );
       
   173         return selectedWordIndex;
       
   174         }
       
   175     else
       
   176         {
       
   177         return KErrGeneral;
       
   178         }
       
   179     }
       
   180 
       
   181 //Removing existing word
       
   182 TInt CAknFepPredOwnWordsModel::RemoveSelectedWordL( TInt aWordIndex )
       
   183     {
       
   184     TPtiUserDictionaryEntry udbEntry;
       
   185     TPtrC word = ( (*iUDBWords)[aWordIndex] ).Mid(1);
       
   186     udbEntry.SetWord( word );
       
   187     if ( KErrNone == iPtiEngine->RemoveEntryFromUserDictionary( udbEntry ) )
       
   188         {
       
   189         iUDBWords->Delete( aWordIndex );
       
   190         return KErrNone;
       
   191         }
       
   192     else
       
   193         {
       
   194         return KErrGeneral;
       
   195         }
       
   196     }
       
   197 
       
   198 //Only to be called if it is known for sure that at least one item is marked/selected
       
   199 TInt CAknFepPredOwnWordsModel::RemoveSelectedWordsL( const CArrayFix<TInt>* aSelectedItems )
       
   200     {
       
   201     TPtiUserDictionaryEntry udbEntry;
       
   202     TInt numOfSelections = aSelectedItems->Count();
       
   203     /*TKeyArrayFix actNumKey(0,ECmpTInt);
       
   204     aSelectedItems->Sort( actNumKey );
       
   205     */
       
   206     //loop backwards so we don't disturb the indices after deletion
       
   207     for (TInt i(numOfSelections-1); i >= 0 ; --i )
       
   208         {
       
   209         TInt selectedItem = aSelectedItems->At(i);
       
   210         TPtrC word = ( (*iUDBWords)[selectedItem] ).Mid(1);
       
   211         udbEntry.SetWord( word );
       
   212         if ( KErrNone == iPtiEngine->RemoveEntryFromUserDictionary( udbEntry ) )
       
   213             {
       
   214             iUDBWords->Delete( selectedItem );
       
   215             }
       
   216         else
       
   217             {
       
   218             return KErrGeneral;
       
   219             }
       
   220         }
       
   221     
       
   222     return KErrNone;
       
   223     }
       
   224