1 /*****************************************************************************
2 * vlm.cpp : VLM Management
3 ****************************************************************************
4 * Copyright ( C ) 2006 the VideoLAN team
5 * $Id: sout.cpp 21875 2007-09-08 16:01:33Z jb $
7 * Authors: Jean-Baptiste Kempf <jb@videolan.org>
8 * Jean-François Massol <jf.massol -at- gmail.com>
9 * Clément Sténac <zorglub@videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * ( at your option ) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 #include "dialogs/vlm.hpp"
27 #include "dialogs/open.hpp"
28 #include "dialogs/sout.hpp"
32 #include <QVBoxLayout>
33 #include <QStackedWidget>
36 #include <QGridLayout>
39 #include <QToolButton>
41 #include <QPushButton>
42 #include <QHBoxLayout>
43 #include <QDateTimeEdit>
45 #include <QHeaderView>
46 #include <QScrollArea>
48 static const char *psz_type[] = { "Broadcast", "Schedule", "VOD" };
50 VLMDialog *VLMDialog::instance = NULL;
52 VLMDialog::VLMDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
54 p_vlm = vlm_New( p_intf );
58 msg_Warn( p_intf, "Couldn't build VLM object ");
61 vlmWrapper = new VLMWrapper( p_vlm );
65 ui.saveButton->hide();
67 #define ADDMEDIATYPES( str, type ) ui.mediaType->addItem( qtr( str ), QVariant( type ) );
68 ADDMEDIATYPES( "Broadcast", QVLM_Broadcast );
69 ADDMEDIATYPES( "Schedule", QVLM_Schedule );
70 ADDMEDIATYPES( "Video On Demand ( VOD )", QVLM_VOD );
74 QGridLayout *schetimelayout = new QGridLayout( ui.schedBox );
75 QLabel *schetimelabel = new QLabel( qtr( "Hours/Minutes/Seconds:" ) );
76 schetimelayout->addWidget( schetimelabel, 0, 0 );
77 QLabel *schedatelabel = new QLabel( qtr( "Day Month Year:" ) );
78 schetimelayout->addWidget( schedatelabel, 1, 0 );
79 QLabel *scherepeatLabel = new QLabel( qtr( "Repeat:" ) );
80 schetimelayout->addWidget( scherepeatLabel, 2, 0 );
81 QLabel *scherepeatTimeLabel = new QLabel( qtr( "Repeat delay:" ) );
82 schetimelayout->addWidget( scherepeatTimeLabel, 3, 0 );
84 time = new QDateTimeEdit( QTime::currentTime() );
85 time->setAlignment( Qt::AlignRight );
86 schetimelayout->addWidget( time, 0, 1, 1, 3 );
88 date = new QDateTimeEdit( QDate::currentDate() );
89 date->setAlignment( Qt::AlignRight );
90 date->setCalendarPopup( true );
92 date->setDisplayFormat( "dd MM yyyy" );
94 date->setDisplayFormat( "dd MMMM yyyy" );
96 schetimelayout->addWidget( date, 1, 1, 1, 3 );
98 scherepeatnumber = new QSpinBox;
99 scherepeatnumber->setAlignment( Qt::AlignRight );
100 schetimelayout->addWidget( scherepeatnumber, 2, 1, 1, 3 );
102 repeatDays = new QSpinBox;
103 repeatDays->setAlignment( Qt::AlignRight );
104 schetimelayout->addWidget( repeatDays, 3, 1, 1, 1 );
105 repeatDays->setSuffix( qtr(" days") );
107 repeatTime = new QDateTimeEdit;
108 repeatTime->setAlignment( Qt::AlignRight );
109 schetimelayout->addWidget( repeatTime, 3, 2, 1, 2 );
110 repeatTime->setDisplayFormat( "hh:mm:ss" );
113 ui.vlmItemScroll->setFrameStyle( QFrame::NoFrame );
114 ui.vlmItemScroll->setWidgetResizable( true );
115 vlmItemWidget = new QWidget;
116 vlmItemLayout = new QVBoxLayout( vlmItemWidget );
117 vlmItemWidget->setLayout( vlmItemLayout );
118 ui.vlmItemScroll->setWidget( vlmItemWidget );
120 QSpacerItem *spacer =
121 new QSpacerItem( 10, 10, QSizePolicy::Minimum, QSizePolicy::Expanding);
122 vlmItemLayout->addItem( spacer );
124 QPushButton *closeButton = new QPushButton( qtr( "Close" ) );
125 ui.buttonBox->addButton( closeButton, QDialogButtonBox::AcceptRole );
127 showScheduleWidget( QVLM_Broadcast );
129 /* Connect the comboBox to show the right Widgets */
130 CONNECT( ui.mediaType, currentIndexChanged( int ),
131 this, showScheduleWidget( int ) );
133 /* Connect the leftList to show the good VLMItem */
134 CONNECT( ui.vlmListItem, currentRowChanged( int ),
135 this, selectVLMItem( int ) );
137 BUTTONACT( closeButton, close() );
138 BUTTONACT( ui.addButton, addVLMItem() );
139 BUTTONACT( ui.clearButton, clearWidgets() );
140 BUTTONACT( ui.saveButton, saveModifications() );
141 BUTTONACT( ui.inputButton, selectInput() );
142 BUTTONACT( ui.outputButton, selectOutput() );
145 VLMDialog::~VLMDialog()
147 /* FIXME :you have to destroy vlm here to close
148 * but we shouldn't destroy vlm here in case somebody else wants it */
153 void VLMDialog::showScheduleWidget( int i )
155 ui.schedBox->setVisible( ( i == QVLM_Schedule ) );
156 ui.loopBCast->setVisible( ( i == QVLM_Broadcast ) );
157 ui.vodBox->setVisible( ( i == QVLM_VOD ) );
160 void VLMDialog::selectVLMItem( int i )
162 ui.vlmItemScroll->ensureWidgetVisible( vlmItems.at( i ) );
165 bool VLMDialog::isNameGenuine( QString name )
167 for( int i = 0; i < vlmItems.size(); i++ )
169 if( vlmItems.at( i )->name == name )
175 void VLMDialog::addVLMItem()
177 int vlmItemCount = vlmItems.size();
179 /* Take the name and Check it */
180 QString name = ui.nameLedit->text();
181 if( name.isEmpty() || !isNameGenuine( name ) )
183 msg_Dbg( p_intf, "VLM Name is empty or already exists, I can't do it" );
187 int type = ui.mediaType->itemData( ui.mediaType->currentIndex() ).toInt();
189 QString typeShortName;
190 QString inputText = ui.inputLedit->text();
191 QString outputText = ui.outputLedit->text();
192 bool b_checked = ui.enableCheck->isChecked();
193 bool b_looped = ui.loopBCast->isChecked();
195 VLMAWidget * vlmAwidget;
200 typeShortName = "Bcast";
201 vlmAwidget = new VLMBroadcast( name, inputText, outputText,
202 b_checked, b_looped, this );
203 VLMWrapper::AddBroadcast( name, inputText, outputText, b_checked, b_looped );
206 typeShortName = "VOD";
207 vlmAwidget = new VLMVod( name, inputText, outputText,
208 b_checked, ui.muxLedit->text(), this );
209 VLMWrapper::AddVod( name, inputText, outputText, b_checked );
212 typeShortName = "Sched";
213 vlmAwidget = new VLMSchedule( name, inputText, outputText,
217 msg_Warn( p_intf, "Something bad happened" );
221 /* Add an Item of the Side List */
222 ui.vlmListItem->addItem( typeShortName + " : " + name );
223 ui.vlmListItem->setCurrentRow( vlmItemCount - 1 );
225 /* Add a new VLMAWidget on the main List */
227 vlmItemLayout->insertWidget( vlmItemCount, vlmAwidget );
228 vlmItems.append( vlmAwidget );
231 void VLMDialog::clearWidgets()
233 ui.nameLedit->clear();
234 ui.inputLedit->clear();
235 ui.outputLedit->clear();
236 time->setTime( QTime::currentTime() );
237 date->setDate( QDate::currentDate() );
238 ui.enableCheck->setChecked( true );
239 ui.nameLedit->setReadOnly( false );
240 ui.loopBCast->setChecked( false );
241 ui.muxLedit->clear();
242 ui.saveButton->hide();
243 ui.addButton->show();
246 void VLMDialog::selectInput()
248 OpenDialog *o = OpenDialog::getInstance( this, p_intf, 0, true );
250 ui.inputLedit->setText( o->getMRL() );
253 void VLMDialog::selectOutput()
255 SoutDialog *s = SoutDialog::getInstance( this, p_intf, false );
256 if( s->exec() == QDialog::Accepted )
257 ui.outputLedit->setText( s->getMrl() );
260 /* Object Modification */
261 void VLMDialog::removeVLMItem( VLMAWidget *vlmObj )
263 int index = vlmItems.indexOf( vlmObj );
264 if( index < 0 ) return;
266 //FIXME, this is going to segfault if the focus in on the ListWidget
267 delete ui.vlmListItem->takeItem( index );
268 vlmItems.removeAt( index );
271 /* HERE BE DRAGONS VLM REQUEST */
274 void VLMDialog::startModifyVLMItem( VLMAWidget *vlmObj )
276 currentIndex = vlmItems.indexOf( vlmObj );
277 if( currentIndex < 0 ) return;
279 msg_Dbg( p_intf, "Type: %i", vlmObj->type );
280 ui.vlmListItem->setCurrentRow( currentIndex );
281 ui.nameLedit->setText( vlmObj->name );
282 ui.inputLedit->setText( vlmObj->input );
283 ui.outputLedit->setText( vlmObj->output );
284 ui.enableCheck->setChecked( vlmObj->b_enabled );
286 switch( vlmObj->type )
289 ui.loopBCast->setChecked( (qobject_cast<VLMBroadcast *>(vlmObj))->b_looped );
292 ui.muxLedit->setText( (qobject_cast<VLMVod *>(vlmObj))->mux );
295 //(qobject_cast<VLMSchedule *>)
299 ui.nameLedit->setReadOnly( true );
300 ui.addButton->hide();
301 ui.saveButton->show();
304 void VLMDialog::saveModifications()
306 VLMAWidget *vlmObj = vlmItems.at( currentIndex );
309 vlmObj->input = ui.inputLedit->text();
310 vlmObj->output = ui.outputLedit->text();
311 vlmObj->setChecked( ui.enableCheck->isChecked() );
312 vlmObj->b_enabled = ui.enableCheck->isChecked();
313 switch( vlmObj->type )
316 (qobject_cast<VLMBroadcast *>(vlmObj))->b_looped = ui.loopBCast->isChecked();
319 (qobject_cast<VLMVod *>(vlmObj))->mux = ui.muxLedit->text();
325 vlmObj->update(); /* It should call the correct function is VLMAWidget
326 is abstract, but I am far from sure... FIXME ? */
331 /*********************************
332 * VLMAWidget - Abstract class
333 ********************************/
335 VLMAWidget::VLMAWidget( QString _name,
341 : QGroupBox( _name, _parent )
347 b_enabled = _enabled;
350 setCheckable( true );
351 setChecked( b_enabled );
353 objLayout = new QGridLayout( this );
354 setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Maximum );
356 nameLabel = new QLabel;
357 objLayout->addWidget( nameLabel, 0, 0, 1, 4 );
359 /*QLabel *time = new QLabel( "--:--/--:--" );
360 objLayout->addWidget( time, 1, 3, 1, 2 );*/
362 QToolButton *modifyButton = new QToolButton;
363 modifyButton->setIcon( QIcon( QPixmap( ":/pixmaps/menus_settings_16px.png" ) ) );
364 objLayout->addWidget( modifyButton, 0, 5 );
366 QToolButton *deleteButton = new QToolButton;
367 deleteButton->setIcon( QIcon( QPixmap( ":/pixmaps/menus_quit_16px.png" ) ) );
368 objLayout->addWidget( deleteButton, 0, 6 );
370 BUTTONACT( modifyButton, modify() );
371 BUTTONACT( deleteButton, del() );
372 CONNECT( this, clicked( bool ), this, toggleEnabled( bool ) );
375 void VLMAWidget::modify()
377 parent->startModifyVLMItem( this );
380 void VLMAWidget::del()
382 parent->removeVLMItem( this );
385 //FIXME, remove me before release
386 void VLMAWidget::enterEvent( QEvent *event )
391 void VLMAWidget::toggleEnabled( bool b_enable )
393 VLMWrapper::EnableItem( name, b_enable );
399 VLMBroadcast::VLMBroadcast( QString _name, QString _input, QString _output,
400 bool _enabled, bool _looped, VLMDialog *_parent)
401 : VLMAWidget( _name, _input, _output,
402 _enabled, _parent, QVLM_Broadcast )
404 nameLabel->setText( "Broadcast: " + name );
405 type = QVLM_Broadcast;
408 playButton = new QToolButton;
409 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
410 objLayout->addWidget( playButton, 1, 0 );
413 QToolButton *stopButton = new QToolButton;
414 stopButton->setIcon( QIcon( QPixmap( ":/pixmaps/stop_16px.png" ) ) );
415 objLayout->addWidget( stopButton, 1, 1 );
417 loopButton = new QToolButton;
418 objLayout->addWidget( loopButton, 1, 2 );
420 BUTTONACT( playButton, togglePlayPause() );
421 BUTTONACT( stopButton, stop() );
422 BUTTONACT( loopButton, toggleLoop() );
427 void VLMBroadcast::update()
429 VLMWrapper::EditBroadcast( name, input, output, b_enabled, b_looped );
431 loopButton->setIcon( QIcon( QPixmap( ":/pixmaps/playlist_repeat_all.png" ) ) );
433 loopButton->setIcon( QIcon( QPixmap( ":/pixmaps/playlist_repeat_off.png" ) ) );
436 void VLMBroadcast::togglePlayPause()
438 if( b_playing = true )
440 VLMWrapper::ControlBroadcast( name, ControlBroadcastPause );
441 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/pause_16px.png" ) ) );
445 VLMWrapper::ControlBroadcast( name, ControlBroadcastPlay );
446 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
448 b_playing = !b_playing;
451 void VLMBroadcast::toggleLoop()
453 b_enabled = !b_enabled;
457 void VLMBroadcast::stop()
459 VLMWrapper::ControlBroadcast( name, ControlBroadcastStop );
460 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
466 VLMSchedule::VLMSchedule( QString name, QString input, QString output,
467 bool enabled, VLMDialog *parent)
468 : VLMAWidget( name, input, output, enabled, parent, QVLM_Schedule )
470 nameLabel->setText( "Schedule: " + name );
473 void VLMSchedule::update()
480 VLMVod::VLMVod( QString name, QString input, QString output,
481 bool enabled, QString _mux, VLMDialog *parent)
482 : VLMAWidget( name, input, output, enabled, parent, QVLM_VOD )
484 nameLabel->setText( "VOD:" + name );
487 muxLabel = new QLabel;
488 objLayout->addWidget( muxLabel, 1, 0 );
493 void VLMVod::update()
495 muxLabel->setText( mux );
496 VLMWrapper::EditVod( name, input, output, b_enabled, mux );
503 vlm_t * VLMWrapper::p_vlm = NULL;
505 VLMWrapper::VLMWrapper( vlm_t *_p_vlm )
510 VLMWrapper::~VLMWrapper()
513 void VLMWrapper::AddBroadcast( const QString name, QString input,
515 bool b_enabled, bool b_loop )
517 vlm_message_t *message;
518 QString command = "new \"" + name + "\" broadcast";
519 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
520 vlm_MessageDelete( message );
521 EditBroadcast( name, input, output, b_enabled, b_loop );
524 void VLMWrapper::EditBroadcast( const QString name, const QString input,
525 const QString output,
526 bool b_enabled, bool b_loop )
528 vlm_message_t *message;
531 command = "setup \"" + name + "\" inputdel all";
532 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
533 vlm_MessageDelete( message );
534 command = "setup \"" + name + "\" input \"" + input + "\"";
535 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
536 vlm_MessageDelete( message );
537 if( !output.isEmpty() )
539 command = "setup \"" + name + "\" output \"" + output + "\"";
540 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
541 vlm_MessageDelete( message );
545 command = "setup \"" + name + "\" enabled";
546 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
547 vlm_MessageDelete( message );
551 command = "setup \"" + name + "\" loop";
552 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
553 vlm_MessageDelete( message );
557 void VLMWrapper::EnableItem( const QString name, bool b_enable )
559 vlm_message_t *message;
560 QString command = "setup \"" + name + ( b_enable ? " enable" : " disable" );
563 void VLMWrapper::ControlBroadcast( const QString name, int BroadcastStatus,
566 vlm_message_t *message;
568 QString command = "control \"" + name;
569 switch( BroadcastStatus )
571 case ControlBroadcastPlay:
574 case ControlBroadcastPause:
577 case ControlBroadcastStop:
580 case ControlBroadcastSeek:
581 command += " seek" + seek;
584 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
585 vlm_MessageDelete( message );
588 void VLMWrapper::AddVod( const QString name, const QString input,
589 const QString output,
590 bool b_enabled, const QString mux )
592 vlm_message_t *message;
593 QString command = "new \"" + name + "\" vod";
594 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
595 vlm_MessageDelete( message );
596 EditVod( name, input, output, b_enabled, mux );
599 void VLMWrapper::EditVod( const QString name, const QString input,
600 const QString output,
604 vlm_message_t *message;
605 QString command = "setup \"" + name + "\" input \"" + input + "\"";
606 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
607 vlm_MessageDelete( message );
608 if( !output.isEmpty() )
610 command = "setup \"" + name + "\" output \"" + output + "\"";
611 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
612 vlm_MessageDelete( message );
616 command = "setup \"" + name + "\" enabled";
617 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
618 vlm_MessageDelete( message );
622 command = "setup \"" + name + "\" mux \"" + mux + "\"";
623 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
624 vlm_MessageDelete( message );