[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / interfaces / json-rpc / SystemOperations.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://www.xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "SystemOperations.h"
22 #include "ApplicationMessenger.h"
23 #include "interfaces/Builtins.h"
24 #include "utils/Variant.h"
25 #include "powermanagement/PowerManager.h"
26
27 using namespace JSONRPC;
28
29 JSONRPC_STATUS CSystemOperations::GetProperties(const CStdString &method, ITransportLayer *transport, IClient *client, const CVariant &parameterObject, CVariant &result)
30 {
31   CVariant properties = CVariant(CVariant::VariantTypeObject);
32   for (unsigned int index = 0; index < parameterObject["properties"].size(); index++)
33   {
34     CStdString propertyName = parameterObject["properties"][index].asString();
35     CVariant property;
36     JSONRPC_STATUS ret;
37     if ((ret = GetPropertyValue(client->GetPermissionFlags(), propertyName, property)) != OK)
38       return ret;
39
40     properties[propertyName] = property;
41   }
42
43   result = properties;
44
45   return OK;
46 }
47
48 JSONRPC_STATUS CSystemOperations::EjectOpticalDrive(const CStdString &method, ITransportLayer *transport, IClient *client, const CVariant &parameterObject, CVariant &result)
49 {
50   return CBuiltins::Execute("EjectTray") == 0 ? ACK : FailedToExecute;
51 }
52
53 JSONRPC_STATUS CSystemOperations::Shutdown(const CStdString &method, ITransportLayer *transport, IClient *client, const CVariant &parameterObject, CVariant &result)
54 {
55   if (g_powerManager.CanPowerdown())
56   {
57     CApplicationMessenger::Get().Powerdown();
58     return ACK;
59   }
60   else
61     return FailedToExecute;
62 }
63
64 JSONRPC_STATUS CSystemOperations::Suspend(const CStdString &method, ITransportLayer *transport, IClient *client, const CVariant &parameterObject, CVariant &result)
65 {
66   if (g_powerManager.CanSuspend())
67   {
68     CApplicationMessenger::Get().Suspend();
69     return ACK;
70   }
71   else
72     return FailedToExecute;
73 }
74
75 JSONRPC_STATUS CSystemOperations::Hibernate(const CStdString &method, ITransportLayer *transport, IClient *client, const CVariant &parameterObject, CVariant &result)
76 {
77   if (g_powerManager.CanHibernate())
78   {
79     CApplicationMessenger::Get().Hibernate();
80     return ACK;
81   }
82   else
83     return FailedToExecute;
84 }
85
86 JSONRPC_STATUS CSystemOperations::Reboot(const CStdString &method, ITransportLayer *transport, IClient *client, const CVariant &parameterObject, CVariant &result)
87 {
88   if (g_powerManager.CanReboot())
89   {
90     CApplicationMessenger::Get().Restart();
91     return ACK;
92   }
93   else
94     return FailedToExecute;
95 }
96
97 JSONRPC_STATUS CSystemOperations::GetPropertyValue(int permissions, const CStdString &property, CVariant &result)
98 {
99   if (property.Equals("canshutdown"))
100     result = g_powerManager.CanPowerdown() && (permissions & ControlPower);
101   else if (property.Equals("cansuspend"))
102     result = g_powerManager.CanSuspend() && (permissions & ControlPower);
103   else if (property.Equals("canhibernate"))
104     result = g_powerManager.CanHibernate() && (permissions & ControlPower);
105   else if (property.Equals("canreboot"))
106     result = g_powerManager.CanReboot() && (permissions & ControlPower);
107   else
108     return InvalidParams;
109
110   return OK;
111 }