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"
27 #include "dialogs/open.hpp"
31 #include <QVBoxLayout>
32 #include <QStackedWidget>
35 #include <QGridLayout>
38 #include <QToolButton>
40 #include <QPushButton>
41 #include <QHBoxLayout>
42 #include <QDateTimeEdit>
44 #include <QHeaderView>
45 #include <QScrollArea>
47 static const char *psz_type[] = { "Broadcast", "Schedule", "VOD" };
49 VLMDialog *VLMDialog::instance = NULL;
51 VLMDialog::VLMDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
53 p_vlm = vlm_New( p_intf );
57 msg_Warn( p_intf, "Couldn't build VLM object ");
60 vlmWrapper = new VLMWrapper( p_vlm );
64 ui.saveButton->hide();
66 #define ADDMEDIATYPES( str, type ) ui.mediaType->addItem( qtr( str ), QVariant( type ) );
67 ADDMEDIATYPES( "Broadcast", QVLM_Broadcast );
68 ADDMEDIATYPES( "Schedule", QVLM_Schedule );
69 ADDMEDIATYPES( "Video On Demand ( VOD )", QVLM_VOD );
73 QGridLayout *schetimelayout = new QGridLayout( ui.schedBox );
74 QLabel *schetimelabel = new QLabel( qtr( "Hours/Minutes/Seconds:" ) );
75 schetimelayout->addWidget( schetimelabel, 0, 0 );
76 QLabel *schedatelabel = new QLabel( qtr( "Day Month Year:" ) );
77 schetimelayout->addWidget( schedatelabel, 1, 0 );
78 QLabel *scherepeatLabel = new QLabel( qtr( "Repeat:" ) );
79 schetimelayout->addWidget( scherepeatLabel, 2, 0 );
80 QLabel *scherepeatTimeLabel = new QLabel( qtr( "Repeat delay:" ) );
81 schetimelayout->addWidget( scherepeatTimeLabel, 3, 0 );
83 time = new QDateTimeEdit( QTime::currentTime() );
84 time->setAlignment( Qt::AlignRight );
85 schetimelayout->addWidget( time, 0, 1, 1, 3 );
87 date = new QDateTimeEdit( QDate::currentDate() );
88 date->setAlignment( Qt::AlignRight );
89 date->setCalendarPopup( true );
91 date->setDisplayFormat( "dd MM yyyy" );
93 date->setDisplayFormat( "dd MMMM yyyy" );
95 schetimelayout->addWidget( date, 1, 1, 1, 3 );
97 scherepeatnumber = new QSpinBox;
98 scherepeatnumber->setAlignment( Qt::AlignRight );
99 schetimelayout->addWidget( scherepeatnumber, 2, 1, 1, 3 );
101 repeatDays = new QSpinBox;
102 repeatDays->setAlignment( Qt::AlignRight );
103 schetimelayout->addWidget( repeatDays, 3, 1, 1, 1 );
104 repeatDays->setSuffix( qtr(" days") );
106 repeatTime = new QDateTimeEdit;
107 repeatTime->setAlignment( Qt::AlignRight );
108 schetimelayout->addWidget( repeatTime, 3, 2, 1, 2 );
109 repeatTime->setDisplayFormat( "hh:mm:ss" );
112 ui.vlmItemScroll->setFrameStyle( QFrame::NoFrame );
113 ui.vlmItemScroll->setWidgetResizable( true );
114 vlmItemWidget = new QWidget;
115 vlmItemLayout = new QVBoxLayout( vlmItemWidget );
116 vlmItemWidget->setLayout( vlmItemLayout );
117 ui.vlmItemScroll->setWidget( vlmItemWidget );
119 QSpacerItem *spacer =
120 new QSpacerItem( 10, 10, QSizePolicy::Minimum, QSizePolicy::Expanding);
121 vlmItemLayout->addItem( spacer );
123 QPushButton *closeButton = new QPushButton( qtr( "Close" ) );
124 ui.buttonBox->addButton( closeButton, QDialogButtonBox::AcceptRole );
126 showScheduleWidget( QVLM_Broadcast );
128 /* Connect the comboBox to show the right Widgets */
129 CONNECT( ui.mediaType, currentIndexChanged( int ),
130 this, showScheduleWidget( int ) );
132 /* Connect the leftList to show the good VLMItem */
133 CONNECT( ui.vlmListItem, currentRowChanged( int ),
134 this, selectVLMItem( int ) );
136 BUTTONACT( closeButton, close() );
137 BUTTONACT( ui.addButton, addVLMItem() );
138 BUTTONACT( ui.clearButton, clearWidgets() );
139 BUTTONACT( ui.saveButton, saveModifications() );
140 BUTTONACT( ui.inputButton, selectInput() );
143 VLMDialog::~VLMDialog()
145 /* FIXME :you have to destroy vlm here to close
146 * but we shouldn't destroy vlm here in case somebody else wants it */
151 void VLMDialog::showScheduleWidget( int i )
153 ui.schedBox->setVisible( ( i == QVLM_Schedule ) );
154 ui.loopBCast->setVisible( ( i == QVLM_Broadcast ) );
155 ui.vodBox->setVisible( ( i == QVLM_VOD ) );
158 void VLMDialog::selectVLMItem( int i )
160 ui.vlmItemScroll->ensureWidgetVisible( vlmItems.at( i ) );
163 bool VLMDialog::isNameGenuine( QString name )
165 for( int i = 0; i < vlmItems.size(); i++ )
167 if( vlmItems.at( i )->name == name )
173 void VLMDialog::addVLMItem()
175 int vlmItemCount = vlmItems.size();
177 /* Take the name and Check it */
178 QString name = ui.nameLedit->text();
179 if( name.isEmpty() || !isNameGenuine( name ) )
181 msg_Dbg( p_intf, "VLM Name is empty or already exists, I can't do it" );
185 int type = ui.mediaType->itemData( ui.mediaType->currentIndex() ).toInt();
187 QString typeShortName;
188 QString inputText = ui.inputLedit->text();
189 QString outputText = ui.outputLedit->text();
190 bool b_checked = ui.enableCheck->isChecked();
191 bool b_looped = ui.loopBCast->isChecked();
193 VLMAWidget * vlmAwidget;
198 typeShortName = "Bcast";
199 vlmAwidget = new VLMBroadcast( name, inputText, outputText,
200 b_checked, b_looped, this );
201 VLMWrapper::AddBroadcast( name, inputText, outputText, b_checked, b_looped );
204 typeShortName = "VOD";
205 vlmAwidget = new VLMVod( name, inputText, outputText,
206 b_checked, ui.muxLedit->text(), this );
207 VLMWrapper::AddVod( name, inputText, outputText, b_checked );
210 typeShortName = "Sched";
211 vlmAwidget = new VLMSchedule( name, inputText, outputText,
215 msg_Warn( p_intf, "Something bad happened" );
219 /* Add an Item of the Side List */
220 ui.vlmListItem->addItem( typeShortName + " : " + name );
221 ui.vlmListItem->setCurrentRow( vlmItemCount - 1 );
223 /* Add a new VLMAWidget on the main List */
225 vlmItemLayout->insertWidget( vlmItemCount, vlmAwidget );
226 vlmItems.append( vlmAwidget );
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 void VLMDialog::selectInput()
246 OpenDialog *o = OpenDialog::getInstance( this, p_intf, 0, true );
248 ui.inputLedit->setText( o->getMRL() );
251 /* Object Modification */
252 void VLMDialog::removeVLMItem( VLMAWidget *vlmObj )
254 int index = vlmItems.indexOf( vlmObj );
255 if( index < 0 ) return;
257 //FIXME, this is going to segfault if the focus in on the ListWidget
258 delete ui.vlmListItem->takeItem( index );
259 vlmItems.removeAt( index );
262 /* HERE BE DRAGONS VLM REQUEST */
265 void VLMDialog::startModifyVLMItem( VLMAWidget *vlmObj )
267 currentIndex = vlmItems.indexOf( vlmObj );
268 if( currentIndex < 0 ) return;
270 msg_Dbg( p_intf, "Type: %i", vlmObj->type );
271 ui.vlmListItem->setCurrentRow( currentIndex );
272 ui.nameLedit->setText( vlmObj->name );
273 ui.inputLedit->setText( vlmObj->input );
274 ui.outputLedit->setText( vlmObj->output );
275 ui.enableCheck->setChecked( vlmObj->b_enabled );
277 switch( vlmObj->type )
280 ui.loopBCast->setChecked( (qobject_cast<VLMBroadcast *>(vlmObj))->b_looped );
283 ui.muxLedit->setText( (qobject_cast<VLMVod *>(vlmObj))->mux );
286 //(qobject_cast<VLMSchedule *>)
290 ui.nameLedit->setReadOnly( true );
291 ui.addButton->hide();
292 ui.saveButton->show();
295 void VLMDialog::saveModifications()
297 VLMAWidget *vlmObj = vlmItems.at( currentIndex );
300 vlmObj->input = ui.inputLedit->text();
301 vlmObj->output = ui.outputLedit->text();
302 vlmObj->setChecked( ui.enableCheck->isChecked() );
303 vlmObj->b_enabled = ui.enableCheck->isChecked();
304 switch( vlmObj->type )
307 (qobject_cast<VLMBroadcast *>(vlmObj))->b_looped = ui.loopBCast->isChecked();
310 (qobject_cast<VLMVod *>(vlmObj))->mux = ui.muxLedit->text();
316 vlmObj->update(); /* It should call the correct function is VLMAWidget
317 is abstract, but I am far from sure... FIXME ? */
322 /*********************************
323 * VLMAWidget - Abstract class
324 ********************************/
326 VLMAWidget::VLMAWidget( QString _name,
332 : QGroupBox( _name, _parent )
338 b_enabled = _enabled;
341 setCheckable( true );
342 setChecked( b_enabled );
344 objLayout = new QGridLayout( this );
345 setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Maximum );
347 nameLabel = new QLabel;
348 objLayout->addWidget( nameLabel, 0, 0, 1, 4 );
350 /*QLabel *time = new QLabel( "--:--/--:--" );
351 objLayout->addWidget( time, 1, 3, 1, 2 );*/
353 QToolButton *modifyButton = new QToolButton;
354 modifyButton->setIcon( QIcon( QPixmap( ":/pixmaps/menus_settings_16px.png" ) ) );
355 objLayout->addWidget( modifyButton, 0, 5 );
357 QToolButton *deleteButton = new QToolButton;
358 deleteButton->setIcon( QIcon( QPixmap( ":/pixmaps/menus_quit_16px.png" ) ) );
359 objLayout->addWidget( deleteButton, 0, 6 );
361 BUTTONACT( modifyButton, modify() );
362 BUTTONACT( deleteButton, del() );
363 CONNECT( this, clicked( bool ), this, toggleEnabled( bool ) );
366 void VLMAWidget::modify()
368 parent->startModifyVLMItem( this );
371 void VLMAWidget::del()
373 parent->removeVLMItem( this );
376 //FIXME, remove me before release
377 void VLMAWidget::enterEvent( QEvent *event )
382 void VLMAWidget::toggleEnabled( bool b_enable )
384 VLMWrapper::EnableItem( name, b_enable );
390 VLMBroadcast::VLMBroadcast( QString _name, QString _input, QString _output,
391 bool _enabled, bool _looped, VLMDialog *_parent)
392 : VLMAWidget( _name, _input, _output,
393 _enabled, _parent, QVLM_Broadcast )
395 nameLabel->setText( "Broadcast: " + name );
396 type = QVLM_Broadcast;
399 playButton = new QToolButton;
400 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
401 objLayout->addWidget( playButton, 1, 0 );
404 QToolButton *stopButton = new QToolButton;
405 stopButton->setIcon( QIcon( QPixmap( ":/pixmaps/stop_16px.png" ) ) );
406 objLayout->addWidget( stopButton, 1, 1 );
408 loopButton = new QToolButton;
409 objLayout->addWidget( loopButton, 1, 2 );
411 BUTTONACT( playButton, togglePlayPause() );
412 BUTTONACT( stopButton, stop() );
413 BUTTONACT( loopButton, toggleLoop() );
418 void VLMBroadcast::update()
420 VLMWrapper::EditBroadcast( name, input, output, b_enabled, b_looped );
422 loopButton->setIcon( QIcon( QPixmap( ":/pixmaps/playlist_repeat_all.png" ) ) );
424 loopButton->setIcon( QIcon( QPixmap( ":/pixmaps/playlist_repeat_off.png" ) ) );
427 void VLMBroadcast::togglePlayPause()
429 if( b_playing = true )
431 VLMWrapper::ControlBroadcast( name, ControlBroadcastPause );
432 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/pause_16px.png" ) ) );
436 VLMWrapper::ControlBroadcast( name, ControlBroadcastPlay );
437 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
439 b_playing = !b_playing;
442 void VLMBroadcast::toggleLoop()
444 b_enabled = !b_enabled;
448 void VLMBroadcast::stop()
450 VLMWrapper::ControlBroadcast( name, ControlBroadcastStop );
451 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
457 VLMSchedule::VLMSchedule( QString name, QString input, QString output,
458 bool enabled, VLMDialog *parent)
459 : VLMAWidget( name, input, output, enabled, parent, QVLM_Schedule )
461 nameLabel->setText( "Schedule: " + name );
464 void VLMSchedule::update()
471 VLMVod::VLMVod( QString name, QString input, QString output,
472 bool enabled, QString _mux, VLMDialog *parent)
473 : VLMAWidget( name, input, output, enabled, parent, QVLM_VOD )
475 nameLabel->setText( "VOD:" + name );
478 muxLabel = new QLabel;
479 objLayout->addWidget( muxLabel, 1, 0 );
484 void VLMVod::update()
486 muxLabel->setText( mux );
487 VLMWrapper::EditVod( name, input, output, b_enabled, mux );
494 vlm_t * VLMWrapper::p_vlm = NULL;
496 VLMWrapper::VLMWrapper( vlm_t *_p_vlm )
501 VLMWrapper::~VLMWrapper()
504 void VLMWrapper::AddBroadcast( const QString name, QString input,
506 bool b_enabled, bool b_loop )
508 vlm_message_t *message;
509 QString command = "new \"" + name + "\" broadcast";
510 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
511 vlm_MessageDelete( message );
512 EditBroadcast( name, input, output, b_enabled, b_loop );
515 void VLMWrapper::EditBroadcast( const QString name, const QString input,
516 const QString output,
517 bool b_enabled, bool b_loop )
519 vlm_message_t *message;
522 command = "setup \"" + name + "\" inputdel all";
523 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
524 vlm_MessageDelete( message );
525 command = "setup \"" + name + "\" input \"" + input + "\"";
526 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
527 vlm_MessageDelete( message );
528 if( !output.isEmpty() )
530 command = "setup \"" + name + "\" output \"" + output + "\"";
531 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
532 vlm_MessageDelete( message );
536 command = "setup \"" + name + "\" enabled";
537 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
538 vlm_MessageDelete( message );
542 command = "setup \"" + name + "\" loop";
543 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
544 vlm_MessageDelete( message );
548 void VLMWrapper::EnableItem( const QString name, bool b_enable )
550 vlm_message_t *message;
551 QString command = "setup \"" + name + ( b_enable ? " enable" : " disable" );
554 void VLMWrapper::ControlBroadcast( const QString name, int BroadcastStatus,
557 vlm_message_t *message;
559 QString command = "control \"" + name;
560 switch( BroadcastStatus )
562 case ControlBroadcastPlay:
565 case ControlBroadcastPause:
568 case ControlBroadcastStop:
571 case ControlBroadcastSeek:
572 command += " seek" + seek;
575 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
576 vlm_MessageDelete( message );
579 void VLMWrapper::AddVod( const QString name, const QString input,
580 const QString output,
581 bool b_enabled, const QString mux )
583 vlm_message_t *message;
584 QString command = "new \"" + name + "\" vod";
585 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
586 vlm_MessageDelete( message );
587 EditVod( name, input, output, b_enabled, mux );
590 void VLMWrapper::EditVod( const QString name, const QString input,
591 const QString output,
595 vlm_message_t *message;
596 QString command = "setup \"" + name + "\" input \"" + input + "\"";
597 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
598 vlm_MessageDelete( message );
599 if( !output.isEmpty() )
601 command = "setup \"" + name + "\" output \"" + output + "\"";
602 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
603 vlm_MessageDelete( message );
607 command = "setup \"" + name + "\" enabled";
608 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
609 vlm_MessageDelete( message );
613 command = "setup \"" + name + "\" mux \"" + mux + "\"";
614 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
615 vlm_MessageDelete( message );