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 "GUIListItem.h"
22 #include "GUIListItemLayout.h"
23 #include "utils/Archive.h"
24 #include "utils/CharsetConverter.h"
25 #include "utils/Variant.h"
29 CGUIListItem::CGUIListItem(const CGUIListItem& item)
32 m_focusedLayout = NULL;
37 CGUIListItem::CGUIListItem(void)
44 m_overlayIcon = ICON_OVERLAY_NONE;
46 m_focusedLayout = NULL;
49 CGUIListItem::CGUIListItem(const CStdString& strLabel)
53 m_strLabel = strLabel;
54 SetSortLabel(strLabel);
57 m_overlayIcon = ICON_OVERLAY_NONE;
59 m_focusedLayout = NULL;
62 CGUIListItem::~CGUIListItem(void)
67 void CGUIListItem::SetLabel(const CStdString& strLabel)
69 if (m_strLabel == strLabel)
71 m_strLabel = strLabel;
72 if (m_sortLabel.IsEmpty())
73 SetSortLabel(strLabel);
77 const CStdString& CGUIListItem::GetLabel() const
83 void CGUIListItem::SetLabel2(const CStdString& strLabel2)
85 if (m_strLabel2 == strLabel2)
87 m_strLabel2 = strLabel2;
91 const CStdString& CGUIListItem::GetLabel2() const
96 void CGUIListItem::SetSortLabel(const CStdString &label)
98 g_charsetConverter.utf8ToW(label, m_sortLabel, false);
99 // no need to invalidate - this is never shown in the UI
102 void CGUIListItem::SetSortLabel(const CStdStringW &label)
107 const CStdStringW& CGUIListItem::GetSortLabel() const
112 void CGUIListItem::SetArt(const std::string &type, const std::string &url)
114 ArtMap::iterator i = m_art.find(type);
115 if (i == m_art.end() || i->second != url)
122 void CGUIListItem::SetArt(const ArtMap &art)
128 void CGUIListItem::SetArtFallback(const std::string &from, const std::string &to)
130 m_artFallbacks[from] = to;
133 void CGUIListItem::ClearArt()
136 m_artFallbacks.clear();
139 void CGUIListItem::AppendArt(const ArtMap &art, const std::string &prefix)
141 for (ArtMap::const_iterator i = art.begin(); i != art.end(); ++i)
142 SetArt(prefix.empty() ? i->first : prefix + '.' + i->first, i->second);
145 std::string CGUIListItem::GetArt(const std::string &type) const
147 ArtMap::const_iterator i = m_art.find(type);
148 if (i != m_art.end())
150 i = m_artFallbacks.find(type);
151 if (i != m_artFallbacks.end())
153 ArtMap::const_iterator j = m_art.find(i->second);
154 if (j != m_art.end())
160 const CGUIListItem::ArtMap &CGUIListItem::GetArt() const
165 bool CGUIListItem::HasArt(const std::string &type) const
167 return !GetArt(type).empty();
170 void CGUIListItem::SetIconImage(const CStdString& strIcon)
172 if (m_strIcon == strIcon)
178 const CStdString& CGUIListItem::GetIconImage() const
183 void CGUIListItem::SetOverlayImage(GUIIconOverlay icon, bool bOnOff)
185 GUIIconOverlay newIcon = (bOnOff) ? GUIIconOverlay((int)(icon)+1) : icon;
186 if (m_overlayIcon == newIcon)
188 m_overlayIcon = newIcon;
192 CStdString CGUIListItem::GetOverlayImage() const
194 switch (m_overlayIcon)
196 case ICON_OVERLAY_RAR:
197 return "OverlayRAR.png";
198 case ICON_OVERLAY_ZIP:
199 return "OverlayZIP.png";
200 case ICON_OVERLAY_TRAINED:
201 return "OverlayTrained.png";
202 case ICON_OVERLAY_HAS_TRAINER:
203 return "OverlayHasTrainer.png";
204 case ICON_OVERLAY_LOCKED:
205 return "OverlayLocked.png";
206 case ICON_OVERLAY_UNWATCHED:
207 return "OverlayUnwatched.png";
208 case ICON_OVERLAY_WATCHED:
209 return "OverlayWatched.png";
210 case ICON_OVERLAY_HD:
211 return "OverlayHD.png";
217 void CGUIListItem::Select(bool bOnOff)
219 m_bSelected = bOnOff;
222 bool CGUIListItem::HasIcon() const
224 return (m_strIcon.size() != 0);
227 bool CGUIListItem::HasOverlay() const
229 return (m_overlayIcon != CGUIListItem::ICON_OVERLAY_NONE);
232 bool CGUIListItem::IsSelected() const
237 const CGUIListItem& CGUIListItem::operator =(const CGUIListItem& item)
239 if (&item == this) return * this;
240 m_strLabel2 = item.m_strLabel2;
241 m_strLabel = item.m_strLabel;
242 m_sortLabel = item.m_sortLabel;
244 m_bSelected = item.m_bSelected;
245 m_strIcon = item.m_strIcon;
246 m_overlayIcon = item.m_overlayIcon;
247 m_bIsFolder = item.m_bIsFolder;
248 m_mapProperties = item.m_mapProperties;
250 m_artFallbacks = item.m_artFallbacks;
255 void CGUIListItem::Archive(CArchive &ar)
266 ar << (int)m_mapProperties.size();
267 for (PropertyMap::const_iterator it = m_mapProperties.begin(); it != m_mapProperties.end(); it++)
272 ar << (int)m_art.size();
273 for (ArtMap::const_iterator i = m_art.begin(); i != m_art.end(); i++)
278 ar << (int)m_artFallbacks.size();
279 for (ArtMap::const_iterator i = m_artFallbacks.begin(); i != m_artFallbacks.end(); i++)
296 m_overlayIcon = GUIIconOverlay(overlayIcon);
300 for (int i = 0; i < mapSize; i++)
306 SetProperty(key, value);
309 for (int i = 0; i < mapSize; i++)
311 std::string key, value;
314 m_art.insert(make_pair(key, value));
317 for (int i = 0; i < mapSize; i++)
319 std::string key, value;
322 m_artFallbacks.insert(make_pair(key, value));
326 void CGUIListItem::Serialize(CVariant &value)
328 value["isFolder"] = m_bIsFolder;
329 value["strLabel"] = m_strLabel;
330 value["strLabel2"] = m_strLabel2;
331 value["sortLabel"] = m_sortLabel;
332 value["strIcon"] = m_strIcon;
333 value["selected"] = m_bSelected;
335 for (PropertyMap::const_iterator it = m_mapProperties.begin(); it != m_mapProperties.end(); it++)
337 value["properties"][it->first] = it->second;
339 for (ArtMap::const_iterator it = m_art.begin(); it != m_art.end(); it++)
340 value["art"][it->first] = it->second;
343 void CGUIListItem::FreeIcons()
351 void CGUIListItem::FreeMemory(bool immediately)
355 m_layout->FreeResources(immediately);
361 m_focusedLayout->FreeResources(immediately);
362 delete m_focusedLayout;
363 m_focusedLayout = NULL;
367 void CGUIListItem::SetLayout(CGUIListItemLayout *layout)
373 CGUIListItemLayout *CGUIListItem::GetLayout()
378 void CGUIListItem::SetFocusedLayout(CGUIListItemLayout *layout)
380 delete m_focusedLayout;
381 m_focusedLayout = layout;
384 CGUIListItemLayout *CGUIListItem::GetFocusedLayout()
386 return m_focusedLayout;
389 void CGUIListItem::SetInvalid()
391 if (m_layout) m_layout->SetInvalid();
392 if (m_focusedLayout) m_focusedLayout->SetInvalid();
395 void CGUIListItem::SetProperty(const CStdString &strKey, const CVariant &value)
397 m_mapProperties[strKey] = value;
400 CVariant CGUIListItem::GetProperty(const CStdString &strKey) const
402 PropertyMap::const_iterator iter = m_mapProperties.find(strKey);
403 if (iter == m_mapProperties.end())
404 return CVariant(CVariant::VariantTypeNull);
409 bool CGUIListItem::HasProperty(const CStdString &strKey) const
411 PropertyMap::const_iterator iter = m_mapProperties.find(strKey);
412 if (iter == m_mapProperties.end())
418 void CGUIListItem::ClearProperty(const CStdString &strKey)
420 PropertyMap::iterator iter = m_mapProperties.find(strKey);
421 if (iter != m_mapProperties.end())
422 m_mapProperties.erase(iter);
425 void CGUIListItem::ClearProperties()
427 m_mapProperties.clear();
430 void CGUIListItem::IncrementProperty(const CStdString &strKey, int nVal)
432 int64_t i = GetProperty(strKey).asInteger();
434 SetProperty(strKey, i);
437 void CGUIListItem::IncrementProperty(const CStdString &strKey, double dVal)
439 double d = GetProperty(strKey).asDouble();
441 SetProperty(strKey, d);
444 void CGUIListItem::AppendProperties(const CGUIListItem &item)
446 for (PropertyMap::const_iterator i = item.m_mapProperties.begin(); i != item.m_mapProperties.end(); ++i)
447 SetProperty(i->first, i->second);