PageRenderTime 36ms CodeModel.GetById 13ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/xbmc/AutoSwitch.cpp

http://github.com/xbmc/xbmc
C++ | 232 lines | 174 code | 29 blank | 29 comment | 32 complexity | 7bbae78e7f33cdc0bf31738982af6edb MD5 | raw file
  1/*
  2 *  Copyright (C) 2005-2018 Team Kodi
  3 *  This file is part of Kodi - https://kodi.tv
  4 *
  5 *  SPDX-License-Identifier: GPL-2.0-or-later
  6 *  See LICENSES/README.md for more information.
  7 */
  8
  9#include "AutoSwitch.h"
 10
 11#include "FileItem.h"
 12#include "ServiceBroker.h"
 13#include "guilib/GUIComponent.h"
 14#include "guilib/GUIWindowManager.h"
 15#include "guilib/WindowIDs.h"
 16#include "settings/Settings.h"
 17#include "settings/SettingsComponent.h"
 18#include "view/ViewState.h"
 19
 20#define METHOD_BYFOLDERS  0
 21#define METHOD_BYFILES   1
 22#define METHOD_BYTHUMBPERCENT 2
 23#define METHOD_BYFILECOUNT 3
 24#define METHOD_BYFOLDERTHUMBS 4
 25
 26CAutoSwitch::CAutoSwitch(void) = default;
 27
 28CAutoSwitch::~CAutoSwitch(void) = default;
 29
 30/// \brief Generic function to add a layer of transparency to the calling window
 31/// \param vecItems Vector of FileItems passed from the calling window
 32int CAutoSwitch::GetView(const CFileItemList &vecItems)
 33{
 34  int iSortMethod = -1;
 35  int iPercent = 0;
 36  int iCurrentWindow = CServiceBroker::GetGUI()->GetWindowManager().GetActiveWindow();
 37  bool bHideParentFolderItems = !CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(CSettings::SETTING_FILELISTS_SHOWPARENTDIRITEMS);
 38
 39  switch (iCurrentWindow)
 40  {
 41  case WINDOW_PICTURES:
 42    {
 43      iSortMethod = METHOD_BYFILECOUNT;
 44    }
 45    break;
 46
 47  case WINDOW_PROGRAMS:
 48    {
 49      iSortMethod = METHOD_BYTHUMBPERCENT;
 50      iPercent = 50;  // 50% of thumbs -> use thumbs.
 51    }
 52    break;
 53
 54  default:
 55    {
 56      if(MetadataPercentage(vecItems) > 0.25)
 57        return DEFAULT_VIEW_INFO;
 58      else
 59        return DEFAULT_VIEW_LIST;
 60    }
 61    break;
 62  }
 63
 64  bool bThumbs = false;
 65
 66  switch (iSortMethod)
 67  {
 68  case METHOD_BYFOLDERS:
 69    bThumbs = ByFolders(vecItems);
 70    break;
 71
 72  case METHOD_BYFILES:
 73    bThumbs = ByFiles(bHideParentFolderItems, vecItems);
 74    break;
 75
 76  case METHOD_BYTHUMBPERCENT:
 77    bThumbs = ByThumbPercent(bHideParentFolderItems, iPercent, vecItems);
 78    break;
 79  case METHOD_BYFILECOUNT:
 80    bThumbs = ByFileCount(vecItems);
 81    break;
 82  case METHOD_BYFOLDERTHUMBS:
 83    bThumbs = ByFolderThumbPercentage(bHideParentFolderItems, iPercent, vecItems);
 84    break;
 85  }
 86
 87  // the GUIViewControl object will default down to small icons if a big icon
 88  // view is not available.
 89  return bThumbs ? DEFAULT_VIEW_BIG_ICONS : DEFAULT_VIEW_LIST;
 90}
 91
 92/// \brief Auto Switch method based on the current directory \e containing ALL folders and \e atleast one non-default thumb
 93/// \param vecItems Vector of FileItems
 94bool CAutoSwitch::ByFolders(const CFileItemList& vecItems)
 95{
 96  bool bThumbs = false;
 97  // is the list all folders?
 98  if (vecItems.GetFolderCount() == vecItems.Size())
 99  {
100    // test for thumbs
101    for (int i = 0; i < vecItems.Size(); i++)
102    {
103      const CFileItemPtr pItem = vecItems[i];
104      if (pItem->HasArt("thumb"))
105      {
106        bThumbs = true;
107        break;
108      }
109    }
110  }
111  return bThumbs;
112}
113
114/// \brief Auto Switch method based on the current directory \e not containing ALL files and \e atleast one non-default thumb
115/// \param bHideParentDirItems - are we not counting the ".." item?
116/// \param vecItems Vector of FileItems
117bool CAutoSwitch::ByFiles(bool bHideParentDirItems, const CFileItemList& vecItems)
118{
119  bool bThumbs = false;
120  int iCompare = 0;
121
122  // parent directorys are visible, increment
123  if (!bHideParentDirItems)
124  {
125    iCompare = 1;
126  }
127
128  // confirm the list is not just files and folderback
129  if (vecItems.GetFolderCount() > iCompare)
130  {
131    // test for thumbs
132    for (int i = 0; i < vecItems.Size(); i++)
133    {
134      const CFileItemPtr pItem = vecItems[i];
135      if (pItem->HasArt("thumb"))
136      {
137        bThumbs = true;
138        break;
139      }
140    }
141  }
142  return bThumbs;
143}
144
145
146/// \brief Auto Switch method based on the percentage of non-default thumbs \e in the current directory
147/// \param iPercent Percent of non-default thumbs to autoswitch on
148/// \param vecItems Vector of FileItems
149bool CAutoSwitch::ByThumbPercent(bool bHideParentDirItems, int iPercent, const CFileItemList& vecItems)
150{
151  bool bThumbs = false;
152  int iNumThumbs = 0;
153  int iNumItems = vecItems.Size();
154  if (!bHideParentDirItems)
155  {
156    iNumItems--;
157  }
158
159  if (iNumItems <= 0) return false;
160
161  for (int i = 0; i < vecItems.Size(); i++)
162  {
163    const CFileItemPtr pItem = vecItems[i];
164    if (pItem->HasArt("thumb"))
165    {
166      iNumThumbs++;
167      float fTempPercent = ( (float)iNumThumbs / (float)iNumItems ) * (float)100;
168      if (fTempPercent >= (float)iPercent)
169      {
170        bThumbs = true;
171        break;
172      }
173    }
174  }
175
176  return bThumbs;
177}
178
179/// \brief Auto Switch method based on whether there is more than 25% files.
180/// \param iPercent Percent of non-default thumbs to autoswitch on
181bool CAutoSwitch::ByFileCount(const CFileItemList& vecItems)
182{
183  if (vecItems.Size() == 0) return false;
184  float fPercent = (float)vecItems.GetFileCount() / vecItems.Size();
185  return (fPercent > 0.25);
186}
187
188// returns true if:
189// 1. Have more than 75% folders and
190// 2. Have more than percent folders with thumbs
191bool CAutoSwitch::ByFolderThumbPercentage(bool hideParentDirItems, int percent, const CFileItemList &vecItems)
192{
193  int numItems = vecItems.Size();
194  if (!hideParentDirItems)
195    numItems--;
196  if (numItems <= 0) return false;
197
198  int fileCount = vecItems.GetFileCount();
199  if (fileCount > 0.25f * numItems) return false;
200
201  int numThumbs = 0;
202  for (int i = 0; i < vecItems.Size(); i++)
203  {
204    const CFileItemPtr item = vecItems[i];
205    if (item->m_bIsFolder && item->HasArt("thumb"))
206    {
207      numThumbs++;
208      if (numThumbs >= 0.01f * percent * (numItems - fileCount))
209        return true;
210    }
211  }
212
213  return false;
214}
215
216float CAutoSwitch::MetadataPercentage(const CFileItemList &vecItems)
217{
218  int count = 0;
219  int total = vecItems.Size();
220  for (int i = 0; i < vecItems.Size(); i++)
221  {
222    const CFileItemPtr item = vecItems[i];
223    if(item->HasMusicInfoTag()
224    || item->HasVideoInfoTag()
225    || item->HasPictureInfoTag()
226    || item->HasProperty("Addon.ID"))
227      count++;
228    if(item->IsParentFolder())
229      total--;
230  }
231  return (total != 0) ? ((float)count / total) : 0.0f;
232}