emailservices/nmutilities/src/nmcontacthistorymodel_p.cpp
changeset 74 6c59112cfd31
parent 68 83cc6bae1de8
child 76 38bf5461e270
equal deleted inserted replaced
69:4e54af54a4a1 74:6c59112cfd31
    14 * Description: Private implementation of Contact History Model API
    14 * Description: Private implementation of Contact History Model API
    15 *
    15 *
    16 */
    16 */
    17 
    17 
    18 #include "emailmru.h"
    18 #include "emailmru.h"
       
    19 #include "emailtrace.h"
    19 #include "nmcontacthistorymodel_p.h"
    20 #include "nmcontacthistorymodel_p.h"
    20 
    21 
    21 // --------------------------------------------------------------------------
    22 // --------------------------------------------------------------------------
    22 // Start of implementation (NmContactHistoryModelPrivate)
    23 // Start of implementation (NmContactHistoryModelPrivate)
    23 // --------------------------------------------------------------------------
    24 // --------------------------------------------------------------------------
    28     NmContactHistoryModelPrivate::HistoryModelType enum.
    29     NmContactHistoryModelPrivate::HistoryModelType enum.
    29 */
    30 */
    30 NmContactHistoryModelPrivate::NmContactHistoryModelPrivate(
    31 NmContactHistoryModelPrivate::NmContactHistoryModelPrivate(
    31     const NmContactHistoryModelType modelType) :
    32     const NmContactHistoryModelType modelType) :
    32     mType(modelType),
    33     mType(modelType),
    33     mContactManager(0),
    34     mContactManager(NULL),
    34     mModelReady(false)
    35     mModelReady(false)
    35 {
    36 {
       
    37     NM_FUNCTION;
       
    38 
    36     mContactManager = new QContactManager("symbian");
    39     mContactManager = new QContactManager("symbian");
    37     mNameOrder = EmailMRU::nameOrder();
    40     mNameOrder = EmailMRU::nameOrder();
    38 }
    41 }
    39 
    42 
    40 /*!
    43 /*!
    41     Destructor of ContactHistoryModel
    44     Destructor of ContactHistoryModel
    42 */
    45 */
    43 NmContactHistoryModelPrivate::~NmContactHistoryModelPrivate()
    46 NmContactHistoryModelPrivate::~NmContactHistoryModelPrivate()
    44 {
    47 {
       
    48     NM_FUNCTION;
       
    49 
    45     delete mContactManager;
    50     delete mContactManager;
    46     mPrivateItemList.clear();
    51     mPrivateItemList.clear();
    47     mModelItemList.clear();
    52     mModelItemList.clear();
    48 }
    53 }
    49 
    54 
    56 
    61 
    57     Note:: QContactManager supports max. ten characters long queries.
    62     Note:: QContactManager supports max. ten characters long queries.
    58 */
    63 */
    59 void NmContactHistoryModelPrivate::queryDatabases(const QString& query)
    64 void NmContactHistoryModelPrivate::queryDatabases(const QString& query)
    60 {
    65 {
       
    66     NM_FUNCTION;
       
    67 
    61     mModelReady = false;
    68     mModelReady = false;
    62     // Clear contacts in the list
    69     // Clear contacts in the list
    63     mPrivateItemList.clear();
    70     mPrivateItemList.clear();
    64     mModelItemList.clear();
    71     mModelItemList.clear();
    65     mMruList.clear();
    72     mMruList.clear();
    66     mMruMatches.clear();
    73     mMruMatches.clear();
    67 
    74 
       
    75     mNameOrder = EmailMRU::nameOrder();
       
    76 
    68     // Modify search to suit our needs
    77     // Modify search to suit our needs
    69     // Space must be removed, because it is understood as logigal AND
    78     // Space must be removed, because it is understood as logigal AND
    70     // with QContactManager API
    79     // with QContactManager API
    71     QString modifiedQuery = query;
    80     QString modifiedQuery = query;
    72 
    81 
    73     int spcPosition = query.indexOf(" ");
    82     int spcPosition = query.indexOf(" ");
    74 
    83 
    75     if ( spcPosition != -1 )
    84     if ( spcPosition != -1 )
    76         {
    85     {
    77         modifiedQuery = query.left(spcPosition);
    86         modifiedQuery = query.left(spcPosition);
    78         }
    87     }
    79 
    88 
       
    89     // Get matching MRU items
    80     queryMruDatabase(modifiedQuery);
    90     queryMruDatabase(modifiedQuery);
       
    91     
       
    92     // Populate mPrivateItemList with matching MRU items.
    81     populateListWithMruItems(modifiedQuery);
    93     populateListWithMruItems(modifiedQuery);
    82 
    94 
    83     if (mContactManager)
    95     // Get matching IDs from Contacts DB
    84         {
    96     QList<QContactLocalId> cnt_ids = queryContactDatabase(modifiedQuery);
    85         // Get matching IDs from Contacts DB
    97 
    86         QList<QContactLocalId> cnt_ids = queryContactDatabase(modifiedQuery);
    98     // Populate mPrivateItemList with contact items.
    87 
    99     populateListWithContactItems(cnt_ids, modifiedQuery);
    88         // Populate mPrivateItemList with contact items.
   100 
    89         populateListWithContactItems(cnt_ids, modifiedQuery);
   101 
    90         }
   102     #ifdef _DEBUG
    91 
   103     
    92     // Currently we will always emit 0 as completion code.
   104         for (int i = 0; i < mPrivateItemList.size(); i++)
    93 
   105         {
    94     //TODO: Will be removed, Debug Code.
   106             QSharedPointer<NmContactHistoryModelItemData> itemData = mPrivateItemList[i];
    95     for (int i = 0; i < mPrivateItemList.size(); i++)
   107 
    96         {
   108             QString dbgString = itemData->mItems[0].mItemText;
    97         QSharedPointer<NmContactHistoryModelItemData> itemData = mPrivateItemList[i];
   109             dbgString.append(" ");
    98 
   110             dbgString.append(itemData->mItems[1].mItemText);
    99         QString dbgString = itemData->mItems[0].mItemText;
   111 
   100         dbgString.append(" ");
   112             qDebug(dbgString.toLatin1());
   101         dbgString.append(itemData->mItems[1].mItemText);
   113         }
   102 
   114         
   103         qDebug(dbgString.toLatin1());
   115     #endif
   104 
   116 
   105 
       
   106         }
       
   107 
   117 
   108     emit queryCompleted(0);
   118     emit queryCompleted(0);
   109 }
   119 }
   110 
   120 
   111 /*!
   121 /*!
   117     \sa queryDatabases( )
   127     \sa queryDatabases( )
   118 */
   128 */
   119 QList<QContactLocalId> NmContactHistoryModelPrivate::queryContactDatabase(
   129 QList<QContactLocalId> NmContactHistoryModelPrivate::queryContactDatabase(
   120     const QString &query)
   130     const QString &query)
   121 {
   131 {
       
   132     NM_FUNCTION;
       
   133 
   122     // Define filter
   134     // Define filter
   123     QContactDetailFilter df;
   135     QContactDetailFilter df;
   124 
   136 
   125     df.setDetailDefinitionName(QContactEmailAddress::DefinitionName,
   137     df.setDetailDefinitionName(QContactEmailAddress::DefinitionName,
   126                                QContactEmailAddress::FieldEmailAddress );
   138                                QContactEmailAddress::FieldEmailAddress );
   146     \sa queryDatabases( )
   158     \sa queryDatabases( )
   147 */
   159 */
   148 bool NmContactHistoryModelPrivate::queryMruDatabase(
   160 bool NmContactHistoryModelPrivate::queryMruDatabase(
   149     const QString &query)
   161     const QString &query)
   150 {
   162 {
       
   163     NM_FUNCTION;
       
   164     bool rVal = false;
       
   165     
   151     if (mType == EmailAddressModel)
   166     if (mType == EmailAddressModel)
   152     {
   167     {
   153         bool mruListFilled = fillMruMatchList();
   168         bool mruListFilled = fillMruMatchList();
   154 
   169 
   155         if (!mruListFilled)
   170         if (!mruListFilled)
   156         {
   171         {
   157             return false;
   172             return rVal;
   158         }
   173         }
   159 
   174 
   160         QHashIterator<QString, QString> i(mMruList);
   175         QHashIterator<QString, QString> i(mMruList);
   161 
   176 
   162         while (i.hasNext())
   177         while (i.hasNext())
   177             }
   192             }
   178             else if (name.contains(query, Qt::CaseInsensitive))
   193             else if (name.contains(query, Qt::CaseInsensitive))
   179             {
   194             {
   180                 mMruMatches.insert(name, address);
   195                 mMruMatches.insert(name, address);
   181             }
   196             }
   182         }
   197         }      
   183     }
   198         rVal = true;
   184 
   199     }
   185     return true;
   200 
       
   201     return rVal;
   186 }
   202 }
   187 
   203 
   188 /*!
   204 /*!
   189     Internal helper function for queryDatabases()
   205     Internal helper function for queryDatabases()
   190     Not meant to be used alone.
   206     Not meant to be used alone.
   195 */
   211 */
   196 void NmContactHistoryModelPrivate::populateListWithContactItems(
   212 void NmContactHistoryModelPrivate::populateListWithContactItems(
   197     const QList<QContactLocalId> cnt_ids,
   213     const QList<QContactLocalId> cnt_ids,
   198     const QString &modifiedQuery)
   214     const QString &modifiedQuery)
   199 {
   215 {
       
   216     NM_FUNCTION;
       
   217 
   200     int cntCount = cnt_ids.count();
   218     int cntCount = cnt_ids.count();
   201     // Populate mPrivateItemList with contact items.
   219     // Populate mPrivateItemList with contact items.
   202     for ( int cntIndex = 0; cntIndex < cntCount; cntIndex++)
   220     for ( int cntIndex = 0; cntIndex < cntCount; cntIndex++)
   203     {
   221     {
   204         QContactLocalId cid = cnt_ids.at( cntIndex );
   222         QContactLocalId cid = cnt_ids.at( cntIndex );
   286     \sa queryDatabases( )
   304     \sa queryDatabases( )
   287 */
   305 */
   288 void NmContactHistoryModelPrivate::populateListWithMruItems(
   306 void NmContactHistoryModelPrivate::populateListWithMruItems(
   289     const QString &query)
   307     const QString &query)
   290 {
   308 {
   291     QMapIterator<QString, QString> i(mMruMatches);
   309     NM_FUNCTION;
   292 
   310 
   293     while (i.hasNext())
   311     QMapIterator<QString, QString> mruMatch(mMruMatches);
   294     {
   312 
   295         i.next();
   313     while (mruMatch.hasNext())
       
   314     {
       
   315         mruMatch.next();
   296         // For Display name (ex. John Doe)
   316         // For Display name (ex. John Doe)
   297         NmContactHistoryModelSubItem itemSubItem1;
   317         NmContactHistoryModelSubItem itemSubItem1;
   298         // For Email address (ex. john.doe@company.com)
   318         // For Email address (ex. john.doe@company.com)
   299         NmContactHistoryModelSubItem itemSubItem2;
   319         NmContactHistoryModelSubItem itemSubItem2;
   300 
   320 
   301         itemSubItem1.mItemText = i.key();
   321         QString name = mruMatch.key();
   302         itemSubItem2.mItemText = i.value();
   322         QString address = mruMatch.value();
       
   323 
       
   324         itemSubItem1.mItemText = name;
       
   325         itemSubItem2.mItemText = address;
   303 
   326 
   304         // markup for first item.
   327         // markup for first item.
   305         if( i.key().indexOf(query, 0, Qt::CaseInsensitive) == 0)
   328         if( name.indexOf(query, 0, Qt::CaseInsensitive) == 0)
   306         {
   329         {
   307             itemSubItem1.mMatchingRanges.append(0);
   330             itemSubItem1.mMatchingRanges.append(0);
   308             itemSubItem1.mMatchingRanges.append(query.length()-1);
   331             itemSubItem1.mMatchingRanges.append(query.length()-1);
   309         }
   332         }
   310 
   333 
   311         QRegExp rx("[,\\s]", Qt::CaseInsensitive);
   334         QRegExp rx("[,\\s]", Qt::CaseInsensitive);
   312         bool separatorExists = i.key().contains(rx);
   335         bool separatorExists = name.contains(rx);
   313 
   336 
   314         if (separatorExists)
   337         if (separatorExists)
   315         {
   338         {
   316             int indexLN = i.key().indexOf(",", 0, Qt::CaseInsensitive);
   339             int indexLN = name.indexOf(",", 0, Qt::CaseInsensitive);
   317 
   340 
   318             if (indexLN == -1)
   341             if (indexLN == -1)
   319             {
   342             {
   320                 indexLN = i.key().indexOf(" ", 0, Qt::CaseInsensitive);
   343                 indexLN = name.indexOf(" ", 0, Qt::CaseInsensitive);
   321             }
   344             }
   322 
   345 
   323             if (indexLN > 0)
   346             if (indexLN > 0)
   324             {
   347             {
   325                 int matchPos = i.key().indexOf(query, indexLN, Qt::CaseInsensitive);
   348                 int matchPos = name.indexOf(query, indexLN, Qt::CaseInsensitive);
   326 
   349 
   327                 if (matchPos > 0)
   350                 if (matchPos > 0)
   328                 {
   351                 {
   329                     itemSubItem1.mMatchingRanges.append(matchPos);
   352                     itemSubItem1.mMatchingRanges.append(matchPos);
   330                     itemSubItem1.mMatchingRanges.append((matchPos+query.length())-1);
   353                     itemSubItem1.mMatchingRanges.append((matchPos+query.length())-1);
   331                 }
   354                 }
   332             }
   355             }
   333         }
   356         }
   334 
   357 
   335         // markup for second item.
   358         // markup for second item.
   336         if (itemSubItem2.mItemText.indexOf(query, 0, Qt::CaseInsensitive) == 0 )
   359         if (address.indexOf(query, 0, Qt::CaseInsensitive) == 0 )
   337          {
   360          {
   338              itemSubItem2.mMatchingRanges.append(0);
   361              itemSubItem2.mMatchingRanges.append(0);
   339              itemSubItem2.mMatchingRanges.append(query.length()-1);
   362              itemSubItem2.mMatchingRanges.append(query.length()-1);
   340          }
   363          }
   341 
   364 
   361 
   384 
   362     \sa queryDatabases( )
   385     \sa queryDatabases( )
   363 */
   386 */
   364 void NmContactHistoryModelPrivate::refreshDataModel()
   387 void NmContactHistoryModelPrivate::refreshDataModel()
   365 {
   388 {
       
   389     NM_FUNCTION;
       
   390 
   366     mModelItemList.clear();
   391     mModelItemList.clear();
   367 
   392 
   368     for (int cntIndex = 0;
   393     for (int cntIndex = 0;
   369              cntIndex < mPrivateItemList.count();
   394              cntIndex < mPrivateItemList.count();
   370              cntIndex++)
   395              cntIndex++)
   394     mModelReady = true;
   419     mModelReady = true;
   395 
   420 
   396 }
   421 }
   397 
   422 
   398 /*!
   423 /*!
   399     This is called by public class NmContactHistoryModel when rowcount() is requested
   424     This is called by public class NmContactHistoryModel when model row count is requested
   400     Not meant to be used alone.
   425     Not meant to be used alone.
   401 
   426 
   402 */
   427 */
   403 int NmContactHistoryModelPrivate::rowCount(const QModelIndex &parent) const
   428 int NmContactHistoryModelPrivate::modelRowCount(const QModelIndex &parent) const
   404 {
   429 {
       
   430     NM_FUNCTION;
       
   431 
   405     Q_UNUSED(parent);
   432     Q_UNUSED(parent);
   406     return mModelItemList.count();
   433     return mModelItemList.count();
   407 }
   434 }
   408 
   435 
   409 /*!
   436 /*!
       
   437     This is called by public class NmContactHistoryModel when private data count is required
       
   438     ie. When model isn't populated yet but data query is complete.
       
   439     Not meant to be used alone.
       
   440 
       
   441 */
       
   442 int NmContactHistoryModelPrivate::privateDataCount() const
       
   443 {
       
   444     NM_FUNCTION;
       
   445     return mPrivateItemList.count();
       
   446 }
       
   447 
       
   448 /*!
   410     This is called by public class NmContactHistoryModel when data() is requested
   449     This is called by public class NmContactHistoryModel when data() is requested
   411     Not meant to be used alone.
   450     Not meant to be used alone.
   412 
   451 
   413 */
   452 */
   414 QVariant NmContactHistoryModelPrivate::data(const QModelIndex &index, int role) const
   453 QVariant NmContactHistoryModelPrivate::data(const QModelIndex &index, int role) const
   415 {
   454 {
       
   455     NM_FUNCTION;
       
   456 
   416     if ( mModelReady )
   457     if ( mModelReady )
   417     {
   458     {
   418         if (!index.isValid() ||
   459         if (!index.isValid() ||
   419             index.row() >= mModelItemList.count() ||
   460             index.row() >= mModelItemList.count() ||
   420             index.row() < 0)
   461             index.row() < 0)
   421         {
   462         {
   422             return QVariant();
   463             return QVariant();
   423         }
   464         }
   424 
   465         else if (role == Qt::DisplayRole)
   425         if (role == Qt::DisplayRole)
       
   426         {
   466         {
   427             QVariant v;
   467             QVariant v;
   428             NmContactHistoryModelItem i = mModelItemList.at(index.row());
   468             NmContactHistoryModelItem i = mModelItemList.at(index.row());
   429             v.setValue(i);
   469             v.setValue(i);
   430             return v;
   470             return v;
   440 
   480 
   441     \sa queryMruDatabase( )
   481     \sa queryMruDatabase( )
   442 */
   482 */
   443 bool NmContactHistoryModelPrivate::fillMruMatchList()
   483 bool NmContactHistoryModelPrivate::fillMruMatchList()
   444 {
   484 {
   445     //TODO: Possible optimization, remove extra calls EmailMRU(), We need to get list only once.
   485     NM_FUNCTION;
       
   486 
   446     EmailMRU *mru = new EmailMRU();
   487     EmailMRU *mru = new EmailMRU();
       
   488     bool rVal = false;
   447 
   489 
   448     if (mru)
   490     if (mru)
   449     {
   491     {
   450         int addressCount = mru->entryCount();
   492         int addressCount = mru->entryCount();
   451 
   493 
   461                 mMruList.insertMulti(key, value);
   503                 mMruList.insertMulti(key, value);
   462             }
   504             }
   463         }
   505         }
   464 
   506 
   465         delete mru;
   507         delete mru;
   466         return true;
   508         rVal = true;
   467     }
   509     }
   468     else
   510     else
   469     {
   511     {
   470         return false;
   512         rVal = false;
   471     }
   513     }
       
   514 
       
   515     return rVal;
   472 }
   516 }
   473 
   517 
   474 /*!
   518 /*!
   475     This is called from populateListWithContactItems() when correct order of
   519     This is called from populateListWithContactItems() when correct order of
   476     first name, last name setting is required
   520     first name, last name setting is required
   481     \sa populateListWithContactItems( )
   525     \sa populateListWithContactItems( )
   482 
   526 
   483     \return concennated string formatted as contact setting specifies.
   527     \return concennated string formatted as contact setting specifies.
   484 */
   528 */
   485 QString NmContactHistoryModelPrivate::obeyContactOrder(const QString &firstName,
   529 QString NmContactHistoryModelPrivate::obeyContactOrder(const QString &firstName,
   486                                                        const QString &lastName)
   530                                                        const QString &lastName) const
   487 {
   531 {
       
   532     NM_FUNCTION;
       
   533 
   488     QString result;
   534     QString result;
   489 
   535 
   490     switch (mNameOrder)
   536     switch (mNameOrder)
   491     {
   537     {
   492         case LastNameFirstName:
   538         case LastNameFirstName: