Merge pull request #25614 from garbear/show-version
[xbmc.git] / xbmc / programs / GUIWindowPrograms.cpp
blob03e7b425b83bb001293e3e7cf7a8adfa5f8016ba
1 /*
2 * Copyright (C) 2005-2020 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.
7 */
9 #include "GUIWindowPrograms.h"
11 #include "Autorun.h"
12 #include "FileItem.h"
13 #include "FileItemList.h"
14 #include "GUIPassword.h"
15 #include "ServiceBroker.h"
16 #include "Util.h"
17 #include "addons/gui/GUIDialogAddonInfo.h"
18 #include "dialogs/GUIDialogMediaSource.h"
19 #include "guilib/GUIComponent.h"
20 #include "guilib/GUIWindowManager.h"
21 #include "input/actions/ActionIDs.h"
22 #include "media/MediaLockState.h"
23 #include "settings/MediaSourceSettings.h"
24 #include "utils/StringUtils.h"
26 #define CONTROL_BTNVIEWASICONS 2
27 #define CONTROL_BTNSORTBY 3
28 #define CONTROL_BTNSORTASC 4
29 #define CONTROL_LABELFILES 12
31 CGUIWindowPrograms::CGUIWindowPrograms(void)
32 : CGUIMediaWindow(WINDOW_PROGRAMS, "MyPrograms.xml")
34 m_thumbLoader.SetObserver(this);
35 m_dlgProgress = NULL;
36 m_rootDir.AllowNonLocalSources(false); // no nonlocal shares for this window please
40 CGUIWindowPrograms::~CGUIWindowPrograms(void) = default;
42 bool CGUIWindowPrograms::OnMessage(CGUIMessage& message)
44 switch ( message.GetMessage() )
46 case GUI_MSG_WINDOW_DEINIT:
48 if (m_thumbLoader.IsLoading())
49 m_thumbLoader.StopThread();
51 break;
53 case GUI_MSG_WINDOW_INIT:
55 m_dlgProgress = CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogProgress>(WINDOW_DIALOG_PROGRESS);
57 // is this the first time accessing this window?
58 if (m_vecItems->GetPath() == "?" && message.GetStringParam().empty())
59 message.SetStringParam(CMediaSourceSettings::GetInstance().GetDefaultSource("programs"));
61 return CGUIMediaWindow::OnMessage(message);
63 break;
65 case GUI_MSG_CLICKED:
67 if (m_viewControl.HasControl(message.GetSenderId())) // list/thumb control
69 int iAction = message.GetParam1();
70 int iItem = m_viewControl.GetSelectedItem();
71 if (iAction == ACTION_PLAYER_PLAY)
73 OnPlayMedia(iItem);
74 return true;
76 else if (iAction == ACTION_SHOW_INFO)
78 OnItemInfo(iItem);
79 return true;
83 break;
86 return CGUIMediaWindow::OnMessage(message);
89 void CGUIWindowPrograms::GetContextButtons(int itemNumber, CContextButtons &buttons)
91 if (itemNumber < 0 || itemNumber >= m_vecItems->Size())
92 return;
93 CFileItemPtr item = m_vecItems->Get(itemNumber);
94 if (item)
96 if ( m_vecItems->IsVirtualDirectoryRoot() || m_vecItems->GetPath() == "sources://programs/" )
98 CGUIDialogContextMenu::GetContextButtons("programs", item, buttons);
101 CGUIMediaWindow::GetContextButtons(itemNumber, buttons);
104 bool CGUIWindowPrograms::OnContextButton(int itemNumber, CONTEXT_BUTTON button)
106 CFileItemPtr item = (itemNumber >= 0 && itemNumber < m_vecItems->Size()) ? m_vecItems->Get(itemNumber) : CFileItemPtr();
108 if (CGUIDialogContextMenu::OnContextButton("programs", item, button))
110 Update("");
111 return true;
113 return CGUIMediaWindow::OnContextButton(itemNumber, button);
116 bool CGUIWindowPrograms::OnAddMediaSource()
118 return CGUIDialogMediaSource::ShowAndAddMediaSource("programs");
121 bool CGUIWindowPrograms::Update(const std::string &strDirectory, bool updateFilterPath /* = true */)
123 if (m_thumbLoader.IsLoading())
124 m_thumbLoader.StopThread();
126 if (!CGUIMediaWindow::Update(strDirectory, updateFilterPath))
127 return false;
129 m_thumbLoader.Load(*m_vecItems);
130 return true;
133 bool CGUIWindowPrograms::OnPlayMedia(int iItem, const std::string&)
135 if ( iItem < 0 || iItem >= m_vecItems->Size() ) return false;
136 CFileItemPtr pItem = m_vecItems->Get(iItem);
138 #ifdef HAS_OPTICAL_DRIVE
139 if (pItem->IsDVD())
140 return MEDIA_DETECT::CAutorun::PlayDiscAskResume(m_vecItems->Get(iItem)->GetPath());
141 #endif
143 if (pItem->m_bIsFolder) return false;
145 return false;
148 std::string CGUIWindowPrograms::GetStartFolder(const std::string &dir)
150 std::string lower(dir); StringUtils::ToLower(lower);
151 if (lower == "plugins" || lower == "addons")
152 return "addons://sources/executable/";
153 else if (lower == "androidapps")
154 return "androidapp://sources/apps/";
156 SetupShares();
157 std::vector<CMediaSource> shares;
158 m_rootDir.GetSources(shares);
159 bool bIsSourceName = false;
160 int iIndex = CUtil::GetMatchingSource(dir, shares, bIsSourceName);
161 if (iIndex > -1)
163 if (iIndex < static_cast<int>(shares.size()) && shares[iIndex].m_iHasLock == LOCK_STATE_LOCKED)
165 CFileItem item(shares[iIndex]);
166 if (!g_passwordManager.IsItemUnlocked(&item,"programs"))
167 return "";
169 if (bIsSourceName)
170 return shares[iIndex].strPath;
171 return dir;
173 return CGUIMediaWindow::GetStartFolder(dir);
176 void CGUIWindowPrograms::OnItemInfo(int iItem)
178 if (iItem < 0 || iItem >= m_vecItems->Size())
179 return;
181 CFileItemPtr item = m_vecItems->Get(iItem);
182 if (!m_vecItems->IsPlugin() && (item->IsPlugin() || item->IsScript()))
184 CGUIDialogAddonInfo::ShowForItem(item);