diff --git a/src/libtomahawk/CMakeLists.txt b/src/libtomahawk/CMakeLists.txt index ace74383f..ed838d840 100644 --- a/src/libtomahawk/CMakeLists.txt +++ b/src/libtomahawk/CMakeLists.txt @@ -272,6 +272,7 @@ set( libSources collection.cpp playlist.cpp resolver.cpp + ExternalResolver.cpp query.cpp result.cpp source.cpp @@ -393,6 +394,7 @@ set( libHeaders collection.h query.h resolver.h + ExternalResolver.h result.h source.h sourceplaylistinterface.h @@ -647,8 +649,9 @@ TARGET_LINK_LIBRARIES( tomahawklib ${TAGLIB_LIBRARIES} ${CLUCENE_LIBRARIES} ${LIBECHONEST_LIBRARY} + ${QT_QTSQL_LIBRARY} ${QT_QTUITOOLS_LIBRARY} - ${QT_LIBRARIES} + ${QT_QTGUI_LIBRARY} ${OS_SPECIFIC_LINK_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${LINK_LIBRARIES} diff --git a/src/libtomahawk/ExternalResolver.cpp b/src/libtomahawk/ExternalResolver.cpp new file mode 100644 index 000000000..d0ab1689e --- /dev/null +++ b/src/libtomahawk/ExternalResolver.cpp @@ -0,0 +1,128 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Christian Muehlhaeuser + * + * 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 . + */ + +#include "ExternalResolver.h" + + +#include +#include +#include + +#include "utils/logger.h" + +#ifndef ENABLE_HEADLESS + #include + #include +#endif +#include + +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(); +} + diff --git a/src/libtomahawk/ExternalResolver.h b/src/libtomahawk/ExternalResolver.h new file mode 100644 index 000000000..2cedce15d --- /dev/null +++ b/src/libtomahawk/ExternalResolver.h @@ -0,0 +1,85 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Christian Muehlhaeuser + * + * 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 . + */ + +#ifndef EXTERNALRESOLVER_H +#define EXTERNALRESOLVER_H + + +#include "query.h" +#include "dllmacro.h" +#include "resolver.h" + + +#include + +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 diff --git a/src/libtomahawk/pipeline.cpp b/src/libtomahawk/pipeline.cpp index b5a4d89b5..af0b271e8 100644 --- a/src/libtomahawk/pipeline.cpp +++ b/src/libtomahawk/pipeline.cpp @@ -22,6 +22,7 @@ #include "functimeout.h" #include "database/database.h" +#include "ExternalResolver.h" #include "resolvers/scriptresolver.h" #include "resolvers/qtscriptresolver.h" diff --git a/src/libtomahawk/resolver.cpp b/src/libtomahawk/resolver.cpp index 55eb02a76..e69de29bb 100644 --- a/src/libtomahawk/resolver.cpp +++ b/src/libtomahawk/resolver.cpp @@ -1,128 +0,0 @@ -/* === This file is part of Tomahawk Player - === - * - * Copyright 2010-2011, Christian Muehlhaeuser - * - * 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 . - */ - -#include "resolver.h" - - -#include -#include -#include - -#include "utils/logger.h" - -#ifndef ENABLE_HEADLESS - #include - #include -#endif -#include - -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(); -} - diff --git a/src/libtomahawk/resolver.h b/src/libtomahawk/resolver.h index 6694224fe..ec5337ec4 100644 --- a/src/libtomahawk/resolver.h +++ b/src/libtomahawk/resolver.h @@ -34,8 +34,6 @@ */ -class QWidget; - namespace Tomahawk { @@ -54,54 +52,6 @@ public slots: 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 #endif // RESOLVER_H