open.cpp 11.8 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 73
    setWindowTitle( qtr( "Open" ) );
    resize( 410, 300 );
74

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

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

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

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

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

100
    /* Select Button */
101
    selectButton = new QPushButton( qtr( "Select" ) );
102

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

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

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

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

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

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

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

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

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

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

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

OpenDialog::~OpenDialog()
163
{}
164

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

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

203 204 205 206 207
/* 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
208 209
}

210 211
void OpenDialog::toggleAdvancedPanel()
{
212 213
    if( ui.advancedFrame->isVisible() )
    {
214
        ui.advancedFrame->hide();
215
        //FIXME: Clear Bug here. Qt ?
216 217
        if( size().isValid() )
            resize( size().width(), size().height() - ui.advancedFrame->height() );
218 219 220
    }
    else
    {
221 222 223 224
        ui.advancedFrame->show();
    }
}

225
/***********
226 227
 * Actions *
 ***********/
228
/* If Cancel is pressed or escaped */
229 230
void OpenDialog::cancel()
{
231
    /* Clear the panels */
232
    for( int i = 0; i < OPEN_TAB_MAX; i++ )
233 234 235 236 237 238 239 240 241
        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();
242 243
}

244 245
/* If EnterKey is pressed */
void OpenDialog::close()
246
{
247 248 249 250
    if( windowModality() != Qt::NonModal )
        accept();
    else
        selectSlots();
251
}
252 253

/* Play button */
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
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();
    }
}

273 274
void OpenDialog::play()
{
275
    finish( false );
276 277 278 279
}

void OpenDialog::enqueue()
{
280
    finish( true );
281 282
}

283

284
void OpenDialog::finish( bool b_enqueue = false )
285
{
286
    toggleVisible();
287
    mrl = ui.advancedLineInput->text();
288

289
    if( windowModality() == Qt::NonModal )
290
    {
291 292
        QStringList tempMRL = SeparateEntries( mrl );
        for( size_t i = 0; i < tempMRL.size(); i++ )
293
        {
294 295 296
            bool b_start = !i && !b_enqueue;
            input_item_t *p_input;

297
            p_input = input_ItemNew( p_intf, qtu( tempMRL[i] ), NULL );
298 299 300 301 302

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

306
            /* Switch between enqueuing and starting the item */
307 308 309 310
            /* FIXME: playlist_AddInput() can fail */
            playlist_AddInput( THEPL, p_input,
                PLAYLIST_APPEND | ( b_start ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
                PLAYLIST_END, VLC_TRUE, VLC_FALSE );
311
        }
312 313 314
    }
    else
        accept();
315 316
}

317 318 319 320 321 322 323 324 325
void OpenDialog::transcode()
{
    stream( true );
}

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

329 330 331 332 333
/* Update the MRL */
void OpenDialog::updateMRL( QString tempMRL )
{
    mainMRL = tempMRL;
    updateMRL();
334 335
}

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

353
void OpenDialog::newCachingMethod( QString method )
Clément Stenac's avatar
Clément Stenac committed
354 355 356
{
    if( method != storedMethod ) {
        storedMethod = method;
357 358
        int i_value = config_GetInt( p_intf, qta( storedMethod ) );
        ui.cacheSpinBox->setValue( i_value );
Clément Stenac's avatar
Clément Stenac committed
359 360
    }
}
361

362
QStringList OpenDialog::SeparateEntries( QString entries )
363 364 365 366 367 368 369 370 371
{
    bool b_quotes_mode = false;

    QStringList entries_array;
    QString entry;

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

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

    return entries_array;
}