1 /*****************************************************************************
2 * vlm.cpp : VLM Management
3 ****************************************************************************
4 * Copyright © 2008 the VideoLAN team
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 *importButton = new QPushButton( qtr( "Import" ) );
134 ui.buttonBox->addButton( importButton, QDialogButtonBox::ActionRole );
136 QPushButton *exportButton = new QPushButton( qtr( "Export" ) );
137 ui.buttonBox->addButton( exportButton, QDialogButtonBox::ActionRole );
139 QPushButton *closeButton = new QPushButton( qtr( "Close" ) );
140 ui.buttonBox->addButton( closeButton, QDialogButtonBox::AcceptRole );
143 showScheduleWidget( QVLM_Broadcast );
145 /* Connect the comboBox to show the right Widgets */
146 CONNECT( ui.mediaType, currentIndexChanged( int ),
147 this, showScheduleWidget( int ) );
149 /* Connect the leftList to show the good VLMItem */
150 CONNECT( ui.vlmListItem, currentRowChanged( int ),
151 this, selectVLMItem( int ) );
153 BUTTONACT( closeButton, close() );
154 BUTTONACT( exportButton, exportVLMConf() );
155 BUTTONACT( importButton, importVLMConf() );
156 BUTTONACT( ui.addButton, addVLMItem() );
157 BUTTONACT( ui.clearButton, clearWidgets() );
158 BUTTONACT( ui.saveButton, saveModifications() );
159 BUTTONACT( ui.inputButton, selectInput() );
160 BUTTONACT( ui.outputButton, selectOutput() );
163 VLMDialog::~VLMDialog()
165 /* FIXME :you have to destroy vlm here to close
166 * but we shouldn't destroy vlm here in case somebody else wants it */
171 void VLMDialog::showScheduleWidget( int i )
173 ui.schedBox->setVisible( ( i == QVLM_Schedule ) );
174 ui.loopBCast->setVisible( ( i == QVLM_Broadcast ) );
175 ui.vodBox->setVisible( ( i == QVLM_VOD ) );
178 void VLMDialog::selectVLMItem( int i )
181 ui.vlmItemScroll->ensureWidgetVisible( vlmItems.at( i ) );
184 bool VLMDialog::isNameGenuine( QString name )
186 for( int i = 0; i < vlmItems.size(); i++ )
188 if( vlmItems.at( i )->name == name )
194 void VLMDialog::addVLMItem()
196 int vlmItemCount = vlmItems.size();
198 /* Take the name and Check it */
199 QString name = ui.nameLedit->text();
200 if( name.isEmpty() || !isNameGenuine( name ) )
202 msg_Dbg( p_intf, "VLM Name is empty or already exists, I can't do it" );
206 int type = ui.mediaType->itemData( ui.mediaType->currentIndex() ).toInt();
208 QString typeShortName;
209 QString inputText = ui.inputLedit->text();
210 QString outputText = ui.outputLedit->text();
211 bool b_checked = ui.enableCheck->isChecked();
212 bool b_looped = ui.loopBCast->isChecked();
213 QDateTime schetime = time->dateTime();
214 QDateTime schedate = date->dateTime();
215 int repeatnum = scherepeatnumber->value();
216 int repeatdays = repeatDays->value();
217 VLMAWidget * vlmAwidget;
222 typeShortName = "Bcast";
223 vlmAwidget = new VLMBroadcast( name, inputText, outputText,
224 b_checked, b_looped, this );
225 VLMWrapper::AddBroadcast( name, inputText, outputText, b_checked,
229 typeShortName = "VOD";
230 vlmAwidget = new VLMVod( name, inputText, outputText,
231 b_checked, ui.muxLedit->text(), this );
232 VLMWrapper::AddVod( name, inputText, outputText, b_checked );
235 typeShortName = "Sched";
236 vlmAwidget = new VLMSchedule( name, inputText, outputText,
237 schetime, schedate, repeatnum,
238 repeatdays, b_checked, this );
239 VLMWrapper::AddSchedule( name, inputText, outputText, schetime,
240 schedate, repeatnum, repeatdays, b_checked);
243 msg_Warn( p_intf, "Something bad happened" );
247 /* Add an Item of the Side List */
248 ui.vlmListItem->addItem( typeShortName + " : " + name );
249 ui.vlmListItem->setCurrentRow( vlmItemCount - 1 );
251 /* Add a new VLMAWidget on the main List */
253 vlmItemLayout->insertWidget( vlmItemCount, vlmAwidget );
254 vlmItems.append( vlmAwidget );
258 // FIXME : VOD are not exported to the file
259 bool VLMDialog::exportVLMConf()
261 QString saveVLMConfFileName = QFileDialog::getSaveFileName(
262 this, qtr( "Choose a filename to save the VLM configuration..." ),
263 qfu( p_intf->p_libvlc->psz_homedir ),
264 qtr( "VLM conf (*.vlm) ;; All (*.*)" ) );
266 if( !saveVLMConfFileName.isEmpty() )
268 vlm_message_t *message;
269 QString command = "save \"" + saveVLMConfFileName + "\"";
270 vlm_ExecuteCommand( p_vlm , qtu( command ) , &message );
271 vlm_MessageDelete( message );
278 bool VLMDialog::importVLMConf()
280 QString openVLMConfFileName = QFileDialog::getOpenFileName(
281 this, qtr( "Choose a VLM configuration file to open..." ),
282 qfu( p_intf->p_libvlc->psz_homedir ),
283 qtr( "VLM conf (*.vlm) ;; All (*.*)" ) );
285 if( !openVLMConfFileName.isEmpty() )
287 vlm_message_t *message;
288 QString command = "load \"" + openVLMConfFileName + "\"";
289 vlm_ExecuteCommand( p_vlm, qtu( command ) , &message );
290 vlm_MessageDelete( message );
296 void VLMDialog::clearWidgets()
298 ui.nameLedit->clear();
299 ui.inputLedit->clear();
300 ui.outputLedit->clear();
301 time->setTime( QTime::currentTime() );
302 date->setDate( QDate::currentDate() );
303 ui.enableCheck->setChecked( true );
304 ui.nameLedit->setReadOnly( false );
305 ui.loopBCast->setChecked( false );
306 ui.muxLedit->clear();
307 ui.saveButton->hide();
308 ui.addButton->show();
311 void VLMDialog::selectInput()
313 OpenDialog *o = OpenDialog::getInstance( this, p_intf, SELECT, true );
315 ui.inputLedit->setText( o->getMRL() );
318 void VLMDialog::selectOutput()
320 SoutDialog *s = SoutDialog::getInstance( this, p_intf, false );
321 if( s->exec() == QDialog::Accepted )
322 ui.outputLedit->setText( s->getMrl() );
325 /* Object Modification */
326 void VLMDialog::removeVLMItem( VLMAWidget *vlmObj )
328 int index = vlmItems.indexOf( vlmObj );
329 if( index < 0 ) return;
330 delete ui.vlmListItem->takeItem( index );
331 vlmItems.removeAt( index );
334 /* HERE BE DRAGONS VLM REQUEST */
337 void VLMDialog::startModifyVLMItem( VLMAWidget *vlmObj )
339 currentIndex = vlmItems.indexOf( vlmObj );
340 if( currentIndex < 0 ) return;
342 msg_Dbg( p_intf, "Type: %i", vlmObj->type );
343 ui.vlmListItem->setCurrentRow( currentIndex );
344 ui.nameLedit->setText( vlmObj->name );
345 ui.inputLedit->setText( vlmObj->input );
346 ui.outputLedit->setText( vlmObj->output );
347 ui.enableCheck->setChecked( vlmObj->b_enabled );
349 switch( vlmObj->type )
352 ui.loopBCast->setChecked( (qobject_cast<VLMBroadcast *>(vlmObj))->b_looped );
355 ui.muxLedit->setText( (qobject_cast<VLMVod *>(vlmObj))->mux );
358 time->setDateTime( ( qobject_cast<VLMSchedule *>(vlmObj))->schetime );
359 date->setDateTime( ( qobject_cast<VLMSchedule *>(vlmObj))->schedate );
363 ui.nameLedit->setReadOnly( true );
364 ui.addButton->hide();
365 ui.saveButton->show();
368 void VLMDialog::saveModifications()
370 VLMAWidget *vlmObj = vlmItems.at( currentIndex );
373 vlmObj->input = ui.inputLedit->text();
374 vlmObj->output = ui.outputLedit->text();
375 vlmObj->setChecked( ui.enableCheck->isChecked() );
376 vlmObj->b_enabled = ui.enableCheck->isChecked();
377 switch( vlmObj->type )
380 (qobject_cast<VLMBroadcast *>(vlmObj))->b_looped = ui.loopBCast->isChecked();
383 (qobject_cast<VLMVod *>(vlmObj))->mux = ui.muxLedit->text();
386 (qobject_cast<VLMSchedule *>(vlmObj))->schetime = time->dateTime();
387 (qobject_cast<VLMSchedule *>(vlmObj))->schedate = date->dateTime();
388 (qobject_cast<VLMSchedule *>(vlmObj))->rNumber = scherepeatnumber->value();
389 (qobject_cast<VLMSchedule *>(vlmObj))->rDays = repeatDays->value();
393 vlmObj->update(); /* It should call the correct function is VLMAWidget
394 is abstract, but I am far from sure... FIXME ? */
399 /*********************************
400 * VLMAWidget - Abstract class
401 ********************************/
403 VLMAWidget::VLMAWidget( QString _name,
409 : QGroupBox( _name, _parent )
415 b_enabled = _enabled;
418 setCheckable( true );
419 setChecked( b_enabled );
421 objLayout = new QGridLayout( this );
422 setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Maximum );
424 nameLabel = new QLabel;
425 objLayout->addWidget( nameLabel, 0, 0, 1, 4 );
427 /*QLabel *time = new QLabel( "--:--/--:--" );
428 objLayout->addWidget( time, 1, 3, 1, 2 );*/
430 QToolButton *modifyButton = new QToolButton;
431 modifyButton->setIcon( QIcon( QPixmap( ":/pixmaps/menus_settings_16px.png" ) ) );
432 objLayout->addWidget( modifyButton, 0, 5 );
434 QToolButton *deleteButton = new QToolButton;
435 deleteButton->setIcon( QIcon( QPixmap( ":/pixmaps/menus_quit_16px.png" ) ) );
436 objLayout->addWidget( deleteButton, 0, 6 );
438 BUTTONACT( modifyButton, modify() );
439 BUTTONACT( deleteButton, del() );
440 CONNECT( this, clicked( bool ), this, toggleEnabled( bool ) );
443 void VLMAWidget::modify()
445 parent->startModifyVLMItem( this );
448 void VLMAWidget::del()
450 parent->removeVLMItem( this );
453 //FIXME, remove me before release
454 void VLMAWidget::enterEvent( QEvent *event )
459 void VLMAWidget::toggleEnabled( bool b_enable )
461 VLMWrapper::EnableItem( name, b_enable );
467 VLMBroadcast::VLMBroadcast( QString _name, QString _input, QString _output,
468 bool _enabled, bool _looped, VLMDialog *_parent)
469 : VLMAWidget( _name, _input, _output,
470 _enabled, _parent, QVLM_Broadcast )
472 nameLabel->setText( "Broadcast: " + name );
473 type = QVLM_Broadcast;
476 playButton = new QToolButton;
477 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
478 objLayout->addWidget( playButton, 1, 0 );
481 QToolButton *stopButton = new QToolButton;
482 stopButton->setIcon( QIcon( QPixmap( ":/pixmaps/stop_16px.png" ) ) );
483 objLayout->addWidget( stopButton, 1, 1 );
485 loopButton = new QToolButton;
486 objLayout->addWidget( loopButton, 1, 2 );
488 BUTTONACT( playButton, togglePlayPause() );
489 BUTTONACT( stopButton, stop() );
490 BUTTONACT( loopButton, toggleLoop() );
495 void VLMBroadcast::update()
497 VLMWrapper::EditBroadcast( name, input, output, b_enabled, b_looped );
499 loopButton->setIcon( QIcon( QPixmap( ":/pixmaps/playlist_repeat_all.png" ) ) );
501 loopButton->setIcon( QIcon( QPixmap( ":/pixmaps/playlist_repeat_off.png" ) ) );
504 void VLMBroadcast::togglePlayPause()
506 if( b_playing = true )
508 VLMWrapper::ControlBroadcast( name, ControlBroadcastPause );
509 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/pause_16px.png" ) ) );
513 VLMWrapper::ControlBroadcast( name, ControlBroadcastPlay );
514 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
516 b_playing = !b_playing;
519 void VLMBroadcast::toggleLoop()
521 b_enabled = !b_enabled;
525 void VLMBroadcast::stop()
527 VLMWrapper::ControlBroadcast( name, ControlBroadcastStop );
528 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
534 VLMSchedule::VLMSchedule( QString name, QString input, QString output,
535 QDateTime _schetime, QDateTime _schedate,
536 int _scherepeatnumber, int _repeatDays,
537 bool enabled, VLMDialog *parent )
538 : VLMAWidget( name, input, output, enabled, parent, QVLM_Schedule )
540 nameLabel->setText( "Schedule: " + name );
541 schetime = _schetime;
542 schedate = _schedate;
543 rNumber = _scherepeatnumber;
545 type = QVLM_Schedule;
549 void VLMSchedule::update()
551 VLMWrapper::EditSchedule( name, input, output, schetime, schedate,
552 rNumber, rDays, b_enabled);
558 VLMVod::VLMVod( QString name, QString input, QString output,
559 bool enabled, QString _mux, VLMDialog *parent)
560 : VLMAWidget( name, input, output, enabled, parent, QVLM_VOD )
562 nameLabel->setText( "VOD:" + name );
565 muxLabel = new QLabel;
566 objLayout->addWidget( muxLabel, 1, 0 );
571 void VLMVod::update()
573 muxLabel->setText( mux );
574 VLMWrapper::EditVod( name, input, output, b_enabled, mux );
581 vlm_t * VLMWrapper::p_vlm = NULL;
583 VLMWrapper::VLMWrapper( vlm_t *_p_vlm )
588 VLMWrapper::~VLMWrapper()
591 void VLMWrapper::AddBroadcast( const QString name, QString input,
593 bool b_enabled, bool b_loop )
595 vlm_message_t *message;
596 QString command = "new \"" + name + "\" broadcast";
597 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
598 vlm_MessageDelete( message );
599 EditBroadcast( name, input, output, b_enabled, b_loop );
602 void VLMWrapper::EditBroadcast( const QString name, const QString input,
603 const QString output,
604 bool b_enabled, bool b_loop )
606 vlm_message_t *message;
609 command = "setup \"" + name + "\" inputdel all";
610 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
611 vlm_MessageDelete( message );
612 command = "setup \"" + name + "\" input \"" + input + "\"";
613 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
614 vlm_MessageDelete( message );
615 if( !output.isEmpty() )
617 command = "setup \"" + name + "\" output \"" + output + "\"";
618 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
619 vlm_MessageDelete( message );
623 command = "setup \"" + name + "\" enabled";
624 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
625 vlm_MessageDelete( message );
629 command = "setup \"" + name + "\" loop";
630 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
631 vlm_MessageDelete( message );
635 void VLMWrapper::EnableItem( const QString name, bool b_enable )
637 vlm_message_t *message;
638 QString command = "setup \"" + name + ( b_enable ? " enable" : " disable" );
641 void VLMWrapper::ControlBroadcast( const QString name, int BroadcastStatus,
644 vlm_message_t *message;
646 QString command = "control \"" + name;
647 switch( BroadcastStatus )
649 case ControlBroadcastPlay:
652 case ControlBroadcastPause:
655 case ControlBroadcastStop:
658 case ControlBroadcastSeek:
659 command += " seek" + seek;
662 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
663 vlm_MessageDelete( message );
666 void VLMWrapper::AddVod( const QString name, const QString input,
667 const QString output,
668 bool b_enabled, const QString mux )
670 vlm_message_t *message;
671 QString command = "new \"" + name + "\" vod";
672 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
673 vlm_MessageDelete( message );
674 EditVod( name, input, output, b_enabled, mux );
677 void VLMWrapper::EditVod( const QString name, const QString input,
678 const QString output,
682 vlm_message_t *message;
683 QString command = "setup \"" + name + "\" input \"" + input + "\"";
684 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
685 vlm_MessageDelete( message );
686 if( !output.isEmpty() )
688 command = "setup \"" + name + "\" output \"" + output + "\"";
689 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
690 vlm_MessageDelete( message );
694 command = "setup \"" + name + "\" enabled";
695 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
696 vlm_MessageDelete( message );
700 command = "setup \"" + name + "\" mux \"" + mux + "\"";
701 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
702 vlm_MessageDelete( message );
706 void VLMWrapper::AddSchedule( const QString name, const QString input,
707 const QString output, QDateTime _schetime,
709 int _scherepeatnumber, int _repeatDays,
710 bool b_enabled, const QString mux )
712 vlm_message_t *message;
713 QString command = "new \"" + name + "\" schedule";
714 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
715 vlm_MessageDelete( message );
716 EditSchedule( name, input, output, _schetime, _schedate,
717 _scherepeatnumber, _repeatDays, b_enabled, mux );
720 void VLMWrapper::EditSchedule( const QString name, const QString input,
721 const QString output, QDateTime _schetime,
722 QDateTime _schedate, int _scherepeatnumber,
723 int _repeatDays, bool b_enabled,
726 vlm_message_t *message;
727 QString command = "setup \"" + name + "\" input \"" + input + "\"";
728 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
729 vlm_MessageDelete( message );
731 if( !output.isEmpty() )
733 command = "setup \"" + name + "\" output \"" + output + "\"";
734 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
735 vlm_MessageDelete( message );
740 command = "setup \"" + name + "\" enabled";
741 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
742 vlm_MessageDelete( message );
747 command = "setup \"" + name + "\" mux \"" + mux + "\"";
748 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
749 vlm_MessageDelete( message );
752 command = "setup \"" + name + "\" date \"" +
753 _schedate.toString( "yyyy/MM/dd" )+ "-" +
754 _schetime.toString( "hh:mm:ss" ) + "\"";
755 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
756 vlm_MessageDelete( message );
757 if( _scherepeatnumber > 0 )
759 command = "setup \"" + name + "\" repeat \"" + _scherepeatnumber + "\"";
760 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
761 vlm_MessageDelete( message );