emailservices/nmutilities/src/emailmru.cpp
changeset 68 83cc6bae1de8
child 74 6c59112cfd31
equal deleted inserted replaced
67:459da34cdb45 68:83cc6bae1de8
       
     1 /*
       
     2  * Copyright (c) 2007-2010 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:  Object for accessing most recent used email addresses and
       
    15  * display names.
       
    16  *
       
    17  */
       
    18 
       
    19 #include "emailtrace.h"
       
    20 #include "emailmru.h"
       
    21 
       
    22 #include <XQSettingsManager>
       
    23 #include <XQCentralRepositoryUtils>
       
    24 #include <XQCentralRepositorySearchCriteria>
       
    25 
       
    26 /*!
       
    27     Constructor
       
    28  */
       
    29 
       
    30 // ID 0, Last saved ID.
       
    31 // ID 1, Count of MRU Items.
       
    32 
       
    33 EmailMRU::EmailMRU()
       
    34 {
       
    35     NM_FUNCTION;
       
    36 
       
    37     mRepositoryManager = new XQSettingsManager();
       
    38     Q_CHECK_PTR(mRepositoryManager);
       
    39 
       
    40     mLatestIndex = readCenRep(currentIndexKey).toInt();
       
    41     mCount = readCenRep(countKey).toInt();
       
    42 }
       
    43 
       
    44 /*!
       
    45     Destructor
       
    46  */
       
    47 EmailMRU::~EmailMRU()
       
    48 {
       
    49     NM_FUNCTION;
       
    50 
       
    51     QString entryIndex;
       
    52     entryIndex.setNum(mLatestIndex);
       
    53 
       
    54     QString count;
       
    55     count.setNum(mCount);
       
    56 
       
    57     writeCenRep(currentIndexKey, entryIndex);
       
    58     writeCenRep(countKey, count);
       
    59 
       
    60     delete mRepositoryManager;
       
    61 }
       
    62 
       
    63 /*!
       
    64     Add entry to MRU database
       
    65     \param displayName
       
    66     \param emailAddress
       
    67     \return success, was the update operation successful
       
    68  */
       
    69 bool EmailMRU::updateMRU(QString displayName, QString emailAddress)
       
    70 {
       
    71     NM_FUNCTION;
       
    72 
       
    73     bool success = false;
       
    74     qint32 foundEntryIndex = searchForAddress(emailAddress);
       
    75     if (foundEntryIndex) {
       
    76         success = updateEntry(foundEntryIndex, displayName);
       
    77     }
       
    78 
       
    79     else {
       
    80         qint32 entryIndex = getNextEntryIndex();
       
    81         success = writeEntry(entryIndex, displayName, emailAddress);
       
    82         mCount++;
       
    83         if (mCount > emailAddressHistorySize) {
       
    84             mCount = emailAddressHistorySize;
       
    85         }
       
    86     }
       
    87 
       
    88     return success;
       
    89 }
       
    90 
       
    91 /*!
       
    92     Get the number of entries
       
    93     \return number of entries
       
    94  */
       
    95 quint32 EmailMRU::entryCount() const
       
    96 {
       
    97     return mCount;
       
    98 }
       
    99 
       
   100 /*!
       
   101     Get entry details
       
   102     \param entryIndex entry index to look for
       
   103     \param displayName will contain displayname of entry if found
       
   104     \param address will contain address of entry if found
       
   105     \return success
       
   106  */
       
   107 bool EmailMRU::getEntry(qint32 entryIndex, QString &displayName, QString &address)
       
   108 {
       
   109     NM_FUNCTION;
       
   110 
       
   111     bool success = false;
       
   112 
       
   113     if ((entryIndex >= 1) && (mCount > 0)) {
       
   114         displayName = readCenRep(nameKeyByEntryIndex(entryIndex));
       
   115         address = readCenRep(addressKeyByEntryIndex(entryIndex));
       
   116         success = true;
       
   117 
       
   118         if (address.size()==0) {
       
   119             success = false;
       
   120         }
       
   121     }
       
   122 
       
   123     return success;
       
   124 }
       
   125 
       
   126 /*!
       
   127     Resets repository
       
   128  */
       
   129 void EmailMRU::reset()
       
   130 {
       
   131     NM_FUNCTION;
       
   132 
       
   133     XQCentralRepositoryUtils utils(*mRepositoryManager);
       
   134     utils.resetRepository(emailMruRepositoryKey);
       
   135     mLatestIndex = 0;
       
   136     mCount = 0;
       
   137 }
       
   138 
       
   139 /*!
       
   140     Writes to central repository
       
   141     \param index where to write
       
   142     \param value what to write
       
   143     \return success, whether write operation succeeded
       
   144  */
       
   145 bool EmailMRU::writeCenRep(qint32 index, QString value)
       
   146 {
       
   147     NM_FUNCTION;
       
   148 
       
   149     XQCentralRepositorySettingsKey writeKey(emailMruRepositoryKey, index);
       
   150     bool success = mRepositoryManager->writeItemValue(writeKey, value);
       
   151     if (!success) {
       
   152         XQCentralRepositoryUtils utils(*mRepositoryManager);
       
   153         success = utils.createKey(writeKey, value);
       
   154     }
       
   155 
       
   156     return success;
       
   157 }
       
   158 
       
   159 /*!
       
   160     Reads from central repository
       
   161     \param index to read
       
   162     \return value
       
   163  */
       
   164 QString EmailMRU::readCenRep(qint32 index)
       
   165 {
       
   166     NM_FUNCTION;
       
   167 
       
   168     XQCentralRepositorySettingsKey readKey(emailMruRepositoryKey, index);
       
   169     return mRepositoryManager->readItemValue(readKey, XQSettingsManager::TypeString).value<QString> ();
       
   170 }
       
   171 
       
   172 /*!
       
   173     Translates entry indexes into real central repository indexes
       
   174     \return central repository index
       
   175  */
       
   176 quint32 EmailMRU::getNextEntryIndex()
       
   177 {
       
   178     NM_FUNCTION;
       
   179 
       
   180     mLatestIndex++;
       
   181 
       
   182     if (mLatestIndex > emailAddressHistorySize) {
       
   183         mLatestIndex = 1;
       
   184     }
       
   185 
       
   186     return mLatestIndex;
       
   187 }
       
   188 
       
   189 /*!
       
   190     Translates central repository indexes into entry index
       
   191     \param realIndex central repository index
       
   192     \return entry index
       
   193  */
       
   194 qint32 EmailMRU::entryIndex(qint32 crKey)
       
   195 {
       
   196     NM_FUNCTION;
       
   197 
       
   198     qint32 entryIndex = 0;
       
   199 
       
   200     if (crKey%2 == 0){
       
   201         entryIndex = crKey/2;
       
   202     }
       
   203     else {
       
   204         entryIndex = (crKey-1)/2;
       
   205     }
       
   206 
       
   207     return entryIndex;
       
   208 }
       
   209 
       
   210 /*!
       
   211     Translates entry index into cenrep email address index
       
   212     \param entryIndex
       
   213     \return cenrep email address index
       
   214  */
       
   215 qint32 EmailMRU::addressKeyByEntryIndex(qint32 entryIndex)
       
   216 {
       
   217     return entryIndex*2+1;
       
   218 }
       
   219 
       
   220 /*!
       
   221     Translates entry index into cenrep displayname index
       
   222     \param entryIndex
       
   223     \return cenrep displayname index
       
   224  */
       
   225 qint32 EmailMRU::nameKeyByEntryIndex(qint32 entryIndex)
       
   226 {
       
   227     return entryIndex*2;
       
   228 }
       
   229 
       
   230 /*!
       
   231     Checks if MRU list already contains the address
       
   232     \param address to search
       
   233     \return entry index of found address, zero if not found
       
   234  */
       
   235 qint32 EmailMRU::searchForAddress(QString address)
       
   236 {
       
   237     NM_FUNCTION;
       
   238 
       
   239     quint32 partialKey(0x00000000);
       
   240     quint32 bitMask(0x00000000);
       
   241     XQCentralRepositorySearchCriteria criteria(emailMruRepositoryKey, partialKey, bitMask);
       
   242     criteria.setValueCriteria(address);
       
   243 
       
   244     // Find the keys.
       
   245     XQCentralRepositoryUtils utils(*mRepositoryManager);
       
   246     QList<XQCentralRepositorySettingsKey> foundKeys = utils.findKeys(criteria);
       
   247 
       
   248     qint32 index = 0;
       
   249     // We should only get one key as a result.
       
   250     if (foundKeys.count() == 1) {
       
   251         index = foundKeys[0].key();
       
   252     }
       
   253     return entryIndex(index);
       
   254 }
       
   255 
       
   256 /*!
       
   257     Moves entry, will overwrite the newEntryIndex
       
   258     \param oldEntryIndex entry to be moved
       
   259     \param newEntryIndex where to move
       
   260  */
       
   261 void EmailMRU::moveEntry(qint32 oldEntryIndex, qint32 newEntryIndex)
       
   262 {
       
   263     NM_FUNCTION;
       
   264 
       
   265     // Save the values to be moved
       
   266     QString name = readCenRep(nameKeyByEntryIndex(oldEntryIndex));
       
   267     QString address = readCenRep(addressKeyByEntryIndex(oldEntryIndex));
       
   268 
       
   269     // Overwrite values into new entry
       
   270     writeEntry(newEntryIndex, name, address);
       
   271 }
       
   272 
       
   273 /*!
       
   274     Updates displayname and sets entry as newest
       
   275     \param entryIndex to update
       
   276     \param newDisplayName
       
   277     \return success was update operation successfull
       
   278  */
       
   279 bool EmailMRU::updateEntry(qint32 entryIndex, QString newDisplayName)
       
   280 {
       
   281     NM_FUNCTION;
       
   282 
       
   283     // Save address
       
   284     QString address = readCenRep(addressKeyByEntryIndex(entryIndex));
       
   285 
       
   286     // Move all entries following the one to be updated
       
   287     // until we go past the newest entry
       
   288     for (qint32 i = entryIndex, j = 0; i != mLatestIndex;) {
       
   289         j = i+1;
       
   290         if (j > emailAddressHistorySize) j = 1;
       
   291 
       
   292         moveEntry(j, i);
       
   293 
       
   294         i++;
       
   295         if (i > emailAddressHistorySize) i = 1;
       
   296     }
       
   297 
       
   298     // Write the updated entry as the newest entry
       
   299     return writeEntry(mLatestIndex, newDisplayName, address);
       
   300 }
       
   301 
       
   302 /*!
       
   303     Writes entry. Overwrites any existing data at give index
       
   304     \param entryIndex to write
       
   305     \param displayName
       
   306     \param emailAddress
       
   307     \return success was update operation successfull
       
   308  */
       
   309 bool EmailMRU::writeEntry(qint32 entryIndex, QString displayName, QString emailAddress)
       
   310 {
       
   311     NM_FUNCTION;
       
   312 
       
   313     bool nameSuccess = false;
       
   314     bool addressSuccess = false;
       
   315 
       
   316     nameSuccess = writeCenRep(nameKeyByEntryIndex(entryIndex), displayName);
       
   317     if (nameSuccess) {
       
   318         addressSuccess = writeCenRep(addressKeyByEntryIndex(entryIndex), emailAddress);
       
   319     }
       
   320 
       
   321     // Writing name succeeded but writing address not, we destroy the name
       
   322     if (!addressSuccess) {
       
   323         XQCentralRepositoryUtils utils(*mRepositoryManager);
       
   324         XQCentralRepositorySettingsKey deleteKey(emailMruRepositoryKey, nameKeyByEntryIndex(entryIndex));
       
   325         utils.deleteKey(deleteKey);
       
   326     }
       
   327 
       
   328     return (nameSuccess & addressSuccess);
       
   329 }
       
   330 
       
   331 /*!
       
   332     Writes entry. Overwrites any existing data at give index
       
   333     \return contacts setting for lastname / firstname order.
       
   334  */
       
   335 int EmailMRU::nameOrder()
       
   336 {
       
   337     XQSettingsManager *repositoryManager = new XQSettingsManager();
       
   338     Q_CHECK_PTR(repositoryManager);
       
   339 
       
   340     XQCentralRepositorySettingsKey readKey(contactsRepositoryKey,
       
   341                                            nameOrderKey);
       
   342 
       
   343     int nameOrder = repositoryManager->readItemValue(readKey,
       
   344                                                      XQSettingsManager::TypeInt).toInt();
       
   345 
       
   346     delete repositoryManager;
       
   347 
       
   348     return nameOrder;
       
   349 }
       
   350