summaryrefslogtreecommitdiffstats
path: root/tdecore/tdehw/hwlibdaemons/tdedbus/PowerService.cpp
blob: 9b269a9de190dd664c5fc1c4a36fce9e8c9290d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * PowerService.cpp
 *
 *  Created on: Feb 1, 2021
 *      Author: emanoil
 *
 * hardwarecontrol Copyright (C) 2021 trinity desktop development team
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <unistd.h>

//#include <kdebug.h>
#include <tqfile.h>
#include <tqtextstream.h>

#include "PowerService.h"

#define POWER_STATE_PATH  "/sys/power/state"
#define POWER_DISK_PATH   "/sys/power/disk"

PowerService::PowerService(TQT_DBusConnection &conn)
: DeviceServiceBase(conn)
{
    // TODO Auto-generated constructor stub

}

PowerService::~PowerService()
{
    // TODO Auto-generated destructor stub
}

bool PowerService::canSetPower(TQString state, TQT_DBusError& error) {

    bool method = false;
    if (canSetDeviceValue(POWER_STATE_PATH,error)) {
        TQFile file1( POWER_STATE_PATH );
        if ( file1.open( IO_ReadOnly ) ) {
            TQTextStream stream( &file1 );
            TQString line = stream.readLine(); // line of text excluding '\n'
            if ( line.find(state, 0) != -1 )
                method = true;
            file1.close();
        } else {
            error = TQT_DBusError::stdInvalidArgs(TQString ("Can not write device: ").append(POWER_STATE_PATH));
        }
    }

    // send reply
    return method;
}

bool PowerService::setPower(TQString state, TQT_DBusError& error) {

    bool written = false;
    if (canSetPower(state,error)) {
        if ( setDeviceValue(POWER_STATE_PATH, state, error) ) {
            written = true;
        } else {
            error = TQT_DBusError::stdFailed(TQString ("Can not set state: " + state));
        }
    } else {
        error = TQT_DBusError::stdFailed(TQString ("Can not write device: ").append(POWER_STATE_PATH));
    }

    return written;
}

bool PowerService::canSetHibernation(TQString state, TQT_DBusError& error) {

    // check if path is writable
    bool state_writable = canSetDeviceValue(POWER_STATE_PATH,error);
    bool disk_writable = canSetDeviceValue(POWER_DISK_PATH,error);

    // check if method is supported
    bool method1 = false, method2 = false;
    if (state_writable && disk_writable) {
        TQFile file1( POWER_STATE_PATH );
        if ( file1.open( IO_ReadOnly ) ) {
            TQTextStream stream( &file1 );
            TQString line = stream.readLine(); // line of text excluding '\n'
            if ( line.find("disk", 0) != -1 )
                method1 = true;
            file1.close();
//            kdDebug() <<  "Method1 for " << state << " is " << method1 << "\n";
        } else {
            error = TQT_DBusError::stdInvalidArgs(TQString ("Could not open ").append(POWER_STATE_PATH));
            return false;
        }

        TQFile file2(POWER_DISK_PATH);
        if ( file2.open( IO_ReadOnly ) ) {
            TQTextStream stream( &file2 );
            TQString line = stream.readLine(); // line of text excluding '\n'
            if ( line.find(state, 0) != -1 )
                method2 = true;
            file2.close();
        } else {
            error = TQT_DBusError::stdInvalidArgs(TQString ("Could not open ").append(POWER_DISK_PATH));
            return false;
        }
    }

    // send reply
    return state_writable && disk_writable && method1 && method2;
}

bool PowerService::setHibernation(TQString state, TQT_DBusError& error) {

    // set hibernation state
    bool written1 = false, written2 = false;

    if (canSetHibernation(state,error)) {
        TQFile file1(POWER_DISK_PATH);
        if (!file1.open( IO_WriteOnly ) ) {
            error = TQT_DBusError::stdFailed(TQString ("Could not open device ").append(POWER_DISK_PATH));
            return false;
        }
        TQTextStream stream1( &file1 );
        stream1 << state;
        file1.close();
        written1 = true;

        TQFile file2(POWER_STATE_PATH);
        if (!file2.open( IO_WriteOnly ) ) {
            error = TQT_DBusError::stdFailed(TQString ("Could not open device ").append(POWER_STATE_PATH));
            return false;
        }
        TQTextStream stream2( &file2 );
        stream2 << "disk";
        file2.close();
        written2 = true;
    } else {
        error = TQT_DBusError::stdFailed(TQString ("Could not set state: " + state));
        return false;
    }

    return written1 && written2;
}

/*!
 * Implement virtual methods
 *
 */

bool PowerService::CanStandby(bool& value, TQT_DBusError& error) {
    value = canSetPower("standby", error);
    if (error.isValid()) {
        tqDebug(error.message().local8Bit());
        return false;
    }
    return true;
}

bool PowerService::Standby(bool& value, TQT_DBusError& error) {
    value = setPower("standby", error);
    if (error.isValid()) {
        tqDebug(error.message().local8Bit());
        return false;
    }
    return true;
}

bool PowerService::CanFreeze(bool& value, TQT_DBusError& error) {
    value = canSetPower("freeze", error);
    if (error.isValid()) {
        tqDebug(error.message().local8Bit());
        return false;
    }
    return true;
}

bool PowerService::Freeze(bool& value, TQT_DBusError& error) {
    value = setPower("freeze", error);
    if (error.isValid()) {
        tqDebug(error.message().local8Bit());
        return false;
    }
    return true;
}

bool PowerService::CanSuspend(bool& value, TQT_DBusError& error) {
    value = canSetPower("mem", error);
    if (error.isValid()) {
        tqDebug(error.message().local8Bit());
        return false;
    }
    return true;
}

bool PowerService::Suspend(bool& value, TQT_DBusError& error) {
    value = setPower("mem", error);
    if (error.isValid()) {
        tqDebug(error.message().local8Bit());
        return false;
    }
    return true;
}

bool PowerService::CanHibernate(bool& value, TQT_DBusError& error) {
    value = canSetHibernation("platform", error);
    if (error.isValid()) {
        tqDebug(error.message().local8Bit());
        return false;
    }
    return true;
}

bool PowerService::Hibernate(bool& value, TQT_DBusError& error) {
    value = setHibernation("platform", error);
    if (error.isValid()) {
        tqDebug(error.message().local8Bit());
        return false;
    }
    return true;
}

bool PowerService::CanHybridSuspend(bool& value, TQT_DBusError& error) {
    value = canSetHibernation("suspend", error);
    if (error.isValid()) {
        tqDebug(error.message().local8Bit());
        return false;
    }
    return true;
}

bool PowerService::HybridSuspend(bool& value, TQT_DBusError& error) {
    value = setHibernation("suspend", error);
    if (error.isValid()) {
        tqDebug(error.message().local8Bit());
        return false;
    }
    return true;
}

bool PowerService::CanSetHibernationMethod(bool& value, TQT_DBusError& error) {
    value = canSetDeviceValue(POWER_DISK_PATH, error);
    if (error.isValid()) {
        tqDebug(error.message().local8Bit());
        return false;
    }
    return true;
}

bool PowerService::SetHibernationMethod(const TQString& method, bool& value, TQT_DBusError& error) {
    // set hibernation method
    if (!method.isEmpty()) {
        value = setDeviceValue(POWER_DISK_PATH, method, error);
    }
    else {
        value = false;
        error = TQT_DBusError::stdInvalidArgs(TQString ("Invalid argument for method: " + method));
    }
    if (error.isValid()) {
        tqDebug(error.message().local8Bit());
        return false;
    }
    return true;
}


void PowerService::handleMethodReply(const TQT_DBusMessage& reply) {
    m_connection->send(reply);
}