From 0403301c9a003e849f7a64d762221b802d2da708 Mon Sep 17 00:00:00 2001 From: "Uwe L. Korn" Date: Sun, 23 Jun 2013 10:58:12 +0200 Subject: [PATCH] Move ScriptEngine into its own files --- src/libtomahawk/CMakeLists.txt | 1 + src/libtomahawk/resolvers/JSResolver.cpp | 95 +-------------- src/libtomahawk/resolvers/JSResolver.h | 28 +---- src/libtomahawk/resolvers/ScriptEngine.cpp | 128 +++++++++++++++++++++ src/libtomahawk/resolvers/ScriptEngine.h | 58 ++++++++++ 5 files changed, 190 insertions(+), 120 deletions(-) create mode 100644 src/libtomahawk/resolvers/ScriptEngine.cpp create mode 100644 src/libtomahawk/resolvers/ScriptEngine.h diff --git a/src/libtomahawk/CMakeLists.txt b/src/libtomahawk/CMakeLists.txt index cce9aa59f..51ec98f37 100644 --- a/src/libtomahawk/CMakeLists.txt +++ b/src/libtomahawk/CMakeLists.txt @@ -99,6 +99,7 @@ set( libGuiSources resolvers/ExternalResolverGui.cpp resolvers/ScriptResolver.cpp resolvers/JSResolver.cpp + resolvers/ScriptEngine.cpp utils/ImageRegistry.cpp utils/WidgetDragFilter.cpp diff --git a/src/libtomahawk/resolvers/JSResolver.cpp b/src/libtomahawk/resolvers/JSResolver.cpp index c72ca7ba9..4edc8c4a6 100644 --- a/src/libtomahawk/resolvers/JSResolver.cpp +++ b/src/libtomahawk/resolvers/JSResolver.cpp @@ -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 ) ), - SLOT( sslErrorHandler( QNetworkReply*, QList ) ) ); -} - - -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& 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 %1, but we can't confirm that your connection is secure:

" - "%2

" - "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; -} diff --git a/src/libtomahawk/resolvers/JSResolver.h b/src/libtomahawk/resolvers/JSResolver.h index 461f1649f..c430003ad 100644 --- a/src/libtomahawk/resolvers/JSResolver.h +++ b/src/libtomahawk/resolvers/JSResolver.h @@ -3,6 +3,7 @@ * Copyright 2010-2011, Christian Muehlhaeuser * Copyright 2010-2011, Leo Franchi * Copyright 2013, Teo Mrnjavac + * Copyright 2013, Uwe L. Korn * * 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& errlist ); - -private: - JSResolver* m_parent; - QString m_scriptPath; - QString m_header; -}; - - class DLLEXPORT JSResolver : public Tomahawk::ExternalResolverGui { Q_OBJECT diff --git a/src/libtomahawk/resolvers/ScriptEngine.cpp b/src/libtomahawk/resolvers/ScriptEngine.cpp new file mode 100644 index 000000000..dd610a304 --- /dev/null +++ b/src/libtomahawk/resolvers/ScriptEngine.cpp @@ -0,0 +1,128 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Christian Muehlhaeuser + * Copyright 2010-2011, Leo Franchi + * Copyright 2013, Teo Mrnjavac + * Copyright 2013, Uwe L. Korn + * + * 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 "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 +#include + +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 ) ), + SLOT( sslErrorHandler( QNetworkReply*, QList ) ) ); +} + + +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& 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 %1, but we can't confirm that your connection is secure:

" + "%2

" + "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; +} + diff --git a/src/libtomahawk/resolvers/ScriptEngine.h b/src/libtomahawk/resolvers/ScriptEngine.h new file mode 100644 index 000000000..c90377527 --- /dev/null +++ b/src/libtomahawk/resolvers/ScriptEngine.h @@ -0,0 +1,58 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Christian Muehlhaeuser + * Copyright 2010-2011, Leo Franchi + * Copyright 2013, Teo Mrnjavac + * Copyright 2013, Uwe L. Korn + * + * 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 SCRIPTENGINE_H +#define SCRIPTENGINE_H + +#include "DllMacro.h" + +#include +#include + +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& errlist ); + +private: + JSResolver* m_parent; + QString m_scriptPath; + QString m_header; +}; + +#endif // SCRIPTENGINE_H