logsui/logsengine/src/logsmodel.cpp
changeset 0 4a5361db8937
child 2 7119b73b84d6
equal deleted inserted replaced
-1:000000000000 0:4a5361db8937
       
     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 "logsmodel.h"
       
    19 #include "logsdbconnector.h"
       
    20 #include "logsevent.h"
       
    21 #include "logslogger.h"
       
    22 #include "logscall.h"
       
    23 #include "logscontact.h"
       
    24 #include "logsengdefs.h"
       
    25 #include "logsdetailsmodel.h"
       
    26 #include "logsmatchesmodel.h"
       
    27 #include "logsmessage.h"
       
    28 #include "logseventdata.h"
       
    29 #include "logscommondata.h"
       
    30 #include <hbicon.h>
       
    31 #include <QStringList>
       
    32 
       
    33 Q_DECLARE_METATYPE(LogsEvent *)
       
    34 Q_DECLARE_METATYPE(LogsCall *)
       
    35 Q_DECLARE_METATYPE(LogsDetailsModel *)
       
    36 Q_DECLARE_METATYPE(LogsMessage *)
       
    37 Q_DECLARE_METATYPE(LogsContact *)
       
    38 Q_DECLARE_METATYPE(LogsMatchesModel *)
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 LogsModel::LogsModel(LogsModelType modelType, bool resourceControl) : 
       
    45     LogsAbstractModel(), mModelType(modelType)
       
    46 {
       
    47     LOGS_QDEBUG( "logs [ENG] -> LogsModel::LogsModel()" )
       
    48     
       
    49     initIcons();
       
    50     
       
    51     bool allEvents( mModelType == LogsFullModel );
       
    52     mDbConnector = new LogsDbConnector( mEvents, allEvents, resourceControl );
       
    53     connect( mDbConnector, SIGNAL( dataAdded(QList<int>) ), 
       
    54             this, SLOT( dataAdded(QList<int>) ));
       
    55     connect( mDbConnector, SIGNAL( dataUpdated(QList<int>) ), 
       
    56             this, SLOT( dataUpdated(QList<int>) ));
       
    57     connect( mDbConnector, SIGNAL( dataRemoved(QList<int>) ), 
       
    58             this, SLOT( dataRemoved(QList<int>) ));
       
    59     mDbConnector->init();
       
    60     mDbConnector->start();
       
    61     
       
    62     LOGS_QDEBUG( "logs [ENG] <- LogsModel::LogsModel()" )
       
    63 }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 //
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 LogsModel::~LogsModel()
       
    70 {
       
    71     LOGS_QDEBUG( "logs [ENG] -> LogsModel::~LogsModel()" )
       
    72     
       
    73     delete mDbConnector;
       
    74     
       
    75     LogsCommonData::freeCommonData();
       
    76     
       
    77     LOGS_QDEBUG( "logs [ENG] <- LogsModel::~LogsModel()" )
       
    78 }
       
    79 
       
    80 // -----------------------------------------------------------------------------
       
    81 //
       
    82 // -----------------------------------------------------------------------------
       
    83 //
       
    84 bool LogsModel::clearList(LogsModel::ClearType cleartype)
       
    85 {
       
    86     LOGS_QDEBUG( "logs [ENG] -> LogsModel::clearList()" )
       
    87     
       
    88     connect( mDbConnector, SIGNAL(clearingCompleted(int)), 
       
    89          this, SIGNAL(clearingCompleted(int)) );
       
    90     return mDbConnector->clearList(cleartype);
       
    91 }
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 // -----------------------------------------------------------------------------
       
    96 //
       
    97 bool LogsModel::markEventsSeen(LogsModel::ClearType cleartype)
       
    98 {
       
    99     LOGS_QDEBUG( "logs [ENG] -> LogsModel::markEventsSeen()" )
       
   100 
       
   101     QList<int> markedEvents;
       
   102     foreach ( LogsEvent* event, mEvents ){
       
   103         if ( matchEventWithClearType(*event, cleartype) && !event->isSeenLocally() ){
       
   104             markedEvents.append(event->logId());
       
   105         }
       
   106     }
       
   107     connect( mDbConnector, SIGNAL(markingCompleted(int)), 
       
   108              this, SIGNAL(markingCompleted(int)) );
       
   109     bool retVal = mDbConnector->markEventsSeen(markedEvents);
       
   110     LOGS_QDEBUG_2( "logs [ENG] <- LogsModel::markEventsSeen()", retVal )
       
   111     return retVal;
       
   112 }
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 //
       
   116 // -----------------------------------------------------------------------------
       
   117 //
       
   118 int LogsModel::clearMissedCallsCounter()
       
   119 {
       
   120     LOGS_QDEBUG( "logs [ENG] -> LogsModel::clearMissedCallsCounter()" )
       
   121     int err = mDbConnector->clearMissedCallsCounter();
       
   122     LOGS_QDEBUG_2( "logs [ENG] <- LogsModel::clearMissedCallsCounter(), err", err )
       
   123     return err;
       
   124 }
       
   125 
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 // -----------------------------------------------------------------------------
       
   129 //
       
   130 int LogsModel::refreshData()
       
   131 {
       
   132     LOGS_QDEBUG( "logs [ENG] -> LogsModel::refreshData()" )
       
   133     int err = mDbConnector->refreshData();
       
   134     LOGS_QDEBUG_2( "logs [ENG] <- LogsModel::refreshData(), err", err )
       
   135     return err;
       
   136 }
       
   137 
       
   138 // -----------------------------------------------------------------------------
       
   139 //
       
   140 // -----------------------------------------------------------------------------
       
   141 //
       
   142 int LogsModel::compressData()
       
   143 {
       
   144     LOGS_QDEBUG( "logs [ENG] -> LogsModel::compressData()" )
       
   145     int err = mDbConnector->compressData();
       
   146     LOGS_QDEBUG_2( "logs [ENG] <- LogsModel::compressData(), err", err )
       
   147     return err;
       
   148 }
       
   149 
       
   150 // -----------------------------------------------------------------------------
       
   151 //
       
   152 // -----------------------------------------------------------------------------
       
   153 //
       
   154 int LogsModel::predictiveSearchStatus()
       
   155 {
       
   156     return mDbConnector->predictiveSearchStatus();
       
   157 }
       
   158 
       
   159 // -----------------------------------------------------------------------------
       
   160 //
       
   161 // -----------------------------------------------------------------------------
       
   162 //
       
   163 int LogsModel::setPredictiveSearch(bool enabled)
       
   164 {
       
   165     return mDbConnector->setPredictiveSearch(enabled);
       
   166 }
       
   167 
       
   168 
       
   169 // -----------------------------------------------------------------------------
       
   170 // From QAbstractListModel
       
   171 // -----------------------------------------------------------------------------
       
   172 //
       
   173 int LogsModel::rowCount(const QModelIndex & /* parent */) const
       
   174 {
       
   175     return mEvents.count();
       
   176 }
       
   177 
       
   178 // -----------------------------------------------------------------------------
       
   179 // From QAbstractItemModel
       
   180 // -----------------------------------------------------------------------------
       
   181 //
       
   182 QVariant LogsModel::data(const QModelIndex &index, int role) const
       
   183 {
       
   184     if (!index.isValid() || 
       
   185          index.row() >= mEvents.count() || 
       
   186          index.row() < 0 ) {
       
   187         return QVariant();
       
   188     }
       
   189 
       
   190     LogsEvent* event = mEvents.at(index.row());
       
   191     if (role == Qt::DisplayRole){
       
   192         QStringList list;
       
   193         list << getCallerId( *event );
       
   194         list << event->time().toTimeSpec(Qt::LocalTime).toString();
       
   195         return QVariant(list);
       
   196     } else if (role == Qt::DecorationRole) {
       
   197         QList<QVariant> icons;
       
   198         getDecorationData(*event, icons);
       
   199         return QVariant(icons);
       
   200     } else if ( role == RoleDetailsModel ) {
       
   201         LOGS_QDEBUG( "logs [ENG]    LogsModel::data() RoleDetailsModel" )
       
   202         LogsDetailsModel* detailsModel = 
       
   203             new LogsDetailsModel( *mDbConnector, *event );
       
   204         QVariant var = qVariantFromValue( detailsModel );
       
   205         return var;
       
   206     }
       
   207     LogsModelItemContainer item(event);
       
   208     return doGetData(role, item);
       
   209 }
       
   210 
       
   211 // -----------------------------------------------------------------------------
       
   212 // 
       
   213 // -----------------------------------------------------------------------------
       
   214 //
       
   215 LogsMatchesModel* LogsModel::logsMatchesModel()
       
   216 {
       
   217     LOGS_QDEBUG( "logs [ENG]    LogsModel::logsMatchesModel()" )
       
   218     return new LogsMatchesModel( *this, *mDbConnector );
       
   219 }
       
   220 
       
   221 // -----------------------------------------------------------------------------
       
   222 // NOTE: documentation mentions that beginInsertRows should be called
       
   223 // before adding data to the model. We are not conforming to that at
       
   224 // the moment and still everything works fine. If there is problems
       
   225 // in future, dbconnector should give prenotification about data addition.
       
   226 // Same applies for dataUpdated and dataRemoved.
       
   227 // -----------------------------------------------------------------------------
       
   228 //
       
   229 void LogsModel::dataAdded(QList<int> addedIndexes)
       
   230 {
       
   231     LOGS_QDEBUG_2( "logs [ENG] -> LogsModel::dataAdded(), idxs:", addedIndexes );
       
   232     LOGS_QDEBUG_EVENT_ARR(mEvents)
       
   233     
       
   234     QList< QList<int> > sequences = findSequentialIndexes(addedIndexes);
       
   235     for ( int i = 0; i < sequences.count(); i++ ) {
       
   236         beginInsertRows(QModelIndex(), sequences.at(i).first(), sequences.at(i).last());
       
   237         endInsertRows();
       
   238     }
       
   239     LOGS_QDEBUG( "logs [ENG] <- LogsModel::dataAdded()" )
       
   240 }
       
   241 
       
   242 // -----------------------------------------------------------------------------
       
   243 //
       
   244 // -----------------------------------------------------------------------------
       
   245 //
       
   246 void LogsModel::dataUpdated(QList<int> updatedIndexes)
       
   247 {
       
   248     LOGS_QDEBUG_2( "logs [ENG] -> LogsModel::dataUpdated(), idxs:", updatedIndexes );
       
   249     LOGS_QDEBUG_EVENT_ARR(mEvents)
       
   250     
       
   251     QList< QList<int> > sequences = findSequentialIndexes(updatedIndexes);
       
   252     for ( int i = 0; i < sequences.count(); i++ ) {
       
   253         QModelIndex top = index(sequences.at(i).first());
       
   254         QModelIndex bottom = index(sequences.at(i).last());
       
   255         emit dataChanged(top, bottom);
       
   256     }
       
   257     LOGS_QDEBUG( "logs [ENG] <- LogsModel::dataUpdated()" )
       
   258 }
       
   259 
       
   260 // -----------------------------------------------------------------------------
       
   261 //
       
   262 // -----------------------------------------------------------------------------
       
   263 //
       
   264 void LogsModel::dataRemoved(QList<int> removedIndexes)
       
   265 {
       
   266     LOGS_QDEBUG_2( "logs [ENG] -> LogsModel::dataRemoved(), idxs:", removedIndexes );
       
   267     LOGS_QDEBUG_EVENT_ARR(mEvents)
       
   268     
       
   269     QList< QList<int> > sequences = findSequentialIndexes(removedIndexes);
       
   270     for ( int i = ( sequences.count() - 1 ); i >= 0; i-- ) {
       
   271         beginRemoveRows(QModelIndex(), sequences.at(i).first(), sequences.at(i).last());
       
   272         endRemoveRows();
       
   273     }
       
   274     LOGS_QDEBUG( "logs [ENG] <- LogsModel::dataRemoved()" )
       
   275 }
       
   276 
       
   277 // -----------------------------------------------------------------------------
       
   278 //
       
   279 // -----------------------------------------------------------------------------
       
   280 //
       
   281 QList< QList<int> > LogsModel::findSequentialIndexes(const QList<int>& indexes)
       
   282 {
       
   283     QList< QList<int> > sequences;
       
   284     QList<int> currSequence;
       
   285     int prevIndex = indexes.at(0) - 1;
       
   286     for ( int i = 0; i < indexes.count(); i++ ){
       
   287         int currIndex = indexes.at(i);
       
   288         if ( prevIndex+1 != currIndex ){
       
   289             
       
   290             sequences.append(currSequence);
       
   291             currSequence.clear();
       
   292         } 
       
   293         currSequence.append(currIndex);
       
   294         prevIndex = currIndex;
       
   295     }
       
   296     
       
   297     if ( !currSequence.isEmpty() ){
       
   298         // Add last sequence if such exist
       
   299         sequences.append(currSequence);
       
   300     }
       
   301     return sequences;
       
   302 }
       
   303 
       
   304 // -----------------------------------------------------------------------------
       
   305 // Caller id:
       
   306 // name
       
   307 // or num
       
   308 // or remote url
       
   309 // or no num
       
   310 // -----------------------------------------------------------------------------
       
   311 //
       
   312 QString LogsModel::getCallerId(const LogsEvent& event) const
       
   313 {
       
   314     QString callerId(event.remoteParty());
       
   315     if ( callerId.length() == 0 ){
       
   316         callerId = event.number();
       
   317     }
       
   318     if ( callerId.length() == 0 && event.logsEventData() ){
       
   319         callerId = event.logsEventData()->remoteUrl();
       
   320     }
       
   321     if ( callerId.length() == 0 ){
       
   322         callerId = tr("No number");
       
   323     }
       
   324     int duplicates = event.duplicates();
       
   325     if ( duplicates > 0 && !event.isSeenLocally() ){
       
   326         callerId.append( "(" );
       
   327         callerId.append( QString::number(duplicates + 1) );
       
   328         callerId.append( ")");
       
   329     }
       
   330     return callerId;
       
   331 }
       
   332 
       
   333 // -----------------------------------------------------------------------------
       
   334 //
       
   335 // -----------------------------------------------------------------------------
       
   336 //
       
   337 void LogsModel::initIcons()
       
   338 {
       
   339     LOGS_QDEBUG( "logs [ENG] -> LogsModel::LogsModel()" )
       
   340     
       
   341     HbIcon* icon = new HbIcon(logsDialledVoiceCallIconId);    
       
   342     mIcons.insert(logsDialledVoiceCallIconId, icon);
       
   343     icon = new HbIcon(logsMissedVoiceCallIconId);    
       
   344     mIcons.insert(logsMissedVoiceCallIconId, icon);
       
   345     icon = new HbIcon(logsMissedVoiceCallUnseenIconId);    
       
   346     mIcons.insert(logsMissedVoiceCallUnseenIconId, icon);
       
   347     icon = new HbIcon(logsReceivedVoiceCallIconId);    
       
   348     mIcons.insert(logsReceivedVoiceCallIconId, icon);
       
   349 
       
   350     icon = new HbIcon(logsDialledVideoCallIconId);    
       
   351     mIcons.insert(logsDialledVideoCallIconId, icon);
       
   352     icon = new HbIcon(logsMissedVideoCallIconId);    
       
   353     mIcons.insert(logsMissedVideoCallIconId, icon);
       
   354     icon = new HbIcon(logsMissedVideoCallUnseenIconId);    
       
   355     mIcons.insert(logsMissedVideoCallUnseenIconId, icon);
       
   356     icon = new HbIcon(logsReceivedVideoCallIconId);    
       
   357     mIcons.insert(logsReceivedVideoCallIconId, icon);
       
   358     
       
   359     icon = new HbIcon(logsDialledVoipCallIconId);    
       
   360     mIcons.insert(logsDialledVoipCallIconId, icon);
       
   361     icon = new HbIcon(logsMissedVoipCallIconId);    
       
   362     mIcons.insert(logsMissedVoipCallIconId, icon);
       
   363     icon = new HbIcon(logsMissedVoipCallUnseenIconId);    
       
   364     mIcons.insert(logsMissedVoipCallUnseenIconId, icon);
       
   365     icon = new HbIcon(logsReceivedVoipCallIconId);    
       
   366     mIcons.insert(logsReceivedVoipCallIconId, icon);
       
   367 
       
   368     LOGS_QDEBUG( "logs [ENG] <- LogsModel::LogsModel()" )
       
   369 }
       
   370 
       
   371 // ----------------------------------------------------------------------------
       
   372 //
       
   373 // ----------------------------------------------------------------------------
       
   374 //
       
   375 bool LogsModel::matchEventWithClearType(
       
   376         const LogsEvent& event, LogsModel::ClearType clearType)
       
   377 {
       
   378     bool match( false );
       
   379     LogsEvent::LogsDirection dir = event.direction();
       
   380     switch (clearType){
       
   381         case LogsModel::TypeLogsClearAll:
       
   382             match = true;
       
   383             break;
       
   384         case LogsModel::TypeLogsClearReceived:
       
   385             match = ( dir == LogsEvent::DirIn );
       
   386             break;
       
   387         case LogsModel::TypeLogsClearCalled:
       
   388             match = ( dir == LogsEvent::DirOut );
       
   389             break;
       
   390         case LogsModel::TypeLogsClearMissed:
       
   391             match = ( dir == LogsEvent::DirMissed );
       
   392             break;
       
   393         default:
       
   394            break;
       
   395     }
       
   396     return match;
       
   397 }
       
   398