]> git.sesse.net Git - vlc/blob - plugins/kde/kde_interface.cpp
* ALL: the first libvlc commit.
[vlc] / plugins / kde / kde_interface.cpp
1 /***************************************************************************
2                           kde_interface.cpp  -  description
3                              -------------------
4     begin                : Sun Mar 25 2001
5     copyright            : (C) 2001 by andres
6     email                : dae@chez.com
7  ***************************************************************************/
8
9 #include "kde_disc.h"
10 #include "kde_interface.h"
11 #include "kde_net.h"
12 #include "kde_menu.h"
13 #include "kde_slider.h"
14 #include "kde_preferences.h"
15
16 #include <iostream.h>
17
18 #include <kaction.h>
19 #include <kfiledialog.h>
20 #include <klocale.h>
21 #include <kstdaction.h>
22 #include <kurl.h>
23 #include <kurldrag.h>
24 #include <qcursor.h>
25 #include <qdragobject.h>
26 #include <qtimer.h>
27 #include <kdialog.h>
28
29 #define ID_STATUS_MSG       1
30 #define ID_DATE             2
31 #define ID_STREAM_SOURCE    3
32
33 KInterface::KInterface( intf_thread_t *p_intf, QWidget *parent,
34         const char *name ) : KMainWindow(parent,name)
35 {
36     setAcceptDrops(true);
37
38     this->p_intf = p_intf;
39
40     fDiskDialog = new KDiskDialog( this );
41     fNetDialog = new KNetDialog( this );
42     fTitleMenu = new KTitleMenu( p_intf, this );
43
44     fSlider = new KVLCSlider( QSlider::Horizontal, this );
45     fSlider->setMaxValue(10000);
46     connect( fSlider, SIGNAL( userChanged( int ) ), this, SLOT( slotSliderMoved( int ) ) );
47     connect( fSlider, SIGNAL( valueChanged( int ) ), this, SLOT( slotSliderChanged( int ) ) );
48     setCentralWidget(fSlider);
49
50     fTimer = new QTimer( this );
51     connect( fTimer, SIGNAL( timeout() ), this, SLOT( slotManage() ) );
52     fTimer->start( 100 );
53
54     resize( 400, 30 );
55
56     ///////////////////////////////////////////////////////////////////
57     // call inits to invoke all other construction parts
58     // XXX could we move this up ?
59     initStatusBar();
60     initActions();
61
62     // add certain calls to the popup menu
63     fileOpen->plug( fTitleMenu );
64     fileOpenRecent->plug( fTitleMenu );
65     diskOpen->plug( fTitleMenu );
66     streamOpen->plug( fTitleMenu );
67     play->plug( fTitleMenu );
68     pause->plug( fTitleMenu );
69     slow->plug( fTitleMenu );
70     fast->plug( fTitleMenu );
71     fileClose->plug( fTitleMenu );
72     fileQuit->plug( fTitleMenu );
73 }
74
75 KInterface::~KInterface()
76 {
77     ;
78 }
79
80 void KInterface::initActions()
81 {
82     fileOpen = KStdAction::open(this, SLOT(slotFileOpen()), actionCollection());
83     fileOpenRecent = KStdAction::openRecent(this, SLOT(slotFileOpenRecent(const KURL&)), actionCollection());
84     fileClose = KStdAction::close(this, SLOT(slotFileClose()), actionCollection());
85     preferences = KStdAction::preferences(this, SLOT(slotShowPreferences()), actionCollection());
86     fileQuit = KStdAction::quit(this, SLOT(slotFileQuit()), actionCollection());
87     viewToolBar = KStdAction::showToolbar(this, SLOT(slotViewToolBar()), actionCollection());
88     viewStatusBar = KStdAction::showStatusbar(this, SLOT(slotViewStatusBar()), actionCollection());
89
90     diskOpen = new KAction( i18n( "Open &Disk" ), 0, 0, this, SLOT( slotOpenDisk() ), actionCollection(), "open_disk" );
91     streamOpen = new KAction( i18n( "Open &Stream" ), 0, 0, this, SLOT( slotOpenStream() ), actionCollection(), "open_stream" );
92     backward = new KAction( i18n( "&Backward" ), 0, 0, this, SLOT( slotBackward() ), actionCollection(), "backward" );
93     stop = new KAction( i18n( "&Stop" ), 0, 0, this, SLOT( slotStop() ), actionCollection(), "stop" );
94     play = new KAction( i18n( "&Play" ), 0, 0, this, SLOT( slotPlay() ), actionCollection(), "play" );
95     pause = new KAction( i18n( "P&ause" ), 0, 0, this, SLOT( slotPause() ), actionCollection(), "pause" );
96     slow = new KAction( i18n( "&Slow" ), 0, 0, this, SLOT( slotSlow() ), actionCollection(), "slow" );
97     fast = new KAction( i18n( "Fas&t" ), 0, 0, this, SLOT( slotFast() ), actionCollection(), "fast" );
98     prev = new KAction( i18n( "Prev" ), 0, 0, this, SLOT( slotPrev() ), actionCollection(), "prev" );
99     next = new KAction( i18n( "Next" ), 0, 0, this, SLOT( slotNext() ), actionCollection(), "next" );
100     
101     fileOpen->setStatusText(i18n("Opens an existing document"));
102     fileOpenRecent->setStatusText(i18n("Opens a recently used file"));
103     fileClose->setStatusText(i18n("Closes the actual document"));
104     fileQuit->setStatusText(i18n("Quits the application"));
105     viewToolBar->setStatusText(i18n("Enables/disables the toolbar"));
106     viewStatusBar->setStatusText(i18n("Enables/disables the statusbar"));
107
108     diskOpen->setStatusText( i18n( "Opens a disk") );
109     streamOpen->setStatusText( i18n( "Opens a network stream" ) );
110     backward->setStatusText( i18n( "Backward" ) );
111     stop->setStatusText( i18n( "Stops playback" ) );
112     play->setStatusText( i18n( "Starts playback" ) );
113     pause->setStatusText( i18n( "Pauses playback" ) );
114     slow->setStatusText( i18n( "Slow" ) );
115     fast->setStatusText( i18n( "Fast" ) );
116     prev->setStatusText( i18n( "Prev" ) );
117     next->setStatusText( i18n( "Next" ) );
118     // use the absolute path to your ktestui.rc file for testing purpose in createGUI();
119
120     createGUI("plugins/kde/kde_ui.rc");
121 }
122
123 void KInterface::initStatusBar()
124 {
125   ///////////////////////////////////////////////////////////////////
126   // STATUSBAR
127   // TODO: add your own items you need for displaying current application status.
128     statusBar()->insertItem(i18n("Ready."), ID_STATUS_MSG, 1, false);
129     statusBar()->setItemAlignment( ID_STATUS_MSG, AlignLeft | AlignVCenter );
130     statusBar()->insertItem( "0:00:00", ID_DATE, 0, true );
131 }
132
133 /////////////////////////////////////////////////////////////////////
134 // SLOT IMPLEMENTATION
135 /////////////////////////////////////////////////////////////////////
136
137 void KInterface::slotFileOpen()
138 {
139     slotStatusMsg( i18n( "Opening file..." ) );
140     KURL url=KFileDialog::getOpenURL( QString::null,
141             i18n( "*|All files" ), this, i18n( "Open File..." ) );
142
143     if( !url.isEmpty() )
144     {
145         fileOpenRecent->addURL( url );
146         intf_PlaylistAdd( p_intf->p_vlc->p_playlist, PLAYLIST_END, url.path() );
147     }
148
149     slotStatusMsg( i18n( "Ready." ) );
150 }
151
152 void KInterface::slotFileOpenRecent(const KURL& url)
153 {
154   slotStatusMsg(i18n("Opening file..."));
155   slotStatusMsg(i18n("Ready."));
156 }
157
158 void KInterface::slotFileClose()
159 {
160   slotStatusMsg(i18n("Closing file..."));
161     
162   close();
163
164   slotStatusMsg(i18n("Ready."));
165 }
166
167 void KInterface::slotFileQuit()
168 {
169     slotStatusMsg(i18n("Exiting..."));
170     p_intf->p_sys->p_app->quit();
171     slotStatusMsg(i18n("Ready."));
172 }
173
174 void KInterface::slotViewToolBar()
175 {
176   slotStatusMsg(i18n("Toggling toolbar..."));
177   ///////////////////////////////////////////////////////////////////
178   // turn Toolbar on or off
179   if(!viewToolBar->isChecked())
180   {
181     toolBar("mainToolBar")->hide();
182   }
183   else
184   {
185     toolBar("mainToolBar")->show();
186   }        
187
188   slotStatusMsg(i18n("Ready."));
189 }
190
191 void KInterface::slotViewStatusBar()
192 {
193   slotStatusMsg(i18n("Toggle the statusbar..."));
194   ///////////////////////////////////////////////////////////////////
195   //turn Statusbar on or off
196   if(!viewStatusBar->isChecked())
197   {
198     statusBar()->hide();
199   }
200   else
201   {
202     statusBar()->show();
203   }
204
205   slotStatusMsg(i18n("Ready."));
206 }
207
208 void KInterface::slotShowPreferences()
209 {
210     // Do something
211     KPreferences(this->p_intf, "main", this, "preferences");
212 }
213
214 void KInterface::slotStatusMsg(const QString &text)
215 {
216   ///////////////////////////////////////////////////////////////////
217   // change status message permanently
218   statusBar()->clear();
219   statusBar()->changeItem(text, ID_STATUS_MSG);
220 }
221
222 void KInterface::slotManage()
223 {
224     vlc_mutex_lock( &p_intf->change_lock );
225
226     /* If the "display popup" flag has changed */
227     if( p_intf->b_menu_change )
228     {
229         fTitleMenu->popup( ( QCursor::pos() ) );
230         p_intf->b_menu_change = 0;
231     }
232
233     /* Update language/chapter menus after user request */
234 #if 0
235     if( p_intf->p_vlc->p_input_bank->pp_input[0] != NULL && p_intf->p_sys->p_window != NULL &&
236         p_intf->p_sys->b_menus_update )
237     {
238 //        GnomeSetupMenu( p_intf );
239     }
240 #endif
241
242     /* Manage the slider */
243 #define p_area p_intf->p_vlc->p_input_bank->pp_input[0]->stream.p_selected_area
244     if( (p_intf->p_vlc->p_input_bank->pp_input[0] != NULL) && (p_area->i_size != 0 ))
245     {
246         fSlider->setValue( ( 10000. * p_area->i_tell ) / p_area->i_size );
247     }
248 #undef p_area
249
250     /* Manage core vlc functions through the callback */
251     p_intf->pf_manage(p_intf);
252
253     if( p_intf->p_vlc->b_die )
254     {
255         p_intf->p_sys->p_app->quit();
256     }
257
258     vlc_mutex_unlock( &p_intf->change_lock );
259 }
260
261 void KInterface::slotSliderMoved( int position )
262 {
263 // XXX is this locking really useful ?
264     vlc_mutex_lock( &p_intf->change_lock );
265
266     off_t i_seek = ( position * p_intf->p_vlc->p_input_bank->pp_input[0]->stream.p_selected_area->i_size ) / 10000;
267     input_Seek( p_intf->p_vlc->p_input_bank->pp_input[0]->p_this, i_seek, INPUT_SEEK_SET );
268
269     vlc_mutex_unlock( &p_intf->change_lock );
270 }
271
272 void KInterface::slotSliderChanged( int position )
273 {
274     if( p_intf->p_vlc->p_input_bank->pp_input[0] != NULL )
275     {
276         char psz_time[ OFFSETTOTIME_MAX_SIZE ];
277
278         vlc_mutex_lock( &p_intf->p_vlc->p_input_bank->pp_input[0]->stream.stream_lock );
279
280 #define p_area p_intf->p_vlc->p_input_bank->pp_input[0]->stream.p_selected_area
281         statusBar()->changeItem( input_OffsetToTime( p_intf->p_vlc->p_input_bank->pp_input[0], psz_time, ( p_area->i_size * position ) / 10000 ), ID_DATE );
282 #undef p_area
283
284         vlc_mutex_unlock( &p_intf->p_vlc->p_input_bank->pp_input[0]->stream.stream_lock );
285      }
286 }
287
288 void KInterface::slotOpenDisk()
289 {
290     int r = fDiskDialog->exec();
291     if ( r )
292     {
293         // Build source name
294         QString source;
295         source += fDiskDialog->type();
296         source += ':';
297         source += fDiskDialog->device();
298
299         source += '@';
300         source += fDiskDialog->title();
301         source += ',';
302         source += fDiskDialog->chapter();
303
304         // add it to playlist
305         intf_PlaylistAdd( p_intf->p_vlc->p_playlist, PLAYLIST_END, source.latin1() );
306
307         // Select added item and switch to disk interface
308         intf_PlaylistJumpto( p_intf->p_vlc->p_playlist, p_intf->p_vlc->p_playlist->i_size-2 );
309         if( p_intf->p_vlc->p_input_bank->pp_input[0] != NULL )
310         {
311             p_intf->p_vlc->p_input_bank->pp_input[0]->b_eof = 1;
312         }
313     }
314 }
315
316 void KInterface::slotOpenStream()
317 {
318     int r = fNetDialog->exec();
319     if ( r )
320     {
321         // Build source name
322         QString source;
323         source += fNetDialog->protocol();
324         source += "://";
325         source += fNetDialog->server();
326         source += ":";
327         source += QString().setNum( fNetDialog->port() );
328
329         // add it to playlist
330         intf_PlaylistAdd( p_intf->p_vlc->p_playlist, PLAYLIST_END, source.latin1() );
331         intf_PlaylistJumpto( p_intf->p_vlc->p_playlist, p_intf->p_vlc->p_playlist->i_size-2 );
332
333         if( p_intf->p_vlc->p_input_bank->pp_input[0] != NULL )
334         {
335             p_intf->p_vlc->p_input_bank->pp_input[0]->b_eof = 1;
336         }
337     }
338 }
339
340 void KInterface::slotPlay()
341 {
342     if( p_intf->p_vlc->p_input_bank->pp_input[0] != NULL )
343     {
344         input_SetStatus( p_intf->p_vlc->p_input_bank->pp_input[0], INPUT_STATUS_PLAY );
345     }
346 }
347
348 void KInterface::slotPause()
349 {
350     if ( p_intf->p_vlc->p_input_bank->pp_input[0] != NULL )
351     {
352         input_SetStatus( p_intf->p_vlc->p_input_bank->pp_input[0], INPUT_STATUS_PAUSE );
353     }
354 }
355
356 void KInterface::slotStop()
357 {
358     if( p_intf->p_vlc->p_input_bank->pp_input[0] != NULL )
359     {
360         /* end playing item */
361         p_intf->p_vlc->p_input_bank->pp_input[0]->b_eof = 1;
362
363         /* update playlist */
364         vlc_mutex_lock( &p_intf->p_vlc->p_playlist->change_lock );
365
366         p_intf->p_vlc->p_playlist->i_index--;
367         p_intf->p_vlc->p_playlist->b_stopped = 1;
368
369         vlc_mutex_unlock( &p_intf->p_vlc->p_playlist->change_lock );
370     }
371 }
372
373 void KInterface::slotBackward()
374 {
375     msg_Err( p_intf, "KInterface::slotBackward() - Unimplemented" );
376 }
377
378 void KInterface::slotPrev()
379 {
380     if( p_intf->p_vlc->p_input_bank->pp_input[0] != NULL )
381     {
382         /* FIXME: temporary hack */
383         intf_PlaylistPrev( p_intf->p_vlc->p_playlist );
384         intf_PlaylistPrev( p_intf->p_vlc->p_playlist );
385         p_intf->p_vlc->p_input_bank->pp_input[0]->b_eof = 1;
386     }
387 }
388
389 void KInterface::slotNext()
390 {
391     if( p_intf->p_vlc->p_input_bank->pp_input[0] != NULL )
392     {
393         /* FIXME: temporary hack */
394         p_intf->p_vlc->p_input_bank->pp_input[0]->b_eof = 1;
395     }
396 }
397
398 void KInterface::slotSlow()
399 {
400     if( p_intf->p_vlc->p_input_bank->pp_input[0] != NULL )
401     {
402         input_SetStatus( p_intf->p_vlc->p_input_bank->pp_input[0], INPUT_STATUS_SLOWER );
403     }
404 }
405
406 void KInterface::slotFast()
407 {
408     if( p_intf->p_vlc->p_input_bank->pp_input[0] != NULL )
409     {
410         input_SetStatus( p_intf->p_vlc->p_input_bank->pp_input[0], INPUT_STATUS_FASTER );
411     }
412 }
413
414 void KInterface::dragEnterEvent( QDragEnterEvent *event )
415 {
416     event->accept( QUriDrag::canDecode( event ) );
417 }
418
419 void KInterface::dropEvent( QDropEvent *event )
420 {
421     KURL::List urlList;
422
423     if ( KURLDrag::decode( event, urlList ) ) {
424         for ( KURL::List::ConstIterator i = urlList.begin(); i != urlList.end(); i++ )
425         {
426             // XXX add a private function to add a KURL with checking
427             // actually a whole class for core abstraction would be neat
428             if( !(*i).isEmpty() )
429             {
430                 fileOpenRecent->addURL( *i );
431                 intf_PlaylistAdd( p_intf->p_vlc->p_playlist, PLAYLIST_END, (*i).path() );
432             }
433         }
434     }
435 }