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