File indexing completed on 2025-01-05 04:26:49
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 "AmarokBookmarkScript.h" 0018 0019 #include "amarokconfig.h" 0020 #include "amarokurls/AmarokUrl.h" 0021 #include "amarokurls/BookmarkGroup.h" 0022 #include "amarokurls/BookmarkModel.h" 0023 #include "amarokurls/ContextUrlGenerator.h" 0024 #include "amarokurls/NavigationUrlGenerator.h" 0025 #include <amarokurls/PlayUrlGenerator.h> 0026 #include "playlist/PlaylistViewUrlGenerator.h" 0027 #include "scripting/scriptengine/ScriptingDefines.h" 0028 0029 #include <QJSEngine> 0030 0031 Q_DECLARE_METATYPE( StringMap ) 0032 0033 using namespace AmarokScript; 0034 0035 AmarokBookmarkScript::AmarokBookmarkScript( QJSEngine *engine ) 0036 : QObject( engine ) 0037 , m_engine( engine ) 0038 { 0039 QJSValue scriptObject = m_engine->newQObject( this ); 0040 m_engine->globalObject().property( QStringLiteral("Amarok") ).setProperty( QStringLiteral("Bookmark"), scriptObject ); 0041 0042 qRegisterMetaType<BookmarkGroupPtr>(); 0043 QMetaType::registerConverter<BookmarkGroupPtr,QJSValue>( [=] (BookmarkGroupPtr bGroup) { return toScriptValue<BookmarkGroupPtr, BookmarkGroupPrototype>( m_engine, bGroup ); } ); 0044 QMetaType::registerConverter<QJSValue,BookmarkGroupPtr>( [] (QJSValue jsValue) { 0045 BookmarkGroupPtr bGroup; 0046 fromScriptValue<BookmarkGroupPtr, BookmarkGroupPrototype>( jsValue, bGroup ); 0047 return bGroup; 0048 } ); 0049 0050 qRegisterMetaType<AmarokUrlPtr>(); 0051 QMetaType::registerConverter<AmarokUrlPtr, QJSValue>( [=] (AmarokUrlPtr url) { return toScriptValue<AmarokUrlPtr, BookmarkPrototype>( m_engine, url ); } ); 0052 QMetaType::registerConverter<QJSValue, AmarokUrlPtr>( [] (QJSValue jsValue) { 0053 AmarokUrlPtr url; 0054 fromScriptValue<AmarokUrlPtr, BookmarkPrototype>( jsValue, url ); 0055 return url; 0056 } ); 0057 0058 QJSValue bookmarkGroupCtor = scriptObject.property("bookmarkGroupCtorWrapper"); 0059 m_engine->globalObject().setProperty( QStringLiteral("BookmarkGroup"), bookmarkGroupCtor ); 0060 0061 QJSValue bookmarkCtor = scriptObject.property("bookmarkCtorWrapper"); 0062 m_engine->globalObject().setProperty( QStringLiteral("Bookmark"), bookmarkCtor ); 0063 0064 qRegisterMetaType<BookmarkGroupList>(); 0065 QMetaType::registerConverter<BookmarkGroupList, QJSValue>( [=] (BookmarkGroupList bgList) { return toScriptArray<BookmarkGroupList>( m_engine, bgList ); } ); 0066 QMetaType::registerConverter<QJSValue, BookmarkGroupList>( [] (QJSValue jsValue) { 0067 BookmarkGroupList bgList; 0068 fromScriptArray<BookmarkGroupList>( jsValue, bgList ); 0069 return bgList; 0070 } ); 0071 0072 qRegisterMetaType<BookmarkList>(); 0073 QMetaType::registerConverter<BookmarkList,QJSValue>( [=] (BookmarkList bList) { return toScriptArray<BookmarkList>( m_engine, bList); } ); 0074 QMetaType::registerConverter<QJSValue,BookmarkList>( [] (QJSValue jsValue) { 0075 BookmarkList bList; 0076 fromScriptArray<BookmarkList>( jsValue, bList ); 0077 return bList; 0078 } ); 0079 } 0080 0081 AmarokUrlPtr 0082 AmarokBookmarkScript::contextView() 0083 { 0084 return AmarokUrlPtr( new AmarokUrl( ContextUrlGenerator::instance()->createContextBookmark() ) ); 0085 } 0086 0087 AmarokUrlPtr 0088 AmarokBookmarkScript::currentPlaylistView() 0089 { 0090 return AmarokUrlPtr( new AmarokUrl( Playlist::ViewUrlGenerator::instance()->createUrl() ) ); 0091 } 0092 0093 AmarokUrlPtr 0094 AmarokBookmarkScript::browserView() 0095 { 0096 return AmarokUrlPtr( new AmarokUrl( NavigationUrlGenerator::instance()->CreateAmarokUrl() ) ); 0097 } 0098 0099 AmarokUrlPtr 0100 AmarokBookmarkScript::createCurrentTrackBookmark() 0101 { 0102 return AmarokUrlPtr( new AmarokUrl( PlayUrlGenerator::instance()->createCurrentTrackBookmark() ) ); 0103 } 0104 0105 QJSValue 0106 AmarokBookmarkScript::bookmarkCtorWrapper( QJSValue arg0, QJSValue arg1 ) 0107 { 0108 return BookmarkPrototype::bookmarkCtor( arg0, arg1, m_engine ); 0109 } 0110 0111 QJSValue 0112 AmarokBookmarkScript::bookmarkGroupCtorWrapper( QJSValue arg0, QJSValue arg1 ) 0113 { 0114 return BookmarkGroupPrototype::bookmarkGroupCtor( arg0, arg1, m_engine ); 0115 } 0116 0117 ////////////////////////////////////////////////////////////////////////////////////////////////// 0118 // BookmarkGroupPrototype 0119 ////////////////////////////////////////////////////////////////////////////////////////////////// 0120 0121 QJSValue 0122 BookmarkGroupPrototype::bookmarkGroupCtor( QJSValue &arg0, QJSValue &arg1, QJSEngine *engine ) 0123 { 0124 BookmarkGroup *group = nullptr; 0125 if ( arg1.isUndefined() ) { 0126 if( arg0.isString() ) 0127 group = new BookmarkGroup( arg0.toString() ); 0128 } else { 0129 if( arg0.isString() ) 0130 { 0131 QString name = arg0.toString(); 0132 if( arg1.isString() ) 0133 group = new BookmarkGroup( name, arg1.toString() ); 0134 else if( dynamic_cast<BookmarkGroupPrototype*>( arg1.toQObject() ) ) 0135 group = new BookmarkGroup( name, dynamic_cast<BookmarkGroupPrototype*>( arg1.toQObject() )->data() ); 0136 } 0137 } 0138 if( !group ) { 0139 /* TODO - Use commented code once QT versions >= 5.12 0140 QJSValue errorObj = engine->newErrorObject( QJSValue::TypeError, QStringLiteral("Invalid arguments!") ); 0141 engine->throwError( errorObj.errorType(), errorObj.toString() ); 0142 return errorObj; 0143 */ 0144 return engine->evaluate("throw new TypeError('Invalid arguments!')"); 0145 } 0146 0147 return engine->newQObject( new BookmarkGroupPrototype( BookmarkGroupPtr( group ) ) ); 0148 } 0149 0150 BookmarkGroupPrototype::BookmarkGroupPrototype( const BookmarkGroupPtr &group ) 0151 :QObject( nullptr ) 0152 , m_group( group ) 0153 {} 0154 0155 BookmarkList 0156 BookmarkGroupPrototype::childBookmarks() const 0157 { 0158 return m_group->childBookmarks(); 0159 } 0160 0161 BookmarkGroupList 0162 BookmarkGroupPrototype::childGroups() const 0163 { 0164 return m_group->childGroups(); 0165 } 0166 0167 void 0168 BookmarkGroupPrototype::clear() 0169 { 0170 m_group->clear(); 0171 } 0172 0173 void BookmarkGroupPrototype::deleteChildBookmark( const AmarokUrlPtr &bookmark ) 0174 { 0175 m_group->deleteChild( BookmarkViewItemPtr::staticCast( bookmark ) ); 0176 } 0177 0178 void 0179 BookmarkGroupPrototype::deleteChildBookmarkgroup( const BookmarkGroupPtr &bookmarkGroup ) 0180 { 0181 m_group->deleteChild( BookmarkViewItemPtr::staticCast( bookmarkGroup ) ); 0182 } 0183 0184 void 0185 BookmarkGroupPrototype::save() 0186 { 0187 m_group->save(); 0188 BookmarkModel::instance()->reloadFromDb(); 0189 } 0190 0191 // private 0192 int 0193 BookmarkGroupPrototype::childCount() const 0194 { 0195 return m_group->childCount(); 0196 } 0197 0198 QString 0199 BookmarkGroupPrototype::description() const 0200 { 0201 return m_group->description(); 0202 } 0203 0204 int 0205 BookmarkGroupPrototype::id() const 0206 { 0207 return m_group->id(); 0208 } 0209 0210 QString 0211 BookmarkGroupPrototype::name() const 0212 { 0213 return m_group->name(); 0214 } 0215 0216 BookmarkGroupPtr 0217 BookmarkGroupPrototype::parent() const 0218 { 0219 return m_group->parent(); 0220 } 0221 0222 void 0223 BookmarkGroupPrototype::setDescription( const QString &description ) 0224 { 0225 m_group->setDescription( description ); 0226 } 0227 0228 void 0229 BookmarkGroupPrototype::setName( const QString &name ) 0230 { 0231 m_group->rename( name ); 0232 } 0233 0234 void 0235 BookmarkGroupPrototype::setParent( const BookmarkGroupPtr &parent ) 0236 { 0237 m_group->reparent( parent ); 0238 } 0239 0240 ////////////////////////////////////////////////////////////////////////////////////////////////// 0241 // BookmarkPrototype 0242 ////////////////////////////////////////////////////////////////////////////////////////////////// 0243 0244 // public 0245 0246 // script invokable 0247 bool 0248 BookmarkPrototype::save() 0249 { 0250 bool success( m_url->saveToDb() ); 0251 if( success ) 0252 BookmarkModel::instance()->reloadFromDb(); 0253 return success; 0254 } 0255 0256 QJSValue 0257 BookmarkPrototype::bookmarkCtor( QJSValue &arg0, QJSValue &arg1, QJSEngine *engine ) 0258 { 0259 AmarokUrlPtr url; 0260 if ( arg1.isUndefined() ) { 0261 if( arg0.isString() ) 0262 url = new AmarokUrl( arg0.toString() ); 0263 } else { 0264 if( arg0.isString() ) 0265 { 0266 BookmarkGroupPrototype *proto = dynamic_cast<BookmarkGroupPrototype*>( arg1.toQObject() ); 0267 if( proto ) 0268 url = new AmarokUrl( arg0.toString(), proto->data() ); 0269 } 0270 else 0271 { 0272 Meta::TrackPtr track = engine->fromScriptValue<Meta::TrackPtr>( arg0 ); 0273 if( track && arg1.toVariant().canConvert( QVariant::LongLong ) ) 0274 url = new AmarokUrl( PlayUrlGenerator::instance()->createTrackBookmark( track, arg1.toVariant().toLongLong() ) ); 0275 } 0276 } 0277 if( !url ) { 0278 /* TODO - Use commented code once QT versions >= 5.12 0279 QJSValue errorObj = engine->newErrorObject( QJSValue::TypeError, QStringLiteral("Invalid arguments!") ); 0280 engine->throwError( errorObj.errorType(), errorObj.toString() ); 0281 return errorObj; 0282 */ 0283 return engine->evaluate("throw new TypeError('Invalid arguments!')"); 0284 } 0285 0286 return engine->newQObject( new BookmarkPrototype( url ) ); 0287 } 0288 0289 BookmarkPrototype::BookmarkPrototype( const AmarokUrlPtr &bookmark ) 0290 : QObject( nullptr ) 0291 , m_url( bookmark ) 0292 {} 0293 0294 void 0295 BookmarkPrototype::initFromString( const QString &urlString ) 0296 { 0297 m_url->initFromString( urlString ); 0298 } 0299 0300 StringMap 0301 BookmarkPrototype::args() const 0302 { 0303 return m_url->args(); 0304 } 0305 0306 void 0307 BookmarkPrototype::setArg( const QString &name, const QString &value ) 0308 { 0309 m_url->setArg( name, value ); 0310 } 0311 0312 QString 0313 BookmarkPrototype::prettyCommand() const 0314 { 0315 return m_url->prettyCommand(); 0316 } 0317 0318 void 0319 BookmarkPrototype::removeFromDb() 0320 { 0321 m_url->removeFromDb(); 0322 } 0323 0324 bool 0325 BookmarkPrototype::run() 0326 { 0327 return m_url->run(); 0328 } 0329 0330 // private 0331 0332 QString 0333 BookmarkPrototype::command() const 0334 { 0335 return m_url->command(); 0336 } 0337 0338 void 0339 BookmarkPrototype::setCommand( const QString &command ) 0340 { 0341 m_url->setCommand( command ); 0342 } 0343 0344 QString 0345 BookmarkPrototype::description() const 0346 { 0347 return m_url->description(); 0348 } 0349 0350 void 0351 BookmarkPrototype::setDescription( const QString &description ) 0352 { 0353 m_url->setDescription( description ); 0354 } 0355 0356 int 0357 BookmarkPrototype::id() const 0358 { 0359 return m_url->id(); 0360 } 0361 0362 void 0363 BookmarkPrototype::setId( int id ) 0364 { 0365 m_url->setId( id ); 0366 } 0367 0368 QString 0369 BookmarkPrototype::customValue() const 0370 { 0371 return m_url->customValue(); 0372 } 0373 0374 void 0375 BookmarkPrototype::setCustomValue( const QString &custom ) 0376 { 0377 m_url->setCustomValue( custom ); 0378 } 0379 0380 bool 0381 BookmarkPrototype::isNull() const 0382 { 0383 return m_url->isNull(); 0384 } 0385 0386 QString 0387 BookmarkPrototype::name() const 0388 { 0389 return m_url->name(); 0390 } 0391 0392 void 0393 BookmarkPrototype::setName( const QString &name ) 0394 { 0395 m_url->setName( name ); 0396 } 0397 0398 0399 BookmarkGroupPtr 0400 BookmarkPrototype::parent() const 0401 { 0402 return m_url->parent(); 0403 } 0404 0405 void 0406 BookmarkPrototype::setParent( const BookmarkGroupPtr &parent ) 0407 { 0408 m_url->reparent( parent ); 0409 } 0410 0411 QString 0412 BookmarkPrototype::path() const 0413 { 0414 return m_url->path(); 0415 } 0416 0417 void 0418 BookmarkPrototype::setPath( const QString &path ) 0419 { 0420 m_url->setPath( path ); 0421 } 0422 0423 QString 0424 BookmarkPrototype::url() const 0425 { 0426 return m_url->url(); 0427 }