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
9 #include "amarokconfig.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"
26 #include <KIconLoader>
29 #include <KPushButton>
30 #include <KStandardDirs>
33 #include <KAuthorized>
37 #include <Q3PopupMenu>
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;
61 safePlug( KActionCollection
*ac
, const char *name
, QWidget
*w
)
65 KAction
*a
= (KAction
*) ac
->action( name
);
66 if( a
&& w
) w
->addAction( a
);
71 //////////////////////////////////////////////////////////////////////////////////////////
73 // KActionMenu doesn't work very well, so we derived our own
74 //////////////////////////////////////////////////////////////////////////////////////////
76 MenuAction::MenuAction( KActionCollection
*ac
)
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()
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 );
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;
115 KActionCollection
*ac
= Amarok::actionCollection();
117 safePlug( ac
, "repeat", this );
118 safePlug( ac
, "random_mode", this );
122 safePlug( ac
, "playlist_playmedia", this );
123 safePlug( ac
, "play_audiocd", this );
124 safePlug( ac
, "lastfm_play", this );
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 );
137 safePlug( ac
, "update_collection", this );
138 safePlug( ac
, "rescan_collection", this );
143 safePlug( ac
, KStandardAction::name(KStandardAction::ShowMenubar
), this );
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 );
155 addMenu( helpMenu( this ) );
159 safePlug( ac
, KStandardAction::name(KStandardAction::Quit
), this );
161 #ifdef HAVE_LIBVISUAL
162 Amarok::actionCollection()->action( "visualizations" )->setEnabled( false );
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 //////////////////////////////////////////////////////////////////////////////////////////
186 //////////////////////////////////////////////////////////////////////////////////////////
188 PlayPauseAction::PlayPauseAction( KActionCollection
*ac
)
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()) );
201 PlayPauseAction::engineStateChanged( Engine::State state
, Engine::State
/*oldState*/ )
204 case Engine::Playing
:
206 setIcon( KIcon(Amarok::icon( "pause" )) );
207 setText( i18n( "Pause" ) );
211 setIcon( KIcon(Amarok::icon( "pause" )) );
212 setText( i18n( "Pause" ) );
216 setIcon( KIcon(Amarok::icon( "play" )) );
217 setText( i18n( "Play" ) );
223 //////////////////////////////////////////////////////////////////////////////////////////
225 //////////////////////////////////////////////////////////////////////////////////////////
227 ToggleAction::ToggleAction( const QString
&text
, void ( *f
) ( bool ), KActionCollection
* const ac
, const char *name
)
232 ac
->addAction(name
, this);
235 void ToggleAction::setChecked( bool b
)
237 const bool announce
= b
!= isChecked();
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();
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 //////////////////////////////////////////////////////////////////////////////////////////
258 //////////////////////////////////////////////////////////////////////////////////////////
260 SelectAction::SelectAction( const QString
&text
, void ( *f
) ( int ), KActionCollection
* const ac
, const char *name
)
265 ac
->addAction(name
, this);
268 void SelectAction::setCurrentItem( int n
)
270 const bool announce
= n
!= currentItem();
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();
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
)
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() );
305 QString
SelectAction::currentText() const {
306 return KSelectAction::currentText() + "<br /><br />" + i18n("Click to change");
309 //////////////////////////////////////////////////////////////////////////////////////////
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" ) );
321 RandomAction::setCurrentItem( int n
)
324 //if( KAction *a = parentCollection()->action( "favor_tracks" ) )
325 // a->setEnabled( n );
326 SelectAction::setCurrentItem( n
);
330 //////////////////////////////////////////////////////////////////////////////////////////
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 //////////////////////////////////////////////////////////////////////////////////////////
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 //////////////////////////////////////////////////////////////////////////////////////////
359 //////////////////////////////////////////////////////////////////////////////////////////
360 BurnMenuAction::BurnMenuAction( KActionCollection
*ac
)
363 setText(i18n( "Burn" ));
364 ac
->addAction("burn_menu", this);
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;
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"
404 static BurnMenu menu
;
409 BurnMenu::slotBurnCurrentPlaylist() //SLOT
411 K3bExporter::instance()->exportCurrentPlaylist();
415 BurnMenu::slotBurnSelectedTracks() //SLOT
417 K3bExporter::instance()->exportSelectedTracks();
421 //////////////////////////////////////////////////////////////////////////////////////////
423 //////////////////////////////////////////////////////////////////////////////////////////
425 StopAction::StopAction( KActionCollection
*ac
)
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 );
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) ) );
480 static StopMenu menu
;
485 StopMenu::slotAboutToShow()
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 );
500 StopMenu::slotStopNow() //SLOT
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 );
512 StopMenu::slotStopAfterTrack() //SLOT
515 // Playlist* pl = Playlist::instance();
516 // const int mode = pl->stopAfterMode();
518 // pl->setStopAfterMode( mode == Playlist::StopAfterCurrent
519 // ? Playlist::DoNotStop
520 // : Playlist::StopAfterCurrent );
524 StopMenu::slotStopAfterQueue() //SLOT
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"