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"
32 #include <QSignalMapper>
35 #define MENU_MAP(a,e) ((uint32_t)( (((uint16_t)a) << 16) | ((uint16_t)e) ))
36 #define MENU_GET_ACTION(a) ( (uint16_t)( ((uint32_t)a) >> 16 ) )
37 #define MENU_GET_EXTENSION(a) ( (uint16_t)( ((uint32_t)a) & 0xFFFF ) )
39 ExtensionsManager* ExtensionsManager::instance = NULL;
41 ExtensionsManager::ExtensionsManager( intf_thread_t *_p_intf, QObject *parent )
42 : QObject( parent ), p_intf( _p_intf ), p_extensions_manager( NULL )
45 assert( ExtensionsManager::instance == NULL );
48 menuMapper = new QSignalMapper( this );
49 CONNECT( menuMapper, mapped( int ), this, triggerMenu( int ) );
50 CONNECT( THEMIM->getIM(), statusChanged( int ), this, playingChanged( int ) );
51 CONNECT( THEMIM, inputChanged( input_thread_t* ),
52 this, inputChanged( input_thread_t* ) );
57 ExtensionsManager::~ExtensionsManager()
59 msg_Dbg( p_intf, "Killing extension dialog provider" );
60 ExtensionsDialogProvider::killInstance();
61 if( p_extensions_manager )
63 module_unneed( p_extensions_manager, p_extensions_manager->p_module );
64 vlc_object_release( p_extensions_manager );
68 bool ExtensionsManager::loadExtensions()
70 if( !p_extensions_manager )
72 p_extensions_manager = ( extensions_manager_t* )
73 vlc_object_create( p_intf, sizeof( extensions_manager_t ) );
74 if( !p_extensions_manager )
77 emit extensionsUpdated();
80 vlc_object_attach( p_extensions_manager, p_intf );
82 p_extensions_manager->p_module =
83 module_need( p_extensions_manager, "extension", NULL, false );
85 if( !p_extensions_manager->p_module )
87 msg_Err( p_intf, "Unable to load extensions module" );
88 vlc_object_release( p_extensions_manager );
89 p_extensions_manager = NULL;
91 emit extensionsUpdated();
95 /* Initialize dialog provider */
96 p_edp = ExtensionsDialogProvider::getInstance( p_intf,
97 p_extensions_manager );
100 msg_Err( p_intf, "Unable to create dialogs provider for extensions" );
101 module_unneed( p_extensions_manager,
102 p_extensions_manager->p_module );
103 vlc_object_release( p_extensions_manager );
104 p_extensions_manager = NULL;
106 emit extensionsUpdated();
112 emit extensionsUpdated();
116 void ExtensionsManager::unloadExtensions()
118 if( !p_extensions_manager )
121 module_unneed( p_extensions_manager, p_extensions_manager->p_module );
122 vlc_object_release( p_extensions_manager );
123 p_extensions_manager = NULL;
124 emit extensionsUpdated();
125 ExtensionsDialogProvider::killInstance();
128 void ExtensionsManager::reloadExtensions()
132 emit extensionsUpdated();
135 void ExtensionsManager::menu( QMenu *current )
137 assert( current != NULL );
140 // This case can happen: do nothing
144 vlc_mutex_lock( &p_extensions_manager->lock );
147 extension_t *p_ext = NULL;
149 FOREACH_ARRAY( p_ext, p_extensions_manager->extensions )
151 bool b_Active = extension_IsActivated( p_extensions_manager, p_ext );
153 if( b_Active && extension_HasMenu( p_extensions_manager, p_ext ) )
155 QMenu *submenu = new QMenu( qfu( p_ext->psz_title ) );
156 char **ppsz_titles = NULL;
157 uint16_t *pi_ids = NULL;
159 action = current->addMenu( submenu );
161 action->setCheckable( true );
162 action->setChecked( true );
164 if( extension_GetMenu( p_extensions_manager, p_ext,
165 &ppsz_titles, &pi_ids ) == VLC_SUCCESS )
167 for( int i = 0; ppsz_titles[i] != NULL; ++i )
170 action = submenu->addAction( qfu( ppsz_titles[i] ) );
171 menuMapper->setMapping( action,
172 MENU_MAP( pi_ids[i], i_ext ) );
173 CONNECT( action, triggered(), menuMapper, map() );
177 action = submenu->addAction( qtr( "Empty" ) );
178 action->setEnabled( false );
183 msg_Warn( p_intf, "Could not get menu for extension '%s'",
185 action = submenu->addAction( qtr( "Empty" ) );
186 action->setEnabled( false );
189 submenu->addSeparator();
190 action = submenu->addAction( QIcon( ":/menu/quit" ),
191 qtr( "Deactivate" ) );
192 menuMapper->setMapping( action, MENU_MAP( 0, i_ext ) );
193 CONNECT( action, triggered(), menuMapper, map() );
197 action = current->addAction( qfu( p_ext->psz_title ) );
198 menuMapper->setMapping( action, MENU_MAP( 0, i_ext ) );
199 CONNECT( action, triggered(), menuMapper, map() );
201 if( !extension_TriggerOnly( p_extensions_manager, p_ext ) )
203 action->setCheckable( true );
204 action->setChecked( b_Active );
211 vlc_mutex_unlock( &p_extensions_manager->lock );
214 void ExtensionsManager::triggerMenu( int id )
216 uint16_t i_ext = MENU_GET_EXTENSION( id );
217 uint16_t i_action = MENU_GET_ACTION( id );
219 vlc_mutex_lock( &p_extensions_manager->lock );
221 if( (int) i_ext > p_extensions_manager->extensions.i_size )
223 msg_Dbg( p_intf, "can't trigger extension with wrong id %d",
228 extension_t *p_ext = ARRAY_VAL( p_extensions_manager->extensions, i_ext );
229 assert( p_ext != NULL);
231 vlc_mutex_unlock( &p_extensions_manager->lock );
235 msg_Dbg( p_intf, "activating or triggering extension '%s'",
238 if( extension_TriggerOnly( p_extensions_manager, p_ext ) )
240 extension_Trigger( p_extensions_manager, p_ext );
244 if( !extension_IsActivated( p_extensions_manager, p_ext ) )
245 extension_Activate( p_extensions_manager, p_ext );
247 extension_Deactivate( p_extensions_manager, p_ext );
252 msg_Dbg( p_intf, "triggering extension '%s', on menu with id = 0x%x",
253 p_ext->psz_title, i_action );
255 extension_TriggerMenu( p_extensions_manager, p_ext, i_action );
259 void ExtensionsManager::inputChanged( input_thread_t* p_input )
261 vlc_mutex_lock( &p_extensions_manager->lock );
264 FOREACH_ARRAY( p_ext, p_extensions_manager->extensions )
266 if( extension_IsActivated( p_extensions_manager, p_ext ) )
268 extension_SetInput( p_extensions_manager, p_ext, p_input );
273 vlc_mutex_unlock( &p_extensions_manager->lock );
276 void ExtensionsManager::playingChanged( int state )
278 vlc_mutex_lock( &p_extensions_manager->lock );
281 FOREACH_ARRAY( p_ext, p_extensions_manager->extensions )
283 if( extension_IsActivated( p_extensions_manager, p_ext ) )
285 extension_PlayingChanged( p_extensions_manager, p_ext, state );
290 vlc_mutex_unlock( &p_extensions_manager->lock );