File indexing completed on 2025-01-05 04:26:51

0001 /****************************************************************************************
0002  * Copyright (c) 2008 Peter ZHOU <peterzhoulei@gmail.com>                               *
0003  * Copyright (c) 2008 Mark Kretschmann <kretschmann@kde.org>                            *
0004  *                                                                                      *
0005  * This program is free software; you can redistribute it and/or modify it under        *
0006  * the terms of the GNU General Public License as published by the Free Software        *
0007  * Foundation; either version 2 of the License, or (at your option) any later           *
0008  * version.                                                                             *
0009  *                                                                                      *
0010  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0012  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0013  *                                                                                      *
0014  * You should have received a copy of the GNU General Public License along with         *
0015  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0016  ****************************************************************************************/
0017 
0018 #include "AmarokScript.h"
0019 
0020 #include "core/support/Amarok.h"
0021 #include "App.h"
0022 #include "core/support/Debug.h"
0023 #include "scripting/scriptmanager/ScriptManager.h"
0024 
0025 #include <KMessageBox>
0026 
0027 #include <QJSEngine>
0028 
0029 AmarokScript::AmarokScript::AmarokScript( const QString &name, QJSEngine *engine )
0030     : QObject( engine )
0031     , m_name( name )
0032 {
0033     QJSValue scriptObject = engine->newQObject( this );
0034     engine->globalObject().setProperty( QStringLiteral("Amarok"), scriptObject );
0035     if( ScriptManager::instance()->m_scripts.contains( name ) )
0036         connect( ScriptManager::instance()->m_scripts[name], &ScriptItem::uninstalled, this, &AmarokScript::uninstalled );
0037 }
0038 
0039 void
0040 AmarokScript::AmarokScript::quitAmarok()
0041 {
0042     pApp->quit();
0043 }
0044 
0045 void
0046 AmarokScript::AmarokScript::debug( const QString& text ) const
0047 {
0048     ::debug() << "SCRIPT" << m_name << ": " << text;
0049 }
0050 
0051 int
0052 AmarokScript::AmarokScript::alert( const QString& text, const QString& type ) const
0053 {
0054     //Ok = 1, Cancel = 2, Yes = 3, No = 4, Continue = 5
0055     if( type == QLatin1String("error") )
0056     {
0057         KMessageBox::error( nullptr, text );
0058         return -1;
0059     }
0060     else if( type == QLatin1String("information") )
0061     {
0062         KMessageBox::information( nullptr, text );
0063         return -1;
0064     }
0065     else if( type == QLatin1String("questionYesNo") )
0066         return KMessageBox::questionYesNo( nullptr, text );
0067     else if( type == QLatin1String("questionYesNoCancel") )
0068         return KMessageBox::questionYesNo( nullptr, text );
0069     else if( type == QLatin1String("warningYesNo") )
0070         return KMessageBox::warningYesNo( nullptr, text );
0071     else if( type == QLatin1String("warningContinueCancel") )
0072         return KMessageBox::warningContinueCancel( nullptr, text );
0073     else if( type == QLatin1String("warningYesNoCancel") )
0074         return KMessageBox::warningYesNoCancel( nullptr, text );
0075 
0076     debug( QStringLiteral("alert type not found!") );
0077     return -1;
0078 }
0079 
0080 void
0081 AmarokScript::AmarokScript::end()
0082 {
0083     ScriptManager::instance()->stopScript( m_name );
0084 }
0085 
0086 bool
0087 AmarokScript::AmarokScript::runScript( const QString& name ) const
0088 {
0089     return ScriptManager::instance()->runScript( name, true );
0090 }
0091 
0092 bool
0093 AmarokScript::AmarokScript::stopScript( const QString& name ) const
0094 {
0095     return ScriptManager::instance()->stopScript( name );
0096 }
0097 
0098 QStringList
0099 AmarokScript::AmarokScript::listRunningScripts() const
0100 {
0101     return ScriptManager::instance()->listRunningScripts();
0102 }