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