phonebookui/cntlistmodel/cntcacheitems.cpp
changeset 81 640d30f4fb64
equal deleted inserted replaced
77:c18f9fa7f42e 81:640d30f4fb64
       
     1 /*
       
     2 * Copyright (c) 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: Classes for each of the items (name, info and icon) in cache.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <cntcacheitems.h>
       
    19 
       
    20 /*!
       
    21     \class CntNameCacheItem
       
    22     \brief Represents one name entry in cache.
       
    23     
       
    24     CntNameCacheItem wraps functionality for efficiently storing, serializing
       
    25     and changing the display format of one name in cache.
       
    26 
       
    27     \class CntInfoCacheItem
       
    28     \brief Holds data for info entry in cache.
       
    29 
       
    30     \class CntIconCacheItem
       
    31     \brief Holds data for icon entry in cache.
       
    32  */
       
    33 
       
    34 /*!
       
    35     Creates a CntNameCacheItem object.
       
    36  */
       
    37 CntNameCacheItem::CntNameCacheItem(QContactLocalId id, const QString& firstName, const QString& lastName, CntNameOrder nameFormat)
       
    38 {
       
    39     mContactId = id;
       
    40     setFormattedName(firstName, lastName, nameFormat);
       
    41 }
       
    42 
       
    43 /*!
       
    44     Destroys the CntNameCacheItem object.
       
    45  */
       
    46 CntNameCacheItem::~CntNameCacheItem()
       
    47 {
       
    48 }
       
    49 
       
    50 /*!
       
    51     Changes the format used to present the name.
       
    52  */
       
    53 void CntNameCacheItem::setNameFormat(CntNameOrder newFormat)
       
    54 {
       
    55     QString firstName = mName.mid(mFirstNamePosition&0xffff, mFirstNamePosition>>16);
       
    56     QString lastName = mName.mid(mLastNamePosition&0xffff, mLastNamePosition>>16);
       
    57     setFormattedName(firstName, lastName, newFormat);
       
    58 }
       
    59 
       
    60 /*!
       
    61     Copies the contents of the other cache item to this one.
       
    62  */
       
    63 void CntNameCacheItem::operator=(const CntNameCacheItem &other)
       
    64 {
       
    65     mContactId = other.mContactId;
       
    66     mFirstNamePosition = other.mFirstNamePosition;
       
    67     mLastNamePosition = other.mLastNamePosition;
       
    68     mName = other.mName;
       
    69 }
       
    70 
       
    71 /*!
       
    72     Externalizes a CntNameCacheItem object.
       
    73  */
       
    74 void CntNameCacheItem::externalize(QDataStream &stream)
       
    75 {
       
    76     stream << mContactId;
       
    77     stream << mFirstNamePosition;
       
    78     stream << mLastNamePosition;
       
    79     stream << mName;
       
    80 }
       
    81 
       
    82 /*!
       
    83     Internalizes a CntNameCacheItem object.
       
    84  */
       
    85 CntNameCacheItem* CntNameCacheItem::internalize(QDataStream &stream, CntNameOrder nameFormat)
       
    86 {
       
    87     quint32 id;
       
    88     quint32 firstNamePosition;
       
    89     quint32 lastNamePosition;
       
    90     QString name;
       
    91     
       
    92     stream >> id;
       
    93     stream >> firstNamePosition;
       
    94     stream >> lastNamePosition;
       
    95     stream >> name;
       
    96     
       
    97     QString firstName = name.mid(firstNamePosition&0xffff, firstNamePosition>>16);
       
    98     QString lastName = name.mid(lastNamePosition&0xffff, lastNamePosition>>16);
       
    99 
       
   100     return new CntNameCacheItem(id, firstName, lastName, nameFormat);
       
   101 }
       
   102 
       
   103 /*!
       
   104     Sets the formatted name and positions of the first name and last name,
       
   105     according to the name format in the parameter.
       
   106  */
       
   107 void CntNameCacheItem::setFormattedName(const QString& firstName, const QString& lastName, CntNameOrder nameFormat)
       
   108 {
       
   109     int firstNameLength = firstName.length();
       
   110     int lastNameLength = lastName.length();
       
   111 
       
   112     if (lastNameLength == 0) {
       
   113         mName = firstName;
       
   114         mFirstNamePosition = firstNameLength << 16;
       
   115         mLastNamePosition = 0;
       
   116     } else if (firstNameLength == 0) {
       
   117         mName = lastName;
       
   118         mFirstNamePosition = 0;
       
   119         mLastNamePosition = lastNameLength << 16;
       
   120     } else {
       
   121         if (nameFormat == CntOrderLastFirst) {
       
   122             mName = lastName + " " + firstName;
       
   123             mFirstNamePosition = (firstNameLength << 16) | (lastNameLength + 1);
       
   124             mLastNamePosition = (lastNameLength << 16);
       
   125         } else if (nameFormat == CntOrderLastCommaFirst) {
       
   126             mName = lastName + ", " + firstName;
       
   127             mFirstNamePosition = (firstNameLength << 16) | (lastNameLength + 2);
       
   128             mLastNamePosition = (lastNameLength << 16);
       
   129         } else {
       
   130             mName = firstName + " " + lastName;
       
   131             mFirstNamePosition = (firstNameLength << 16);
       
   132             mLastNamePosition = (lastNameLength << 16) | (firstNameLength + 1);
       
   133         }
       
   134     }
       
   135 }
       
   136 
       
   137 QString CntNameCacheItem::firstName() const
       
   138 {
       
   139     return mName.mid(mFirstNamePosition&0xffff, mFirstNamePosition>>16);
       
   140 }
       
   141 
       
   142 QString CntNameCacheItem::lastName() const
       
   143 {
       
   144     return mName.mid(mLastNamePosition&0xffff, mLastNamePosition>>16);
       
   145 }