1 /*****************************************************************************
2 * vlm.cpp : VLM Management
3 ****************************************************************************
4 * Copyright © 2008 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 *****************************************************************************/
30 #include "dialogs/vlm.hpp"
33 #include "dialogs/open.hpp"
34 #include "dialogs/sout.hpp"
38 #include <QVBoxLayout>
39 #include <QStackedWidget>
42 #include <QGridLayout>
45 #include <QToolButton>
47 #include <QPushButton>
48 #include <QHBoxLayout>
49 #include <QDateTimeEdit>
52 #include <QHeaderView>
53 #include <QScrollArea>
55 static const char *psz_type[] = { "Broadcast", "Schedule", "VOD" };
57 VLMDialog *VLMDialog::instance = NULL;
59 VLMDialog::VLMDialog( QWidget *parent, intf_thread_t *_p_intf ) : QVLCDialog( parent, _p_intf )
61 p_vlm = vlm_New( p_intf );
65 msg_Warn( p_intf, "Couldn't build VLM object ");
68 vlmWrapper = new VLMWrapper( p_vlm );
72 ui.saveButton->hide();
74 #define ADDMEDIATYPES( str, type ) ui.mediaType->addItem( qtr( str ), QVariant( type ) );
75 ADDMEDIATYPES( "Broadcast", QVLM_Broadcast );
76 ADDMEDIATYPES( "Schedule", QVLM_Schedule );
77 ADDMEDIATYPES( "Video On Demand ( VOD )", QVLM_VOD );
81 QGridLayout *schetimelayout = new QGridLayout( ui.schedBox );
82 QLabel *schetimelabel = new QLabel( qtr( "Hours/Minutes/Seconds:" ) );
83 schetimelayout->addWidget( schetimelabel, 0, 0 );
84 QLabel *schedatelabel = new QLabel( qtr( "Day Month Year:" ) );
85 schetimelayout->addWidget( schedatelabel, 1, 0 );
86 QLabel *scherepeatLabel = new QLabel( qtr( "Repeat:" ) );
87 schetimelayout->addWidget( scherepeatLabel, 2, 0 );
88 QLabel *scherepeatTimeLabel = new QLabel( qtr( "Repeat delay:" ) );
89 schetimelayout->addWidget( scherepeatTimeLabel, 3, 0 );
91 time = new QDateTimeEdit( QTime::currentTime() );
92 time->setAlignment( Qt::AlignRight );
93 time->setDisplayFormat( "hh:mm:ss" );
94 schetimelayout->addWidget( time, 0, 1, 1, 3 );
96 date = new QDateTimeEdit( QDate::currentDate() );
97 date->setAlignment( Qt::AlignRight );
98 date->setCalendarPopup( true );
100 date->setDisplayFormat( "dd MM yyyy" );
102 date->setDisplayFormat( "dd MMMM yyyy" );
104 schetimelayout->addWidget( date, 1, 1, 1, 3 );
106 scherepeatnumber = new QSpinBox;
107 scherepeatnumber->setAlignment( Qt::AlignRight );
108 schetimelayout->addWidget( scherepeatnumber, 2, 1, 1, 3 );
110 repeatDays = new QSpinBox;
111 repeatDays->setAlignment( Qt::AlignRight );
112 schetimelayout->addWidget( repeatDays, 3, 1, 1, 1 );
113 repeatDays->setSuffix( qtr(" days") );
115 repeatTime = new QDateTimeEdit;
116 repeatTime->setAlignment( Qt::AlignRight );
117 schetimelayout->addWidget( repeatTime, 3, 2, 1, 2 );
118 repeatTime->setDisplayFormat( "hh:mm:ss" );
121 ui.vlmItemScroll->setFrameStyle( QFrame::NoFrame );
122 ui.vlmItemScroll->setWidgetResizable( true );
123 vlmItemWidget = new QWidget;
124 vlmItemLayout = new QVBoxLayout( vlmItemWidget );
125 vlmItemWidget->setLayout( vlmItemLayout );
126 ui.vlmItemScroll->setWidget( vlmItemWidget );
128 QSpacerItem *spacer =
129 new QSpacerItem( 10, 10, QSizePolicy::Minimum, QSizePolicy::Expanding);
130 vlmItemLayout->addItem( spacer );
132 QPushButton *closeButton = new QPushButton( qtr( "Close" ) );
133 ui.buttonBox->addButton( closeButton, QDialogButtonBox::AcceptRole );
135 showScheduleWidget( QVLM_Broadcast );
137 /* Connect the comboBox to show the right Widgets */
138 CONNECT( ui.mediaType, currentIndexChanged( int ),
139 this, showScheduleWidget( int ) );
141 /* Connect the leftList to show the good VLMItem */
142 CONNECT( ui.vlmListItem, currentRowChanged( int ),
143 this, selectVLMItem( int ) );
145 BUTTONACT( closeButton, close() );
146 BUTTONACT( ui.addButton, addVLMItem() );
147 BUTTONACT( ui.clearButton, clearWidgets() );
148 BUTTONACT( ui.saveButton, saveModifications() );
149 BUTTONACT( ui.inputButton, selectInput() );
150 BUTTONACT( ui.outputButton, selectOutput() );
153 VLMDialog::~VLMDialog()
155 /* FIXME :you have to destroy vlm here to close
156 * but we shouldn't destroy vlm here in case somebody else wants it */
161 void VLMDialog::showScheduleWidget( int i )
163 ui.schedBox->setVisible( ( i == QVLM_Schedule ) );
164 ui.loopBCast->setVisible( ( i == QVLM_Broadcast ) );
165 ui.vodBox->setVisible( ( i == QVLM_VOD ) );
168 void VLMDialog::selectVLMItem( int i )
171 ui.vlmItemScroll->ensureWidgetVisible( vlmItems.at( i ) );
174 bool VLMDialog::isNameGenuine( QString name )
176 for( int i = 0; i < vlmItems.size(); i++ )
178 if( vlmItems.at( i )->name == name )
184 void VLMDialog::addVLMItem()
186 int vlmItemCount = vlmItems.size();
188 /* Take the name and Check it */
189 QString name = ui.nameLedit->text();
190 if( name.isEmpty() || !isNameGenuine( name ) )
192 msg_Dbg( p_intf, "VLM Name is empty or already exists, I can't do it" );
196 int type = ui.mediaType->itemData( ui.mediaType->currentIndex() ).toInt();
198 QString typeShortName;
199 QString inputText = ui.inputLedit->text();
200 QString outputText = ui.outputLedit->text();
201 bool b_checked = ui.enableCheck->isChecked();
202 bool b_looped = ui.loopBCast->isChecked();
203 QDateTime schetime = time->dateTime();
204 QDateTime schedate = date->dateTime();
205 int repeatnum = scherepeatnumber->value();
206 int repeatdays = repeatDays->value();
207 VLMAWidget * vlmAwidget;
212 typeShortName = "Bcast";
213 vlmAwidget = new VLMBroadcast( name, inputText, outputText,
214 b_checked, b_looped, this );
215 VLMWrapper::AddBroadcast( name, inputText, outputText, b_checked,
219 typeShortName = "VOD";
220 vlmAwidget = new VLMVod( name, inputText, outputText,
221 b_checked, ui.muxLedit->text(), this );
222 VLMWrapper::AddVod( name, inputText, outputText, b_checked );
225 typeShortName = "Sched";
226 vlmAwidget = new VLMSchedule( name, inputText, outputText,
227 schetime, schedate, repeatnum,
228 repeatdays, b_checked, this );
229 VLMWrapper::AddSchedule( name, inputText, outputText, schetime,
230 schedate, repeatnum, repeatdays, b_checked);
233 msg_Warn( p_intf, "Something bad happened" );
237 /* Add an Item of the Side List */
238 ui.vlmListItem->addItem( typeShortName + " : " + name );
239 ui.vlmListItem->setCurrentRow( vlmItemCount - 1 );
241 /* Add a new VLMAWidget on the main List */
243 vlmItemLayout->insertWidget( vlmItemCount, vlmAwidget );
244 vlmItems.append( vlmAwidget );
248 void VLMDialog::clearWidgets()
250 ui.nameLedit->clear();
251 ui.inputLedit->clear();
252 ui.outputLedit->clear();
253 time->setTime( QTime::currentTime() );
254 date->setDate( QDate::currentDate() );
255 ui.enableCheck->setChecked( true );
256 ui.nameLedit->setReadOnly( false );
257 ui.loopBCast->setChecked( false );
258 ui.muxLedit->clear();
259 ui.saveButton->hide();
260 ui.addButton->show();
263 void VLMDialog::selectInput()
265 OpenDialog *o = OpenDialog::getInstance( this, p_intf, 0, true );
267 ui.inputLedit->setText( o->getMRL() );
270 void VLMDialog::selectOutput()
272 SoutDialog *s = SoutDialog::getInstance( this, p_intf, false );
273 if( s->exec() == QDialog::Accepted )
274 ui.outputLedit->setText( s->getMrl() );
277 /* Object Modification */
278 void VLMDialog::removeVLMItem( VLMAWidget *vlmObj )
280 int index = vlmItems.indexOf( vlmObj );
281 if( index < 0 ) return;
282 delete ui.vlmListItem->takeItem( index );
283 vlmItems.removeAt( index );
286 /* HERE BE DRAGONS VLM REQUEST */
289 void VLMDialog::startModifyVLMItem( VLMAWidget *vlmObj )
291 currentIndex = vlmItems.indexOf( vlmObj );
292 if( currentIndex < 0 ) return;
294 msg_Dbg( p_intf, "Type: %i", vlmObj->type );
295 ui.vlmListItem->setCurrentRow( currentIndex );
296 ui.nameLedit->setText( vlmObj->name );
297 ui.inputLedit->setText( vlmObj->input );
298 ui.outputLedit->setText( vlmObj->output );
299 ui.enableCheck->setChecked( vlmObj->b_enabled );
301 switch( vlmObj->type )
304 ui.loopBCast->setChecked( (qobject_cast<VLMBroadcast *>(vlmObj))->b_looped );
307 ui.muxLedit->setText( (qobject_cast<VLMVod *>(vlmObj))->mux );
310 time->setDateTime( ( qobject_cast<VLMSchedule *>(vlmObj))->schetime );
311 date->setDateTime( ( qobject_cast<VLMSchedule *>(vlmObj))->schedate );
315 ui.nameLedit->setReadOnly( true );
316 ui.addButton->hide();
317 ui.saveButton->show();
320 void VLMDialog::saveModifications()
322 VLMAWidget *vlmObj = vlmItems.at( currentIndex );
325 vlmObj->input = ui.inputLedit->text();
326 vlmObj->output = ui.outputLedit->text();
327 vlmObj->setChecked( ui.enableCheck->isChecked() );
328 vlmObj->b_enabled = ui.enableCheck->isChecked();
329 switch( vlmObj->type )
332 (qobject_cast<VLMBroadcast *>(vlmObj))->b_looped = ui.loopBCast->isChecked();
335 (qobject_cast<VLMVod *>(vlmObj))->mux = ui.muxLedit->text();
338 (qobject_cast<VLMSchedule *>(vlmObj))->schetime = time->dateTime();
339 (qobject_cast<VLMSchedule *>(vlmObj))->schedate = date->dateTime();
340 (qobject_cast<VLMSchedule *>(vlmObj))->rNumber = scherepeatnumber->value();
341 (qobject_cast<VLMSchedule *>(vlmObj))->rDays = repeatDays->value();
345 vlmObj->update(); /* It should call the correct function is VLMAWidget
346 is abstract, but I am far from sure... FIXME ? */
351 /*********************************
352 * VLMAWidget - Abstract class
353 ********************************/
355 VLMAWidget::VLMAWidget( QString _name,
361 : QGroupBox( _name, _parent )
367 b_enabled = _enabled;
370 setCheckable( true );
371 setChecked( b_enabled );
373 objLayout = new QGridLayout( this );
374 setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Maximum );
376 nameLabel = new QLabel;
377 objLayout->addWidget( nameLabel, 0, 0, 1, 4 );
379 /*QLabel *time = new QLabel( "--:--/--:--" );
380 objLayout->addWidget( time, 1, 3, 1, 2 );*/
382 QToolButton *modifyButton = new QToolButton;
383 modifyButton->setIcon( QIcon( QPixmap( ":/pixmaps/menus_settings_16px.png" ) ) );
384 objLayout->addWidget( modifyButton, 0, 5 );
386 QToolButton *deleteButton = new QToolButton;
387 deleteButton->setIcon( QIcon( QPixmap( ":/pixmaps/menus_quit_16px.png" ) ) );
388 objLayout->addWidget( deleteButton, 0, 6 );
390 BUTTONACT( modifyButton, modify() );
391 BUTTONACT( deleteButton, del() );
392 CONNECT( this, clicked( bool ), this, toggleEnabled( bool ) );
395 void VLMAWidget::modify()
397 parent->startModifyVLMItem( this );
400 void VLMAWidget::del()
402 parent->removeVLMItem( this );
405 //FIXME, remove me before release
406 void VLMAWidget::enterEvent( QEvent *event )
411 void VLMAWidget::toggleEnabled( bool b_enable )
413 VLMWrapper::EnableItem( name, b_enable );
419 VLMBroadcast::VLMBroadcast( QString _name, QString _input, QString _output,
420 bool _enabled, bool _looped, VLMDialog *_parent)
421 : VLMAWidget( _name, _input, _output,
422 _enabled, _parent, QVLM_Broadcast )
424 nameLabel->setText( "Broadcast: " + name );
425 type = QVLM_Broadcast;
428 playButton = new QToolButton;
429 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
430 objLayout->addWidget( playButton, 1, 0 );
433 QToolButton *stopButton = new QToolButton;
434 stopButton->setIcon( QIcon( QPixmap( ":/pixmaps/stop_16px.png" ) ) );
435 objLayout->addWidget( stopButton, 1, 1 );
437 loopButton = new QToolButton;
438 objLayout->addWidget( loopButton, 1, 2 );
440 BUTTONACT( playButton, togglePlayPause() );
441 BUTTONACT( stopButton, stop() );
442 BUTTONACT( loopButton, toggleLoop() );
447 void VLMBroadcast::update()
449 VLMWrapper::EditBroadcast( name, input, output, b_enabled, b_looped );
451 loopButton->setIcon( QIcon( QPixmap( ":/pixmaps/playlist_repeat_all.png" ) ) );
453 loopButton->setIcon( QIcon( QPixmap( ":/pixmaps/playlist_repeat_off.png" ) ) );
456 void VLMBroadcast::togglePlayPause()
458 if( b_playing = true )
460 VLMWrapper::ControlBroadcast( name, ControlBroadcastPause );
461 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/pause_16px.png" ) ) );
465 VLMWrapper::ControlBroadcast( name, ControlBroadcastPlay );
466 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
468 b_playing = !b_playing;
471 void VLMBroadcast::toggleLoop()
473 b_enabled = !b_enabled;
477 void VLMBroadcast::stop()
479 VLMWrapper::ControlBroadcast( name, ControlBroadcastStop );
480 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
486 VLMSchedule::VLMSchedule( QString name, QString input, QString output,
487 QDateTime _schetime, QDateTime _schedate,
488 int _scherepeatnumber, int _repeatDays,
489 bool enabled, VLMDialog *parent )
490 : VLMAWidget( name, input, output, enabled, parent, QVLM_Schedule )
492 nameLabel->setText( "Schedule: " + name );
493 schetime = _schetime;
494 schedate = _schedate;
495 rNumber = _scherepeatnumber;
497 type = QVLM_Schedule;
501 void VLMSchedule::update()
503 VLMWrapper::EditSchedule( name, input, output, schetime, schedate,
504 rNumber, rDays, b_enabled);
510 VLMVod::VLMVod( QString name, QString input, QString output,
511 bool enabled, QString _mux, VLMDialog *parent)
512 : VLMAWidget( name, input, output, enabled, parent, QVLM_VOD )
514 nameLabel->setText( "VOD:" + name );
517 muxLabel = new QLabel;
518 objLayout->addWidget( muxLabel, 1, 0 );
523 void VLMVod::update()
525 muxLabel->setText( mux );
526 VLMWrapper::EditVod( name, input, output, b_enabled, mux );
533 vlm_t * VLMWrapper::p_vlm = NULL;
535 VLMWrapper::VLMWrapper( vlm_t *_p_vlm )
540 VLMWrapper::~VLMWrapper()
543 void VLMWrapper::AddBroadcast( const QString name, QString input,
545 bool b_enabled, bool b_loop )
547 vlm_message_t *message;
548 QString command = "new \"" + name + "\" broadcast";
549 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
550 vlm_MessageDelete( message );
551 EditBroadcast( name, input, output, b_enabled, b_loop );
554 void VLMWrapper::EditBroadcast( const QString name, const QString input,
555 const QString output,
556 bool b_enabled, bool b_loop )
558 vlm_message_t *message;
561 command = "setup \"" + name + "\" inputdel all";
562 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
563 vlm_MessageDelete( message );
564 command = "setup \"" + name + "\" input \"" + input + "\"";
565 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
566 vlm_MessageDelete( message );
567 if( !output.isEmpty() )
569 command = "setup \"" + name + "\" output \"" + output + "\"";
570 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
571 vlm_MessageDelete( message );
575 command = "setup \"" + name + "\" enabled";
576 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
577 vlm_MessageDelete( message );
581 command = "setup \"" + name + "\" loop";
582 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
583 vlm_MessageDelete( message );
587 void VLMWrapper::EnableItem( const QString name, bool b_enable )
589 vlm_message_t *message;
590 QString command = "setup \"" + name + ( b_enable ? " enable" : " disable" );
593 void VLMWrapper::ControlBroadcast( const QString name, int BroadcastStatus,
596 vlm_message_t *message;
598 QString command = "control \"" + name;
599 switch( BroadcastStatus )
601 case ControlBroadcastPlay:
604 case ControlBroadcastPause:
607 case ControlBroadcastStop:
610 case ControlBroadcastSeek:
611 command += " seek" + seek;
614 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
615 vlm_MessageDelete( message );
618 void VLMWrapper::AddVod( const QString name, const QString input,
619 const QString output,
620 bool b_enabled, const QString mux )
622 vlm_message_t *message;
623 QString command = "new \"" + name + "\" vod";
624 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
625 vlm_MessageDelete( message );
626 EditVod( name, input, output, b_enabled, mux );
629 void VLMWrapper::EditVod( const QString name, const QString input,
630 const QString output,
634 vlm_message_t *message;
635 QString command = "setup \"" + name + "\" input \"" + input + "\"";
636 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
637 vlm_MessageDelete( message );
638 if( !output.isEmpty() )
640 command = "setup \"" + name + "\" output \"" + output + "\"";
641 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
642 vlm_MessageDelete( message );
646 command = "setup \"" + name + "\" enabled";
647 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
648 vlm_MessageDelete( message );
652 command = "setup \"" + name + "\" mux \"" + mux + "\"";
653 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
654 vlm_MessageDelete( message );
658 void VLMWrapper::AddSchedule( const QString name, const QString input,
659 const QString output, QDateTime _schetime,
661 int _scherepeatnumber, int _repeatDays,
662 bool b_enabled, const QString mux )
664 vlm_message_t *message;
665 QString command = "new \"" + name + "\" schedule";
666 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
667 vlm_MessageDelete( message );
668 EditSchedule( name, input, output, _schetime, _schedate,
669 _scherepeatnumber, _repeatDays, b_enabled, mux );
672 void VLMWrapper::EditSchedule( const QString name, const QString input,
673 const QString output, QDateTime _schetime,
674 QDateTime _schedate, int _scherepeatnumber,
675 int _repeatDays, bool b_enabled,
678 vlm_message_t *message;
679 QString command = "setup \"" + name + "\" input \"" + input + "\"";
680 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
681 vlm_MessageDelete( message );
683 if( !output.isEmpty() )
685 command = "setup \"" + name + "\" output \"" + output + "\"";
686 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
687 vlm_MessageDelete( message );
692 command = "setup \"" + name + "\" enabled";
693 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
694 vlm_MessageDelete( message );
699 command = "setup \"" + name + "\" mux \"" + mux + "\"";
700 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
701 vlm_MessageDelete( message );
704 command = "setup \"" + name + "\" date \"" +
705 _schedate.toString( "yyyy/MM/dd" )+ "-" +
706 _schetime.toString( "hh:mm:ss" ) + "\"";
707 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
708 vlm_MessageDelete( message );
709 if( _scherepeatnumber > 0 )
711 command = "setup \"" + name + "\" repeat \"" + _scherepeatnumber + "\"";
712 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
713 vlm_MessageDelete( message );