[cstdstring] demise Format, replacing with StringUtils::Format
[vuplus_xbmc] / xbmc / interfaces / legacy / Addon.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
21 #include "Addon.h"
22 #include "LanguageHook.h"
23
24 #include "addons/AddonManager.h"
25 #include "addons/GUIDialogAddonSettings.h"
26 #include "guilib/GUIWindowManager.h"
27 #include "GUIUserMessages.h"
28 #include "utils/StringUtils.h"
29
30 using namespace ADDON;
31
32 namespace XBMCAddon
33 {
34   namespace xbmcaddon
35   {
36     String Addon::getDefaultId() { return languageHook == NULL ? emptyString : languageHook->GetAddonId(); }
37
38     String Addon::getAddonVersion() { return languageHook == NULL ? emptyString : languageHook->GetAddonVersion(); }
39
40     Addon::Addon(const char* cid) throw (AddonException)
41     {
42       String id(cid ? cid : emptyString);
43
44       // if the id wasn't passed then get the id from
45       //   the global dictionary
46       if (id.empty())
47         id = getDefaultId();
48
49       // if we still don't have an id then bail
50       if (id.empty())
51         throw AddonException("No valid addon id could be obtained. None was passed and the script wasn't executed in a normal xbmc manner.");
52
53       // if we still fail we MAY be able to recover.
54       if (!ADDON::CAddonMgr::Get().GetAddon(id.c_str(), pAddon))
55       {
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");
59
60         if (version <= allowable)
61         {
62           // try the default ...
63           id = getDefaultId();
64
65           if (id.empty() || !ADDON::CAddonMgr::Get().GetAddon(id.c_str(), pAddon))
66             throw AddonException("Could not get AddonPtr!");
67           else
68             CLog::Log(LOGERROR,"Use of deprecated functionality. Please to not assume that \"os.getcwd\" will return the script directory.");
69         }
70         else
71         {
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());
75         }
76       }
77
78       CAddonMgr::Get().AddToUpdateableAddons(pAddon);
79     }
80
81     Addon::~Addon()
82     {
83       CAddonMgr::Get().RemoveFromUpdateableAddons(pAddon);
84     }
85
86     String Addon::getLocalizedString(int id)
87     {
88       return pAddon->GetString(id);
89     }
90
91     String Addon::getSetting(const char* id)
92     {
93       return pAddon->GetSetting(id);
94     }
95
96     void Addon::setSetting(const char* id, const String& value)
97     {
98       DelayedCallGuard dcguard(languageHook);
99       ADDON::AddonPtr addon(pAddon);
100       bool save=true;
101       if (g_windowManager.IsWindowActive(WINDOW_DIALOG_ADDON_SETTINGS))
102       {
103         CGUIDialogAddonSettings* dialog = (CGUIDialogAddonSettings*)g_windowManager.GetWindow(WINDOW_DIALOG_ADDON_SETTINGS);
104         if (dialog->GetCurrentID() == addon->ID())
105         {
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);
112           save=false;
113         }
114       }
115       if (save)
116       {
117         addon->UpdateSetting(id, value);
118         addon->SaveSettings();
119       }
120     }
121
122     void Addon::openSettings()
123     {
124       DelayedCallGuard dcguard(languageHook);
125       // show settings dialog
126       ADDON::AddonPtr addon(pAddon);
127       CGUIDialogAddonSettings::ShowAndGetInput(addon);
128     }
129
130     String Addon::getAddonInfo(const char* id) throw (AddonException)
131     {
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)
145         return pAddon->ID();
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)
153       {
154         return StringUtils::Format("%d", pAddon->Stars());
155       }
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());
162       else
163         throw AddonException("'%s' is an invalid Id", id);
164     }
165   }
166 }