[cstdstring] demise Format, replacing with StringUtils::Format
[vuplus_xbmc] / xbmc / filesystem / FavouritesDirectory.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 "FavouritesDirectory.h"
22 #include "File.h"
23 #include "Util.h"
24 #include "profiles/ProfilesManager.h"
25 #include "FileItem.h"
26 #include "utils/XBMCTinyXML.h"
27 #include "utils/log.h"
28 #include "utils/StringUtils.h"
29 #include "utils/URIUtils.h"
30 #include "settings/AdvancedSettings.h"
31 #include "video/VideoInfoTag.h"
32 #include "URL.h"
33
34 namespace XFILE
35 {
36
37
38 bool CFavouritesDirectory::GetDirectory(const CStdString& strPath, CFileItemList &items)
39 {
40   items.Clear();
41   CURL url(strPath);
42   
43   if (url.GetProtocol() == "favourites")
44   {
45     Load(items); //load the default favourite files
46   }
47   return LoadFavourites(strPath, items); //directly load the given file
48 }
49   
50 bool CFavouritesDirectory::Exists(const char* strPath)
51 {
52   CURL url(strPath);
53   
54   if (url.GetProtocol() == "favourites")
55   {
56     return XFILE::CFile::Exists("special://xbmc/system/favourites.xml") 
57         || XFILE::CFile::Exists(URIUtils::AddFileToFolder(CProfilesManager::Get().GetProfileUserDataFolder(), "favourites.xml"));
58   }
59   return XFILE::CFile::Exists(strPath); //directly load the given file
60 }
61
62 bool CFavouritesDirectory::Load(CFileItemList &items)
63 {
64   items.Clear();
65   CStdString favourites;
66
67   favourites = "special://xbmc/system/favourites.xml";
68   if(XFILE::CFile::Exists(favourites))
69     CFavouritesDirectory::LoadFavourites(favourites, items);
70   else
71     CLog::Log(LOGDEBUG, "CFavourites::Load - no system favourites found, skipping");
72   favourites = URIUtils::AddFileToFolder(CProfilesManager::Get().GetProfileUserDataFolder(), "favourites.xml");
73   if(XFILE::CFile::Exists(favourites))
74     CFavouritesDirectory::LoadFavourites(favourites, items);
75   else
76     CLog::Log(LOGDEBUG, "CFavourites::Load - no userdata favourites found, skipping");
77
78   return true;
79 }
80
81 bool CFavouritesDirectory::LoadFavourites(const CStdString& strPath, CFileItemList& items)
82 {
83   CXBMCTinyXML doc;
84   if (!doc.LoadFile(strPath))
85   {
86     CLog::Log(LOGERROR, "Unable to load %s (row %i column %i)", strPath.c_str(), doc.Row(), doc.Column());
87     return false;
88   }
89   TiXmlElement *root = doc.RootElement();
90   if (!root || strcmp(root->Value(), "favourites"))
91   {
92     CLog::Log(LOGERROR, "Favourites.xml doesn't contain the <favourites> root element");
93     return false;
94   }
95
96   TiXmlElement *favourite = root->FirstChildElement("favourite");
97   while (favourite)
98   {
99     // format:
100     // <favourite name="Cool Video" thumb="foo.jpg">PlayMedia(c:\videos\cool_video.avi)</favourite>
101     // <favourite name="My Album" thumb="bar.tbn">ActivateWindow(MyMusic,c:\music\my album)</favourite>
102     // <favourite name="Apple Movie Trailers" thumb="path_to_thumb.png">RunScript(special://xbmc/scripts/apple movie trailers/default.py)</favourite>
103     const char *name = favourite->Attribute("name");
104     const char *thumb = favourite->Attribute("thumb");
105     if (name && favourite->FirstChild())
106     {
107       if(!items.Contains(favourite->FirstChild()->Value()))
108       {
109         CFileItemPtr item(new CFileItem(name));
110         item->SetPath(favourite->FirstChild()->Value());
111         if (thumb) item->SetArt("thumb", thumb);
112         items.Add(item);
113       }
114     }
115     favourite = favourite->NextSiblingElement("favourite");
116   }
117   return true;
118 }
119
120 bool CFavouritesDirectory::Save(const CFileItemList &items)
121 {
122   CStdString favourites;
123   CXBMCTinyXML doc;
124   TiXmlElement xmlRootElement("favourites");
125   TiXmlNode *rootNode = doc.InsertEndChild(xmlRootElement);
126   if (!rootNode) return false;
127
128   for (int i = 0; i < items.Size(); i++)
129   {
130     const CFileItemPtr item = items[i];
131     TiXmlElement favNode("favourite");
132     favNode.SetAttribute("name", item->GetLabel().c_str());
133     if (item->HasArt("thumb"))
134       favNode.SetAttribute("thumb", item->GetArt("thumb").c_str());
135     TiXmlText execute(item->GetPath());
136     favNode.InsertEndChild(execute);
137     rootNode->InsertEndChild(favNode);
138   }
139
140   favourites = URIUtils::AddFileToFolder(CProfilesManager::Get().GetProfileUserDataFolder(), "favourites.xml");
141   return doc.SaveFile(favourites);
142 }
143
144 bool CFavouritesDirectory::AddOrRemove(CFileItem *item, int contextWindow)
145 {
146   if (!item) return false;
147
148   // load our list
149   CFileItemList items;
150   Load(items);
151
152   CStdString executePath(GetExecutePath(*item, contextWindow));
153
154   CFileItemPtr match = items.Get(executePath);
155   if (match)
156   { // remove the item
157     items.Remove(match.get());
158   }
159   else
160   { // create our new favourite item
161     CFileItemPtr favourite(new CFileItem(item->GetLabel()));
162     if (item->GetLabel().IsEmpty())
163       favourite->SetLabel(CUtil::GetTitleFromPath(item->GetPath(), item->m_bIsFolder));
164     favourite->SetArt("thumb", item->GetArt("thumb"));
165     favourite->SetPath(executePath);
166     items.Add(favourite);
167   }
168
169   // and save our list again
170   return Save(items);
171 }
172
173 bool CFavouritesDirectory::IsFavourite(CFileItem *item, int contextWindow)
174 {
175   CFileItemList items;
176   if (!Load(items)) return false;
177
178   return items.Contains(GetExecutePath(*item, contextWindow));
179 }
180
181 CStdString CFavouritesDirectory::GetExecutePath(const CFileItem &item, int contextWindow)
182 {
183   return GetExecutePath(item, StringUtils::Format("%i", contextWindow));
184 }
185
186 CStdString CFavouritesDirectory::GetExecutePath(const CFileItem &item, const std::string &contextWindow)
187 {
188   CStdString execute;
189   if (item.m_bIsFolder && (g_advancedSettings.m_playlistAsFolders ||
190                             !(item.IsSmartPlayList() || item.IsPlayList())))
191   {
192     if (!contextWindow.empty())
193       execute = StringUtils::Format("ActivateWindow(%s,%s,return)", contextWindow.c_str(), StringUtils::Paramify(item.GetPath()).c_str());
194   }
195   else if (item.IsScript())
196     execute = StringUtils::Format("RunScript(%s)", StringUtils::Paramify(item.GetPath().Mid(9)).c_str());
197   else if (item.IsAndroidApp())
198     execute = StringUtils::Format("StartAndroidActivity(%s)", StringUtils::Paramify(item.GetPath().Mid(26)).c_str());
199   else  // assume a media file
200   {
201     if (item.IsVideoDb() && item.HasVideoInfoTag())
202       execute = StringUtils::Format("PlayMedia(%s)", StringUtils::Paramify(item.GetVideoInfoTag()->m_strFileNameAndPath).c_str());
203     else
204       execute = StringUtils::Format("PlayMedia(%s)", StringUtils::Paramify(item.GetPath()).c_str());
205   }
206   return execute;
207 }
208
209 }