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