PageRenderTime 37ms CodeModel.GetById 16ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/libtomahawk/sip/SipModel.cpp

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