File indexing completed on 2024-05-19 04:50:24

0001 /****************************************************************************************
0002  * Copyright (c) 2007 Nikolaj Hald Nielsen <nhn@kde.org>                                *
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 "ScriptableServiceManager.h"
0018 
0019 
0020 #include "core-impl/collections/support/MemoryCollection.h"
0021 #include "core/support/Debug.h"
0022 #include "ScriptableServiceCollection.h"
0023 #include "ScriptableServiceMeta.h"
0024 #include "scripting/scriptmanager/ScriptManager.h"
0025 #include "../ServiceMetaBase.h"
0026 #include "browsers/servicebrowser/ServiceBrowser.h"
0027 
0028 
0029 using namespace Meta;
0030 
0031 ScriptableServiceManager * ScriptableServiceManager::s_instance = nullptr;
0032 
0033 
0034 ScriptableServiceManager::ScriptableServiceManager()
0035 {}
0036 
0037 
0038 ScriptableServiceManager::~ScriptableServiceManager()
0039 {
0040     DEBUG_BLOCK
0041 }  
0042 
0043 
0044 bool ScriptableServiceManager::initService( const QString &name, int levels, const QString &shortDescription,  const QString &rootHtml, bool showSearchBar ) {
0045 
0046     DEBUG_BLOCK
0047 
0048     debug() << "initializing scripted service: " << name;
0049 
0050     ScriptableService * service = new ScriptableService ( name );
0051     m_serviceMap[name] = service;
0052 
0053     service->setIcon( QIcon::fromTheme( QStringLiteral("view-services-scripted-amarok") ) );
0054     service->setShortDescription( shortDescription );
0055     service->init( levels, rootHtml, showSearchBar );
0056     m_rootHtml = rootHtml;
0057 
0058     debug() << "emitting scripted service " << name;
0059     Q_EMIT addService( service );
0060 
0061     return true;
0062 }
0063 
0064 
0065 int ScriptableServiceManager::insertItem( const QString &serviceName, int level, int parentId, const QString &name, const QString &infoHtml, const QString &callbackData, const QString &playableUrl,
0066                                           const QString & albumOverride, const QString & artistOverride, const QString & genreOverride,
0067                                           const QString & composerOverride, int yearOverride, const QString &coverUrl)
0068 {
0069     if ( !m_serviceMap.contains( serviceName ) ) {
0070         //invalid service name
0071         return -1;
0072     }
0073 
0074     return m_serviceMap[serviceName]->insertItem( level, parentId, name, infoHtml, callbackData, playableUrl, albumOverride, artistOverride, genreOverride, composerOverride, yearOverride, coverUrl );
0075 
0076     //return -1; // FIXME: what should this return?
0077 }
0078 
0079 void ScriptableServiceManager::setCurrentInfo( const QString &serviceName, const QString & info )
0080 {
0081     DEBUG_BLOCK
0082     if ( !m_serviceMap.contains( serviceName ) ) {
0083     //invalid service name
0084         return;
0085     }
0086 
0087     m_serviceMap[serviceName]->setCurrentInfo( info );
0088 }
0089 
0090 
0091 void ScriptableServiceManager::donePopulating(const QString & serviceName, int parentId)
0092 {
0093     DEBUG_BLOCK
0094     debug() << "Service name: " << serviceName << ", parent id: " << parentId;
0095     if ( !m_serviceMap.contains( serviceName ) ) {
0096         //invalid service name
0097         return;
0098     }
0099 
0100     m_serviceMap[serviceName]->donePopulating( parentId );
0101 }
0102 
0103 void ScriptableServiceManager::removeRunningScript(const QString & name)
0104 {
0105     if ( !m_serviceMap.contains( name ) ) {
0106         debug() << "no such service to remove";
0107         return;
0108     }
0109 
0110     //service gets deleted by serviceBrowser
0111     ServiceBrowser::instance()->removeCategory( m_serviceMap.take( name ) );
0112 }
0113 
0114 void ScriptableServiceManager::setIcon( const QString & serviceName, const QPixmap & icon )
0115 {
0116     DEBUG_BLOCK
0117             debug() << "service: " << serviceName;
0118     if ( !m_serviceMap.contains( serviceName ) ) {
0119         //invalid service name
0120         debug() << "does not exist.... ";
0121         return;
0122     }
0123 
0124 
0125     m_serviceMap[serviceName]->setIcon( QIcon( icon ) );
0126     Q_EMIT( serviceUpdated( m_serviceMap[serviceName] ) );
0127 }
0128 
0129 void ScriptableServiceManager::setEmblem( const QString & serviceName, const QPixmap & emblem )
0130 {
0131     if ( !m_serviceMap.contains( serviceName ) ) {
0132         //invalid service name
0133         return;
0134     }
0135     
0136     m_serviceMap[serviceName]->setCustomEmblem( emblem );
0137     Q_EMIT( serviceUpdated( m_serviceMap[serviceName] ) );
0138 }
0139 
0140 
0141 void ScriptableServiceManager::setScalableEmblem ( const QString& serviceName, const QString& emblemPath )
0142 {
0143     if ( !m_serviceMap.contains( serviceName ) ) {
0144         //invalid service name
0145         return;
0146     }
0147     
0148     m_serviceMap[serviceName]->setCustomScalableEmblem( emblemPath );
0149     Q_EMIT( serviceUpdated( m_serviceMap[serviceName] ) );
0150 }
0151 
0152 
0153 ScriptableService * ScriptableServiceManager::service(const QString &name)
0154 {
0155     
0156     if ( !m_serviceMap.contains( name ) ) {
0157         return nullptr;
0158     }
0159 
0160     return m_serviceMap[name];
0161 }
0162 
0163 
0164 namespace The {
0165     ScriptableServiceManager*
0166     scriptableServiceManager()
0167     {
0168         if ( ScriptableServiceManager::s_instance == nullptr )
0169             ScriptableServiceManager::s_instance = new ScriptableServiceManager();
0170 
0171         return ScriptableServiceManager::s_instance;
0172     }
0173 }
0174 
0175 
0176 
0177 
0178 
0179 
0180 
0181 
0182 
0183 
0184 
0185