Renamed FileActions and PlayerActions to FileOperations and PlayerOperations
[xbmc:xbmc-antiquated.git] / xbmc / lib / libjsonrpc / PlayerOperations.cpp
1 /*\r
2  *      Copyright (C) 2005-2010 Team XBMC\r
3  *      http://www.xbmc.org\r
4  *\r
5  *  This Program is free software; you can redistribute it and/or modify\r
6  *  it under the terms of the GNU General Public License as published by\r
7  *  the Free Software Foundation; either version 2, or (at your option)\r
8  *  any later version.\r
9  *\r
10  *  This Program is distributed in the hope that it will be useful,\r
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
13  *  GNU General Public License for more details.\r
14  *\r
15  *  You should have received a copy of the GNU General Public License\r
16  *  along with XBMC; see the file COPYING.  If not, write to\r
17  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.\r
18  *  http://www.gnu.org/copyleft/gpl.html\r
19  *\r
20  */
21
22 #include "PlayerOperations.h"
23 #include "Application.h"
24 #include "Builtins.h"
25 #include "Util.h"
26 #include "PlayListPlayer.h"
27
28 using namespace Json;
29 using namespace JSONRPC;
30
31 JSON_STATUS CPlayerOperations::GetActivePlayers(const CStdString &method, ITransportLayer *transport, IClient *client, const Value& parameterObject, Value &result)
32 {
33   if (g_application.IsPlayingVideo())
34     result["players"].append("video");
35   else if (g_application.IsPlayingAudio())
36     result["players"].append("music");
37
38   return OK;
39 }
40
41 JSON_STATUS CPlayerOperations::PlayPause(const CStdString &method, ITransportLayer *transport, IClient *client, const Value& parameterObject, Value &result)
42 {
43   if (!IsCorrectPlayer(method))
44     return FailedToExecute;
45
46   CBuiltins::Execute("playercontrol(play)");
47   result["playing"] = g_application.IsPlaying();
48   result["paused"] = g_application.IsPaused();
49   return OK;
50 }
51
52 JSON_STATUS CPlayerOperations::Stop(const CStdString &method, ITransportLayer *transport, IClient *client, const Value& parameterObject, Value &result)
53 {
54   if (!IsCorrectPlayer(method))
55     return FailedToExecute;
56
57   CAction action;
58   action.id = ACTION_STOP;
59   g_application.getApplicationMessenger().SendAction(action);
60   return ACK;
61 }
62
63 JSON_STATUS CPlayerOperations::SkipPrevious(const CStdString &method, ITransportLayer *transport, IClient *client, const Value& parameterObject, Value &result)
64 {
65   if (!IsCorrectPlayer(method))
66     return FailedToExecute;
67
68   CAction action;
69   action.id = ACTION_PREV_ITEM;
70   g_application.getApplicationMessenger().SendAction(action);
71   return ACK;
72 }
73
74 JSON_STATUS CPlayerOperations::SkipNext(const CStdString &method, ITransportLayer *transport, IClient *client, const Value& parameterObject, Value &result)
75 {
76   if (!IsCorrectPlayer(method))
77     return FailedToExecute;
78
79   CAction action;
80   action.id = ACTION_NEXT_ITEM;
81   g_application.getApplicationMessenger().SendAction(action);
82   return ACK;
83 }
84
85 JSON_STATUS CPlayerOperations::BigSkipBackward(const CStdString &method, ITransportLayer *transport, IClient *client, const Value& parameterObject, Value &result)
86 {
87   if (!IsCorrectPlayer(method))
88     return FailedToExecute;
89
90   CBuiltins::Execute("playercontrol(bigskipbackward)");
91   return ACK;
92 }
93
94 JSON_STATUS CPlayerOperations::BigSkipForward(const CStdString &method, ITransportLayer *transport, IClient *client, const Value& parameterObject, Value &result)
95 {
96   if (!IsCorrectPlayer(method))
97     return FailedToExecute;
98
99   CBuiltins::Execute("playercontrol(bigskipforward)");
100   return ACK;
101 }
102
103 JSON_STATUS CPlayerOperations::SmallSkipBackward(const CStdString &method, ITransportLayer *transport, IClient *client, const Value& parameterObject, Value &result)
104 {
105   if (!IsCorrectPlayer(method))
106     return FailedToExecute;
107
108   CBuiltins::Execute("playercontrol(smallskipbackward)");
109   return ACK;
110 }
111
112 JSON_STATUS CPlayerOperations::SmallSkipForward(const CStdString &method, ITransportLayer *transport, IClient *client, const Value& parameterObject, Value &result)
113 {
114   if (!IsCorrectPlayer(method))
115     return FailedToExecute;
116
117   CBuiltins::Execute("playercontrol(smallskipforward)");
118   return ACK;
119 }
120
121 JSON_STATUS CPlayerOperations::Rewind(const CStdString &method, ITransportLayer *transport, IClient *client, const Value& parameterObject, Value &result)
122 {
123   if (!IsCorrectPlayer(method))
124     return FailedToExecute;
125
126   CAction action;
127   action.id = ACTION_PLAYER_REWIND;
128   g_application.getApplicationMessenger().SendAction(action);
129   return ACK;
130 }
131
132 JSON_STATUS CPlayerOperations::Forward(const CStdString &method, ITransportLayer *transport, IClient *client, const Value& parameterObject, Value &result)
133 {
134   if (!IsCorrectPlayer(method))
135     return FailedToExecute;
136
137   CAction action;
138   action.id = ACTION_PLAYER_FORWARD;
139   g_application.getApplicationMessenger().SendAction(action);
140   return ACK;
141 }
142
143 JSON_STATUS CPlayerOperations::Record(const CStdString &method, ITransportLayer *transport, IClient *client, const Value& parameterObject, Value &result)
144 {
145   if (!IsCorrectPlayer(method))
146     return FailedToExecute;
147
148   CBuiltins::Execute("playercontrol(record)");
149   return ACK;
150 }
151
152 JSON_STATUS CPlayerOperations::GetTime(const CStdString &method, ITransportLayer *transport, IClient *client, const Json::Value& parameterObject, Json::Value &result)
153 {
154   if (!IsCorrectPlayer(method))
155     return FailedToExecute;
156
157   result["time"] = (int)g_application.GetTime();
158   result["total"] = (int)g_application.GetTotalTime();
159   return OK;
160 }
161
162 JSON_STATUS CPlayerOperations::GetTimeMS(const CStdString &method, ITransportLayer *transport, IClient *client, const Json::Value& parameterObject, Json::Value &result)
163 {
164   if (!IsCorrectPlayer(method))
165     return FailedToExecute;
166
167   result["time"] = (int)(g_application.GetTime() * 1000.0);
168   result["total"] = (int)(g_application.GetTotalTime() * 1000.0);
169   return OK;
170 }
171
172 JSON_STATUS CPlayerOperations::GetPercentage(const CStdString &method, ITransportLayer *transport, IClient *client, const Json::Value& parameterObject, Json::Value &result)
173 {
174   if (!IsCorrectPlayer(method))
175     return FailedToExecute;
176
177   Value val = g_application.GetPercentage();
178   result.swap(val);
179   return OK;
180 }
181
182 JSON_STATUS CPlayerOperations::SeekTime(const CStdString &method, ITransportLayer *transport, IClient *client, const Json::Value& parameterObject, Json::Value &result)
183 {
184   if (!parameterObject.isInt())
185     return InvalidParams;
186   if (!IsCorrectPlayer(method))
187     return FailedToExecute;
188
189   g_application.SeekTime(parameterObject.asInt());
190   return ACK;
191 }
192
193 JSON_STATUS CPlayerOperations::GetPlaylist(const CStdString &method, ITransportLayer *transport, IClient *client, const Json::Value& parameterObject, Json::Value &result)
194 {
195   int playlist = PLAYLIST_NONE;
196   if (method.Left(5).Equals("music"))
197     playlist = PLAYLIST_MUSIC;
198   else if (method.Left(5).Equals("video"))
199     playlist = PLAYLIST_VIDEO;
200
201   Value val = playlist;
202   result.swap(val);
203
204   return OK;
205 }
206
207 bool CPlayerOperations::IsCorrectPlayer(const CStdString &method)
208 {
209   return (method.Left(5).Equals("music") && g_application.IsPlayingAudio()) || (method.Left(5).Equals("video") && g_application.IsPlayingVideo());
210 }