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 "AutoSwitch.h"
23 #include "guilib/GUIWindowManager.h"
24 #include "guilib/WindowIDs.h"
25 #include "settings/Settings.h"
26 #include "view/ViewState.h"
28 #define METHOD_BYFOLDERS 0
29 #define METHOD_BYFILES 1
30 #define METHOD_BYTHUMBPERCENT 2
31 #define METHOD_BYFILECOUNT 3
32 #define METHOD_BYFOLDERTHUMBS 4
34 CAutoSwitch::CAutoSwitch(void)
37 CAutoSwitch::~CAutoSwitch(void)
40 /// \brief Generic function to add a layer of transparency to the calling window
41 /// \param vecItems Vector of FileItems passed from the calling window
42 int CAutoSwitch::GetView(const CFileItemList &vecItems)
46 int iCurrentWindow = g_windowManager.GetActiveWindow();
47 bool bHideParentFolderItems = !CSettings::Get().GetBool("filelists.showparentdiritems");
49 switch (iCurrentWindow)
51 case WINDOW_MUSIC_FILES:
53 iSortMethod = METHOD_BYFOLDERTHUMBS;
58 case WINDOW_VIDEO_FILES:
60 iSortMethod = METHOD_BYTHUMBPERCENT;
61 iPercent = 50; // 50% of thumbs -> use thumbs.
67 iSortMethod = METHOD_BYFILECOUNT;
73 iSortMethod = METHOD_BYTHUMBPERCENT;
74 iPercent = 50; // 50% of thumbs -> use thumbs.
80 if(MetadataPercentage(vecItems) > 0.25)
81 return DEFAULT_VIEW_INFO;
83 return DEFAULT_VIEW_LIST;
92 case METHOD_BYFOLDERS:
93 bThumbs = ByFolders(vecItems);
97 bThumbs = ByFiles(bHideParentFolderItems, vecItems);
100 case METHOD_BYTHUMBPERCENT:
101 bThumbs = ByThumbPercent(bHideParentFolderItems, iPercent, vecItems);
103 case METHOD_BYFILECOUNT:
104 bThumbs = ByFileCount(vecItems);
106 case METHOD_BYFOLDERTHUMBS:
107 bThumbs = ByFolderThumbPercentage(bHideParentFolderItems, iPercent, vecItems);
111 // the GUIViewControl object will default down to small icons if a big icon
112 // view is not available.
113 return bThumbs ? DEFAULT_VIEW_BIG_ICONS : DEFAULT_VIEW_LIST;
116 /// \brief Auto Switch method based on the current directory \e containing ALL folders and \e atleast one non-default thumb
117 /// \param vecItems Vector of FileItems
118 bool CAutoSwitch::ByFolders(const CFileItemList& vecItems)
120 bool bThumbs = false;
121 // is the list all folders?
122 if (vecItems.GetFolderCount() == vecItems.Size())
125 for (int i = 0; i < vecItems.Size(); i++)
127 const CFileItemPtr pItem = vecItems[i];
128 if (pItem->HasArt("thumb"))
138 /// \brief Auto Switch method based on the current directory \e not containing ALL files and \e atleast one non-default thumb
139 /// \param bHideParentDirItems - are we not counting the ".." item?
140 /// \param vecItems Vector of FileItems
141 bool CAutoSwitch::ByFiles(bool bHideParentDirItems, const CFileItemList& vecItems)
143 bool bThumbs = false;
146 // parent directorys are visible, incrememt
147 if (!bHideParentDirItems)
152 // confirm the list is not just files and folderback
153 if (vecItems.GetFolderCount() > iCompare)
156 for (int i = 0; i < vecItems.Size(); i++)
158 const CFileItemPtr pItem = vecItems[i];
159 if (pItem->HasArt("thumb"))
170 /// \brief Auto Switch method based on the percentage of non-default thumbs \e in the current directory
171 /// \param iPercent Percent of non-default thumbs to autoswitch on
172 /// \param vecItems Vector of FileItems
173 bool CAutoSwitch::ByThumbPercent(bool bHideParentDirItems, int iPercent, const CFileItemList& vecItems)
175 bool bThumbs = false;
177 int iNumItems = vecItems.Size();
178 if (!bHideParentDirItems)
183 if (iNumItems <= 0) return false;
185 for (int i = 0; i < vecItems.Size(); i++)
187 const CFileItemPtr pItem = vecItems[i];
188 if (pItem->HasArt("thumb"))
191 float fTempPercent = ( (float)iNumThumbs / (float)iNumItems ) * (float)100;
192 if (fTempPercent >= (float)iPercent)
203 /// \brief Auto Switch method based on whether there is more than 25% files.
204 /// \param iPercent Percent of non-default thumbs to autoswitch on
205 bool CAutoSwitch::ByFileCount(const CFileItemList& vecItems)
207 if (vecItems.Size() == 0) return false;
208 float fPercent = (float)vecItems.GetFileCount() / vecItems.Size();
209 return (fPercent > 0.25);
213 // 1. Have more than 75% folders and
214 // 2. Have more than percent folders with thumbs
215 bool CAutoSwitch::ByFolderThumbPercentage(bool hideParentDirItems, int percent, const CFileItemList &vecItems)
217 int numItems = vecItems.Size();
218 if (!hideParentDirItems)
220 if (numItems <= 0) return false;
222 int fileCount = vecItems.GetFileCount();
223 if (fileCount > 0.25f * numItems) return false;
226 for (int i = 0; i < vecItems.Size(); i++)
228 const CFileItemPtr item = vecItems[i];
229 if (item->m_bIsFolder && item->HasArt("thumb"))
232 if (numThumbs >= 0.01f * percent * (numItems - fileCount))
240 float CAutoSwitch::MetadataPercentage(const CFileItemList &vecItems)
243 int total = vecItems.Size();
244 for (int i = 0; i < vecItems.Size(); i++)
246 const CFileItemPtr item = vecItems[i];
247 if(item->HasMusicInfoTag()
248 || item->HasVideoInfoTag()
249 || item->HasPictureInfoTag()
250 || item->HasProperty("Addon.ID"))
252 if(item->IsParentFolder())
255 return (float)count / total;