open.cpp 12.1 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
OpenDialog* OpenDialog::getInstance( QWidget *parent, intf_thread_t *p_intf,
        int _action_flag, bool modal )
{
    /* Creation */
    if( !instance )
        instance = new OpenDialog( parent, p_intf, modal, _action_flag );
    else
    {
        /* Request the instance but change small details:
           - Button menu
           - Modality on top of the parent dialog */
        instance->i_action_flag = _action_flag;
        instance->setMenuAction();
        if( modal ) instance->setWindowModality( Qt::WindowModal );
    }
    return instance;
}

OpenDialog::OpenDialog( QWidget *parent,
                        intf_thread_t *_p_intf,
                        bool modal,
60
                        int _action_flag )  :  QVLCDialog( parent, _p_intf )
61
{
62
    i_action_flag = _action_flag;
63

64 65 66 67 68 69
    if( modal ) /* Select mode */
    {
        setWindowModality( Qt::WindowModal );
        i_action_flag = SELECT;
    }

70
    /* Basic Creation of the Window */
71
    ui.setupUi( this );
72
    setWindowTitle( qtr( "Open" ) );
73 74
    /* resize( 410, 600 ); */
    setMinimumSize( 520, 460 );
75

76
    /* Tab definition and creation */
77 78 79
    fileOpenPanel    = new FileOpenPanel( ui.Tab, p_intf );
    discOpenPanel    = new DiscOpenPanel( ui.Tab, p_intf );
    netOpenPanel     = new NetOpenPanel( ui.Tab, p_intf );
80
    captureOpenPanel = new CaptureOpenPanel( ui.Tab, p_intf );
81

82
    /* Insert the tabs */
83 84 85 86
    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,
87
                       qtr( "Capture &Device" ) );
Clément Stenac's avatar
Clément Stenac committed
88

89 90 91 92 93
    /* Hide the Slave input widgets */
    ui.slaveLabel->hide();
    ui.slaveText->hide();
    ui.slaveBrowseButton->hide();

94
    /* Buttons Creation */
95
    /* Play Button */
96
    playButton = ui.playButton;
97

98
    /* Cancel Button */
99
    cancelButton = new QPushButton( qtr( "&Cancel" ) );
100

101
    /* Select Button */
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
102
    selectButton = new QPushButton( qtr( "&Select" ) );
103

104
    /* Menu for the Play button */
105
    QMenu * openButtonMenu = new QMenu( "Open" );
106
    openButtonMenu->addAction( qtr( "&Enqueue" ), this, SLOT( enqueue() ),
107
                                    QKeySequence( "Alt+E" ) );
108
    openButtonMenu->addAction( qtr( "&Play" ), this, SLOT( play() ),
109
                                    QKeySequence( "Alt+P" ) );
110
    openButtonMenu->addAction( qtr( "&Stream" ), this, SLOT( stream() ) ,
111
                                    QKeySequence( "Alt+S" ) );
112
    openButtonMenu->addAction( qtr( "&Convert" ), this, SLOT( transcode() ) ,
113
                                    QKeySequence( "Alt+C" ) );
114

115
    ui.menuButton->setMenu( openButtonMenu );
116

117 118
    /* Add the three Buttons */
    ui.buttonsBox->addButton( selectButton, QDialogButtonBox::AcceptRole );
119 120
    ui.buttonsBox->addButton( cancelButton, QDialogButtonBox::RejectRole );

121 122 123
    /* At creation time, modify the default buttons */
    setMenuAction();

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

127 128 129 130
    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 ) );
131

Clément Stenac's avatar
Clément Stenac committed
132
    CONNECT( fileOpenPanel, methodChanged( QString ),
133
             this, newCachingMethod( QString ) );
Clément Stenac's avatar
Clément Stenac committed
134
    CONNECT( netOpenPanel, methodChanged( QString ),
135
             this, newCachingMethod( QString ) );
136
    CONNECT( discOpenPanel, methodChanged( QString ),
137
             this, newCachingMethod( QString ) );
138
    CONNECT( captureOpenPanel, methodChanged( QString ),
139
             this, newCachingMethod( QString ) );
Clément Stenac's avatar
Clément Stenac committed
140

141
    /* Advanced frame Connects */
142 143 144
    CONNECT( ui.slaveText, textChanged( QString ), this, updateMRL() );
    CONNECT( ui.cacheSpinBox, valueChanged( int ), this, updateMRL() );
    CONNECT( ui.startTimeSpinBox, valueChanged( int ), this, updateMRL() );
145
    BUTTONACT( ui.advancedCheckBox , toggleAdvancedPanel() );
Clément Stenac's avatar
Clément Stenac committed
146

147
    /* Buttons action */
148
    BUTTONACT( playButton, selectSlots() );
149
    BUTTONACT( selectButton, close() );
150
    BUTTONACT( cancelButton, cancel() );
151

152 153 154 155 156
    /* Hide the advancedPanel */
    if( !config_GetInt( p_intf, "qt-adv-options" ) )
        ui.advancedFrame->hide();
    else
        ui.advancedCheckBox->setChecked( true );
157

Clément Stenac's avatar
Clément Stenac committed
158 159
    /* Initialize caching */
    storedMethod = "";
160
    newCachingMethod( "file-caching" );
161 162 163
}

OpenDialog::~OpenDialog()
164
{}
165

166 167
/* Finish the dialog and decide if you open another one after */
void OpenDialog::setMenuAction()
168
{
169
    if( i_action_flag == SELECT )
170
    {
171 172
        playButton->hide();
        selectButton->show();
173
        selectButton->setDefault( true );
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    }
    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();
193
        playButton->setDefault( true );
194 195
        selectButton->hide();
    }
196 197
}

198
void OpenDialog::showTab( int i_tab=0 )
199
{
200
    ui.Tab->setCurrentIndex( i_tab );
201
    show();
202 203
}

204 205 206 207 208
/* Function called on signal currentChanged triggered */
void OpenDialog::signalCurrent()
{
    if( ui.Tab->currentWidget() != NULL )
        ( dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() ) )->updateMRL();
Clément Stenac's avatar
Clément Stenac committed
209 210
}

211 212
void OpenDialog::toggleAdvancedPanel()
{
213 214
    if( ui.advancedFrame->isVisible() )
    {
215
        ui.advancedFrame->hide();
216
        //setMinimumSize( 520, 460 );
217
        if( size().isValid() )
218 219
            resize( size().width(), size().height()
                    - ui.advancedFrame->height() );
220 221 222
    }
    else
    {
223
        ui.advancedFrame->show();
224 225 226 227
        //setMinimumSize( 520, 460 + ui.advancedFrame->height() );
        if( size().isValid() )
            resize( size().width(), size().height()
                    + ui.advancedFrame->height() );
228 229 230
    }
}

231
/***********
232 233
 * Actions *
 ***********/
234
/* If Cancel is pressed or escaped */
235 236
void OpenDialog::cancel()
{
237
    /* Clear the panels */
238
    for( int i = 0; i < OPEN_TAB_MAX; i++ )
239 240 241 242 243 244 245 246 247
        dynamic_cast<OpenPanel*>( ui.Tab->widget( i ) )->clear();

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

    /* If in Select Mode, reject instead of hiding */
    if( windowModality() != Qt::NonModal ) reject();
    else hide();
248 249
}

250 251
/* If EnterKey is pressed */
void OpenDialog::close()
252
{
253 254 255 256
    if( windowModality() != Qt::NonModal )
        accept();
    else
        selectSlots();
257
}
258 259

/* Play button */
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
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();
    }
}

279 280
void OpenDialog::play()
{
281
    finish( false );
282 283 284 285
}

void OpenDialog::enqueue()
{
286
    finish( true );
287 288
}

289

290
void OpenDialog::finish( bool b_enqueue = false )
291
{
292
    toggleVisible();
293
    mrl = ui.advancedLineInput->text();
294

295
    if( windowModality() == Qt::NonModal )
296
    {
297 298
        QStringList tempMRL = SeparateEntries( mrl );
        for( size_t i = 0; i < tempMRL.size(); i++ )
299
        {
300 301 302
            bool b_start = !i && !b_enqueue;
            input_item_t *p_input;

303
            p_input = input_ItemNew( p_intf, qtu( tempMRL[i] ), NULL );
304 305 306 307 308

            /* Insert options */
            while( i + 1 < tempMRL.size() && tempMRL[i + 1].startsWith( ":" ) )
            {
                i++;
309
                input_ItemAddOption( p_input, qtu( tempMRL[i] ) );
310 311
            }

312
            /* Switch between enqueuing and starting the item */
313 314 315
            /* FIXME: playlist_AddInput() can fail */
            playlist_AddInput( THEPL, p_input,
                PLAYLIST_APPEND | ( b_start ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
316
                PLAYLIST_END, true, pl_Unlocked );
317
            vlc_gc_decref( p_input );
318
        }
319 320 321
    }
    else
        accept();
322 323
}

324 325 326 327 328 329 330 331 332
void OpenDialog::transcode()
{
    stream( true );
}

void OpenDialog::stream( bool b_transcode_only )
{
    mrl = ui.advancedLineInput->text();
    toggleVisible();
333
    THEDP->streamingDialog( this, mrl, b_transcode_only );
334
}
Clément Stenac's avatar
Clément Stenac committed
335

336 337 338 339 340
/* Update the MRL */
void OpenDialog::updateMRL( QString tempMRL )
{
    mainMRL = tempMRL;
    updateMRL();
341 342
}

Clément Stenac's avatar
Clément Stenac committed
343
void OpenDialog::updateMRL() {
344
    mrl = mainMRL;
Clément Stenac's avatar
Clément Stenac committed
345 346 347
    if( ui.slaveCheckbox->isChecked() ) {
        mrl += " :input-slave=" + ui.slaveText->text();
    }
348
    int i_cache = config_GetInt( p_intf, qta( storedMethod ) );
Clément Stenac's avatar
Clément Stenac committed
349
    if( i_cache != ui.cacheSpinBox->value() ) {
350 351
        mrl += QString( " :%1=%2" ).arg( storedMethod ).
                                  arg( ui.cacheSpinBox->value() );
Clément Stenac's avatar
Clément Stenac committed
352
    }
353 354 355
    if( ui.startTimeSpinBox->value() ) {
        mrl += " :start-time=" + QString( "%1" ).
            arg( ui.startTimeSpinBox->value() );
356
    }
357
    ui.advancedLineInput->setText( mrl );
Clément Stenac's avatar
Clément Stenac committed
358 359
}

360
void OpenDialog::newCachingMethod( QString method )
Clément Stenac's avatar
Clément Stenac committed
361 362 363
{
    if( method != storedMethod ) {
        storedMethod = method;
364 365
        int i_value = config_GetInt( p_intf, qta( storedMethod ) );
        ui.cacheSpinBox->setValue( i_value );
Clément Stenac's avatar
Clément Stenac committed
366 367
    }
}
368

369
QStringList OpenDialog::SeparateEntries( QString entries )
370 371 372 373 374 375 376 377 378
{
    bool b_quotes_mode = false;

    QStringList entries_array;
    QString entry;

    int index = 0;
    while( index < entries.size() )
    {
379
        int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
380
        if( delim_pos < 0 ) delim_pos = entries.size() - 1;
381
        entry += entries.mid( index, delim_pos - index + 1 );
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
        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 );
406
            entry.clear();
407 408 409 410 411 412 413 414 415
        }
        else
        {;}
    }

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

    return entries_array;
}