From 83051cd554004be2592025d2acb809586eaf6fdb Mon Sep 17 00:00:00 2001 From: Dominik Schmidt Date: Mon, 5 Dec 2011 14:24:07 +0100 Subject: [PATCH] Split ExternalResolver --- src/libtomahawk/CMakeLists.txt | 2 + src/libtomahawk/ExternalResolver.cpp | 101 -------------- src/libtomahawk/ExternalResolver.h | 9 +- src/libtomahawk/ExternalResolverGui.cpp | 124 ++++++++++++++++++ src/libtomahawk/ExternalResolverGui.h | 55 ++++++++ .../resolvers/qtscriptresolver.cpp | 2 +- src/libtomahawk/resolvers/qtscriptresolver.h | 4 +- src/libtomahawk/resolvers/scriptresolver.cpp | 2 +- src/libtomahawk/resolvers/scriptresolver.h | 4 +- src/resolverconfigdelegate.cpp | 2 +- src/resolversmodel.cpp | 12 +- src/settingsdialog.cpp | 6 +- 12 files changed, 201 insertions(+), 122 deletions(-) create mode 100644 src/libtomahawk/ExternalResolverGui.cpp create mode 100644 src/libtomahawk/ExternalResolverGui.h diff --git a/src/libtomahawk/CMakeLists.txt b/src/libtomahawk/CMakeLists.txt index ed838d840..3c275a63b 100644 --- a/src/libtomahawk/CMakeLists.txt +++ b/src/libtomahawk/CMakeLists.txt @@ -88,6 +88,7 @@ set( libGuiSources playlist/topbar/lineedit.cpp playlist/topbar/searchbutton.cpp + ExternalResolverGui.cpp resolvers/scriptresolver.cpp resolvers/qtscriptresolver.cpp @@ -204,6 +205,7 @@ set( libGuiHeaders playlist/dynamic/widgets/DynamicSetupWidget.h playlist/dynamic/widgets/LoadingSpinner.h + ExternalResolverGui.h resolvers/scriptresolver.h resolvers/qtscriptresolver.h diff --git a/src/libtomahawk/ExternalResolver.cpp b/src/libtomahawk/ExternalResolver.cpp index d0ab1689e..1906f358b 100644 --- a/src/libtomahawk/ExternalResolver.cpp +++ b/src/libtomahawk/ExternalResolver.cpp @@ -18,111 +18,10 @@ #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 index 2cedce15d..a63f42396 100644 --- a/src/libtomahawk/ExternalResolver.h +++ b/src/libtomahawk/ExternalResolver.h @@ -53,7 +53,6 @@ public: 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) @@ -68,18 +67,12 @@ 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 +#endif // EXTERNALESOLVER_H diff --git a/src/libtomahawk/ExternalResolverGui.cpp b/src/libtomahawk/ExternalResolverGui.cpp new file mode 100644 index 000000000..63f5392c7 --- /dev/null +++ b/src/libtomahawk/ExternalResolverGui.cpp @@ -0,0 +1,124 @@ +/* === 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 "ExternalResolverGui.h" + + +#include +#include +#include + +#include +#include +#include +#include "utils/logger.h" + +Tomahawk::ExternalResolverGui::ExternalResolverGui(const QString& filePath) + : Tomahawk::ExternalResolver(filePath) +{ +} + + +QVariant +Tomahawk::ExternalResolverGui::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; +} + + +void +Tomahawk::ExternalResolverGui::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::ExternalResolverGui::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::ExternalResolverGui::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/ExternalResolverGui.h b/src/libtomahawk/ExternalResolverGui.h new file mode 100644 index 000000000..04c370ede --- /dev/null +++ b/src/libtomahawk/ExternalResolverGui.h @@ -0,0 +1,55 @@ +/* === 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 EXTERNALRESOLVERGUI_H +#define EXTERNALRESOLVERGUI_H + +#include "ExternalResolver.h" +#include "dllmacro.h" + +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 ExternalResolverGui : public ExternalResolver +{ +Q_OBJECT + +public: + ExternalResolverGui( const QString& filePath ); + virtual QWidget* configUI() const = 0; + +protected: + QWidget* widgetFromData( QByteArray& data, QWidget* parent = 0 ); + QVariant configMsgFromWidget( QWidget* w ); + QByteArray fixDataImagePaths( const QByteArray& data, bool compressed, const QVariantMap& images ); + +private: + void addChildProperties( QObject* parent, QVariantMap& m ); +}; + +}; //ns + +#endif // RESOLVER_H diff --git a/src/libtomahawk/resolvers/qtscriptresolver.cpp b/src/libtomahawk/resolvers/qtscriptresolver.cpp index 2481be2a0..d518c15f2 100644 --- a/src/libtomahawk/resolvers/qtscriptresolver.cpp +++ b/src/libtomahawk/resolvers/qtscriptresolver.cpp @@ -201,7 +201,7 @@ ScriptEngine::javaScriptConsoleMessage( const QString& message, int lineNumber, QtScriptResolver::QtScriptResolver( const QString& scriptPath ) - : Tomahawk::ExternalResolver( scriptPath ) + : Tomahawk::ExternalResolverGui( scriptPath ) , m_ready( false ) , m_stopped( true ) , m_error( Tomahawk::ExternalResolver::NoError ) diff --git a/src/libtomahawk/resolvers/qtscriptresolver.h b/src/libtomahawk/resolvers/qtscriptresolver.h index c7fcba1f4..00c453c94 100644 --- a/src/libtomahawk/resolvers/qtscriptresolver.h +++ b/src/libtomahawk/resolvers/qtscriptresolver.h @@ -19,7 +19,7 @@ #ifndef QTSCRIPTRESOLVER_H #define QTSCRIPTRESOLVER_H -#include "resolver.h" +#include "ExternalResolverGui.h" #include "query.h" #include "utils/tomahawkutils.h" #include "config.h" @@ -114,7 +114,7 @@ private: }; -class DLLEXPORT QtScriptResolver : public Tomahawk::ExternalResolver +class DLLEXPORT QtScriptResolver : public Tomahawk::ExternalResolverGui { Q_OBJECT diff --git a/src/libtomahawk/resolvers/scriptresolver.cpp b/src/libtomahawk/resolvers/scriptresolver.cpp index 7d7bd1fd5..29a259906 100644 --- a/src/libtomahawk/resolvers/scriptresolver.cpp +++ b/src/libtomahawk/resolvers/scriptresolver.cpp @@ -32,7 +32,7 @@ ScriptResolver::ScriptResolver( const QString& exe ) - : Tomahawk::ExternalResolver( exe ) + : Tomahawk::ExternalResolverGui( exe ) , m_num_restarts( 0 ) , m_msgsize( 0 ) , m_ready( false ) diff --git a/src/libtomahawk/resolvers/scriptresolver.h b/src/libtomahawk/resolvers/scriptresolver.h index e635089ec..9191396be 100644 --- a/src/libtomahawk/resolvers/scriptresolver.h +++ b/src/libtomahawk/resolvers/scriptresolver.h @@ -26,13 +26,13 @@ #include #include "query.h" -#include "resolver.h" +#include "ExternalResolverGui.h" #include "dllmacro.h" class QWidget; -class DLLEXPORT ScriptResolver : public Tomahawk::ExternalResolver +class DLLEXPORT ScriptResolver : public Tomahawk::ExternalResolverGui { Q_OBJECT diff --git a/src/resolverconfigdelegate.cpp b/src/resolverconfigdelegate.cpp index 9ff6c8f1a..5df004827 100644 --- a/src/resolverconfigdelegate.cpp +++ b/src/resolverconfigdelegate.cpp @@ -19,7 +19,7 @@ #include "resolverconfigdelegate.h" #include "resolversmodel.h" -#include "resolver.h" +#include "ExternalResolverGui.h" #include #include diff --git a/src/resolversmodel.cpp b/src/resolversmodel.cpp index 1f80052c4..b27e00791 100644 --- a/src/resolversmodel.cpp +++ b/src/resolversmodel.cpp @@ -22,7 +22,7 @@ #include "tomahawksettings.h" #include "tomahawkapp.h" -#include "resolver.h" +#include "ExternalResolverGui.h" #include "pipeline.h" #include "config.h" #include "AtticaManager.h" @@ -47,7 +47,9 @@ ResolversModel::data( const QModelIndex& index, int role ) const if( !index.isValid() || !hasIndex( index.row(), index.column(), QModelIndex() ) ) return QVariant(); - Tomahawk::ExternalResolver* res = Tomahawk::Pipeline::instance()->scriptResolvers().at( index.row() ); + Tomahawk::ExternalResolver* r = Tomahawk::Pipeline::instance()->scriptResolvers().at( index.row() ); + Tomahawk::ExternalResolverGui* res = qobject_cast< Tomahawk::ExternalResolverGui* >( r ); + Q_ASSERT(res); // this is part of the gui, so there should be no non-gui resolvers switch( role ) { case Qt::DisplayRole: @@ -135,7 +137,9 @@ ResolversModel::addResolver( const QString& resolver, bool enable ) { const int count = rowCount( QModelIndex() ); beginInsertRows( QModelIndex(), count, count ); - Tomahawk::ExternalResolver* res = Tomahawk::Pipeline::instance()->addScriptResolver( resolver, enable ); + Tomahawk::ExternalResolver* r = Tomahawk::Pipeline::instance()->addScriptResolver( resolver, enable ); + Tomahawk::ExternalResolverGui* res = qobject_cast< Tomahawk::ExternalResolverGui* >( r ); + Q_ASSERT(res); // this is part of the gui, so there should be no non-gui resolvers connect( res, SIGNAL( changed() ), this, SLOT( resolverChanged() ) ); endInsertRows(); @@ -184,7 +188,7 @@ ResolversModel::resolverChanged() if ( Tomahawk::Pipeline::instance()->scriptResolvers().contains( res ) ) { - qDebug() << "Got resolverChanged signal, does it have a config UI yet?" << res->configUI(); + qDebug() << "Got resolverChanged signal, does it have a config UI yet?" << qobject_cast< Tomahawk::ExternalResolverGui* >(res)->configUI(); const QModelIndex idx = index( Tomahawk::Pipeline::instance()->scriptResolvers().indexOf( res ), 0, QModelIndex() ); emit dataChanged( idx, idx ); diff --git a/src/settingsdialog.cpp b/src/settingsdialog.cpp index 80d02067c..e79c3d6e7 100644 --- a/src/settingsdialog.cpp +++ b/src/settingsdialog.cpp @@ -43,6 +43,7 @@ #include "musicscanner.h" #include "pipeline.h" #include "resolver.h" +#include "ExternalResolverGui.h" #include "resolverconfigdelegate.h" #include "resolversmodel.h" #include "scanmanager.h" @@ -563,10 +564,11 @@ void SettingsDialog::openResolverConfig( const QString& resolver ) { Tomahawk::ExternalResolver* r = Tomahawk::Pipeline::instance()->resolverForPath( resolver ); - if( r && r->configUI() ) + Tomahawk::ExternalResolverGui* res = qobject_cast< Tomahawk::ExternalResolverGui* >( r ); + if( res && res->configUI() ) { #ifndef Q_WS_MAC - DelegateConfigWrapper dialog( r->configUI(), "Resolver Configuration", this ); + DelegateConfigWrapper dialog( res->configUI(), "Resolver Configuration", this ); QWeakPointer< DelegateConfigWrapper > watcher( &dialog ); int ret = dialog.exec(); if( !watcher.isNull() && ret == QDialog::Accepted )