1
/***************************************************************************
2
smb4ksolidinterface - This class provides an interface to KDE's
6
copyright : (C) 2008 by Alexander Reinholdt
7
email : dustpuppy@users.berlios.de
8
***************************************************************************/
10
/***************************************************************************
11
* This program is free software; you can redistribute it and/or modify *
12
* it under the terms of the GNU General Public License as published by *
13
* the Free Software Foundation; either version 2 of the License, or *
14
* (at your option) any later version. *
16
* This program is distributed in the hope that it will be useful, but *
17
* WITHOUT ANY WARRANTY; without even the implied warranty of *
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
19
* General Public License for more details. *
21
* You should have received a copy of the GNU General Public License *
22
* along with this program; if not, write to the *
23
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
25
***************************************************************************/
31
#include <solid/deviceinterface.h>
32
#include <solid/devicenotifier.h>
33
#include <solid/device.h>
34
#include <solid/acadapter.h>
35
#include <solid/battery.h>
36
#include <solid/powermanagement.h>
38
// application specific includes
39
#include <smb4ksolidinterface.h>
41
class Smb4KSolidInterfacePrivate
44
Smb4KSolidInterface instance;
47
K_GLOBAL_STATIC( Smb4KSolidInterfacePrivate, priv );
50
Smb4KSolidInterface::Smb4KSolidInterface()
52
m_button_pressed( UnknownButton ),
53
m_network_status( Unknown )
59
Smb4KSolidInterface::~Smb4KSolidInterface()
64
Smb4KSolidInterface *Smb4KSolidInterface::self()
66
return &priv->instance;
70
void Smb4KSolidInterface::init()
72
// Connect to device notifier.
73
connect( Solid::DeviceNotifier::instance(), SIGNAL( deviceAdded( const QString & ) ),
74
this, SLOT( slotDeviceAdded( const QString & ) ) );
76
connect( Solid::DeviceNotifier::instance(), SIGNAL( deviceRemoved( const QString & ) ),
77
this, SLOT( slotDeviceRemoved( const QString & ) ) );
80
QList<Solid::Device> list_btn = Solid::Device::listFromType( Solid::DeviceInterface::Button, QString() );
82
foreach ( const Solid::Device &device_btn, list_btn )
84
if ( device_btn.isValid() )
86
const Solid::Button *button = device_btn.as<Solid::Button>();
87
connect( button, SIGNAL( pressed( Solid::Button::ButtonType, const QString & ) ),
88
this, SLOT( slotButtonPressed( Solid::Button::ButtonType, const QString & ) ) );
98
// FIXME: Check for hibernation, etc. as well as for the system waking
101
// Get the AC adapter(s)
102
QList<Solid::Device> list_ac = Solid::Device::listFromType( Solid::DeviceInterface::AcAdapter, QString() );
104
foreach ( const Solid::Device &device_ac, list_ac )
106
if ( device_ac.isValid() )
108
const Solid::AcAdapter *acadapter = device_ac.as<Solid::AcAdapter>();
109
connect( acadapter, SIGNAL( plugStateChanged( bool, const QString & ) ),
110
this, SLOT( slotAcPlugStateChanged( bool, const QString & ) ) );
112
// FIXME: Check the current state.
122
// Get the primary batteries
123
QList<Solid::Device> list_bat = Solid::Device::listFromType( Solid::DeviceInterface::Battery, QString() );
125
foreach ( const Solid::Device &device_bat, list_bat )
127
if ( device_bat.isValid() )
129
const Solid::Battery *battery = device_bat.as<Solid::Battery>();
131
switch ( battery->type() )
133
case Solid::Battery::PrimaryBattery:
135
connect( battery, SIGNAL( chargeStateChanged( int, const QString & ) ),
136
this, SLOT( slotBatteryChargeStateChanged( int, const QString & ) ) );
138
connect( battery, SIGNAL( chargePercentChanged( int, const QString & ) ),
139
this, SLOT( slotBatteryChargePercentChanged( int, const QString & ) ) );
141
// FIXME: Check charge state and value
159
// Check network status and connect to the notifier
160
slotNetworkStatusChanged( Solid::Networking::status() );
162
connect( Solid::Networking::notifier(), SIGNAL( statusChanged( Solid::Networking::Status ) ),
163
this, SLOT( slotNetworkStatusChanged( Solid::Networking::Status ) ) );
167
/////////////////////////////////////////////////////////////////////////////
168
// SLOT IMPLEMENTATIONS
169
/////////////////////////////////////////////////////////////////////////////
171
void Smb4KSolidInterface::slotDeviceAdded( const QString &udi )
173
// Work around the fact that there is no signal that tells
174
// us when the computer woke up. Check the UDIs in the map
175
// and emit the wokeUp()
176
if ( m_button_pressed != UnknownButton &&
177
m_removed_devices[udi] == m_button_pressed )
179
m_removed_devices.remove( udi );
181
if ( m_removed_devices.isEmpty() )
183
// Work around: Computer woke up.
184
m_button_pressed = UnknownButton;
195
void Smb4KSolidInterface::slotDeviceRemoved( const QString &udi )
197
// Work around the fact that there is no signal that tells us,
198
// that the computer has been woken up. Store the removed device(s)
199
// and the pressed button to figure out later (in slotDeviceAdded())
200
// if the computer became active again.
201
if ( m_button_pressed != UnknownButton )
203
m_removed_devices.insert( udi, m_button_pressed );
212
void Smb4KSolidInterface::slotButtonPressed( Solid::Button::ButtonType type, const QString &udi )
216
case Solid::Button::LidButton:
218
m_button_pressed = LidButton;
221
case Solid::Button::SleepButton:
223
m_button_pressed = SleepButton;
226
case Solid::Button::PowerButton:
228
m_button_pressed = PowerButton;
233
m_button_pressed = UnknownButton;
238
emit buttonPressed( m_button_pressed );
242
void Smb4KSolidInterface::slotAcPlugStateChanged( bool state, const QString &udi )
246
kDebug() << "AC adapter plugged ..." << endl;
250
kDebug() << "AC adapter unplugged ..." << endl;
255
void Smb4KSolidInterface::slotBatteryChargeStateChanged( int state, const QString &udi )
259
case Solid::Battery::Discharging:
261
kDebug() << "Battery is discharging ..." << endl;
264
case Solid::Battery::Charging:
266
kDebug() << "Battery is charging ..." << endl;
271
kDebug() << "Unknown battery state ..." << endl;
278
void Smb4KSolidInterface::slotBatteryChargePercentChanged( int value, const QString &udi )
280
kDebug() << "Battery charge percent value: " << value << endl;
284
void Smb4KSolidInterface::slotNetworkStatusChanged( Solid::Networking::Status status )
288
case Solid::Networking::Connecting:
290
m_network_status = Connecting;
293
case Solid::Networking::Connected:
295
m_network_status = Connected;
298
case Solid::Networking::Disconnecting:
300
m_network_status = Disconnecting;
303
case Solid::Networking::Unconnected:
305
m_network_status = Disconnected;
308
case Solid::Networking::Unknown:
311
m_network_status = Unknown;
316
emit networkStatusChanged( m_network_status );
319
#include "smb4ksolidinterface.moc"