PageRenderTime 267ms CodeModel.GetById 81ms app.highlight 113ms RepoModel.GetById 68ms app.codeStats 0ms

/src/libtomahawk/ActionCollection.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 361 lines | 272 code | 54 blank | 35 comment | 11 complexity | b977b00b610e77d38624a8b067934569 MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4 *   Copyright 2010-2012, Jeff Mitchell <jeff@tomahawk-player.org>
  5 *   Copyright 2012,      Leo Franchi   <lfranchi@kde.org>
  6 *   Copyright 2012,      Teo Mrnjavac   <teo@kde.org>
  7 *
  8 *   Tomahawk is free software: you can redistribute it and/or modify
  9 *   it under the terms of the GNU General Public License as published by
 10 *   the Free Software Foundation, either version 3 of the License, or
 11 *   (at your option) any later version.
 12 *
 13 *   Tomahawk is distributed in the hope that it will be useful,
 14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 16 *   GNU General Public License for more details.
 17 *
 18 *   You should have received a copy of the GNU General Public License
 19 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 20 */
 21
 22#include "ActionCollection.h"
 23
 24#include "TomahawkSettings.h"
 25#include "Source.h"
 26#include "audio/AudioEngine.h"
 27#include "utils/ImageRegistry.h"
 28#include "utils/TomahawkUtils.h"
 29#include "utils/Logger.h"
 30
 31#include <QCoreApplication>
 32
 33
 34ActionCollection* ActionCollection::s_instance = 0;
 35ActionCollection* ActionCollection::instance()
 36{
 37    return s_instance;
 38}
 39
 40
 41ActionCollection::ActionCollection( QObject* parent )
 42    : QObject( parent )
 43{
 44    s_instance = this;
 45    initActions();
 46}
 47
 48
 49ActionCollection::~ActionCollection()
 50{
 51    s_instance = 0;
 52    foreach( QString key, m_actionCollection.keys() )
 53        delete m_actionCollection[ key ];
 54}
 55
 56
 57void
 58ActionCollection::initActions()
 59{
 60    // ATTENTION: Don't set ApplicationSpecificRole for submenu actions: they won't show up on OS X (Qt 5.5)
 61
 62    QAction *latchOn = new QAction( tr( "&Listen Along" ), this );
 63    latchOn->setIcon( ImageRegistry::instance()->icon( RESPATH "images/headphones.svg" ) );
 64    m_actionCollection[ "latchOn" ] = latchOn;
 65    QAction *latchOff = new QAction( tr( "Stop &Listening Along" ), this );
 66    latchOff->setIcon( ImageRegistry::instance()->icon( RESPATH "images/headphones-off.svg" ) );
 67    m_actionCollection[ "latchOff" ] = latchOff;
 68
 69    QAction *realtimeFollowingAlong = new QAction( tr( "&Follow in Real-Time" ), this );
 70    realtimeFollowingAlong->setCheckable( true );
 71    m_actionCollection[ "realtimeFollowingAlong" ] = realtimeFollowingAlong;
 72
 73    bool isPublic = TomahawkSettings::instance()->privateListeningMode() == TomahawkSettings::PublicListening;
 74    m_actionCollection[ "togglePrivacy" ] = new QAction( tr( "&Listen Privately" ) , this );
 75    m_actionCollection[ "togglePrivacy" ]->setCheckable( true );
 76    m_actionCollection[ "togglePrivacy" ]->setChecked( !isPublic );
 77    connect( m_actionCollection[ "togglePrivacy" ], SIGNAL( triggered() ), SLOT( togglePrivateListeningMode() ), Qt::UniqueConnection );
 78
 79    m_actionCollection[ "loadPlaylist" ] =   new QAction( tr( "&Load Playlist" ), this );
 80    m_actionCollection[ "loadStation" ] =    new QAction( tr( "&Load Station" ), this );
 81    m_actionCollection[ "renamePlaylist" ] = new QAction( tr( "&Rename Playlist" ), this );
 82    m_actionCollection[ "renameStation" ] = new QAction( tr( "&Rename Station" ), this );
 83    m_actionCollection[ "copyPlaylist" ] =   new QAction( tr( "&Copy Playlist Link" ), this );
 84    m_actionCollection[ "playPause" ] =      new QAction( tr( "&Play" ), this );
 85    m_actionCollection[ "playPause" ]->setIcon( ImageRegistry::instance()->icon( RESPATH "images/play.svg" ) );
 86    m_actionCollection[ "playPause" ]->setShortcut( Qt::Key_Space );
 87    m_actionCollection[ "playPause" ]->setShortcutContext( Qt::ApplicationShortcut );
 88    m_actionCollection[ "stop" ] =           new QAction( tr( "&Stop" ), this );
 89    m_actionCollection[ "previousTrack" ] =  new QAction( tr( "&Previous Track" ), this );
 90    m_actionCollection[ "previousTrack" ]->setIcon( ImageRegistry::instance()->icon( RESPATH "images/back.svg" ) );
 91//    m_actionCollection[ "previousTrack" ]->setShortcut( QKeySequence( "Left" ) );
 92    m_actionCollection[ "nextTrack" ] =      new QAction( tr( "&Next Track" ), this );
 93    m_actionCollection[ "nextTrack" ]->setIcon( ImageRegistry::instance()->icon( RESPATH "images/forward.svg" ) );
 94//    m_actionCollection[ "nextTrack" ]->setShortcut( QKeySequence( "Right" ) );
 95    m_actionCollection[ "quit" ] =           new QAction( tr( "&Quit" ), this );
 96    m_actionCollection[ "quit" ]->setShortcut( QKeySequence::Quit );
 97    m_actionCollection[ "quit" ]->setShortcutContext( Qt::ApplicationShortcut );
 98    m_actionCollection[ "quit" ]->setMenuRole( QAction::QuitRole );
 99
100    // connect actions to AudioEngine
101    AudioEngine *ae = AudioEngine::instance();
102    connect( m_actionCollection[ "playPause" ],     SIGNAL( triggered() ), ae,   SLOT( playPause() ), Qt::UniqueConnection );
103    connect( m_actionCollection[ "stop" ],          SIGNAL( triggered() ), ae,   SLOT( stop() ),      Qt::UniqueConnection );
104    connect( m_actionCollection[ "previousTrack" ], SIGNAL( triggered() ), ae,   SLOT( previous() ),  Qt::UniqueConnection );
105    connect( m_actionCollection[ "nextTrack" ],     SIGNAL( triggered() ), ae,   SLOT( next() ),      Qt::UniqueConnection );
106
107    // main menu actions
108    m_actionCollection[ "importPlaylist" ] = new QAction( tr( "Import Playlist..." ), this );
109    m_actionCollection[ "updateCollection" ] = new QAction( tr( "U&pdate Collection" ), this );
110    m_actionCollection[ "rescanCollection" ] = new QAction( tr( "Fully &Rescan Collection" ), this );
111    m_actionCollection[ "showOfflineSources" ] = new QAction( tr( "Show Offline Friends" ), this );
112    m_actionCollection[ "showOfflineSources" ]->setCheckable( true );
113    m_actionCollection[ "preferences" ] = new QAction( tr( "&Configure %applicationName..." ), this );
114    m_actionCollection[ "preferences" ]->setMenuRole( QAction::PreferencesRole );
115    m_actionCollection[ "createPlaylist" ] = new QAction( tr( "Create Playlist" ), this );
116    m_actionCollection[ "createPlaylist" ]->setShortcut( QKeySequence( "Ctrl+N" ) );
117    m_actionCollection[ "createPlaylist" ]->setShortcutContext( Qt::ApplicationShortcut );
118    // echonest is dead, disable stations
119    /*
120    m_actionCollection[ "createStation" ] = new QAction( tr( "Create Station" ), this );
121    m_actionCollection[ "createStation" ]->setShortcut( QKeySequence( "Ctrl+S" ) );
122    m_actionCollection[ "createStation" ]->setShortcutContext( Qt::ApplicationShortcut );
123    */
124#ifdef Q_OS_MAC
125    m_actionCollection[ "minimize" ] = new QAction( tr( "Minimize" ), this );
126    m_actionCollection[ "minimize" ]->setShortcut( QKeySequence( "Ctrl+M" ) );
127    m_actionCollection[ "zoom" ] = new QAction( tr( "Zoom" ), this );
128    m_actionCollection[ "zoom" ]->setShortcut( QKeySequence( "Meta+Ctrl+Z" ) );
129    m_actionCollection[ "fullscreen" ] = new QAction( tr( "Enter Full Screen" ), this );
130    m_actionCollection[ "fullscreen" ]->setShortcut( QKeySequence( "Meta+Ctrl+F" ) );
131#else
132    m_actionCollection[ "toggleMenuBar" ] = new QAction( tr( "Hide Menu Bar" ), this );
133    m_actionCollection[ "toggleMenuBar" ]->setShortcut( QKeySequence( "Ctrl+M" ) );
134    m_actionCollection[ "toggleMenuBar" ]->setShortcutContext( Qt::ApplicationShortcut );
135#endif
136    m_actionCollection[ "diagnostics" ] = new QAction( tr( "Diagnostics..." ), this );
137    m_actionCollection[ "diagnostics" ]->setMenuRole( QAction::ApplicationSpecificRole );
138    m_actionCollection[ "aboutTomahawk" ] = new QAction( tr( "About &%applicationName..." ), this );
139    m_actionCollection[ "aboutTomahawk" ]->setMenuRole( QAction::AboutRole );
140    m_actionCollection[ "legalInfo" ] = new QAction( tr( "&Legal Information..." ), this );
141    m_actionCollection[ "legalInfo" ]->setMenuRole( QAction::ApplicationSpecificRole );
142    m_actionCollection[ "openLogfile" ] = new QAction( tr( "&View Logfile" ), this );
143    m_actionCollection[ "openLogfile" ]->setMenuRole( QAction::ApplicationSpecificRole );
144    #if defined( Q_OS_MAC ) && defined( HAVE_SPARKLE ) || defined( Q_OS_WIN )
145    m_actionCollection[ "checkForUpdates" ] = new QAction( tr( "Check For Updates..." ), this );
146    m_actionCollection[ "checkForUpdates" ]->setMenuRole( QAction::ApplicationSpecificRole );
147#endif
148    m_actionCollection[ "crashNow" ] = new QAction( "Crash now...", this );
149    m_actionCollection[ "whatsnew_0_8" ] = new QAction( tr( "%applicationName 0.8" ) , this );
150    m_actionCollection[ "reportBug" ] = new QAction( tr( "Report a Bug" ) , this );
151    m_actionCollection[ "getSupport" ] = new QAction( tr( "Get Support" ) , this );
152    m_actionCollection[ "helpTranslate" ] = new QAction( tr( "Help Us Translate" ) , this );
153}
154
155
156QMenuBar*
157ActionCollection::createMenuBar( QWidget *parent )
158{
159    QMenuBar* menuBar = new QMenuBar( parent );
160    menuBar->setFont( TomahawkUtils::systemFont() );
161
162    QMenu* controlsMenu = new QMenu( tr( "&Controls" ), menuBar );
163    controlsMenu->setFont( TomahawkUtils::systemFont() );
164    controlsMenu->addAction( m_actionCollection[ "playPause" ] );
165    controlsMenu->addAction( m_actionCollection[ "previousTrack" ] );
166    controlsMenu->addAction( m_actionCollection[ "nextTrack" ] );
167    controlsMenu->addSeparator();
168    controlsMenu->addAction( m_actionCollection[ "togglePrivacy" ] );
169    controlsMenu->addAction( m_actionCollection[ "showOfflineSources" ] );
170    controlsMenu->addSeparator();
171    controlsMenu->addAction( m_actionCollection[ "createPlaylist" ] );
172    // echonest is dead, disable stations
173    // controlsMenu->addAction( m_actionCollection[ "createStation" ] );
174    controlsMenu->addAction( m_actionCollection[ "importPlaylist" ] );
175    controlsMenu->addAction( m_actionCollection[ "updateCollection" ] );
176    controlsMenu->addAction( m_actionCollection[ "rescanCollection" ] );
177    controlsMenu->addSeparator();
178    controlsMenu->addAction( m_actionCollection[ "quit" ] );
179
180    QMenu* settingsMenu = new QMenu( tr( "&Settings" ), menuBar );
181    settingsMenu->setFont( TomahawkUtils::systemFont() );
182#ifndef Q_OS_MAC
183    settingsMenu->addAction( m_actionCollection[ "toggleMenuBar" ] );
184#endif
185    settingsMenu->addAction( m_actionCollection[ "preferences" ] );
186
187    QMenu* helpMenu = new QMenu( tr( "&Help" ), menuBar );
188    helpMenu->setFont( TomahawkUtils::systemFont() );
189    helpMenu->addAction( m_actionCollection[ "diagnostics" ] );
190    helpMenu->addAction( m_actionCollection[ "openLogfile" ] );
191    helpMenu->addAction( m_actionCollection[ "legalInfo" ] );
192    helpMenu->addAction( m_actionCollection["getSupport"] );
193    helpMenu->addAction( m_actionCollection["reportBug"] );
194    helpMenu->addAction( m_actionCollection["helpTranslate"] );
195    helpMenu->addSeparator();
196    QMenu* whatsNew = helpMenu->addMenu( ImageRegistry::instance()->icon( RESPATH "images/whatsnew.svg" ), tr( "What's New in ..." ) );
197    whatsNew->setFont( TomahawkUtils::systemFont() );
198    whatsNew->addAction( m_actionCollection[ "whatsnew_0_8" ] );
199    helpMenu->addAction( m_actionCollection[ "aboutTomahawk" ] );
200
201    // Setup update check
202#ifndef Q_OS_MAC
203    helpMenu->insertSeparator( m_actionCollection[ "legalInfo" ] );
204#endif
205
206#if defined( Q_OS_MAC ) && defined( HAVE_SPARKLE )
207    helpMenu->addAction( m_actionCollection[ "checkForUpdates" ] );
208#elif defined( Q_OS_WIN )
209    helpMenu->addSeparator();
210    helpMenu->addAction( m_actionCollection[ "checkForUpdates" ] );
211#endif
212    if ( qApp->arguments().contains( "--debug" ) )
213    {
214        helpMenu->addSeparator();
215        helpMenu->addAction( m_actionCollection[ "crashNow" ] );
216    }
217
218    menuBar->addMenu( controlsMenu );
219    menuBar->addMenu( settingsMenu );
220
221#if defined( Q_OS_MAC )
222    QMenu* windowMenu = new QMenu( tr( "&Window" ), menuBar );
223    windowMenu->setFont( TomahawkUtils::systemFont() );
224    windowMenu->addAction( m_actionCollection[ "minimize" ] );
225    windowMenu->addAction( m_actionCollection[ "zoom" ] );
226    windowMenu->addAction( m_actionCollection[ "fullscreen" ] );
227
228    menuBar->addMenu( windowMenu );
229#endif
230
231    menuBar->addMenu( helpMenu );
232    return menuBar;
233}
234
235
236QMenu*
237ActionCollection::createCompactMenu( QWidget* parent )
238{
239    QMenu* compactMenu = new QMenu( tr( "Main Menu" ), parent );
240    compactMenu->setFont( TomahawkUtils::systemFont() );
241
242    compactMenu->addAction( m_actionCollection[ "playPause" ] );
243    compactMenu->addAction( m_actionCollection[ "previousTrack" ] );
244    compactMenu->addAction( m_actionCollection[ "nextTrack" ] );
245    compactMenu->addSeparator();
246    compactMenu->addAction( m_actionCollection[ "togglePrivacy" ] );
247    compactMenu->addAction( m_actionCollection[ "showOfflineSources" ] );
248    compactMenu->addSeparator();
249    compactMenu->addAction( m_actionCollection[ "importPlaylist" ] );
250    compactMenu->addAction( m_actionCollection[ "updateCollection" ] );
251    compactMenu->addAction( m_actionCollection[ "rescanCollection" ] );
252    compactMenu->addSeparator();
253
254#ifdef Q_OS_MAC // This should never happen anyway
255    compactMenu->addAction( m_actionCollection[ "minimize" ] );
256    compactMenu->addAction( m_actionCollection[ "zoom" ] );
257#else
258    compactMenu->addAction( m_actionCollection[ "toggleMenuBar" ] );
259#endif
260    compactMenu->addAction( m_actionCollection[ "preferences" ] );
261    compactMenu->addSeparator();
262
263    compactMenu->addAction( m_actionCollection[ "diagnostics" ] );
264    compactMenu->addAction( m_actionCollection[ "openLogfile" ] );
265    compactMenu->addAction( m_actionCollection[ "legalInfo" ] );
266    QMenu* whatsNew = compactMenu->addMenu( ImageRegistry::instance()->icon( RESPATH "images/whatsnew.svg" ), tr( "What's New in ..." ) );
267    whatsNew->addAction( m_actionCollection[ "whatsnew_0_8" ] );
268    compactMenu->addAction( m_actionCollection[ "aboutTomahawk" ] );
269
270    // Setup update check
271#ifndef Q_OS_MAC
272    compactMenu->insertSeparator( m_actionCollection[ "legalInfo" ] );
273#endif
274
275#if defined( Q_OS_MAC ) && defined( HAVE_SPARKLE )
276    compactMenu->addAction( m_actionCollection[ "checkForUpdates" ] );
277#elif defined( Q_OS_WIN )
278    compactMenu->addSeparator();
279    compactMenu->addAction( m_actionCollection[ "checkForUpdates" ] );
280#endif
281    if ( qApp->arguments().contains( "--debug" ) )
282    {
283        compactMenu->addSeparator();
284        compactMenu->addAction( m_actionCollection[ "crashNow" ] );
285    }
286    compactMenu->addSeparator();
287    compactMenu->addAction( m_actionCollection["getSupport"] );
288    compactMenu->addAction( m_actionCollection["reportBug"] );
289    compactMenu->addAction( m_actionCollection["helpTranslate"] );
290    compactMenu->addSeparator();
291    compactMenu->addAction( m_actionCollection[ "quit" ] );
292
293    return compactMenu;
294}
295
296
297void
298ActionCollection::addAction( ActionCollection::ActionDestination category, QAction* action, QObject* notify )
299{
300    QList< QAction* > actions = m_categoryActions.value( category );
301    actions.append( action );
302    m_categoryActions[ category ] = actions;
303
304    if ( notify )
305        m_actionNotifiers[ action ] = notify;
306}
307
308
309QAction*
310ActionCollection::getAction( const QString& name )
311{
312    return m_actionCollection.value( name, 0 );
313}
314
315
316QObject*
317ActionCollection::actionNotifier( QAction* action )
318{
319    return m_actionNotifiers.value( action, 0 );
320}
321
322
323QList< QAction* >
324ActionCollection::getAction( ActionCollection::ActionDestination category )
325{
326    return m_categoryActions.value( category );
327}
328
329
330void
331ActionCollection::removeAction( QAction* action )
332{
333    removeAction( action, LocalPlaylists );
334}
335
336
337void
338ActionCollection::removeAction( QAction* action, ActionCollection::ActionDestination category )
339{
340    QList< QAction* > actions = m_categoryActions.value( category );
341    actions.removeAll( action );
342    m_categoryActions[ category ] = actions;
343
344    m_actionNotifiers.remove( action );
345}
346
347
348void
349ActionCollection::togglePrivateListeningMode()
350{
351    tDebug() << Q_FUNC_INFO;
352    if ( TomahawkSettings::instance()->privateListeningMode() == TomahawkSettings::PublicListening )
353        TomahawkSettings::instance()->setPrivateListeningMode( TomahawkSettings::FullyPrivate );
354    else
355        TomahawkSettings::instance()->setPrivateListeningMode( TomahawkSettings::PublicListening );
356
357    bool isPublic = TomahawkSettings::instance()->privateListeningMode() == TomahawkSettings::PublicListening;
358    m_actionCollection[ "togglePrivacy" ]->setChecked( !isPublic );
359
360    emit privacyModeChanged();
361}