2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "AutoSwitch.h"
12 #include "FileItemList.h"
13 #include "ServiceBroker.h"
14 #include "guilib/GUIComponent.h"
15 #include "guilib/GUIWindowManager.h"
16 #include "guilib/WindowIDs.h"
17 #include "settings/Settings.h"
18 #include "settings/SettingsComponent.h"
19 #include "view/ViewState.h"
21 #define METHOD_BYFOLDERS 0
22 #define METHOD_BYFILES 1
23 #define METHOD_BYTHUMBPERCENT 2
24 #define METHOD_BYFILECOUNT 3
25 #define METHOD_BYFOLDERTHUMBS 4
27 CAutoSwitch::CAutoSwitch(void) = default;
29 CAutoSwitch::~CAutoSwitch(void) = default;
31 /// \brief Generic function to add a layer of transparency to the calling window
32 /// \param vecItems Vector of FileItems passed from the calling window
33 int CAutoSwitch::GetView(const CFileItemList
&vecItems
)
37 int iCurrentWindow
= CServiceBroker::GetGUI()->GetWindowManager().GetActiveWindow();
38 bool bHideParentFolderItems
= !CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(CSettings::SETTING_FILELISTS_SHOWPARENTDIRITEMS
);
40 switch (iCurrentWindow
)
44 iSortMethod
= METHOD_BYFILECOUNT
;
50 iSortMethod
= METHOD_BYTHUMBPERCENT
;
51 iPercent
= 50; // 50% of thumbs -> use thumbs.
57 if (MetadataPercentage(vecItems
) > 0.25f
)
58 return DEFAULT_VIEW_INFO
;
60 return DEFAULT_VIEW_LIST
;
69 case METHOD_BYFOLDERS
:
70 bThumbs
= ByFolders(vecItems
);
74 bThumbs
= ByFiles(bHideParentFolderItems
, vecItems
);
77 case METHOD_BYTHUMBPERCENT
:
78 bThumbs
= ByThumbPercent(bHideParentFolderItems
, iPercent
, vecItems
);
80 case METHOD_BYFILECOUNT
:
81 bThumbs
= ByFileCount(vecItems
);
83 case METHOD_BYFOLDERTHUMBS
:
84 bThumbs
= ByFolderThumbPercentage(bHideParentFolderItems
, iPercent
, vecItems
);
88 // the GUIViewControl object will default down to small icons if a big icon
89 // view is not available.
90 return bThumbs
? DEFAULT_VIEW_BIG_ICONS
: DEFAULT_VIEW_LIST
;
93 /// \brief Auto Switch method based on the current directory \e containing ALL folders and \e atleast one non-default thumb
94 /// \param vecItems Vector of FileItems
95 bool CAutoSwitch::ByFolders(const CFileItemList
& vecItems
)
98 // is the list all folders?
99 if (vecItems
.GetFolderCount() == vecItems
.Size())
102 for (int i
= 0; i
< vecItems
.Size(); i
++)
104 const CFileItemPtr pItem
= vecItems
[i
];
105 if (pItem
->HasArt("thumb"))
115 /// \brief Auto Switch method based on the current directory \e not containing ALL files and \e atleast one non-default thumb
116 /// \param bHideParentDirItems - are we not counting the ".." item?
117 /// \param vecItems Vector of FileItems
118 bool CAutoSwitch::ByFiles(bool bHideParentDirItems
, const CFileItemList
& vecItems
)
120 bool bThumbs
= false;
123 // parent directories are visible, increment
124 if (!bHideParentDirItems
)
129 // confirm the list is not just files and folderback
130 if (vecItems
.GetFolderCount() > iCompare
)
133 for (int i
= 0; i
< vecItems
.Size(); i
++)
135 const CFileItemPtr pItem
= vecItems
[i
];
136 if (pItem
->HasArt("thumb"))
147 /// \brief Auto Switch method based on the percentage of non-default thumbs \e in the current directory
148 /// \param iPercent Percent of non-default thumbs to autoswitch on
149 /// \param vecItems Vector of FileItems
150 bool CAutoSwitch::ByThumbPercent(bool bHideParentDirItems
, int iPercent
, const CFileItemList
& vecItems
)
152 bool bThumbs
= false;
154 int iNumItems
= vecItems
.Size();
155 if (!bHideParentDirItems
)
160 if (iNumItems
<= 0) return false;
162 for (int i
= 0; i
< vecItems
.Size(); i
++)
164 const CFileItemPtr pItem
= vecItems
[i
];
165 if (pItem
->HasArt("thumb"))
168 float fTempPercent
= ( (float)iNumThumbs
/ (float)iNumItems
) * (float)100;
169 if (fTempPercent
>= (float)iPercent
)
180 /// \brief Auto Switch method based on whether there is more than 25% files.
181 /// \param iPercent Percent of non-default thumbs to autoswitch on
182 bool CAutoSwitch::ByFileCount(const CFileItemList
& vecItems
)
184 if (vecItems
.Size() == 0) return false;
185 float fPercent
= (float)vecItems
.GetFileCount() / vecItems
.Size();
186 return (fPercent
> 0.25f
);
190 // 1. Have more than 75% folders and
191 // 2. Have more than percent folders with thumbs
192 bool CAutoSwitch::ByFolderThumbPercentage(bool hideParentDirItems
, int percent
, const CFileItemList
&vecItems
)
194 int numItems
= vecItems
.Size();
195 if (!hideParentDirItems
)
197 if (numItems
<= 0) return false;
199 int fileCount
= vecItems
.GetFileCount();
200 if (fileCount
> 0.25f
* numItems
) return false;
203 for (int i
= 0; i
< vecItems
.Size(); i
++)
205 const CFileItemPtr item
= vecItems
[i
];
206 if (item
->m_bIsFolder
&& item
->HasArt("thumb"))
209 if (numThumbs
>= 0.01f
* percent
* (numItems
- fileCount
))
217 float CAutoSwitch::MetadataPercentage(const CFileItemList
&vecItems
)
220 int total
= vecItems
.Size();
221 for (int i
= 0; i
< vecItems
.Size(); i
++)
223 const CFileItemPtr item
= vecItems
[i
];
224 if(item
->HasMusicInfoTag()
225 || item
->HasVideoInfoTag()
226 || item
->HasPictureInfoTag()
227 || item
->HasProperty("Addon.ID"))
229 if(item
->IsParentFolder())
232 return (total
!= 0) ? ((float)count
/ total
) : 0.0f
;