Merge pull request #2297 from phil65/Votes
[vuplus_xbmc] / xbmc / AutoSwitch.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://www.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 "AutoSwitch.h"
22 #include "settings/Settings.h"
23 #include "settings/GUISettings.h"
24 #include "guilib/GUIWindowManager.h"
25 #include "FileItem.h"
26 #include "guilib/WindowIDs.h"
27 #include "view/ViewState.h"
28
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
34
35 CAutoSwitch::CAutoSwitch(void)
36 {}
37
38 CAutoSwitch::~CAutoSwitch(void)
39 {}
40
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)
44 {
45   int iSortMethod = -1;
46   int iPercent = 0;
47   int iCurrentWindow = g_windowManager.GetActiveWindow();
48   bool bHideParentFolderItems = !g_guiSettings.GetBool("filelists.showparentdiritems");
49
50   switch (iCurrentWindow)
51   {
52   case WINDOW_MUSIC_FILES:
53     {
54       iSortMethod = METHOD_BYFOLDERTHUMBS;
55       iPercent = 50;
56     }
57     break;
58
59   case WINDOW_VIDEO_FILES:
60     {
61       iSortMethod = METHOD_BYTHUMBPERCENT;
62       iPercent = 50;  // 50% of thumbs -> use thumbs.
63     }
64     break;
65
66   case WINDOW_PICTURES:
67     {
68       iSortMethod = METHOD_BYFILECOUNT;
69     }
70     break;
71
72   case WINDOW_PROGRAMS:
73     {
74       iSortMethod = METHOD_BYTHUMBPERCENT;
75       iPercent = 50;  // 50% of thumbs -> use thumbs.
76     }
77     break;
78
79   default:
80     {
81       if(MetadataPercentage(vecItems) > 0.25)
82         return DEFAULT_VIEW_INFO;
83       else
84         return DEFAULT_VIEW_LIST;
85     }
86     break;
87   }
88
89   bool bThumbs = false;
90
91   switch (iSortMethod)
92   {
93   case METHOD_BYFOLDERS:
94     bThumbs = ByFolders(vecItems);
95     break;
96
97   case METHOD_BYFILES:
98     bThumbs = ByFiles(bHideParentFolderItems, vecItems);
99     break;
100
101   case METHOD_BYTHUMBPERCENT:
102     bThumbs = ByThumbPercent(bHideParentFolderItems, iPercent, vecItems);
103     break;
104   case METHOD_BYFILECOUNT:
105     bThumbs = ByFileCount(vecItems);
106     break;
107   case METHOD_BYFOLDERTHUMBS:
108     bThumbs = ByFolderThumbPercentage(bHideParentFolderItems, iPercent, vecItems);
109     break;
110   }
111
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;
115 }
116
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)
120 {
121   bool bThumbs = false;
122   // is the list all folders?
123   if (vecItems.GetFolderCount() == vecItems.Size())
124   {
125     // test for thumbs
126     for (int i = 0; i < vecItems.Size(); i++)
127     {
128       const CFileItemPtr pItem = vecItems[i];
129       if (pItem->HasArt("thumb"))
130       {
131         bThumbs = true;
132         break;
133       }
134     }
135   }
136   return bThumbs;
137 }
138
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)
143 {
144   bool bThumbs = false;
145   int iCompare = 0;
146
147   // parent directorys are visible, incrememt
148   if (!bHideParentDirItems)
149   {
150     iCompare = 1;
151   }
152
153   // confirm the list is not just files and folderback
154   if (vecItems.GetFolderCount() > iCompare)
155   {
156     // test for thumbs
157     for (int i = 0; i < vecItems.Size(); i++)
158     {
159       const CFileItemPtr pItem = vecItems[i];
160       if (pItem->HasArt("thumb"))
161       {
162         bThumbs = true;
163         break;
164       }
165     }
166   }
167   return bThumbs;
168 }
169
170
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)
175 {
176   bool bThumbs = false;
177   int iNumThumbs = 0;
178   int iNumItems = vecItems.Size();
179   if (!bHideParentDirItems)
180   {
181     iNumItems--;
182   }
183
184   if (iNumItems <= 0) return false;
185
186   for (int i = 0; i < vecItems.Size(); i++)
187   {
188     const CFileItemPtr pItem = vecItems[i];
189     if (pItem->HasArt("thumb"))
190     {
191       iNumThumbs++;
192       float fTempPercent = ( (float)iNumThumbs / (float)iNumItems ) * (float)100;
193       if (fTempPercent >= (float)iPercent)
194       {
195         bThumbs = true;
196         break;
197       }
198     }
199   }
200
201   return bThumbs;
202 }
203
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)
207 {
208   if (vecItems.Size() == 0) return false;
209   float fPercent = (float)vecItems.GetFileCount() / vecItems.Size();
210   return (fPercent > 0.25);
211 }
212
213 // returns true if:
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)
217 {
218   int numItems = vecItems.Size();
219   if (!hideParentDirItems)
220     numItems--;
221   if (numItems <= 0) return false;
222
223   int fileCount = vecItems.GetFileCount();
224   if (fileCount > 0.25f * numItems) return false;
225
226   int numThumbs = 0;
227   for (int i = 0; i < vecItems.Size(); i++)
228   {
229     const CFileItemPtr item = vecItems[i];
230     if (item->m_bIsFolder && item->HasArt("thumb"))
231     {
232       numThumbs++;
233       if (numThumbs >= 0.01f * percent * (numItems - fileCount))
234         return true;
235     }
236   }
237
238   return false;
239 }
240
241 float CAutoSwitch::MetadataPercentage(const CFileItemList &vecItems)
242 {
243   int count = 0;
244   int total = vecItems.Size();
245   for (int i = 0; i < vecItems.Size(); i++)
246   {
247     const CFileItemPtr item = vecItems[i];
248     if(item->HasMusicInfoTag()
249     || item->HasVideoInfoTag()
250     || item->HasPictureInfoTag()
251     || item->HasProperty("Addon.ID"))
252       count++;
253     if(item->IsParentFolder())
254       total--;
255   }
256   return (float)count / total;
257 }