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/>.
21 #include "FavouritesDirectory.h"
24 #include "profiles/ProfilesManager.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"
38 bool CFavouritesDirectory::GetDirectory(const CStdString& strPath, CFileItemList &items)
43 if (url.GetProtocol() == "favourites")
45 Load(items); //load the default favourite files
47 return LoadFavourites(strPath, items); //directly load the given file
50 bool CFavouritesDirectory::Exists(const char* strPath)
54 if (url.GetProtocol() == "favourites")
56 return XFILE::CFile::Exists("special://xbmc/system/favourites.xml")
57 || XFILE::CFile::Exists(URIUtils::AddFileToFolder(CProfilesManager::Get().GetProfileUserDataFolder(), "favourites.xml"));
59 return XFILE::CFile::Exists(strPath); //directly load the given file
62 bool CFavouritesDirectory::Load(CFileItemList &items)
65 CStdString favourites;
67 favourites = "special://xbmc/system/favourites.xml";
68 if(XFILE::CFile::Exists(favourites))
69 CFavouritesDirectory::LoadFavourites(favourites, items);
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);
76 CLog::Log(LOGDEBUG, "CFavourites::Load - no userdata favourites found, skipping");
81 bool CFavouritesDirectory::LoadFavourites(const CStdString& strPath, CFileItemList& items)
84 if (!doc.LoadFile(strPath))
86 CLog::Log(LOGERROR, "Unable to load %s (row %i column %i)", strPath.c_str(), doc.Row(), doc.Column());
89 TiXmlElement *root = doc.RootElement();
90 if (!root || strcmp(root->Value(), "favourites"))
92 CLog::Log(LOGERROR, "Favourites.xml doesn't contain the <favourites> root element");
96 TiXmlElement *favourite = root->FirstChildElement("favourite");
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())
107 if(!items.Contains(favourite->FirstChild()->Value()))
109 CFileItemPtr item(new CFileItem(name));
110 item->SetPath(favourite->FirstChild()->Value());
111 if (thumb) item->SetArt("thumb", thumb);
115 favourite = favourite->NextSiblingElement("favourite");
120 bool CFavouritesDirectory::Save(const CFileItemList &items)
122 CStdString favourites;
124 TiXmlElement xmlRootElement("favourites");
125 TiXmlNode *rootNode = doc.InsertEndChild(xmlRootElement);
126 if (!rootNode) return false;
128 for (int i = 0; i < items.Size(); i++)
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);
140 favourites = URIUtils::AddFileToFolder(CProfilesManager::Get().GetProfileUserDataFolder(), "favourites.xml");
141 return doc.SaveFile(favourites);
144 bool CFavouritesDirectory::AddOrRemove(CFileItem *item, int contextWindow)
146 if (!item) return false;
152 CStdString executePath(GetExecutePath(*item, contextWindow));
154 CFileItemPtr match = items.Get(executePath);
157 items.Remove(match.get());
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);
169 // and save our list again
173 bool CFavouritesDirectory::IsFavourite(CFileItem *item, int contextWindow)
176 if (!Load(items)) return false;
178 return items.Contains(GetExecutePath(*item, contextWindow));
181 CStdString CFavouritesDirectory::GetExecutePath(const CFileItem &item, int contextWindow)
183 return GetExecutePath(item, StringUtils::Format("%i", contextWindow));
186 CStdString CFavouritesDirectory::GetExecutePath(const CFileItem &item, const std::string &contextWindow)
189 if (item.m_bIsFolder && (g_advancedSettings.m_playlistAsFolders ||
190 !(item.IsSmartPlayList() || item.IsPlayList())))
192 if (!contextWindow.empty())
193 execute = StringUtils::Format("ActivateWindow(%s,%s,return)", contextWindow.c_str(), StringUtils::Paramify(item.GetPath()).c_str());
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
201 if (item.IsVideoDb() && item.HasVideoInfoTag())
202 execute = StringUtils::Format("PlayMedia(%s)", StringUtils::Paramify(item.GetVideoInfoTag()->m_strFileNameAndPath).c_str());
204 execute = StringUtils::Format("PlayMedia(%s)", StringUtils::Paramify(item.GetPath()).c_str());