open.cpp 16.1 KB
Newer Older
1 2
/*****************************************************************************
 * open.cpp : Advanced open dialog
3
 *****************************************************************************
4
 * Copyright © 2006-2011 the VideoLAN team
5 6 7 8 9 10
 *
 * Authors: Jean-Baptiste Kempf <jb@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
11
 * (at your option) any later version.
12 13 14 15 16 17 18 19
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
20 21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
22

23 24 25
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
26

27
#include "dialogs/open.hpp"
28
#include "dialogs_provider.hpp"
Ludovic Fauvet's avatar
Ludovic Fauvet committed
29
#include "recents.hpp"
30
#include "util/qt_dirs.hpp"
Ludovic Fauvet's avatar
Ludovic Fauvet committed
31

32
#include <QTabWidget>
33
#include <QRegExp>
34
#include <QMenu>
35

36 37 38
#ifndef NDEBUG
# define DEBUG_QT 1
#endif
39

40 41
OpenDialog *OpenDialog::instance = NULL;

42
OpenDialog* OpenDialog::getInstance( QWidget *parent, intf_thread_t *p_intf,
43
        bool b_rawInstance, int _action_flag, bool b_selectMode, bool _b_pl )
44 45 46
{
    /* Creation */
    if( !instance )
47 48
        instance = new OpenDialog( parent, p_intf, b_selectMode,
                                   _action_flag, _b_pl );
49
    else if( !b_rawInstance )
50 51
    {
        /* Request the instance but change small details:
52
           - Button menu */
53 54 55
        if( b_selectMode )
            _action_flag = SELECT; /* This should be useless, but we never know
                                      if the call is correct */
56
        instance->setWindowModality( Qt::WindowModal );
57
        instance->i_action_flag = _action_flag;
58
        instance->b_pl = _b_pl;
59 60 61 62 63 64 65
        instance->setMenuAction();
    }
    return instance;
}

OpenDialog::OpenDialog( QWidget *parent,
                        intf_thread_t *_p_intf,
66
                        bool b_selectMode,
67 68
                        int _action_flag,
                        bool _b_pl)  :  QVLCDialog( parent, _p_intf )
69
{
70
    i_action_flag = _action_flag;
71
    b_pl =_b_pl;
72

73
    if( b_selectMode ) /* Select mode */
74 75
        i_action_flag = SELECT;

76
    /* Basic Creation of the Window */
77
    ui.setupUi( this );
78
    setWindowTitle( qtr( "Open Media" ) );
Nick Pope's avatar
Nick Pope committed
79
    setWindowRole( "vlc-open-media" );
80
    setWindowModality( Qt::WindowModal );
81

82
    /* Tab definition and creation */
83 84 85 86
    fileOpenPanel    = new FileOpenPanel( this, p_intf );
    discOpenPanel    = new DiscOpenPanel( this, p_intf );
    netOpenPanel     = new NetOpenPanel( this, p_intf );
    captureOpenPanel = new CaptureOpenPanel( this, p_intf );
87

88
    /* Insert the tabs */
89
    ui.Tab->insertTab( OPEN_FILE_TAB, fileOpenPanel, QIcon( ":/type/file-asym.svg" ),
90
                       qtr( "&File" ) );
91
    ui.Tab->insertTab( OPEN_DISC_TAB, discOpenPanel, QIcon( ":/type/disc.svg" ),
92
                       qtr( "&Disc" ) );
93
    ui.Tab->insertTab( OPEN_NETWORK_TAB, netOpenPanel, QIcon( ":/type/network.svg" ),
94
                       qtr( "&Network" ) );
95
    ui.Tab->insertTab( OPEN_CAPTURE_TAB, captureOpenPanel,
96
                       QIcon( ":/type/capture-card.svg" ), qtr( "Capture &Device" ) );
Clément Stenac's avatar
Clément Stenac committed
97

98 99 100 101 102
    /* Hide the Slave input widgets */
    ui.slaveLabel->hide();
    ui.slaveText->hide();
    ui.slaveBrowseButton->hide();

103
    /* Buttons Creation */
104
    /* Play Button */
105
    playButton = ui.playButton;
106

107
    /* Cancel Button */
108
    cancelButton = new QPushButton( qtr( "&Cancel" ) );
109

110
    /* Select Button */
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
111
    selectButton = new QPushButton( qtr( "&Select" ) );
112

113
    /* Menu for the Play button */
Hannes Domani's avatar
Hannes Domani committed
114
    QMenu * openButtonMenu = new QMenu( "Open", playButton );
115
    openButtonMenu->addAction( qtr( "&Enqueue" ), this, SLOT( enqueue() ),
116
                                    QKeySequence( "Alt+E" ) );
117
    openButtonMenu->addAction( qtr( "&Play" ), this, SLOT( play() ),
118
                                    QKeySequence( "Alt+P" ) );
119
    openButtonMenu->addAction( qtr( "&Stream" ), this, SLOT( stream() ) ,
120
                                    QKeySequence( "Alt+S" ) );
121 122
    openButtonMenu->addAction( qtr( "C&onvert" ), this, SLOT( transcode() ) ,
                                    QKeySequence( "Alt+O" ) );
123

124
    playButton->setMenu( openButtonMenu );
125

126 127
    /* Add the three Buttons */
    ui.buttonsBox->addButton( selectButton, QDialogButtonBox::AcceptRole );
128 129
    ui.buttonsBox->addButton( cancelButton, QDialogButtonBox::RejectRole );

130 131 132
    /* At creation time, modify the default buttons */
    setMenuAction();

Clément Stenac's avatar
Clément Stenac committed
133
    /* Force MRL update on tab change */
134
    CONNECT( ui.Tab, currentChanged( int ), this, signalCurrent( int ) );
Clément Stenac's avatar
Clément Stenac committed
135

136
    CONNECT( fileOpenPanel, mrlUpdated( const QStringList&, const QString& ),
137
             this, updateMRL( const QStringList&, const QString& ) );
138
    CONNECT( netOpenPanel, mrlUpdated( const QStringList&, const QString& ),
139
             this, updateMRL( const QStringList&, const QString& ) );
140
    CONNECT( discOpenPanel, mrlUpdated( const QStringList&, const QString& ),
141
             this, updateMRL( const QStringList&, const QString& ) );
142
    CONNECT( captureOpenPanel, mrlUpdated( const QStringList&, const QString& ),
143
             this, updateMRL( const QStringList&, const QString& ) );
144

145
    CONNECT( fileOpenPanel, methodChanged( const QString& ),
146
             this, newCachingMethod( const QString& ) );
147
    CONNECT( netOpenPanel, methodChanged( const QString& ),
148
             this, newCachingMethod( const QString& ) );
149
    CONNECT( discOpenPanel, methodChanged( const QString& ),
150
             this, newCachingMethod( const QString& ) );
151
    CONNECT( captureOpenPanel, methodChanged( const QString& ),
152
             this, newCachingMethod( const QString& ) );
Clément Stenac's avatar
Clément Stenac committed
153

154
    /* Advanced frame Connects */
155
    CONNECT( ui.slaveCheckbox, toggled( bool ), this, updateMRL() );
156
    CONNECT( ui.slaveText, textChanged( const QString& ), this, updateMRL() );
157
    CONNECT( ui.cacheSpinBox, valueChanged( int ), this, updateMRL() );
158
    CONNECT( ui.startTimeTimeEdit, timeChanged ( const QTime& ), this, updateMRL() );
159
    CONNECT( ui.stopTimeTimeEdit, timeChanged ( const QTime& ), this, updateMRL() );
160
    BUTTONACT( ui.advancedCheckBox, toggleAdvancedPanel() );
161
    BUTTONACT( ui.slaveBrowseButton, browseInputSlave() );
Clément Stenac's avatar
Clément Stenac committed
162

163
    /* Buttons action */
164
    BUTTONACT( playButton, selectSlots() );
165
    BUTTONACT( selectButton, close() );
166
    BUTTONACT( cancelButton, cancel() );
167

168
    /* Hide the advancedPanel */
169
    if( !getSettings()->value( "OpenDialog/advanced", false ).toBool())
170
    {
171
        ui.advancedFrame->hide();
172 173
        ui.advancedFrame->setEnabled( false );
    }
174 175
    else
        ui.advancedCheckBox->setChecked( true );
176

Clément Stenac's avatar
Clément Stenac committed
177 178
    /* Initialize caching */
    storedMethod = "";
179
    newCachingMethod( "file-caching" );
180

181 182
    /* enforce section due to .ui bug */
    ui.startTimeTimeEdit->setCurrentSection( QDateTimeEdit::SecondSection );
183
    ui.stopTimeTimeEdit->setCurrentSection( QDateTimeEdit::SecondSection );
184

185
    setMinimumSize( sizeHint() );
186
    setMaximumWidth( 900 );
187
    resize( getSettings()->value( "OpenDialog/size", QSize( 500, 400 ) ).toSize() );
188 189
}

190 191
/* Finish the dialog and decide if you open another one after */
void OpenDialog::setMenuAction()
192
{
193
    if( i_action_flag == SELECT )
194
    {
195 196
        playButton->hide();
        selectButton->show();
197
        selectButton->setDefault( true );
198 199 200 201 202 203 204 205 206
    }
    else
    {
        switch ( i_action_flag )
        {
        case OPEN_AND_STREAM:
            playButton->setText( qtr( "&Stream" ) );
            break;
        case OPEN_AND_SAVE:
207
            playButton->setText( qtr( "C&onvert / Save" ) );
208 209 210 211 212 213 214 215 216 217 218
            break;
        case OPEN_AND_ENQUEUE:
            playButton->setText( qtr( "&Enqueue" ) );
            break;
        case OPEN_AND_PLAY:
        default:
            playButton->setText( qtr( "&Play" ) );
        }
        playButton->show();
        selectButton->hide();
    }
219 220
}

221
OpenDialog::~OpenDialog()
222
{
223
    getSettings()->setValue( "OpenDialog/size", size() -
224 225
                 ( ui.advancedFrame->isEnabled() ?
                   QSize(0, ui.advancedFrame->height()) : QSize(0, 0) ) );
226
    getSettings()->setValue( "OpenDialog/advanced", ui.advancedFrame->isVisible() );
227 228
}

229 230 231 232
/* Used by VLM dialog and inputSlave selection */
QString OpenDialog::getMRL( bool b_all )
{
    if( itemsMRL.count() == 0 ) return "";
233
    return b_all ? itemsMRL[0] + getOptions()
234 235 236
                 : itemsMRL[0];
}

237 238 239 240 241 242 243 244 245
QStringList OpenDialog::getMRLs( bool b_with_options )
{
    if ( !b_with_options ) return itemsMRL;
    QStringList postfixedMRLs;
    foreach( const QString &mrl, itemsMRL )
        postfixedMRLs << QString( mrl ).append( getOptions() );
    return postfixedMRLs;
}

246 247 248 249 250 251
QString OpenDialog::getOptions()
{
    return ui.advancedLineInput->text();
}

void OpenDialog::showTab( int i_tab )
252
{
253
    if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
254 255
    ui.Tab->setCurrentIndex( i_tab );
    show();
256
    if( ui.Tab->currentWidget() != NULL )
257
    {
258
        OpenPanel *panel = qobject_cast<OpenPanel *>( ui.Tab->currentWidget() );
259 260 261
        assert( panel );
        panel->onFocus();
    }
Clément Stenac's avatar
Clément Stenac committed
262 263
}

264 265
void OpenDialog::toggleAdvancedPanel()
{
266 267
    if( ui.advancedFrame->isVisible() )
    {
268
        ui.advancedFrame->hide();
269
        ui.advancedFrame->setEnabled( false );
270
        if( size().isValid() )
271 272
            resize( size().width(), size().height()
                    - ui.advancedFrame->height() );
273 274 275
    }
    else
    {
276
        ui.advancedFrame->show();
277
        ui.advancedFrame->setEnabled( true );
278 279 280
        if( size().isValid() )
            resize( size().width(), size().height()
                    + ui.advancedFrame->height() );
281 282 283
    }
}

284 285 286 287 288 289 290 291 292 293 294 295 296 297
void OpenDialog::browseInputSlave()
{
    OpenDialog *od = new OpenDialog( this, p_intf, true, SELECT );
    od->exec();
    ui.slaveText->setText( od->getMRL( false ) );
    delete od;
}

/* Function called on signal currentChanged triggered */
void OpenDialog::signalCurrent( int i_tab )
{
    if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
    if( ui.Tab->currentWidget() != NULL )
    {
298
        OpenPanel *panel = qobject_cast<OpenPanel *>( ui.Tab->currentWidget() );
299 300 301
        assert( panel );
        panel->onFocus();
        panel->updateMRL();
302 303 304
        panel->updateContext( i_action_flag == OPEN_AND_PLAY ?
                              OpenPanel::CONTEXT_INTERACTIVE :
                              OpenPanel::CONTEXT_BATCH );
305 306 307
    }
}

308
/***********
309 310
 * Actions *
 ***********/
311
/* If Cancel is pressed or escaped */
312 313
void OpenDialog::cancel()
{
314
    /* Clear the panels */
315
    for( int i = 0; i < OPEN_TAB_MAX; i++ )
316
        qobject_cast<OpenPanel*>( ui.Tab->widget( i ) )->clear();
317 318

    /* Clear the variables */
319 320
    itemsMRL.clear();
    optionsMRL.clear();
321 322

    /* If in Select Mode, reject instead of hiding */
323
    if( i_action_flag == SELECT ) reject();
324
    else hide();
325 326
}

327 328
/* If EnterKey is pressed */
void OpenDialog::close()
329
{
330 331
    /* If in Select Mode, accept instead of selecting a Slot */
    if( i_action_flag == SELECT )
332 333 334
        accept();
    else
        selectSlots();
335
}
336 337

/* Play button */
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
void OpenDialog::selectSlots()
{
    switch ( i_action_flag )
    {
    case OPEN_AND_STREAM:
        stream();
        break;
    case OPEN_AND_SAVE:
        transcode();
        break;
    case OPEN_AND_ENQUEUE:
        enqueue();
        break;
    case OPEN_AND_PLAY:
    default:
        play();
    }
}

357
/* Play Action, called from selectSlots or play Menu */
358 359
void OpenDialog::play()
{
360
    enqueue( false );
361 362
}

363 364
/* Enqueue Action, called from selectSlots or enqueue Menu */
void OpenDialog::enqueue( bool b_enqueue )
365
{
366
    toggleVisible();
367

368
    if( i_action_flag == SELECT )
369
    {
370 371 372 373
        accept();
        return;
    }

374
    for( int i = 0; i < OPEN_TAB_MAX; i++ )
375
        qobject_cast<OpenPanel*>( ui.Tab->widget( i ) )->onAccept();
376

377 378 379
    /* Sort alphabetically */
    itemsMRL.sort();

380
    /* Go through the item list */
381
    for( int i = 0; i < itemsMRL.count(); i++ )
382 383
    {
        bool b_start = !i && !b_enqueue;
384

385
        /* Take options from the UI, not from what we stored */
386
        QStringList optionsList = getOptions().split( " :" );
387

388
        /* Switch between enqueuing and starting the item */
389
        Open::openMRLwithOptions( p_intf, itemsMRL[i], &optionsList, b_start, b_pl );
390
    }
391 392
}

393 394 395 396 397 398 399
void OpenDialog::transcode()
{
    stream( true );
}

void OpenDialog::stream( bool b_transcode_only )
{
400 401 402
//    QString soutMRL = getMRL( false );
//    if( soutMRL.isEmpty() ) return;

403 404 405
    for( int i = 0; i < OPEN_TAB_MAX; i++ )
        qobject_cast<OpenPanel*>( ui.Tab->widget( i ) )->onAccept();

406 407 408 409 410 411
    QStringList soutMRLS = getMRLs(false);
    if(soutMRLS.empty())
    {
        return;
    }

412
    toggleVisible();
413

414
    /* Dbg and send :D */
415 416 417 418 419 420
    msg_Dbg( p_intf, "MRL(s) passed to the Sout: %i", soutMRLS.length() );
    for(int i = 0; i < soutMRLS.length(); i++)
    {
        msg_Dbg( p_intf, "MRL(s) passed to the Sout: %s", qtu( soutMRLS[i] ) );
    }
    THEDP->streamingDialog( this, soutMRLS, b_transcode_only,
421
                            getOptions().split( " :" ) );
422
}
Clément Stenac's avatar
Clément Stenac committed
423

424
/* Update the MRL items from the panels */
425
void OpenDialog::updateMRL( const QStringList& item, const QString& tempMRL )
426
{
427 428
    optionsMRL = tempMRL;
    itemsMRL = item;
429
    updateMRL();
430 431
}

432 433 434 435 436
/* Format the time to milliseconds */
QString TimeToMilliseconds( const QTimeEdit *t ) {
    return QString::number( ( t->minimumTime().msecsTo( t->time() ) ) / 1000.0, 'f', 3);
}

437
/* Update the complete MRL */
Clément Stenac's avatar
Clément Stenac committed
438
void OpenDialog::updateMRL() {
439
    QString mrl = optionsMRL;
Clément Stenac's avatar
Clément Stenac committed
440 441 442
    if( ui.slaveCheckbox->isChecked() ) {
        mrl += " :input-slave=" + ui.slaveText->text();
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
443 444
    mrl += QString( " :%1=%2" ).arg( storedMethod ).
                                arg( ui.cacheSpinBox->value() );
445
    if( ui.startTimeTimeEdit->time() != ui.startTimeTimeEdit->minimumTime() ) {
446 447 448 449
        mrl += " :start-time=" + TimeToMilliseconds( ui.startTimeTimeEdit );
    }
    if( ui.stopTimeTimeEdit->time() > ui.startTimeTimeEdit->time() ) {
        mrl += " :stop-time=" + TimeToMilliseconds( ui.stopTimeTimeEdit );
450
    }
451
    ui.advancedLineInput->setText( mrl );
452
    ui.mrlLine->setText( itemsMRL.join( " " ) );
453 454 455
    /* Only allow action without valid items */
    playButton->setEnabled( !itemsMRL.isEmpty() );
    selectButton->setEnabled( !itemsMRL.isEmpty() );
Clément Stenac's avatar
Clément Stenac committed
456 457
}

458
/* Change the caching combobox */
459
void OpenDialog::newCachingMethod( const QString& method )
Clément Stenac's avatar
Clément Stenac committed
460 461 462
{
    if( method != storedMethod ) {
        storedMethod = method;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
463
        int i_value = var_InheritInteger( p_intf, qtu( storedMethod ) );
464
        ui.cacheSpinBox->setValue( i_value );
Clément Stenac's avatar
Clément Stenac committed
465 466
    }
}
467

468 469
/* Split the entries
 * FIXME! */
470
QStringList OpenDialog::SeparateEntries( const QString& entries )
471 472 473 474 475 476 477
{
    bool b_quotes_mode = false;

    QStringList entries_array;
    QString entry;

    int index = 0;
478
    while( index < entries.count() )
479
    {
480
        int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
481
        if( delim_pos < 0 ) delim_pos = entries.count() - 1;
482
        entry += entries.mid( index, delim_pos - index + 1 );
483 484 485 486 487 488 489
        index = delim_pos + 1;

        if( entry.isEmpty() ) continue;

        if( !b_quotes_mode && entry.endsWith( "\"" ) )
        {
            /* Enters quotes mode */
490
            entry.truncate( entry.count() - 1 );
491 492 493 494 495
            b_quotes_mode = true;
        }
        else if( b_quotes_mode && entry.endsWith( "\"" ) )
        {
            /* Finished the quotes mode */
496
            entry.truncate( entry.count() - 1 );
497 498 499 500 501
            b_quotes_mode = false;
        }
        else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
        {
            /* we found a non-quoted standalone string */
502
            if( index < entries.count() ||
503 504
                entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
                entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
505
                entry.truncate( entry.count() - 1 );
506
            if( !entry.isEmpty() ) entries_array.append( entry );
507
            entry.clear();
508 509 510 511 512 513 514 515 516
        }
        else
        {;}
    }

    if( !entry.isEmpty() ) entries_array.append( entry );

    return entries_array;
}
517