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 vlmWrapper = new VLMWrapper( p_intf );
52 if( !vlmWrapper->AttachVLM() )
54 msg_Warn( p_intf, "Couldn't build VLM object ");
60 ui.saveButton->hide();
62 #define ADDMEDIATYPES( str, type ) ui.mediaType->addItem( qtr( str ), QVariant( type ) );
63 ADDMEDIATYPES( "Broadcast", QVLM_Broadcast );
64 ADDMEDIATYPES( "Schedule", QVLM_Schedule );
65 ADDMEDIATYPES( "Video On Demand ( VOD )", QVLM_VOD );
69 QGridLayout *schetimelayout = new QGridLayout( ui.schedBox );
70 QLabel *schetimelabel = new QLabel( qtr( "Hours/Minutes/Seconds:" ) );
71 schetimelayout->addWidget( schetimelabel, 0, 0 );
72 QLabel *schedatelabel = new QLabel( qtr( "Day Month Year:" ) );
73 schetimelayout->addWidget( schedatelabel, 1, 0 );
74 QLabel *scherepeatLabel = new QLabel( qtr( "Repeat:" ) );
75 schetimelayout->addWidget( scherepeatLabel, 2, 0 );
76 QLabel *scherepeatTimeLabel = new QLabel( qtr( "Repeat delay:" ) );
77 schetimelayout->addWidget( scherepeatTimeLabel, 3, 0 );
79 time = new QDateTimeEdit( QTime::currentTime() );
80 time->setAlignment( Qt::AlignRight );
81 schetimelayout->addWidget( time, 0, 1, 1, 3 );
83 date = new QDateTimeEdit( QDate::currentDate() );
84 date->setAlignment( Qt::AlignRight );
85 date->setCalendarPopup( true );
87 date->setDisplayFormat( "dd MM yyyy" );
89 date->setDisplayFormat( "dd MMMM yyyy" );
91 schetimelayout->addWidget( date, 1, 1, 1, 3 );
93 scherepeatnumber = new QSpinBox;
94 scherepeatnumber->setAlignment( Qt::AlignRight );
95 schetimelayout->addWidget( scherepeatnumber, 2, 1, 1, 3 );
97 repeatDays = new QSpinBox;
98 repeatDays->setAlignment( Qt::AlignRight );
99 schetimelayout->addWidget( repeatDays, 3, 1, 1, 1 );
100 repeatDays->setSuffix( qtr(" days") );
102 repeatTime = new QDateTimeEdit;
103 repeatTime->setAlignment( Qt::AlignRight );
104 schetimelayout->addWidget( repeatTime, 3, 2, 1, 2 );
105 repeatTime->setDisplayFormat( "hh:mm:ss" );
108 ui.vlmItemScroll->setFrameStyle( QFrame::NoFrame );
109 ui.vlmItemScroll->setWidgetResizable( true );
110 vlmItemWidget = new QWidget;
111 vlmItemLayout = new QVBoxLayout( vlmItemWidget );
112 vlmItemWidget->setLayout( vlmItemLayout );
113 ui.vlmItemScroll->setWidget( vlmItemWidget );
115 QSpacerItem *spacer =
116 new QSpacerItem( 10, 10, QSizePolicy::Minimum, QSizePolicy::Expanding);
117 vlmItemLayout->addItem( spacer );
119 QPushButton *closeButton = new QPushButton( qtr( "Close" ) );
120 ui.buttonBox->addButton( closeButton, QDialogButtonBox::AcceptRole );
122 showScheduleWidget( QVLM_Broadcast );
124 /* Connect the comboBox to show the right Widgets */
125 CONNECT( ui.mediaType, currentIndexChanged( int ),
126 this, showScheduleWidget( int ) );
128 /* Connect the leftList to show the good VLMItem */
129 CONNECT( ui.vlmListItem, currentRowChanged( int ),
130 this, selectVLMItem( int ) );
132 BUTTONACT( closeButton, close() );
133 BUTTONACT( ui.addButton, addVLMItem() );
134 BUTTONACT( ui.clearButton, clearWidgets() );
135 BUTTONACT( ui.saveButton, saveModifications() );
138 VLMDialog::~VLMDialog()
143 void VLMDialog::showScheduleWidget( int i )
145 ui.schedBox->setVisible( ( i == QVLM_Schedule ) );
146 ui.loopBCast->setVisible( ( i == QVLM_Broadcast ) );
147 ui.vodBox->setVisible( ( i == QVLM_VOD ) );
150 void VLMDialog::selectVLMItem( int i )
152 ui.vlmItemScroll->ensureWidgetVisible( vlmItems.at( i ) );
155 bool VLMDialog::isNameGenuine( QString name )
157 for( int i = 0; i < vlmItems.size(); i++ )
159 if( vlmItems.at( i )->name == name )
165 void VLMDialog::addVLMItem()
167 int vlmItemCount = vlmItems.size();
169 /* Take the name and Check it */
170 QString name = ui.nameLedit->text();
171 if( name.isEmpty() || !isNameGenuine( name ) )
173 msg_Dbg( p_intf, "VLM Name is empty or already exists, I can't do it" );
177 int type = ui.mediaType->itemData( ui.mediaType->currentIndex() ).toInt();
179 QString typeShortName;
180 QString inputText = ui.inputLedit->text();
181 QString outputText = ui.outputLedit->text();
182 bool b_checked = ui.enableCheck->isChecked();
183 bool b_looped = ui.loopBCast->isChecked();
185 VLMAWidget * vlmAwidget;
190 typeShortName = "Bcast";
191 vlmAwidget = new VLMBroadcast( name, inputText, outputText,
192 b_checked, b_looped, this );
193 VLMWrapper::AddBroadcast( vlmWrapper->GetVLM(), name, inputText, outputText, b_checked, b_looped );
196 typeShortName = "VOD";
197 vlmAwidget = new VLMVod( name, inputText, outputText,
198 b_checked, ui.muxLedit->text(), this );
199 VLMWrapper::AddVod( vlmWrapper->GetVLM(), name, inputText, outputText, b_checked );
202 typeShortName = "Sched";
203 vlmAwidget = new VLMSchedule( name, inputText, outputText,
207 msg_Warn( p_intf, "Something bad happened" );
211 /* Add an Item of the Side List */
212 ui.vlmListItem->addItem( typeShortName + " : " + name );
213 ui.vlmListItem->setCurrentRow( vlmItemCount - 1 );
215 /* Add a new VLMAWidget on the main List */
217 vlmItemLayout->insertWidget( vlmItemCount, vlmAwidget );
218 vlmItems.append( vlmAwidget );
220 /* HERE BE DRAGONS VLM REQUEST */
223 void VLMDialog::clearWidgets()
225 ui.nameLedit->clear();
226 ui.inputLedit->clear();
227 ui.outputLedit->clear();
228 time->setTime( QTime::currentTime() );
229 date->setDate( QDate::currentDate() );
230 ui.enableCheck->setChecked( true );
231 ui.nameLedit->setReadOnly( false );
232 ui.loopBCast->setChecked( false );
233 ui.muxLedit->clear();
234 ui.saveButton->hide();
235 ui.addButton->show();
238 /* Object Modification */
239 void VLMDialog::removeVLMItem( VLMAWidget *vlmObj )
241 int index = vlmItems.indexOf( vlmObj );
242 if( index < 0 ) return;
244 //FIXME, this is going to segfault if the focus in on the ListWidget
245 delete ui.vlmListItem->takeItem( index );
246 vlmItems.removeAt( index );
249 /* HERE BE DRAGONS VLM REQUEST */
252 void VLMDialog::startModifyVLMItem( VLMAWidget *vlmObj )
254 currentIndex = vlmItems.indexOf( vlmObj );
255 if( currentIndex < 0 ) return;
257 msg_Dbg( p_intf, "Type: %i", vlmObj->type );
258 ui.vlmListItem->setCurrentRow( currentIndex );
259 ui.nameLedit->setText( vlmObj->name );
260 ui.inputLedit->setText( vlmObj->input );
261 ui.outputLedit->setText( vlmObj->output );
262 ui.enableCheck->setChecked( vlmObj->b_enabled );
264 switch( vlmObj->type )
267 ui.loopBCast->setChecked( (qobject_cast<VLMBroadcast *>(vlmObj))->b_looped );
270 ui.muxLedit->setText( (qobject_cast<VLMVod *>(vlmObj))->mux );
273 //(qobject_cast<VLMSchedule *>)
277 ui.nameLedit->setReadOnly( true );
278 ui.addButton->hide();
279 ui.saveButton->show();
282 void VLMDialog::saveModifications()
284 VLMAWidget *vlmObj = vlmItems.at( currentIndex );
287 vlmObj->input = ui.inputLedit->text();
288 vlmObj->output = ui.outputLedit->text();
289 vlmObj->setChecked( ui.enableCheck->isChecked() );
290 vlmObj->b_enabled = ui.enableCheck->isChecked();
291 switch( vlmObj->type )
294 (qobject_cast<VLMBroadcast *>(vlmObj))->b_looped = ui.loopBCast->isChecked();
297 (qobject_cast<VLMVod *>(vlmObj))->mux = ui.muxLedit->text();
303 vlmObj->update(); /* It should call the correct function is VLMAWidget
304 is abstract, but I am far from sure... FIXME ? */
309 /*********************************
310 * VLMAWidget - Abstract class
311 ********************************/
313 VLMAWidget::VLMAWidget( QString _name,
319 : QGroupBox( _name, _parent )
325 b_enabled = _enabled;
328 setCheckable( true );
329 setChecked( b_enabled );
331 objLayout = new QGridLayout( this );
332 setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Maximum );
334 nameLabel = new QLabel;
335 objLayout->addWidget( nameLabel, 0, 0, 1, 4 );
337 /*QLabel *time = new QLabel( "--:--/--:--" );
338 objLayout->addWidget( time, 1, 3, 1, 2 );*/
340 QToolButton *modifyButton = new QToolButton;
341 modifyButton->setIcon( QIcon( QPixmap( ":/pixmaps/menus_settings_16px.png" ) ) );
342 objLayout->addWidget( modifyButton, 0, 5 );
344 QToolButton *deleteButton = new QToolButton;
345 deleteButton->setIcon( QIcon( QPixmap( ":/pixmaps/menus_quit_16px.png" ) ) );
346 objLayout->addWidget( deleteButton, 0, 6 );
348 BUTTONACT( modifyButton, modify() );
349 BUTTONACT( deleteButton, del() );
352 void VLMAWidget::modify()
354 parent->startModifyVLMItem( this );
357 void VLMAWidget::del()
359 parent->removeVLMItem( this );
362 //FIXME, remove me before release
363 void VLMAWidget::enterEvent( QEvent *event )
371 VLMBroadcast::VLMBroadcast( QString _name, QString _input, QString _output,
372 bool _enabled, bool _looped, VLMDialog *_parent)
373 : VLMAWidget( _name, _input, _output,
374 _enabled, _parent, QVLM_Broadcast )
376 nameLabel->setText( "Broadcast: " + name );
377 type = QVLM_Broadcast;
380 playButton = new QToolButton;
381 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
382 objLayout->addWidget( playButton, 1, 0 );
385 QToolButton *stopButton = new QToolButton;
386 stopButton->setIcon( QIcon( QPixmap( ":/pixmaps/stop_16px.png" ) ) );
387 objLayout->addWidget( stopButton, 1, 1 );
389 loopButton = new QToolButton;
390 objLayout->addWidget( loopButton, 1, 2 );
392 BUTTONACT( playButton, togglePlayPause() );
393 BUTTONACT( stopButton, stop() );
394 BUTTONACT( loopButton, toggleLoop() );
399 void VLMBroadcast::update()
401 VLMWrapper::EditBroadcast( THEVLM, name, input, output, b_enabled, b_looped );
403 loopButton->setIcon( QIcon( QPixmap( ":/pixmaps/playlist_repeat_all.png" ) ) );
405 loopButton->setIcon( QIcon( QPixmap( ":/pixmaps/playlist_repeat_off.png" ) ) );
408 void VLMBroadcast::togglePlayPause()
410 if( b_playing = true )
412 VLMWrapper::ControlBroadcast( THEVLM, name, ControlBroadcastPause );
413 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/pause_16px.png" ) ) );
417 VLMWrapper::ControlBroadcast( THEVLM, name, ControlBroadcastPlay );
418 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
420 b_playing = !b_playing;
423 void VLMBroadcast::toggleLoop()
425 b_enabled = !b_enabled;
429 void VLMBroadcast::stop()
431 VLMWrapper::ControlBroadcast( THEVLM, name, ControlBroadcastStop );
432 playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
438 VLMSchedule::VLMSchedule( QString name, QString input, QString output,
439 bool enabled, VLMDialog *parent)
440 : VLMAWidget( name, input, output, enabled, parent, QVLM_Schedule )
442 nameLabel->setText( "Schedule: " + name );
445 void VLMSchedule::update()
452 VLMVod::VLMVod( QString name, QString input, QString output,
453 bool enabled, QString _mux, VLMDialog *parent)
454 : VLMAWidget( name, input, output, enabled, parent, QVLM_VOD )
456 nameLabel->setText( "VOD:" + name );
459 muxLabel = new QLabel;
460 objLayout->addWidget( muxLabel, 1, 0 );
465 void VLMVod::update()
467 muxLabel->setText( mux );
468 VLMWrapper::EditVod( THEVLM, name, input, output, b_enabled, mux );
475 VLMWrapper::VLMWrapper( intf_thread_t *_p_intf )
478 p_vlm = vlm_New( p_intf );
481 VLMWrapper::~VLMWrapper()
483 /* FIXME :you have to destroy vlm here to close
484 * but we shouldn't destroy vlm here in case somebody else wants it */
489 bool VLMWrapper::AttachVLM()
491 p_vlm = vlm_New( p_intf );
492 return (p_vlm ? true: false );
495 void VLMWrapper::AddBroadcast( vlm_t *p_vlm, const QString name, QString input,
497 bool b_enabled, bool b_loop )
499 vlm_message_t *message;
500 QString command = "new \"" + name + "\" broadcast";
501 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
502 vlm_MessageDelete( message );
503 EditBroadcast( p_vlm, name, input, output, b_enabled, b_loop );
506 void VLMWrapper::EditBroadcast( vlm_t *p_vlm, const QString name, const QString input,
507 const QString output,
508 bool b_enabled, bool b_loop )
510 vlm_message_t *message;
513 command = "setup \"" + name + "\" inputdel all";
514 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
515 vlm_MessageDelete( message );
516 command = "setup \"" + name + "\" input \"" + input + "\"";
517 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
518 vlm_MessageDelete( message );
519 if( !output.isEmpty() )
521 command = "setup \"" + name + "\" output \"" + output + "\"";
522 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
523 vlm_MessageDelete( message );
527 command = "setup \"" + name + "\" enabled";
528 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
529 vlm_MessageDelete( message );
533 command = "setup \"" + name + "\" loop";
534 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
535 vlm_MessageDelete( message );
539 void VLMWrapper::ControlBroadcast( vlm_t *p_vlm, const QString name, int BroadcastStatus,
542 vlm_message_t *message;
544 QString command = "setup \"" + name;
545 switch( BroadcastStatus )
547 case ControlBroadcastPlay:
550 case ControlBroadcastPause:
553 case ControlBroadcastStop:
556 case ControlBroadcastSeek:
557 command += " seek" + seek;
560 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
561 vlm_MessageDelete( message );
564 void VLMWrapper::AddVod( vlm_t *p_vlm, const QString name, const QString input,
565 const QString output,
566 bool b_enabled, const QString mux )
568 vlm_message_t *message;
569 QString command = "new \"" + name + "\" vod";
570 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
571 vlm_MessageDelete( message );
572 EditVod( p_vlm, name, input, output, b_enabled, mux );
575 void VLMWrapper::EditVod( vlm_t *p_vlm, const QString name, const QString input,
576 const QString output,
580 vlm_message_t *message;
581 QString command = "setup \"" + name + "\" input \"" + input + "\"";
582 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
583 vlm_MessageDelete( message );
584 if( !output.isEmpty() )
586 command = "setup \"" + name + "\" output \"" + output + "\"";
587 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
588 vlm_MessageDelete( message );
592 command = "setup \"" + name + "\" enabled";
593 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
594 vlm_MessageDelete( message );
598 command = "setup \"" + name + "\" mux \"" + mux + "\"";
599 vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
600 vlm_MessageDelete( message );