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