logsui/logsengine/inc/logsmatchesmodel.h
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Tue, 04 May 2010 12:39:37 +0300
changeset 0 4a5361db8937
child 2 7119b73b84d6
permissions -rw-r--r--
Revision: 201015 Kit: 201018

/*
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:
*
*/

#ifndef LOGSMATCHESMODEL_H
#define LOGSMATCHESMODEL_H

#include <logsexport.h>
#include <logsabstractmodel.h>
#include "logsengdefs.h"

class LogsEvent;
class LogsDbConnector;
class LogsCntFinder;
class LogsCntEntry;
class LogsContact;
class LogsMatchesModelItemContainer;
class LogsThumbIconManager;
typedef QObject LogsCntEntryHandle;

/**
 * Model for log event details.
 *
 */
class LogsMatchesModel : public LogsAbstractModel 
{
    Q_OBJECT
    
public:
    
    explicit LogsMatchesModel( LogsAbstractModel& parentModel,
                               LogsDbConnector& dbConnector );
    
public: // The exported API
  
    LOGSENGINE_EXPORT ~LogsMatchesModel();
    LOGSENGINE_EXPORT void logsMatches(const QString& pattern);
    
    /**
     * Factory method for creating a new contact object. Transfers ownership.
     */
    LOGSENGINE_EXPORT LogsContact* createContact(const QString& number);
    
    /**
     * Returns cenrep key status of predictive search feature. 
     * @return 0 - feature is permanently off and can't be turned on,
     *         1 - feature is on
     *         2 - feature is temporarily off and can be turned on 
     *         negative value indicates some error in fetching the key
     */
    LOGSENGINE_EXPORT int predictiveSearchStatus();
    
    /**
     * Allows to modify cenrep key value of predictive search features. 
     * However, this function can't be used if feature is set permanently off 
     * (see predictiveSearchStatus())
     * @param enabled, specify whether cenrep key will be set to 1 or 2
     * @ return 0 if cenrep key value modified succesfully,
     *          -1 in case of some error
     */
    LOGSENGINE_EXPORT int setPredictiveSearch(bool enabled);
        
public: // From QAbstractItemModel
    
    virtual int rowCount(const QModelIndex &parent) const;
    virtual QVariant data(const QModelIndex &index, int role) const;
 
public: // From LogsAbstractModel
    
    virtual QVariant createCall(const LogsModelItemContainer& item) const;
    virtual QVariant createMessage(const LogsModelItemContainer& item) const;
    virtual QVariant createContact(const LogsModelItemContainer& item) const;
   
    
private slots:

    void queryReady();
    void updateContactIcon(int index);  

    void eventsUpdated(const QModelIndex& first, const QModelIndex& last);
    void eventsAdded(const QModelIndex& parent, int first, int last);
    void eventsRemoved(const QModelIndex& parent, int first, int last);
	
    void doSearchQuery();
    void doModelReset();
    void forceSearchQuery();
    
private:

	void initPredictiveSearch();
	
	LogsMatchesModelItemContainer* addSearchResult(int resultIndex);
    bool updateSearchResult(LogsMatchesModelItemContainer& item) const;
    void readEvents(int first, int last);
    void getLogsMatches( const QString& pattern, bool async = false, bool force = false );  
    void updateSearchEntry(LogsCntEntry& entry, LogsEvent& event);
    QString stripPhoneNumber(const QString& phoneNumber) const;
    
private: //data 
    
    LogsAbstractModel& mParentModel;
    LogsCntFinder* mLogsCntFinder;
    
    QList<LogsMatchesModelItemContainer*> mMatches;
    QMap<LogsCntEntryHandle*, LogsEvent*> mSearchEvents;
    
    QString mCurrentSearchPattern;
    QString mPrevSearchPattern;
    LogsThumbIconManager   *mIconManager;
    bool mSearchEnabled;
    int mResultCount;
    
private:
        
    friend class UT_LogsModel;
    friend class UT_LogsMatchesModel;
    
};

/**
 * Contains log event or contact match
 */
class LogsMatchesModelItemContainer : public LogsModelItemContainer {
public:
    LogsMatchesModelItemContainer(LogsAbstractModel& parentModel,
								  LogsThumbIconManager& mIconManager,
                                  int resultIndex);
    virtual ~LogsMatchesModelItemContainer();
    
    void setEvent(const LogsEvent& event);
    void setContact(unsigned int contactId);
    unsigned int contact() const;
    QString number() const;
    QString contactName() const;
    bool isNull() const;
    bool isEventMatch() const;
    QStringList texts();
    QList<QVariant> icons(int row);
    void updateData(const LogsCntEntry& entry);
    int resultIndex() const;
    
private:
    
    QString getFormattedCallerId(const LogsCntEntry& entry) const;
    void getFormattedContactInfo( 
            const LogsCntEntry& entry, QString& contactName, QString& contactNumber ) const;
    
private:

    LogsAbstractModel& mParentModel;
    unsigned int mContactId;
    QString mContactName;
    QString mContactNumber;
    QString mAvatarPath;
    QString mFormattedCallerId;
	LogsThumbIconManager& mIconManager;
	int mResultIndex;
	
private:	
    
	friend class UT_LogsMatchesModel;
};

#endif //LOGSMATCHESMODEL_H