This repository has been archived by the owner on Sep 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Favourites.cpp
186 lines (167 loc) · 5.92 KB
/
Favourites.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Copyright (C) 2005-2008 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include "Favourites.h"
#include "filesystem/File.h"
#include "Util.h"
#include "guilib/Key.h"
#include "settings/Settings.h"
#include "FileItem.h"
#include "tinyXML/tinyxml.h"
#include "utils/log.h"
#include "utils/URIUtils.h"
#include "settings/AdvancedSettings.h"
#include "video/VideoInfoTag.h"
bool CFavourites::Load(CFileItemList &items)
{
items.Clear();
CStdString favourites;
favourites = "special://xbmc/system/favourites.xml";
if(XFILE::CFile::Exists(favourites))
CFavourites::LoadFavourites(favourites, items);
else
CLog::Log(LOGDEBUG, "CFavourites::Load - no system favourites found, skipping");
URIUtils::AddFileToFolder(g_settings.GetProfileUserDataFolder(), "favourites.xml", favourites);
if(XFILE::CFile::Exists(favourites))
CFavourites::LoadFavourites(favourites, items);
else
CLog::Log(LOGDEBUG, "CFavourites::Load - no userdata favourites found, skipping");
return true;
}
bool CFavourites::LoadFavourites(CStdString& strPath, CFileItemList& items)
{
TiXmlDocument doc;
if (!doc.LoadFile(strPath))
{
CLog::Log(LOGERROR, "Unable to load %s (row %i column %i)", strPath.c_str(), doc.Row(), doc.Column());
return false;
}
TiXmlElement *root = doc.RootElement();
if (!root || strcmp(root->Value(), "favourites"))
{
CLog::Log(LOGERROR, "Favourites.xml doesn't contain the <favourites> root element");
return false;
}
TiXmlElement *favourite = root->FirstChildElement("favourite");
while (favourite)
{
// format:
// <favourite name="Cool Video" thumb="foo.jpg">PlayMedia(c:\videos\cool_video.avi)</favourite>
// <favourite name="My Album" thumb="bar.tbn">ActivateWindow(MyMusic,c:\music\my album)</favourite>
// <favourite name="Apple Movie Trailers" thumb="path_to_thumb.png">RunScript(special://xbmc/scripts/apple movie trailers/default.py)</favourite>
const char *name = favourite->Attribute("name");
const char *thumb = favourite->Attribute("thumb");
if (name && favourite->FirstChild())
{
if(!items.Contains(favourite->FirstChild()->Value()))
{
CFileItemPtr item(new CFileItem(name));
item->SetPath(favourite->FirstChild()->Value());
if (thumb) item->SetThumbnailImage(thumb);
items.Add(item);
}
}
favourite = favourite->NextSiblingElement("favourite");
}
return true;
}
bool CFavourites::Save(const CFileItemList &items)
{
CStdString favourites;
TiXmlDocument doc;
TiXmlElement xmlRootElement("favourites");
TiXmlNode *rootNode = doc.InsertEndChild(xmlRootElement);
if (!rootNode) return false;
for (int i = 0; i < items.Size(); i++)
{
const CFileItemPtr item = items[i];
TiXmlElement favNode("favourite");
favNode.SetAttribute("name", item->GetLabel().c_str());
if (item->HasThumbnail())
favNode.SetAttribute("thumb", item->GetThumbnailImage().c_str());
TiXmlText execute(item->GetPath());
favNode.InsertEndChild(execute);
rootNode->InsertEndChild(favNode);
}
URIUtils::AddFileToFolder(g_settings.GetProfileUserDataFolder(), "favourites.xml", favourites);
return doc.SaveFile(favourites);
}
bool CFavourites::AddOrRemove(CFileItem *item, int contextWindow)
{
if (!item) return false;
// load our list
CFileItemList items;
Load(items);
CStdString executePath(GetExecutePath(item, contextWindow));
CFileItemPtr match = items.Get(executePath);
if (match)
{ // remove the item
items.Remove(match.get());
}
else
{ // create our new favourite item
CFileItemPtr favourite(new CFileItem(item->GetLabel()));
if (item->GetLabel().IsEmpty())
favourite->SetLabel(CUtil::GetTitleFromPath(item->GetPath(), item->m_bIsFolder));
favourite->SetThumbnailImage(item->GetThumbnailImage());
favourite->SetPath(executePath);
items.Add(favourite);
}
// and save our list again
return Save(items);
}
bool CFavourites::IsFavourite(CFileItem *item, int contextWindow)
{
CFileItemList items;
if (!Load(items)) return false;
return items.Contains(GetExecutePath(item, contextWindow));
}
static CStdString Paramify(const CStdString& param)
{
CStdString result(param);
result.Replace("\\", "\\\\");
result.Replace("\"", "\\\"");
return "\"" + result + "\"";
}
#ifdef UNIT_TESTING
bool CFavourites::TestParamify()
{
return (Paramify("test") == "\"test\"" &&
Paramify("test\"foo\"test") == "\"test\\\"foo\\\"test\"" &&
Paramify("C:\\foo\\bar\\") == "\"C:\\\\foo\\\\bar\\\\\"");
}
#endif
CStdString CFavourites::GetExecutePath(const CFileItem *item, int contextWindow)
{
CStdString execute;
if (item->m_bIsFolder && (g_advancedSettings.m_playlistAsFolders ||
!(item->IsSmartPlayList() || item->IsPlayList())))
execute.Format("ActivateWindow(%i,%s)", contextWindow, Paramify(item->GetPath()));
else if (item->IsScript())
execute.Format("RunScript(%s)", Paramify(item->GetPath().Mid(9)));
else // assume a media file
{
if (item->IsVideoDb() && item->HasVideoInfoTag())
execute.Format("PlayMedia(%s)", Paramify(item->GetVideoInfoTag()->m_strFileNameAndPath));
else
execute.Format("PlayMedia(%s)", Paramify(item->GetPath()));
}
return execute;
}