open.cpp 13.2 KB
Newer Older
1 2
/*****************************************************************************
 * open.cpp : Advanced open dialog
3
 *****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
4
 * Copyright © 2006-2007 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.
 *****************************************************************************/
23 24 25
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
26

27 28 29 30
#include "input_manager.hpp"

#include "dialogs/open.hpp"

31 32
#include <QTabWidget>
#include <QGridLayout>
33
#include <QFileDialog>
34
#include <QRegExp>
35
#include <QMenu>
36 37 38

OpenDialog *OpenDialog::instance = NULL;

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

OpenDialog::OpenDialog( QWidget *parent,
                        intf_thread_t *_p_intf,
64
                        bool b_selectMode,
65
                        int _action_flag )  :  QVLCDialog( parent, _p_intf )
66
{
67
    i_action_flag = _action_flag;
68

69 70 71 72
    /* Workaround the Win32 Vout that put the video on top at regular times */
#ifdef WIN32
    setWindowFlags( Qt::WindowStaysOnTopHint | Qt::Dialog );
#endif
73

74
    if( b_selectMode ) /* Select mode */
75 76
    {
        i_action_flag = SELECT;
77
        setWindowModality( Qt::WindowModal );
78 79
    }

80
    /* Basic Creation of the Window */
81
    ui.setupUi( this );
82
    setWindowTitle( qtr( "Open" ) );
83
    /* resize( 410, 600 ); */
84
    setMinimumSize( 520, 490 );
85

86
    /* Tab definition and creation */
87 88 89
    fileOpenPanel    = new FileOpenPanel( ui.Tab, p_intf );
    discOpenPanel    = new DiscOpenPanel( ui.Tab, p_intf );
    netOpenPanel     = new NetOpenPanel( ui.Tab, p_intf );
90
    captureOpenPanel = new CaptureOpenPanel( ui.Tab, p_intf );
91

92
    /* Insert the tabs */
93 94 95 96
    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,
97
                       qtr( "Capture &Device" ) );
Clément Stenac's avatar
Clément Stenac committed
98

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

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

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

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

114
    /* Menu for the Play button */
115
    QMenu * openButtonMenu = new QMenu( "Open" );
116
    openButtonMenu->addAction( qtr( "&Enqueue" ), this, SLOT( enqueue() ),
117
                                    QKeySequence( "Alt+E" ) );
118
    openButtonMenu->addAction( qtr( "&Play" ), this, SLOT( play() ),
119
                                    QKeySequence( "Alt+P" ) );
120
    openButtonMenu->addAction( qtr( "&Stream" ), this, SLOT( stream() ) ,
121
                                    QKeySequence( "Alt+S" ) );
122
    openButtonMenu->addAction( qtr( "&Convert" ), this, SLOT( transcode() ) ,
123
                                    QKeySequence( "Alt+C" ) );
124

125
    ui.menuButton->setMenu( openButtonMenu );
126

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

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

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

137 138 139 140
    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 ) );
141

Clément Stenac's avatar
Clément Stenac committed
142
    CONNECT( fileOpenPanel, methodChanged( QString ),
143
             this, newCachingMethod( QString ) );
Clément Stenac's avatar
Clément Stenac committed
144
    CONNECT( netOpenPanel, methodChanged( QString ),
145
             this, newCachingMethod( QString ) );
146
    CONNECT( discOpenPanel, methodChanged( QString ),
147
             this, newCachingMethod( QString ) );
148
    CONNECT( captureOpenPanel, methodChanged( QString ),
149
             this, newCachingMethod( QString ) );
Clément Stenac's avatar
Clément Stenac committed
150

151
    /* Advanced frame Connects */
152 153 154
    CONNECT( ui.slaveText, textChanged( QString ), this, updateMRL() );
    CONNECT( ui.cacheSpinBox, valueChanged( int ), this, updateMRL() );
    CONNECT( ui.startTimeSpinBox, valueChanged( int ), this, updateMRL() );
155
    BUTTONACT( ui.advancedCheckBox, toggleAdvancedPanel() );
156
    BUTTONACT( ui.slaveBrowseButton, browseInputSlave() );
Clément Stenac's avatar
Clément Stenac committed
157

158
    /* Buttons action */
159
    BUTTONACT( playButton, selectSlots() );
160
    BUTTONACT( selectButton, close() );
161
    BUTTONACT( cancelButton, cancel() );
162

163 164 165 166 167
    /* Hide the advancedPanel */
    if( !config_GetInt( p_intf, "qt-adv-options" ) )
        ui.advancedFrame->hide();
    else
        ui.advancedCheckBox->setChecked( true );
168

Clément Stenac's avatar
Clément Stenac committed
169 170
    /* Initialize caching */
    storedMethod = "";
171
    newCachingMethod( "file-caching" );
172 173

    resize( getSettings()->value( "opendialog-size", QSize( 520, 490 ) ).toSize() );
174 175 176
}

OpenDialog::~OpenDialog()
177 178 179
{
    getSettings()->setValue( "opendialog-size", size() );
}
180

181 182
/* Finish the dialog and decide if you open another one after */
void OpenDialog::setMenuAction()
183
{
184
    if( i_action_flag == SELECT )
185
    {
186 187
        playButton->hide();
        selectButton->show();
188
        selectButton->setDefault( true );
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    }
    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();
209
        playButton->setDefault( true );
210
    }
211 212
}

213
void OpenDialog::showTab( int i_tab )
214
{
215
    if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();
216
    ui.Tab->setCurrentIndex( i_tab );
217
    show();
218 219
}

220
/* Function called on signal currentChanged triggered */
221
void OpenDialog::signalCurrent( int i_tab )
222
{
223 224
    if( i_tab == OPEN_CAPTURE_TAB ) captureOpenPanel->initialize();

225 226
    if( ui.Tab->currentWidget() != NULL )
        ( dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() ) )->updateMRL();
Clément Stenac's avatar
Clément Stenac committed
227 228
}

229 230
void OpenDialog::toggleAdvancedPanel()
{
231 232
    if( ui.advancedFrame->isVisible() )
    {
233
        ui.advancedFrame->hide();
234
        //setMinimumSize( 520, 460 );
235
        if( size().isValid() )
236 237
            resize( size().width(), size().height()
                    - ui.advancedFrame->height() );
238 239 240
    }
    else
    {
241
        ui.advancedFrame->show();
242 243 244 245
        //setMinimumSize( 520, 460 + ui.advancedFrame->height() );
        if( size().isValid() )
            resize( size().width(), size().height()
                    + ui.advancedFrame->height() );
246 247 248
    }
}

249
/***********
250 251
 * Actions *
 ***********/
252
/* If Cancel is pressed or escaped */
253 254
void OpenDialog::cancel()
{
255
    /* Clear the panels */
256
    for( int i = 0; i < OPEN_TAB_MAX; i++ )
257 258 259 260 261 262 263
        dynamic_cast<OpenPanel*>( ui.Tab->widget( i ) )->clear();

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

    /* If in Select Mode, reject instead of hiding */
264
    if( i_action_flag == SELECT ) reject();
265
    else hide();
266 267
}

268 269
/* If EnterKey is pressed */
void OpenDialog::close()
270
{
271 272
    /* If in Select Mode, accept instead of selecting a Slot */
    if( i_action_flag == SELECT )
273 274 275
        accept();
    else
        selectSlots();
276
}
277 278

/* Play button */
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
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();
    }
}

298 299
void OpenDialog::play()
{
300
    finish( false );
301 302 303 304
}

void OpenDialog::enqueue()
{
305
    finish( true );
306 307
}

308

309
void OpenDialog::finish( bool b_enqueue = false )
310
{
311
    toggleVisible();
312
    mrl = ui.advancedLineInput->text();
313

314
    if( i_action_flag != SELECT )
315
    {
316 317
        QStringList tempMRL = SeparateEntries( mrl );
        for( size_t i = 0; i < tempMRL.size(); i++ )
318
        {
319 320 321
            bool b_start = !i && !b_enqueue;
            input_item_t *p_input;

322
            p_input = input_item_New( p_intf, qtu( tempMRL[i] ), NULL );
323 324 325 326 327

            /* Insert options */
            while( i + 1 < tempMRL.size() && tempMRL[i + 1].startsWith( ":" ) )
            {
                i++;
328
                input_item_AddOption( p_input, qtu( tempMRL[i] ) );
329 330
            }

331
            /* Switch between enqueuing and starting the item */
332 333 334
            /* FIXME: playlist_AddInput() can fail */
            playlist_AddInput( THEPL, p_input,
                PLAYLIST_APPEND | ( b_start ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
335
                PLAYLIST_END, true, pl_Unlocked );
336
            vlc_gc_decref( p_input );
337
        }
338 339 340
    }
    else
        accept();
341 342
}

343 344 345 346 347 348 349 350 351
void OpenDialog::transcode()
{
    stream( true );
}

void OpenDialog::stream( bool b_transcode_only )
{
    mrl = ui.advancedLineInput->text();
    toggleVisible();
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
352 353
    QStringList listMRL = SeparateEntries( mrl );
    if( listMRL.size() > 0 )
354
    THEDP->streamingDialog( this, listMRL[0], b_transcode_only );
355
}
Clément Stenac's avatar
Clément Stenac committed
356

357 358 359 360 361
/* Update the MRL */
void OpenDialog::updateMRL( QString tempMRL )
{
    mainMRL = tempMRL;
    updateMRL();
362 363
}

Clément Stenac's avatar
Clément Stenac committed
364
void OpenDialog::updateMRL() {
365
    mrl = mainMRL;
Clément Stenac's avatar
Clément Stenac committed
366 367 368
    if( ui.slaveCheckbox->isChecked() ) {
        mrl += " :input-slave=" + ui.slaveText->text();
    }
369
    int i_cache = config_GetInt( p_intf, qta( storedMethod ) );
Clément Stenac's avatar
Clément Stenac committed
370
    if( i_cache != ui.cacheSpinBox->value() ) {
371 372
        mrl += QString( " :%1=%2" ).arg( storedMethod ).
                                  arg( ui.cacheSpinBox->value() );
Clément Stenac's avatar
Clément Stenac committed
373
    }
374 375 376
    if( ui.startTimeSpinBox->value() ) {
        mrl += " :start-time=" + QString( "%1" ).
            arg( ui.startTimeSpinBox->value() );
377
    }
378
    ui.advancedLineInput->setText( mrl );
Clément Stenac's avatar
Clément Stenac committed
379 380
}

381
void OpenDialog::newCachingMethod( QString method )
Clément Stenac's avatar
Clément Stenac committed
382 383 384
{
    if( method != storedMethod ) {
        storedMethod = method;
385 386
        int i_value = config_GetInt( p_intf, qta( storedMethod ) );
        ui.cacheSpinBox->setValue( i_value );
Clément Stenac's avatar
Clément Stenac committed
387 388
    }
}
389

390
QStringList OpenDialog::SeparateEntries( QString entries )
391 392 393 394 395 396 397 398 399
{
    bool b_quotes_mode = false;

    QStringList entries_array;
    QString entry;

    int index = 0;
    while( index < entries.size() )
    {
400
        int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
401
        if( delim_pos < 0 ) delim_pos = entries.size() - 1;
402
        entry += entries.mid( index, delim_pos - index + 1 );
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
        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 );
427
            entry.clear();
428 429 430 431 432 433 434 435 436
        }
        else
        {;}
    }

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

    return entries_array;
}
437 438 439 440 441 442 443 444

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