mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-05 05:37:29 +02:00
Split Resolver classes into separate files
This commit is contained in:
@@ -272,6 +272,7 @@ set( libSources
|
|||||||
collection.cpp
|
collection.cpp
|
||||||
playlist.cpp
|
playlist.cpp
|
||||||
resolver.cpp
|
resolver.cpp
|
||||||
|
ExternalResolver.cpp
|
||||||
query.cpp
|
query.cpp
|
||||||
result.cpp
|
result.cpp
|
||||||
source.cpp
|
source.cpp
|
||||||
@@ -393,6 +394,7 @@ set( libHeaders
|
|||||||
collection.h
|
collection.h
|
||||||
query.h
|
query.h
|
||||||
resolver.h
|
resolver.h
|
||||||
|
ExternalResolver.h
|
||||||
result.h
|
result.h
|
||||||
source.h
|
source.h
|
||||||
sourceplaylistinterface.h
|
sourceplaylistinterface.h
|
||||||
@@ -647,8 +649,9 @@ TARGET_LINK_LIBRARIES( tomahawklib
|
|||||||
${TAGLIB_LIBRARIES}
|
${TAGLIB_LIBRARIES}
|
||||||
${CLUCENE_LIBRARIES}
|
${CLUCENE_LIBRARIES}
|
||||||
${LIBECHONEST_LIBRARY}
|
${LIBECHONEST_LIBRARY}
|
||||||
|
${QT_QTSQL_LIBRARY}
|
||||||
${QT_QTUITOOLS_LIBRARY}
|
${QT_QTUITOOLS_LIBRARY}
|
||||||
${QT_LIBRARIES}
|
${QT_QTGUI_LIBRARY}
|
||||||
${OS_SPECIFIC_LINK_LIBRARIES}
|
${OS_SPECIFIC_LINK_LIBRARIES}
|
||||||
${CMAKE_THREAD_LIBS_INIT}
|
${CMAKE_THREAD_LIBS_INIT}
|
||||||
${LINK_LIBRARIES}
|
${LINK_LIBRARIES}
|
||||||
|
128
src/libtomahawk/ExternalResolver.cpp
Normal file
128
src/libtomahawk/ExternalResolver.cpp
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||||
|
*
|
||||||
|
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
|
||||||
|
*
|
||||||
|
* Tomahawk is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Tomahawk is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "ExternalResolver.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <QMetaProperty>
|
||||||
|
#include <QBuffer>
|
||||||
|
#include <QDir>
|
||||||
|
|
||||||
|
#include "utils/logger.h"
|
||||||
|
|
||||||
|
#ifndef ENABLE_HEADLESS
|
||||||
|
#include <QIcon>
|
||||||
|
#include <QWidget>
|
||||||
|
#endif
|
||||||
|
#include <QUiLoader>
|
||||||
|
|
||||||
|
Tomahawk::ExternalResolver::ErrorState
|
||||||
|
Tomahawk::ExternalResolver::error() const
|
||||||
|
{
|
||||||
|
return NoError;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef ENABLE_HEADLESS
|
||||||
|
QVariant
|
||||||
|
Tomahawk::ExternalResolver::configMsgFromWidget( QWidget* w )
|
||||||
|
{
|
||||||
|
if( !w )
|
||||||
|
return QVariant();
|
||||||
|
|
||||||
|
// generate a qvariantmap of all the widgets in the hierarchy, and for each one include the list of properties and values
|
||||||
|
QVariantMap widgetMap;
|
||||||
|
addChildProperties( w, widgetMap );
|
||||||
|
// qDebug() << "Generated widget variant:" << widgetMap;
|
||||||
|
return widgetMap;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void
|
||||||
|
Tomahawk::ExternalResolver::addChildProperties( QObject* widget, QVariantMap& m )
|
||||||
|
{
|
||||||
|
// recursively add all properties of this widget to the map, then repeat on all children.
|
||||||
|
// bare QWidgets are boring---so skip them! They have no input that the user can set.
|
||||||
|
if( !widget || !widget->isWidgetType() )
|
||||||
|
return;
|
||||||
|
|
||||||
|
if( qstrcmp( widget->metaObject()->className(), "QWidget" ) != 0 )
|
||||||
|
{
|
||||||
|
// qDebug() << "Adding properties for this:" << widget->metaObject()->className();
|
||||||
|
// add this widget's properties
|
||||||
|
QVariantMap props;
|
||||||
|
for( int i = 0; i < widget->metaObject()->propertyCount(); i++ )
|
||||||
|
{
|
||||||
|
QString prop = widget->metaObject()->property( i ).name();
|
||||||
|
QVariant val = widget->property( prop.toLatin1() );
|
||||||
|
// clean up for QJson....
|
||||||
|
if( val.canConvert< QPixmap >() || val.canConvert< QImage >() || val.canConvert< QIcon >() )
|
||||||
|
continue;
|
||||||
|
props[ prop ] = val.toString();
|
||||||
|
// qDebug() << QString( "%1: %2" ).arg( prop ).arg( props[ prop ].toString() );
|
||||||
|
}
|
||||||
|
m[ widget->objectName() ] = props;
|
||||||
|
}
|
||||||
|
// and recurse
|
||||||
|
foreach( QObject* child, widget->children() )
|
||||||
|
addChildProperties( child, m );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
QWidget*
|
||||||
|
Tomahawk::ExternalResolver::widgetFromData( QByteArray& data, QWidget* parent )
|
||||||
|
{
|
||||||
|
if( data.isEmpty() )
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
QUiLoader l;
|
||||||
|
QBuffer b( &data );
|
||||||
|
QWidget* w = l.load( &b, parent );
|
||||||
|
|
||||||
|
return w;
|
||||||
|
}
|
||||||
|
|
||||||
|
QByteArray
|
||||||
|
Tomahawk::ExternalResolver::fixDataImagePaths( const QByteArray& data, bool compressed, const QVariantMap& images )
|
||||||
|
{
|
||||||
|
// with a list of images and image data, write each to a temp file, replace the path in the .ui file with the temp file path
|
||||||
|
QString uiFile = QString::fromUtf8( data );
|
||||||
|
foreach( const QString& filename, images.keys() )
|
||||||
|
{
|
||||||
|
if( !uiFile.contains( filename ) ) // make sure the image is used
|
||||||
|
continue;
|
||||||
|
|
||||||
|
QString fullPath = QDir::tempPath() + "/" + filename;
|
||||||
|
QFile imgF( fullPath );
|
||||||
|
if( !imgF.open( QIODevice::WriteOnly ) )
|
||||||
|
{
|
||||||
|
qWarning() << "Failed to write to temporary image in UI file:" << filename << fullPath;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
QByteArray data = images[ filename ].toByteArray();
|
||||||
|
|
||||||
|
// qDebug() << "expanding data:" << data << compressed;
|
||||||
|
data = compressed ? qUncompress( QByteArray::fromBase64( data ) ) : QByteArray::fromBase64( data );
|
||||||
|
imgF.write( data );
|
||||||
|
imgF.close();
|
||||||
|
|
||||||
|
// replace the path to the image with the real path
|
||||||
|
uiFile.replace( filename, fullPath );
|
||||||
|
}
|
||||||
|
return uiFile.toUtf8();
|
||||||
|
}
|
||||||
|
|
85
src/libtomahawk/ExternalResolver.h
Normal file
85
src/libtomahawk/ExternalResolver.h
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||||
|
*
|
||||||
|
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
|
||||||
|
*
|
||||||
|
* Tomahawk is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Tomahawk is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef EXTERNALRESOLVER_H
|
||||||
|
#define EXTERNALRESOLVER_H
|
||||||
|
|
||||||
|
|
||||||
|
#include "query.h"
|
||||||
|
#include "dllmacro.h"
|
||||||
|
#include "resolver.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
|
||||||
|
class QWidget;
|
||||||
|
|
||||||
|
namespace Tomahawk
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generic resolver object, used to manage a resolver that Tomahawk knows about
|
||||||
|
*
|
||||||
|
* You *must* start() a resolver after creating an ExternalResolver in order to use it,
|
||||||
|
* otherwise it will not do anything.
|
||||||
|
*/
|
||||||
|
class DLLEXPORT ExternalResolver : public Resolver
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
enum ErrorState {
|
||||||
|
NoError,
|
||||||
|
FileNotFound,
|
||||||
|
FailedToLoad
|
||||||
|
};
|
||||||
|
|
||||||
|
ExternalResolver( const QString& filePath ) { m_filePath = filePath; }
|
||||||
|
|
||||||
|
virtual QString filePath() const { return m_filePath; }
|
||||||
|
|
||||||
|
virtual QWidget* configUI() const = 0;
|
||||||
|
virtual void saveConfig() = 0;
|
||||||
|
|
||||||
|
virtual void reload() {} // Reloads from file (especially useful to check if file now exists)
|
||||||
|
virtual ErrorState error() const;
|
||||||
|
virtual bool running() const = 0;
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
virtual void start() = 0;
|
||||||
|
virtual void stop() = 0;
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void changed(); // if config widget was added/removed
|
||||||
|
|
||||||
|
protected:
|
||||||
|
QWidget* widgetFromData( QByteArray& data, QWidget* parent = 0 );
|
||||||
|
QVariant configMsgFromWidget( QWidget* w );
|
||||||
|
QByteArray fixDataImagePaths( const QByteArray& data, bool compressed, const QVariantMap& images );
|
||||||
|
|
||||||
|
void setFilePath( const QString& path ) { m_filePath = path; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
void addChildProperties( QObject* parent, QVariantMap& m );
|
||||||
|
|
||||||
|
QString m_filePath;
|
||||||
|
};
|
||||||
|
|
||||||
|
}; //ns
|
||||||
|
|
||||||
|
#endif // EXTERNALRESOLVER_H
|
@@ -22,6 +22,7 @@
|
|||||||
|
|
||||||
#include "functimeout.h"
|
#include "functimeout.h"
|
||||||
#include "database/database.h"
|
#include "database/database.h"
|
||||||
|
#include "ExternalResolver.h"
|
||||||
#include "resolvers/scriptresolver.h"
|
#include "resolvers/scriptresolver.h"
|
||||||
#include "resolvers/qtscriptresolver.h"
|
#include "resolvers/qtscriptresolver.h"
|
||||||
|
|
||||||
|
@@ -1,128 +0,0 @@
|
|||||||
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
|
||||||
*
|
|
||||||
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
|
|
||||||
*
|
|
||||||
* Tomahawk is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* Tomahawk is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "resolver.h"
|
|
||||||
|
|
||||||
|
|
||||||
#include <QMetaProperty>
|
|
||||||
#include <QBuffer>
|
|
||||||
#include <QDir>
|
|
||||||
|
|
||||||
#include "utils/logger.h"
|
|
||||||
|
|
||||||
#ifndef ENABLE_HEADLESS
|
|
||||||
#include <QIcon>
|
|
||||||
#include <QWidget>
|
|
||||||
#endif
|
|
||||||
#include <QUiLoader>
|
|
||||||
|
|
||||||
Tomahawk::ExternalResolver::ErrorState
|
|
||||||
Tomahawk::ExternalResolver::error() const
|
|
||||||
{
|
|
||||||
return NoError;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifndef ENABLE_HEADLESS
|
|
||||||
QVariant
|
|
||||||
Tomahawk::ExternalResolver::configMsgFromWidget( QWidget* w )
|
|
||||||
{
|
|
||||||
if( !w )
|
|
||||||
return QVariant();
|
|
||||||
|
|
||||||
// generate a qvariantmap of all the widgets in the hierarchy, and for each one include the list of properties and values
|
|
||||||
QVariantMap widgetMap;
|
|
||||||
addChildProperties( w, widgetMap );
|
|
||||||
// qDebug() << "Generated widget variant:" << widgetMap;
|
|
||||||
return widgetMap;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void
|
|
||||||
Tomahawk::ExternalResolver::addChildProperties( QObject* widget, QVariantMap& m )
|
|
||||||
{
|
|
||||||
// recursively add all properties of this widget to the map, then repeat on all children.
|
|
||||||
// bare QWidgets are boring---so skip them! They have no input that the user can set.
|
|
||||||
if( !widget || !widget->isWidgetType() )
|
|
||||||
return;
|
|
||||||
|
|
||||||
if( qstrcmp( widget->metaObject()->className(), "QWidget" ) != 0 )
|
|
||||||
{
|
|
||||||
// qDebug() << "Adding properties for this:" << widget->metaObject()->className();
|
|
||||||
// add this widget's properties
|
|
||||||
QVariantMap props;
|
|
||||||
for( int i = 0; i < widget->metaObject()->propertyCount(); i++ )
|
|
||||||
{
|
|
||||||
QString prop = widget->metaObject()->property( i ).name();
|
|
||||||
QVariant val = widget->property( prop.toLatin1() );
|
|
||||||
// clean up for QJson....
|
|
||||||
if( val.canConvert< QPixmap >() || val.canConvert< QImage >() || val.canConvert< QIcon >() )
|
|
||||||
continue;
|
|
||||||
props[ prop ] = val.toString();
|
|
||||||
// qDebug() << QString( "%1: %2" ).arg( prop ).arg( props[ prop ].toString() );
|
|
||||||
}
|
|
||||||
m[ widget->objectName() ] = props;
|
|
||||||
}
|
|
||||||
// and recurse
|
|
||||||
foreach( QObject* child, widget->children() )
|
|
||||||
addChildProperties( child, m );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
QWidget*
|
|
||||||
Tomahawk::ExternalResolver::widgetFromData( QByteArray& data, QWidget* parent )
|
|
||||||
{
|
|
||||||
if( data.isEmpty() )
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
QUiLoader l;
|
|
||||||
QBuffer b( &data );
|
|
||||||
QWidget* w = l.load( &b, parent );
|
|
||||||
|
|
||||||
return w;
|
|
||||||
}
|
|
||||||
|
|
||||||
QByteArray
|
|
||||||
Tomahawk::ExternalResolver::fixDataImagePaths( const QByteArray& data, bool compressed, const QVariantMap& images )
|
|
||||||
{
|
|
||||||
// with a list of images and image data, write each to a temp file, replace the path in the .ui file with the temp file path
|
|
||||||
QString uiFile = QString::fromUtf8( data );
|
|
||||||
foreach( const QString& filename, images.keys() )
|
|
||||||
{
|
|
||||||
if( !uiFile.contains( filename ) ) // make sure the image is used
|
|
||||||
continue;
|
|
||||||
|
|
||||||
QString fullPath = QDir::tempPath() + "/" + filename;
|
|
||||||
QFile imgF( fullPath );
|
|
||||||
if( !imgF.open( QIODevice::WriteOnly ) )
|
|
||||||
{
|
|
||||||
qWarning() << "Failed to write to temporary image in UI file:" << filename << fullPath;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
QByteArray data = images[ filename ].toByteArray();
|
|
||||||
|
|
||||||
// qDebug() << "expanding data:" << data << compressed;
|
|
||||||
data = compressed ? qUncompress( QByteArray::fromBase64( data ) ) : QByteArray::fromBase64( data );
|
|
||||||
imgF.write( data );
|
|
||||||
imgF.close();
|
|
||||||
|
|
||||||
// replace the path to the image with the real path
|
|
||||||
uiFile.replace( filename, fullPath );
|
|
||||||
}
|
|
||||||
return uiFile.toUtf8();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@@ -34,8 +34,6 @@
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class QWidget;
|
|
||||||
|
|
||||||
namespace Tomahawk
|
namespace Tomahawk
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -54,54 +52,6 @@ public slots:
|
|||||||
virtual void resolve( const Tomahawk::query_ptr& query ) = 0;
|
virtual void resolve( const Tomahawk::query_ptr& query ) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Generic resolver object, used to manage a resolver that Tomahawk knows about
|
|
||||||
*
|
|
||||||
* You *must* start() a resolver after creating an ExternalResolver in order to use it,
|
|
||||||
* otherwise it will not do anything.
|
|
||||||
*/
|
|
||||||
class DLLEXPORT ExternalResolver : public Resolver
|
|
||||||
{
|
|
||||||
Q_OBJECT
|
|
||||||
|
|
||||||
public:
|
|
||||||
enum ErrorState {
|
|
||||||
NoError,
|
|
||||||
FileNotFound,
|
|
||||||
FailedToLoad
|
|
||||||
};
|
|
||||||
|
|
||||||
ExternalResolver( const QString& filePath ) { m_filePath = filePath; }
|
|
||||||
|
|
||||||
virtual QString filePath() const { return m_filePath; }
|
|
||||||
|
|
||||||
virtual QWidget* configUI() const = 0;
|
|
||||||
virtual void saveConfig() = 0;
|
|
||||||
|
|
||||||
virtual void reload() {} // Reloads from file (especially useful to check if file now exists)
|
|
||||||
virtual ErrorState error() const;
|
|
||||||
virtual bool running() const = 0;
|
|
||||||
|
|
||||||
public slots:
|
|
||||||
virtual void start() = 0;
|
|
||||||
virtual void stop() = 0;
|
|
||||||
|
|
||||||
signals:
|
|
||||||
void changed(); // if config widget was added/removed
|
|
||||||
|
|
||||||
protected:
|
|
||||||
QWidget* widgetFromData( QByteArray& data, QWidget* parent = 0 );
|
|
||||||
QVariant configMsgFromWidget( QWidget* w );
|
|
||||||
QByteArray fixDataImagePaths( const QByteArray& data, bool compressed, const QVariantMap& images );
|
|
||||||
|
|
||||||
void setFilePath( const QString& path ) { m_filePath = path; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
void addChildProperties( QObject* parent, QVariantMap& m );
|
|
||||||
|
|
||||||
QString m_filePath;
|
|
||||||
};
|
|
||||||
|
|
||||||
}; //ns
|
}; //ns
|
||||||
|
|
||||||
#endif // RESOLVER_H
|
#endif // RESOLVER_H
|
||||||
|
Reference in New Issue
Block a user