1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-04-22 00:42:04 +02:00

Move ScriptEngine into its own files

This commit is contained in:
Uwe L. Korn 2013-06-23 10:58:12 +02:00 committed by Michael Zanetti
parent 10712c7efb
commit 0403301c9a
5 changed files with 190 additions and 120 deletions

View File

@ -99,6 +99,7 @@ set( libGuiSources
resolvers/ExternalResolverGui.cpp
resolvers/ScriptResolver.cpp
resolvers/JSResolver.cpp
resolvers/ScriptEngine.cpp
utils/ImageRegistry.cpp
utils/WidgetDragFilter.cpp

View File

@ -33,6 +33,7 @@
#include "Pipeline.h"
#include "Result.h"
#include "ScriptCollection.h"
#include "ScriptEngine.h"
#include "SourceList.h"
#include "TomahawkSettings.h"
#include "TomahawkVersion.h"
@ -1100,97 +1101,3 @@ JSResolver::resolverCollections()
// Then when there's callbacks from a resolver, it sends source name, collection id
// + data.
}
ScriptEngine::ScriptEngine( JSResolver* parent )
: QWebPage( (QObject*) parent )
, m_parent( parent )
{
settings()->setAttribute( QWebSettings::OfflineStorageDatabaseEnabled, true );
settings()->setOfflineStoragePath( TomahawkUtils::appDataDir().path() );
settings()->setAttribute(QWebSettings::LocalStorageEnabled, true );
settings()->setLocalStoragePath( TomahawkUtils::appDataDir().path() );
settings()->setAttribute( QWebSettings::LocalStorageDatabaseEnabled, true );
settings()->setAttribute( QWebSettings::LocalContentCanAccessFileUrls, true );
settings()->setAttribute( QWebSettings::LocalContentCanAccessRemoteUrls, true );
// Tomahawk is not a user agent
m_header = QWebPage::userAgentForUrl( QUrl() ).replace( QString( "%1/%2" )
.arg( TOMAHAWK_APPLICATION_NAME )
.arg( TOMAHAWK_VERSION )
,"");
tLog( LOGVERBOSE ) << "JSResolver Using header" << m_header;
connect( networkAccessManager(), SIGNAL( sslErrors( QNetworkReply*, QList<QSslError> ) ),
SLOT( sslErrorHandler( QNetworkReply*, QList<QSslError> ) ) );
}
void
ScriptEngine::javaScriptConsoleMessage( const QString& message, int lineNumber, const QString& sourceID )
{
tLog() << "JAVASCRIPT:" << m_scriptPath << message << lineNumber << sourceID;
#ifndef DEBUG_BUILD
JobStatusView::instance()->model()->addJob( new ErrorStatusMessage( tr( "Script Resolver Error: %1 %2 %3 %4" ).arg( m_scriptPath ).arg( message ).arg( lineNumber ).arg( sourceID ) ) );
#endif
}
void
ScriptEngine::sslErrorHandler( QNetworkReply* qnr, const QList<QSslError>& errlist )
{
tDebug() << Q_FUNC_INFO;
QByteArray digest = errlist.first().certificate().digest();
int result = -1;
if ( !TomahawkSettings::instance()->isSslCertKnown( digest ) )
{
foreach ( const QSslError& err, errlist )
tDebug() << Q_FUNC_INFO << "SSL error:" << err;
QMessageBox question( TomahawkUtils::tomahawkWindow() );
question.setWindowTitle( tr( "SSL Error" ) );
question.setText( tr( "You have asked Tomahawk to connect securely to <b>%1</b>, but we can't confirm that your connection is secure:<br><br>"
"<b>%2</b><br><br>"
"Do you want to trust this connection?" )
.arg( qnr->url().host() )
.arg( errlist.first().errorString() ) );
question.setStandardButtons( QMessageBox::No );
question.addButton( tr( "Trust certificate" ), QMessageBox::AcceptRole );
result = question.exec();
//FIXME: discuss whether we want to store rejects, too (needs settings management to remove the decision?)
if ( result == QMessageBox::AcceptRole )
TomahawkSettings::instance()->setSslCertTrusted( digest, result == QMessageBox::AcceptRole );
}
if ( TomahawkSettings::instance()->isSslCertTrusted( digest ) )
{
qnr->ignoreSslErrors();
}
}
QString
ScriptEngine::userAgentForUrl( const QUrl& url ) const
{
Q_UNUSED( url );
return m_header;
}
void
ScriptEngine::setScriptPath( const QString& scriptPath )
{
m_scriptPath = scriptPath;
}
bool
ScriptEngine::shouldInterruptJavaScript()
{
return true;
}

View File

@ -3,6 +3,7 @@
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
* Copyright 2013, Teo Mrnjavac <teo@kde.org>
* Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -36,6 +37,7 @@
#include "DllMacro.h"
class JSResolver;
class ScriptEngine;
class DLLEXPORT JSResolverHelper : public QObject
{
@ -86,32 +88,6 @@ private:
JSResolver* m_resolver;
};
class DLLEXPORT ScriptEngine : public QWebPage
{
Q_OBJECT
public:
explicit ScriptEngine( JSResolver* parent );
QString userAgentForUrl( const QUrl& url ) const;
void setScriptPath( const QString& scriptPath );
public slots:
bool shouldInterruptJavaScript();
protected:
virtual void javaScriptConsoleMessage( const QString& message, int lineNumber, const QString& sourceID );
private slots:
void sslErrorHandler( QNetworkReply* qnr, const QList<QSslError>& errlist );
private:
JSResolver* m_parent;
QString m_scriptPath;
QString m_header;
};
class DLLEXPORT JSResolver : public Tomahawk::ExternalResolverGui
{
Q_OBJECT

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>
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
* Copyright 2013, Teo Mrnjavac <teo@kde.org>
* Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
*
* 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 "ScriptEngine.h"
#include "jobview/ErrorStatusMessage.h"
#include "jobview/JobStatusModel.h"
#include "jobview/JobStatusView.h"
#include "utils/Logger.h"
#include "utils/TomahawkUtils.h"
#include "utils/TomahawkUtilsGui.h"
#include "TomahawkSettings.h"
#include "TomahawkVersion.h"
#include <QDir>
#include <QMessageBox>
ScriptEngine::ScriptEngine( JSResolver* parent )
: QWebPage( (QObject*) parent )
, m_parent( parent )
{
settings()->setAttribute( QWebSettings::OfflineStorageDatabaseEnabled, true );
settings()->setOfflineStoragePath( TomahawkUtils::appDataDir().path() );
settings()->setAttribute(QWebSettings::LocalStorageEnabled, true );
settings()->setLocalStoragePath( TomahawkUtils::appDataDir().path() );
settings()->setAttribute( QWebSettings::LocalStorageDatabaseEnabled, true );
settings()->setAttribute( QWebSettings::LocalContentCanAccessFileUrls, true );
settings()->setAttribute( QWebSettings::LocalContentCanAccessRemoteUrls, true );
// Tomahawk is not a user agent
m_header = QWebPage::userAgentForUrl( QUrl() ).replace( QString( "%1/%2" )
.arg( TOMAHAWK_APPLICATION_NAME )
.arg( TOMAHAWK_VERSION )
,"");
tLog( LOGVERBOSE ) << "JSResolver Using header" << m_header;
connect( networkAccessManager(), SIGNAL( sslErrors( QNetworkReply*, QList<QSslError> ) ),
SLOT( sslErrorHandler( QNetworkReply*, QList<QSslError> ) ) );
}
void
ScriptEngine::javaScriptConsoleMessage( const QString& message, int lineNumber, const QString& sourceID )
{
tLog() << "JAVASCRIPT:" << m_scriptPath << message << lineNumber << sourceID;
#ifndef DEBUG_BUILD
JobStatusView::instance()->model()->addJob( new ErrorStatusMessage( tr( "Script Resolver Error: %1 %2 %3 %4" ).arg( m_scriptPath ).arg( message ).arg( lineNumber ).arg( sourceID ) ) );
#endif
}
void
ScriptEngine::sslErrorHandler( QNetworkReply* qnr, const QList<QSslError>& errlist )
{
tDebug() << Q_FUNC_INFO;
QByteArray digest = errlist.first().certificate().digest();
int result = -1;
if ( !TomahawkSettings::instance()->isSslCertKnown( digest ) )
{
foreach ( const QSslError& err, errlist )
tDebug() << Q_FUNC_INFO << "SSL error:" << err;
QMessageBox question( TomahawkUtils::tomahawkWindow() );
question.setWindowTitle( tr( "SSL Error" ) );
question.setText( tr( "You have asked Tomahawk to connect securely to <b>%1</b>, but we can't confirm that your connection is secure:<br><br>"
"<b>%2</b><br><br>"
"Do you want to trust this connection?" )
.arg( qnr->url().host() )
.arg( errlist.first().errorString() ) );
question.setStandardButtons( QMessageBox::No );
question.addButton( tr( "Trust certificate" ), QMessageBox::AcceptRole );
result = question.exec();
//FIXME: discuss whether we want to store rejects, too (needs settings management to remove the decision?)
if ( result == QMessageBox::AcceptRole )
TomahawkSettings::instance()->setSslCertTrusted( digest, result == QMessageBox::AcceptRole );
}
if ( TomahawkSettings::instance()->isSslCertTrusted( digest ) )
{
qnr->ignoreSslErrors();
}
}
QString
ScriptEngine::userAgentForUrl( const QUrl& url ) const
{
Q_UNUSED( url );
return m_header;
}
void
ScriptEngine::setScriptPath( const QString& scriptPath )
{
m_scriptPath = scriptPath;
}
bool
ScriptEngine::shouldInterruptJavaScript()
{
return true;
}

View File

@ -0,0 +1,58 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
* Copyright 2013, Teo Mrnjavac <teo@kde.org>
* Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
*
* 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 SCRIPTENGINE_H
#define SCRIPTENGINE_H
#include "DllMacro.h"
#include <QSslError>
#include <QWebPage>
class JSResolver;
class QNetworkReply;
class DLLEXPORT ScriptEngine : public QWebPage
{
Q_OBJECT
public:
explicit ScriptEngine( JSResolver* parent );
QString userAgentForUrl( const QUrl& url ) const;
void setScriptPath( const QString& scriptPath );
public slots:
bool shouldInterruptJavaScript();
protected:
virtual void javaScriptConsoleMessage( const QString& message, int lineNumber, const QString& sourceID );
private slots:
void sslErrorHandler( QNetworkReply* qnr, const QList<QSslError>& errlist );
private:
JSResolver* m_parent;
QString m_scriptPath;
QString m_header;
};
#endif // SCRIPTENGINE_H