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