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