]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/vlm.cpp
Qt4 - VLM: simplifications.
[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( vlmWrapper->p_vlc, 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( vlmWrapper->GetVLM(), 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 }
356
357 void VLMAWidget::modify()
358 {
359     parent->startModifyVLMItem( this );
360 }
361
362 void VLMAWidget::del()
363 {
364     parent->removeVLMItem( this );
365 }
366
367 //FIXME, remove me before release
368 void VLMAWidget::enterEvent( QEvent *event )
369 {
370     printf( "test" );
371 }
372
373 /****************
374  * VLMBroadcast 
375  ****************/
376 VLMBroadcast::VLMBroadcast( QString _name, QString _input, QString _output,
377                             bool _enabled, bool _looped, VLMDialog *_parent)
378                           : VLMAWidget( _name, _input, _output,
379                                         _enabled, _parent, QVLM_Broadcast )
380 {
381     nameLabel->setText( "Broadcast: " + name );
382     type = QVLM_Broadcast;
383     b_looped = _looped;
384
385     playButton = new QToolButton;
386     playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
387     objLayout->addWidget( playButton, 1, 0 );
388     b_playing = true;
389
390     QToolButton *stopButton = new QToolButton;
391     stopButton->setIcon( QIcon( QPixmap( ":/pixmaps/stop_16px.png" ) ) );
392     objLayout->addWidget( stopButton, 1, 1 );
393
394     loopButton = new QToolButton;
395     objLayout->addWidget( loopButton, 1, 2 );
396
397     BUTTONACT( playButton, togglePlayPause() );
398     BUTTONACT( stopButton, stop() );
399     BUTTONACT( loopButton, toggleLoop() );
400     
401     update();
402 }
403
404 void VLMBroadcast::update()
405 {
406     //VLMWrapper::EditBroadcast( VLMWrapper::p_vlm, name, input, output, b_enabled, b_looped );
407     if( b_looped )
408         loopButton->setIcon( QIcon( QPixmap( ":/pixmaps/playlist_repeat_all.png" ) ) );
409     else
410         loopButton->setIcon( QIcon( QPixmap( ":/pixmaps/playlist_repeat_off.png" ) ) );
411 }
412
413 void VLMBroadcast::togglePlayPause()
414 {
415     if( b_playing = true )
416     {
417       //  VLMWrapper::ControlBroadcast( VLMWrapper::p_vlm, name, ControlBroadcastPause );
418         playButton->setIcon( QIcon( QPixmap( ":/pixmaps/pause_16px.png" ) ) );
419     }
420     else
421     {
422         //VLMWrapper::ControlBroadcast( VLMWrapper::p_vlm, name, ControlBroadcastPlay );
423         playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
424     }
425     b_playing = !b_playing;
426 }
427
428 void VLMBroadcast::toggleLoop()
429 {
430     b_enabled = !b_enabled;
431     update();
432 }
433
434 void VLMBroadcast::stop()
435 {
436     //VLMWrapper::ControlBroadcast( VLMWrapper::p_vlm, name, ControlBroadcastStop );
437     playButton->setIcon( QIcon( QPixmap( ":/pixmaps/play_16px.png" ) ) );
438 }
439
440 /****************
441  * VLMSchedule
442  ****************/
443 VLMSchedule::VLMSchedule( QString name, QString input, QString output,
444                             bool enabled, VLMDialog *parent) 
445             : VLMAWidget( name, input, output, enabled, parent, QVLM_Schedule )
446 {
447     nameLabel->setText( "Schedule: " + name );
448 }
449
450 void VLMSchedule::update()
451 {
452 }
453
454 /****************
455  * VLMVOD
456  ****************/
457 VLMVod::VLMVod( QString name, QString input, QString output,
458                 bool enabled, QString _mux, VLMDialog *parent)
459        : VLMAWidget( name, input, output, enabled, parent, QVLM_VOD )
460 {
461     nameLabel->setText( "VOD:" + name );
462
463     mux = _mux;
464     muxLabel = new QLabel;
465     objLayout->addWidget( muxLabel, 1, 0 );
466     
467     update();
468 }
469
470 void VLMVod::update()
471 {
472     muxLabel->setText( mux );
473     //VLMWrapper::EditVod( p_vlm, name, input, output, b_enabled, mux );
474 }
475
476
477 /*******************
478  * VLMWrapper
479  *******************/
480 vlm_t * VLMWrapper::p_vlm = NULL;
481
482 VLMWrapper::VLMWrapper( vlm_t *_p_vlm )
483 {
484     p_vlm = _p_vlm;
485 }
486
487 VLMWrapper::~VLMWrapper()
488 {}
489
490 void VLMWrapper::AddBroadcast( const QString name, QString input,
491                                QString output,
492                                bool b_enabled, bool b_loop  )
493 {
494     vlm_message_t *message;
495     QString command = "new \"" + name + "\" broadcast";
496     vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
497     vlm_MessageDelete( message );
498     EditBroadcast( name, input, output, b_enabled, b_loop );
499 }
500
501 void VLMWrapper::EditBroadcast( const QString name, const QString input,
502                                 const QString output,
503                                 bool b_enabled, bool b_loop  )
504 {
505     vlm_message_t *message;
506     QString command;
507     
508     command = "setup \"" + name + "\" inputdel all";
509     vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
510     vlm_MessageDelete( message );
511     command = "setup \"" + name + "\" input \"" + input + "\"";
512     vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
513     vlm_MessageDelete( message );
514     if( !output.isEmpty() )
515     {
516         command = "setup \"" + name + "\" output \"" + output + "\"";
517         vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
518         vlm_MessageDelete( message );
519     }
520     if( b_enabled )
521     {
522         command = "setup \"" + name + "\" enabled";
523         vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
524         vlm_MessageDelete( message );
525     }
526     if( b_loop )
527     {
528         command = "setup \"" + name + "\" loop";
529         vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
530         vlm_MessageDelete( message );
531     }
532 }
533
534 void VLMWrapper::ControlBroadcast( const QString name, int BroadcastStatus, 
535                                    unsigned int seek )
536 {
537     vlm_message_t *message;
538
539     QString command = "setup \"" + name;
540     switch( BroadcastStatus )
541     {
542     case ControlBroadcastPlay:
543         command += " play";
544         break;
545     case ControlBroadcastPause:
546         command += " pause";
547         break;
548     case ControlBroadcastStop:
549         command += " stop";
550         break;
551     case ControlBroadcastSeek:
552         command += " seek" + seek;
553         break;
554     }
555     vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
556     vlm_MessageDelete( message );
557 }
558
559 void VLMWrapper::AddVod( const QString name, const QString input,
560                          const QString output,
561                          bool b_enabled, const QString mux )
562 {
563     vlm_message_t *message;
564     QString command = "new \"" + name + "\" vod";
565     vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
566     vlm_MessageDelete( message );
567     EditVod(  name, input, output, b_enabled, mux );
568 }
569
570 void VLMWrapper::EditVod( const QString name, const QString input,
571                           const QString output, 
572                           bool b_enabled,
573                           const QString mux )
574 {
575     vlm_message_t *message;
576     QString command = "setup \"" + name + "\" input \"" + input + "\"";
577     vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
578     vlm_MessageDelete( message );
579     if( !output.isEmpty() )
580     {
581         command = "setup \"" + name + "\" output \"" + output + "\"";
582         vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
583         vlm_MessageDelete( message );
584     }
585     if( b_enabled )
586     {
587         command = "setup \"" + name + "\" enabled";
588         vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
589         vlm_MessageDelete( message );
590     }
591     if( !mux.isEmpty() )
592     {
593         command = "setup \"" + name + "\" mux \"" + mux + "\"";
594         vlm_ExecuteCommand( p_vlm, qtu( command ), &message );
595         vlm_MessageDelete( message );
596     }
597 }