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>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * ( at your option ) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #include "dialogs/vlm.hpp"
29 #include <QVBoxLayout>
30 #include <QStackedWidget>
33 #include <QGridLayout>
36 #include <QToolButton>
38 #include <QPushButton>
39 #include <QHBoxLayout>
40 #include <QDateTimeEdit>
42 #include <QHeaderView>
43 #include <QScrollArea>
45 static const char *psz_type[] = { "Broadcast", "Schedule", "VOD" };
47 VLMDialog *VLMDialog::instance = NULL;
49 VLMDialog::VLMDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
51 p_vlm = vlm_New( p_intf );
55 msg_Warn( p_intf, "Couldn't build VLM object ");
58 vlmWrapper = new VLMWrapper( p_vlm );
62 ui.saveButton->hide();
64 #define ADDMEDIATYPES( str, type ) ui.mediaType->addItem( qtr( str ), QVariant( type ) );
65 ADDMEDIATYPES( "Broadcast", QVLM_Broadcast );
66 ADDMEDIATYPES( "Schedule", QVLM_Schedule );
67 ADDMEDIATYPES( "Video On Demand ( VOD )", QVLM_VOD );
71 QGridLayout *schetimelayout = new QGridLayout( ui.schedBox );
72 QLabel *schetimelabel = new QLabel( qtr( "Hours/Minutes/Seconds:" ) );
73 schetimelayout->addWidget( schetimelabel, 0, 0 );
74 QLabel *schedatelabel = new QLabel( qtr( "Day Month Year:" ) );
75 schetimelayout->addWidget( schedatelabel, 1, 0 );
76 QLabel *scherepeatLabel = new QLabel( qtr( "Repeat:" ) );
77 schetimelayout->addWidget( scherepeatLabel, 2, 0 );
78 QLabel *scherepeatTimeLabel = new QLabel( qtr( "Repeat delay:" ) );
79 schetimelayout->addWidget( scherepeatTimeLabel, 3, 0 );
81 time = new QDateTimeEdit( QTime::currentTime() );
82 time->setAlignment( Qt::AlignRight );
83 schetimelayout->addWidget( time, 0, 1, 1, 3 );
85 date = new QDateTimeEdit( QDate::currentDate() );
86 date->setAlignment( Qt::AlignRight );
87 date->setCalendarPopup( true );
89 date->setDisplayFormat( "dd MM yyyy" );
91 date->setDisplayFormat( "dd MMMM yyyy" );
93 schetimelayout->addWidget( date, 1, 1, 1, 3 );
95 scherepeatnumber = new QSpinBox;
96 scherepeatnumber->setAlignment( Qt::AlignRight );
97 schetimelayout->addWidget( scherepeatnumber, 2, 1, 1, 3 );
99 repeatDays = new QSpinBox;
100 repeatDays->setAlignment( Qt::AlignRight );
101 schetimelayout->addWidget( repeatDays, 3, 1, 1, 1 );
102 repeatDays->setSuffix( qtr(" days") );
104 repeatTime = new QDateTimeEdit;
105 repeatTime->setAlignment( Qt::AlignRight );
106 schetimelayout->addWidget( repeatTime, 3, 2, 1, 2 );
107 repeatTime->setDisplayFormat( "hh:mm:ss" );
110 ui.vlmItemScroll->setFrameStyle( QFrame::NoFrame );
111 ui.vlmItemScroll->setWidgetResizable( true );
112 vlmItemWidget = new QWidget;
113 vlmItemLayout = new QVBoxLayout( vlmItemWidget );
114 vlmItemWidget->setLayout( vlmItemLayout );
115 ui.vlmItemScroll->setWidget( vlmItemWidget );
117 QSpacerItem *spacer =
118 new QSpacerItem( 10, 10, QSizePolicy::Minimum, QSizePolicy::Expanding);
119 vlmItemLayout->addItem( spacer );
121 QPushButton *closeButton = new QPushButton( qtr( "Close" ) );
122 ui.buttonBox->addButton( closeButton, QDialogButtonBox::AcceptRole );
124 showScheduleWidget( QVLM_Broadcast );
126 /* Connect the comboBox to show the right Widgets */
127 CONNECT( ui.mediaType, currentIndexChanged( int ),
128 this, showScheduleWidget( int ) );
130 /* Connect the leftList to show the good VLMItem */
131 CONNECT( ui.vlmListItem, currentRowChanged( int ),
132 this, selectVLMItem( int ) );
134 BUTTONACT( closeButton, close() );
135 BUTTONACT( ui.addButton, addVLMItem() );
136 BUTTONACT( ui.clearButton, clearWidgets() );
137 BUTTONACT( ui.saveButton, saveModifications() );
140 VLMDialog::~VLMDialog()
142 /* FIXME :you have to destroy vlm here to close
143 * but we shouldn't destroy vlm here in case somebody else wants it */
148 void VLMDialog::showScheduleWidget( int i )
150 ui.schedBox->setVisible( ( i == QVLM_Schedule ) );
151 ui.loopBCast->setVisible( ( i == QVLM_Broadcast ) );
152 ui.vodBox->setVisible( ( i == QVLM_VOD ) );
155 void VLMDialog::selectVLMItem( int i )
157 ui.vlmItemScroll->ensureWidgetVisible( vlmItems.at( i ) );
160 bool VLMDialog::isNameGenuine( QString name )
162 for( int i = 0; i < vlmItems.size(); i++ )
164 if( vlmItems.at( i )->name == name )
170 void VLMDialog::addVLMItem()
172 int vlmItemCount = vlmItems.size();
174 /* Take the name and Check it */
175 QString name = ui.nameLedit->text();
176 if( name.isEmpty() || !isNameGenuine( name ) )
178 msg_Dbg( p_intf, "VLM Name is empty or already exists, I can't do it" );
182 int type = ui.mediaType->itemData( ui.mediaType->currentIndex() ).toInt();
184 QString typeShortName;
185 QString inputText = ui.inputLedit->text();
186 QString outputText = ui.outputLedit->text();
187 bool b_checked = ui.enableCheck->isChecked();
188 bool b_looped = ui.loopBCast->isChecked();
190 VLMAWidget * vlmAwidget;
195 typeShortName = "Bcast";
196 vlmAwidget = new VLMBroadcast( name, inputText, outputText,
197 b_checked, b_looped, this );
198 VLMWrapper::AddBroadcast( name, inputText, outputText, b_checked, b_looped );
201 typeShortName = "VOD";
202 vlmAwidget = new VLMVod( name, inputText, outputText,
203 b_checked, ui.muxLedit->text(), this );
204 VLMWrapper::AddVod( name, inputText, outputText, b_checked );
207 typeShortName = "Sched";
208 vlmAwidget = new VLMSchedule( name, inputText, outputText,
212 msg_Warn( p_intf, "Something bad happened" );
216 /* Add an Item of the Side List */
217 ui.vlmListItem->addItem( typeShortName + " : " + name );
218 ui.vlmListItem->setCurrentRow( vlmItemCount - 1 );
220 /* Add a new VLMAWidget on the main List */
222 vlmItemLayout->insertWidget( vlmItemCount, vlmAwidget );
223 vlmItems.append( vlmAwidget );
225 /* HERE BE DRAGONS VLM REQUEST */
228 void VLMDialog::clearWidgets()
230 ui.nameLedit->clear();
231 ui.inputLedit->clear();
232 ui.outputLedit->clear();
233 time->setTime( QTime::currentTime() );
234 date->setDate( QDate::currentDate() );
235 ui.enableCheck->setChecked( true );
236 ui.nameLedit->setReadOnly( false );
237 ui.loopBCast->setChecked( false );
238 ui.muxLedit->clear();
239 ui.saveButton->hide();
240 ui.addButton->show();
243 /* Object Modification */
244 void VLMDialog::removeVLMItem( VLMAWidget *vlmObj )
246 int index = vlmItems.indexOf( vlmObj );
247 if( index < 0 ) return;
249 //FIXME, this is going to segfault if the focus in on the ListWidget
250 delete ui.vlmListItem->takeItem( index );
251 vlmItems.removeAt( index );
254 /* HERE BE DRAGONS VLM REQUEST */
257 void VLMDialog::startModifyVLMItem( VLMAWidget *vlmObj )
259 currentIndex = vlmItems.indexOf( vlmObj );
260 if( currentIndex < 0 ) return;
262 msg_Dbg( p_intf, "Type: %i", vlmObj->type );
263 ui.vlmListItem->setCurrentRow( currentIndex );
264 ui.nameLedit->setText( vlmObj->name );
265 ui.inputLedit->setText( vlmObj->input );
266 ui.outputLedit->setText( vlmObj->output );
267 ui.enableCheck->setChecked( vlmObj->b_enabled );
269 switch( vlmObj->type )
272 ui.loopBCast->setChecked( (qobject_cast<VLMBroadcast *>(vlmObj))->b_looped );
275 ui.muxLedit->setText( (qobject_cast<VLMVod *>(vlmObj))->mux );
278 //(qobject_cast<VLMSchedule *>)
282 ui.nameLedit->setReadOnly( true );
283 ui.addButton->hide();
284 ui.saveButton->show();
287 void VLMDialog::saveModifications()
289 VLMAWidget *vlmObj = vlmItems.at( currentIndex );
292 vlmObj->input = ui.inputLedit->text();
293 vlmObj->output = ui.outputLedit->text();
294 vlmObj->setChecked( ui.enableCheck->isChecked() );
295 vlmObj->b_enabled = ui.enableCheck->isChecked();
296 switch( vlmObj->type )
299 (qobject_cast<VLMBroadcast *>(vlmObj))->b_looped = ui.loopBCast->isChecked();
302 (qobject_cast<VLMVod *>(vlmObj))->mux = ui.muxLedit->text();
308 vlmObj->update(); /* It should call the correct function is VLMAWidget
309 is abstract, but I am far from sure... FIXME ? */
314 /*********************************
315 * VLMAWidget - Abstract class
316 ********************************/
318 VLMAWidget::VLMAWidget( QString _name,
324 : QGroupBox( _name, _parent )
330 b_enabled = _enabled;
333 setCheckable( true );
334 setChecked( b_enabled );
336 objLayout = new QGridLayout( this );
337 setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Maximum );
339 nameLabel = new QLabel;
340 objLayout->addWidget( nameLabel, 0, 0, 1, 4 );
342 /*QLabel *time = new QLabel( "--:--/--:--" );
343 objLayout->addWidget( time, 1, 3, 1, 2 );*/
345 QToolButton *modifyButton = new QToolButton;
346 modifyButton->setIcon( QIcon( QPixmap( ":/pixmaps/menus_settings_16px.png" ) ) );
347 objLayout->addWidget( modifyButton, 0, 5 );
349 QToolButton *deleteButton = new QToolButton;
350 deleteButton->setIcon( QIcon( QPixmap( ":/pixmaps/menus_quit_16px.png" ) ) );
351 objLayout->addWidget( deleteButton, 0, 6 );
353 BUTTONACT( modifyButton, modify() );
354 BUTTONACT( deleteButton, del() );
355 CONNECT( this, clicked( bool ), this, toggleEnabled( bool ) );
358 void VLMAWidget::modify()
360 parent->startModifyVLMItem( this );
363 void VLMAWidget::del()
365 parent->removeVLMItem( this );
368 //FIXME, remove me before release
369 void VLMAWidget::enterEvent( QEvent *event )
374 void VLMAWidget::toggleEnabled( bool b_enable )
376 VLMWrapper::EnableItem( name, b_enable );
382 VLMBroadcast::VLMBroadcast( QString _name, QString _input, QString _output,
383 bool _enabled, bool _looped, VLMDialog *_parent)
384 : VLMAWidget( _name, _input, _output,
385 _enabled, _parent, QVLM_Broadcast )
387 nameLabel->setText( "Broadcast: " + name );
388 type = QVLM_Broadcast;
391 playButton = new QToolButton;
392 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
393 objLayout->addWidget( playButton, 1, 0 );
396 QToolButton *stopButton = new QToolButton;
397 stopButton->setIcon( QIcon( QPixmap( ":/pixmaps/stop_16px.png" ) ) );
398 objLayout->addWidget( stopButton, 1, 1 );
400 loopButton = new QToolButton;
401 objLayout->addWidget( loopButton, 1, 2 );
403 BUTTONACT( playButton, togglePlayPause() );
404 BUTTONACT( stopButton, stop() );
405 BUTTONACT( loopButton, toggleLoop() );
410 void VLMBroadcast::update()
412 VLMWrapper::EditBroadcast( name, input, output, b_enabled, b_looped );
414 loopButton->setIcon( QIcon( QPixmap( ":/pixmaps/playlist_repeat_all.png" ) ) );
416 loopButton->setIcon( QIcon( QPixmap( ":/pixmaps/playlist_repeat_off.png" ) ) );
419 void VLMBroadcast::togglePlayPause()
421 if( b_playing = true )
423 VLMWrapper::ControlBroadcast( name, ControlBroadcastPause );
424 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/pause_16px.png" ) ) );
428 VLMWrapper::ControlBroadcast( name, ControlBroadcastPlay );
429 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
431 b_playing = !b_playing;
434 void VLMBroadcast::toggleLoop()
436 b_enabled = !b_enabled;
440 void VLMBroadcast::stop()
442 VLMWrapper::ControlBroadcast( name, ControlBroadcastStop );
443 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
449 VLMSchedule::VLMSchedule( QString name, QString input, QString output,
450 bool enabled, VLMDialog *parent)
451 : VLMAWidget( name, input, output, enabled, parent, QVLM_Schedule )
453 nameLabel->setText( "Schedule: " + name );
456 void VLMSchedule::update()
463 VLMVod::VLMVod( QString name, QString input, QString output,
464 bool enabled, QString _mux, VLMDialog *parent)
465 : VLMAWidget( name, input, output, enabled, parent, QVLM_VOD )
467 nameLabel->setText( "VOD:" + name );
470 muxLabel = new QLabel;
471 objLayout->addWidget( muxLabel, 1, 0 );
476 void VLMVod::update()
478 muxLabel->setText( mux );
479 VLMWrapper::EditVod( name, input, output, b_enabled, mux );
486 vlm_t * VLMWrapper::p_vlm = NULL;
488 VLMWrapper::VLMWrapper( vlm_t *_p_vlm )
493 VLMWrapper::~VLMWrapper()
496 void VLMWrapper::AddBroadcast( const QString name, QString input,
498 bool b_enabled, bool b_loop )
500 vlm_message_t *message;
501 QString command = "new \"" + name + "\" broadcast";
502 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
503 vlm_MessageDelete( message );
504 EditBroadcast( name, input, output, b_enabled, b_loop );
507 void VLMWrapper::EditBroadcast( const QString name, const QString input,
508 const QString output,
509 bool b_enabled, bool b_loop )
511 vlm_message_t *message;
514 command = "setup \"" + name + "\" inputdel all";
515 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
516 vlm_MessageDelete( message );
517 command = "setup \"" + name + "\" input \"" + input + "\"";
518 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
519 vlm_MessageDelete( message );
520 if( !output.isEmpty() )
522 command = "setup \"" + name + "\" output \"" + output + "\"";
523 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
524 vlm_MessageDelete( message );
528 command = "setup \"" + name + "\" enabled";
529 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
530 vlm_MessageDelete( message );
534 command = "setup \"" + name + "\" loop";
535 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
536 vlm_MessageDelete( message );
540 void VLMWrapper::EnableItem( const QString name, bool b_enable )
542 vlm_message_t *message;
543 QString command = "setup \"" + name + ( b_enable ? " enable" : " disable" );
546 void VLMWrapper::ControlBroadcast( const QString name, int BroadcastStatus,
549 vlm_message_t *message;
551 QString command = "control \"" + name;
552 switch( BroadcastStatus )
554 case ControlBroadcastPlay:
557 case ControlBroadcastPause:
560 case ControlBroadcastStop:
563 case ControlBroadcastSeek:
564 command += " seek" + seek;
567 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
568 vlm_MessageDelete( message );
571 void VLMWrapper::AddVod( const QString name, const QString input,
572 const QString output,
573 bool b_enabled, const QString mux )
575 vlm_message_t *message;
576 QString command = "new \"" + name + "\" vod";
577 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
578 vlm_MessageDelete( message );
579 EditVod( name, input, output, b_enabled, mux );
582 void VLMWrapper::EditVod( const QString name, const QString input,
583 const QString output,
587 vlm_message_t *message;
588 QString command = "setup \"" + name + "\" input \"" + input + "\"";
589 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
590 vlm_MessageDelete( message );
591 if( !output.isEmpty() )
593 command = "setup \"" + name + "\" output \"" + output + "\"";
594 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
595 vlm_MessageDelete( message );
599 command = "setup \"" + name + "\" enabled";
600 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
601 vlm_MessageDelete( message );
605 command = "setup \"" + name + "\" mux \"" + mux + "\"";
606 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
607 vlm_MessageDelete( message );