]> git.sesse.net Git - vlc/blob - modules/gui/qt4/extensions_manager.cpp
Qt4: remove useless alive check on input
[vlc] / modules / gui / qt4 / extensions_manager.cpp
1 /*****************************************************************************
2  * extensions_manager.cpp: Extensions manager for Qt
3  ****************************************************************************
4  * Copyright (C) 2009-2010 VideoLAN and authors
5  * $Id$
6  *
7  * Authors: Jean-Philippe AndrĂ© < jpeg # videolan.org >
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #include "extensions_manager.hpp"
25 #include "input_manager.hpp"
26 #include "dialogs/extensions.hpp"
27
28 #include <vlc_modules.h>
29 #include "assert.h"
30
31 #include <QMenu>
32 #include <QAction>
33 #include <QSignalMapper>
34 #include <QIcon>
35
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 ) )
39
40 ExtensionsManager* ExtensionsManager::instance = NULL;
41
42 ExtensionsManager::ExtensionsManager( intf_thread_t *_p_intf, QObject *parent )
43         : QObject( parent ), p_intf( _p_intf ), p_extensions_manager( NULL )
44         , p_edp( NULL )
45 {
46     assert( ExtensionsManager::instance == NULL );
47     instance = this;
48
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* ) );
56     b_unloading = false;
57     b_failed = false;
58 }
59
60 ExtensionsManager::~ExtensionsManager()
61 {
62     msg_Dbg( p_intf, "Killing extension dialog provider" );
63     ExtensionsDialogProvider::killInstance();
64     if( p_extensions_manager )
65     {
66         module_unneed( p_extensions_manager, p_extensions_manager->p_module );
67         vlc_object_release( p_extensions_manager );
68     }
69 }
70
71 bool ExtensionsManager::loadExtensions()
72 {
73     if( !p_extensions_manager )
74     {
75         p_extensions_manager = ( extensions_manager_t* )
76                     vlc_object_create( p_intf, sizeof( extensions_manager_t ) );
77         if( !p_extensions_manager )
78         {
79             b_failed = true;
80             emit extensionsUpdated();
81             return false;
82         }
83
84         p_extensions_manager->p_module =
85                 module_need( p_extensions_manager, "extension", NULL, false );
86
87         if( !p_extensions_manager->p_module )
88         {
89             msg_Err( p_intf, "Unable to load extensions module" );
90             vlc_object_release( p_extensions_manager );
91             p_extensions_manager = NULL;
92             b_failed = true;
93             emit extensionsUpdated();
94             return false;
95         }
96
97         /* Initialize dialog provider */
98         p_edp = ExtensionsDialogProvider::getInstance( p_intf,
99                                                        p_extensions_manager );
100         if( !p_edp )
101         {
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;
107             b_failed = true;
108             emit extensionsUpdated();
109             return false;
110         }
111         b_unloading = false;
112     }
113     b_failed = false;
114     emit extensionsUpdated();
115     return true;
116 }
117
118 void ExtensionsManager::unloadExtensions()
119 {
120     if( !p_extensions_manager )
121         return;
122     b_unloading = true;
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;
127 }
128
129 void ExtensionsManager::reloadExtensions()
130 {
131     unloadExtensions();
132     loadExtensions();
133     emit extensionsUpdated();
134 }
135
136 void ExtensionsManager::menu( QMenu *current )
137 {
138     assert( current != NULL );
139     if( !isLoaded() )
140     {
141         // This case can happen: do nothing
142         return;
143     }
144
145     vlc_mutex_lock( &p_extensions_manager->lock );
146
147     QAction *action;
148     extension_t *p_ext = NULL;
149     int i_ext = 0;
150     FOREACH_ARRAY( p_ext, p_extensions_manager->extensions )
151     {
152         bool b_Active = extension_IsActivated( p_extensions_manager, p_ext );
153
154         if( b_Active && extension_HasMenu( p_extensions_manager, p_ext ) )
155         {
156             QMenu *submenu = new QMenu( qfu( p_ext->psz_title ), current );
157             char **ppsz_titles = NULL;
158             uint16_t *pi_ids = NULL;
159             size_t i_num = 0;
160             action = current->addMenu( submenu );
161
162             action->setCheckable( true );
163             action->setChecked( true );
164
165             if( extension_GetMenu( p_extensions_manager, p_ext,
166                                    &ppsz_titles, &pi_ids ) == VLC_SUCCESS )
167             {
168                 for( int i = 0; ppsz_titles[i] != NULL; ++i )
169                 {
170                     ++i_num;
171                     action = submenu->addAction( qfu( ppsz_titles[i] ) );
172                     menuMapper->setMapping( action,
173                                             MENU_MAP( pi_ids[i], i_ext ) );
174                     CONNECT( action, triggered(), menuMapper, map() );
175                     free( ppsz_titles[i] );
176                 }
177                 if( !i_num )
178                 {
179                     action = submenu->addAction( qtr( "Empty" ) );
180                     action->setEnabled( false );
181                 }
182                 free( ppsz_titles );
183                 free( pi_ids );
184             }
185             else
186             {
187                 msg_Warn( p_intf, "Could not get menu for extension '%s'",
188                           p_ext->psz_title );
189                 action = submenu->addAction( qtr( "Empty" ) );
190                 action->setEnabled( false );
191             }
192
193             submenu->addSeparator();
194             action = submenu->addAction( QIcon( ":/menu/quit" ),
195                                          qtr( "Deactivate" ) );
196             menuMapper->setMapping( action, MENU_MAP( 0, i_ext ) );
197             CONNECT( action, triggered(), menuMapper, map() );
198         }
199         else
200         {
201             action = current->addAction( qfu( p_ext->psz_title ) );
202             menuMapper->setMapping( action, MENU_MAP( 0, i_ext ) );
203             CONNECT( action, triggered(), menuMapper, map() );
204
205             if( !extension_TriggerOnly( p_extensions_manager, p_ext ) )
206             {
207                 action->setCheckable( true );
208                 action->setChecked( b_Active );
209             }
210         }
211         i_ext++;
212     }
213     FOREACH_END()
214
215     vlc_mutex_unlock( &p_extensions_manager->lock );
216 }
217
218 void ExtensionsManager::triggerMenu( int id )
219 {
220     uint16_t i_ext = MENU_GET_EXTENSION( id );
221     uint16_t i_action = MENU_GET_ACTION( id );
222
223     vlc_mutex_lock( &p_extensions_manager->lock );
224
225     if( (int) i_ext > p_extensions_manager->extensions.i_size )
226     {
227         msg_Dbg( p_intf, "can't trigger extension with wrong id %d",
228                  (int) i_ext );
229         return;
230     }
231
232     extension_t *p_ext = ARRAY_VAL( p_extensions_manager->extensions, i_ext );
233     assert( p_ext != NULL);
234
235     vlc_mutex_unlock( &p_extensions_manager->lock );
236
237     if( i_action == 0 )
238     {
239         msg_Dbg( p_intf, "activating or triggering extension '%s'",
240                  p_ext->psz_title );
241
242         if( extension_TriggerOnly( p_extensions_manager, p_ext ) )
243         {
244             extension_Trigger( p_extensions_manager, p_ext );
245         }
246         else
247         {
248             if( !extension_IsActivated( p_extensions_manager, p_ext ) )
249                 extension_Activate( p_extensions_manager, p_ext );
250             else
251                 extension_Deactivate( p_extensions_manager, p_ext );
252         }
253     }
254     else
255     {
256         msg_Dbg( p_intf, "triggering extension '%s', on menu with id = 0x%x",
257                  p_ext->psz_title, i_action );
258
259         extension_TriggerMenu( p_extensions_manager, p_ext, i_action );
260     }
261 }
262
263 void ExtensionsManager::inputChanged( input_thread_t* p_input )
264 {
265     //This is unlikely, but can happen if no extension modules can be loaded.
266     if ( p_extensions_manager == NULL )
267         return ;
268     vlc_mutex_lock( &p_extensions_manager->lock );
269
270     extension_t *p_ext;
271     FOREACH_ARRAY( p_ext, p_extensions_manager->extensions )
272     {
273         if( extension_IsActivated( p_extensions_manager, p_ext ) )
274         {
275             extension_SetInput( p_extensions_manager, p_ext, p_input );
276         }
277     }
278     FOREACH_END()
279
280     vlc_mutex_unlock( &p_extensions_manager->lock );
281 }
282
283 void ExtensionsManager::playingChanged( int state )
284 {
285     //This is unlikely, but can happen if no extension modules can be loaded.
286     if ( p_extensions_manager == NULL )
287         return ;
288     vlc_mutex_lock( &p_extensions_manager->lock );
289
290     extension_t *p_ext;
291     FOREACH_ARRAY( p_ext, p_extensions_manager->extensions )
292     {
293         if( extension_IsActivated( p_extensions_manager, p_ext ) )
294         {
295             extension_PlayingChanged( p_extensions_manager, p_ext, state );
296         }
297     }
298     FOREACH_END()
299
300     vlc_mutex_unlock( &p_extensions_manager->lock );
301 }
302
303 void ExtensionsManager::metaChanged( input_item_t* )
304 {
305     //This is unlikely, but can happen if no extension modules can be loaded.
306     if ( p_extensions_manager == NULL )
307         return ;
308     vlc_mutex_lock( &p_extensions_manager->lock );
309     extension_t *p_ext;
310     FOREACH_ARRAY( p_ext, p_extensions_manager->extensions )
311     {
312         if( extension_IsActivated( p_extensions_manager, p_ext ) )
313         {
314             extension_MetaChanged( p_extensions_manager, p_ext );
315         }
316     }
317     FOREACH_END()
318     vlc_mutex_unlock( &p_extensions_manager->lock );
319 }