QT4: implement the open dialog from the playlist
[vlc.git] / modules / gui / qt4 / dialogs_provider.cpp
1 /*****************************************************************************
2  * main_inteface.cpp : Main interface
3  *****************************************************************************
4  * Copyright (C) 2006-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
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 <QEvent>
26 #include <QApplication>
27 #include <QSignalMapper>
28 #include <QFileDialog>
29
30 #include "qt4.hpp"
31 #include "dialogs_provider.hpp"
32 #include "main_interface.hpp"
33 #include "menus.hpp"
34 #include <vlc_intf_strings.h>
35
36 /* The dialogs */
37 #include "dialogs/playlist.hpp"
38 #include "dialogs/preferences.hpp"
39 #include "dialogs/mediainfo.hpp"
40 #include "dialogs/messages.hpp"
41 #include "dialogs/extended.hpp"
42 #include "dialogs/sout.hpp"
43 #include "dialogs/open.hpp"
44 #include "dialogs/help.hpp"
45 #include "dialogs/gototime.hpp"
46
47 DialogsProvider* DialogsProvider::instance = NULL;
48
49 DialogsProvider::DialogsProvider( intf_thread_t *_p_intf ) :
50                                   QObject( NULL ), p_intf( _p_intf )
51 {
52     fixed_timer = new QTimer( this );
53     fixed_timer->start( 150 /* milliseconds */ );
54
55     menusMapper = new QSignalMapper();
56     CONNECT( menusMapper, mapped(QObject *), this, menuAction( QObject *) );
57
58     menusUpdateMapper = new QSignalMapper();
59     CONNECT( menusUpdateMapper, mapped(QObject *),
60              this, menuUpdateAction( QObject *) );
61
62     SDMapper = new QSignalMapper();
63     CONNECT( SDMapper, mapped (QString), this, SDMenuAction( QString ) );
64 }
65
66 DialogsProvider::~DialogsProvider()
67 {
68     PlaylistDialog::killInstance();
69     MediaInfoDialog::killInstance();
70 }
71
72 void DialogsProvider::quit()
73 {
74     p_intf->b_die = VLC_TRUE;
75     QApplication::quit();
76 }
77
78 void DialogsProvider::customEvent( QEvent *event )
79 {
80     if( event->type() == DialogEvent_Type )
81     {
82         DialogEvent *de = static_cast<DialogEvent*>(event);
83         switch( de->i_dialog )
84         {
85             case INTF_DIALOG_FILE_SIMPLE:
86             case INTF_DIALOG_FILE:
87                 openDialog(); break;
88             case INTF_DIALOG_DISC:
89                 openDiscDialog(); break;
90             case INTF_DIALOG_NET:
91                 openNetDialog(); break;
92             case INTF_DIALOG_SAT:
93             case INTF_DIALOG_CAPTURE:
94                 openCaptureDialog(); break;
95             case INTF_DIALOG_PLAYLIST:
96                 playlistDialog(); break;
97             case INTF_DIALOG_MESSAGES:
98                 messagesDialog(); break;
99             case INTF_DIALOG_FILEINFO:
100                mediaInfoDialog(); break;
101             case INTF_DIALOG_PREFS:
102                prefsDialog(); break;
103             case INTF_DIALOG_BOOKMARKS:
104                bookmarksDialog(); break;
105             case INTF_DIALOG_EXTENDED:
106                extendedDialog(); break;
107                /* We might want to make it better with custom functions */
108             case INTF_DIALOG_POPUPMENU:
109                QVLCMenu::PopupMenu( p_intf, (de->i_arg != 0) ); break;
110             case INTF_DIALOG_AUDIOPOPUPMENU:
111                QVLCMenu::AudioPopupMenu( p_intf ); break;
112             case INTF_DIALOG_VIDEOPOPUPMENU:
113                QVLCMenu::VideoPopupMenu( p_intf ); break;
114             case INTF_DIALOG_MISCPOPUPMENU:
115                QVLCMenu::MiscPopupMenu( p_intf ); break;
116             case INTF_DIALOG_INTERACTION:
117                doInteraction( de->p_arg ); break;
118             case INTF_DIALOG_VLM:
119                vlmDialog(); break;
120             case INTF_DIALOG_WIZARD:
121             case INTF_DIALOG_UPDATEVLC:
122             case INTF_DIALOG_EXIT:
123             default:
124                msg_Warn( p_intf, "unimplemented dialog\n" );
125         }
126     }
127 }
128
129 /****************************************************************************
130  * Individual simple dialogs
131  ****************************************************************************/
132 void DialogsProvider::playlistDialog()
133 {
134     PlaylistDialog::getInstance( p_intf )->toggleVisible();
135 }
136
137 void DialogsProvider::prefsDialog()
138 {
139     PrefsDialog::getInstance( p_intf )->toggleVisible();
140 }
141 void DialogsProvider::extendedDialog()
142 {
143     ExtendedDialog::getInstance( p_intf )->toggleVisible();
144 }
145
146 void DialogsProvider::messagesDialog()
147 {
148     MessagesDialog::getInstance( p_intf )->toggleVisible();
149 }
150
151 void DialogsProvider::gotoTimeDialog()
152 {
153     GotoTimeDialog::getInstance( p_intf )->toggleVisible();
154 }
155
156 void DialogsProvider::vlmDialog()
157 {
158     /* FIXME */
159     /*  VLMDialog::getInstance( p_intf )->toggleVisible(); */
160 }
161
162 void DialogsProvider::helpDialog()
163 {
164     HelpDialog::getInstance( p_intf )->toggleVisible();
165 }
166
167 void DialogsProvider::aboutDialog()
168 {
169     AboutDialog::getInstance( p_intf )->toggleVisible();
170 }
171
172 void DialogsProvider::mediaInfoDialog()
173 {
174     MediaInfoDialog::getInstance( p_intf )->toggleVisible();
175 }
176
177 void DialogsProvider::mediaCodecDialog()
178 {
179     MediaInfoDialog::getInstance( p_intf )->showTab( 1 );
180 }
181
182 void DialogsProvider::bookmarksDialog()
183 {
184     /* FIXME */
185     /*  BookmarkDialog::getInstance( p_intf )->toggleVisible(); */
186 }
187
188 /****************************************************************************
189  * All the open/add stuff
190  ****************************************************************************/
191
192 void DialogsProvider::openDialog()
193 {
194     openDialog( OPEN_FILE_TAB );
195 }
196 void DialogsProvider::openFileDialog()
197 {
198     openDialog( OPEN_FILE_TAB );
199 }
200 void DialogsProvider::openDiscDialog()
201 {
202     openDialog( OPEN_DISC_TAB );
203 }
204 void DialogsProvider::openNetDialog()
205 {
206     openDialog( OPEN_NETWORK_TAB );
207 }
208 void DialogsProvider::openCaptureDialog()
209 {
210     openDialog( OPEN_CAPTURE_TAB );
211 }
212 void DialogsProvider::openDialog( int i_tab )
213 {
214     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf )->showTab( i_tab );
215 }
216
217 void DialogsProvider::PLAppendDialog()
218 {
219     
220     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, ENQUEUE )
221         ->showTab(0);
222 }
223 void DialogsProvider::MLAppendDialog()
224 {
225 }
226
227 /**** Simple open ****/
228 QStringList DialogsProvider::showSimpleOpen( QString help, int filters,
229                                              QString path )
230 {
231     QString fileTypes = "";
232     if( filters & EXT_FILTER_MEDIA ) {
233         ADD_FILTER_MEDIA( fileTypes );
234     }
235     if( filters & EXT_FILTER_VIDEO ) {
236         ADD_FILTER_VIDEO( fileTypes );
237     }
238     if( filters & EXT_FILTER_AUDIO ) {
239         ADD_FILTER_AUDIO( fileTypes );
240     }
241     if( filters & EXT_FILTER_PLAYLIST ) {
242         ADD_FILTER_PLAYLIST( fileTypes );
243     }
244     if( filters & EXT_FILTER_SUBTITLE ) {
245         ADD_FILTER_SUBTITLE( fileTypes );
246     }
247     ADD_FILTER_ALL( fileTypes );
248     fileTypes.replace(QString(";*"), QString(" *"));
249     return QFileDialog::getOpenFileNames( NULL,
250         help.isNull() ? qfu(I_OP_SEL_FILES ) : help,
251         path.isNull() ? qfu( p_intf->p_libvlc->psz_homedir ) : path,
252         fileTypes );
253 }
254
255 void DialogsProvider::addFromSimple( bool pl, bool go)
256 {
257     QStringList files = DialogsProvider::showSimpleOpen();
258     int i = 0;
259     foreach( QString file, files )
260     {
261         const char * psz_utf8 = qtu( file );
262         playlist_Add( THEPL, psz_utf8, NULL,
263                       go ? ( PLAYLIST_APPEND | ( i ? 0 : PLAYLIST_GO ) |
264                                                ( i ? PLAYLIST_PREPARSE : 0 ) )
265                          : ( PLAYLIST_APPEND | PLAYLIST_PREPARSE ),
266                       PLAYLIST_END,
267                       pl ? VLC_TRUE : VLC_FALSE, VLC_FALSE );
268         i++;
269     }
270 }
271
272 void DialogsProvider::simplePLAppendDialog()
273 {
274     addFromSimple( true, false );
275 }
276
277 void DialogsProvider::simpleMLAppendDialog()
278 {
279     addFromSimple( false, false );
280 }
281
282 void DialogsProvider::simpleOpenDialog()
283 {
284     addFromSimple( true, true );
285 }
286
287 /****************
288  * Playlist     *
289  ****************/
290 void DialogsProvider::openPlaylist()
291 {
292     QStringList files = showSimpleOpen( qtr( "Open playlist file" ),
293                                         EXT_FILTER_PLAYLIST );
294     foreach( QString file, files )
295     {
296         playlist_Import( THEPL, qtu(file) );
297     }
298 }
299
300 void DialogsProvider::savePlaylist()
301 {
302     QFileDialog *qfd = new QFileDialog( NULL,
303                                    qtr("Choose a filename to save playlist"),
304                                    qfu( p_intf->p_libvlc->psz_homedir ),
305                                    qtr("XSPF playlist (*.xspf);; ") +
306                                    qtr("M3U playlist (*.m3u);; Any (*.*) ") );
307     qfd->setFileMode( QFileDialog::AnyFile );
308     qfd->setAcceptMode( QFileDialog::AcceptSave );
309     qfd->setConfirmOverwrite( true );
310
311     if( qfd->exec() == QDialog::Accepted )
312     {
313         if( qfd->selectedFiles().count() > 0 )
314         {
315             char *psz_module, *psz_m3u = "export-m3u",
316                  *psz_xspf = "export-xspf";
317
318             QString file = qfd->selectedFiles().first();
319             QString filter = qfd->selectedFilter();
320
321             if( file.contains(".xsp") ||
322                 ( filter.contains(".xspf") && !file.contains(".m3u") ) )
323             {
324                 psz_module = psz_xspf;
325                 if( !file.contains( ".xsp" ) )
326                     file.append( ".xspf" );
327             }
328             else
329             {
330                 psz_module = psz_m3u;
331                 if( !file.contains( ".m3u" ) )
332                     file.append( ".m3u" );
333             }
334
335             playlist_Export( THEPL, qtu(file), THEPL->p_local_category,
336                              psz_module);
337         }
338     }
339     delete qfd;
340 }
341
342 static void openDirectory( intf_thread_t* p_intf, bool pl, bool go )
343 {
344     QString dir = QFileDialog::getExistingDirectory ( 0,
345                                                      _("Open directory") );
346     input_item_t *p_input = input_ItemNewExt( THEPL, qtu(dir), NULL,
347                                                0, NULL, -1 );
348     playlist_AddInput( THEPL, p_input,
349                        go ? ( PLAYLIST_APPEND | PLAYLIST_GO ) : PLAYLIST_APPEND,
350                        PLAYLIST_END, pl, VLC_FALSE );
351     input_Read( THEPL, p_input, VLC_FALSE );
352 }
353
354 void DialogsProvider::PLAppendDir()
355 {
356     openDirectory( p_intf, true, false );
357 }
358
359 void DialogsProvider::MLAppendDir()
360 {
361     openDirectory( p_intf, false , false );
362 }
363
364
365 /****************************************************************************
366  * Sout emulation
367  ****************************************************************************/
368
369 void DialogsProvider::streamingDialog( QString mrl, bool b_transcode_only )
370 {
371     SoutDialog *s = new SoutDialog( p_intf->p_sys->p_mi, p_intf,
372                                                     b_transcode_only );
373     if( s->exec() == QDialog::Accepted )
374     {
375         msg_Err(p_intf, "mrl %s\n", qta(s->mrl));
376         /* Just do it */
377         int i_len = strlen( qtu(s->mrl) ) + 10;
378         char *psz_option = (char*)malloc(i_len);
379         snprintf( psz_option, i_len - 1, ":sout=%s", qtu(s->mrl));
380
381         playlist_AddExt( THEPL, qtu( mrl ), "Streaming",
382                          PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END,
383                         -1, &psz_option, 1, VLC_TRUE, VLC_FALSE );
384     }
385     delete s;
386 }
387
388 void DialogsProvider::openThenStreamingDialogs()
389 {
390     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, OPEN_AND_STREAM )
391                                 ->showTab( 0 );
392 }
393
394 void DialogsProvider::openThenTranscodingDialogs()
395 {
396     OpenDialog::getInstance( p_intf->p_sys->p_mi , p_intf, OPEN_AND_SAVE )
397                                 ->showTab( 0 );
398 }
399 /*
400 void DialogsProvider::streamingDialog()
401 {
402     OpenDialog *o = new OpenDialog( p_intf->p_sys->p_mi, p_intf, true );
403     if ( o->exec() == QDialog::Accepted )
404     {
405         SoutDialog *s = new SoutDialog( p_intf->p_sys->p_mi, p_intf );
406         if( s->exec() == QDialog::Accepted )
407         {
408             msg_Err(p_intf, "mrl %s\n", qta(s->mrl));
409             /* Just do it  
410             int i_len = strlen( qtu(s->mrl) ) + 10;
411             char *psz_option = (char*)malloc(i_len);
412             snprintf( psz_option, i_len - 1, ":sout=%s", qtu(s->mrl));
413
414             playlist_AddExt( THEPL, qtu( o->mrl ), "Streaming",
415                              PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END,
416                              -1, &psz_option, 1, VLC_TRUE, VLC_FALSE );
417         }
418         delete s;
419     }
420     delete o;
421 }*/
422
423
424
425 /****************************************************************************
426  * Menus / Interaction
427  ****************************************************************************/
428
429 void DialogsProvider::menuAction( QObject *data )
430 {
431     QVLCMenu::DoAction( p_intf, data );
432 }
433
434 void DialogsProvider::menuUpdateAction( QObject *data )
435 {
436     MenuFunc * f = qobject_cast<MenuFunc *>(data);
437     f->doFunc( p_intf );
438 }
439
440 void DialogsProvider::SDMenuAction( QString data )
441 {
442     char *psz_sd = strdup( qtu( data ) );
443     if( !playlist_IsServicesDiscoveryLoaded( THEPL, psz_sd ) )
444         playlist_ServicesDiscoveryAdd( THEPL, psz_sd );
445     else
446         playlist_ServicesDiscoveryRemove( THEPL, psz_sd );
447
448     free( psz_sd );
449 }
450
451
452 void DialogsProvider::doInteraction( intf_dialog_args_t *p_arg )
453 {
454     InteractionDialog *qdialog;
455     interaction_dialog_t *p_dialog = p_arg->p_dialog;
456     switch( p_dialog->i_action )
457     {
458     case INTERACT_NEW:
459         qdialog = new InteractionDialog( p_intf, p_dialog );
460         p_dialog->p_private = (void*)qdialog;
461         if( !(p_dialog->i_status == ANSWERED_DIALOG) )
462             qdialog->show();
463         break;
464     case INTERACT_UPDATE:
465         qdialog = (InteractionDialog*)(p_dialog->p_private);
466         if( qdialog)
467             qdialog->update();
468         break;
469     case INTERACT_HIDE:
470         qdialog = (InteractionDialog*)(p_dialog->p_private);
471         if( qdialog )
472             qdialog->hide();
473         p_dialog->i_status = HIDDEN_DIALOG;
474         break;
475     case INTERACT_DESTROY:
476         qdialog = (InteractionDialog*)(p_dialog->p_private);
477         if( !p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
478             delete qdialog;
479         p_dialog->i_status = DESTROYED_DIALOG;
480         break;
481     }
482 }
483
484 void DialogsProvider::hideMenus()
485 {
486     /* TODO */
487 }
488
489 void DialogsProvider::switchToSkins()
490 {
491     var_SetString( p_intf, "intf-switch", "skins2" );
492 }