]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/vlm.cpp
Qt4 - Use a main instance of Sout for VLM. Clean DP since all dialogs (except Bookmar...
[vlc] / modules / gui / qt4 / dialogs / vlm.cpp
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 $
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 #include "dialogs/vlm.hpp"
27 #include "dialogs/open.hpp"
28 #include "dialogs/sout.hpp"
29
30 #include <QString>
31 #include <QComboBox>
32 #include <QVBoxLayout>
33 #include <QStackedWidget>
34 #include <QLabel>
35 #include <QWidget>
36 #include <QGridLayout>
37 #include <QLineEdit>
38 #include <QCheckBox>
39 #include <QToolButton>
40 #include <QGroupBox>
41 #include <QPushButton>
42 #include <QHBoxLayout>
43 #include <QDateTimeEdit>
44 #include <QSpinBox>
45 #include <QHeaderView>
46 #include <QScrollArea>
47
48 static const char *psz_type[] = { "Broadcast", "Schedule", "VOD" };
49
50 VLMDialog *VLMDialog::instance = NULL;
51
52 VLMDialog::VLMDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
53 {
54     p_vlm = vlm_New( p_intf );
55
56     if( !p_vlm )
57     {
58         msg_Warn( p_intf, "Couldn't build VLM object ");
59         return;
60     }
61     vlmWrapper = new VLMWrapper( p_vlm );
62
63     // UI stuff
64     ui.setupUi( this );
65     ui.saveButton->hide();
66
67 #define ADDMEDIATYPES( str, type ) ui.mediaType->addItem( qtr( str ), QVariant( type ) );
68     ADDMEDIATYPES( "Broadcast", QVLM_Broadcast );
69     ADDMEDIATYPES( "Schedule", QVLM_Schedule );
70     ADDMEDIATYPES( "Video On Demand ( VOD )", QVLM_VOD );
71 #undef ADDMEDIATYPES
72
73     /* Schedule Stuffs */
74     QGridLayout *schetimelayout = new QGridLayout( ui.schedBox );
75     QLabel *schetimelabel = new QLabel( qtr( "Hours/Minutes/Seconds:" ) );
76     schetimelayout->addWidget( schetimelabel, 0, 0 );
77     QLabel *schedatelabel = new QLabel( qtr( "Day Month Year:" ) );
78     schetimelayout->addWidget( schedatelabel, 1, 0 );
79     QLabel *scherepeatLabel = new QLabel( qtr( "Repeat:" ) );
80     schetimelayout->addWidget( scherepeatLabel, 2, 0 );
81     QLabel *scherepeatTimeLabel = new QLabel( qtr( "Repeat delay:" ) );
82     schetimelayout->addWidget( scherepeatTimeLabel, 3, 0 );
83
84     time = new QDateTimeEdit( QTime::currentTime() );
85     time->setAlignment( Qt::AlignRight );
86     schetimelayout->addWidget( time, 0, 1, 1, 3 );
87
88     date = new QDateTimeEdit( QDate::currentDate() );
89     date->setAlignment( Qt::AlignRight );
90     date->setCalendarPopup( true );
91 #ifdef WIN32
92     date->setDisplayFormat( "dd MM yyyy" );
93 #else
94     date->setDisplayFormat( "dd MMMM yyyy" );
95 #endif
96     schetimelayout->addWidget( date, 1, 1, 1, 3 );
97
98     scherepeatnumber = new QSpinBox;
99     scherepeatnumber->setAlignment( Qt::AlignRight );
100     schetimelayout->addWidget( scherepeatnumber, 2, 1, 1, 3 );
101
102     repeatDays = new QSpinBox;
103     repeatDays->setAlignment( Qt::AlignRight );
104     schetimelayout->addWidget( repeatDays, 3, 1, 1, 1 );
105     repeatDays->setSuffix( qtr(" days") );
106
107     repeatTime = new QDateTimeEdit;
108     repeatTime->setAlignment( Qt::AlignRight );
109     schetimelayout->addWidget( repeatTime, 3, 2, 1, 2 );
110     repeatTime->setDisplayFormat( "hh:mm:ss" );
111
112     /* scrollArea */
113     ui.vlmItemScroll->setFrameStyle( QFrame::NoFrame );
114     ui.vlmItemScroll->setWidgetResizable( true );
115     vlmItemWidget = new QWidget;
116     vlmItemLayout = new QVBoxLayout( vlmItemWidget );
117     vlmItemWidget->setLayout( vlmItemLayout );
118     ui.vlmItemScroll->setWidget( vlmItemWidget );
119
120     QSpacerItem *spacer =
121         new QSpacerItem( 10, 10, QSizePolicy::Minimum, QSizePolicy::Expanding);
122     vlmItemLayout->addItem( spacer );
123
124     QPushButton *closeButton = new QPushButton( qtr( "Close" ) );
125     ui.buttonBox->addButton( closeButton, QDialogButtonBox::AcceptRole );
126
127     showScheduleWidget( QVLM_Broadcast );
128
129     /* Connect the comboBox to show the right Widgets */
130     CONNECT( ui.mediaType, currentIndexChanged( int ),
131              this, showScheduleWidget( int ) );
132
133     /* Connect the leftList to show the good VLMItem */
134     CONNECT( ui.vlmListItem, currentRowChanged( int ),
135              this, selectVLMItem( int ) );
136
137     BUTTONACT( closeButton, close() );
138     BUTTONACT( ui.addButton, addVLMItem() );
139     BUTTONACT( ui.clearButton, clearWidgets() );
140     BUTTONACT( ui.saveButton, saveModifications() );
141     BUTTONACT( ui.inputButton, selectInput() );
142     BUTTONACT( ui.outputButton, selectOutput() );
143 }
144
145 VLMDialog::~VLMDialog()
146 {
147    /* FIXME :you have to destroy vlm here to close
148     * but we shouldn't destroy vlm here in case somebody else wants it */
149     if( p_vlm )
150         vlm_Delete( p_vlm );
151 }
152
153 void VLMDialog::showScheduleWidget( int i )
154 {
155     ui.schedBox->setVisible( ( i == QVLM_Schedule ) );
156     ui.loopBCast->setVisible( ( i == QVLM_Broadcast ) );
157     ui.vodBox->setVisible( ( i == QVLM_VOD ) );
158 }
159
160 void VLMDialog::selectVLMItem( int i )
161 {
162     ui.vlmItemScroll->ensureWidgetVisible( vlmItems.at( i ) );
163 }
164
165 bool VLMDialog::isNameGenuine( QString name )
166 {
167     for( int i = 0; i < vlmItems.size(); i++ )
168     {
169         if( vlmItems.at( i )->name == name )
170             return false;
171     }
172     return true;
173 }
174
175 void VLMDialog::addVLMItem()
176 {
177     int vlmItemCount = vlmItems.size();
178
179     /* Take the name and Check it */
180     QString name = ui.nameLedit->text();
181     if( name.isEmpty() || !isNameGenuine( name ) )
182     {
183         msg_Dbg( p_intf, "VLM Name is empty or already exists, I can't do it" );
184         return;
185     }
186
187     int type = ui.mediaType->itemData( ui.mediaType->currentIndex() ).toInt();
188
189     QString typeShortName;
190     QString inputText = ui.inputLedit->text();
191     QString outputText = ui.outputLedit->text();
192     bool b_checked = ui.enableCheck->isChecked();
193     bool b_looped = ui.loopBCast->isChecked();
194
195     VLMAWidget * vlmAwidget;
196
197     switch( type )
198     {
199     case QVLM_Broadcast:
200         typeShortName = "Bcast";
201         vlmAwidget = new VLMBroadcast( name, inputText, outputText,
202                                   b_checked, b_looped, this );
203         VLMWrapper::AddBroadcast( name, inputText, outputText, b_checked, b_looped );
204     break;
205     case QVLM_VOD:
206         typeShortName = "VOD";
207         vlmAwidget = new VLMVod( name, inputText, outputText,
208                                  b_checked, ui.muxLedit->text(), this );
209         VLMWrapper::AddVod( name, inputText, outputText, b_checked );
210         break;
211     case QVLM_Schedule:
212         typeShortName = "Sched";
213         vlmAwidget = new VLMSchedule( name, inputText, outputText,
214                                       b_checked, this );
215         break;
216     default:
217         msg_Warn( p_intf, "Something bad happened" );
218         return;
219     }
220
221     /* Add an Item of the Side List */
222     ui.vlmListItem->addItem( typeShortName + " : " + name );
223     ui.vlmListItem->setCurrentRow( vlmItemCount - 1 );
224
225     /* Add a new VLMAWidget on the main List */
226
227     vlmItemLayout->insertWidget( vlmItemCount, vlmAwidget );
228     vlmItems.append( vlmAwidget );
229 }
230
231 void VLMDialog::clearWidgets()
232 {
233     ui.nameLedit->clear();
234     ui.inputLedit->clear();
235     ui.outputLedit->clear();
236     time->setTime( QTime::currentTime() );
237     date->setDate( QDate::currentDate() );
238     ui.enableCheck->setChecked( true );
239     ui.nameLedit->setReadOnly( false );
240     ui.loopBCast->setChecked( false );
241     ui.muxLedit->clear();
242     ui.saveButton->hide();
243     ui.addButton->show();
244 }
245
246 void VLMDialog::selectInput()
247 {
248     OpenDialog *o = OpenDialog::getInstance( this, p_intf, 0, true );
249     o->exec();
250     ui.inputLedit->setText( o->getMRL() );
251 }
252
253 void VLMDialog::selectOutput()
254 {
255     SoutDialog *s = SoutDialog::getInstance( this, p_intf, false );
256     if( s->exec() == QDialog::Accepted )
257         ui.outputLedit->setText( s->getMrl() );
258 }
259
260 /* Object Modification */
261 void VLMDialog::removeVLMItem( VLMAWidget *vlmObj )
262 {
263     int index = vlmItems.indexOf( vlmObj );
264     if( index < 0 ) return;
265
266     //FIXME, this is going to segfault if the focus in on the ListWidget
267     delete ui.vlmListItem->takeItem( index );
268     vlmItems.removeAt( index );
269     delete vlmObj;
270
271     /* HERE BE DRAGONS VLM REQUEST */
272 }
273
274 void VLMDialog::startModifyVLMItem( VLMAWidget *vlmObj )
275 {
276     currentIndex = vlmItems.indexOf( vlmObj );
277     if( currentIndex < 0 ) return;
278
279     msg_Dbg( p_intf, "Type: %i", vlmObj->type );
280     ui.vlmListItem->setCurrentRow( currentIndex );
281     ui.nameLedit->setText( vlmObj->name );
282     ui.inputLedit->setText( vlmObj->input );
283     ui.outputLedit->setText( vlmObj->output );
284     ui.enableCheck->setChecked( vlmObj->b_enabled );
285
286     switch( vlmObj->type )
287     {
288     case QVLM_Broadcast:
289         ui.loopBCast->setChecked( (qobject_cast<VLMBroadcast *>(vlmObj))->b_looped );
290         break;
291     case QVLM_VOD:
292         ui.muxLedit->setText( (qobject_cast<VLMVod *>(vlmObj))->mux );
293         break;
294     case QVLM_Schedule:
295         //(qobject_cast<VLMSchedule *>)
296         break;
297     }
298
299     ui.nameLedit->setReadOnly( true );
300     ui.addButton->hide();
301     ui.saveButton->show();
302 }
303
304 void VLMDialog::saveModifications()
305 {
306     VLMAWidget *vlmObj = vlmItems.at( currentIndex );
307     if( vlmObj )
308     {
309         vlmObj->input = ui.inputLedit->text();
310         vlmObj->output = ui.outputLedit->text();
311         vlmObj->setChecked( ui.enableCheck->isChecked() );
312         vlmObj->b_enabled = ui.enableCheck->isChecked();
313         switch( vlmObj->type )
314         {
315         case QVLM_Broadcast:
316             (qobject_cast<VLMBroadcast *>(vlmObj))->b_looped = ui.loopBCast->isChecked();
317             break;
318         case QVLM_VOD:
319             (qobject_cast<VLMVod *>(vlmObj))->mux = ui.muxLedit->text();
320             break;
321         case QVLM_Schedule:
322             break;
323            //           vlmObj->
324         }
325         vlmObj->update(); /* It should call the correct function is VLMAWidget
326                              is abstract, but I am far from sure... FIXME ? */
327     }
328     clearWidgets();
329 }
330
331 /*********************************
332  * VLMAWidget - Abstract class
333  ********************************/
334
335 VLMAWidget::VLMAWidget( QString _name,
336                         QString _input,
337                         QString _output,
338                         bool _enabled,
339                         VLMDialog *_parent,
340                         int _type )
341                       : QGroupBox( _name, _parent )
342 {
343     parent = _parent;
344     name = _name;
345     input = _input;
346     output = _output;
347     b_enabled = _enabled;
348     type = _type;
349
350     setCheckable( true );
351     setChecked( b_enabled );
352
353     objLayout = new QGridLayout( this );
354     setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Maximum );
355
356     nameLabel = new QLabel;
357     objLayout->addWidget( nameLabel, 0, 0, 1, 4 );
358
359     /*QLabel *time = new QLabel( "--:--/--:--" );
360     objLayout->addWidget( time, 1, 3, 1, 2 );*/
361
362     QToolButton *modifyButton = new QToolButton;
363     modifyButton->setIcon( QIcon( QPixmap( ":/pixmaps/menus_settings_16px.png" ) ) );
364     objLayout->addWidget( modifyButton, 0, 5 );
365
366     QToolButton *deleteButton = new QToolButton;
367     deleteButton->setIcon( QIcon( QPixmap( ":/pixmaps/menus_quit_16px.png" ) ) );
368     objLayout->addWidget( deleteButton, 0, 6 );
369
370     BUTTONACT( modifyButton, modify() );
371     BUTTONACT( deleteButton, del() );
372     CONNECT( this, clicked( bool ), this, toggleEnabled( bool ) );
373 }
374
375 void VLMAWidget::modify()
376 {
377     parent->startModifyVLMItem( this );
378 }
379
380 void VLMAWidget::del()
381 {
382     parent->removeVLMItem( this );
383 }
384
385 //FIXME, remove me before release
386 void VLMAWidget::enterEvent( QEvent *event )
387 {
388     printf( "test" );
389 }
390
391 void VLMAWidget::toggleEnabled( bool b_enable )
392 {
393     VLMWrapper::EnableItem( name, b_enable );
394 }
395
396 /****************
397  * VLMBroadcast
398  ****************/
399 VLMBroadcast::VLMBroadcast( QString _name, QString _input, QString _output,
400                             bool _enabled, bool _looped, VLMDialog *_parent)
401                           : VLMAWidget( _name, _input, _output,
402                                         _enabled, _parent, QVLM_Broadcast )
403 {
404     nameLabel->setText( "Broadcast: " + name );
405     type = QVLM_Broadcast;
406     b_looped = _looped;
407
408     playButton = new QToolButton;
409     playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
410     objLayout->addWidget( playButton, 1, 0 );
411     b_playing = true;
412
413     QToolButton *stopButton = new QToolButton;
414     stopButton->setIcon( QIcon( QPixmap( ":/pixmaps/stop_16px.png" ) ) );
415     objLayout->addWidget( stopButton, 1, 1 );
416
417     loopButton = new QToolButton;
418     objLayout->addWidget( loopButton, 1, 2 );
419
420     BUTTONACT( playButton, togglePlayPause() );
421     BUTTONACT( stopButton, stop() );
422     BUTTONACT( loopButton, toggleLoop() );
423
424     update();
425 }
426
427 void VLMBroadcast::update()
428 {
429     VLMWrapper::EditBroadcast( name, input, output, b_enabled, b_looped );
430     if( b_looped )
431         loopButton->setIcon( QIcon( QPixmap( ":/pixmaps/playlist_repeat_all.png" ) ) );
432     else
433         loopButton->setIcon( QIcon( QPixmap( ":/pixmaps/playlist_repeat_off.png" ) ) );
434 }
435
436 void VLMBroadcast::togglePlayPause()
437 {
438     if( b_playing = true )
439     {
440         VLMWrapper::ControlBroadcast( name, ControlBroadcastPause );
441         playButton->setIcon( QIcon( QPixmap( ":/pixmaps/pause_16px.png" ) ) );
442     }
443     else
444     {
445         VLMWrapper::ControlBroadcast( name, ControlBroadcastPlay );
446         playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
447     }
448     b_playing = !b_playing;
449 }
450
451 void VLMBroadcast::toggleLoop()
452 {
453     b_enabled = !b_enabled;
454     update();
455 }
456
457 void VLMBroadcast::stop()
458 {
459     VLMWrapper::ControlBroadcast( name, ControlBroadcastStop );
460     playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
461 }
462
463 /****************
464  * VLMSchedule
465  ****************/
466 VLMSchedule::VLMSchedule( QString name, QString input, QString output,
467                             bool enabled, VLMDialog *parent)
468             : VLMAWidget( name, input, output, enabled, parent, QVLM_Schedule )
469 {
470     nameLabel->setText( "Schedule: " + name );
471 }
472
473 void VLMSchedule::update()
474 {
475 }
476
477 /****************
478  * VLMVOD
479  ****************/
480 VLMVod::VLMVod( QString name, QString input, QString output,
481                 bool enabled, QString _mux, VLMDialog *parent)
482        : VLMAWidget( name, input, output, enabled, parent, QVLM_VOD )
483 {
484     nameLabel->setText( "VOD:" + name );
485
486     mux = _mux;
487     muxLabel = new QLabel;
488     objLayout->addWidget( muxLabel, 1, 0 );
489
490     update();
491 }
492
493 void VLMVod::update()
494 {
495     muxLabel->setText( mux );
496     VLMWrapper::EditVod( name, input, output, b_enabled, mux );
497 }
498
499
500 /*******************
501  * VLMWrapper
502  *******************/
503 vlm_t * VLMWrapper::p_vlm = NULL;
504
505 VLMWrapper::VLMWrapper( vlm_t *_p_vlm )
506 {
507     p_vlm = _p_vlm;
508 }
509
510 VLMWrapper::~VLMWrapper()
511 {}
512
513 void VLMWrapper::AddBroadcast( const QString name, QString input,
514                                QString output,
515                                bool b_enabled, bool b_loop  )
516 {
517     vlm_message_t *message;
518     QString command = "new \"" + name + "\" broadcast";
519     vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
520     vlm_MessageDelete( message );
521     EditBroadcast( name, input, output, b_enabled, b_loop );
522 }
523
524 void VLMWrapper::EditBroadcast( const QString name, const QString input,
525                                 const QString output,
526                                 bool b_enabled, bool b_loop  )
527 {
528     vlm_message_t *message;
529     QString command;
530
531     command = "setup \"" + name + "\" inputdel all";
532     vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
533     vlm_MessageDelete( message );
534     command = "setup \"" + name + "\" input \"" + input + "\"";
535     vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
536     vlm_MessageDelete( message );
537     if( !output.isEmpty() )
538     {
539         command = "setup \"" + name + "\" output \"" + output + "\"";
540         vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
541         vlm_MessageDelete( message );
542     }
543     if( b_enabled )
544     {
545         command = "setup \"" + name + "\" enabled";
546         vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
547         vlm_MessageDelete( message );
548     }
549     if( b_loop )
550     {
551         command = "setup \"" + name + "\" loop";
552         vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
553         vlm_MessageDelete( message );
554     }
555 }
556
557 void VLMWrapper::EnableItem( const QString name, bool b_enable )
558 {
559     vlm_message_t *message;
560     QString command = "setup \"" + name + ( b_enable ? " enable" : " disable" );
561 }
562
563 void VLMWrapper::ControlBroadcast( const QString name, int BroadcastStatus,
564                                    unsigned int seek )
565 {
566     vlm_message_t *message;
567
568     QString command = "control \"" + name;
569     switch( BroadcastStatus )
570     {
571     case ControlBroadcastPlay:
572         command += " play";
573         break;
574     case ControlBroadcastPause:
575         command += " pause";
576         break;
577     case ControlBroadcastStop:
578         command += " stop";
579         break;
580     case ControlBroadcastSeek:
581         command += " seek" + seek;
582         break;
583     }
584     vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
585     vlm_MessageDelete( message );
586 }
587
588 void VLMWrapper::AddVod( const QString name, const QString input,
589                          const QString output,
590                          bool b_enabled, const QString mux )
591 {
592     vlm_message_t *message;
593     QString command = "new \"" + name + "\" vod";
594     vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
595     vlm_MessageDelete( message );
596     EditVod(  name, input, output, b_enabled, mux );
597 }
598
599 void VLMWrapper::EditVod( const QString name, const QString input,
600                           const QString output,
601                           bool b_enabled,
602                           const QString mux )
603 {
604     vlm_message_t *message;
605     QString command = "setup \"" + name + "\" input \"" + input + "\"";
606     vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
607     vlm_MessageDelete( message );
608     if( !output.isEmpty() )
609     {
610         command = "setup \"" + name + "\" output \"" + output + "\"";
611         vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
612         vlm_MessageDelete( message );
613     }
614     if( b_enabled )
615     {
616         command = "setup \"" + name + "\" enabled";
617         vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
618         vlm_MessageDelete( message );
619     }
620     if( !mux.isEmpty() )
621     {
622         command = "setup \"" + name + "\" mux \"" + mux + "\"";
623         vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
624         vlm_MessageDelete( message );
625     }
626 }