File indexing completed on 2024-05-05 04:49:17
0001 /**************************************************************************************** 0002 * Copyright (c) 2007 Maximilian Kossick <maximilian.kossick@googlemail.com> * 0003 * Copyright (c) 2007 Nikolaj Hald Nielsen <nhn@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 #define DEBUG_PREFIX "ServicePluginManager" 0019 0020 #include "ServicePluginManager.h" 0021 0022 #include "browsers/servicebrowser/ServiceBrowser.h" 0023 #include "core/support/Debug.h" 0024 #include "services/ServiceBase.h" 0025 0026 #include <KService> 0027 0028 #include <QSet> 0029 #include <QCoreApplication> 0030 0031 ServicePluginManager *ServicePluginManager::s_instance = nullptr; 0032 0033 ServicePluginManager * 0034 ServicePluginManager::instance() 0035 { 0036 if( !s_instance ) { 0037 s_instance = new ServicePluginManager(); 0038 } 0039 0040 return s_instance; 0041 } 0042 0043 0044 void 0045 ServicePluginManager::destroy() 0046 { 0047 if( s_instance ) { 0048 delete s_instance; 0049 s_instance = nullptr; 0050 } 0051 } 0052 0053 0054 ServicePluginManager::ServicePluginManager() 0055 : QObject() 0056 { 0057 DEBUG_BLOCK 0058 // ensure this object is created in a main thread 0059 Q_ASSERT( thread() == QCoreApplication::instance()->thread() ); 0060 0061 setObjectName( QStringLiteral("ServicePluginManager") ); 0062 } 0063 0064 0065 ServicePluginManager::~ServicePluginManager() 0066 { 0067 } 0068 0069 0070 void 0071 ServicePluginManager::setFactories( const QList<QSharedPointer<Plugins::PluginFactory> > &factories ) 0072 { 0073 QSet<QSharedPointer<Plugins::PluginFactory> > newFactories(factories.begin(), factories.end()); 0074 QSet<QSharedPointer<Plugins::PluginFactory> > oldFactories(m_factories.begin(), m_factories.end()); 0075 0076 // remove old factories 0077 for( const auto &pFactory : oldFactories - newFactories ) 0078 { 0079 auto factory = qobject_cast<ServiceFactory*>( pFactory ); 0080 if( !factory ) 0081 continue; 0082 0083 foreach( ServiceBase * service, factory->activeServices() ) 0084 ServiceBrowser::instance()->removeCategory( service ); 0085 factory->clearActiveServices(); 0086 } 0087 0088 // create new factories 0089 for( const auto &pFactory : newFactories - oldFactories ) 0090 { 0091 auto factory = qobject_cast<ServiceFactory*>( pFactory ); 0092 if( !factory ) 0093 continue; 0094 0095 connect( factory.data(), &ServiceFactory::newService, this, &ServicePluginManager::slotNewService ); 0096 connect( factory.data(), &ServiceFactory::removeService, this, &ServicePluginManager::slotRemoveService ); 0097 } 0098 0099 m_factories = factories; 0100 } 0101 0102 void 0103 ServicePluginManager::slotNewService( ServiceBase *newService ) 0104 { 0105 DEBUG_BLOCK 0106 debug() << "new service:" << newService->name(); 0107 ServiceBrowser::instance()->addCategory( newService ); 0108 } 0109 0110 void 0111 ServicePluginManager::slotRemoveService( ServiceBase *removedService ) 0112 { 0113 DEBUG_BLOCK 0114 debug() << "removed service:" << removedService->name(); 0115 ServiceBrowser::instance()->removeCategory( removedService ); 0116 } 0117 0118 QStringList 0119 ServicePluginManager::loadedServices() const 0120 { 0121 QStringList names; 0122 for( const auto &pFactory : m_factories ) 0123 { 0124 auto factory = qobject_cast<ServiceFactory*>( pFactory ); 0125 if( !factory ) 0126 continue; 0127 0128 foreach( ServiceBase *service, factory->activeServices() ) 0129 names << service->name(); 0130 } 0131 return names; 0132 } 0133 0134 QStringList 0135 ServicePluginManager::loadedServiceNames() const 0136 { 0137 return ServiceBrowser::instance()->categories().keys(); 0138 } 0139 0140 QString 0141 ServicePluginManager::serviceDescription( const QString & serviceName ) 0142 { 0143 //get named service 0144 if ( !ServiceBrowser::instance()->categories().contains( serviceName ) ) 0145 { 0146 return i18n( "No service named %1 is currently loaded", serviceName ); 0147 } 0148 0149 ServiceBase * service = dynamic_cast<ServiceBase *>( ServiceBrowser::instance()->categories().value( serviceName ) ); 0150 0151 if ( service == nullptr ) 0152 return QString(); 0153 0154 return service->shortDescription(); 0155 } 0156 0157 QString 0158 ServicePluginManager::serviceMessages( const QString & serviceName ) 0159 { 0160 //get named service 0161 if ( !ServiceBrowser::instance()->categories().contains( serviceName ) ) 0162 { 0163 return i18n( "No service named %1 is currently loaded", serviceName ); 0164 } 0165 0166 ServiceBase * service = dynamic_cast<ServiceBase *>( ServiceBrowser::instance()->categories().value( serviceName ) ); 0167 0168 if ( service == nullptr ) 0169 return QString(); 0170 0171 return service->messages(); 0172 } 0173 0174 QString 0175 ServicePluginManager::sendMessage( const QString & serviceName, const QString & message ) 0176 { 0177 //get named service 0178 if ( !ServiceBrowser::instance()->categories().contains( serviceName ) ) 0179 { 0180 return i18n( "No service named %1 is currently loaded", serviceName ); 0181 } 0182 0183 ServiceBase * service = dynamic_cast<ServiceBase *>( ServiceBrowser::instance()->categories().value( serviceName ) ); 0184 0185 if ( service == nullptr ) 0186 return QString(); 0187 0188 return service->sendMessage( message ); 0189 } 0190