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"
30 #include <QVBoxLayout>
31 #include <QStackedWidget>
34 #include <QGridLayout>
37 #include <QToolButton>
39 #include <QPushButton>
40 #include <QHBoxLayout>
41 #include <QDateTimeEdit>
43 #include <QHeaderView>
44 #include <QScrollArea>
46 static const char *psz_type[] = { "Broadcast", "Schedule", "VOD" };
48 VLMDialog *VLMDialog::instance = NULL;
50 VLMDialog::VLMDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
52 p_vlm = vlm_New( p_intf );
56 msg_Warn( p_intf, "Couldn't build VLM object ");
59 vlmWrapper = new VLMWrapper( p_vlm );
63 ui.saveButton->hide();
65 #define ADDMEDIATYPES( str, type ) ui.mediaType->addItem( qtr( str ), QVariant( type ) );
66 ADDMEDIATYPES( "Broadcast", QVLM_Broadcast );
67 ADDMEDIATYPES( "Schedule", QVLM_Schedule );
68 ADDMEDIATYPES( "Video On Demand ( VOD )", QVLM_VOD );
72 QGridLayout *schetimelayout = new QGridLayout( ui.schedBox );
73 QLabel *schetimelabel = new QLabel( qtr( "Hours/Minutes/Seconds:" ) );
74 schetimelayout->addWidget( schetimelabel, 0, 0 );
75 QLabel *schedatelabel = new QLabel( qtr( "Day Month Year:" ) );
76 schetimelayout->addWidget( schedatelabel, 1, 0 );
77 QLabel *scherepeatLabel = new QLabel( qtr( "Repeat:" ) );
78 schetimelayout->addWidget( scherepeatLabel, 2, 0 );
79 QLabel *scherepeatTimeLabel = new QLabel( qtr( "Repeat delay:" ) );
80 schetimelayout->addWidget( scherepeatTimeLabel, 3, 0 );
82 time = new QDateTimeEdit( QTime::currentTime() );
83 time->setAlignment( Qt::AlignRight );
84 schetimelayout->addWidget( time, 0, 1, 1, 3 );
86 date = new QDateTimeEdit( QDate::currentDate() );
87 date->setAlignment( Qt::AlignRight );
88 date->setCalendarPopup( true );
90 date->setDisplayFormat( "dd MM yyyy" );
92 date->setDisplayFormat( "dd MMMM yyyy" );
94 schetimelayout->addWidget( date, 1, 1, 1, 3 );
96 scherepeatnumber = new QSpinBox;
97 scherepeatnumber->setAlignment( Qt::AlignRight );
98 schetimelayout->addWidget( scherepeatnumber, 2, 1, 1, 3 );
100 repeatDays = new QSpinBox;
101 repeatDays->setAlignment( Qt::AlignRight );
102 schetimelayout->addWidget( repeatDays, 3, 1, 1, 1 );
103 repeatDays->setSuffix( qtr(" days") );
105 repeatTime = new QDateTimeEdit;
106 repeatTime->setAlignment( Qt::AlignRight );
107 schetimelayout->addWidget( repeatTime, 3, 2, 1, 2 );
108 repeatTime->setDisplayFormat( "hh:mm:ss" );
111 ui.vlmItemScroll->setFrameStyle( QFrame::NoFrame );
112 ui.vlmItemScroll->setWidgetResizable( true );
113 vlmItemWidget = new QWidget;
114 vlmItemLayout = new QVBoxLayout( vlmItemWidget );
115 vlmItemWidget->setLayout( vlmItemLayout );
116 ui.vlmItemScroll->setWidget( vlmItemWidget );
118 QSpacerItem *spacer =
119 new QSpacerItem( 10, 10, QSizePolicy::Minimum, QSizePolicy::Expanding);
120 vlmItemLayout->addItem( spacer );
122 QPushButton *closeButton = new QPushButton( qtr( "Close" ) );
123 ui.buttonBox->addButton( closeButton, QDialogButtonBox::AcceptRole );
125 showScheduleWidget( QVLM_Broadcast );
127 /* Connect the comboBox to show the right Widgets */
128 CONNECT( ui.mediaType, currentIndexChanged( int ),
129 this, showScheduleWidget( int ) );
131 /* Connect the leftList to show the good VLMItem */
132 CONNECT( ui.vlmListItem, currentRowChanged( int ),
133 this, selectVLMItem( int ) );
135 BUTTONACT( closeButton, close() );
136 BUTTONACT( ui.addButton, addVLMItem() );
137 BUTTONACT( ui.clearButton, clearWidgets() );
138 BUTTONACT( ui.saveButton, saveModifications() );
141 VLMDialog::~VLMDialog()
143 /* FIXME :you have to destroy vlm here to close
144 * but we shouldn't destroy vlm here in case somebody else wants it */
149 void VLMDialog::showScheduleWidget( int i )
151 ui.schedBox->setVisible( ( i == QVLM_Schedule ) );
152 ui.loopBCast->setVisible( ( i == QVLM_Broadcast ) );
153 ui.vodBox->setVisible( ( i == QVLM_VOD ) );
156 void VLMDialog::selectVLMItem( int i )
158 ui.vlmItemScroll->ensureWidgetVisible( vlmItems.at( i ) );
161 bool VLMDialog::isNameGenuine( QString name )
163 for( int i = 0; i < vlmItems.size(); i++ )
165 if( vlmItems.at( i )->name == name )
171 void VLMDialog::addVLMItem()
173 int vlmItemCount = vlmItems.size();
175 /* Take the name and Check it */
176 QString name = ui.nameLedit->text();
177 if( name.isEmpty() || !isNameGenuine( name ) )
179 msg_Dbg( p_intf, "VLM Name is empty or already exists, I can't do it" );
183 int type = ui.mediaType->itemData( ui.mediaType->currentIndex() ).toInt();
185 QString typeShortName;
186 QString inputText = ui.inputLedit->text();
187 QString outputText = ui.outputLedit->text();
188 bool b_checked = ui.enableCheck->isChecked();
189 bool b_looped = ui.loopBCast->isChecked();
191 VLMAWidget * vlmAwidget;
196 typeShortName = "Bcast";
197 vlmAwidget = new VLMBroadcast( name, inputText, outputText,
198 b_checked, b_looped, this );
199 VLMWrapper::AddBroadcast( name, inputText, outputText, b_checked, b_looped );
202 typeShortName = "VOD";
203 vlmAwidget = new VLMVod( name, inputText, outputText,
204 b_checked, ui.muxLedit->text(), this );
205 VLMWrapper::AddVod( name, inputText, outputText, b_checked );
208 typeShortName = "Sched";
209 vlmAwidget = new VLMSchedule( name, inputText, outputText,
213 msg_Warn( p_intf, "Something bad happened" );
217 /* Add an Item of the Side List */
218 ui.vlmListItem->addItem( typeShortName + " : " + name );
219 ui.vlmListItem->setCurrentRow( vlmItemCount - 1 );
221 /* Add a new VLMAWidget on the main List */
223 vlmItemLayout->insertWidget( vlmItemCount, vlmAwidget );
224 vlmItems.append( vlmAwidget );
226 /* HERE BE DRAGONS VLM REQUEST */
229 void VLMDialog::clearWidgets()
231 ui.nameLedit->clear();
232 ui.inputLedit->clear();
233 ui.outputLedit->clear();
234 time->setTime( QTime::currentTime() );
235 date->setDate( QDate::currentDate() );
236 ui.enableCheck->setChecked( true );
237 ui.nameLedit->setReadOnly( false );
238 ui.loopBCast->setChecked( false );
239 ui.muxLedit->clear();
240 ui.saveButton->hide();
241 ui.addButton->show();
244 /* Object Modification */
245 void VLMDialog::removeVLMItem( VLMAWidget *vlmObj )
247 int index = vlmItems.indexOf( vlmObj );
248 if( index < 0 ) return;
250 //FIXME, this is going to segfault if the focus in on the ListWidget
251 delete ui.vlmListItem->takeItem( index );
252 vlmItems.removeAt( index );
255 /* HERE BE DRAGONS VLM REQUEST */
258 void VLMDialog::startModifyVLMItem( VLMAWidget *vlmObj )
260 currentIndex = vlmItems.indexOf( vlmObj );
261 if( currentIndex < 0 ) return;
263 msg_Dbg( p_intf, "Type: %i", vlmObj->type );
264 ui.vlmListItem->setCurrentRow( currentIndex );
265 ui.nameLedit->setText( vlmObj->name );
266 ui.inputLedit->setText( vlmObj->input );
267 ui.outputLedit->setText( vlmObj->output );
268 ui.enableCheck->setChecked( vlmObj->b_enabled );
270 switch( vlmObj->type )
273 ui.loopBCast->setChecked( (qobject_cast<VLMBroadcast *>(vlmObj))->b_looped );
276 ui.muxLedit->setText( (qobject_cast<VLMVod *>(vlmObj))->mux );
279 //(qobject_cast<VLMSchedule *>)
283 ui.nameLedit->setReadOnly( true );
284 ui.addButton->hide();
285 ui.saveButton->show();
288 void VLMDialog::saveModifications()
290 VLMAWidget *vlmObj = vlmItems.at( currentIndex );
293 vlmObj->input = ui.inputLedit->text();
294 vlmObj->output = ui.outputLedit->text();
295 vlmObj->setChecked( ui.enableCheck->isChecked() );
296 vlmObj->b_enabled = ui.enableCheck->isChecked();
297 switch( vlmObj->type )
300 (qobject_cast<VLMBroadcast *>(vlmObj))->b_looped = ui.loopBCast->isChecked();
303 (qobject_cast<VLMVod *>(vlmObj))->mux = ui.muxLedit->text();
309 vlmObj->update(); /* It should call the correct function is VLMAWidget
310 is abstract, but I am far from sure... FIXME ? */
315 /*********************************
316 * VLMAWidget - Abstract class
317 ********************************/
319 VLMAWidget::VLMAWidget( QString _name,
325 : QGroupBox( _name, _parent )
331 b_enabled = _enabled;
334 setCheckable( true );
335 setChecked( b_enabled );
337 objLayout = new QGridLayout( this );
338 setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Maximum );
340 nameLabel = new QLabel;
341 objLayout->addWidget( nameLabel, 0, 0, 1, 4 );
343 /*QLabel *time = new QLabel( "--:--/--:--" );
344 objLayout->addWidget( time, 1, 3, 1, 2 );*/
346 QToolButton *modifyButton = new QToolButton;
347 modifyButton->setIcon( QIcon( QPixmap( ":/pixmaps/menus_settings_16px.png" ) ) );
348 objLayout->addWidget( modifyButton, 0, 5 );
350 QToolButton *deleteButton = new QToolButton;
351 deleteButton->setIcon( QIcon( QPixmap( ":/pixmaps/menus_quit_16px.png" ) ) );
352 objLayout->addWidget( deleteButton, 0, 6 );
354 BUTTONACT( modifyButton, modify() );
355 BUTTONACT( deleteButton, del() );
356 CONNECT( this, clicked( bool ), this, toggleEnabled( bool ) );
359 void VLMAWidget::modify()
361 parent->startModifyVLMItem( this );
364 void VLMAWidget::del()
366 parent->removeVLMItem( this );
369 //FIXME, remove me before release
370 void VLMAWidget::enterEvent( QEvent *event )
375 void VLMAWidget::toggleEnabled( bool b_enable )
377 VLMWrapper::EnableItem( name, b_enable );
383 VLMBroadcast::VLMBroadcast( QString _name, QString _input, QString _output,
384 bool _enabled, bool _looped, VLMDialog *_parent)
385 : VLMAWidget( _name, _input, _output,
386 _enabled, _parent, QVLM_Broadcast )
388 nameLabel->setText( "Broadcast: " + name );
389 type = QVLM_Broadcast;
392 playButton = new QToolButton;
393 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
394 objLayout->addWidget( playButton, 1, 0 );
397 QToolButton *stopButton = new QToolButton;
398 stopButton->setIcon( QIcon( QPixmap( ":/pixmaps/stop_16px.png" ) ) );
399 objLayout->addWidget( stopButton, 1, 1 );
401 loopButton = new QToolButton;
402 objLayout->addWidget( loopButton, 1, 2 );
404 BUTTONACT( playButton, togglePlayPause() );
405 BUTTONACT( stopButton, stop() );
406 BUTTONACT( loopButton, toggleLoop() );
411 void VLMBroadcast::update()
413 VLMWrapper::EditBroadcast( name, input, output, b_enabled, b_looped );
415 loopButton->setIcon( QIcon( QPixmap( ":/pixmaps/playlist_repeat_all.png" ) ) );
417 loopButton->setIcon( QIcon( QPixmap( ":/pixmaps/playlist_repeat_off.png" ) ) );
420 void VLMBroadcast::togglePlayPause()
422 if( b_playing = true )
424 VLMWrapper::ControlBroadcast( name, ControlBroadcastPause );
425 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/pause_16px.png" ) ) );
429 VLMWrapper::ControlBroadcast( name, ControlBroadcastPlay );
430 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
432 b_playing = !b_playing;
435 void VLMBroadcast::toggleLoop()
437 b_enabled = !b_enabled;
441 void VLMBroadcast::stop()
443 VLMWrapper::ControlBroadcast( name, ControlBroadcastStop );
444 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
450 VLMSchedule::VLMSchedule( QString name, QString input, QString output,
451 bool enabled, VLMDialog *parent)
452 : VLMAWidget( name, input, output, enabled, parent, QVLM_Schedule )
454 nameLabel->setText( "Schedule: " + name );
457 void VLMSchedule::update()
464 VLMVod::VLMVod( QString name, QString input, QString output,
465 bool enabled, QString _mux, VLMDialog *parent)
466 : VLMAWidget( name, input, output, enabled, parent, QVLM_VOD )
468 nameLabel->setText( "VOD:" + name );
471 muxLabel = new QLabel;
472 objLayout->addWidget( muxLabel, 1, 0 );
477 void VLMVod::update()
479 muxLabel->setText( mux );
480 VLMWrapper::EditVod( name, input, output, b_enabled, mux );
487 vlm_t * VLMWrapper::p_vlm = NULL;
489 VLMWrapper::VLMWrapper( vlm_t *_p_vlm )
494 VLMWrapper::~VLMWrapper()
497 void VLMWrapper::AddBroadcast( const QString name, QString input,
499 bool b_enabled, bool b_loop )
501 vlm_message_t *message;
502 QString command = "new \"" + name + "\" broadcast";
503 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
504 vlm_MessageDelete( message );
505 EditBroadcast( name, input, output, b_enabled, b_loop );
508 void VLMWrapper::EditBroadcast( const QString name, const QString input,
509 const QString output,
510 bool b_enabled, bool b_loop )
512 vlm_message_t *message;
515 command = "setup \"" + name + "\" inputdel all";
516 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
517 vlm_MessageDelete( message );
518 command = "setup \"" + name + "\" input \"" + input + "\"";
519 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
520 vlm_MessageDelete( message );
521 if( !output.isEmpty() )
523 command = "setup \"" + name + "\" output \"" + output + "\"";
524 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
525 vlm_MessageDelete( message );
529 command = "setup \"" + name + "\" enabled";
530 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
531 vlm_MessageDelete( message );
535 command = "setup \"" + name + "\" loop";
536 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
537 vlm_MessageDelete( message );
541 void VLMWrapper::EnableItem( const QString name, bool b_enable )
543 vlm_message_t *message;
544 QString command = "setup \"" + name + ( b_enable ? " enable" : " disable" );
547 void VLMWrapper::ControlBroadcast( const QString name, int BroadcastStatus,
550 vlm_message_t *message;
552 QString command = "control \"" + name;
553 switch( BroadcastStatus )
555 case ControlBroadcastPlay:
558 case ControlBroadcastPause:
561 case ControlBroadcastStop:
564 case ControlBroadcastSeek:
565 command += " seek" + seek;
568 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
569 vlm_MessageDelete( message );
572 void VLMWrapper::AddVod( const QString name, const QString input,
573 const QString output,
574 bool b_enabled, const QString mux )
576 vlm_message_t *message;
577 QString command = "new \"" + name + "\" vod";
578 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
579 vlm_MessageDelete( message );
580 EditVod( name, input, output, b_enabled, mux );
583 void VLMWrapper::EditVod( const QString name, const QString input,
584 const QString output,
588 vlm_message_t *message;
589 QString command = "setup \"" + name + "\" input \"" + input + "\"";
590 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
591 vlm_MessageDelete( message );
592 if( !output.isEmpty() )
594 command = "setup \"" + name + "\" output \"" + output + "\"";
595 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
596 vlm_MessageDelete( message );
600 command = "setup \"" + name + "\" enabled";
601 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
602 vlm_MessageDelete( message );
606 command = "setup \"" + name + "\" mux \"" + mux + "\"";
607 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
608 vlm_MessageDelete( message );