Merge pull request #4857 from t-nelson/Gotham_13.2_backports
[vuplus_xbmc] / xbmc / filesystem / DirectoryHistory.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 "DirectoryHistory.h"
22 #include "utils/log.h"
23 #include "utils/StringUtils.h"
24 #include "utils/URIUtils.h"
25
26 using namespace std;
27
28 const CStdString& CDirectoryHistory::CPathHistoryItem::GetPath(bool filter /* = false */) const
29 {
30   if (filter && !m_strFilterPath.empty())
31     return m_strFilterPath;
32
33   return m_strPath;
34 }
35
36 CDirectoryHistory::~CDirectoryHistory()
37 {
38   m_vecHistory.clear();
39   m_vecPathHistory.clear();
40 }
41
42 void CDirectoryHistory::RemoveSelectedItem(const CStdString& strDirectory)
43 {
44   HistoryMap::iterator iter = m_vecHistory.find(preparePath(strDirectory));
45   if (iter != m_vecHistory.end())
46     m_vecHistory.erase(iter);
47 }
48
49 void CDirectoryHistory::SetSelectedItem(const CStdString& strSelectedItem, const CStdString& strDirectory)
50 {
51   if (strSelectedItem.empty())
52     return;
53   
54   CStdString strDir = preparePath(strDirectory);
55   CStdString strItem = preparePath(strSelectedItem, false);
56   
57   HistoryMap::iterator iter = m_vecHistory.find(strDir);
58   if (iter != m_vecHistory.end())
59   {
60     iter->second.m_strItem = strItem;
61     return;
62   }
63
64   CHistoryItem item;
65   item.m_strItem = strItem;
66   item.m_strDirectory = strDir;
67   m_vecHistory[strDir] = item;
68 }
69
70 const CStdString& CDirectoryHistory::GetSelectedItem(const CStdString& strDirectory) const
71 {
72   HistoryMap::const_iterator iter = m_vecHistory.find(preparePath(strDirectory));
73   if (iter != m_vecHistory.end())
74     return iter->second.m_strItem;
75
76   return StringUtils::EmptyString;
77 }
78
79 void CDirectoryHistory::AddPath(const CStdString& strPath, const CStdString &strFilterPath /* = "" */)
80 {
81   if (!m_vecPathHistory.empty() && m_vecPathHistory.back().m_strPath == strPath)
82     return;
83
84   CPathHistoryItem item;
85   item.m_strPath = strPath;
86   item.m_strFilterPath = strFilterPath;
87   m_vecPathHistory.push_back(item);
88 }
89
90 void CDirectoryHistory::AddPathFront(const CStdString& strPath, const CStdString &strFilterPath /* = "" */)
91 {
92   CPathHistoryItem item;
93   item.m_strPath = strPath;
94   item.m_strFilterPath = strFilterPath;
95   m_vecPathHistory.insert(m_vecPathHistory.begin(), item);
96 }
97
98 CStdString CDirectoryHistory::GetParentPath(bool filter /* = false */)
99 {
100   if (m_vecPathHistory.empty())
101     return StringUtils::EmptyString;
102
103   return m_vecPathHistory.back().GetPath(filter);
104 }
105
106 CStdString CDirectoryHistory::RemoveParentPath(bool filter /* = false */)
107 {
108   if (m_vecPathHistory.empty())
109     return StringUtils::EmptyString;
110
111   CStdString strParent = GetParentPath(filter);
112   m_vecPathHistory.pop_back();
113   return strParent;
114 }
115
116 void CDirectoryHistory::ClearPathHistory()
117 {
118   m_vecPathHistory.clear();
119 }
120
121 bool CDirectoryHistory::IsMusicSearchUrl(CPathHistoryItem &i)
122 {
123   return StringUtils::StartsWith(i.GetPath(), "musicsearch://");
124 }
125
126 void CDirectoryHistory::ClearSearchHistory()
127 {
128   m_vecPathHistory.erase(remove_if(m_vecPathHistory.begin(), m_vecPathHistory.end(), IsMusicSearchUrl), m_vecPathHistory.end());
129 }
130
131 void CDirectoryHistory::DumpPathHistory()
132 {
133   // debug log
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());
137 }
138
139 CStdString CDirectoryHistory::preparePath(const CStdString &strDirectory, bool tolower /* = true */)
140 {
141   CStdString strDir = strDirectory;
142   if (tolower)
143     StringUtils::ToLower(strDir);
144
145   URIUtils::RemoveSlashAtEnd(strDir);
146
147   return strDir;
148 }