]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/dialogs.cpp
e08097af08191213e9c9ed877a524171d99401dd
[vlc] / modules / gui / skins2 / src / dialogs.cpp
1 /*****************************************************************************
2  * dialogs.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 #include "dialogs.hpp"
26 #include "../commands/async_queue.hpp"
27 #include "../commands/cmd_change_skin.hpp"
28 #include "../commands/cmd_quit.hpp"
29
30
31 /// Callback called when a new skin is chosen
32 static void showChangeSkinCB( intf_dialog_args_t *pArg )
33 {
34     intf_thread_t *pIntf = (intf_thread_t *)pArg->p_arg;
35
36     if( pArg->i_results )
37     {
38         if( pArg->psz_results[0] )
39         {
40             // Create a change skin command
41             CmdChangeSkin *pCmd = new CmdChangeSkin( pIntf,
42                                                      pArg->psz_results[0] );
43
44             // Push the command in the asynchronous command queue
45             AsyncQueue *pQueue = AsyncQueue::instance( pIntf );
46             pQueue->remove( "change skin" );
47             pQueue->push( CmdGenericPtr( pCmd ) );
48         }
49     }
50     else if( !pIntf->p_sys->p_theme )
51     {
52         // If no theme is already loaded, it's time to quit!
53         CmdQuit *pCmd = new CmdQuit( pIntf );
54         AsyncQueue *pQueue = AsyncQueue::instance( pIntf );
55         pQueue->push( CmdGenericPtr( pCmd ) );
56     }
57 }
58
59
60 /// Callback called when the popup menu is requested
61 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
62                         vlc_value_t old_val, vlc_value_t new_val, void *param )
63 {
64     Dialogs *p_dialogs = (Dialogs *)param;
65     p_dialogs->showPopupMenu( new_val.b_bool );
66
67     return VLC_SUCCESS;
68 }
69
70
71 Dialogs::Dialogs( intf_thread_t *pIntf ):
72     SkinObject( pIntf ), m_pProvider( NULL ), m_pModule( NULL )
73 {
74 }
75
76
77 Dialogs::~Dialogs()
78 {
79     if( m_pProvider && m_pModule )
80     {
81         // Detach the dialogs provider from its parent interface
82         vlc_object_detach( m_pProvider );
83
84         module_Unneed( m_pProvider, m_pModule );
85         vlc_object_destroy( m_pProvider );
86     }
87
88     /* Unregister callbacks */
89     var_DelCallback( getIntf()->p_sys->p_playlist, "intf-popupmenu",
90                      PopupMenuCB, this );
91 }
92
93
94 Dialogs *Dialogs::instance( intf_thread_t *pIntf )
95 {
96     if( ! pIntf->p_sys->p_dialogs )
97     {
98         Dialogs *pDialogs = new Dialogs( pIntf );
99         if( pDialogs->init() )
100         {
101             // Initialization succeeded
102             pIntf->p_sys->p_dialogs = pDialogs;
103         }
104         else
105         {
106             // Initialization failed
107             delete pDialogs;
108         }
109     }
110     return pIntf->p_sys->p_dialogs;
111 }
112
113
114 void Dialogs::destroy( intf_thread_t *pIntf )
115 {
116     if( pIntf->p_sys->p_dialogs )
117     {
118         delete pIntf->p_sys->p_dialogs;
119         pIntf->p_sys->p_dialogs = NULL;
120     }
121 }
122
123
124 bool Dialogs::init()
125 {
126     // Allocate descriptor
127     m_pProvider = (intf_thread_t *)vlc_object_create( getIntf(),
128                                                       VLC_OBJECT_DIALOGS );
129     if( m_pProvider == NULL )
130     {
131         msg_Err( getIntf(), "out of memory" );
132         return false;
133     }
134
135     m_pModule = module_Need( m_pProvider, "dialogs provider", NULL, 0 );
136     if( m_pModule == NULL )
137     {
138         msg_Err( getIntf(), "No suitable dialogs provider found" );
139         vlc_object_destroy( m_pProvider );
140         m_pProvider = NULL;
141         return false;
142     }
143
144     // Attach the dialogs provider to its parent interface
145     vlc_object_attach( m_pProvider, getIntf() );
146
147     // Initialize dialogs provider
148     // (returns as soon as initialization is done)
149     if( m_pProvider->pf_run )
150     {
151         m_pProvider->pf_run( m_pProvider );
152     }
153
154     /* Register callback for the intf-popupmenu variable */
155     var_AddCallback( getIntf()->p_sys->p_playlist, "intf-popupmenu",
156                      PopupMenuCB, this );
157
158     return true;
159 }
160
161
162 void Dialogs::showChangeSkin()
163 {
164     if( m_pProvider && m_pProvider->pf_show_dialog )
165     {
166         intf_dialog_args_t *p_arg =
167             (intf_dialog_args_t *)malloc( sizeof(intf_dialog_args_t) );
168         memset( p_arg, 0, sizeof(intf_dialog_args_t) );
169
170         p_arg->b_blocking = false;
171
172         p_arg->psz_title = strdup( _("Open a skin file") );
173         p_arg->psz_extensions =
174             strdup( _("Skin files (*.vlt)|*.vlt|Skin files (*.xml)|*.xml|") );
175
176         p_arg->p_arg = getIntf();
177         p_arg->pf_callback = showChangeSkinCB;
178
179         m_pProvider->pf_show_dialog( m_pProvider, INTF_DIALOG_FILE_GENERIC,
180                                      0, p_arg );
181     }
182 }
183
184
185 void Dialogs::showFileSimple( bool play )
186 {
187     if( m_pProvider && m_pProvider->pf_show_dialog )
188     {
189         m_pProvider->pf_show_dialog( m_pProvider, INTF_DIALOG_FILE_SIMPLE,
190                                      (int)play, 0 );
191     }
192 }
193
194
195 void Dialogs::showFile( bool play )
196 {
197     if( m_pProvider && m_pProvider->pf_show_dialog )
198     {
199         m_pProvider->pf_show_dialog( m_pProvider, INTF_DIALOG_FILE,
200                                      (int)play, 0 );
201     }
202 }
203
204
205 void Dialogs::showDisc( bool play )
206 {
207     if( m_pProvider && m_pProvider->pf_show_dialog )
208     {
209         m_pProvider->pf_show_dialog( m_pProvider, INTF_DIALOG_DISC,
210                                      (int)play, 0 );
211     }
212 }
213
214
215 void Dialogs::showNet( bool play )
216 {
217     if( m_pProvider && m_pProvider->pf_show_dialog )
218     {
219         m_pProvider->pf_show_dialog( m_pProvider, INTF_DIALOG_NET,
220                                      (int)play, 0 );
221     }
222 }
223
224
225 void Dialogs::showMessages()
226 {
227     if( m_pProvider && m_pProvider->pf_show_dialog )
228     {
229         m_pProvider->pf_show_dialog( m_pProvider, INTF_DIALOG_MESSAGES, 0, 0 );
230     }
231 }
232
233
234 void Dialogs::showPrefs()
235 {
236     if( m_pProvider && m_pProvider->pf_show_dialog )
237     {
238         m_pProvider->pf_show_dialog( m_pProvider, INTF_DIALOG_PREFS, 0, 0 );
239     }
240 }
241
242
243 void Dialogs::showFileInfo()
244 {
245    if( m_pProvider && m_pProvider->pf_show_dialog )
246     {
247        m_pProvider->pf_show_dialog( m_pProvider, INTF_DIALOG_FILEINFO, 0, 0 );
248     }
249 }
250
251
252 void Dialogs::showPopupMenu( bool bShow )
253 {
254     if( m_pProvider && m_pProvider->pf_show_dialog )
255     {
256         m_pProvider->pf_show_dialog( m_pProvider, INTF_DIALOG_POPUPMENU,
257                                      (int)bShow, 0 );
258     }
259 }
260