src/hbplugins/inputengines/hbautocomplete/hbautocomplete.cpp
changeset 0 16d8024aca5e
child 1 f7ac710697a9
equal deleted inserted replaced
-1:000000000000 0:16d8024aca5e
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     6 **
       
     7 ** This file is part of the HbPlugins module of the UI Extensions for Mobile.
       
     8 **
       
     9 ** GNU Lesser General Public License Usage
       
    10 ** This file may be used under the terms of the GNU Lesser General Public
       
    11 ** License version 2.1 as published by the Free Software Foundation and
       
    12 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    13 ** Please review the following information to ensure the GNU Lesser General
       
    14 ** Public License version 2.1 requirements will be met:
       
    15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    16 **
       
    17 ** In addition, as a special exception, Nokia gives you certain additional
       
    18 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    20 **
       
    21 ** If you have questions regarding the use of this file, please contact
       
    22 ** Nokia at developer.feedback@nokia.com.
       
    23 **
       
    24 ****************************************************************************/
       
    25 #include "hbautocomplete.h"
       
    26 #include <hbinputextrauserdictionary.h>
       
    27 #include <hbinputextradictionarycollection.h>
       
    28 #include <hbinputextradictionaryfactory.h>
       
    29 
       
    30 /// @cond
       
    31 
       
    32 class HbAutoCompletePrivate
       
    33 {
       
    34 public:
       
    35     HbAutoCompletePrivate() 
       
    36          : mActiveDictionary(0),
       
    37            mActiveCollection(0),
       
    38            mCandidatesOutdated(false)
       
    39     {}
       
    40 
       
    41 public:
       
    42     HbExtraUserDictionary *mActiveDictionary;
       
    43     HbExtraDictionaryCollection *mActiveCollection;
       
    44     QString mCurrentWord;
       
    45     QStringList mCandidates;
       
    46     bool mCandidatesOutdated;
       
    47     QList<HbInputLanguage> mLanguages;
       
    48 };
       
    49 
       
    50 /// @endcond
       
    51 
       
    52 const QString HbAutoCompleteEngineVersion("0.1");
       
    53 
       
    54 /*!
       
    55 Constructs the object.
       
    56 */
       
    57 HbAutoComplete::HbAutoComplete() : d_ptr(new HbAutoCompletePrivate) 
       
    58 {
       
    59     Q_D(HbAutoComplete);
       
    60 
       
    61     d->mLanguages.append(HbInputLanguage());
       
    62 }
       
    63 
       
    64 /*!
       
    65 Destructs the object.
       
    66 */ 
       
    67 HbAutoComplete::~HbAutoComplete()
       
    68 {
       
    69     delete d_ptr;
       
    70 }
       
    71 
       
    72 /*!
       
    73 Reuturns list of supported languages. This engine can be used with any input language so it just returns
       
    74 a list containing HbInputLanguageAny.
       
    75 */
       
    76 QList<HbInputLanguage> HbAutoComplete::languages() const
       
    77 {
       
    78     Q_D(const HbAutoComplete);
       
    79     return QList<HbInputLanguage>(d->mLanguages);
       
    80 }
       
    81 
       
    82 /*!
       
    83 Handles delete key press. 
       
    84 */
       
    85 void HbAutoComplete::deleteKeyPress(HbPredictionCallback* callback)
       
    86 {
       
    87     Q_UNUSED(callback)
       
    88     Q_D(HbAutoComplete);
       
    89 
       
    90     d->mCurrentWord.chop(1);
       
    91     d->mCandidatesOutdated = true;
       
    92 }
       
    93 
       
    94 /*!
       
    95 Commits active word. Word is added to the dictionry (unless it isn't there already)
       
    96 and the use count is increased. In case of collection, the word is added to the first 
       
    97 dictionary in the collection unless it already exits in at least one of them.
       
    98 */
       
    99 void HbAutoComplete::commit(const QString &word)
       
   100 {
       
   101     Q_UNUSED(word);
       
   102     Q_D(HbAutoComplete);
       
   103 
       
   104     if (d->mCurrentWord.size()) {
       
   105         addUsedWord(d->mCurrentWord);
       
   106     }
       
   107 
       
   108     clear();
       
   109 }
       
   110 
       
   111 /*!
       
   112 Clears active word.
       
   113 */
       
   114 void HbAutoComplete::clear()
       
   115 {
       
   116     Q_D(HbAutoComplete);
       
   117 
       
   118     d->mCurrentWord.clear();
       
   119     d->mCandidates.clear();
       
   120 }
       
   121 
       
   122 /*!
       
   123 Adds word to active dictionary or collection if it doesn't already exits theer and increases the 
       
   124 use frequency counter. In case of a collection, word is added to the first enabled dictionary.
       
   125 */
       
   126 void HbAutoComplete::addUsedWord(const QString& word)
       
   127 {
       
   128     Q_D(HbAutoComplete);
       
   129 
       
   130     if (word.size() > 0) {
       
   131         if (d->mActiveDictionary) {
       
   132             d->mActiveDictionary->addWord(word);
       
   133             d->mActiveDictionary->incrementUseCount(word);
       
   134         } else if (d->mActiveCollection) {
       
   135             if (!d->mActiveCollection->hasWord(word)) {
       
   136                 QList<int> dictionaries = d->mActiveCollection->dictionaries();
       
   137                 for (int i = 0; i < dictionaries.count(); i++) {
       
   138                     if (!d->mActiveCollection->isDisabled(dictionaries[i])) {
       
   139                         HbExtraUserDictionary *firstOne = HbExtraDictionaryFactory::instance()->existingDictionary(dictionaries.at(i));
       
   140                         if (firstOne) {
       
   141                             firstOne->addWord(word);
       
   142                         }
       
   143                     }
       
   144                 }
       
   145             } else {
       
   146                 d->mActiveCollection->incrementUseCount(word);
       
   147             }
       
   148         }
       
   149     } 
       
   150 }
       
   151 
       
   152 /*!
       
   153 Handles key presses. Empty implementation, isn't used at the moment. 
       
   154 */
       
   155 void HbAutoComplete::appendKeyPress(const int keycode, const Qt::KeyboardModifiers modifiers, const HbTextCase textCase, HbPredictionCallback* callback)
       
   156 {
       
   157     Q_UNUSED(keycode)
       
   158     Q_UNUSED(modifiers)
       
   159     Q_UNUSED(textCase)
       
   160     Q_UNUSED(callback)
       
   161 }
       
   162 
       
   163 /*!
       
   164 Sets active word.
       
   165 */
       
   166 void HbAutoComplete::setWord(const QString& word, HbPredictionCallback* callback)
       
   167 {
       
   168     Q_UNUSED(callback)
       
   169     Q_D(HbAutoComplete);
       
   170 
       
   171     if (word.size() < KExtraUserDictionaryMaxWordLength) {
       
   172         d->mCurrentWord = word;
       
   173         d->mCandidatesOutdated = true;
       
   174     }
       
   175 }
       
   176 
       
   177 void HbAutoComplete::updateCandidates(int& bestGuessLocation, bool& noMoreCandidates)
       
   178 {
       
   179     Q_D(HbAutoComplete);
       
   180 
       
   181     bestGuessLocation = 0;
       
   182     noMoreCandidates = false;
       
   183  
       
   184     if (d->mCandidatesOutdated) {
       
   185         if (d->mActiveDictionary && d->mCurrentWord.size() > 0) {
       
   186             d->mCandidates = d->mActiveDictionary->findMatches(d->mCurrentWord, true); 
       
   187         } else if (d->mActiveCollection &&  d->mCurrentWord.size() > 0) {
       
   188             d->mCandidates = d->mActiveCollection->findMatches(d->mCurrentWord); 
       
   189         }
       
   190         d->mCandidatesOutdated = false;
       
   191     }
       
   192 }
       
   193 
       
   194 /*!
       
   195 returns a bit vector of supported prediction features.
       
   196 */ 
       
   197 HbInputPredictionFeature HbAutoComplete::features() const
       
   198 {
       
   199     return (HbInputPredictionFeature)(HbPredFeatureExtraDictionaries | HbPredFeatureWordCompletion);
       
   200 }
       
   201 
       
   202 /*!
       
   203 Returns vendor id string. 
       
   204 */
       
   205 QString HbAutoComplete::vendorIdString() const
       
   206 {
       
   207     return HbAutoCompleteVendorIdString;	
       
   208 }
       
   209 
       
   210 /*!
       
   211 Returns engine version string.
       
   212 */
       
   213 QString HbAutoComplete::engineVersion() const
       
   214 {
       
   215     return HbAutoCompleteEngineVersion;
       
   216 }
       
   217 
       
   218 /*!
       
   219 Returns true if the engine supports given language / keyboard combination. In case of HbAutoCompletion,
       
   220 this method always returns true because the keyboard doesn't matter.
       
   221 */
       
   222 bool HbAutoComplete::supportsKeyboardType(const HbInputLanguage &language, HbKeyboardType keyboard) const
       
   223 {
       
   224     Q_UNUSED(language)
       
   225     Q_UNUSED(keyboard)
       
   226 
       
   227     // Keyboard doesn't matter, all the communication is done via
       
   228     // appendKeyPressUnicode for now.
       
   229     return true;
       
   230 }
       
   231 
       
   232 
       
   233 /*!
       
   234 Sets active dictionary. Previous active dictionary or collection is disabled.
       
   235 */
       
   236 void HbAutoComplete::setExtraUserDictionary(int aId)
       
   237 {
       
   238     Q_D(HbAutoComplete);
       
   239 
       
   240    // Deactivate possible collection.
       
   241     delete d->mActiveCollection;
       
   242     d->mActiveCollection = 0;
       
   243 
       
   244     d->mActiveDictionary = HbExtraDictionaryFactory::instance()->createDictionary(aId);
       
   245 	if (d->mActiveDictionary) {
       
   246 		d->mActiveDictionary->setHostEngine(this);
       
   247 	}
       
   248     clear();
       
   249 }
       
   250 
       
   251 /*!
       
   252 Sets active dictionary collection. Previous active dictionary or collection is disabled.
       
   253 */
       
   254 void HbAutoComplete::setExtraUserDictionaries(const QList<int>& idList)
       
   255 {
       
   256     Q_UNUSED(idList)
       
   257     Q_D(HbAutoComplete);
       
   258 
       
   259     d->mActiveDictionary = 0;  // Not owned, so doesn't need to be deleted.
       
   260     delete d->mActiveCollection;
       
   261 
       
   262     d->mActiveCollection = new HbExtraDictionaryCollection(idList);
       
   263     clear();
       
   264 }
       
   265 
       
   266 /*!
       
   267 Returns the length of current input sequence.
       
   268 */
       
   269 int HbAutoComplete::inputLength()
       
   270 {
       
   271     Q_D(HbAutoComplete);
       
   272  
       
   273     return d->mCurrentWord.size();
       
   274 }
       
   275 
       
   276 /*!
       
   277 An empty implementation. Not needed in HbAutoCompletion.
       
   278 */
       
   279 void HbAutoComplete::setCandidateList(QStringList* candidateList)
       
   280 {
       
   281     Q_UNUSED(candidateList)
       
   282 }
       
   283 
       
   284 /*!
       
   285 Returns current candidate list. Reconstructs the list only when it is out of date.
       
   286 */
       
   287 QStringList HbAutoComplete::candidateList()
       
   288 {
       
   289     Q_D(HbAutoComplete);
       
   290 
       
   291     if (d->mCandidatesOutdated) {
       
   292         if (d->mActiveDictionary && d->mCurrentWord.size() > 0) {
       
   293             d->mCandidates = d->mActiveDictionary->findMatches(d->mCurrentWord, true); 
       
   294         } else if (d->mActiveCollection && d->mCurrentWord.size() > 0) {
       
   295             d->mCandidates = d->mActiveCollection->findMatches(d->mCurrentWord);
       
   296         } else {
       
   297             d->mCandidates.clear();
       
   298         }
       
   299         d->mCandidatesOutdated = false;
       
   300     }
       
   301 
       
   302     return QStringList(d->mCandidates);
       
   303 }
       
   304 
       
   305 /*!
       
   306 Sets keyboard type. This method is not needed in HbAutoCompletion.
       
   307 */
       
   308 void HbAutoComplete::setKeyboard(HbKeyboardType aKeyboardType)
       
   309 {
       
   310     Q_UNUSED(aKeyboardType)
       
   311 }
       
   312 
       
   313 /*!
       
   314 Appends new character to be used as part of the input sequence. The candidate list is flagged to be
       
   315 out of date.
       
   316 */
       
   317 void HbAutoComplete::appendCharacter(const QChar aChar, const HbTextCase textCase, HbPredictionCallback* callback)
       
   318 {
       
   319     Q_UNUSED(callback)
       
   320     Q_UNUSED(textCase)
       
   321     Q_D(HbAutoComplete);
       
   322 
       
   323     if (d->mCurrentWord.size() < KExtraUserDictionaryMaxWordLength) {
       
   324         d->mCurrentWord.append(aChar);
       
   325         d->mCandidatesOutdated = true;
       
   326     }
       
   327 }
       
   328 
       
   329 // End of file 
       
   330