2 * Copyright (C) 2005-2013 Team XBMC
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)
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.
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/>.
22 #include "LanguageHook.h"
24 #include "addons/AddonManager.h"
25 #include "addons/GUIDialogAddonSettings.h"
26 #include "guilib/GUIWindowManager.h"
27 #include "GUIUserMessages.h"
28 #include "utils/StringUtils.h"
30 using namespace ADDON;
36 String Addon::getDefaultId() { return languageHook == NULL ? emptyString : languageHook->GetAddonId(); }
38 String Addon::getAddonVersion() { return languageHook == NULL ? emptyString : languageHook->GetAddonVersion(); }
40 Addon::Addon(const char* cid) throw (AddonException)
42 String id(cid ? cid : emptyString);
44 // if the id wasn't passed then get the id from
45 // the global dictionary
49 // if we still don't have an id then bail
51 throw AddonException("No valid addon id could be obtained. None was passed and the script wasn't executed in a normal xbmc manner.");
53 // if we still fail we MAY be able to recover.
54 if (!ADDON::CAddonMgr::Get().GetAddon(id.c_str(), pAddon))
56 // we need to check the version prior to trying a bw compatibility trick
57 ADDON::AddonVersion version(getAddonVersion());
58 ADDON::AddonVersion allowable("1.0");
60 if (version <= allowable)
62 // try the default ...
65 if (id.empty() || !ADDON::CAddonMgr::Get().GetAddon(id.c_str(), pAddon))
66 throw AddonException("Could not get AddonPtr!");
68 CLog::Log(LOGERROR,"Use of deprecated functionality. Please to not assume that \"os.getcwd\" will return the script directory.");
72 throw AddonException("Could not get AddonPtr given a script id of %s."
73 "If you are trying to use 'os.getcwd' to set the path, you cannot do that in a %s plugin.",
74 id.c_str(), version.Print().c_str());
78 CAddonMgr::Get().AddToUpdateableAddons(pAddon);
83 CAddonMgr::Get().RemoveFromUpdateableAddons(pAddon);
86 String Addon::getLocalizedString(int id)
88 return pAddon->GetString(id);
91 String Addon::getSetting(const char* id)
93 return pAddon->GetSetting(id);
96 void Addon::setSetting(const char* id, const String& value)
98 DelayedCallGuard dcguard(languageHook);
99 ADDON::AddonPtr addon(pAddon);
101 if (g_windowManager.IsWindowActive(WINDOW_DIALOG_ADDON_SETTINGS))
103 CGUIDialogAddonSettings* dialog = (CGUIDialogAddonSettings*)g_windowManager.GetWindow(WINDOW_DIALOG_ADDON_SETTINGS);
104 if (dialog->GetCurrentID() == addon->ID())
106 CGUIMessage message(GUI_MSG_SETTING_UPDATED,0,0);
107 std::vector<CStdString> params;
108 params.push_back(id);
109 params.push_back(value);
110 message.SetStringParams(params);
111 g_windowManager.SendThreadMessage(message,WINDOW_DIALOG_ADDON_SETTINGS);
117 addon->UpdateSetting(id, value);
118 addon->SaveSettings();
122 void Addon::openSettings()
124 DelayedCallGuard dcguard(languageHook);
125 // show settings dialog
126 ADDON::AddonPtr addon(pAddon);
127 CGUIDialogAddonSettings::ShowAndGetInput(addon);
130 String Addon::getAddonInfo(const char* id) throw (AddonException)
132 if (strcmpi(id, "author") == 0)
133 return pAddon->Author();
134 else if (strcmpi(id, "changelog") == 0)
135 return pAddon->ChangeLog();
136 else if (strcmpi(id, "description") == 0)
137 return pAddon->Description();
138 else if (strcmpi(id, "disclaimer") == 0)
139 return pAddon->Disclaimer();
140 else if (strcmpi(id, "fanart") == 0)
141 return pAddon->FanArt();
142 else if (strcmpi(id, "icon") == 0)
143 return pAddon->Icon();
144 else if (strcmpi(id, "id") == 0)
146 else if (strcmpi(id, "name") == 0)
147 return pAddon->Name();
148 else if (strcmpi(id, "path") == 0)
149 return pAddon->Path();
150 else if (strcmpi(id, "profile") == 0)
151 return pAddon->Profile();
152 else if (strcmpi(id, "stars") == 0)
154 return StringUtils::Format("%d", pAddon->Stars());
156 else if (strcmpi(id, "summary") == 0)
157 return pAddon->Summary();
158 else if (strcmpi(id, "type") == 0)
159 return ADDON::TranslateType(pAddon->Type());
160 else if (strcmpi(id, "version") == 0)
161 return String(pAddon->Version().c_str());
163 throw AddonException("'%s' is an invalid Id", id);