]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/vlm.cpp
c0734d7f8fbf49322b5d89b2128f24d45d2d43bd
[vlc] / modules / gui / qt4 / dialogs / vlm.cpp
1 /*****************************************************************************
2  * vlm.cpp : VLM Management
3  ****************************************************************************
4  * Copyright © 2008 the VideoLAN team
5  * $Id$
6  *
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>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "dialogs/vlm.hpp"
31
32 #ifdef ENABLE_VLM
33 #include "dialogs/open.hpp"
34 #include "dialogs/sout.hpp"
35 #include "util/qt_dirs.hpp"
36
37 #include <QString>
38 #include <QComboBox>
39 #include <QVBoxLayout>
40 #include <QStackedWidget>
41 #include <QLabel>
42 #include <QWidget>
43 #include <QGridLayout>
44 #include <QLineEdit>
45 #include <QCheckBox>
46 #include <QToolButton>
47 #include <QGroupBox>
48 #include <QPushButton>
49 #include <QHBoxLayout>
50 #include <QDateTimeEdit>
51 #include <QDateTime>
52 #include <QSpinBox>
53 #include <QHeaderView>
54 #include <QScrollArea>
55 #include <QFileDialog>
56
57
58 VLMDialog *VLMDialog::instance = NULL;
59
60 VLMDialog::VLMDialog( QWidget *parent, intf_thread_t *_p_intf ) : QVLCDialog( parent, _p_intf )
61 {
62     p_vlm = vlm_New( p_intf );
63
64     if( !p_vlm )
65     {
66         msg_Warn( p_intf, "Couldn't build VLM object ");
67         return;
68     }
69     vlmWrapper = new VLMWrapper( p_vlm );
70
71     // UI stuff
72     ui.setupUi( this );
73     ui.saveButton->hide();
74
75 #define ADDMEDIATYPES( str, type ) ui.mediaType->addItem( qtr( str ), QVariant( type ) );
76     ADDMEDIATYPES( N_("Broadcast"), QVLM_Broadcast );
77     ADDMEDIATYPES( N_("Schedule"), QVLM_Schedule );
78     ADDMEDIATYPES( N_("Video On Demand ( VOD )"), QVLM_VOD );
79 #undef ADDMEDIATYPES
80
81     /* Schedule Stuffs */
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 );
91
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 );
96
97     date = new QDateTimeEdit( QDate::currentDate() );
98     date->setAlignment( Qt::AlignRight );
99     date->setCalendarPopup( true );
100 #ifdef WIN32
101     date->setDisplayFormat( "dd MM yyyy" );
102 #else
103     date->setDisplayFormat( "dd MMMM yyyy" );
104 #endif
105     schetimelayout->addWidget( date, 1, 1, 1, 3 );
106
107     scherepeatnumber = new QSpinBox;
108     scherepeatnumber->setAlignment( Qt::AlignRight );
109     schetimelayout->addWidget( scherepeatnumber, 2, 1, 1, 3 );
110
111     repeatDays = new QSpinBox;
112     repeatDays->setAlignment( Qt::AlignRight );
113     schetimelayout->addWidget( repeatDays, 3, 1, 1, 1 );
114     repeatDays->setSuffix( qtr(" days") );
115
116     repeatTime = new QDateTimeEdit;
117     repeatTime->setAlignment( Qt::AlignRight );
118     schetimelayout->addWidget( repeatTime, 3, 2, 1, 2 );
119     repeatTime->setDisplayFormat( "hh:mm:ss" );
120
121     /* scrollArea */
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 );
128
129     QSpacerItem *spacer =
130         new QSpacerItem( 10, 10, QSizePolicy::Minimum, QSizePolicy::Expanding);
131     vlmItemLayout->addItem( spacer );
132
133     QPushButton *importButton = new QPushButton( qtr( "I&mport" ) );
134     ui.buttonBox->addButton( importButton, QDialogButtonBox::ActionRole );
135
136     QPushButton *exportButton = new QPushButton( qtr( "E&xport" ) );
137     ui.buttonBox->addButton( exportButton, QDialogButtonBox::ActionRole );
138
139     QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
140     ui.buttonBox->addButton( closeButton, QDialogButtonBox::AcceptRole );
141
142
143     showScheduleWidget( QVLM_Broadcast );
144
145     /* Connect the comboBox to show the right Widgets */
146     CONNECT( ui.mediaType, currentIndexChanged( int ),
147              this, showScheduleWidget( int ) );
148
149     /* Connect the leftList to show the good VLMItem */
150     CONNECT( ui.vlmListItem, currentRowChanged( int ),
151              this, selectVLMItem( int ) );
152
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() );
161     //readSettings( "VLM", QSize( 700, 500 ) );
162 }
163
164 VLMDialog::~VLMDialog()
165 {
166     delete vlmWrapper;
167
168     //writeSettings( "VLM" );
169    /* TODO :you have to destroy vlm here to close
170     * but we shouldn't destroy vlm here in case somebody else wants it */
171     if( p_vlm )
172     {
173         vlm_Delete( p_vlm );
174     }
175 }
176
177 void VLMDialog::showScheduleWidget( int i )
178 {
179     ui.schedBox->setVisible( ( i == QVLM_Schedule ) );
180     ui.loopBCast->setVisible( ( i == QVLM_Broadcast ) );
181     ui.vodBox->setVisible( ( i == QVLM_VOD ) );
182 }
183
184 void VLMDialog::selectVLMItem( int i )
185 {
186     if( i >= 0 )
187         ui.vlmItemScroll->ensureWidgetVisible( vlmItems.at( i ) );
188 }
189
190 bool VLMDialog::isNameGenuine( const QString& name )
191 {
192     for( int i = 0; i < vlmItems.size(); i++ )
193     {
194         if( vlmItems.at( i )->name == name )
195             return false;
196     }
197     return true;
198 }
199
200 void VLMDialog::addVLMItem()
201 {
202     int vlmItemCount = vlmItems.size();
203
204     /* Take the name and Check it */
205     QString name = ui.nameLedit->text();
206     if( name.isEmpty() || !isNameGenuine( name ) )
207     {
208         msg_Err( p_intf, "VLM Name is empty or already exists, I can't do it" );
209         return;
210     }
211
212     int type = ui.mediaType->itemData( ui.mediaType->currentIndex() ).toInt();
213
214     QString typeShortName;
215     QString inputText = ui.inputLedit->text();
216     QString outputText = ui.outputLedit->text();
217     bool b_checked = ui.enableCheck->isChecked();
218     bool b_looped = ui.loopBCast->isChecked();
219     QDateTime schetime = time->dateTime();
220     QDateTime schedate = date->dateTime();
221     int repeatnum = scherepeatnumber->value();
222     int repeatdays = repeatDays->value();
223     VLMAWidget * vlmAwidget;
224     outputText.remove( ":sout=" );
225
226     switch( type )
227     {
228     case QVLM_Broadcast:
229         typeShortName = "Bcast";
230         vlmAwidget = new VLMBroadcast( name, inputText, outputText,
231                                        b_checked, b_looped, this );
232         VLMWrapper::AddBroadcast( name, inputText, outputText, b_checked,
233                                   b_looped );
234     break;
235     case QVLM_VOD:
236         typeShortName = "VOD";
237         vlmAwidget = new VLMVod( name, inputText, outputText,
238                                  b_checked, ui.muxLedit->text(), this );
239         VLMWrapper::AddVod( name, inputText, outputText, b_checked );
240         break;
241     case QVLM_Schedule:
242         typeShortName = "Sched";
243         vlmAwidget = new VLMSchedule( name, inputText, outputText,
244                                       schetime, schedate, repeatnum,
245                                       repeatdays, b_checked, this );
246         VLMWrapper::AddSchedule( name, inputText, outputText, schetime,
247                                  schedate, repeatnum, repeatdays, b_checked);
248         break;
249     default:
250         msg_Warn( p_intf, "Something bad happened" );
251         return;
252     }
253
254     /* Add an Item of the Side List */
255     ui.vlmListItem->addItem( typeShortName + " : " + name );
256     ui.vlmListItem->setCurrentRow( vlmItemCount - 1 );
257
258     /* Add a new VLMAWidget on the main List */
259
260     vlmItemLayout->insertWidget( vlmItemCount, vlmAwidget );
261     vlmItems.append( vlmAwidget );
262     clearWidgets();
263 }
264
265 /* TODO : VOD are not exported to the file */
266 bool VLMDialog::exportVLMConf()
267 {
268     QString saveVLMConfFileName = QFileDialog::getSaveFileName( this,
269                                         qtr( "Save VLM configuration as..." ),
270                                         QVLCUserDir( VLC_DOCUMENTS_DIR ),
271                                         qtr( "VLM conf (*.vlm);;All (*)" ) );
272
273     if( !saveVLMConfFileName.isEmpty() )
274     {
275         vlm_message_t *message;
276         QString command = "save \"" + saveVLMConfFileName + "\"";
277         vlm_ExecuteCommand( p_vlm , qtu( command ) , &message );
278         vlm_MessageDelete( message );
279         return true;
280     }
281
282     return false;
283 }
284
285 void VLMDialog::mediasPopulator()
286 {
287     if( p_vlm )
288     {
289         int i_nMedias;
290         QString typeShortName;
291         int vlmItemCount;
292         vlm_media_t ***ppp_dsc = (vlm_media_t ***)malloc( sizeof( vlm_media_t ) );
293
294         /* Get medias informations and numbers */
295         vlm_Control( p_vlm, VLM_GET_MEDIAS, ppp_dsc, &i_nMedias );
296
297         /* Loop on all of them */
298         for( int i = 0; i < i_nMedias; i++ )
299         {
300             VLMAWidget * vlmAwidget;
301             vlmItemCount = vlmItems.size();
302
303             QString mediaName = qfu( (*ppp_dsc)[i]->psz_name );
304             /* It may have several inputs, we take the first one by default
305                  - an evolution will be to manage these inputs in the gui */
306             QString inputText = qfu( (*ppp_dsc)[i]->ppsz_input[0] );
307
308             QString outputText = qfu( (*ppp_dsc)[i]->psz_output );
309
310             /* Schedule media is a quite especial, maybe there is another way to grab informations */
311             if( (*ppp_dsc)[i]->b_vod )
312             {
313                 typeShortName = "VOD";
314                 QString mux = qfu( (*ppp_dsc)[i]->vod.psz_mux );
315                 vlmAwidget = new VLMVod( mediaName, inputText, outputText,
316                                     (*ppp_dsc)[i]->b_enabled, mux, this );
317             }
318             else
319             {
320                 typeShortName = "Bcast";
321                 vlmAwidget = new VLMBroadcast( mediaName, inputText, outputText,
322                                   (*ppp_dsc)[i]->b_enabled, (*ppp_dsc)[i]->broadcast.b_loop, this );
323             }
324             /* Add an Item of the Side List */
325             ui.vlmListItem->addItem( typeShortName + " : " + mediaName );
326             ui.vlmListItem->setCurrentRow( vlmItemCount - 1 );
327
328             /* Add a new VLMAWidget on the main List */
329             vlmItemLayout->insertWidget( vlmItemCount, vlmAwidget );
330             vlmItems.append( vlmAwidget );
331             clearWidgets();
332         }
333         free( ppp_dsc );
334     }
335 }
336
337 bool VLMDialog::importVLMConf()
338 {
339     QString openVLMConfFileName = toNativeSeparators(
340             QFileDialog::getOpenFileName(
341             this, qtr( "Open VLM configuration..." ),
342             QVLCUserDir( VLC_DOCUMENTS_DIR ),
343             qtr( "VLM conf (*.vlm);;All (*)" ) ) );
344
345     if( !openVLMConfFileName.isEmpty() )
346     {
347         vlm_message_t *message;
348         int status;
349         QString command = "load \"" + openVLMConfFileName + "\"";
350         status = vlm_ExecuteCommand( p_vlm, qtu( command ) , &message );
351         vlm_MessageDelete( message );
352         if( status == 0 )
353         {
354             mediasPopulator();
355         }
356         else
357         {
358             msg_Warn( p_intf, "Failed to import vlm configuration file : %s", qtu( command ) );
359             return false;
360         }
361         return true;
362     }
363     return false;
364 }
365
366 void VLMDialog::clearWidgets()
367 {
368     ui.nameLedit->clear();
369     ui.inputLedit->clear();
370     ui.outputLedit->clear();
371     time->setTime( QTime::currentTime() );
372     date->setDate( QDate::currentDate() );
373     ui.enableCheck->setChecked( true );
374     ui.nameLedit->setReadOnly( false );
375     ui.loopBCast->setChecked( false );
376     ui.muxLedit->clear();
377     ui.saveButton->hide();
378     ui.addButton->show();
379 }
380
381 void VLMDialog::selectInput()
382 {
383     OpenDialog *o = OpenDialog::getInstance( this, p_intf, false, SELECT, true );
384     o->exec();
385     ui.inputLedit->setText( o->getMRL() );
386 }
387
388 void VLMDialog::selectOutput()
389 {
390     SoutDialog *s = new SoutDialog( this, p_intf );
391     if( s->exec() == QDialog::Accepted )
392         ui.outputLedit->setText( s->getMrl() );
393 }
394
395 /* Object Modification */
396 void VLMDialog::removeVLMItem( VLMAWidget *vlmObj )
397 {
398     int index = vlmItems.indexOf( vlmObj );
399     if( index < 0 ) return;
400     delete ui.vlmListItem->takeItem( index );
401     vlmItems.removeAt( index );
402     delete vlmObj;
403
404     /* HERE BE DRAGONS VLM REQUEST */
405 }
406
407 void VLMDialog::startModifyVLMItem( VLMAWidget *vlmObj )
408 {
409     currentIndex = vlmItems.indexOf( vlmObj );
410     if( currentIndex < 0 ) return;
411
412     ui.vlmListItem->setCurrentRow( currentIndex );
413     ui.nameLedit->setText( vlmObj->name );
414     ui.inputLedit->setText( vlmObj->input );
415     ui.outputLedit->setText( vlmObj->output );
416     ui.enableCheck->setChecked( vlmObj->b_enabled );
417
418     switch( vlmObj->type )
419     {
420     case QVLM_Broadcast:
421         ui.loopBCast->setChecked( (qobject_cast<VLMBroadcast *>(vlmObj))->b_looped );
422         break;
423     case QVLM_VOD:
424         ui.muxLedit->setText( (qobject_cast<VLMVod *>(vlmObj))->mux );
425         break;
426     case QVLM_Schedule:
427         time->setDateTime( ( qobject_cast<VLMSchedule *>(vlmObj))->schetime );
428         date->setDateTime( ( qobject_cast<VLMSchedule *>(vlmObj))->schedate );
429         break;
430     }
431
432     ui.nameLedit->setReadOnly( true );
433     ui.addButton->hide();
434     ui.saveButton->show();
435 }
436
437 void VLMDialog::saveModifications()
438 {
439     VLMAWidget *vlmObj = vlmItems.at( currentIndex );
440     if( vlmObj )
441     {
442         vlmObj->input = ui.inputLedit->text();
443         vlmObj->output = ui.outputLedit->text().remove( ":sout=" );
444         vlmObj->setChecked( ui.enableCheck->isChecked() );
445         vlmObj->b_enabled = ui.enableCheck->isChecked();
446         switch( vlmObj->type )
447         {
448         case QVLM_Broadcast:
449             (qobject_cast<VLMBroadcast *>(vlmObj))->b_looped = ui.loopBCast->isChecked();
450             break;
451         case QVLM_VOD:
452             (qobject_cast<VLMVod *>(vlmObj))->mux = ui.muxLedit->text();
453             break;
454         case QVLM_Schedule:
455             (qobject_cast<VLMSchedule *>(vlmObj))->schetime = time->dateTime();
456             (qobject_cast<VLMSchedule *>(vlmObj))->schedate = date->dateTime();
457             (qobject_cast<VLMSchedule *>(vlmObj))->rNumber = scherepeatnumber->value();
458             (qobject_cast<VLMSchedule *>(vlmObj))->rDays = repeatDays->value();
459             break;
460            //           vlmObj->
461         }
462         vlmObj->update();
463     }
464     clearWidgets();
465 }
466
467 /*********************************
468  * VLMAWidget - Abstract class
469  ********************************/
470
471 VLMAWidget::VLMAWidget( const QString& _name,
472                         const QString& _input,
473                         const QString& _output,
474                         bool _enabled,
475                         VLMDialog *_parent,
476                         int _type )
477                       : QGroupBox( _name, _parent )
478 {
479     parent = _parent;
480     name = _name;
481     input = _input;
482     output = _output;
483     b_enabled = _enabled;
484     type = _type;
485
486     setCheckable( true );
487     setChecked( b_enabled );
488
489     objLayout = new QGridLayout( this );
490     setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Maximum );
491
492     nameLabel = new QLabel;
493     objLayout->addWidget( nameLabel, 0, 0, 1, 4 );
494
495     /*QLabel *time = new QLabel( "--:--/--:--" );
496     objLayout->addWidget( time, 1, 3, 1, 2 );*/
497
498     QToolButton *modifyButton = new QToolButton;
499     modifyButton->setIcon( QIcon( ":/menu/settings" ) );
500     objLayout->addWidget( modifyButton, 0, 5 );
501
502     QToolButton *deleteButton = new QToolButton;
503     deleteButton->setIcon( QIcon( ":/menu/quit" ) );
504     objLayout->addWidget( deleteButton, 0, 6 );
505
506     BUTTONACT( modifyButton, modify() );
507     BUTTONACT( deleteButton, del() );
508     CONNECT( this, clicked( bool ), this, toggleEnabled( bool ) );
509 }
510
511 void VLMAWidget::modify()
512 {
513     parent->startModifyVLMItem( this );
514 }
515
516 void VLMAWidget::del()
517 {
518     parent->removeVLMItem( this );
519 }
520
521 void VLMAWidget::toggleEnabled( bool b_enable )
522 {
523     VLMWrapper::EnableItem( name, b_enable );
524 }
525
526 /****************
527  * VLMBroadcast
528  ****************/
529 VLMBroadcast::VLMBroadcast( const QString& _name, const QString& _input,
530                             const QString& _output, bool _enabled,
531                             bool _looped, VLMDialog *_parent )
532                           : VLMAWidget( _name, _input, _output,
533                                         _enabled, _parent, QVLM_Broadcast )
534 {
535     nameLabel->setText( qtr("Broadcast: ") + name );
536     type = QVLM_Broadcast;
537     b_looped = _looped;
538
539     playButton = new QToolButton;
540     playButton->setIcon( QIcon( ":/menu/play" ) );
541     objLayout->addWidget( playButton, 1, 0 );
542     b_playing = true;
543
544     QToolButton *stopButton = new QToolButton;
545     stopButton->setIcon( QIcon( ":/toolbar/stop_b" ) );
546     objLayout->addWidget( stopButton, 1, 1 );
547
548     loopButton = new QToolButton;
549     objLayout->addWidget( loopButton, 1, 2 );
550
551     BUTTONACT( playButton, togglePlayPause() );
552     BUTTONACT( stopButton, stop() );
553     BUTTONACT( loopButton, toggleLoop() );
554
555     update();
556 }
557
558 void VLMBroadcast::update()
559 {
560     VLMWrapper::EditBroadcast( name, input, output, b_enabled, b_looped );
561     if( b_looped )
562         loopButton->setIcon( QIcon( ":/buttons/playlist/repeat_all" ) );
563     else
564         loopButton->setIcon( QIcon( ":/buttons/playlist/repeat_off" ) );
565 }
566
567 void VLMBroadcast::togglePlayPause()
568 {
569     if( b_playing )
570     {
571         VLMWrapper::ControlBroadcast( name, ControlBroadcastPause );
572         playButton->setIcon( QIcon( ":/menu/pause" ) );
573     }
574     else
575     {
576         VLMWrapper::ControlBroadcast( name, ControlBroadcastPlay );
577         playButton->setIcon( QIcon( ":/menu/play" ) );
578     }
579     b_playing = !b_playing;
580 }
581
582 void VLMBroadcast::toggleLoop()
583 {
584     b_enabled = !b_enabled;
585     update();
586 }
587
588 void VLMBroadcast::stop()
589 {
590     VLMWrapper::ControlBroadcast( name, ControlBroadcastStop );
591     playButton->setIcon( QIcon( ":/menu/play" ) );
592 }
593
594 /****************
595  * VLMSchedule
596  ****************/
597 VLMSchedule::VLMSchedule( const QString& name, const QString& input,
598                           const QString& output, QDateTime _schetime,
599                           QDateTime _schedate, int _scherepeatnumber,
600                           int _repeatDays, bool enabled, VLMDialog *parent )
601             : VLMAWidget( name, input, output, enabled, parent, QVLM_Schedule )
602 {
603     nameLabel->setText( qtr("Schedule: ") + name );
604     schetime = _schetime;
605     schedate = _schedate;
606     rNumber = _scherepeatnumber;
607     rDays = _repeatDays;
608     type = QVLM_Schedule;
609     update();
610 }
611
612 void VLMSchedule::update()
613 {
614    VLMWrapper::EditSchedule( name, input, output, schetime, schedate,
615                              rNumber, rDays, b_enabled);
616 }
617
618 /****************
619  * VLMVOD
620  ****************/
621 VLMVod::VLMVod( const QString& name, const QString& input, const QString& output,
622                 bool enabled, const QString& _mux, VLMDialog *parent)
623        : VLMAWidget( name, input, output, enabled, parent, QVLM_VOD )
624 {
625     nameLabel->setText( qtr("VOD: ") + name );
626
627     mux = _mux;
628     muxLabel = new QLabel;
629     objLayout->addWidget( muxLabel, 1, 0 );
630
631     update();
632 }
633
634 void VLMVod::update()
635 {
636     muxLabel->setText( mux );
637     VLMWrapper::EditVod( name, input, output, b_enabled, mux );
638 }
639
640
641 /*******************
642  * VLMWrapper
643  *******************/
644 vlm_t * VLMWrapper::p_vlm = NULL;
645
646 VLMWrapper::VLMWrapper( vlm_t *_p_vlm )
647 {
648     p_vlm = _p_vlm;
649 }
650
651 VLMWrapper::~VLMWrapper()
652 {
653     p_vlm = NULL;
654 }
655
656 void VLMWrapper::AddBroadcast( const QString& name, const QString& input,
657                                const QString& output,
658                                bool b_enabled, bool b_loop  )
659 {
660     vlm_message_t *message;
661     QString command = "new \"" + name + "\" broadcast";
662     vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
663     vlm_MessageDelete( message );
664     EditBroadcast( name, input, output, b_enabled, b_loop );
665 }
666
667 void VLMWrapper::EditBroadcast( const QString& name, const QString& input,
668                                 const QString& output,
669                                 bool b_enabled, bool b_loop  )
670 {
671     vlm_message_t *message;
672     QString command;
673
674     command = "setup \"" + name + "\" inputdel all";
675     vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
676     vlm_MessageDelete( message );
677     command = "setup \"" + name + "\" input \"" + input + "\"";
678     vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
679     vlm_MessageDelete( message );
680     if( !output.isEmpty() )
681     {
682         command = "setup \"" + name + "\" output \"" + output + "\"";
683         vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
684         vlm_MessageDelete( message );
685     }
686     if( b_enabled )
687     {
688         command = "setup \"" + name + "\" enabled";
689         vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
690         vlm_MessageDelete( message );
691     }
692     if( b_loop )
693     {
694         command = "setup \"" + name + "\" loop";
695         vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
696         vlm_MessageDelete( message );
697     }
698 }
699
700 void VLMWrapper::EnableItem( const QString& name, bool b_enable )
701 {
702     vlm_message_t *message;
703     QString command = "setup \"" + name + ( b_enable ? " enable" : " disable" );
704     vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
705     vlm_MessageDelete( message );
706 }
707
708 void VLMWrapper::ControlBroadcast( const QString& name, int BroadcastStatus,
709                                    unsigned int seek )
710 {
711     vlm_message_t *message;
712
713     QString command = "control \"" + name + "\"";
714     switch( BroadcastStatus )
715     {
716     case ControlBroadcastPlay:
717         command += " play";
718         break;
719     case ControlBroadcastPause:
720         command += " pause";
721         break;
722     case ControlBroadcastStop:
723         command += " stop";
724         break;
725     case ControlBroadcastSeek:
726         command += " seek" + seek;
727         break;
728     }
729     vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
730     vlm_MessageDelete( message );
731 }
732
733 void VLMWrapper::AddVod( const QString& name, const QString& input,
734                          const QString& output,
735                          bool b_enabled, const QString& mux )
736 {
737     vlm_message_t *message;
738     QString command = "new \"" + name + "\" vod";
739     vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
740     vlm_MessageDelete( message );
741     EditVod(  name, input, output, b_enabled, mux );
742 }
743
744 void VLMWrapper::EditVod( const QString& name, const QString& input,
745                           const QString& output,
746                           bool b_enabled,
747                           const QString& mux )
748 {
749     vlm_message_t *message;
750     QString command = "setup \"" + name + "\" input \"" + input + "\"";
751     vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
752     vlm_MessageDelete( message );
753
754     if( !output.isEmpty() )
755     {
756         command = "setup \"" + name + "\" output \"" + output + "\"";
757         vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
758         vlm_MessageDelete( message );
759     }
760
761     if( b_enabled )
762     {
763         command = "setup \"" + name + "\" enabled";
764         vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
765         vlm_MessageDelete( message );
766     }
767     if( !mux.isEmpty() )
768     {
769         command = "setup \"" + name + "\" mux \"" + mux + "\"";
770         vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
771         vlm_MessageDelete( message );
772     }
773 }
774
775 void VLMWrapper::AddSchedule( const QString& name, const QString& input,
776                               const QString& output, QDateTime _schetime,
777                               QDateTime _schedate,
778                               int _scherepeatnumber, int _repeatDays,
779                               bool b_enabled, const QString& mux )
780 {
781     vlm_message_t *message;
782     QString command = "new \"" + name + "\" schedule";
783     vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
784     vlm_MessageDelete( message );
785     EditSchedule(  name, input, output, _schetime, _schedate,
786             _scherepeatnumber, _repeatDays, b_enabled, mux );
787 }
788
789 void VLMWrapper::EditSchedule( const QString& name, const QString& input,
790                                const QString& output, QDateTime _schetime,
791                                QDateTime _schedate, int _scherepeatnumber,
792                                int _repeatDays, bool b_enabled,
793                                const QString& mux )
794 {
795     vlm_message_t *message;
796     QString command = "setup \"" + name + "\" input \"" + input + "\"";
797     vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
798     vlm_MessageDelete( message );
799
800     if( !output.isEmpty() )
801     {
802         command = "setup \"" + name + "\" output \"" + output + "\"";
803         vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
804         vlm_MessageDelete( message );
805     }
806
807     if( b_enabled )
808     {
809         command = "setup \"" + name + "\" enabled";
810         vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
811         vlm_MessageDelete( message );
812     }
813
814     if( !mux.isEmpty() )
815     {
816         command = "setup \"" + name + "\" mux \"" + mux + "\"";
817         vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
818         vlm_MessageDelete( message );
819     }
820
821     command = "setup \"" + name + "\" date \"" +
822         _schedate.toString( "yyyy/MM/dd" )+ "-" +
823         _schetime.toString( "hh:mm:ss" ) + "\"";
824     vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
825     vlm_MessageDelete( message );
826
827     if( _scherepeatnumber > 0 )
828     {
829        command = "setup \"" + name + "\" repeat \"" + _scherepeatnumber + "\"";
830        vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
831        vlm_MessageDelete( message );
832     }
833
834     if( _repeatDays > 0 )
835     {
836        command = "setup \"" + name + "\" period \"" + _repeatDays + "\"";
837        vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
838        vlm_MessageDelete( message );
839     }
840 }
841
842 void VLMDialog::toggleVisible()
843 {
844     QList<VLMAWidget *>::iterator it;
845     for( it = vlmItems.begin(); it != vlmItems.end(); it++ )
846     {
847         VLMAWidget *item =  *it;
848         delete item;
849         item = NULL;
850     }
851     vlmItems.clear();
852     ui.vlmListItem->clear();
853     mediasPopulator();
854     QVLCDialog::toggleVisible();
855 }
856
857
858 #endif