/src/libtomahawk/sip/SipModel.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 225 lines · 157 code · 43 blank · 25 comment · 36 complexity · 5b5850ed977064752aff49aa4dd70bf5 MD5 · raw file

  1. /*
  2. Copyright (C) 2011 Leo Franchi <lfranchi@kde.org>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "SipModel.h"
  15. #include "tomahawksettings.h"
  16. #include "sip/SipHandler.h"
  17. #include "sip/SipPlugin.h"
  18. #include "utils/logger.h"
  19. SipModel::SipModel( QObject* parent )
  20. : QAbstractItemModel( parent )
  21. {
  22. connect( SipHandler::instance(), SIGNAL( stateChanged( SipPlugin*, SipPlugin::ConnectionState ) ), this, SLOT( pluginStateChanged( SipPlugin* ) ) );
  23. connect( SipHandler::instance(), SIGNAL( pluginAdded( SipPlugin* ) ), this, SLOT( pluginAdded( SipPlugin* ) ) );
  24. connect( SipHandler::instance(), SIGNAL( pluginRemoved( SipPlugin* ) ), this, SLOT( pluginRemoved( SipPlugin* ) ) );
  25. // TODO disable inline factories for now
  26. /*
  27. foreach( SipPluginFactory* f, SipHandler::instance()->pluginFactories() ) {
  28. if( f->isCreatable() )
  29. m_factories << f;
  30. } */
  31. }
  32. SipModel::~SipModel()
  33. {
  34. }
  35. QVariant
  36. SipModel::data( const QModelIndex& index, int role ) const
  37. {
  38. if( !index.isValid() )
  39. return QVariant();
  40. if( !index.parent().isValid() && index.row() == SipHandler::instance()->allPlugins().count() ) { // last row, this is the factory
  41. if( role == Qt::DisplayRole )
  42. return tr( "Add New Account..." );
  43. else if( role == FactoryRole )
  44. return true;
  45. else
  46. return QVariant();
  47. }
  48. if( !index.parent().isValid() ) { // account
  49. QList< SipPlugin* > plugins = SipHandler::instance()->allPlugins();
  50. Q_ASSERT( index.row() <= plugins.size() );
  51. SipPlugin* p = plugins[ index.row() ];
  52. switch( role )
  53. {
  54. case Qt::DisplayRole:
  55. case SipModel::PluginName:
  56. return p->accountName();
  57. case SipModel::ConnectionStateRole:
  58. return p->connectionState();
  59. case SipModel::HasConfig:
  60. return ( p->configWidget() != 0 );
  61. case SipModel::FactoryRole:
  62. return false;
  63. case Qt::DecorationRole:
  64. return p->icon();
  65. case SipModel::SipPluginData:
  66. return QVariant::fromValue< QObject* >( p );
  67. case Qt::CheckStateRole:
  68. return SipHandler::instance()->enabledPlugins().contains( p ) ? Qt::Checked : Qt::Unchecked;
  69. default:
  70. return QVariant();
  71. }
  72. }
  73. if( index.parent().isValid() ) { // this is a factory type
  74. SipPluginFactory* p = m_factories.at( index.row() );
  75. switch( role )
  76. {
  77. case Qt::DisplayRole:
  78. return p->prettyName();
  79. case SipModel::FactoryItemRole:
  80. return true;
  81. case SipModel::FactoryItemIcon:
  82. return p->icon();
  83. case SipModel::SipPluginFactoryData:
  84. return QVariant::fromValue< QObject* >( p );
  85. default:
  86. return QVariant();
  87. }
  88. }
  89. return QVariant();
  90. }
  91. bool
  92. SipModel::setData( const QModelIndex& index, const QVariant& value, int role )
  93. {
  94. Q_ASSERT( index.isValid() && index.row() <= SipHandler::instance()->allPlugins().count() );
  95. if( role == Qt::CheckStateRole ) {
  96. Qt::CheckState state = static_cast< Qt::CheckState >( value.toInt() );
  97. QList< SipPlugin* > plugins = SipHandler::instance()->allPlugins();
  98. SipPlugin* p = plugins[ index.row() ];
  99. if( state == Qt::Checked && !SipHandler::instance()->enabledPlugins().contains( p ) ) {
  100. SipHandler::instance()->enablePlugin( p );
  101. } else if( state == Qt::Unchecked ) {
  102. SipHandler::instance()->disablePlugin( p );
  103. }
  104. dataChanged( index, index );
  105. return true;
  106. }
  107. return false;
  108. }
  109. QModelIndex
  110. SipModel::index( int row, int column, const QModelIndex& parent ) const
  111. {
  112. if( !parent.isValid() )
  113. return hasIndex( row, column, parent ) ? createIndex( row, column, 0 ) : QModelIndex();
  114. // qDebug() << "Creating index for non-top level row!";
  115. // it's a child of the Add Account, e.g. a factory
  116. if( hasIndex( row, column, parent ) ) {
  117. return createIndex( row, column, 1 /* magic */ );
  118. }
  119. return QModelIndex();
  120. }
  121. QModelIndex
  122. SipModel::parent( const QModelIndex& child ) const
  123. {
  124. if( !child.isValid() )
  125. return QModelIndex();
  126. if( child.internalId() == 1 ) {
  127. return index( SipHandler::instance()->allPlugins().size(), 0, QModelIndex() );
  128. }
  129. return QModelIndex();
  130. }
  131. int
  132. SipModel::rowCount( const QModelIndex& parent ) const
  133. {
  134. if( !parent.isValid() ) // invalid root node
  135. return SipHandler::instance()->allPlugins().size() /* TODO inline factories disabled + 1*/;
  136. if( parent.isValid() && !parent.parent().isValid() ) { // top level item
  137. if( parent.row() == SipHandler::instance()->allPlugins().count() ) {// last row, this is the factory
  138. return m_factories.count();
  139. }
  140. }
  141. return 0;
  142. }
  143. int
  144. SipModel::columnCount(const QModelIndex& parent) const
  145. {
  146. Q_UNUSED( parent );
  147. return 1;
  148. }
  149. Qt::ItemFlags
  150. SipModel::flags( const QModelIndex& index ) const
  151. {
  152. if( index.data( SipModel::FactoryRole ).toBool() || index.data( SipModel::FactoryItemRole ).toBool() )
  153. return QAbstractItemModel::flags( index ) & ~Qt::ItemIsSelectable;
  154. return QAbstractItemModel::flags( index ) | Qt::ItemIsUserCheckable;
  155. }
  156. void
  157. SipModel::pluginAdded( SipPlugin* p )
  158. {
  159. Q_UNUSED( p );
  160. // we assume sip plugins are added at the end of the list.
  161. Q_ASSERT( SipHandler::instance()->allPlugins().last() == p );
  162. int size = SipHandler::instance()->allPlugins().count() - 1;
  163. beginInsertRows( QModelIndex(), size, size );
  164. endInsertRows();
  165. }
  166. void
  167. SipModel::pluginRemoved( SipPlugin* p )
  168. {
  169. int idx = SipHandler::instance()->allPlugins().indexOf( p );
  170. beginRemoveRows( QModelIndex(), idx, idx );
  171. endRemoveRows();
  172. }
  173. void
  174. SipModel::pluginStateChanged( SipPlugin* p )
  175. {
  176. int at = SipHandler::instance()->allPlugins().indexOf( p );
  177. QModelIndex idx = index( at, 0, QModelIndex() );
  178. emit dataChanged( idx, idx );
  179. }