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