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 "DirectoryHistory.h"
22 #include "utils/log.h"
23 #include "utils/StringUtils.h"
24 #include "utils/URIUtils.h"
28 const CStdString& CDirectoryHistory::CPathHistoryItem::GetPath(bool filter /* = false */) const
30 if (filter && !m_strFilterPath.empty())
31 return m_strFilterPath;
36 CDirectoryHistory::~CDirectoryHistory()
39 m_vecPathHistory.clear();
42 void CDirectoryHistory::RemoveSelectedItem(const CStdString& strDirectory)
44 HistoryMap::iterator iter = m_vecHistory.find(preparePath(strDirectory));
45 if (iter != m_vecHistory.end())
46 m_vecHistory.erase(iter);
49 void CDirectoryHistory::SetSelectedItem(const CStdString& strSelectedItem, const CStdString& strDirectory)
51 if (strSelectedItem.empty())
54 CStdString strDir = preparePath(strDirectory);
55 CStdString strItem = preparePath(strSelectedItem, false);
57 HistoryMap::iterator iter = m_vecHistory.find(strDir);
58 if (iter != m_vecHistory.end())
60 iter->second.m_strItem = strItem;
65 item.m_strItem = strItem;
66 item.m_strDirectory = strDir;
67 m_vecHistory[strDir] = item;
70 const CStdString& CDirectoryHistory::GetSelectedItem(const CStdString& strDirectory) const
72 HistoryMap::const_iterator iter = m_vecHistory.find(preparePath(strDirectory));
73 if (iter != m_vecHistory.end())
74 return iter->second.m_strItem;
76 return StringUtils::EmptyString;
79 void CDirectoryHistory::AddPath(const CStdString& strPath, const CStdString &strFilterPath /* = "" */)
81 if (!m_vecPathHistory.empty() && m_vecPathHistory.back().m_strPath == strPath)
84 CPathHistoryItem item;
85 item.m_strPath = strPath;
86 item.m_strFilterPath = strFilterPath;
87 m_vecPathHistory.push_back(item);
90 void CDirectoryHistory::AddPathFront(const CStdString& strPath, const CStdString &strFilterPath /* = "" */)
92 CPathHistoryItem item;
93 item.m_strPath = strPath;
94 item.m_strFilterPath = strFilterPath;
95 m_vecPathHistory.insert(m_vecPathHistory.begin(), item);
98 CStdString CDirectoryHistory::GetParentPath(bool filter /* = false */)
100 if (m_vecPathHistory.empty())
101 return StringUtils::EmptyString;
103 return m_vecPathHistory.back().GetPath(filter);
106 CStdString CDirectoryHistory::RemoveParentPath(bool filter /* = false */)
108 if (m_vecPathHistory.empty())
109 return StringUtils::EmptyString;
111 CStdString strParent = GetParentPath(filter);
112 m_vecPathHistory.pop_back();
116 void CDirectoryHistory::ClearPathHistory()
118 m_vecPathHistory.clear();
121 bool CDirectoryHistory::IsMusicSearchUrl(CPathHistoryItem &i)
123 return StringUtils::StartsWith(i.GetPath(), "musicsearch://");
126 void CDirectoryHistory::ClearSearchHistory()
128 m_vecPathHistory.erase(remove_if(m_vecPathHistory.begin(), m_vecPathHistory.end(), IsMusicSearchUrl), m_vecPathHistory.end());
131 void CDirectoryHistory::DumpPathHistory()
134 CLog::Log(LOGDEBUG,"Current m_vecPathHistory:");
135 for (int i = 0; i < (int)m_vecPathHistory.size(); ++i)
136 CLog::Log(LOGDEBUG, " %02i.[%s; %s]", i, m_vecPathHistory[i].m_strPath.c_str(), m_vecPathHistory[i].m_strFilterPath.c_str());
139 CStdString CDirectoryHistory::preparePath(const CStdString &strDirectory, bool tolower /* = true */)
141 CStdString strDir = strDirectory;
143 StringUtils::ToLower(strDir);
145 URIUtils::RemoveSlashAtEnd(strDir);