PageRenderTime 149ms CodeModel.GetById 60ms app.highlight 18ms RepoModel.GetById 63ms app.codeStats 6ms

/src/GetNewStuffModel.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 156 lines | 115 code | 18 blank | 23 comment | 18 complexity | 640a43967445054e80f8f2220976d7b2 MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
  4 *
  5 *   Tomahawk is free software: you can redistribute it and/or modify
  6 *   it under the terms of the GNU General Public License as published by
  7 *   the Free Software Foundation, either version 3 of the License, or
  8 *   (at your option) any later version.
  9 *
 10 *   Tomahawk is distributed in the hope that it will be useful,
 11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 13 *   GNU General Public License for more details.
 14 *
 15 *   You should have received a copy of the GNU General Public License
 16 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 17 */
 18
 19#include "GetNewStuffModel.h"
 20
 21#include "utils/tomahawkutils.h"
 22#include "utils/logger.h"
 23
 24#include <QPixmap>
 25#include <QUrl>
 26#include "AtticaManager.h"
 27
 28GetNewStuffModel::GetNewStuffModel( QObject* parent )
 29    : QAbstractListModel ( parent )
 30{
 31
 32    if ( AtticaManager::instance()->resolversLoaded() )
 33        m_contentList = AtticaManager::instance()->resolvers();
 34
 35    connect( AtticaManager::instance(), SIGNAL( resolversReloaded( Attica::Content::List ) ), this, SLOT( resolversReloaded( Attica::Content::List ) ) );
 36    connect( AtticaManager::instance(), SIGNAL( resolverStateChanged( QString ) ), this, SLOT( resolverStateChanged( QString ) ) );
 37
 38}
 39
 40GetNewStuffModel::~GetNewStuffModel()
 41{
 42}
 43
 44void
 45GetNewStuffModel::resolversReloaded( const Attica::Content::List& resolvers )
 46{
 47    beginResetModel();
 48    m_contentList = resolvers;
 49    endResetModel();
 50}
 51
 52void
 53GetNewStuffModel::resolverStateChanged( const QString& resolverId )
 54{
 55    for ( int i = 0; i < m_contentList.count(); i++ )
 56    {
 57        const Attica::Content resolver = m_contentList[ i ];
 58        if ( resolver.id() == resolverId )
 59        {
 60            QModelIndex idx = index( i, 0, QModelIndex() );
 61            emit dataChanged( idx, idx );
 62        }
 63    }
 64}
 65
 66
 67QVariant
 68GetNewStuffModel::data( const QModelIndex& index, int role ) const
 69{
 70    if ( !index.isValid() || !hasIndex( index.row(), index.column(), index.parent() ) )
 71        return QVariant();
 72
 73    Attica::Content resolver = m_contentList[ index.row() ];
 74    switch ( role )
 75    {
 76        case Qt::DisplayRole:
 77            return resolver.name();
 78        case Qt::DecorationRole:
 79            return QVariant::fromValue< QPixmap >( AtticaManager::instance()->iconForResolver( resolver ) );
 80        case DownloadUrlRole:
 81            // TODO
 82            return QUrl();
 83        case RatingRole:
 84            return resolver.rating() / 20; // rating is out of 100
 85        case DownloadCounterRole:
 86            return resolver.downloads();
 87        case VersionRole:
 88            return resolver.version();
 89        case DescriptionRole:
 90            return resolver.description();
 91        case TypeRole:
 92            return ResolverType;
 93        case AuthorRole:
 94            return resolver.author();
 95        case StateRole:
 96            return (int)AtticaManager::instance()->resolverState( resolver );
 97        case UserHasRatedRole:
 98            return AtticaManager::instance()->userHasRated( resolver );
 99    }
100    return QVariant();
101}
102
103int
104GetNewStuffModel::rowCount( const QModelIndex& parent ) const
105{
106    Q_UNUSED( parent );
107    return m_contentList.count();
108}
109
110bool
111GetNewStuffModel::setData( const QModelIndex &index, const QVariant &value, int role )
112{
113    Q_UNUSED( value );
114    if ( !hasIndex( index.row(), index.column(), index.parent() ) )
115        return false;
116
117
118    Attica::Content resolver = m_contentList[ index.row() ];
119    AtticaManager::ResolverState state = AtticaManager::instance()->resolverState( resolver );
120    if ( role == Qt::EditRole )
121    {
122        switch( state )
123        {
124            case AtticaManager::Uninstalled:
125                // install
126                AtticaManager::instance()->installResolver( resolver );
127                break;
128            case AtticaManager::Installing:
129            case AtticaManager::Upgrading:
130                // Do nothing, busy
131                break;
132            case AtticaManager::Installed:
133                // Uninstall
134                AtticaManager::instance()->uninstallResolver( resolver );
135                break;
136            case AtticaManager::NeedsUpgrade:
137                AtticaManager::instance()->upgradeResolver( resolver );
138                break;
139            default:
140                //FIXME -- this handles e.g. Failed
141                break;
142        };
143    } else if ( role == RatingRole )
144    {
145        // For now only allow rating if a resolver is installed!
146        if ( state != AtticaManager::Installed && state != AtticaManager::NeedsUpgrade )
147            return false;
148        if ( AtticaManager::instance()->userHasRated( resolver ) )
149            return false;
150        m_contentList[ index.row() ].setRating( value.toInt() * 20 );
151        AtticaManager::instance()->uploadRating( m_contentList[ index.row() ] );
152    }
153    emit dataChanged( index, index );
154
155    return true;
156}