1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-19 15:29:42 +01:00

Split Resolver classes into separate files

This commit is contained in:
Dominik Schmidt 2011-12-04 13:35:37 +01:00
parent 09087e2a8e
commit cfce10fbec
6 changed files with 218 additions and 179 deletions

View File

@ -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}

View 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();
}

View 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

View File

@ -22,6 +22,7 @@
#include "functimeout.h"
#include "database/database.h"
#include "ExternalResolver.h"
#include "resolvers/scriptresolver.h"
#include "resolvers/qtscriptresolver.h"

View File

@ -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();
}

View File

@ -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