1 /*****************************************************************************
2 * extensions_manager.cpp: Extensions manager for Qt
3 ****************************************************************************
4 * Copyright (C) 2009-2010 VideoLAN and authors
7 * Authors: Jean-Philippe André < jpeg # videolan.org >
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #include "extensions_manager.hpp"
25 #include "input_manager.hpp"
26 #include "dialogs/extensions.hpp"
28 #include <vlc_modules.h>
33 #include <QSignalMapper>
36 #define MENU_MAP(a,e) ((uint32_t)( (((uint16_t)a) << 16) | ((uint16_t)e) ))
37 #define MENU_GET_ACTION(a) ( (uint16_t)( ((uint32_t)a) >> 16 ) )
38 #define MENU_GET_EXTENSION(a) ( (uint16_t)( ((uint32_t)a) & 0xFFFF ) )
40 ExtensionsManager* ExtensionsManager::instance = NULL;
42 ExtensionsManager::ExtensionsManager( intf_thread_t *_p_intf, QObject *parent )
43 : QObject( parent ), p_intf( _p_intf ), p_extensions_manager( NULL )
46 assert( ExtensionsManager::instance == NULL );
49 menuMapper = new QSignalMapper( this );
50 CONNECT( menuMapper, mapped( int ), this, triggerMenu( int ) );
51 CONNECT( THEMIM->getIM(), playingStatusChanged( int ), this, playingChanged( int ) );
52 DCONNECT( THEMIM, inputChanged( input_thread_t* ),
53 this, inputChanged( input_thread_t* ) );
54 CONNECT( THEMIM->getIM(), metaChanged( input_item_t* ),
55 this, metaChanged( input_item_t* ) );
60 ExtensionsManager::~ExtensionsManager()
62 msg_Dbg( p_intf, "Killing extension dialog provider" );
63 ExtensionsDialogProvider::killInstance();
64 if( p_extensions_manager )
66 module_unneed( p_extensions_manager, p_extensions_manager->p_module );
67 vlc_object_release( p_extensions_manager );
71 bool ExtensionsManager::loadExtensions()
73 if( !p_extensions_manager )
75 p_extensions_manager = ( extensions_manager_t* )
76 vlc_object_create( p_intf, sizeof( extensions_manager_t ) );
77 if( !p_extensions_manager )
80 emit extensionsUpdated();
84 p_extensions_manager->p_module =
85 module_need( p_extensions_manager, "extension", NULL, false );
87 if( !p_extensions_manager->p_module )
89 msg_Err( p_intf, "Unable to load extensions module" );
90 vlc_object_release( p_extensions_manager );
91 p_extensions_manager = NULL;
93 emit extensionsUpdated();
97 /* Initialize dialog provider */
98 p_edp = ExtensionsDialogProvider::getInstance( p_intf,
99 p_extensions_manager );
102 msg_Err( p_intf, "Unable to create dialogs provider for extensions" );
103 module_unneed( p_extensions_manager,
104 p_extensions_manager->p_module );
105 vlc_object_release( p_extensions_manager );
106 p_extensions_manager = NULL;
108 emit extensionsUpdated();
114 emit extensionsUpdated();
118 void ExtensionsManager::unloadExtensions()
120 if( !p_extensions_manager )
123 ExtensionsDialogProvider::killInstance();
124 module_unneed( p_extensions_manager, p_extensions_manager->p_module );
125 vlc_object_release( p_extensions_manager );
126 p_extensions_manager = NULL;
129 void ExtensionsManager::reloadExtensions()
133 emit extensionsUpdated();
136 void ExtensionsManager::menu( QMenu *current )
138 assert( current != NULL );
141 // This case can happen: do nothing
145 vlc_mutex_lock( &p_extensions_manager->lock );
148 extension_t *p_ext = NULL;
150 FOREACH_ARRAY( p_ext, p_extensions_manager->extensions )
152 bool b_Active = extension_IsActivated( p_extensions_manager, p_ext );
154 if( b_Active && extension_HasMenu( p_extensions_manager, p_ext ) )
156 QMenu *submenu = new QMenu(
157 qfu( p_ext->psz_shortdescription ? p_ext->psz_shortdescription: p_ext->psz_title ),
159 char **ppsz_titles = NULL;
160 uint16_t *pi_ids = NULL;
162 action = current->addMenu( submenu );
164 action->setCheckable( true );
165 action->setChecked( true );
167 if( extension_GetMenu( p_extensions_manager, p_ext,
168 &ppsz_titles, &pi_ids ) == VLC_SUCCESS )
170 for( int i = 0; ppsz_titles[i] != NULL; ++i )
173 action = submenu->addAction( qfu( ppsz_titles[i] ) );
174 menuMapper->setMapping( action,
175 MENU_MAP( pi_ids[i], i_ext ) );
176 CONNECT( action, triggered(), menuMapper, map() );
177 free( ppsz_titles[i] );
181 action = submenu->addAction( qtr( "Empty" ) );
182 action->setEnabled( false );
189 msg_Warn( p_intf, "Could not get menu for extension '%s'",
191 action = submenu->addAction( qtr( "Empty" ) );
192 action->setEnabled( false );
195 submenu->addSeparator();
196 action = submenu->addAction( QIcon( ":/menu/quit" ),
197 qtr( "Deactivate" ) );
198 menuMapper->setMapping( action, MENU_MAP( 0, i_ext ) );
199 CONNECT( action, triggered(), menuMapper, map() );
203 action = current->addAction(
204 qfu( p_ext->psz_shortdescription ? p_ext->psz_shortdescription: p_ext->psz_title ) );
205 menuMapper->setMapping( action, MENU_MAP( 0, i_ext ) );
206 CONNECT( action, triggered(), menuMapper, map() );
208 if( !extension_TriggerOnly( p_extensions_manager, p_ext ) )
210 action->setCheckable( true );
211 action->setChecked( b_Active );
218 vlc_mutex_unlock( &p_extensions_manager->lock );
221 void ExtensionsManager::triggerMenu( int id )
223 uint16_t i_ext = MENU_GET_EXTENSION( id );
224 uint16_t i_action = MENU_GET_ACTION( id );
226 vlc_mutex_lock( &p_extensions_manager->lock );
228 if( (int) i_ext > p_extensions_manager->extensions.i_size )
230 msg_Dbg( p_intf, "can't trigger extension with wrong id %d",
232 vlc_mutex_unlock( &p_extensions_manager->lock );
236 extension_t *p_ext = ARRAY_VAL( p_extensions_manager->extensions, i_ext );
237 assert( p_ext != NULL);
239 vlc_mutex_unlock( &p_extensions_manager->lock );
243 msg_Dbg( p_intf, "activating or triggering extension '%s'",
246 if( extension_TriggerOnly( p_extensions_manager, p_ext ) )
248 extension_Trigger( p_extensions_manager, p_ext );
252 if( !extension_IsActivated( p_extensions_manager, p_ext ) )
253 extension_Activate( p_extensions_manager, p_ext );
255 extension_Deactivate( p_extensions_manager, p_ext );
260 msg_Dbg( p_intf, "triggering extension '%s', on menu with id = 0x%x",
261 p_ext->psz_title, i_action );
263 extension_TriggerMenu( p_extensions_manager, p_ext, i_action );
267 void ExtensionsManager::inputChanged( input_thread_t* p_input )
269 //This is unlikely, but can happen if no extension modules can be loaded.
270 if ( p_extensions_manager == NULL )
272 vlc_mutex_lock( &p_extensions_manager->lock );
275 FOREACH_ARRAY( p_ext, p_extensions_manager->extensions )
277 if( extension_IsActivated( p_extensions_manager, p_ext ) )
279 extension_SetInput( p_extensions_manager, p_ext, p_input );
284 vlc_mutex_unlock( &p_extensions_manager->lock );
287 void ExtensionsManager::playingChanged( int state )
289 //This is unlikely, but can happen if no extension modules can be loaded.
290 if ( p_extensions_manager == NULL )
292 vlc_mutex_lock( &p_extensions_manager->lock );
295 FOREACH_ARRAY( p_ext, p_extensions_manager->extensions )
297 if( extension_IsActivated( p_extensions_manager, p_ext ) )
299 extension_PlayingChanged( p_extensions_manager, p_ext, state );
304 vlc_mutex_unlock( &p_extensions_manager->lock );
307 void ExtensionsManager::metaChanged( input_item_t* )
309 //This is unlikely, but can happen if no extension modules can be loaded.
310 if ( p_extensions_manager == NULL )
312 vlc_mutex_lock( &p_extensions_manager->lock );
314 FOREACH_ARRAY( p_ext, p_extensions_manager->extensions )
316 if( extension_IsActivated( p_extensions_manager, p_ext ) )
318 extension_MetaChanged( p_extensions_manager, p_ext );
322 vlc_mutex_unlock( &p_extensions_manager->lock );