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