1 /****************************************************************************************
2 * Copyright (c) 2008 Peter ZHOU <peterzhoulei@gmail.com> *
3 * Copyright (c) 2008 Nikolaj Hald Nielsen <nhn@kde.org> *
4 * *
5 * This program is free software; you can redistribute it and/or modify it under *
6 * the terms of the GNU General Public License as published by the Free Software *
7 * Foundation; either version 2 of the License, or (at your option) any later *
8 * version. *
9 * *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY *
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
12 * PARTICULAR PURPOSE. See the GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License along with *
15 * this program. If not, see <http://www.gnu.org/licenses/>. *
16 ****************************************************************************************/
17
18 #define DEBUG_PREFIX "AmarokScriptableServiceScript"
19
20 #include "AmarokScriptableServiceScript.h"
21
22 #include "App.h"
23 #include "core/support/Debug.h"
24 #include "services/scriptable/ScriptableServiceManager.h"
25 #include "scripting/scriptengine/AmarokStreamItemScript.h"
26 #include "scripting/scriptmanager/ScriptManager.h"
27
28 #include <QJSEngine>
29
30 using namespace AmarokScript;
31
ScriptableServiceScript(QJSEngine * engine)32 ScriptableServiceScript::ScriptableServiceScript( QJSEngine* engine )
33 : QObject( engine )
34 , m_scriptEngine( engine )
35 {
36 DEBUG_BLOCK
37 QJSValue scriptObj = engine->newQObject( this );
38 scriptObj.setPrototype( QJSValue() );
39 const QJSValue ctor = scriptObj.property("ScriptableServiceScript_prototype_ctor");
40 engine->globalObject().setProperty( QStringLiteral("ScriptableServiceScript_prototype_ctor"), ctor );
41 // Simulates creation of ScriptableServiceScript object of QTScript
42 engine->evaluate( QStringLiteral("function ScriptableServiceScript( serviceName, levels, shortDescription, rootHtml, showSearchBar) {"
43 "Object.assign( this, ScriptableServiceScript_prototype_ctor(serviceName, levels, shortDescription, rootHtml, showSearchBar) ); }")
44 );
45 }
46
47 QObject*
ScriptableServiceScript_prototype_ctor(QString serviceName,int levels,QString shortDescription,QString rootHtml,bool showSearchBar)48 ScriptableServiceScript::ScriptableServiceScript_prototype_ctor( QString serviceName, int levels, QString shortDescription, QString rootHtml, bool showSearchBar )
49 {
50 DEBUG_BLOCK
51 if( !ScriptManager::instance()->m_scripts.contains( serviceName ) )
52 {
53 error() << "The name of the scriptable script should be the same with the one in the script.spec file!";
54 return nullptr;
55 }
56 QObject* qObj = ScriptManager::instance()->m_scripts.value(serviceName)->service();
57 QJSValue scriptObj = m_scriptEngine->newQObject( qObj );
58 m_scriptEngine->globalObject().setProperty( QStringLiteral("ScriptableServiceScript"), scriptObj );
59 The::scriptableServiceManager()->initService( serviceName, levels, shortDescription, rootHtml, showSearchBar );
60 return qObj;
61 }
62
63 QJSValue
ScriptableServiceScript_prototype_populate(QJSEngine * engine)64 ScriptableServiceScript::ScriptableServiceScript_prototype_populate( QJSEngine *engine )
65 {
66 Q_UNUSED( engine );
67 debug() << "prototype populating here!";
68 return QJSValue( QJSValue::UndefinedValue );
69 }
70
71 int
insertItem(StreamItem * item)72 ScriptableServiceScript::insertItem( StreamItem* item )
73 {
74 return The::scriptableServiceManager()->insertItem( m_serviceName, item->level(), m_currentId, item->itemName(), item->infoHtml(), item->callbackData(), item->playableUrl(),
75 item->album(), item->artist(), item->genre(), item->composer(), item->year(),
76 item->coverUrl() );
77 }
78
79 int
donePopulating() const80 ScriptableServiceScript::donePopulating() const
81 {
82 DEBUG_BLOCK
83
84 The::scriptableServiceManager()->donePopulating( m_serviceName, m_currentId );
85 return -1; // Fixme: return the right thing.
86 }
87
88 void
slotPopulate(const QString & name,int level,int parent_id,const QString & callbackData,const QString & filter)89 ScriptableServiceScript::slotPopulate( const QString &name, int level, int parent_id, const QString &callbackData, const QString &filter )
90 {
91 DEBUG_BLOCK
92 m_currentId = parent_id;
93 m_serviceName = name;
94 Q_EMIT( populate( level, callbackData, filter ) );
95 }
96
97 void
slotRequestInfo(const QString & name,int level,const QString & callbackData)98 ScriptableServiceScript::slotRequestInfo( const QString &name, int level, const QString &callbackData )
99 {
100 DEBUG_BLOCK
101 m_serviceName = name;
102 Q_EMIT( fetchInfo( level, callbackData ) );
103 }
104
105 void
slotCustomize(const QString & name)106 ScriptableServiceScript::slotCustomize( const QString &name )
107 {
108 DEBUG_BLOCK
109 m_serviceName = name;
110 Q_EMIT( customize() );
111 }
112
113
114 void
setIcon(const QPixmap & icon)115 ScriptableServiceScript::setIcon( const QPixmap &icon )
116 {
117 The::scriptableServiceManager()->setIcon( m_serviceName, icon );
118 }
119
120 void
setEmblem(const QPixmap & emblem)121 ScriptableServiceScript::setEmblem( const QPixmap &emblem )
122 {
123 The::scriptableServiceManager()->setEmblem( m_serviceName, emblem );
124 }
125
126 void
setScalableEmblem(const QString & emblemPath)127 ScriptableServiceScript::setScalableEmblem ( const QString& emblemPath )
128 {
129 The::scriptableServiceManager()->setScalableEmblem( m_serviceName, emblemPath );
130 }
131
132 void
setCurrentInfo(const QString & infoHtml)133 ScriptableServiceScript::setCurrentInfo( const QString &infoHtml )
134 {
135 The::scriptableServiceManager()->setCurrentInfo( m_serviceName, infoHtml );
136 }
137