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