PageRenderTime 104ms CodeModel.GetById 41ms app.highlight 29ms RepoModel.GetById 31ms app.codeStats 0ms

/src/resolversmodel.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 251 lines | 192 code | 42 blank | 17 comment | 31 complexity | e147ecf68b3c8cf20be92d3f466eaf44 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 "resolversmodel.h"
 20
 21#include <QFileInfo>
 22
 23#include "tomahawksettings.h"
 24#include "tomahawkapp.h"
 25#include "ExternalResolverGui.h"
 26#include "pipeline.h"
 27#include "config.h"
 28#include "AtticaManager.h"
 29#include "utils/logger.h"
 30
 31ResolversModel::ResolversModel( QObject* parent )
 32    : QAbstractListModel( parent )
 33{
 34     addInstalledResolvers();
 35}
 36
 37
 38ResolversModel::~ResolversModel()
 39{
 40
 41}
 42
 43
 44QVariant
 45ResolversModel::data( const QModelIndex& index, int role ) const
 46{
 47    if( !index.isValid() || !hasIndex( index.row(), index.column(), QModelIndex() ) )
 48        return QVariant();
 49
 50    Tomahawk::ExternalResolver* r = Tomahawk::Pipeline::instance()->scriptResolvers().at( index.row() );
 51    Tomahawk::ExternalResolverGui* res = qobject_cast< Tomahawk::ExternalResolverGui* >( r );
 52    Q_ASSERT(res); // this is part of the gui, so there should be no non-gui resolvers
 53    switch( role )
 54    {
 55    case Qt::DisplayRole:
 56    case ResolversModel::ResolverName:
 57        return res->name();
 58    case ResolversModel::ResolverPath:
 59        return res->filePath();
 60    case ResolversModel::HasConfig:
 61        return res->configUI() != 0;
 62    case ResolversModel::ErrorState:
 63        return res->error();
 64    case Qt::CheckStateRole:
 65        return res->running() ? Qt::Checked : Qt::Unchecked;
 66    case Qt::ToolTipRole:
 67        return res->filePath();
 68    default:
 69        return QVariant();
 70    }
 71}
 72
 73
 74bool
 75ResolversModel::setData( const QModelIndex& index, const QVariant& value, int role )
 76{
 77    if ( !hasIndex( index.row(), index.column(), QModelIndex() ) )
 78        return false;
 79
 80    Tomahawk::ExternalResolver* r = Tomahawk::Pipeline::instance()->scriptResolvers().at( index.row() );
 81    if ( r && r->error() == Tomahawk::ExternalResolver::FileNotFound )  // give it a shot to see if the user manually fixed paths
 82    {
 83        r->reload();
 84
 85        if( r->error() == Tomahawk::ExternalResolver::FileNotFound ) // Nope, no luck. Doesn't exist on disk, don't let user mess with it
 86            return false;
 87    }
 88    else if ( !r && !QFile::exists( r->filePath() ) )
 89    {
 90        return false;
 91    }
 92
 93    if ( role == Qt::CheckStateRole )
 94    {
 95        Qt::CheckState state = static_cast< Qt::CheckState >( value.toInt() );
 96
 97        if ( state == Qt::Checked && !r->running() ) {
 98            r->start();
 99        }
100        else if ( state == Qt::Unchecked )
101        {
102            r->stop();
103        }
104
105        emit dataChanged( index, index );
106        return true;
107    }
108    return false;
109}
110
111
112int
113ResolversModel::rowCount( const QModelIndex& parent ) const
114{
115    Q_UNUSED( parent );
116    return Tomahawk::Pipeline::instance()->scriptResolvers().count();
117}
118
119
120int
121ResolversModel::columnCount(const QModelIndex& parent) const
122{
123    Q_UNUSED( parent );
124    return 1;
125}
126
127
128Qt::ItemFlags
129ResolversModel::flags( const QModelIndex& index ) const
130{
131    return QAbstractItemModel::flags(index) | Qt::ItemIsUserCheckable;
132}
133
134
135void
136ResolversModel::addResolver( const QString& resolver, bool enable )
137{
138    const int count = rowCount( QModelIndex() );
139    beginInsertRows( QModelIndex(), count, count );
140    Tomahawk::ExternalResolver* r = Tomahawk::Pipeline::instance()->addScriptResolver( resolver, enable );
141    Tomahawk::ExternalResolverGui* res = qobject_cast< Tomahawk::ExternalResolverGui* >( r );
142    Q_ASSERT(res); // this is part of the gui, so there should be no non-gui resolvers
143    connect( res, SIGNAL( changed() ), this, SLOT( resolverChanged() ) );
144    endInsertRows();
145
146    if ( res->configUI() )
147        emit openConfig( res->filePath() );
148    else
149        m_waitingForLoad << resolver;
150}
151
152
153void
154ResolversModel::atticaResolverInstalled( const QString& resolverId )
155{
156#ifdef LIBATTICA_FOUND
157    Tomahawk::ExternalResolver* r = Tomahawk::Pipeline::instance()->resolverForPath( AtticaManager::instance()->pathFromId( resolverId ) );
158    if ( !r )
159        return;
160    const int idx = Tomahawk::Pipeline::instance()->scriptResolvers().indexOf( r );
161    if ( idx >= 0 )
162    {
163        beginInsertRows( QModelIndex(), idx, idx );
164        endInsertRows();
165    }
166#endif
167}
168
169
170void
171ResolversModel::removeResolver( const QString& resolver )
172{
173    const int idx = Tomahawk::Pipeline::instance()->scriptResolvers().indexOf( Tomahawk::Pipeline::instance()->resolverForPath( resolver ) );
174    if ( idx < 0 )
175        return;
176
177    beginRemoveRows( QModelIndex(), idx, idx );
178    Tomahawk::Pipeline::instance()->removeScriptResolver( resolver );
179    endRemoveRows();
180}
181
182
183void
184ResolversModel::resolverChanged()
185{
186    Tomahawk::ExternalResolver* res = qobject_cast< Tomahawk::ExternalResolver* >( sender() );
187    Q_ASSERT( res );
188
189    if ( Tomahawk::Pipeline::instance()->scriptResolvers().contains( res ) )
190    {
191        qDebug() << "Got resolverChanged signal, does it have a config UI yet?" << qobject_cast< Tomahawk::ExternalResolverGui* >(res)->configUI();
192        const QModelIndex idx = index( Tomahawk::Pipeline::instance()->scriptResolvers().indexOf( res ), 0, QModelIndex() );
193        emit dataChanged( idx, idx );
194
195        if ( m_waitingForLoad.contains( res->filePath() ) )
196        {
197            m_waitingForLoad.remove( res->filePath() );
198            emit openConfig( res->filePath() );
199        }
200    }
201}
202
203
204void
205ResolversModel::addInstalledResolvers()
206{
207    QList< QDir > pluginDirs;
208
209    QDir appDir( qApp->applicationDirPath() );
210    QDir libDir( CMAKE_INSTALL_PREFIX "/lib" );
211    QDir libexecDir( CMAKE_INSTALL_FULL_LIBEXECDIR );
212
213    QDir lib64Dir( appDir );
214    lib64Dir.cdUp();
215    lib64Dir.cd( "lib64" );
216
217    pluginDirs << appDir << libDir << lib64Dir << libexecDir;
218    foreach ( const QDir& pluginDir, pluginDirs )
219    {
220        qDebug() << "Checking directory for resolvers:" << pluginDir;
221        foreach ( QString fileName, pluginDir.entryList( QStringList() << "*_tomahawkresolver*", QDir::Files ) ){
222            if ( fileName.contains( "_tomahawkresolver" ) ) {
223                const QString path = pluginDir.absoluteFilePath( fileName );
224                bool found = false;
225                foreach ( Tomahawk::ExternalResolver* res, Tomahawk::Pipeline::instance()->scriptResolvers() )
226                {
227                    if ( res->filePath() == path )
228                        found = true;
229                }
230                if ( !found ) {
231                    Tomahawk::Pipeline::instance()->addScriptResolver( path, false );
232                }
233            }
234        }
235    }
236}
237
238
239void
240ResolversModel::saveScriptResolvers()
241{
242    QStringList enabled, all;
243    foreach ( Tomahawk::ExternalResolver* res, Tomahawk::Pipeline::instance()->scriptResolvers() )
244    {
245        all << res->filePath();
246        if ( res->running() )
247            enabled << res->filePath();
248    }
249    TomahawkSettings::instance()->setAllScriptResolvers( all );
250    TomahawkSettings::instance()->setEnabledScriptResolvers( enabled );
251}