phonebookui/cntcommonui/collections/cntcollectionlistmodelworker.cpp
changeset 72 6abfb1094884
child 81 640d30f4fb64
equal deleted inserted replaced
67:59984e68247d 72:6abfb1094884
       
     1 /*
       
     2 * Copyright (c) 2009 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:
       
    15 *
       
    16 */
       
    17 
       
    18 #include "cntcollectionlistmodelworker.h"
       
    19 #include "cntfavourite.h"
       
    20 #include "cntdebug.h"
       
    21 
       
    22 #include <hbglobal.h>
       
    23 #include <hbapplication.h>
       
    24 
       
    25 #include <cntuids.h>
       
    26 #include <xqsettingskey.h>
       
    27 
       
    28 // CONSTANTS
       
    29 const int CntNamesLengthLimit = 30;
       
    30 
       
    31 /*!
       
    32     Constructor
       
    33 */
       
    34 CntCollectionListModelWorker::CntCollectionListModelWorker(QString unnamed, QString noFavs, QString noMembers, int sortOrder) :
       
    35     mStarted(false),
       
    36     mProcessingJobs(false),
       
    37     mStopped(false),
       
    38     mManager(NULL),
       
    39     mUnnamed(unnamed),
       
    40     mNoFavorites(noFavs),
       
    41     mNoMembers(noMembers),
       
    42     mSortOrder(sortOrder)
       
    43 {
       
    44     CNT_ENTRY
       
    45     
       
    46     moveToThread(this);
       
    47     
       
    48     CNT_EXIT
       
    49 }
       
    50 
       
    51 /*!
       
    52     Destructor
       
    53     
       
    54     Clears any remaining jobs and waits until the last one gets completed.
       
    55 */
       
    56 CntCollectionListModelWorker::~CntCollectionListModelWorker()
       
    57 {
       
    58     CNT_ENTRY
       
    59     
       
    60     mMutex.lock();
       
    61     
       
    62     mJobs.clear();
       
    63     
       
    64     mMutex.unlock();
       
    65 
       
    66     exit();
       
    67     
       
    68     // Ugly as hell, but needed for UTs or otherwise they'll freeze
       
    69 #ifndef PBK_UNIT_TEST
       
    70     wait();
       
    71 #else
       
    72     wait(5000);
       
    73 #endif
       
    74 
       
    75     CNT_EXIT
       
    76 }
       
    77 
       
    78 /*!
       
    79     Re-implemented from QThread, start the event loop.
       
    80 */
       
    81 void CntCollectionListModelWorker::run()
       
    82 {
       
    83     CNT_ENTRY
       
    84 
       
    85     exec();
       
    86     
       
    87     delete mManager;
       
    88     mManager = NULL;
       
    89     
       
    90     CNT_EXIT
       
    91 }
       
    92 
       
    93 /*!
       
    94     Re-implemented from QThread to capture QEvent::User type events
       
    95     in order to start the job processing loop.
       
    96 
       
    97     \param event the event sent to this object
       
    98     \return bool
       
    99 */
       
   100 bool CntCollectionListModelWorker::event(QEvent *event)
       
   101 {
       
   102     if (event->type() == QEvent::User)
       
   103     {
       
   104         processJobs();
       
   105         return true;
       
   106     }
       
   107 
       
   108     return QThread::event(event);
       
   109 }
       
   110 
       
   111 /*!
       
   112     Schedule an asynch job for the given contact id.
       
   113 
       
   114     \param id the QContactLocalId of the group
       
   115 */
       
   116 void CntCollectionListModelWorker::scheduleJob(int id)
       
   117 {
       
   118     CNT_ENTRY
       
   119     
       
   120     mMutex.lock();
       
   121 
       
   122     if (!mStarted) {
       
   123         // starting the event loop; minimum priority is used as this thread
       
   124         // should interfere as little as possible with more time-critical tasks,
       
   125         // like updating the UI during scrolling
       
   126         start(QThread::IdlePriority);
       
   127         mStarted = true;
       
   128     }
       
   129 
       
   130     if (!mProcessingJobs) {
       
   131         // new job => start processing jobs
       
   132         mProcessingJobs = true;
       
   133         HbApplication::instance()->postEvent(this, new QEvent(QEvent::User));
       
   134     }
       
   135 
       
   136     mJobs.append(id);
       
   137     
       
   138     mMutex.unlock();
       
   139     
       
   140     CNT_EXIT
       
   141 }
       
   142 
       
   143 /*!
       
   144     Job processing loop. Loops forever until there are no more jobs left.
       
   145     If jobs exist, takes the first one from the list and processes it.
       
   146 */
       
   147 void CntCollectionListModelWorker::processJobs()
       
   148 {
       
   149     CNT_ENTRY
       
   150     
       
   151     forever
       
   152     {
       
   153         mMutex.lock();
       
   154         int jobCount = mJobs.count();
       
   155 
       
   156         if (jobCount == 0)
       
   157         {
       
   158             mProcessingJobs = false;
       
   159             mMutex.unlock();
       
   160             break;
       
   161         }
       
   162         else
       
   163         {
       
   164             // get next job
       
   165             int id = mJobs.takeFirst();
       
   166 
       
   167             mMutex.unlock();
       
   168             fetchInformation(id);
       
   169         }
       
   170 
       
   171         HbApplication::processEvents();
       
   172     }
       
   173     
       
   174     CNT_EXIT
       
   175 }
       
   176 
       
   177 /*!
       
   178     Fetch the second row text (member names or placeholder text if no members exist)
       
   179     and the amount of contacts in the group. Emits the result as a signal for
       
   180     CntCollectionListModel to handle.
       
   181 
       
   182     \param id the QContactLocalId of the group in the given row
       
   183 */
       
   184 void CntCollectionListModelWorker::fetchInformation(int id)
       
   185 {
       
   186     CNT_ENTRY
       
   187     
       
   188     if (!mManager)
       
   189     {
       
   190         mManager = new QContactManager("symbian");
       
   191     }
       
   192     
       
   193     QContact contact = mManager->contact(id);
       
   194 
       
   195     int favoriteGroupId = CntFavourite::favouriteGroupId( mManager );
       
   196 
       
   197     QString secondLineText;
       
   198 
       
   199     QContactRelationshipFilter rFilter;
       
   200     rFilter.setRelationshipType(QContactRelationship::HasMember);
       
   201     rFilter.setRelatedContactRole(QContactRelationship::First);
       
   202     rFilter.setRelatedContactId(contact.id());
       
   203 
       
   204     QContactSortOrder sortOrderFirstName;
       
   205     sortOrderFirstName.setDetailDefinitionName(QContactName::DefinitionName,
       
   206             QContactName::FieldFirstName);
       
   207     sortOrderFirstName.setCaseSensitivity(Qt::CaseInsensitive);
       
   208 
       
   209     QContactSortOrder sortOrderLastName;
       
   210     sortOrderLastName.setDetailDefinitionName(QContactName::DefinitionName,
       
   211             QContactName::FieldLastName);
       
   212     sortOrderLastName.setCaseSensitivity(Qt::CaseInsensitive);
       
   213     
       
   214     QList<QContactSortOrder> sortOrders;
       
   215 
       
   216     if (mSortOrder == CntOrderFirstLast)
       
   217     {
       
   218         sortOrders.append(sortOrderFirstName);
       
   219         sortOrders.append(sortOrderLastName);
       
   220     } 
       
   221     else
       
   222     {
       
   223         sortOrders.append(sortOrderLastName);
       
   224         sortOrders.append(sortOrderFirstName);
       
   225     }
       
   226 
       
   227     // group members and their count
       
   228     QList<QContactLocalId> groupMemberIds = mManager->contactIds(rFilter, sortOrders);
       
   229 
       
   230     if (!groupMemberIds.isEmpty())
       
   231     {
       
   232         QStringList nameList;
       
   233         for(int i = 0;i < groupMemberIds.count();i++)
       
   234         {
       
   235             QContactFetchHint nameOnlyFetchHint;
       
   236             /*QStringList details;
       
   237             details << QContactDisplayLabel::DefinitionName;
       
   238             nameOnlyFetchHint.setDetailDefinitionsHint(details);*/
       
   239             nameOnlyFetchHint.setOptimizationHints(QContactFetchHint::NoRelationships);
       
   240 
       
   241             QContact contact = mManager->contact(groupMemberIds.at(i), nameOnlyFetchHint);
       
   242             QString memberName = contact.displayLabel();
       
   243             if (memberName.isEmpty())
       
   244             {
       
   245                 memberName = mUnnamed;
       
   246             }
       
   247 
       
   248             // Incase the format is LastnameCommaFirstname, remove the commas
       
   249             memberName = memberName.replace(", ", " ");
       
   250             nameList << memberName;
       
   251             if (nameList.join(", ").length() > CntNamesLengthLimit)
       
   252             {
       
   253                 break;
       
   254             }
       
   255         }
       
   256         QString names = nameList.join(", ").trimmed();
       
   257 
       
   258         secondLineText = names;
       
   259     }
       
   260     else
       
   261     {
       
   262         if (id == favoriteGroupId)
       
   263         {
       
   264             secondLineText = mNoFavorites;
       
   265         }
       
   266         else
       
   267         {
       
   268             secondLineText = mNoMembers;
       
   269         }
       
   270     }
       
   271 
       
   272     emit fetchDone(id, secondLineText, groupMemberIds);
       
   273     
       
   274     CNT_EXIT
       
   275 }
       
   276