]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/dialogs.cpp
304172bcdcfdc1736480a20b99709fa30b1fce82
[vlc] / modules / gui / skins2 / src / dialogs.cpp
1 /*****************************************************************************
2  * dialogs.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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 #include "../commands/cmd_playlist.hpp"
30 #include "../commands/cmd_playtree.hpp"
31 #include <vlc_playlist.h>
32
33 /// Callback called when a new skin is chosen
34 void Dialogs::showChangeSkinCB( intf_dialog_args_t *pArg )
35 {
36     intf_thread_t *pIntf = (intf_thread_t *)pArg->p_arg;
37
38     if( pArg->i_results )
39     {
40         if( pArg->psz_results[0] )
41         {
42             // Create a change skin command
43             CmdChangeSkin *pCmd =
44                 new CmdChangeSkin( pIntf, sFromLocale( pArg->psz_results[0] ) );
45
46             // Push the command in the asynchronous command queue
47             AsyncQueue *pQueue = AsyncQueue::instance( pIntf );
48             pQueue->push( CmdGenericPtr( pCmd ) );
49         }
50     }
51     else if( !pIntf->p_sys->p_theme )
52     {
53         // If no theme is already loaded, it's time to quit!
54         CmdQuit *pCmd = new CmdQuit( pIntf );
55         AsyncQueue *pQueue = AsyncQueue::instance( pIntf );
56         pQueue->push( CmdGenericPtr( pCmd ) );
57     }
58 }
59
60 void Dialogs::showPlaylistLoadCB( intf_dialog_args_t *pArg )
61 {
62     intf_thread_t *pIntf = (intf_thread_t *)pArg->p_arg;
63
64     if( pArg->i_results && pArg->psz_results[0] )
65     {
66         // Create a Playlist Load command
67         CmdPlaylistLoad *pCmd =
68             new CmdPlaylistLoad( pIntf, sFromLocale( pArg->psz_results[0] ) );
69
70         // Push the command in the asynchronous command queue
71         AsyncQueue *pQueue = AsyncQueue::instance( pIntf );
72         pQueue->push( CmdGenericPtr( pCmd ) );
73     }
74 }
75
76
77 void Dialogs::showPlaylistSaveCB( intf_dialog_args_t *pArg )
78 {
79     intf_thread_t *pIntf = (intf_thread_t *)pArg->p_arg;
80
81     if( pArg->i_results && pArg->psz_results[0] )
82     {
83         // Create a Playlist Save command
84         CmdPlaylistSave *pCmd =
85             new CmdPlaylistSave( pIntf, pArg->psz_results[0] );
86
87         // Push the command in the asynchronous command queue
88         AsyncQueue *pQueue = AsyncQueue::instance( pIntf );
89         pQueue->push( CmdGenericPtr( pCmd ) );
90     }
91 }
92
93
94 /// Callback called when the popup menu is requested
95 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
96                         vlc_value_t old_val, vlc_value_t new_val, void *param )
97 {
98     Dialogs *p_dialogs = (Dialogs *)param;
99     p_dialogs->showPopupMenu( new_val.b_bool != 0, INTF_DIALOG_POPUPMENU );
100
101     return VLC_SUCCESS;
102 }
103
104
105 Dialogs::Dialogs( intf_thread_t *pIntf ):
106     SkinObject( pIntf ), m_pProvider( NULL ), m_pModule( NULL )
107 {
108 }
109
110
111 Dialogs::~Dialogs()
112 {
113     if( m_pProvider && m_pModule )
114     {
115         // Detach the dialogs provider from its parent interface
116         module_unneed( m_pProvider, m_pModule );
117         vlc_object_release( m_pProvider );
118     }
119
120     /* Unregister callbacks */
121     var_DelCallback( getIntf()->p_libvlc, "intf-popupmenu",
122                      PopupMenuCB, this );
123 }
124
125
126 Dialogs *Dialogs::instance( intf_thread_t *pIntf )
127 {
128     if( ! pIntf->p_sys->p_dialogs )
129     {
130         Dialogs *pDialogs = new Dialogs( pIntf );
131         if( pDialogs->init() )
132         {
133             // Initialization succeeded
134             pIntf->p_sys->p_dialogs = pDialogs;
135         }
136         else
137         {
138             // Initialization failed
139             delete pDialogs;
140         }
141     }
142     return pIntf->p_sys->p_dialogs;
143 }
144
145
146 void Dialogs::destroy( intf_thread_t *pIntf )
147 {
148     delete pIntf->p_sys->p_dialogs;
149     pIntf->p_sys->p_dialogs = NULL;
150 }
151
152
153 bool Dialogs::init()
154 {
155     // Allocate descriptor
156     m_pProvider = (intf_thread_t *)vlc_object_create( getIntf(),
157                                                     sizeof( intf_thread_t ) );
158     if( m_pProvider == NULL )
159         return false;
160
161     // Attach the dialogs provider to its parent interface
162     vlc_object_attach( m_pProvider, getIntf() );
163
164     m_pModule = module_need( m_pProvider, "dialogs provider", NULL, false );
165     if( m_pModule == NULL )
166     {
167         msg_Err( getIntf(), "no suitable dialogs provider found (hint: compile the qt4 plugin, and make sure it is loaded properly)" );
168         vlc_object_release( m_pProvider );
169         m_pProvider = NULL;
170         return false;
171     }
172
173     /* Register callback for the intf-popupmenu variable */
174     var_AddCallback( getIntf()->p_libvlc, "intf-popupmenu",
175                      PopupMenuCB, this );
176
177     return true;
178 }
179
180
181 void Dialogs::showFileGeneric( const string &rTitle, const string &rExtensions,
182                                DlgCallback callback, int flags )
183 {
184     if( m_pProvider && m_pProvider->pf_show_dialog )
185     {
186         intf_dialog_args_t *p_arg = (intf_dialog_args_t*)
187                                     calloc( 1, sizeof( intf_dialog_args_t ) );
188
189         p_arg->psz_title = strdup( rTitle.c_str() );
190         p_arg->psz_extensions = strdup( rExtensions.c_str() );
191
192         p_arg->b_save = flags & kSAVE;
193         p_arg->b_multiple = flags & kMULTIPLE;
194
195         p_arg->p_arg = getIntf();
196         p_arg->pf_callback = callback;
197
198         m_pProvider->pf_show_dialog( m_pProvider, INTF_DIALOG_FILE_GENERIC,
199                                      0, p_arg );
200     }
201 }
202
203
204 void Dialogs::showChangeSkin()
205 {
206     showFileGeneric( _("Open a skin file"),
207                      _("Skin files |*.vlt;*.wsz;*.xml"),
208                      showChangeSkinCB, kOPEN );
209 }
210
211
212 void Dialogs::showPlaylistLoad()
213 {
214     showFileGeneric( _("Open playlist"),
215                      _("Playlist Files|"EXTENSIONS_PLAYLIST"|"
216                        "All Files|*"),
217                      showPlaylistLoadCB, kOPEN );
218 }
219
220
221 void Dialogs::showPlaylistSave()
222 {
223     showFileGeneric( _("Save playlist"), _("XSPF playlist|*.xspf|"
224                                            "M3U file|*.m3u|"
225                                            "HTML playlist|*.html"),
226                      showPlaylistSaveCB, kSAVE );
227 }
228
229 void Dialogs::showPlaylist()
230 {
231     if( m_pProvider && m_pProvider->pf_show_dialog )
232     {
233         m_pProvider->pf_show_dialog( m_pProvider, INTF_DIALOG_PLAYLIST,
234                                      0, NULL );
235     }
236 }
237
238 void Dialogs::showFileSimple( bool play )
239 {
240     if( m_pProvider && m_pProvider->pf_show_dialog )
241     {
242         m_pProvider->pf_show_dialog( m_pProvider, INTF_DIALOG_FILE_SIMPLE,
243                                      (int)play, NULL );
244     }
245 }
246
247
248 void Dialogs::showFile( bool play )
249 {
250     if( m_pProvider && m_pProvider->pf_show_dialog )
251     {
252         m_pProvider->pf_show_dialog( m_pProvider, INTF_DIALOG_FILE,
253                                      (int)play, NULL );
254     }
255 }
256
257
258 void Dialogs::showDirectory( bool play )
259 {
260     if( m_pProvider && m_pProvider->pf_show_dialog )
261     {
262         m_pProvider->pf_show_dialog( m_pProvider, INTF_DIALOG_DIRECTORY,
263                                      (int)play, NULL );
264     }
265 }
266
267
268 void Dialogs::showDisc( bool play )
269 {
270     if( m_pProvider && m_pProvider->pf_show_dialog )
271     {
272         m_pProvider->pf_show_dialog( m_pProvider, INTF_DIALOG_DISC,
273                                      (int)play, NULL );
274     }
275 }
276
277
278 void Dialogs::showNet( bool play )
279 {
280     if( m_pProvider && m_pProvider->pf_show_dialog )
281     {
282         m_pProvider->pf_show_dialog( m_pProvider, INTF_DIALOG_NET,
283                                      (int)play, NULL );
284     }
285 }
286
287
288 void Dialogs::showMessages()
289 {
290     if( m_pProvider && m_pProvider->pf_show_dialog )
291     {
292         m_pProvider->pf_show_dialog( m_pProvider, INTF_DIALOG_MESSAGES, 0, NULL );
293     }
294 }
295
296
297 void Dialogs::showPrefs()
298 {
299     if( m_pProvider && m_pProvider->pf_show_dialog )
300     {
301         m_pProvider->pf_show_dialog( m_pProvider, INTF_DIALOG_PREFS, 0, NULL );
302     }
303 }
304
305
306 void Dialogs::showFileInfo()
307 {
308     if( m_pProvider && m_pProvider->pf_show_dialog )
309     {
310         m_pProvider->pf_show_dialog( m_pProvider, INTF_DIALOG_FILEINFO, 0, NULL );
311     }
312 }
313
314
315 void Dialogs::showStreamingWizard()
316 {
317     if( m_pProvider && m_pProvider->pf_show_dialog )
318     {
319         m_pProvider->pf_show_dialog( m_pProvider, INTF_DIALOG_WIZARD, 0, NULL );
320     }
321 }
322
323
324 void Dialogs::showPopupMenu( bool bShow, int popupType = INTF_DIALOG_POPUPMENU )
325 {
326     if( m_pProvider && m_pProvider->pf_show_dialog )
327     {
328         m_pProvider->pf_show_dialog( m_pProvider, popupType,
329                                      (int)bShow, NULL );
330     }
331 }
332
333 void Dialogs::showInteraction( interaction_dialog_t *p_dialog )
334 {
335     intf_dialog_args_t *p_arg = (intf_dialog_args_t *)
336                                 calloc( 1, sizeof(intf_dialog_args_t) );
337
338     p_arg->p_dialog = p_dialog;
339     p_arg->p_intf = getIntf();
340
341     if( m_pProvider && m_pProvider->pf_show_dialog )
342     {
343         m_pProvider->pf_show_dialog( m_pProvider, INTF_DIALOG_INTERACTION,
344                                      0, p_arg );
345     }
346 }