]> git.sesse.net Git - vlc/blob - modules/gui/qt4/extensions_manager.cpp
Qt: show extension short description in menu
[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(
157                     qfu( p_ext->psz_shortdescription ? p_ext->psz_shortdescription: p_ext->psz_title ),
158                     current );
159             char **ppsz_titles = NULL;
160             uint16_t *pi_ids = NULL;
161             size_t i_num = 0;
162             action = current->addMenu( submenu );
163
164             action->setCheckable( true );
165             action->setChecked( true );
166
167             if( extension_GetMenu( p_extensions_manager, p_ext,
168                                    &ppsz_titles, &pi_ids ) == VLC_SUCCESS )
169             {
170                 for( int i = 0; ppsz_titles[i] != NULL; ++i )
171                 {
172                     ++i_num;
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] );
178                 }
179                 if( !i_num )
180                 {
181                     action = submenu->addAction( qtr( "Empty" ) );
182                     action->setEnabled( false );
183                 }
184                 free( ppsz_titles );
185                 free( pi_ids );
186             }
187             else
188             {
189                 msg_Warn( p_intf, "Could not get menu for extension '%s'",
190                           p_ext->psz_title );
191                 action = submenu->addAction( qtr( "Empty" ) );
192                 action->setEnabled( false );
193             }
194
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() );
200         }
201         else
202         {
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() );
207
208             if( !extension_TriggerOnly( p_extensions_manager, p_ext ) )
209             {
210                 action->setCheckable( true );
211                 action->setChecked( b_Active );
212             }
213         }
214         i_ext++;
215     }
216     FOREACH_END()
217
218     vlc_mutex_unlock( &p_extensions_manager->lock );
219 }
220
221 void ExtensionsManager::triggerMenu( int id )
222 {
223     uint16_t i_ext = MENU_GET_EXTENSION( id );
224     uint16_t i_action = MENU_GET_ACTION( id );
225
226     vlc_mutex_lock( &p_extensions_manager->lock );
227
228     if( (int) i_ext > p_extensions_manager->extensions.i_size )
229     {
230         msg_Dbg( p_intf, "can't trigger extension with wrong id %d",
231                  (int) i_ext );
232         vlc_mutex_unlock( &p_extensions_manager->lock );
233         return;
234     }
235
236     extension_t *p_ext = ARRAY_VAL( p_extensions_manager->extensions, i_ext );
237     assert( p_ext != NULL);
238
239     vlc_mutex_unlock( &p_extensions_manager->lock );
240
241     if( i_action == 0 )
242     {
243         msg_Dbg( p_intf, "activating or triggering extension '%s'",
244                  p_ext->psz_title );
245
246         if( extension_TriggerOnly( p_extensions_manager, p_ext ) )
247         {
248             extension_Trigger( p_extensions_manager, p_ext );
249         }
250         else
251         {
252             if( !extension_IsActivated( p_extensions_manager, p_ext ) )
253                 extension_Activate( p_extensions_manager, p_ext );
254             else
255                 extension_Deactivate( p_extensions_manager, p_ext );
256         }
257     }
258     else
259     {
260         msg_Dbg( p_intf, "triggering extension '%s', on menu with id = 0x%x",
261                  p_ext->psz_title, i_action );
262
263         extension_TriggerMenu( p_extensions_manager, p_ext, i_action );
264     }
265 }
266
267 void ExtensionsManager::inputChanged( input_thread_t* p_input )
268 {
269     //This is unlikely, but can happen if no extension modules can be loaded.
270     if ( p_extensions_manager == NULL )
271         return ;
272     vlc_mutex_lock( &p_extensions_manager->lock );
273
274     extension_t *p_ext;
275     FOREACH_ARRAY( p_ext, p_extensions_manager->extensions )
276     {
277         if( extension_IsActivated( p_extensions_manager, p_ext ) )
278         {
279             extension_SetInput( p_extensions_manager, p_ext, p_input );
280         }
281     }
282     FOREACH_END()
283
284     vlc_mutex_unlock( &p_extensions_manager->lock );
285 }
286
287 void ExtensionsManager::playingChanged( int state )
288 {
289     //This is unlikely, but can happen if no extension modules can be loaded.
290     if ( p_extensions_manager == NULL )
291         return ;
292     vlc_mutex_lock( &p_extensions_manager->lock );
293
294     extension_t *p_ext;
295     FOREACH_ARRAY( p_ext, p_extensions_manager->extensions )
296     {
297         if( extension_IsActivated( p_extensions_manager, p_ext ) )
298         {
299             extension_PlayingChanged( p_extensions_manager, p_ext, state );
300         }
301     }
302     FOREACH_END()
303
304     vlc_mutex_unlock( &p_extensions_manager->lock );
305 }
306
307 void ExtensionsManager::metaChanged( input_item_t* )
308 {
309     //This is unlikely, but can happen if no extension modules can be loaded.
310     if ( p_extensions_manager == NULL )
311         return ;
312     vlc_mutex_lock( &p_extensions_manager->lock );
313     extension_t *p_ext;
314     FOREACH_ARRAY( p_ext, p_extensions_manager->extensions )
315     {
316         if( extension_IsActivated( p_extensions_manager, p_ext ) )
317         {
318             extension_MetaChanged( p_extensions_manager, p_ext );
319         }
320     }
321     FOREACH_END()
322     vlc_mutex_unlock( &p_extensions_manager->lock );
323 }