File indexing completed on 2025-01-19 04:25:19

0001 /****************************************************************************************
0002  * Copyright (c) 2013 Anmol Ahuja <darthcodus@gmail.com>                                *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "ScriptingDefines.h"
0018 
0019 #include "core/support/Debug.h"
0020 
0021 #include <QMetaEnum>
0022 #include <QQmlEngine>
0023 #include <QRandomGenerator>
0024 #include <QTimer>
0025 
0026 using namespace AmarokScript;
0027 
0028 AmarokScriptEngine::AmarokScriptEngine( QObject *parent )
0029 : QJSEngine(parent)
0030 , internalObject( QStringLiteral("UndocumentedAmarokScriptingInternals") )
0031 {
0032     installExtensions(QJSEngine::TranslationExtension | QJSEngine::ConsoleExtension);
0033     QJSValue scriptObject = newQObject( this );
0034     QQmlEngine::setObjectOwnership( this, QQmlEngine::CppOwnership);
0035     globalObject().setProperty( internalObject, scriptObject );
0036     QJSValue setTimeoutObject = scriptObject.property( QStringLiteral("setTimeout") );
0037     Q_ASSERT( !setTimeoutObject.isUndefined() );
0038     Q_ASSERT( !globalObject().property( internalObject ).property( "invokableDeprecatedCall" ).isUndefined() );
0039     globalObject().setProperty( QStringLiteral("setTimeout"), setTimeoutObject );
0040 }
0041 
0042 void
0043 AmarokScriptEngine::setDeprecatedProperty( const QString &parent, const QString &name, const QJSValue &property )
0044 {
0045     const QString objName = QStringLiteral( "%1%2" ).arg( name, QString::number( QRandomGenerator::global()->generate() ) );
0046     globalObject().property( internalObject ).setProperty( objName, property );
0047     const QString command = QString( "Object.defineProperty( %1, \"%2\", {get : function(){ var iobj= %3; iobj.invokableDeprecatedCall(\""
0048                                                                             " %1.%2 \"); return iobj.%4; },\
0049                                                                             enumerable : true,\
0050                                                                             configurable : false} );" )
0051                                     .arg( parent, name, internalObject, objName );
0052     evaluate( command );
0053 }
0054 
0055 void
0056 AmarokScriptEngine::invokableDeprecatedCall( const QString &call )
0057 {
0058     warning() << "Deprecated function " + call;
0059     Q_EMIT deprecatedCall( call );
0060 }
0061 
0062 void
0063 AmarokScriptEngine::setTimeout( const QJSValue &function, int time, const QJSValue &thisObject, const QJSValue &args )
0064 {
0065     QTimer *timer = new QTimer( this );
0066     timer->setSingleShot( true );
0067     timer->setInterval( time );
0068     m_callbacks[timer] = QJSValueList() << function << thisObject << args;
0069     connect( timer, &QTimer::timeout, this, &AmarokScriptEngine::slotTimeout );
0070     timer->start();
0071 }
0072 
0073 void
0074 AmarokScriptEngine::slotTimeout()
0075 {
0076     QObject *timer = sender();
0077     if( !timer )
0078         return;
0079 
0080     QJSValueList args;
0081     QJSValue thisObject;
0082     if( m_callbacks[timer].size() > 1 )
0083     {
0084         thisObject = m_callbacks[timer][1];
0085         if( m_callbacks[timer].size() == 3 )
0086             for ( quint32 i = 0; i < m_callbacks[timer][2].property(QStringLiteral("length")).toUInt(); ++i )
0087                 args << m_callbacks[timer][2].property( i );
0088     }
0089     m_callbacks[timer][0].callWithInstance( thisObject, args );
0090     m_callbacks.remove( timer );
0091     timer->deleteLater();
0092 }
0093 
0094 QJSValue
0095 AmarokScriptEngine::enumObject( const QMetaEnum &metaEnum )
0096 {
0097     QJSValue enumObj = newObject();
0098     for( int i = 0; i< metaEnum.keyCount(); ++i )
0099         enumObj.setProperty( metaEnum.key(i), QJSEngine::toScriptValue( metaEnum.value(i) ) );
0100     return enumObj;
0101 }
0102 
0103 
0104 AmarokScriptEngine::~AmarokScriptEngine()
0105 {}
0106