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