Fix keymap.
[vuplus_xbmc] / xbmc / addons / Service.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://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 #include "Service.h"
21 #include "AddonManager.h"
22 #include "interfaces/generic/ScriptInvocationManager.h"
23 #include "utils/log.h"
24 #include "system.h"
25
26 using namespace std;
27
28 namespace ADDON
29 {
30
31 CService::CService(const cp_extension_t *ext)
32   : CAddon(ext), m_type(UNKNOWN), m_startOption(LOGIN)
33 {
34   BuildServiceType();
35
36   CStdString start = CAddonMgr::Get().GetExtValue(ext->configuration, "@start");
37   if (start.Equals("startup"))
38     m_startOption = STARTUP;
39 }
40
41
42 CService::CService(const AddonProps &props)
43   : CAddon(props), m_type(UNKNOWN), m_startOption(LOGIN)
44 {
45   BuildServiceType();
46 }
47
48 AddonPtr CService::Clone() const
49 {
50   return AddonPtr(new CService(*this));
51 }
52
53 bool CService::Start()
54 {
55   bool ret = true;
56   switch (m_type)
57   {
58 #ifdef HAS_PYTHON
59   case PYTHON:
60     ret = (CScriptInvocationManager::Get().Execute(LibPath(), this->shared_from_this()) != -1);
61     break;
62 #endif
63
64   case UNKNOWN:
65   default:
66     ret = false;
67     break;
68   }
69
70   return ret;
71 }
72
73 bool CService::Stop()
74 {
75   bool ret = true;
76
77   switch (m_type)
78   {
79 #ifdef HAS_PYTHON
80   case PYTHON:
81     ret = CScriptInvocationManager::Get().Stop(LibPath());
82     break;
83 #endif
84
85   case UNKNOWN:
86   default:
87     ret = false;
88     break;
89   }
90
91   return ret;
92 }
93
94 void CService::BuildServiceType()
95 {
96   CStdString str = LibPath();
97   CStdString ext;
98
99   size_t p = str.find_last_of('.');
100   if (p != string::npos)
101     ext = str.substr(p + 1);
102
103 #ifdef HAS_PYTHON
104   CStdString pythonExt = ADDON_PYTHON_EXT;
105   pythonExt.erase(0, 2);
106   if ( ext.Equals(pythonExt) )
107     m_type = PYTHON;
108   else
109 #endif
110   {
111     m_type = UNKNOWN;
112     CLog::Log(LOGERROR, "ADDON: extension '%s' is not currently supported for service addon", ext.c_str());
113   }
114 }
115
116 }