/src/GetNewStuffModel.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 156 lines · 115 code · 18 blank · 23 comment · 19 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. #include "GetNewStuffModel.h"
  19. #include "utils/tomahawkutils.h"
  20. #include "utils/logger.h"
  21. #include <QPixmap>
  22. #include <QUrl>
  23. #include "AtticaManager.h"
  24. GetNewStuffModel::GetNewStuffModel( QObject* parent )
  25. : QAbstractListModel ( parent )
  26. {
  27. if ( AtticaManager::instance()->resolversLoaded() )
  28. m_contentList = AtticaManager::instance()->resolvers();
  29. connect( AtticaManager::instance(), SIGNAL( resolversReloaded( Attica::Content::List ) ), this, SLOT( resolversReloaded( Attica::Content::List ) ) );
  30. connect( AtticaManager::instance(), SIGNAL( resolverStateChanged( QString ) ), this, SLOT( resolverStateChanged( QString ) ) );
  31. }
  32. GetNewStuffModel::~GetNewStuffModel()
  33. {
  34. }
  35. void
  36. GetNewStuffModel::resolversReloaded( const Attica::Content::List& resolvers )
  37. {
  38. beginResetModel();
  39. m_contentList = resolvers;
  40. endResetModel();
  41. }
  42. void
  43. GetNewStuffModel::resolverStateChanged( const QString& resolverId )
  44. {
  45. for ( int i = 0; i < m_contentList.count(); i++ )
  46. {
  47. const Attica::Content resolver = m_contentList[ i ];
  48. if ( resolver.id() == resolverId )
  49. {
  50. QModelIndex idx = index( i, 0, QModelIndex() );
  51. emit dataChanged( idx, idx );
  52. }
  53. }
  54. }
  55. QVariant
  56. GetNewStuffModel::data( const QModelIndex& index, int role ) const
  57. {
  58. if ( !index.isValid() || !hasIndex( index.row(), index.column(), index.parent() ) )
  59. return QVariant();
  60. Attica::Content resolver = m_contentList[ index.row() ];
  61. switch ( role )
  62. {
  63. case Qt::DisplayRole:
  64. return resolver.name();
  65. case Qt::DecorationRole:
  66. return QVariant::fromValue< QPixmap >( AtticaManager::instance()->iconForResolver( resolver ) );
  67. case DownloadUrlRole:
  68. // TODO
  69. return QUrl();
  70. case RatingRole:
  71. return resolver.rating() / 20; // rating is out of 100
  72. case DownloadCounterRole:
  73. return resolver.downloads();
  74. case VersionRole:
  75. return resolver.version();
  76. case DescriptionRole:
  77. return resolver.description();
  78. case TypeRole:
  79. return ResolverType;
  80. case AuthorRole:
  81. return resolver.author();
  82. case StateRole:
  83. return (int)AtticaManager::instance()->resolverState( resolver );
  84. case UserHasRatedRole:
  85. return AtticaManager::instance()->userHasRated( resolver );
  86. }
  87. return QVariant();
  88. }
  89. int
  90. GetNewStuffModel::rowCount( const QModelIndex& parent ) const
  91. {
  92. Q_UNUSED( parent );
  93. return m_contentList.count();
  94. }
  95. bool
  96. GetNewStuffModel::setData( const QModelIndex &index, const QVariant &value, int role )
  97. {
  98. Q_UNUSED( value );
  99. if ( !hasIndex( index.row(), index.column(), index.parent() ) )
  100. return false;
  101. Attica::Content resolver = m_contentList[ index.row() ];
  102. AtticaManager::ResolverState state = AtticaManager::instance()->resolverState( resolver );
  103. if ( role == Qt::EditRole )
  104. {
  105. switch( state )
  106. {
  107. case AtticaManager::Uninstalled:
  108. // install
  109. AtticaManager::instance()->installResolver( resolver );
  110. break;
  111. case AtticaManager::Installing:
  112. case AtticaManager::Upgrading:
  113. // Do nothing, busy
  114. break;
  115. case AtticaManager::Installed:
  116. // Uninstall
  117. AtticaManager::instance()->uninstallResolver( resolver );
  118. break;
  119. case AtticaManager::NeedsUpgrade:
  120. AtticaManager::instance()->upgradeResolver( resolver );
  121. break;
  122. default:
  123. //FIXME -- this handles e.g. Failed
  124. break;
  125. };
  126. } else if ( role == RatingRole )
  127. {
  128. // For now only allow rating if a resolver is installed!
  129. if ( state != AtticaManager::Installed && state != AtticaManager::NeedsUpgrade )
  130. return false;
  131. if ( AtticaManager::instance()->userHasRated( resolver ) )
  132. return false;
  133. m_contentList[ index.row() ].setRating( value.toInt() * 20 );
  134. AtticaManager::instance()->uploadRating( m_contentList[ index.row() ] );
  135. }
  136. emit dataChanged( index, index );
  137. return true;
  138. }