open.cpp 14.1 KB
Newer Older
1 2
/*****************************************************************************
 * open.cpp : Advanced open dialog
3
 *****************************************************************************
4
 * Copyright © 2006-2008 the VideoLAN team
5
 * $Id$
6 7 8 9 10 11
 *
 * 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
12
 * (at your option) any later version.
13 14 15 16 17 18 19 20
 *
 * 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
21 22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
23

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

28 29
#include "dialogs/open.hpp"

30 31
#include "dialogs_provider.hpp"

Ludovic Fauvet's avatar
Ludovic Fauvet committed
32 33
#include "recents.hpp"

34 35
#include <QTabWidget>
#include <QGridLayout>
36
#include <QRegExp>
37
#include <QMenu>
38 39 40

OpenDialog *OpenDialog::instance = NULL;

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

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

75 76 77 78
    /* Workaround the Win32 Vout that put the video on top at regular times */
#ifdef WIN32
    setWindowFlags( Qt::WindowStaysOnTopHint | Qt::Dialog );
#endif
79

80
    if( b_selectMode ) /* Select mode */
81 82
    {
        i_action_flag = SELECT;
83
        setWindowModality( Qt::WindowModal );
84 85
    }

86
    /* Basic Creation of the Window */
87
    ui.setupUi( this );
88
    setWindowTitle( qtr( "Open" ) );
89
    /* resize( 410, 600 ); */
90
    setMinimumSize( 520, 490 );
91

92
    /* Tab definition and creation */
93 94 95
    fileOpenPanel    = new FileOpenPanel( ui.Tab, p_intf );
    discOpenPanel    = new DiscOpenPanel( ui.Tab, p_intf );
    netOpenPanel     = new NetOpenPanel( ui.Tab, p_intf );
96
    captureOpenPanel = new CaptureOpenPanel( ui.Tab, p_intf );
97

98
    /* Insert the tabs */
99 100 101 102
    ui.Tab->insertTab( OPEN_FILE_TAB, fileOpenPanel, qtr( "&File" ) );
    ui.Tab->insertTab( OPEN_DISC_TAB, discOpenPanel, qtr( "&Disc" ) );
    ui.Tab->insertTab( OPEN_NETWORK_TAB, netOpenPanel, qtr( "&Network" ) );
    ui.Tab->insertTab( OPEN_CAPTURE_TAB, captureOpenPanel,
103
                       qtr( "Capture &Device" ) );
Clément Stenac's avatar
Clément Stenac committed
104

105 106 107 108 109
    /* Hide the Slave input widgets */
    ui.slaveLabel->hide();
    ui.slaveText->hide();
    ui.slaveBrowseButton->hide();

110
    /* Buttons Creation */
111
    /* Play Button */
112
    playButton = ui.playButton;
113

114
    /* Cancel Button */
115
    cancelButton = new QPushButton( qtr( "&Cancel" ) );
116

117
    /* Select Button */
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
118
    selectButton = new QPushButton( qtr( "&Select" ) );
119

120
    /* Menu for the Play button */
121
    QMenu * openButtonMenu = new QMenu( "Open" );
122
    openButtonMenu->addAction( qtr( "&Enqueue" ), this, SLOT( enqueue() ),
123
                                    QKeySequence( "Alt+E" ) );
124
    openButtonMenu->addAction( qtr( "&Play" ), this, SLOT( play() ),
125
                                    QKeySequence( "Alt+P" ) );
126
    openButtonMenu->addAction( qtr( "&Stream" ), this, SLOT( stream() ) ,
127
                                    QKeySequence( "Alt+S" ) );
128
    openButtonMenu->addAction( qtr( "&Convert" ), this, SLOT( transcode() ) ,
129
                                    QKeySequence( "Alt+C" ) );
130

131
    ui.menuButton->setMenu( openButtonMenu );
132

133 134
    /* Add the three Buttons */
    ui.buttonsBox->addButton( selectButton, QDialogButtonBox::AcceptRole );
135 136
    ui.buttonsBox->addButton( cancelButton, QDialogButtonBox::RejectRole );

137 138 139
    /* At creation time, modify the default buttons */
    setMenuAction();

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

143 144 145 146
    CONNECT( fileOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
    CONNECT( netOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
    CONNECT( discOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
    CONNECT( captureOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
147

Clément Stenac's avatar
Clément Stenac committed
148
    CONNECT( fileOpenPanel, methodChanged( QString ),
149
             this, newCachingMethod( QString ) );
Clément Stenac's avatar
Clément Stenac committed
150
    CONNECT( netOpenPanel, methodChanged( QString ),
151
             this, newCachingMethod( QString ) );
152
    CONNECT( discOpenPanel, methodChanged( QString ),
153
             this, newCachingMethod( QString ) );
154
    CONNECT( captureOpenPanel, methodChanged( QString ),
155
             this, newCachingMethod( QString ) );
Clément Stenac's avatar
Clément Stenac committed
156

157
    /* Advanced frame Connects */
158 159 160
    CONNECT( ui.slaveText, textChanged( QString ), this, updateMRL() );
    CONNECT( ui.cacheSpinBox, valueChanged( int ), this, updateMRL() );
    CONNECT( ui.startTimeSpinBox, valueChanged( int ), this, updateMRL() );
161
    BUTTONACT( ui.advancedCheckBox, toggleAdvancedPanel() );
162
    BUTTONACT( ui.slaveBrowseButton, browseInputSlave() );
Clément Stenac's avatar
Clément Stenac committed
163

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

169 170 171 172 173
    /* Hide the advancedPanel */
    if( !config_GetInt( p_intf, "qt-adv-options" ) )
        ui.advancedFrame->hide();
    else
        ui.advancedCheckBox->setChecked( true );
174

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

    resize( getSettings()->value( "opendialog-size", QSize( 520, 490 ) ).toSize() );
180 181 182
}

OpenDialog::~OpenDialog()
183 184 185
{
    getSettings()->setValue( "opendialog-size", size() );
}
186

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

219
void OpenDialog::showTab( int i_tab )
220
{
221
    if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
222
    ui.Tab->setCurrentIndex( i_tab );
223
    show();
224 225
}

226
/* Function called on signal currentChanged triggered */
227
void OpenDialog::signalCurrent( int i_tab )
228
{
229 230
    if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();

231 232
    if( ui.Tab->currentWidget() != NULL )
        ( dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() ) )->updateMRL();
Clément Stenac's avatar
Clément Stenac committed
233 234
}

235 236
void OpenDialog::toggleAdvancedPanel()
{
237 238
    if( ui.advancedFrame->isVisible() )
    {
239
        ui.advancedFrame->hide();
240
        //setMinimumSize( 520, 460 );
241
        if( size().isValid() )
242 243
            resize( size().width(), size().height()
                    - ui.advancedFrame->height() );
244 245 246
    }
    else
    {
247
        ui.advancedFrame->show();
248 249 250 251
        //setMinimumSize( 520, 460 + ui.advancedFrame->height() );
        if( size().isValid() )
            resize( size().width(), size().height()
                    + ui.advancedFrame->height() );
252 253 254
    }
}

255
/***********
256 257
 * Actions *
 ***********/
258
/* If Cancel is pressed or escaped */
259 260
void OpenDialog::cancel()
{
261
    /* Clear the panels */
262
    for( int i = 0; i < OPEN_TAB_MAX; i++ )
263 264 265 266 267 268 269
        dynamic_cast<OpenPanel*>( ui.Tab->widget( i ) )->clear();

    /* Clear the variables */
    mrl.clear();
    mainMRL.clear();

    /* If in Select Mode, reject instead of hiding */
270
    if( i_action_flag == SELECT ) reject();
271
    else hide();
272 273
}

274 275
/* If EnterKey is pressed */
void OpenDialog::close()
276
{
277 278
    /* If in Select Mode, accept instead of selecting a Slot */
    if( i_action_flag == SELECT )
279 280 281
        accept();
    else
        selectSlots();
282
}
283 284

/* Play button */
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
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();
    }
}

304 305
void OpenDialog::play()
{
306
    finish( false );
307 308 309 310
}

void OpenDialog::enqueue()
{
311
    finish( true );
312 313
}

314

315
void OpenDialog::finish( bool b_enqueue = false )
316
{
317
    toggleVisible();
318
    mrl = ui.advancedLineInput->text();
319

320
    if( i_action_flag != SELECT )
321
    {
322
        QStringList tempMRL = SeparateEntries( mrl );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
323
        for( int i = 0; i < tempMRL.size(); i++ )
324
        {
325 326 327
            bool b_start = !i && !b_enqueue;
            input_item_t *p_input;

328
            p_input = input_item_New( p_intf, qtu( tempMRL[i] ), NULL );
329 330 331 332 333

            /* Insert options */
            while( i + 1 < tempMRL.size() && tempMRL[i + 1].startsWith( ":" ) )
            {
                i++;
334
                input_item_AddOption( p_input, qtu( tempMRL[i] ), VLC_INPUT_OPTION_TRUSTED );
335 336
            }

337
            /* Switch between enqueuing and starting the item */
338 339 340
            /* FIXME: playlist_AddInput() can fail */
            playlist_AddInput( THEPL, p_input,
                PLAYLIST_APPEND | ( b_start ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
341
                PLAYLIST_END, b_pl ? true : false, pl_Unlocked );
342
            vlc_gc_decref( p_input );
Ludovic Fauvet's avatar
Ludovic Fauvet committed
343 344 345

            /* Do not add the current MRL if playlist_AddInput fail */
            RecentsMRL::getInstance( p_intf )->addRecent( tempMRL[i] );
346
        }
347 348 349
    }
    else
        accept();
350 351
}

352 353 354 355 356 357 358 359 360
void OpenDialog::transcode()
{
    stream( true );
}

void OpenDialog::stream( bool b_transcode_only )
{
    mrl = ui.advancedLineInput->text();
    toggleVisible();
361 362

    /* Separate the entries */
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
363
    QStringList listMRL = SeparateEntries( mrl );
364 365 366

    /* We can only take the first entry since we have no idea what
       to do with many files ? Gather ? */
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
367
    if( listMRL.size() > 0 )
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
    {
        /* First item */
        QString soutMRL = listMRL[0];

        /* Keep all the :xxx options because they are needed see v4l and dshow */
        for( int i = 1; i < listMRL.size(); i++ )
        {
            if( listMRL[i].at( 0 ) == ':' )
                soutMRL.append( " " + listMRL[i] );
            else
                break;
        }

        /* Dbg and send :D */
        msg_Dbg( p_intf, "MRL passed to the Sout: %s", qtu( soutMRL ) );
383
        THEDP->streamingDialog( this, soutMRL, b_transcode_only );
384
    }
385
}
Clément Stenac's avatar
Clément Stenac committed
386

387 388 389 390 391
/* Update the MRL */
void OpenDialog::updateMRL( QString tempMRL )
{
    mainMRL = tempMRL;
    updateMRL();
392 393
}

Clément Stenac's avatar
Clément Stenac committed
394
void OpenDialog::updateMRL() {
395
    mrl = mainMRL;
Clément Stenac's avatar
Clément Stenac committed
396 397 398
    if( ui.slaveCheckbox->isChecked() ) {
        mrl += " :input-slave=" + ui.slaveText->text();
    }
399
    int i_cache = config_GetInt( p_intf, qtu( storedMethod ) );
Clément Stenac's avatar
Clément Stenac committed
400
    if( i_cache != ui.cacheSpinBox->value() ) {
401 402
        mrl += QString( " :%1=%2" ).arg( storedMethod ).
                                  arg( ui.cacheSpinBox->value() );
Clément Stenac's avatar
Clément Stenac committed
403
    }
404 405 406
    if( ui.startTimeSpinBox->value() ) {
        mrl += " :start-time=" + QString( "%1" ).
            arg( ui.startTimeSpinBox->value() );
407
    }
408
    ui.advancedLineInput->setText( mrl );
Clément Stenac's avatar
Clément Stenac committed
409 410
}

411
void OpenDialog::newCachingMethod( QString method )
Clément Stenac's avatar
Clément Stenac committed
412 413 414
{
    if( method != storedMethod ) {
        storedMethod = method;
415
        int i_value = config_GetInt( p_intf, qtu( storedMethod ) );
416
        ui.cacheSpinBox->setValue( i_value );
Clément Stenac's avatar
Clément Stenac committed
417 418
    }
}
419

420
QStringList OpenDialog::SeparateEntries( QString entries )
421 422 423 424 425 426 427 428 429
{
    bool b_quotes_mode = false;

    QStringList entries_array;
    QString entry;

    int index = 0;
    while( index < entries.size() )
    {
430
        int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
431
        if( delim_pos < 0 ) delim_pos = entries.size() - 1;
432
        entry += entries.mid( index, delim_pos - index + 1 );
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
        index = delim_pos + 1;

        if( entry.isEmpty() ) continue;

        if( !b_quotes_mode && entry.endsWith( "\"" ) )
        {
            /* Enters quotes mode */
            entry.truncate( entry.size() - 1 );
            b_quotes_mode = true;
        }
        else if( b_quotes_mode && entry.endsWith( "\"" ) )
        {
            /* Finished the quotes mode */
            entry.truncate( entry.size() - 1 );
            b_quotes_mode = false;
        }
        else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
        {
            /* we found a non-quoted standalone string */
            if( index < entries.size() ||
                entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
                entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
                entry.truncate( entry.size() - 1 );
            if( !entry.isEmpty() ) entries_array.append( entry );
457
            entry.clear();
458 459 460 461 462 463 464 465 466
        }
        else
        {;}
    }

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

    return entries_array;
}
467 468 469 470 471 472 473 474

void OpenDialog::browseInputSlave()
{
    OpenDialog *od = new OpenDialog( this, p_intf, true, SELECT );
    od->exec();
    ui.slaveText->setText( od->getMRL() );
    delete od;
}