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"
22 #include "settings/Settings.h"
23 #include "settings/GUISettings.h"
24 #include "guilib/GUIWindowManager.h"
26 #include "guilib/WindowIDs.h"
27 #include "view/ViewState.h"
29 #define METHOD_BYFOLDERS 0
30 #define METHOD_BYFILES 1
31 #define METHOD_BYTHUMBPERCENT 2
32 #define METHOD_BYFILECOUNT 3
33 #define METHOD_BYFOLDERTHUMBS 4
35 CAutoSwitch::CAutoSwitch(void)
38 CAutoSwitch::~CAutoSwitch(void)
41 /// \brief Generic function to add a layer of transparency to the calling window
42 /// \param vecItems Vector of FileItems passed from the calling window
43 int CAutoSwitch::GetView(const CFileItemList &vecItems)
47 int iCurrentWindow = g_windowManager.GetActiveWindow();
48 bool bHideParentFolderItems = !g_guiSettings.GetBool("filelists.showparentdiritems");
50 switch (iCurrentWindow)
52 case WINDOW_MUSIC_FILES:
54 iSortMethod = METHOD_BYFOLDERTHUMBS;
59 case WINDOW_VIDEO_FILES:
61 iSortMethod = METHOD_BYTHUMBPERCENT;
62 iPercent = 50; // 50% of thumbs -> use thumbs.
68 iSortMethod = METHOD_BYFILECOUNT;
74 iSortMethod = METHOD_BYTHUMBPERCENT;
75 iPercent = 50; // 50% of thumbs -> use thumbs.
81 if(MetadataPercentage(vecItems) > 0.25)
82 return DEFAULT_VIEW_INFO;
84 return DEFAULT_VIEW_LIST;
93 case METHOD_BYFOLDERS:
94 bThumbs = ByFolders(vecItems);
98 bThumbs = ByFiles(bHideParentFolderItems, vecItems);
101 case METHOD_BYTHUMBPERCENT:
102 bThumbs = ByThumbPercent(bHideParentFolderItems, iPercent, vecItems);
104 case METHOD_BYFILECOUNT:
105 bThumbs = ByFileCount(vecItems);
107 case METHOD_BYFOLDERTHUMBS:
108 bThumbs = ByFolderThumbPercentage(bHideParentFolderItems, iPercent, vecItems);
112 // the GUIViewControl object will default down to small icons if a big icon
113 // view is not available.
114 return bThumbs ? DEFAULT_VIEW_BIG_ICONS : DEFAULT_VIEW_LIST;
117 /// \brief Auto Switch method based on the current directory \e containing ALL folders and \e atleast one non-default thumb
118 /// \param vecItems Vector of FileItems
119 bool CAutoSwitch::ByFolders(const CFileItemList& vecItems)
121 bool bThumbs = false;
122 // is the list all folders?
123 if (vecItems.GetFolderCount() == vecItems.Size())
126 for (int i = 0; i < vecItems.Size(); i++)
128 const CFileItemPtr pItem = vecItems[i];
129 if (pItem->HasArt("thumb"))
139 /// \brief Auto Switch method based on the current directory \e not containing ALL files and \e atleast one non-default thumb
140 /// \param bHideParentDirItems - are we not counting the ".." item?
141 /// \param vecItems Vector of FileItems
142 bool CAutoSwitch::ByFiles(bool bHideParentDirItems, const CFileItemList& vecItems)
144 bool bThumbs = false;
147 // parent directorys are visible, incrememt
148 if (!bHideParentDirItems)
153 // confirm the list is not just files and folderback
154 if (vecItems.GetFolderCount() > iCompare)
157 for (int i = 0; i < vecItems.Size(); i++)
159 const CFileItemPtr pItem = vecItems[i];
160 if (pItem->HasArt("thumb"))
171 /// \brief Auto Switch method based on the percentage of non-default thumbs \e in the current directory
172 /// \param iPercent Percent of non-default thumbs to autoswitch on
173 /// \param vecItems Vector of FileItems
174 bool CAutoSwitch::ByThumbPercent(bool bHideParentDirItems, int iPercent, const CFileItemList& vecItems)
176 bool bThumbs = false;
178 int iNumItems = vecItems.Size();
179 if (!bHideParentDirItems)
184 if (iNumItems <= 0) return false;
186 for (int i = 0; i < vecItems.Size(); i++)
188 const CFileItemPtr pItem = vecItems[i];
189 if (pItem->HasArt("thumb"))
192 float fTempPercent = ( (float)iNumThumbs / (float)iNumItems ) * (float)100;
193 if (fTempPercent >= (float)iPercent)
204 /// \brief Auto Switch method based on whether there is more than 25% files.
205 /// \param iPercent Percent of non-default thumbs to autoswitch on
206 bool CAutoSwitch::ByFileCount(const CFileItemList& vecItems)
208 if (vecItems.Size() == 0) return false;
209 float fPercent = (float)vecItems.GetFileCount() / vecItems.Size();
210 return (fPercent > 0.25);
214 // 1. Have more than 75% folders and
215 // 2. Have more than percent folders with thumbs
216 bool CAutoSwitch::ByFolderThumbPercentage(bool hideParentDirItems, int percent, const CFileItemList &vecItems)
218 int numItems = vecItems.Size();
219 if (!hideParentDirItems)
221 if (numItems <= 0) return false;
223 int fileCount = vecItems.GetFileCount();
224 if (fileCount > 0.25f * numItems) return false;
227 for (int i = 0; i < vecItems.Size(); i++)
229 const CFileItemPtr item = vecItems[i];
230 if (item->m_bIsFolder && item->HasArt("thumb"))
233 if (numThumbs >= 0.01f * percent * (numItems - fileCount))
241 float CAutoSwitch::MetadataPercentage(const CFileItemList &vecItems)
244 int total = vecItems.Size();
245 for (int i = 0; i < vecItems.Size(); i++)
247 const CFileItemPtr item = vecItems[i];
248 if(item->HasMusicInfoTag()
249 || item->HasVideoInfoTag()
250 || item->HasPictureInfoTag()
251 || item->HasProperty("Addon.ID"))
253 if(item->IsParentFolder())
256 return (float)count / total;