emailservices/nmutilities/src/nmcontacthistorymodel_p.cpp
changeset 76 38bf5461e270
parent 74 6c59112cfd31
equal deleted inserted replaced
74:6c59112cfd31 76:38bf5461e270
    52     mModelItemList.clear();
    52     mModelItemList.clear();
    53 }
    53 }
    54 
    54 
    55 /*!
    55 /*!
    56     Searches contact & MRU  databases according to query parameters.
    56     Searches contact & MRU  databases according to query parameters.
    57     Emits QueryCompleted() when ready.
    57     Emits queryCompleted() when ready.
    58 
    58 
    59     \param query Text to be searched
    59     \param query Text to be searched
    60     \return none
    60     \return none
    61 
    61 
    62     Note:: QContactManager supports max. ten characters long queries.
    62     Note:: QContactManager supports max. ten characters long queries.
    79     // with QContactManager API
    79     // with QContactManager API
    80     QString modifiedQuery = query;
    80     QString modifiedQuery = query;
    81 
    81 
    82     int spcPosition = query.indexOf(" ");
    82     int spcPosition = query.indexOf(" ");
    83 
    83 
    84     if ( spcPosition != -1 )
    84     if ( spcPosition != -1 ) {
    85     {
       
    86         modifiedQuery = query.left(spcPosition);
    85         modifiedQuery = query.left(spcPosition);
    87     }
    86     }
    88 
    87 
    89     // Get matching MRU items
    88     // Get matching MRU items
    90     queryMruDatabase(modifiedQuery);
    89     queryMruDatabase(modifiedQuery);
    97 
    96 
    98     // Populate mPrivateItemList with contact items.
    97     // Populate mPrivateItemList with contact items.
    99     populateListWithContactItems(cnt_ids, modifiedQuery);
    98     populateListWithContactItems(cnt_ids, modifiedQuery);
   100 
    99 
   101 
   100 
   102     #ifdef _DEBUG
       
   103     
       
   104         for (int i = 0; i < mPrivateItemList.size(); i++)
       
   105         {
       
   106             QSharedPointer<NmContactHistoryModelItemData> itemData = mPrivateItemList[i];
       
   107 
       
   108             QString dbgString = itemData->mItems[0].mItemText;
       
   109             dbgString.append(" ");
       
   110             dbgString.append(itemData->mItems[1].mItemText);
       
   111 
       
   112             qDebug(dbgString.toLatin1());
       
   113         }
       
   114         
       
   115     #endif
       
   116 
       
   117 
       
   118     emit queryCompleted(0);
   101     emit queryCompleted(0);
   119 }
   102 }
   120 
   103 
   121 /*!
   104 /*!
   122     Internal helper function for queryDatabases()
   105     Internal helper function for queryDatabases()
   161     const QString &query)
   144     const QString &query)
   162 {
   145 {
   163     NM_FUNCTION;
   146     NM_FUNCTION;
   164     bool rVal = false;
   147     bool rVal = false;
   165     
   148     
   166     if (mType == EmailAddressModel)
   149     if (mType == EmailAddressModel) {
   167     {
       
   168         bool mruListFilled = fillMruMatchList();
   150         bool mruListFilled = fillMruMatchList();
   169 
   151 
   170         if (!mruListFilled)
   152         if (!mruListFilled) {
   171         {
       
   172             return rVal;
   153             return rVal;
   173         }
   154         }
   174 
   155 
   175         QHashIterator<QString, QString> i(mMruList);
   156         QHashIterator<QString, QString> i(mMruList);
   176 
   157 
   177         while (i.hasNext())
   158         while (i.hasNext()) {
   178         {
       
   179             i.next();
   159             i.next();
   180 
   160 
   181             QString address = i.key();
   161             QString address = i.key();
   182             QString name = i.value();
   162             QString name = i.value();
   183 
   163 
   184             if ((name.length() == 0) && (address.contains('@')))
   164             if ((name.length() == 0) && (address.contains('@'))) {
   185             {
       
   186                 name = address.left(address.indexOf("@"));
   165                 name = address.left(address.indexOf("@"));
   187             }
   166             }
   188 
   167 
   189             if (address.contains(query, Qt::CaseInsensitive))
   168             if (address.contains(query, Qt::CaseInsensitive)) {
   190             {
       
   191                 mMruMatches.insert(name, address);
   169                 mMruMatches.insert(name, address);
   192             }
   170             }
   193             else if (name.contains(query, Qt::CaseInsensitive))
   171             else if (name.contains(query, Qt::CaseInsensitive)) {
   194             {
       
   195                 mMruMatches.insert(name, address);
   172                 mMruMatches.insert(name, address);
   196             }
   173             }
   197         }      
   174         }      
   198         rVal = true;
   175         rVal = true;
   199     }
   176     }
   215 {
   192 {
   216     NM_FUNCTION;
   193     NM_FUNCTION;
   217 
   194 
   218     int cntCount = cnt_ids.count();
   195     int cntCount = cnt_ids.count();
   219     // Populate mPrivateItemList with contact items.
   196     // Populate mPrivateItemList with contact items.
   220     for ( int cntIndex = 0; cntIndex < cntCount; cntIndex++)
   197     for ( int cntIndex = 0; cntIndex < cntCount; cntIndex++) {
   221     {
       
   222         QContactLocalId cid = cnt_ids.at( cntIndex );
   198         QContactLocalId cid = cnt_ids.at( cntIndex );
   223         // Fetch extended contact information from DB by ID
   199         // Fetch extended contact information from DB by ID
   224         QContact contact = mContactManager->contact( cid );
   200         QContact contact = mContactManager->contact( cid );
   225 
   201 
   226         // Get number of associated emails for contact
   202         // Get number of associated emails for contact
   227         int numberOfEmails =  contact.details(
   203         int numberOfEmails =  contact.details(
   228             QContactEmailAddress::DefinitionName).count();
   204             QContactEmailAddress::DefinitionName).count();
   229 
   205 
   230         if (numberOfEmails == 0)
   206         if (numberOfEmails == 0) {
   231         {
   207             continue; 
   232             continue; // Scotty, I need warp speed.
       
   233         }
   208         }
   234 
   209 
   235         // Get contact name details
   210         // Get contact name details
   236         QContactName contactName = contact.detail(
   211         QContactName contactName = contact.detail(
   237             QContactName::DefinitionName);
   212             QContactName::DefinitionName);
   239         QString firstName = contactName.value(
   214         QString firstName = contactName.value(
   240             QContactName::FieldFirstName);
   215             QContactName::FieldFirstName);
   241         QString lastName = contactName.value(
   216         QString lastName = contactName.value(
   242             QContactName::FieldLastName);
   217             QContactName::FieldLastName);
   243 
   218 
   244         for (int emailIndex = 0; emailIndex < numberOfEmails; emailIndex++)
   219         for (int emailIndex = 0; emailIndex < numberOfEmails; emailIndex++) {
   245         {
       
   246             // For Display name (ex. John Doe)
   220             // For Display name (ex. John Doe)
   247             NmContactHistoryModelSubItem itemSubItem1;
   221             NmContactHistoryModelSubItem itemSubItem1;
   248             // For Email address (ex. john.doe@company.com)
   222             // For Email address (ex. john.doe@company.com)
   249             NmContactHistoryModelSubItem itemSubItem2;
   223             NmContactHistoryModelSubItem itemSubItem2;
   250 
   224 
   251             int queryLength = modifiedQuery.length();
   225             int queryLength = modifiedQuery.length();
   252 
   226 
   253             // Obey the contacts setting (first name last name order)
   227             // Obey the contacts setting (first name last name order)
   254             itemSubItem1.mItemText = obeyContactOrder(firstName, lastName);
   228             itemSubItem1.mItemText = obeyContactOrder(firstName, lastName);
   255 
   229 
   256             if( firstName.indexOf(modifiedQuery, 0, Qt::CaseInsensitive) == 0)
   230             if( firstName.indexOf(modifiedQuery, 0, Qt::CaseInsensitive) == 0) {
   257             {
       
   258                 itemSubItem1.mMatchingRanges.append(0);
   231                 itemSubItem1.mMatchingRanges.append(0);
   259                 itemSubItem1.mMatchingRanges.append(queryLength-1);
   232                 itemSubItem1.mMatchingRanges.append(queryLength-1);
   260             }
   233             }
   261 
   234 
   262             if( lastName.indexOf(modifiedQuery, 0, Qt::CaseInsensitive) == 0)
   235             if( lastName.indexOf(modifiedQuery, 0, Qt::CaseInsensitive) == 0) {
   263             {
       
   264                 int indexLN = itemSubItem1.mItemText.indexOf(lastName,
   236                 int indexLN = itemSubItem1.mItemText.indexOf(lastName,
   265                     firstName.length(),
   237                     firstName.length(),
   266                     Qt::CaseInsensitive );
   238                     Qt::CaseInsensitive );
   267 
   239 
   268                 itemSubItem1.mMatchingRanges.append(indexLN);
   240                 itemSubItem1.mMatchingRanges.append(indexLN);
   272             itemSubItem2.mItemText = contact.details (
   244             itemSubItem2.mItemText = contact.details (
   273                  QContactEmailAddress::DefinitionName )[emailIndex].value (
   245                  QContactEmailAddress::DefinitionName )[emailIndex].value (
   274                  QContactEmailAddress::FieldEmailAddress );
   246                  QContactEmailAddress::FieldEmailAddress );
   275 
   247 
   276             if (itemSubItem2.mItemText.indexOf(modifiedQuery, 0,
   248             if (itemSubItem2.mItemText.indexOf(modifiedQuery, 0,
   277                 Qt::CaseInsensitive) == 0 )
   249                 Qt::CaseInsensitive) == 0 ) {
   278             {
       
   279                 itemSubItem2.mMatchingRanges.append(0);
   250                 itemSubItem2.mMatchingRanges.append(0);
   280                 itemSubItem2.mMatchingRanges.append(queryLength-1);
   251                 itemSubItem2.mMatchingRanges.append(queryLength-1);
   281             }
   252             }
   282 
   253 
   283             if ((itemSubItem1.mMatchingRanges.count() != 0) ||
   254             if ((itemSubItem1.mMatchingRanges.count() != 0) ||
   284                 (itemSubItem2.mMatchingRanges.count() != 0) )
   255                 (itemSubItem2.mMatchingRanges.count() != 0) ) {
   285             {
       
   286                 QSharedPointer<NmContactHistoryModelItemData> itemData
   256                 QSharedPointer<NmContactHistoryModelItemData> itemData
   287                     (new NmContactHistoryModelItemData);
   257                     (new NmContactHistoryModelItemData);
   288 
   258 
   289                 itemData->mContactId = cid;
   259                 itemData->mContactId = cid;
   290                 itemData->mItems.append(itemSubItem1);
   260                 itemData->mItems.append(itemSubItem1);
   308 {
   278 {
   309     NM_FUNCTION;
   279     NM_FUNCTION;
   310 
   280 
   311     QMapIterator<QString, QString> mruMatch(mMruMatches);
   281     QMapIterator<QString, QString> mruMatch(mMruMatches);
   312 
   282 
   313     while (mruMatch.hasNext())
   283     while (mruMatch.hasNext()) {
   314     {
       
   315         mruMatch.next();
   284         mruMatch.next();
   316         // For Display name (ex. John Doe)
   285         // For Display name (ex. John Doe)
   317         NmContactHistoryModelSubItem itemSubItem1;
   286         NmContactHistoryModelSubItem itemSubItem1;
   318         // For Email address (ex. john.doe@company.com)
   287         // For Email address (ex. john.doe@company.com)
   319         NmContactHistoryModelSubItem itemSubItem2;
   288         NmContactHistoryModelSubItem itemSubItem2;
   323 
   292 
   324         itemSubItem1.mItemText = name;
   293         itemSubItem1.mItemText = name;
   325         itemSubItem2.mItemText = address;
   294         itemSubItem2.mItemText = address;
   326 
   295 
   327         // markup for first item.
   296         // markup for first item.
   328         if( name.indexOf(query, 0, Qt::CaseInsensitive) == 0)
   297         if( name.indexOf(query, 0, Qt::CaseInsensitive) == 0) {
   329         {
       
   330             itemSubItem1.mMatchingRanges.append(0);
   298             itemSubItem1.mMatchingRanges.append(0);
   331             itemSubItem1.mMatchingRanges.append(query.length()-1);
   299             itemSubItem1.mMatchingRanges.append(query.length()-1);
   332         }
   300         }
   333 
   301 
   334         QRegExp rx("[,\\s]", Qt::CaseInsensitive);
   302         QRegExp rx("[,\\s]", Qt::CaseInsensitive);
   335         bool separatorExists = name.contains(rx);
   303         bool separatorExists = name.contains(rx);
   336 
   304 
   337         if (separatorExists)
   305         if (separatorExists) {
   338         {
       
   339             int indexLN = name.indexOf(",", 0, Qt::CaseInsensitive);
   306             int indexLN = name.indexOf(",", 0, Qt::CaseInsensitive);
   340 
   307 
   341             if (indexLN == -1)
   308             if (indexLN == -1) {
   342             {
       
   343                 indexLN = name.indexOf(" ", 0, Qt::CaseInsensitive);
   309                 indexLN = name.indexOf(" ", 0, Qt::CaseInsensitive);
   344             }
   310             }
   345 
   311 
   346             if (indexLN > 0)
   312             if (indexLN > 0) {
   347             {
       
   348                 int matchPos = name.indexOf(query, indexLN, Qt::CaseInsensitive);
   313                 int matchPos = name.indexOf(query, indexLN, Qt::CaseInsensitive);
   349 
   314 
   350                 if (matchPos > 0)
   315                 if (matchPos > 0) {
   351                 {
       
   352                     itemSubItem1.mMatchingRanges.append(matchPos);
   316                     itemSubItem1.mMatchingRanges.append(matchPos);
   353                     itemSubItem1.mMatchingRanges.append((matchPos+query.length())-1);
   317                     itemSubItem1.mMatchingRanges.append((matchPos+query.length())-1);
   354                 }
   318                 }
   355             }
   319             }
   356         }
   320         }
   357 
   321 
   358         // markup for second item.
   322         // markup for second item.
   359         if (address.indexOf(query, 0, Qt::CaseInsensitive) == 0 )
   323         if (address.indexOf(query, 0, Qt::CaseInsensitive) == 0 ) {
   360          {
       
   361              itemSubItem2.mMatchingRanges.append(0);
   324              itemSubItem2.mMatchingRanges.append(0);
   362              itemSubItem2.mMatchingRanges.append(query.length()-1);
   325              itemSubItem2.mMatchingRanges.append(query.length()-1);
   363          }
   326         }
   364 
   327 
   365         // If match, add to data list.
   328         // If match, add to data list.
   366         if ((itemSubItem1.mMatchingRanges.count() != 0) ||
   329         if ((itemSubItem1.mMatchingRanges.count() != 0) ||
   367             (itemSubItem2.mMatchingRanges.count() != 0) )
   330             (itemSubItem2.mMatchingRanges.count() != 0) ) {
   368         {
       
   369             QSharedPointer<NmContactHistoryModelItemData> itemData
   331             QSharedPointer<NmContactHistoryModelItemData> itemData
   370                 (new NmContactHistoryModelItemData);
   332                 (new NmContactHistoryModelItemData);
   371 
   333 
   372             itemData->mContactId = 0; // No ID available, use 0.
   334             itemData->mContactId = 0; // No ID available, use 0.
   373             itemData->mItems.append(itemSubItem1);
   335             itemData->mItems.append(itemSubItem1);
   390 
   352 
   391     mModelItemList.clear();
   353     mModelItemList.clear();
   392 
   354 
   393     for (int cntIndex = 0;
   355     for (int cntIndex = 0;
   394              cntIndex < mPrivateItemList.count();
   356              cntIndex < mPrivateItemList.count();
   395              cntIndex++)
   357              cntIndex++) {
   396     {
       
   397         // Construct modelItem to be added to list.
   358         // Construct modelItem to be added to list.
   398         NmContactHistoryModelItem modelItem;
   359         NmContactHistoryModelItem modelItem;
   399 
   360 
   400         QSharedPointer<NmContactHistoryModelItemData> itemData(
   361         QSharedPointer<NmContactHistoryModelItemData> itemData(
   401             mPrivateItemList[cntIndex]);
   362             mPrivateItemList[cntIndex]);
   403         int id = itemData->mContactId;
   364         int id = itemData->mContactId;
   404         modelItem.setContactId(itemData->mContactId);
   365         modelItem.setContactId(itemData->mContactId);
   405 
   366 
   406         for (int itemIndex = 0;
   367         for (int itemIndex = 0;
   407                  itemIndex < itemData->mItems.count();
   368                  itemIndex < itemData->mItems.count();
   408                  itemIndex++)
   369                  itemIndex++) {
   409         {
       
   410             QString key = itemData->mItems[itemIndex].mItemText;
   370             QString key = itemData->mItems[itemIndex].mItemText;
   411             modelItem.appendSubItem(itemData->mItems[itemIndex]);
   371             modelItem.appendSubItem(itemData->mItems[itemIndex]);
   412         }
   372         }
   413 
   373 
   414         // Append modelItem to list.
   374         // Append modelItem to list.
   451 
   411 
   452 */
   412 */
   453 QVariant NmContactHistoryModelPrivate::data(const QModelIndex &index, int role) const
   413 QVariant NmContactHistoryModelPrivate::data(const QModelIndex &index, int role) const
   454 {
   414 {
   455     NM_FUNCTION;
   415     NM_FUNCTION;
   456 
   416     QVariant rVariant = QVariant(); 
   457     if ( mModelReady )
   417     
   458     {
   418     if ( mModelReady ) {
   459         if (!index.isValid() ||
   419         if (!index.isValid() ||
   460             index.row() >= mModelItemList.count() ||
   420             index.row() >= mModelItemList.count() ||
   461             index.row() < 0)
   421             index.row() < 0) {
   462         {
   422             rVariant = QVariant();
   463             return QVariant();
   423         }
   464         }
   424         else if (role == Qt::DisplayRole) {
   465         else if (role == Qt::DisplayRole)
       
   466         {
       
   467             QVariant v;
       
   468             NmContactHistoryModelItem i = mModelItemList.at(index.row());
   425             NmContactHistoryModelItem i = mModelItemList.at(index.row());
   469             v.setValue(i);
   426             rVariant.setValue(i);
   470             return v;
   427         }
   471         }
   428     }
   472     }
   429 
   473 
   430     return rVariant;
   474     return QVariant();
       
   475 }
   431 }
   476 
   432 
   477 /*!
   433 /*!
   478     Fills MruMatchList, called from queryMruDatabase.
   434     Fills MruMatchList, called from queryMruDatabase.
   479     Function uses Email MRU API to fetch information from CenRep
   435     Function uses Email MRU API to fetch information from CenRep
   484 {
   440 {
   485     NM_FUNCTION;
   441     NM_FUNCTION;
   486 
   442 
   487     EmailMRU *mru = new EmailMRU();
   443     EmailMRU *mru = new EmailMRU();
   488     bool rVal = false;
   444     bool rVal = false;
   489 
   445     int updateCount = 0;
   490     if (mru)
   446 
   491     {
   447     int addressCount = mru->entryCount();
   492         int addressCount = mru->entryCount();
   448 
   493 
   449     for (int i = addressCount; i > 0; i--) {
   494         for (int i = addressCount; i > 0; i--)
   450         QString key, value; // "Email or Number" & "Display Name"
   495         {
   451         bool success;
   496             QString key, value; // "Email or Number" & "Display Name"
   452 
   497             bool success;
   453         success = mru->getEntry(i, value, key);
   498 
   454 
   499             success = mru->getEntry(i, value, key);
   455         if (success) {
   500 
   456             mMruList.insertMulti(key, value);
   501             if (success)
   457             updateCount++;
   502             {
   458         }
   503                 mMruList.insertMulti(key, value);
   459     }
   504             }
   460 
   505         }
   461     delete mru;
   506 
   462     
   507         delete mru;
   463     if (updateCount == addressCount) {
   508         rVal = true;
   464         rVal = true;
   509     }
   465     } 
   510     else
       
   511     {
       
   512         rVal = false;
       
   513     }
       
   514 
   466 
   515     return rVal;
   467     return rVal;
   516 }
   468 }
   517 
   469 
   518 /*!
   470 /*!
   531 {
   483 {
   532     NM_FUNCTION;
   484     NM_FUNCTION;
   533 
   485 
   534     QString result;
   486     QString result;
   535 
   487 
   536     switch (mNameOrder)
   488     switch (mNameOrder) {
   537     {
   489     case LastNameFirstName:
   538         case LastNameFirstName:
   490         result = lastName + " " + firstName;
   539             result = lastName + " " + firstName;
       
   540         break;
   491         break;
   541 
   492 
   542         case LastNameCommaFirstName:
   493     case LastNameCommaFirstName:
   543             result = lastName + ", " + firstName;
   494         result = lastName + ", " + firstName;
   544         break;
   495         break;
   545 
   496 
   546         case FirstNameLastName:
   497     case FirstNameLastName:
   547             result = firstName + " " + lastName;
   498         result = firstName + " " + lastName;
   548         break;
   499         break;
   549 
   500 
   550         default:
   501     default:
   551             result = firstName + " " + lastName;
   502         result = firstName + " " + lastName;
   552         break;
   503         break;
   553     }
   504     }
   554 
   505 
   555     return result;
   506     return result;
   556 }
   507 }