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>
54 #include <QFileDialog>
56 static const char *psz_type[] = { "Broadcast", "Schedule", "VOD" };
58 VLMDialog *VLMDialog::instance = NULL;
60 VLMDialog::VLMDialog( QWidget *parent, intf_thread_t *_p_intf ) : QVLCDialog( parent, _p_intf )
62 p_vlm = vlm_New( p_intf );
66 msg_Warn( p_intf, "Couldn't build VLM object ");
69 vlmWrapper = new VLMWrapper( p_vlm );
73 ui.saveButton->hide();
75 #define ADDMEDIATYPES( str, type ) ui.mediaType->addItem( qtr( str ), QVariant( type ) );
76 ADDMEDIATYPES( "Broadcast", QVLM_Broadcast );
77 ADDMEDIATYPES( "Schedule", QVLM_Schedule );
78 ADDMEDIATYPES( "Video On Demand ( VOD )", QVLM_VOD );
82 QGridLayout *schetimelayout = new QGridLayout( ui.schedBox );
83 QLabel *schetimelabel = new QLabel( qtr( "Hours/Minutes/Seconds:" ) );
84 schetimelayout->addWidget( schetimelabel, 0, 0 );
85 QLabel *schedatelabel = new QLabel( qtr( "Day Month Year:" ) );
86 schetimelayout->addWidget( schedatelabel, 1, 0 );
87 QLabel *scherepeatLabel = new QLabel( qtr( "Repeat:" ) );
88 schetimelayout->addWidget( scherepeatLabel, 2, 0 );
89 QLabel *scherepeatTimeLabel = new QLabel( qtr( "Repeat delay:" ) );
90 schetimelayout->addWidget( scherepeatTimeLabel, 3, 0 );
92 time = new QDateTimeEdit( QTime::currentTime() );
93 time->setAlignment( Qt::AlignRight );
94 time->setDisplayFormat( "hh:mm:ss" );
95 schetimelayout->addWidget( time, 0, 1, 1, 3 );
97 date = new QDateTimeEdit( QDate::currentDate() );
98 date->setAlignment( Qt::AlignRight );
99 date->setCalendarPopup( true );
101 date->setDisplayFormat( "dd MM yyyy" );
103 date->setDisplayFormat( "dd MMMM yyyy" );
105 schetimelayout->addWidget( date, 1, 1, 1, 3 );
107 scherepeatnumber = new QSpinBox;
108 scherepeatnumber->setAlignment( Qt::AlignRight );
109 schetimelayout->addWidget( scherepeatnumber, 2, 1, 1, 3 );
111 repeatDays = new QSpinBox;
112 repeatDays->setAlignment( Qt::AlignRight );
113 schetimelayout->addWidget( repeatDays, 3, 1, 1, 1 );
114 repeatDays->setSuffix( qtr(" days") );
116 repeatTime = new QDateTimeEdit;
117 repeatTime->setAlignment( Qt::AlignRight );
118 schetimelayout->addWidget( repeatTime, 3, 2, 1, 2 );
119 repeatTime->setDisplayFormat( "hh:mm:ss" );
122 ui.vlmItemScroll->setFrameStyle( QFrame::NoFrame );
123 ui.vlmItemScroll->setWidgetResizable( true );
124 vlmItemWidget = new QWidget;
125 vlmItemLayout = new QVBoxLayout( vlmItemWidget );
126 vlmItemWidget->setLayout( vlmItemLayout );
127 ui.vlmItemScroll->setWidget( vlmItemWidget );
129 QSpacerItem *spacer =
130 new QSpacerItem( 10, 10, QSizePolicy::Minimum, QSizePolicy::Expanding);
131 vlmItemLayout->addItem( spacer );
133 QPushButton *exportButton = new QPushButton( qtr( "Export" ) );
134 ui.buttonBox->addButton( exportButton, QDialogButtonBox::ActionRole );
136 QPushButton *closeButton = new QPushButton( qtr( "Close" ) );
137 ui.buttonBox->addButton( closeButton, QDialogButtonBox::AcceptRole );
140 showScheduleWidget( QVLM_Broadcast );
142 /* Connect the comboBox to show the right Widgets */
143 CONNECT( ui.mediaType, currentIndexChanged( int ),
144 this, showScheduleWidget( int ) );
146 /* Connect the leftList to show the good VLMItem */
147 CONNECT( ui.vlmListItem, currentRowChanged( int ),
148 this, selectVLMItem( int ) );
150 BUTTONACT( closeButton, close() );
151 BUTTONACT( exportButton, exportVLMConf() );
152 BUTTONACT( ui.addButton, addVLMItem() );
153 BUTTONACT( ui.clearButton, clearWidgets() );
154 BUTTONACT( ui.saveButton, saveModifications() );
155 BUTTONACT( ui.inputButton, selectInput() );
156 BUTTONACT( ui.outputButton, selectOutput() );
159 VLMDialog::~VLMDialog()
161 /* FIXME :you have to destroy vlm here to close
162 * but we shouldn't destroy vlm here in case somebody else wants it */
167 void VLMDialog::showScheduleWidget( int i )
169 ui.schedBox->setVisible( ( i == QVLM_Schedule ) );
170 ui.loopBCast->setVisible( ( i == QVLM_Broadcast ) );
171 ui.vodBox->setVisible( ( i == QVLM_VOD ) );
174 void VLMDialog::selectVLMItem( int i )
177 ui.vlmItemScroll->ensureWidgetVisible( vlmItems.at( i ) );
180 bool VLMDialog::isNameGenuine( QString name )
182 for( int i = 0; i < vlmItems.size(); i++ )
184 if( vlmItems.at( i )->name == name )
190 void VLMDialog::addVLMItem()
192 int vlmItemCount = vlmItems.size();
194 /* Take the name and Check it */
195 QString name = ui.nameLedit->text();
196 if( name.isEmpty() || !isNameGenuine( name ) )
198 msg_Dbg( p_intf, "VLM Name is empty or already exists, I can't do it" );
202 int type = ui.mediaType->itemData( ui.mediaType->currentIndex() ).toInt();
204 QString typeShortName;
205 QString inputText = ui.inputLedit->text();
206 QString outputText = ui.outputLedit->text();
207 bool b_checked = ui.enableCheck->isChecked();
208 bool b_looped = ui.loopBCast->isChecked();
209 QDateTime schetime = time->dateTime();
210 QDateTime schedate = date->dateTime();
211 int repeatnum = scherepeatnumber->value();
212 int repeatdays = repeatDays->value();
213 VLMAWidget * vlmAwidget;
218 typeShortName = "Bcast";
219 vlmAwidget = new VLMBroadcast( name, inputText, outputText,
220 b_checked, b_looped, this );
221 VLMWrapper::AddBroadcast( name, inputText, outputText, b_checked,
225 typeShortName = "VOD";
226 vlmAwidget = new VLMVod( name, inputText, outputText,
227 b_checked, ui.muxLedit->text(), this );
228 VLMWrapper::AddVod( name, inputText, outputText, b_checked );
231 typeShortName = "Sched";
232 vlmAwidget = new VLMSchedule( name, inputText, outputText,
233 schetime, schedate, repeatnum,
234 repeatdays, b_checked, this );
235 VLMWrapper::AddSchedule( name, inputText, outputText, schetime,
236 schedate, repeatnum, repeatdays, b_checked);
239 msg_Warn( p_intf, "Something bad happened" );
243 /* Add an Item of the Side List */
244 ui.vlmListItem->addItem( typeShortName + " : " + name );
245 ui.vlmListItem->setCurrentRow( vlmItemCount - 1 );
247 /* Add a new VLMAWidget on the main List */
249 vlmItemLayout->insertWidget( vlmItemCount, vlmAwidget );
250 vlmItems.append( vlmAwidget );
254 // FIXME : VOD are not exported to the file
255 bool VLMDialog::exportVLMConf()
257 QString saveVLMConfFileName = QFileDialog::getSaveFileName(
258 this, qtr( "Choose a filename to save the VLM configuration..." ),
259 qfu( p_intf->p_libvlc->psz_homedir ),
260 qtr( "VLM conf (*.vlm) ;; All (*.*)" ) );
262 if( !saveVLMConfFileName.isEmpty() )
264 vlm_message_t *message;
265 QString command = "save \"" + saveVLMConfFileName + "\"";
266 vlm_ExecuteCommand( p_vlm , qtu( command ) , &message );
267 vlm_MessageDelete( message );
273 // TODO : import configuration file
274 bool VLMDialog::importVLMConf()
278 void VLMDialog::clearWidgets()
280 ui.nameLedit->clear();
281 ui.inputLedit->clear();
282 ui.outputLedit->clear();
283 time->setTime( QTime::currentTime() );
284 date->setDate( QDate::currentDate() );
285 ui.enableCheck->setChecked( true );
286 ui.nameLedit->setReadOnly( false );
287 ui.loopBCast->setChecked( false );
288 ui.muxLedit->clear();
289 ui.saveButton->hide();
290 ui.addButton->show();
293 void VLMDialog::selectInput()
295 OpenDialog *o = OpenDialog::getInstance( this, p_intf, 0, true );
297 ui.inputLedit->setText( o->getMRL() );
300 void VLMDialog::selectOutput()
302 SoutDialog *s = SoutDialog::getInstance( this, p_intf, false );
303 if( s->exec() == QDialog::Accepted )
304 ui.outputLedit->setText( s->getMrl() );
307 /* Object Modification */
308 void VLMDialog::removeVLMItem( VLMAWidget *vlmObj )
310 int index = vlmItems.indexOf( vlmObj );
311 if( index < 0 ) return;
312 delete ui.vlmListItem->takeItem( index );
313 vlmItems.removeAt( index );
316 /* HERE BE DRAGONS VLM REQUEST */
319 void VLMDialog::startModifyVLMItem( VLMAWidget *vlmObj )
321 currentIndex = vlmItems.indexOf( vlmObj );
322 if( currentIndex < 0 ) return;
324 msg_Dbg( p_intf, "Type: %i", vlmObj->type );
325 ui.vlmListItem->setCurrentRow( currentIndex );
326 ui.nameLedit->setText( vlmObj->name );
327 ui.inputLedit->setText( vlmObj->input );
328 ui.outputLedit->setText( vlmObj->output );
329 ui.enableCheck->setChecked( vlmObj->b_enabled );
331 switch( vlmObj->type )
334 ui.loopBCast->setChecked( (qobject_cast<VLMBroadcast *>(vlmObj))->b_looped );
337 ui.muxLedit->setText( (qobject_cast<VLMVod *>(vlmObj))->mux );
340 time->setDateTime( ( qobject_cast<VLMSchedule *>(vlmObj))->schetime );
341 date->setDateTime( ( qobject_cast<VLMSchedule *>(vlmObj))->schedate );
345 ui.nameLedit->setReadOnly( true );
346 ui.addButton->hide();
347 ui.saveButton->show();
350 void VLMDialog::saveModifications()
352 VLMAWidget *vlmObj = vlmItems.at( currentIndex );
355 vlmObj->input = ui.inputLedit->text();
356 vlmObj->output = ui.outputLedit->text();
357 vlmObj->setChecked( ui.enableCheck->isChecked() );
358 vlmObj->b_enabled = ui.enableCheck->isChecked();
359 switch( vlmObj->type )
362 (qobject_cast<VLMBroadcast *>(vlmObj))->b_looped = ui.loopBCast->isChecked();
365 (qobject_cast<VLMVod *>(vlmObj))->mux = ui.muxLedit->text();
368 (qobject_cast<VLMSchedule *>(vlmObj))->schetime = time->dateTime();
369 (qobject_cast<VLMSchedule *>(vlmObj))->schedate = date->dateTime();
370 (qobject_cast<VLMSchedule *>(vlmObj))->rNumber = scherepeatnumber->value();
371 (qobject_cast<VLMSchedule *>(vlmObj))->rDays = repeatDays->value();
375 vlmObj->update(); /* It should call the correct function is VLMAWidget
376 is abstract, but I am far from sure... FIXME ? */
381 /*********************************
382 * VLMAWidget - Abstract class
383 ********************************/
385 VLMAWidget::VLMAWidget( QString _name,
391 : QGroupBox( _name, _parent )
397 b_enabled = _enabled;
400 setCheckable( true );
401 setChecked( b_enabled );
403 objLayout = new QGridLayout( this );
404 setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Maximum );
406 nameLabel = new QLabel;
407 objLayout->addWidget( nameLabel, 0, 0, 1, 4 );
409 /*QLabel *time = new QLabel( "--:--/--:--" );
410 objLayout->addWidget( time, 1, 3, 1, 2 );*/
412 QToolButton *modifyButton = new QToolButton;
413 modifyButton->setIcon( QIcon( QPixmap( ":/pixmaps/menus_settings_16px.png" ) ) );
414 objLayout->addWidget( modifyButton, 0, 5 );
416 QToolButton *deleteButton = new QToolButton;
417 deleteButton->setIcon( QIcon( QPixmap( ":/pixmaps/menus_quit_16px.png" ) ) );
418 objLayout->addWidget( deleteButton, 0, 6 );
420 BUTTONACT( modifyButton, modify() );
421 BUTTONACT( deleteButton, del() );
422 CONNECT( this, clicked( bool ), this, toggleEnabled( bool ) );
425 void VLMAWidget::modify()
427 parent->startModifyVLMItem( this );
430 void VLMAWidget::del()
432 parent->removeVLMItem( this );
435 //FIXME, remove me before release
436 void VLMAWidget::enterEvent( QEvent *event )
441 void VLMAWidget::toggleEnabled( bool b_enable )
443 VLMWrapper::EnableItem( name, b_enable );
449 VLMBroadcast::VLMBroadcast( QString _name, QString _input, QString _output,
450 bool _enabled, bool _looped, VLMDialog *_parent)
451 : VLMAWidget( _name, _input, _output,
452 _enabled, _parent, QVLM_Broadcast )
454 nameLabel->setText( "Broadcast: " + name );
455 type = QVLM_Broadcast;
458 playButton = new QToolButton;
459 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
460 objLayout->addWidget( playButton, 1, 0 );
463 QToolButton *stopButton = new QToolButton;
464 stopButton->setIcon( QIcon( QPixmap( ":/pixmaps/stop_16px.png" ) ) );
465 objLayout->addWidget( stopButton, 1, 1 );
467 loopButton = new QToolButton;
468 objLayout->addWidget( loopButton, 1, 2 );
470 BUTTONACT( playButton, togglePlayPause() );
471 BUTTONACT( stopButton, stop() );
472 BUTTONACT( loopButton, toggleLoop() );
477 void VLMBroadcast::update()
479 VLMWrapper::EditBroadcast( name, input, output, b_enabled, b_looped );
481 loopButton->setIcon( QIcon( QPixmap( ":/pixmaps/playlist_repeat_all.png" ) ) );
483 loopButton->setIcon( QIcon( QPixmap( ":/pixmaps/playlist_repeat_off.png" ) ) );
486 void VLMBroadcast::togglePlayPause()
488 if( b_playing = true )
490 VLMWrapper::ControlBroadcast( name, ControlBroadcastPause );
491 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/pause_16px.png" ) ) );
495 VLMWrapper::ControlBroadcast( name, ControlBroadcastPlay );
496 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
498 b_playing = !b_playing;
501 void VLMBroadcast::toggleLoop()
503 b_enabled = !b_enabled;
507 void VLMBroadcast::stop()
509 VLMWrapper::ControlBroadcast( name, ControlBroadcastStop );
510 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
516 VLMSchedule::VLMSchedule( QString name, QString input, QString output,
517 QDateTime _schetime, QDateTime _schedate,
518 int _scherepeatnumber, int _repeatDays,
519 bool enabled, VLMDialog *parent )
520 : VLMAWidget( name, input, output, enabled, parent, QVLM_Schedule )
522 nameLabel->setText( "Schedule: " + name );
523 schetime = _schetime;
524 schedate = _schedate;
525 rNumber = _scherepeatnumber;
527 type = QVLM_Schedule;
531 void VLMSchedule::update()
533 VLMWrapper::EditSchedule( name, input, output, schetime, schedate,
534 rNumber, rDays, b_enabled);
540 VLMVod::VLMVod( QString name, QString input, QString output,
541 bool enabled, QString _mux, VLMDialog *parent)
542 : VLMAWidget( name, input, output, enabled, parent, QVLM_VOD )
544 nameLabel->setText( "VOD:" + name );
547 muxLabel = new QLabel;
548 objLayout->addWidget( muxLabel, 1, 0 );
553 void VLMVod::update()
555 muxLabel->setText( mux );
556 VLMWrapper::EditVod( name, input, output, b_enabled, mux );
563 vlm_t * VLMWrapper::p_vlm = NULL;
565 VLMWrapper::VLMWrapper( vlm_t *_p_vlm )
570 VLMWrapper::~VLMWrapper()
573 void VLMWrapper::AddBroadcast( const QString name, QString input,
575 bool b_enabled, bool b_loop )
577 vlm_message_t *message;
578 QString command = "new \"" + name + "\" broadcast";
579 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
580 vlm_MessageDelete( message );
581 EditBroadcast( name, input, output, b_enabled, b_loop );
584 void VLMWrapper::EditBroadcast( const QString name, const QString input,
585 const QString output,
586 bool b_enabled, bool b_loop )
588 vlm_message_t *message;
591 command = "setup \"" + name + "\" inputdel all";
592 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
593 vlm_MessageDelete( message );
594 command = "setup \"" + name + "\" input \"" + input + "\"";
595 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
596 vlm_MessageDelete( message );
597 if( !output.isEmpty() )
599 command = "setup \"" + name + "\" output \"" + output + "\"";
600 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
601 vlm_MessageDelete( message );
605 command = "setup \"" + name + "\" enabled";
606 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
607 vlm_MessageDelete( message );
611 command = "setup \"" + name + "\" loop";
612 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
613 vlm_MessageDelete( message );
617 void VLMWrapper::EnableItem( const QString name, bool b_enable )
619 vlm_message_t *message;
620 QString command = "setup \"" + name + ( b_enable ? " enable" : " disable" );
623 void VLMWrapper::ControlBroadcast( const QString name, int BroadcastStatus,
626 vlm_message_t *message;
628 QString command = "control \"" + name;
629 switch( BroadcastStatus )
631 case ControlBroadcastPlay:
634 case ControlBroadcastPause:
637 case ControlBroadcastStop:
640 case ControlBroadcastSeek:
641 command += " seek" + seek;
644 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
645 vlm_MessageDelete( message );
648 void VLMWrapper::AddVod( const QString name, const QString input,
649 const QString output,
650 bool b_enabled, const QString mux )
652 vlm_message_t *message;
653 QString command = "new \"" + name + "\" vod";
654 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
655 vlm_MessageDelete( message );
656 EditVod( name, input, output, b_enabled, mux );
659 void VLMWrapper::EditVod( const QString name, const QString input,
660 const QString output,
664 vlm_message_t *message;
665 QString command = "setup \"" + name + "\" input \"" + input + "\"";
666 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
667 vlm_MessageDelete( message );
668 if( !output.isEmpty() )
670 command = "setup \"" + name + "\" output \"" + output + "\"";
671 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
672 vlm_MessageDelete( message );
676 command = "setup \"" + name + "\" enabled";
677 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
678 vlm_MessageDelete( message );
682 command = "setup \"" + name + "\" mux \"" + mux + "\"";
683 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
684 vlm_MessageDelete( message );
688 void VLMWrapper::AddSchedule( const QString name, const QString input,
689 const QString output, QDateTime _schetime,
691 int _scherepeatnumber, int _repeatDays,
692 bool b_enabled, const QString mux )
694 vlm_message_t *message;
695 QString command = "new \"" + name + "\" schedule";
696 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
697 vlm_MessageDelete( message );
698 EditSchedule( name, input, output, _schetime, _schedate,
699 _scherepeatnumber, _repeatDays, b_enabled, mux );
702 void VLMWrapper::EditSchedule( const QString name, const QString input,
703 const QString output, QDateTime _schetime,
704 QDateTime _schedate, int _scherepeatnumber,
705 int _repeatDays, bool b_enabled,
708 vlm_message_t *message;
709 QString command = "setup \"" + name + "\" input \"" + input + "\"";
710 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
711 vlm_MessageDelete( message );
713 if( !output.isEmpty() )
715 command = "setup \"" + name + "\" output \"" + output + "\"";
716 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
717 vlm_MessageDelete( message );
722 command = "setup \"" + name + "\" enabled";
723 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
724 vlm_MessageDelete( message );
729 command = "setup \"" + name + "\" mux \"" + mux + "\"";
730 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
731 vlm_MessageDelete( message );
734 command = "setup \"" + name + "\" date \"" +
735 _schedate.toString( "yyyy/MM/dd" )+ "-" +
736 _schetime.toString( "hh:mm:ss" ) + "\"";
737 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
738 vlm_MessageDelete( message );
739 if( _scherepeatnumber > 0 )
741 command = "setup \"" + name + "\" repeat \"" + _scherepeatnumber + "\"";
742 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
743 vlm_MessageDelete( message );