homescreenapp/stateplugins/hsapplibrarystateplugin/src/hsaddmodeproxymodel.cpp
changeset 71 1db7cc813a4e
child 81 7dd137878ff8
equal deleted inserted replaced
69:87476091b3f5 71:1db7cc813a4e
       
     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(applicationTypeName());
       
    72     mQuery.setEntryTypeNames(entryTypeNames);
       
    73     mQuery.setAttribute(swTypeKey(), HS_CWRT_APP_TYPE);
       
    74     mQuery.setAttribute(widgetUriAttributeName(),
       
    75                        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(widgetParam())) {
       
    87                 preferences.insert(key.remove(widgetParam()),value);
       
    88             }
       
    89         }
       
    90         int count(0);
       
    91         HsContentService::instance()->widgets(
       
    92                 HS_WIDGET_URI_ATTRIBUTE_CWRT_VALUE,preferences,count);
       
    93         mCwrtWidgetCache.insert(entry->id(), count > 0);
       
    94     }
       
    95 
       
    96 }
       
    97 
       
    98 /*!
       
    99  Return entryid for given Cwrt with mini view widget.
       
   100  \param uri of a widget.
       
   101  \param preferences widget preferences.
       
   102  \retval int entry id or 0 for not cwrt with mini view widgets.
       
   103  */
       
   104 int HsAddModeProxyModel::findCwrtWidgetEntryId(const QVariantHash &preferences)
       
   105 {
       
   106     int result = 0;
       
   107     CaQuery query;
       
   108     QStringList entryTypeNames;
       
   109     query.setEntryTypeNames(entryTypeNames);
       
   110     query.setAttribute(swTypeKey(), HS_CWRT_APP_TYPE);
       
   111     query.setAttribute(widgetUriAttributeName(),
       
   112             HS_WIDGET_URI_ATTRIBUTE_CWRT_VALUE);
       
   113     foreach (QString key, preferences.keys()) {
       
   114         query.setAttribute(widgetParam()+key,
       
   115             preferences.value(key).toString());
       
   116     }
       
   117     QList< QSharedPointer<CaEntry> > entries =
       
   118         CaService::instance()->getEntries(query);
       
   119     if( entries.count() > 0 )
       
   120     {
       
   121         result = entries[0]->id();
       
   122     }
       
   123     return result;
       
   124 }
       
   125 
       
   126 /*
       
   127  Slot called when widget is added to homescreen.
       
   128  If widget is removed we checkhow many instance of this widget is left on HS.
       
   129  \param uri of a widget.
       
   130  \param preferences widget preferences.
       
   131  */
       
   132 void HsAddModeProxyModel::updateCacheOnAddWidget(const QString &uri,
       
   133     const QVariantHash &preferences){
       
   134 	if (uri == HS_WIDGET_URI_ATTRIBUTE_CWRT_VALUE) {
       
   135         int entryId = findCwrtWidgetEntryId(preferences);
       
   136         if (!mCwrtWidgetCache.value(entryId) ) {
       
   137             mCwrtWidgetCache.insert(entryId,true);
       
   138             invalidateFilter();
       
   139         }
       
   140     }
       
   141 }
       
   142 
       
   143 /*
       
   144  Slot called when widget is removed from 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::updateCacheOnRemoveWidget(const QString &uri,
       
   150     const QVariantHash &preferences)
       
   151 {
       
   152     if (uri == HS_WIDGET_URI_ATTRIBUTE_CWRT_VALUE) {
       
   153         int entryId = findCwrtWidgetEntryId(preferences);
       
   154         if (mCwrtWidgetCache.value(entryId)) {
       
   155             mCwrtWidgetCache.insert(entryId,false);
       
   156             invalidateFilter();
       
   157         }
       
   158     }
       
   159 }
       
   160 
       
   161 /*
       
   162  Reimplementated from QSortFilterProxyModel.
       
   163  \param source_row row in source model.
       
   164  \param source_parent source parent index.
       
   165  */
       
   166 bool HsAddModeProxyModel::filterAcceptsRow(int source_row,
       
   167         const QModelIndex &source_parent) const
       
   168 {
       
   169     QVariant entryId = sourceModel()->data(
       
   170             sourceModel()->index(source_row,0,source_parent),
       
   171             CaItemModel::IdRole);
       
   172     return !mCwrtWidgetCache.value(entryId.toInt());
       
   173 }
       
   174 
       
   175 /*
       
   176  Update cache if some cwrt widgets were install/uninstal/update or on mmc card.
       
   177  \param entry changed entry.
       
   178  \param changeType the type of the change. 
       
   179  */
       
   180 void HsAddModeProxyModel::updateEntryStatus(const CaEntry &entry, ChangeType changeType)
       
   181 {
       
   182     if (changeType == RemoveChangeType) {
       
   183         mCwrtWidgetCache.remove(entry.id());
       
   184     } else {
       
   185         mCwrtWidgetCache.insert(entry.id(),
       
   186             HsAppLibStateUtils::isCWRTWidgetOnHomeScreen(&entry));
       
   187     }
       
   188 }