homescreenapp/stateplugins/hsapplibrarystateplugin/src/hsaddmodeproxymodel.cpp
branchRCL_3
changeset 82 5f0182e07bfb
equal deleted inserted replaced
79:f00a6757af32 82:5f0182e07bfb
       
     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: Proxy model for view in add to homescreen mode.
       
    15  *
       
    16  */
       
    17 
       
    18 #include <QObject>
       
    19 #include <canotifier.h>
       
    20 
       
    21 #include "hscontentservice.h"
       
    22 #include "hsaddmodeproxymodel.h"
       
    23 #include "hsmenuitemmodel.h"
       
    24 #include "hsapplibstateutils.h"
       
    25 
       
    26 /*!
       
    27  \class HsAddModeProxyModel
       
    28  \ingroup group_hsmenustateplugin
       
    29  \brief Proxy Model for add to homescreen mode.
       
    30  Filtering cwrt widgets to not displaing them in add mode when they are alredy present on HS.
       
    31  \lib ?library
       
    32  \see QSortFilterProxyModel
       
    33  */
       
    34 
       
    35 /*!
       
    36  Constructor.
       
    37  \param parent parent for object.
       
    38  */
       
    39 HsAddModeProxyModel::HsAddModeProxyModel(QObject *parent) :
       
    40     QSortFilterProxyModel(parent),
       
    41     mCwrtWidgetCache(),
       
    42     mQuery()
       
    43 {
       
    44     initilizeCwrtWidgetCache();
       
    45     invalidateFilter();
       
    46     connect(HsContentService::instance(),
       
    47         SIGNAL(widgetAdded(const QString &, const QVariantHash &)),
       
    48         this, SLOT(updateCacheOnAddWidget(const QString&, const QVariantHash&)));
       
    49     connect(HsContentService::instance(),
       
    50         SIGNAL(widgetRemoved(const QString &, const QVariantHash &)),
       
    51         this, SLOT(updateCacheOnRemoveWidget(const QString&, const QVariantHash&)));
       
    52     // create notifier for a cwrt widgets with query created in initilizeCwrtWidgetCache
       
    53     CaNotifierFilter filter(mQuery);
       
    54     mNotifier.reset(CaService::instance()->createNotifier(filter));
       
    55     connect(mNotifier.data(),
       
    56             SIGNAL(entryChanged(const CaEntry&, ChangeType)),
       
    57             this,
       
    58             SLOT(updateEntryStatus(const CaEntry&, ChangeType)));
       
    59 }
       
    60 
       
    61 /*
       
    62  Destructor.
       
    63  */
       
    64 HsAddModeProxyModel::~HsAddModeProxyModel()
       
    65 {
       
    66 }
       
    67 
       
    68 void HsAddModeProxyModel::initilizeCwrtWidgetCache()
       
    69 {
       
    70     QStringList entryTypeNames;
       
    71     entryTypeNames.append(Hs::applicationTypeName);
       
    72     mQuery.setEntryTypeNames(entryTypeNames);
       
    73     mQuery.setAttribute(Hs::swTypeKey, Hs::HS_CWRT_APP_TYPE);
       
    74     mQuery.setAttribute(Hs::widgetUriAttributeName,
       
    75                        Hs::HS_WIDGET_URI_ATTRIBUTE_CWRT_VALUE);
       
    76     QList< QSharedPointer<CaEntry> > entries =
       
    77             CaService::instance()->getEntries(mQuery);
       
    78     foreach (QSharedPointer<CaEntry> entry, entries) {
       
    79         QVariantHash preferences;
       
    80         QMap<QString, QString> attributes = entry->attributes();
       
    81         QMapIterator<QString, QString> i(attributes);
       
    82         while (i.hasNext()) {
       
    83             i.next();
       
    84             QString key(i.key());
       
    85             QString value(i.value());
       
    86             if (key.contains(Hs::widgetParam)) {
       
    87                 preferences.insert(key.remove(Hs::widgetParam),value);
       
    88             }
       
    89         }
       
    90         int count(0);
       
    91         HsContentService::instance()->widgets(
       
    92                 Hs::HS_WIDGET_URI_ATTRIBUTE_CWRT_VALUE,preferences,count);
       
    93         mCwrtWidgetCache.insert(entry->id(), count > 0);
       
    94     }
       
    95 
       
    96 }
       
    97 
       
    98 /*
       
    99  Reimplementated from QSortFilterProxyModel to forward signals from CaItemModel.
       
   100  \param sourceModel source model.
       
   101  */
       
   102 void HsAddModeProxyModel::setSourceModel(QAbstractItemModel *sourceModel)
       
   103 {
       
   104     QSortFilterProxyModel::setSourceModel(sourceModel);
       
   105     connect(sourceModel,
       
   106             SIGNAL(scrollTo(int, QAbstractItemView::ScrollHint)),
       
   107             this,
       
   108             SIGNAL(scrollTo(int, QAbstractItemView::ScrollHint)));
       
   109     connect(sourceModel,
       
   110             SIGNAL(countChange()),
       
   111             this,
       
   112             SIGNAL(countChange()));
       
   113 }
       
   114 
       
   115 /*!
       
   116  Return entryid for given Cwrt with mini view widget.
       
   117  \param uri of a widget.
       
   118  \param preferences widget preferences.
       
   119  \retval int entry id or 0 for not cwrt with mini view widgets.
       
   120  */
       
   121 int HsAddModeProxyModel::findCwrtWidgetEntryId(const QVariantHash &preferences)
       
   122 {
       
   123     int result = 0;
       
   124     CaQuery query;
       
   125     QStringList entryTypeNames;
       
   126     query.setEntryTypeNames(entryTypeNames);
       
   127     query.setAttribute(Hs::swTypeKey, Hs::HS_CWRT_APP_TYPE);
       
   128     query.setAttribute(Hs::widgetUriAttributeName,
       
   129             Hs::HS_WIDGET_URI_ATTRIBUTE_CWRT_VALUE);
       
   130     foreach (QString key, preferences.keys()) {
       
   131         query.setAttribute(Hs::widgetParam+key,
       
   132             preferences.value(key).toString());
       
   133     }
       
   134     QList< QSharedPointer<CaEntry> > entries =
       
   135         CaService::instance()->getEntries(query);
       
   136     if( entries.count() > 0 )
       
   137     {
       
   138         result = entries[0]->id();
       
   139     }
       
   140     return result;
       
   141 }
       
   142 
       
   143 /*
       
   144  Slot called when widget is added to homescreen.
       
   145  If widget is removed we checkhow many instance of this widget is left on HS.
       
   146  \param uri of a widget.
       
   147  \param preferences widget preferences.
       
   148  */
       
   149 void HsAddModeProxyModel::updateCacheOnAddWidget(const QString &uri,
       
   150     const QVariantHash &preferences){
       
   151 	if (uri == Hs::HS_WIDGET_URI_ATTRIBUTE_CWRT_VALUE) {
       
   152         int entryId = findCwrtWidgetEntryId(preferences);
       
   153         if (!mCwrtWidgetCache.value(entryId) ) {
       
   154             mCwrtWidgetCache.insert(entryId,true);
       
   155             invalidateFilter();
       
   156         }
       
   157     }
       
   158 }
       
   159 
       
   160 /*
       
   161  Slot called when widget is removed from homescreen.
       
   162  If widget is removed we checkhow many instance of this widget is left on HS.
       
   163  \param uri of a widget.
       
   164  \param preferences widget preferences.
       
   165 */
       
   166 void HsAddModeProxyModel::updateCacheOnRemoveWidget(const QString &uri,
       
   167     const QVariantHash &preferences)
       
   168 {
       
   169     if (uri == Hs::HS_WIDGET_URI_ATTRIBUTE_CWRT_VALUE) {
       
   170         int entryId = findCwrtWidgetEntryId(preferences);
       
   171         if (mCwrtWidgetCache.value(entryId)) {
       
   172             mCwrtWidgetCache.insert(entryId,false);
       
   173             invalidateFilter();
       
   174         }
       
   175     }
       
   176 }
       
   177 
       
   178 /*
       
   179  Reimplementated from QSortFilterProxyModel.
       
   180  \param source_row row in source model.
       
   181  \param source_parent source parent index.
       
   182  */
       
   183 bool HsAddModeProxyModel::filterAcceptsRow(int source_row,
       
   184         const QModelIndex &source_parent) const
       
   185 {
       
   186     QVariant entryId = sourceModel()->data(
       
   187             sourceModel()->index(source_row,0,source_parent),
       
   188             CaItemModel::IdRole);
       
   189     return !mCwrtWidgetCache.value(entryId.toInt());
       
   190 }
       
   191 
       
   192 /*
       
   193  Update cache if some cwrt widgets were install/uninstal/update or on mmc card.
       
   194  \param entry changed entry.
       
   195  \param changeType the type of the change. 
       
   196  */
       
   197 void HsAddModeProxyModel::updateEntryStatus(const CaEntry &entry, ChangeType changeType)
       
   198 {
       
   199     if (changeType == RemoveChangeType) {
       
   200         mCwrtWidgetCache.remove(entry.id());
       
   201     } else {
       
   202         mCwrtWidgetCache.insert(entry.id(),
       
   203             HsAppLibStateUtils::isCWRTWidgetOnHomeScreen(&entry));
       
   204     }
       
   205 }
       
   206