File indexing completed on 2024-05-05 04:48:36

0001 /****************************************************************************************
0002  * Copyright (c) 2013 Anmol Ahuja <darthcodus@gmail.com>                                *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #define DEBUG_PREFIX "PowerManager"
0018 
0019 #include "PowerManager.h"
0020 
0021 #include "amarokconfig.h"
0022 #include "App.h"
0023 #include "Debug.h"
0024 #include "EngineController.h"
0025 
0026 #include <QAction>
0027 #include <QDBusConnection>
0028 #include <QDBusInterface>
0029 
0030 #include <KLocalizedString>
0031 
0032 PowerManager::PowerManager( EngineController *engine )
0033     : QObject( engine )
0034     , m_inhibitionCookie( -1 )
0035 {
0036     connect( engine, &EngineController::stopped, this, &PowerManager::slotNotPlaying );
0037     connect( engine, &EngineController::paused, this, &PowerManager::slotNotPlaying );
0038     connect( engine, &EngineController::trackPlaying, this, &PowerManager::slotPlaying );
0039     connect( pApp, &App::settingsChanged, this, &PowerManager::slotSettingsChanged );
0040 
0041     // TODO: Port this to the new Solid API when that is ready
0042     QDBusConnection::systemBus().connect( QStringLiteral("org.freedesktop.login1"),
0043                                           QStringLiteral("/org/freedesktop/login1"),
0044                                           QStringLiteral("org.freedesktop.login1.Manager"),
0045                                           QStringLiteral("PrepareForSleep"),
0046                                           this, SLOT( slotHandleSuspend() ) );
0047 }
0048 
0049 PowerManager::~PowerManager()
0050 {
0051     stopInhibitingSuspend();
0052 }
0053 
0054 void
0055 PowerManager::slotNotPlaying()
0056 {
0057     stopInhibitingSuspend();
0058 }
0059 
0060 void
0061 PowerManager::slotPlaying()
0062 {
0063     if( AmarokConfig::inhibitSuspend() )
0064         startInhibitingSuspend();
0065 }
0066 
0067 void
0068 PowerManager::slotHandleSuspend()
0069 {
0070     DEBUG_BLOCK
0071 
0072     if( AmarokConfig::pauseOnSuspend() && The::engineController()->isPlaying() )
0073         The::engineController()->playPause();
0074 }
0075 
0076 void
0077 PowerManager::slotSettingsChanged()
0078 {
0079     if( AmarokConfig::inhibitSuspend() && The::engineController()->isPlaying() )
0080         startInhibitingSuspend();
0081     else
0082         stopInhibitingSuspend();
0083 }
0084 
0085 void
0086 PowerManager::startInhibitingSuspend()
0087 {
0088     // TODO: Port this to the new Solid API when that is ready
0089 //     if( m_inhibitionCookie == -1 )
0090 //         m_inhibitionCookie = Solid::PowerManagement::beginSuppressingSleep( i18n( "Amarok is currently playing a track" ) );
0091 }
0092 
0093 void
0094 PowerManager::stopInhibitingSuspend()
0095 {
0096     if( m_inhibitionCookie != -1 )
0097     {
0098         // TODO: Port this to the new Solid API when that is ready
0099 //         Solid::PowerManagement::stopSuppressingSleep( m_inhibitionCookie );
0100 //         m_inhibitionCookie = -1;
0101     }
0102 }