homescreenapp/stateplugins/hsapplibrarystateplugin/src/hsaddmodeproxymodel.cpp
author hgs
Fri, 06 Aug 2010 13:36:36 +0300
changeset 71 1db7cc813a4e
child 81 7dd137878ff8
permissions -rw-r--r--
201031
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
71
hgs
parents:
diff changeset
     1
/*
hgs
parents:
diff changeset
     2
 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
hgs
parents:
diff changeset
     3
 * All rights reserved.
hgs
parents:
diff changeset
     4
 * This component and the accompanying materials are made available
hgs
parents:
diff changeset
     5
 * under the terms of "Eclipse Public License v1.0"
hgs
parents:
diff changeset
     6
 * which accompanies this distribution, and is available
hgs
parents:
diff changeset
     7
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
hgs
parents:
diff changeset
     8
 *
hgs
parents:
diff changeset
     9
 * Initial Contributors:
hgs
parents:
diff changeset
    10
 * Nokia Corporation - initial contribution.
hgs
parents:
diff changeset
    11
 *
hgs
parents:
diff changeset
    12
 * Contributors:
hgs
parents:
diff changeset
    13
 *
hgs
parents:
diff changeset
    14
 * Description: Proxy model for view in add to homescreen mode.
hgs
parents:
diff changeset
    15
 *
hgs
parents:
diff changeset
    16
 */
hgs
parents:
diff changeset
    17
hgs
parents:
diff changeset
    18
#include <QObject>
hgs
parents:
diff changeset
    19
#include <canotifier.h>
hgs
parents:
diff changeset
    20
hgs
parents:
diff changeset
    21
#include "hscontentservice.h"
hgs
parents:
diff changeset
    22
#include "hsaddmodeproxymodel.h"
hgs
parents:
diff changeset
    23
#include "hsmenuitemmodel.h"
hgs
parents:
diff changeset
    24
#include "hsapplibstateutils.h"
hgs
parents:
diff changeset
    25
hgs
parents:
diff changeset
    26
/*!
hgs
parents:
diff changeset
    27
 \class HsAddModeProxyModel
hgs
parents:
diff changeset
    28
 \ingroup group_hsmenustateplugin
hgs
parents:
diff changeset
    29
 \brief Proxy Model for add to homescreen mode.
hgs
parents:
diff changeset
    30
 Filtering cwrt widgets to not displaing them in add mode when they are alredy present on HS.
hgs
parents:
diff changeset
    31
 \lib ?library
hgs
parents:
diff changeset
    32
 \see QSortFilterProxyModel
hgs
parents:
diff changeset
    33
 */
hgs
parents:
diff changeset
    34
hgs
parents:
diff changeset
    35
/*!
hgs
parents:
diff changeset
    36
 Constructor.
hgs
parents:
diff changeset
    37
 \param parent parent for object.
hgs
parents:
diff changeset
    38
 */
hgs
parents:
diff changeset
    39
HsAddModeProxyModel::HsAddModeProxyModel(QObject *parent) :
hgs
parents:
diff changeset
    40
    QSortFilterProxyModel(parent),
hgs
parents:
diff changeset
    41
    mCwrtWidgetCache(),
hgs
parents:
diff changeset
    42
    mQuery()
hgs
parents:
diff changeset
    43
{
hgs
parents:
diff changeset
    44
    initilizeCwrtWidgetCache();
hgs
parents:
diff changeset
    45
    invalidateFilter();
hgs
parents:
diff changeset
    46
    connect(HsContentService::instance(),
hgs
parents:
diff changeset
    47
        SIGNAL(widgetAdded(const QString &, const QVariantHash &)),
hgs
parents:
diff changeset
    48
        this, SLOT(updateCacheOnAddWidget(const QString&, const QVariantHash&)));
hgs
parents:
diff changeset
    49
    connect(HsContentService::instance(),
hgs
parents:
diff changeset
    50
        SIGNAL(widgetRemoved(const QString &, const QVariantHash &)),
hgs
parents:
diff changeset
    51
        this, SLOT(updateCacheOnRemoveWidget(const QString&, const QVariantHash&)));
hgs
parents:
diff changeset
    52
    // create notifier for a cwrt widgets with query created in initilizeCwrtWidgetCache
hgs
parents:
diff changeset
    53
    CaNotifierFilter filter(mQuery);
hgs
parents:
diff changeset
    54
    mNotifier.reset(CaService::instance()->createNotifier(filter));
hgs
parents:
diff changeset
    55
    connect(mNotifier.data(),
hgs
parents:
diff changeset
    56
            SIGNAL(entryChanged(const CaEntry&, ChangeType)),
hgs
parents:
diff changeset
    57
            this,
hgs
parents:
diff changeset
    58
            SLOT(updateEntryStatus(const CaEntry&, ChangeType)));
hgs
parents:
diff changeset
    59
}
hgs
parents:
diff changeset
    60
hgs
parents:
diff changeset
    61
/*
hgs
parents:
diff changeset
    62
 Destructor.
hgs
parents:
diff changeset
    63
 */
hgs
parents:
diff changeset
    64
HsAddModeProxyModel::~HsAddModeProxyModel()
hgs
parents:
diff changeset
    65
{
hgs
parents:
diff changeset
    66
}
hgs
parents:
diff changeset
    67
hgs
parents:
diff changeset
    68
void HsAddModeProxyModel::initilizeCwrtWidgetCache()
hgs
parents:
diff changeset
    69
{
hgs
parents:
diff changeset
    70
    QStringList entryTypeNames;
hgs
parents:
diff changeset
    71
    entryTypeNames.append(applicationTypeName());
hgs
parents:
diff changeset
    72
    mQuery.setEntryTypeNames(entryTypeNames);
hgs
parents:
diff changeset
    73
    mQuery.setAttribute(swTypeKey(), HS_CWRT_APP_TYPE);
hgs
parents:
diff changeset
    74
    mQuery.setAttribute(widgetUriAttributeName(),
hgs
parents:
diff changeset
    75
                       HS_WIDGET_URI_ATTRIBUTE_CWRT_VALUE);
hgs
parents:
diff changeset
    76
    QList< QSharedPointer<CaEntry> > entries =
hgs
parents:
diff changeset
    77
            CaService::instance()->getEntries(mQuery);
hgs
parents:
diff changeset
    78
    foreach (QSharedPointer<CaEntry> entry, entries) {
hgs
parents:
diff changeset
    79
        QVariantHash preferences;
hgs
parents:
diff changeset
    80
        QMap<QString, QString> attributes = entry->attributes();
hgs
parents:
diff changeset
    81
        QMapIterator<QString, QString> i(attributes);
hgs
parents:
diff changeset
    82
        while (i.hasNext()) {
hgs
parents:
diff changeset
    83
            i.next();
hgs
parents:
diff changeset
    84
            QString key(i.key());
hgs
parents:
diff changeset
    85
            QString value(i.value());
hgs
parents:
diff changeset
    86
            if (key.contains(widgetParam())) {
hgs
parents:
diff changeset
    87
                preferences.insert(key.remove(widgetParam()),value);
hgs
parents:
diff changeset
    88
            }
hgs
parents:
diff changeset
    89
        }
hgs
parents:
diff changeset
    90
        int count(0);
hgs
parents:
diff changeset
    91
        HsContentService::instance()->widgets(
hgs
parents:
diff changeset
    92
                HS_WIDGET_URI_ATTRIBUTE_CWRT_VALUE,preferences,count);
hgs
parents:
diff changeset
    93
        mCwrtWidgetCache.insert(entry->id(), count > 0);
hgs
parents:
diff changeset
    94
    }
hgs
parents:
diff changeset
    95
hgs
parents:
diff changeset
    96
}
hgs
parents:
diff changeset
    97
hgs
parents:
diff changeset
    98
/*!
hgs
parents:
diff changeset
    99
 Return entryid for given Cwrt with mini view widget.
hgs
parents:
diff changeset
   100
 \param uri of a widget.
hgs
parents:
diff changeset
   101
 \param preferences widget preferences.
hgs
parents:
diff changeset
   102
 \retval int entry id or 0 for not cwrt with mini view widgets.
hgs
parents:
diff changeset
   103
 */
hgs
parents:
diff changeset
   104
int HsAddModeProxyModel::findCwrtWidgetEntryId(const QVariantHash &preferences)
hgs
parents:
diff changeset
   105
{
hgs
parents:
diff changeset
   106
    int result = 0;
hgs
parents:
diff changeset
   107
    CaQuery query;
hgs
parents:
diff changeset
   108
    QStringList entryTypeNames;
hgs
parents:
diff changeset
   109
    query.setEntryTypeNames(entryTypeNames);
hgs
parents:
diff changeset
   110
    query.setAttribute(swTypeKey(), HS_CWRT_APP_TYPE);
hgs
parents:
diff changeset
   111
    query.setAttribute(widgetUriAttributeName(),
hgs
parents:
diff changeset
   112
            HS_WIDGET_URI_ATTRIBUTE_CWRT_VALUE);
hgs
parents:
diff changeset
   113
    foreach (QString key, preferences.keys()) {
hgs
parents:
diff changeset
   114
        query.setAttribute(widgetParam()+key,
hgs
parents:
diff changeset
   115
            preferences.value(key).toString());
hgs
parents:
diff changeset
   116
    }
hgs
parents:
diff changeset
   117
    QList< QSharedPointer<CaEntry> > entries =
hgs
parents:
diff changeset
   118
        CaService::instance()->getEntries(query);
hgs
parents:
diff changeset
   119
    if( entries.count() > 0 )
hgs
parents:
diff changeset
   120
    {
hgs
parents:
diff changeset
   121
        result = entries[0]->id();
hgs
parents:
diff changeset
   122
    }
hgs
parents:
diff changeset
   123
    return result;
hgs
parents:
diff changeset
   124
}
hgs
parents:
diff changeset
   125
hgs
parents:
diff changeset
   126
/*
hgs
parents:
diff changeset
   127
 Slot called when widget is added to homescreen.
hgs
parents:
diff changeset
   128
 If widget is removed we checkhow many instance of this widget is left on HS.
hgs
parents:
diff changeset
   129
 \param uri of a widget.
hgs
parents:
diff changeset
   130
 \param preferences widget preferences.
hgs
parents:
diff changeset
   131
 */
hgs
parents:
diff changeset
   132
void HsAddModeProxyModel::updateCacheOnAddWidget(const QString &uri,
hgs
parents:
diff changeset
   133
    const QVariantHash &preferences){
hgs
parents:
diff changeset
   134
	if (uri == HS_WIDGET_URI_ATTRIBUTE_CWRT_VALUE) {
hgs
parents:
diff changeset
   135
        int entryId = findCwrtWidgetEntryId(preferences);
hgs
parents:
diff changeset
   136
        if (!mCwrtWidgetCache.value(entryId) ) {
hgs
parents:
diff changeset
   137
            mCwrtWidgetCache.insert(entryId,true);
hgs
parents:
diff changeset
   138
            invalidateFilter();
hgs
parents:
diff changeset
   139
        }
hgs
parents:
diff changeset
   140
    }
hgs
parents:
diff changeset
   141
}
hgs
parents:
diff changeset
   142
hgs
parents:
diff changeset
   143
/*
hgs
parents:
diff changeset
   144
 Slot called when widget is removed from homescreen.
hgs
parents:
diff changeset
   145
 If widget is removed we checkhow many instance of this widget is left on HS.
hgs
parents:
diff changeset
   146
 \param uri of a widget.
hgs
parents:
diff changeset
   147
 \param preferences widget preferences.
hgs
parents:
diff changeset
   148
*/
hgs
parents:
diff changeset
   149
void HsAddModeProxyModel::updateCacheOnRemoveWidget(const QString &uri,
hgs
parents:
diff changeset
   150
    const QVariantHash &preferences)
hgs
parents:
diff changeset
   151
{
hgs
parents:
diff changeset
   152
    if (uri == HS_WIDGET_URI_ATTRIBUTE_CWRT_VALUE) {
hgs
parents:
diff changeset
   153
        int entryId = findCwrtWidgetEntryId(preferences);
hgs
parents:
diff changeset
   154
        if (mCwrtWidgetCache.value(entryId)) {
hgs
parents:
diff changeset
   155
            mCwrtWidgetCache.insert(entryId,false);
hgs
parents:
diff changeset
   156
            invalidateFilter();
hgs
parents:
diff changeset
   157
        }
hgs
parents:
diff changeset
   158
    }
hgs
parents:
diff changeset
   159
}
hgs
parents:
diff changeset
   160
hgs
parents:
diff changeset
   161
/*
hgs
parents:
diff changeset
   162
 Reimplementated from QSortFilterProxyModel.
hgs
parents:
diff changeset
   163
 \param source_row row in source model.
hgs
parents:
diff changeset
   164
 \param source_parent source parent index.
hgs
parents:
diff changeset
   165
 */
hgs
parents:
diff changeset
   166
bool HsAddModeProxyModel::filterAcceptsRow(int source_row,
hgs
parents:
diff changeset
   167
        const QModelIndex &source_parent) const
hgs
parents:
diff changeset
   168
{
hgs
parents:
diff changeset
   169
    QVariant entryId = sourceModel()->data(
hgs
parents:
diff changeset
   170
            sourceModel()->index(source_row,0,source_parent),
hgs
parents:
diff changeset
   171
            CaItemModel::IdRole);
hgs
parents:
diff changeset
   172
    return !mCwrtWidgetCache.value(entryId.toInt());
hgs
parents:
diff changeset
   173
}
hgs
parents:
diff changeset
   174
hgs
parents:
diff changeset
   175
/*
hgs
parents:
diff changeset
   176
 Update cache if some cwrt widgets were install/uninstal/update or on mmc card.
hgs
parents:
diff changeset
   177
 \param entry changed entry.
hgs
parents:
diff changeset
   178
 \param changeType the type of the change. 
hgs
parents:
diff changeset
   179
 */
hgs
parents:
diff changeset
   180
void HsAddModeProxyModel::updateEntryStatus(const CaEntry &entry, ChangeType changeType)
hgs
parents:
diff changeset
   181
{
hgs
parents:
diff changeset
   182
    if (changeType == RemoveChangeType) {
hgs
parents:
diff changeset
   183
        mCwrtWidgetCache.remove(entry.id());
hgs
parents:
diff changeset
   184
    } else {
hgs
parents:
diff changeset
   185
        mCwrtWidgetCache.insert(entry.id(),
hgs
parents:
diff changeset
   186
            HsAppLibStateUtils::isCWRTWidgetOnHomeScreen(&entry));
hgs
parents:
diff changeset
   187
    }
hgs
parents:
diff changeset
   188
}