~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to powerdevil/daemon/backends/hal/halsuspendjob.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  This file is part of the KDE project
 
2
    Copyright (C) 2006 Kevin Ottens <ervin@kde.org>
 
3
 
 
4
    This library is free software; you can redistribute it and/or
 
5
    modify it under the terms of the GNU Library General Public
 
6
    License version 2 as published by the Free Software Foundation.
 
7
 
 
8
    This library is distributed in the hope that it will be useful,
 
9
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
    Library General Public License for more details.
 
12
 
 
13
    You should have received a copy of the GNU Library General Public License
 
14
    along with this library; see the file COPYING.LIB.  If not, write to
 
15
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
16
    Boston, MA 02110-1301, USA.
 
17
 
 
18
*/
 
19
 
 
20
#include "halsuspendjob.h"
 
21
 
 
22
#include <QtDBus/QDBusMessage>
 
23
#include <QtDBus/QDBusReply>
 
24
#include <QTimer>
 
25
#include <KConfig>
 
26
#include <KConfigGroup>
 
27
 
 
28
HalSuspendJob::HalSuspendJob(QDBusInterface &powermanagement, QDBusInterface &computer,
 
29
                             PowerDevil::BackendInterface::SuspendMethod method,
 
30
                             PowerDevil::BackendInterface::SuspendMethods supported)
 
31
    : KJob(), m_halPowerManagement(powermanagement), m_halComputer(computer)
 
32
{
 
33
    if (supported  & method)
 
34
    {
 
35
        QDBusReply<bool> reply;
 
36
 
 
37
        switch(method)
 
38
        {
 
39
        case PowerDevil::BackendInterface::ToRam:
 
40
            reply = m_halComputer.call("GetPropertyBoolean", "power_management.can_suspend_hybrid");
 
41
 
 
42
            if (reply.isValid())
 
43
            {
 
44
                bool can_hybrid = reply;
 
45
                if (can_hybrid)
 
46
                {
 
47
                    // Temporary: let's check if system is configured to use Hybrid suspend. Default is no.
 
48
                    KConfig sconf("suspendpreferencesrc");
 
49
                    KConfigGroup group(&sconf, "Preferences");
 
50
                    if (group.readEntry("use_hybrid", false)) {
 
51
                        m_dbusMethod = "SuspendHybrid";
 
52
                    } else {
 
53
                        m_dbusMethod = "Suspend";
 
54
                    }
 
55
                }
 
56
                else
 
57
                {
 
58
                    m_dbusMethod = "Suspend";
 
59
                }
 
60
            }
 
61
            else
 
62
            {
 
63
                m_dbusMethod = "Suspend";
 
64
            }
 
65
            m_dbusParam = 0;
 
66
            break;
 
67
        case PowerDevil::BackendInterface::ToDisk:
 
68
            m_dbusMethod = "Hibernate";
 
69
            m_dbusParam = -1;
 
70
            break;
 
71
        default:
 
72
            break;
 
73
        }
 
74
    }
 
75
}
 
76
 
 
77
HalSuspendJob::~HalSuspendJob()
 
78
{
 
79
 
 
80
}
 
81
 
 
82
void HalSuspendJob::start()
 
83
{
 
84
    QTimer::singleShot(0, this, SLOT(doStart()));
 
85
}
 
86
 
 
87
void HalSuspendJob::kill(bool /*quietly */)
 
88
{
 
89
 
 
90
}
 
91
 
 
92
void HalSuspendJob::doStart()
 
93
{
 
94
    if (m_dbusMethod.isEmpty())
 
95
    {
 
96
        setError(1);
 
97
        setErrorText("Unsupported suspend method");
 
98
        emitResult();
 
99
        return;
 
100
    }
 
101
 
 
102
    QList<QVariant> args;
 
103
 
 
104
    if (m_dbusParam>=0)
 
105
    {
 
106
        args << m_dbusParam;
 
107
    }
 
108
 
 
109
    if (!m_halPowerManagement.callWithCallback(m_dbusMethod, args,
 
110
                                                this, SLOT(resumeDone(const QDBusMessage &))))
 
111
    {
 
112
        setError(1);
 
113
        setErrorText(m_halPowerManagement.lastError().name()+": "
 
114
                     +m_halPowerManagement.lastError().message());
 
115
        emitResult();
 
116
    }
 
117
}
 
118
 
 
119
 
 
120
void HalSuspendJob::resumeDone(const QDBusMessage &reply)
 
121
{
 
122
    if (reply.type() == QDBusMessage::ErrorMessage)
 
123
    {
 
124
        // We ignore the NoReply error, since we can timeout anyway
 
125
        // if the computer is suspended for too long.
 
126
        if (reply.errorName() != "org.freedesktop.DBus.Error.NoReply")
 
127
        {
 
128
            setError(1);
 
129
            setErrorText(reply.errorName()+": "+reply.arguments().at(0).toString());
 
130
        }
 
131
    }
 
132
 
 
133
    emitResult();
 
134
}
 
135
 
 
136
#include "halsuspendjob.moc"