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