Revert previous commit, was incorrect
[amarok.git] / src / actionclasses.cpp
blobfa72ee7e39bcc2499a0f52c7fc50868e49a2bc22
1 // Maintainer: Max Howell <max.howell@methylblue.com>, (C) 2004
2 // Copyright: See COPYING file that comes with this distribution
4 #include "actionclasses.h"
6 #include "config-amarok.h" //HAVE_LIBVISUAL definition
8 #include "amarok.h"
9 #include "amarokconfig.h"
10 #include "app.h"
11 #include "debug.h"
12 #include "collectiondb.h"
13 #include "CoverManager.h"
14 #include "enginecontroller.h"
15 #include "k3bexporter.h"
16 #include "MainWindow.h"
17 //#include "mediumpluginmanager.h"
19 #include "socketserver.h" //Vis::Selector::showInstance()
20 #include "ContextStatusBar.h"
21 #include "threadmanager.h"
22 #include "timeLabel.h"
24 #include <KHBox>
25 #include <KHelpMenu>
26 #include <KIconLoader>
27 #include <KLineEdit>
28 #include <KLocale>
29 #include <KPushButton>
30 #include <KStandardDirs>
31 #include <KToolBar>
32 #include <KUrl>
33 #include <KAuthorized>
35 #include <QPixmap>
36 #include <QToolTip>
37 #include <Q3PopupMenu>
40 namespace Amarok
42 bool repeatNone() { return AmarokConfig::repeat() == AmarokConfig::EnumRepeat::Off; }
43 bool repeatTrack() { return AmarokConfig::repeat() == AmarokConfig::EnumRepeat::Track; }
44 bool repeatAlbum() { return AmarokConfig::repeat() == AmarokConfig::EnumRepeat::Album; }
45 bool repeatPlaylist() { return AmarokConfig::repeat() == AmarokConfig::EnumRepeat::Playlist; }
46 bool randomOff() { return AmarokConfig::randomMode() == AmarokConfig::EnumRandomMode::Off; }
47 bool randomTracks() { return AmarokConfig::randomMode() == AmarokConfig::EnumRandomMode::Tracks; }
48 bool randomAlbums() { return AmarokConfig::randomMode() == AmarokConfig::EnumRandomMode::Albums; }
49 bool favorNone() { return AmarokConfig::favorTracks() == AmarokConfig::EnumFavorTracks::Off; }
50 bool favorScores() { return AmarokConfig::favorTracks() == AmarokConfig::EnumFavorTracks::HigherScores; }
51 bool favorRatings() { return AmarokConfig::favorTracks() == AmarokConfig::EnumFavorTracks::HigherRatings; }
52 bool favorLastPlay() { return AmarokConfig::favorTracks() == AmarokConfig::EnumFavorTracks::LessRecentlyPlayed; }
53 bool entireAlbums() { return repeatAlbum() || randomAlbums(); }
56 using namespace Amarok;
58 KHelpMenu *Menu::s_helpMenu = 0;
60 static void
61 safePlug( KActionCollection *ac, const char *name, QWidget *w )
63 if( ac )
65 KAction *a = (KAction*) ac->action( name );
66 if( a && w ) w->addAction( a );
71 //////////////////////////////////////////////////////////////////////////////////////////
72 // MenuAction && Menu
73 // KActionMenu doesn't work very well, so we derived our own
74 //////////////////////////////////////////////////////////////////////////////////////////
76 MenuAction::MenuAction( KActionCollection *ac )
77 : KAction( 0 )
79 setText(i18n( "Amarok Menu" ));
80 ac->addAction("amarok_menu", this);
81 setShortcutConfigurable ( false ); //FIXME disabled as it doesn't work, should use QCursor::pos()
84 int
85 MenuAction::plug( QWidget *w, int )
87 KToolBar *bar = dynamic_cast<KToolBar*>(w);
89 if( bar && KAuthorized::authorizeKAction( objectName() ) )
91 //const int id = KAction::getToolButtonID();
93 //addContainer( bar, id );
94 w->addAction( this );
95 connect( bar, SIGNAL( destroyed() ), SLOT( slotDestroyed() ) );
97 //TODO create menu on demand
98 //TODO create menu above and aligned within window
99 //TODO make the arrow point upwards!
100 //bar->insertButton( QString::null, id, true, i18n( "Menu" ), index );
101 //bar->alignItemRight( id );
103 //KToolBarButton* button = bar->getButton( id );
104 //button->setPopup( Amarok::Menu::instance() );
105 //button->setObjectName( "toolbutton_amarok_menu" );
106 //button->setIcon( "amarok" );
108 return associatedWidgets().count() - 1;
110 else return -1;
113 Menu::Menu()
115 KActionCollection *ac = Amarok::actionCollection();
117 safePlug( ac, "repeat", this );
118 safePlug( ac, "random_mode", this );
120 addSeparator();
122 safePlug( ac, "playlist_playmedia", this );
123 safePlug( ac, "play_audiocd", this );
124 safePlug( ac, "lastfm_play", this );
126 addSeparator();
128 safePlug( ac, "cover_manager", this );
129 safePlug( ac, "queue_manager", this );
130 safePlug( ac, "visualizations", this );
131 safePlug( ac, "equalizer", this );
132 safePlug( ac, "script_manager", this );
133 safePlug( ac, "statistics", this );
135 addSeparator();
137 safePlug( ac, "update_collection", this );
138 safePlug( ac, "rescan_collection", this );
140 #ifndef Q_WS_MAC
141 addSeparator();
143 safePlug( ac, KStandardAction::name(KStandardAction::ShowMenubar), this );
144 #endif
146 addSeparator();
148 safePlug( ac, KStandardAction::name(KStandardAction::ConfigureToolbars), this );
149 safePlug( ac, KStandardAction::name(KStandardAction::KeyBindings), this );
150 // safePlug( ac, "options_configure_globals", this ); //we created this one
151 safePlug( ac, KStandardAction::name(KStandardAction::Preferences), this );
153 addSeparator();
155 addMenu( helpMenu( this ) );
157 addSeparator();
159 safePlug( ac, KStandardAction::name(KStandardAction::Quit), this );
161 #ifdef HAVE_LIBVISUAL
162 Amarok::actionCollection()->action( "visualizations" )->setEnabled( false );
163 #endif
166 Menu*
167 Menu::instance()
169 static Menu menu;
170 return &menu;
173 KMenu*
174 Menu::helpMenu( QWidget *parent ) //STATIC
176 extern KAboutData aboutData;
178 if ( s_helpMenu == 0 )
179 s_helpMenu = new KHelpMenu( parent, &aboutData, Amarok::actionCollection() );
181 return s_helpMenu->menu();
184 //////////////////////////////////////////////////////////////////////////////////////////
185 // PlayPauseAction
186 //////////////////////////////////////////////////////////////////////////////////////////
188 PlayPauseAction::PlayPauseAction( KActionCollection *ac )
189 : KToggleAction( 0 )
190 , EngineObserver( EngineController::instance() )
192 setText(i18n( "Play/Pause" ));
193 ac->addAction("play_pause", this);
195 engineStateChanged( EngineController::engine()->state() );
197 connect( this, SIGNAL(triggered()), EngineController::instance(), SLOT(playPause()) );
200 void
201 PlayPauseAction::engineStateChanged( Engine::State state, Engine::State /*oldState*/ )
203 switch( state ) {
204 case Engine::Playing:
205 setChecked( false );
206 setIcon( KIcon(Amarok::icon( "pause" )) );
207 setText( i18n( "Pause" ) );
208 break;
209 case Engine::Paused:
210 setChecked( true );
211 setIcon( KIcon(Amarok::icon( "pause" )) );
212 setText( i18n( "Pause" ) );
213 break;
214 case Engine::Empty:
215 setChecked( false );
216 setIcon( KIcon(Amarok::icon( "play" )) );
217 setText( i18n( "Play" ) );
218 break;
219 case Engine::Idle:
220 return;
223 //////////////////////////////////////////////////////////////////////////////////////////
224 // ToggleAction
225 //////////////////////////////////////////////////////////////////////////////////////////
227 ToggleAction::ToggleAction( const QString &text, void ( *f ) ( bool ), KActionCollection* const ac, const char *name )
228 : KToggleAction( 0 )
229 , m_function( f )
231 setText(text);
232 ac->addAction(name, this);
235 void ToggleAction::setChecked( bool b )
237 const bool announce = b != isChecked();
239 m_function( b );
240 KToggleAction::setChecked( b );
241 AmarokConfig::self()->writeConfig(); //So we don't lose the setting when crashing
242 if( announce ) emit toggled( b ); //KToggleAction doesn't do this for us. How gay!
245 void ToggleAction::setEnabled( bool b )
247 const bool announce = b != isEnabled();
249 if( !b )
250 setChecked( false );
251 KToggleAction::setEnabled( b );
252 AmarokConfig::self()->writeConfig(); //So we don't lose the setting when crashing
253 if( announce ) emit QAction::triggered( b );
256 //////////////////////////////////////////////////////////////////////////////////////////
257 // SelectAction
258 //////////////////////////////////////////////////////////////////////////////////////////
260 SelectAction::SelectAction( const QString &text, void ( *f ) ( int ), KActionCollection* const ac, const char *name )
261 : KSelectAction( 0 )
262 , m_function( f )
264 setText(text);
265 ac->addAction(name, this);
268 void SelectAction::setCurrentItem( int n )
270 const bool announce = n != currentItem();
272 m_function( n );
273 KSelectAction::setCurrentItem( n );
274 AmarokConfig::self()->writeConfig(); //So we don't lose the setting when crashing
275 if( announce ) emit triggered( n );
278 void SelectAction::setEnabled( bool b )
280 const bool announce = b != isEnabled();
282 if( !b )
283 setCurrentItem( 0 );
284 KSelectAction::setEnabled( b );
285 AmarokConfig::self()->writeConfig(); //So we don't lose the setting when crashing
286 if( announce ) emit QAction::triggered( b );
289 void SelectAction::setIcons( QStringList icons )
291 m_icons = icons;
292 for( int i = 0, n = items().count(); i < n; ++i )
293 menu()->changeItem( i, KIconLoader::global()->loadIconSet( icons.at( i ), KIconLoader::Small ), menu()->text( i ) );
296 QStringList SelectAction::icons() const { return m_icons; }
298 QString SelectAction::currentIcon() const
300 if( m_icons.count() )
301 return m_icons.at( currentItem() );
302 return QString();
305 QString SelectAction::currentText() const {
306 return KSelectAction::currentText() + "<br /><br />" + i18n("Click to change");
309 //////////////////////////////////////////////////////////////////////////////////////////
310 // RandomAction
311 //////////////////////////////////////////////////////////////////////////////////////////
312 RandomAction::RandomAction( KActionCollection *ac ) :
313 SelectAction( i18n( "Ra&ndom" ), &AmarokConfig::setRandomMode, ac, "random_mode" )
315 setItems( QStringList() << i18n( "&Off" ) << i18n( "&Tracks" ) << i18n( "&Albums" ) );
316 setCurrentItem( AmarokConfig::randomMode() );
317 setIcons( QStringList() << Amarok::icon( "random_no" ) << Amarok::icon( "random_track" ) << Amarok::icon( "random_album" ) );
320 void
321 RandomAction::setCurrentItem( int n )
323 // Porting
324 //if( KAction *a = parentCollection()->action( "favor_tracks" ) )
325 // a->setEnabled( n );
326 SelectAction::setCurrentItem( n );
330 //////////////////////////////////////////////////////////////////////////////////////////
331 // FavorAction
332 //////////////////////////////////////////////////////////////////////////////////////////
333 FavorAction::FavorAction( KActionCollection *ac ) :
334 SelectAction( i18n( "&Favor" ), &AmarokConfig::setFavorTracks, ac, "favor_tracks" )
336 setItems( QStringList() << i18n( "Off" )
337 << i18n( "Higher &Scores" )
338 << i18n( "Higher &Ratings" )
339 << i18n( "Not Recently &Played" ) );
341 setCurrentItem( AmarokConfig::favorTracks() );
342 setEnabled( AmarokConfig::randomMode() );
345 //////////////////////////////////////////////////////////////////////////////////////////
346 // RepeatAction
347 //////////////////////////////////////////////////////////////////////////////////////////
348 RepeatAction::RepeatAction( KActionCollection *ac ) :
349 SelectAction( i18n( "&Repeat" ), &AmarokConfig::setRepeat, ac, "repeat" )
351 setItems( QStringList() << i18n( "&Off" ) << i18n( "&Track" )
352 << i18n( "&Album" ) << i18n( "&Playlist" ) );
353 setIcons( QStringList() << Amarok::icon( "repeat_no" ) << Amarok::icon( "repeat_track" ) << Amarok::icon( "repeat_album" ) << Amarok::icon( "repeat_playlist" ) );
354 setCurrentItem( AmarokConfig::repeat() );
357 //////////////////////////////////////////////////////////////////////////////////////////
358 // BurnMenuAction
359 //////////////////////////////////////////////////////////////////////////////////////////
360 BurnMenuAction::BurnMenuAction( KActionCollection *ac )
361 : KAction( 0 )
363 setText(i18n( "Burn" ));
364 ac->addAction("burn_menu", this);
367 QWidget*
368 BurnMenuAction::createWidget( QWidget *w )
370 KToolBar *bar = dynamic_cast<KToolBar*>(w);
372 if( bar && KAuthorized::authorizeKAction( objectName() ) )
374 //const int id = KAction::getToolButtonID();
376 //addContainer( bar, id );
377 w->addAction( this );
378 connect( bar, SIGNAL( destroyed() ), SLOT( slotDestroyed() ) );
380 //bar->insertButton( QString::null, id, true, i18n( "Burn" ), index );
382 //KToolBarButton* button = bar->getButton( id );
383 //button->setPopup( Amarok::BurnMenu::instance() );
384 //button->setObjectName( "toolbutton_burn_menu" );
385 //button->setIcon( "k3b" );
387 //return associatedWidgets().count() - 1;
388 return 0;
390 //else return -1;
391 else return 0;
394 BurnMenu::BurnMenu()
396 addAction( i18n("Current Playlist"), this, SLOT( slotBurnCurrentPlaylist() ) );
397 addAction( i18n("Selected Tracks"), this, SLOT( slotBurnSelectedTracks() ) );
398 //TODO add "album" and "all tracks by artist"
401 KMenu*
402 BurnMenu::instance()
404 static BurnMenu menu;
405 return &menu;
408 void
409 BurnMenu::slotBurnCurrentPlaylist() //SLOT
411 K3bExporter::instance()->exportCurrentPlaylist();
414 void
415 BurnMenu::slotBurnSelectedTracks() //SLOT
417 K3bExporter::instance()->exportSelectedTracks();
421 //////////////////////////////////////////////////////////////////////////////////////////
422 // StopMenuAction
423 //////////////////////////////////////////////////////////////////////////////////////////
425 StopAction::StopAction( KActionCollection *ac )
426 : KAction( 0 )
428 setText( i18n( "Stop" ) );
429 setIcon( KIcon(Amarok::icon( "stop" )) );
430 connect( this, SIGNAL( triggered() ), EngineController::instance(), SLOT( stop() ) );
431 ac->addAction( "stop", this );
435 StopAction::plug( QWidget *w, int )
437 //KToolBar *bar = dynamic_cast<KToolBar*>(w);
438 w->addAction( this );
440 if( bar && KAuthorized::authorizeKAction( name() ) )
442 //const int id = KAction::getToolButtonID();
444 //addContainer( bar, id );
446 w->addAction( this );
447 connect( bar, SIGNAL( destroyed() ), SLOT( slotDestroyed() ) );
449 //bar->insertButton( QString::null, id, SIGNAL( clicked() ), EngineController::instance(), SLOT( stop() ),
450 // true, i18n( "Stop" ), index );
452 //KToolBarButton* button = bar->getButton( id );
453 //button->setDelayedPopup( Amarok::StopMenu::instance() );
454 //button->setObjectName( "toolbutton_stop_menu" );
455 //button->setIcon( Amarok::icon( "stop" ) );
456 //button->setEnabled( EngineController::instance()->engine()->loaded() ); // Disable button at startup
458 return associatedWidgets().count() - 1;
460 else return QAction::plug( w, index );
462 return 1;
465 StopMenu::StopMenu()
467 addTitle( i18n( "Stop" ) );
469 m_stopNow = addAction( i18n( "Now" ), this, SLOT( slotStopNow() ) );
470 m_stopAfterTrack = addAction( i18n( "After Current Track" ), this, SLOT( slotStopAfterTrack() ) );
471 m_stopAfterQueue = addAction( i18n( "After Queue" ), this, SLOT( slotStopAfterQueue() ) );
473 connect( this, SIGNAL( aboutToShow() ), SLOT( slotAboutToShow() ) );
474 connect( this, SIGNAL( activated(int) ), SLOT( slotActivated(int) ) );
477 KMenu*
478 StopMenu::instance()
480 static StopMenu menu;
481 return &menu;
484 void
485 StopMenu::slotAboutToShow()
487 //PORT 2.0
488 // Playlist *pl = Playlist::instance();
490 m_stopNow->setEnabled( Amarok::actionCollection()->action( "stop" )->isEnabled() );
492 m_stopAfterTrack->setEnabled( EngineController::engine()->loaded() );
493 // m_stopAfterTrack->setChecked( pl->stopAfterMode() == Playlist::StopAfterCurrent );
495 // m_stopAfterQueue->setEnabled( pl->nextTracks().count() );
496 // m_stopAfterQueue->setChecked( pl->stopAfterMode() == Playlist::StopAfterQueue );
499 void
500 StopMenu::slotStopNow() //SLOT
502 //PORT 2.0
503 // Playlist* pl = Playlist::instance();
504 // const int mode = pl->stopAfterMode();
506 // Amarok::actionCollection()->action( "stop" )->trigger();
507 // if( mode == Playlist::StopAfterCurrent || mode == Playlist::StopAfterQueue )
508 // pl->setStopAfterMode( Playlist::DoNotStop );
511 void
512 StopMenu::slotStopAfterTrack() //SLOT
514 //PORT 2.0
515 // Playlist* pl = Playlist::instance();
516 // const int mode = pl->stopAfterMode();
518 // pl->setStopAfterMode( mode == Playlist::StopAfterCurrent
519 // ? Playlist::DoNotStop
520 // : Playlist::StopAfterCurrent );
523 void
524 StopMenu::slotStopAfterQueue() //SLOT
526 //PORT 2.0
527 // Playlist* pl = Playlist::instance();
528 // const int mode = pl->stopAfterMode();
530 // pl->setStopAfterMode( mode == Playlist::StopAfterQueue
531 // ? Playlist::DoNotStop
532 // : Playlist::StopAfterQueue );
535 #include "actionclasses.moc"