userdict/src/UdictModel.cpp
branchRCL_3
changeset 13 261ff9819be3
parent 0 9a3a17bfeb67
equal deleted inserted replaced
12:e9f0e1110077 13:261ff9819be3
       
     1 /*
       
     2 * Copyright (c) 2002-2004 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:     Japanese user dictionary Model class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 // INCLUDE FILES
       
    23 #include    "UdictModel.h"
       
    24 #include    "UdictModel.inl"
       
    25 #include    <PtiEngine.h>
       
    26 
       
    27 // CONSTANTS
       
    28 const TInt KItemListGranularity(8);
       
    29 _LIT(KPanicCatModel, "UserDictModel");
       
    30 
       
    31 // ============================ MEMBER FUNCTIONS ===============================
       
    32 
       
    33 // -----------------------------------------------------------------------------
       
    34 // CUserDictModel::NewL
       
    35 // Two-phased constructor.
       
    36 // -----------------------------------------------------------------------------
       
    37 //
       
    38 CUserDictModel* CUserDictModel::NewL()
       
    39     {
       
    40     CUserDictModel* self = new(ELeave) CUserDictModel;
       
    41     CleanupStack::PushL(self);
       
    42     self->ConstructL();
       
    43     CleanupStack::Pop();
       
    44 
       
    45     return self;
       
    46     }
       
    47 
       
    48 // Destructor
       
    49 CUserDictModel::~CUserDictModel()
       
    50     {
       
    51     delete iItemList;
       
    52     delete iPtiEngine;
       
    53     }
       
    54 
       
    55 // -----------------------------------------------------------------------------
       
    56 // CUserDictModel::AddNewWordL
       
    57 // -----------------------------------------------------------------------------
       
    58 //    
       
    59 TInt CUserDictModel::AddNewWordL(const TUDictElement& aItem)
       
    60     {
       
    61     User::LeaveIfError(IsDictionaryFull() ? KErrDiskFull : KErrNone);
       
    62 
       
    63     // Checks the word list if the entry is not registered to it.
       
    64     TInt index(0);
       
    65     TWordListKey key;
       
    66     if (iItemList->Count() > 0 && !iItemList->FindIsq(aItem, key, index))
       
    67         {
       
    68         User::Leave(KErrAlreadyExists);
       
    69         }
       
    70 
       
    71     // Adds a new entry to the User dictionary.
       
    72     User::LeaveIfError(
       
    73         iPtiEngine->AddUserDictionaryEntry(const_cast<TUDictElement&>(aItem)));
       
    74           
       
    75     // Adds a new entry to the registered word list.
       
    76     iItemList->InsertL(index, aItem);
       
    77 
       
    78     return index;
       
    79     }
       
    80     
       
    81 // -----------------------------------------------------------------------------
       
    82 // CUserDictModel::DeleteWordL
       
    83 // -----------------------------------------------------------------------------
       
    84 //    
       
    85 void CUserDictModel::DeleteWordL(TInt aIndex)
       
    86     {
       
    87     ReloadDictInfoL();
       
    88     const TUDictElement& item = ListItem(aIndex);
       
    89 
       
    90     // Removes the entry from the User dictionary.
       
    91     TInt err(iPtiEngine->RemoveEntryFromUserDictionary(
       
    92                             const_cast<TUDictElement&>(item)));
       
    93     // KErrNotFound means the entry is incorrect. It should be ASSERT.
       
    94     __ASSERT_ALWAYS(err != KErrNotFound, User::Panic(KPanicCatModel, err));
       
    95 
       
    96     // Removes the entry from the registered word list.
       
    97     iItemList->Delete(aIndex);
       
    98     }
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // CUserDictModel::ModifyWordL
       
   102 // -----------------------------------------------------------------------------
       
   103 //    
       
   104 TInt CUserDictModel::ModifyWordL(TInt aIndex, const TUDictElement& aItem)
       
   105     {
       
   106     TInt index = AddNewWordL(aItem);
       
   107     if (index <= aIndex)
       
   108         {
       
   109         DeleteWordL(aIndex + 1);
       
   110         }
       
   111     else
       
   112         {
       
   113         DeleteWordL(aIndex);
       
   114         index--;
       
   115         }
       
   116     return index;
       
   117     }
       
   118 
       
   119 // -----------------------------------------------------------------------------
       
   120 // CUserDictModel::InitListL
       
   121 // -----------------------------------------------------------------------------
       
   122 //    
       
   123 void CUserDictModel::InitListL()
       
   124     {
       
   125     iItemList->Reset();
       
   126 
       
   127     TUDictElement entry;
       
   128     TInt i(0);
       
   129     // Copies the User dictionary entries to iItemList.
       
   130     while (iPtiEngine->GetUserDictionaryEntry(i, entry) == KErrNone)
       
   131         {
       
   132         iItemList->AppendL(entry);
       
   133         i++;
       
   134         }
       
   135 
       
   136     // Sorts the word list by Unicode collation algorithm.
       
   137     TWordListKey key; 
       
   138     iItemList->Sort(key);
       
   139     
       
   140     SetChangeNotice(EFalse);
       
   141     }
       
   142 
       
   143 // -----------------------------------------------------------------------------
       
   144 // CUserDictModel::CUserDictModel
       
   145 // C++ default constructor can NOT contain any code, that
       
   146 // might leave.
       
   147 // -----------------------------------------------------------------------------
       
   148 //
       
   149 CUserDictModel::CUserDictModel()
       
   150     {
       
   151     }
       
   152 
       
   153 // -----------------------------------------------------------------------------
       
   154 // CUserDictModel::ConstructL
       
   155 // Symbian 2nd phase constructor can leave.
       
   156 // -----------------------------------------------------------------------------
       
   157 //
       
   158 void CUserDictModel::ConstructL()
       
   159     {
       
   160     iPtiEngine = CPtiEngine::NewL(ETrue);
       
   161     User::LeaveIfError(
       
   162         iPtiEngine->ActivateLanguageL(ELangJapanese, EPtiEngineHiraganaKanji));
       
   163     iDictionary = 
       
   164         iPtiEngine->DefaultUserDictionary(EPtiEngineHiraganaKanji);
       
   165     User::LeaveIfNull(iDictionary);
       
   166 
       
   167     iItemList = new (ELeave) CUserDictList(KItemListGranularity);
       
   168     }
       
   169 
       
   170 // -----------------------------------------------------------------------------
       
   171 // CUserDictModel::TWordListKey::TWordListKey
       
   172 // -----------------------------------------------------------------------------
       
   173 //
       
   174 CUserDictModel::TWordListKey::TWordListKey()
       
   175 :TKeyArrayFix(0, ECmpCollated)
       
   176     {
       
   177     }
       
   178 
       
   179 // -----------------------------------------------------------------------------
       
   180 // CUserDictModel::TWordListKey::Compare
       
   181 // -----------------------------------------------------------------------------
       
   182 //
       
   183 TInt CUserDictModel::TWordListKey::Compare(TInt aLeft,TInt aRight) const
       
   184     {
       
   185     TUDictElement* left = static_cast<TUDictElement*>(At(aLeft));
       
   186     TUDictElement* right = static_cast<TUDictElement*>(At(aRight));
       
   187     
       
   188     TInt res(left->Reading().CompareC(right->Reading()));
       
   189     if (res == 0)
       
   190         {
       
   191         res = left->Word().CompareC(right->Word());
       
   192         }
       
   193     return res;
       
   194     }
       
   195 
       
   196 // -----------------------------------------------------------------------------
       
   197 // CUserDictModel::MdcaCount
       
   198 // -----------------------------------------------------------------------------
       
   199 //
       
   200 TInt CUserDictModel::MdcaCount() const
       
   201     {
       
   202     return iItemList->Count();
       
   203     }
       
   204 
       
   205 // -----------------------------------------------------------------------------
       
   206 // CUserDictModel::MdcaPoint
       
   207 // -----------------------------------------------------------------------------
       
   208 //
       
   209 TPtrC16 CUserDictModel::MdcaPoint(TInt aIndex) const
       
   210     {
       
   211     TPtrC16 ptr(iItemList->At(aIndex).ListItemText());
       
   212     return ptr;
       
   213     }
       
   214 
       
   215 //  End of File