1 /*
2  *  Copyright (C) 2012-2018 Team Kodi
3  *  This file is part of Kodi - https://kodi.tv
4  *
5  *  SPDX-License-Identifier: GPL-2.0-or-later
6  *  See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "FileItem.h"
12 #include "GUIUserMessages.h"
13 #include "IClient.h"
14 #include "ITransportLayer.h"
15 #include "ServiceBroker.h"
16 #include "guilib/GUIComponent.h"
17 #include "guilib/GUIWindowManager.h"
18 
19 class CVariant;
20 
21 namespace JSONRPC
22 {
23   /*!
24    \ingroup jsonrpc
25    \brief Possible statuc codes of a response
26    to a JSON-RPC request
27    */
28   enum JSONRPC_STATUS
29   {
30     OK = 0,
31     ACK = -1,
32     InvalidRequest = -32600,
33     MethodNotFound = -32601,
34     InvalidParams = -32602,
35     InternalError = -32603,
36     ParseError = -32700,
37     //-32099..-32000 Reserved for implementation-defined server-errors.
38     BadPermission = -32099,
39     FailedToExecute = -32100
40   };
41 
42   /*!
43    \brief Function pointer for JSON-RPC methods
44    */
45   typedef JSONRPC_STATUS (*MethodCall) (const std::string &method, ITransportLayer *transport, IClient *client, const CVariant& parameterObject, CVariant &result);
46 
47   /*!
48    \ingroup jsonrpc
49    \brief Permission categories for json rpc methods
50 
51    A JSON-RPC method will only be called if the caller
52    has the correct permissions to execute the method.
53    The method call needs to be perfectly threadsafe.
54   */
55   enum OperationPermission
56   {
57     ReadData        =    0x1,
58     ControlPlayback =    0x2,
59     ControlNotify   =    0x4,
60     ControlPower    =    0x8,
61     UpdateData      =   0x10,
62     RemoveData      =   0x20,
63     Navigate        =   0x40,
64     WriteFile       =   0x80,
65     ControlSystem   =  0x100,
66     ControlGUI      =  0x200,
67     ManageAddon     =  0x400,
68     ExecuteAddon    =  0x800,
69     ControlPVR      = 0x1000
70   };
71 
72   const int OPERATION_PERMISSION_ALL = (ReadData | ControlPlayback | ControlNotify | ControlPower |
73                                         UpdateData | RemoveData | Navigate | WriteFile | ControlSystem |
74                                         ControlGUI | ManageAddon | ExecuteAddon | ControlPVR);
75 
76   const int OPERATION_PERMISSION_NOTIFICATION = (ControlPlayback | ControlNotify | ControlPower | UpdateData |
77                                                  RemoveData | Navigate | WriteFile | ControlSystem |
78                                                  ControlGUI | ManageAddon | ExecuteAddon | ControlPVR);
79 
80   /*!
81     \brief Returns a string representation for the
82     given OperationPermission
83     \param permission Specific OperationPermission
84     \return String representation of the given OperationPermission
85     */
PermissionToString(const OperationPermission & permission)86   inline const char *PermissionToString(const OperationPermission &permission)
87   {
88     switch (permission)
89     {
90     case ReadData:
91       return "ReadData";
92     case ControlPlayback:
93       return "ControlPlayback";
94     case ControlNotify:
95       return "ControlNotify";
96     case ControlPower:
97       return "ControlPower";
98     case UpdateData:
99       return "UpdateData";
100     case RemoveData:
101       return "RemoveData";
102     case Navigate:
103       return "Navigate";
104     case WriteFile:
105       return "WriteFile";
106     case ControlSystem:
107       return "ControlSystem";
108     case ControlGUI:
109       return "ControlGUI";
110     case ManageAddon:
111       return "ManageAddon";
112     case ExecuteAddon:
113       return "ExecuteAddon";
114     case ControlPVR:
115       return "ControlPVR";
116     default:
117       return "Unknown";
118     }
119   }
120 
121   /*!
122     \brief Returns a OperationPermission value for the given
123     string representation
124     \param permission String representation of the OperationPermission
125     \return OperationPermission value of the given string representation
126     */
StringToPermission(const std::string & permission)127   inline OperationPermission StringToPermission(const std::string& permission)
128   {
129     if (permission.compare("ControlPlayback") == 0)
130       return ControlPlayback;
131     if (permission.compare("ControlNotify") == 0)
132       return ControlNotify;
133     if (permission.compare("ControlPower") == 0)
134       return ControlPower;
135     if (permission.compare("UpdateData") == 0)
136       return UpdateData;
137     if (permission.compare("RemoveData") == 0)
138       return RemoveData;
139     if (permission.compare("Navigate") == 0)
140       return Navigate;
141     if (permission.compare("WriteFile") == 0)
142       return WriteFile;
143     if (permission.compare("ControlSystem") == 0)
144       return ControlSystem;
145     if (permission.compare("ControlGUI") == 0)
146       return ControlGUI;
147     if (permission.compare("ManageAddon") == 0)
148       return ManageAddon;
149     if (permission.compare("ExecuteAddon") == 0)
150       return ExecuteAddon;
151     if (permission.compare("ControlPVR") == 0)
152       return ControlPVR;
153 
154     return ReadData;
155   }
156 
157   class CJSONRPCUtils
158   {
159   public:
NotifyItemUpdated()160     static inline void NotifyItemUpdated()
161     {
162       CGUIMessage message(GUI_MSG_NOTIFY_ALL, 0, 0, GUI_MSG_UPDATE, CServiceBroker::GetGUI()->GetWindowManager().GetActiveWindow());
163       CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage(message);
164     }
NotifyItemUpdated(const CVideoInfoTag & info,const std::map<std::string,std::string> & artwork)165     static inline void NotifyItemUpdated(const CVideoInfoTag& info,
166                                          const std::map<std::string, std::string>& artwork)
167     {
168       CFileItemPtr msgItem(new CFileItem(info));
169       if (!artwork.empty())
170         msgItem->SetArt(artwork);
171       CGUIMessage message(GUI_MSG_NOTIFY_ALL, CServiceBroker::GetGUI()->GetWindowManager().GetActiveWindow(), 0, GUI_MSG_UPDATE_ITEM, 0, msgItem);
172       CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage(message);
173     }
174   };
175 }
176