| 1 |
/* |
| 2 |
* Copyright (C) 2017-2024 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 "GUIDialogPeripherals.h" |
| 10 |
|
| 11 |
#include "FileItem.h" |
| 12 |
#include "ServiceBroker.h" |
| 13 |
#include "dialogs/GUIDialogSelect.h" |
| 14 |
#include "guilib/GUIComponent.h" |
| 15 |
#include "guilib/GUIWindowManager.h" |
| 16 |
#include "guilib/WindowIDs.h" |
| 17 |
#include "messaging/ApplicationMessenger.h" |
| 18 |
#include "messaging/helpers/DialogOKHelper.h" |
| 19 |
#include "peripherals/Peripherals.h" |
| 20 |
#include "peripherals/dialogs/GUIDialogPeripheralSettings.h" |
| 21 |
#include "utils/Variant.h" |
| 22 |
|
| 23 |
#include <mutex> |
| 24 |
|
| 25 |
using namespace KODI; |
| 26 |
using namespace PERIPHERALS; |
| 27 |
|
| 28 |
CGUIDialogPeripherals::CGUIDialogPeripherals() |
| 29 |
{ |
| 30 |
// Initialize CGUIControl via CGUIDialogSelect |
| 31 |
SetID(WINDOW_DIALOG_PERIPHERALS); |
| 32 |
} |
| 33 |
|
| 34 |
CGUIDialogPeripherals::~CGUIDialogPeripherals() = default; |
| 35 |
|
| 36 |
void CGUIDialogPeripherals::OnInitWindow() |
| 37 |
{ |
| 38 |
UpdatePeripheralsSync(); |
| 39 |
CGUIDialogSelect::OnInitWindow(); |
| 40 |
} |
| 41 |
|
| 42 |
void CGUIDialogPeripherals::RegisterPeripheralManager(CPeripherals& manager) |
| 43 |
{ |
| 44 |
m_manager = &manager; |
| 45 |
m_manager->RegisterObserver(this); |
| 46 |
} |
| 47 |
|
| 48 |
void CGUIDialogPeripherals::UnregisterPeripheralManager() |
| 49 |
{ |
| 50 |
if (m_manager != nullptr) |
| 51 |
{ |
| 52 |
m_manager->UnregisterObserver(this); |
| 53 |
m_manager = nullptr; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
CFileItemPtr CGUIDialogPeripherals::GetItem(unsigned int pos) const |
| 58 |
{ |
| 59 |
CFileItemPtr item; |
| 60 |
|
| 61 |
std::unique_lock<CCriticalSection> lock(m_peripheralsMutex); |
| 62 |
|
| 63 |
if (static_cast<int>(pos) < m_peripherals.Size()) |
| 64 |
item = m_peripherals[pos]; |
| 65 |
|
| 66 |
return item; |
| 67 |
} |
| 68 |
|
| 69 |
void CGUIDialogPeripherals::Show(CPeripherals& manager) |
| 70 |
{ |
| 71 |
CGUIDialogPeripherals* pDialog = |
| 72 |
CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogPeripherals>( |
| 73 |
WINDOW_DIALOG_PERIPHERALS); |
| 74 |
if (pDialog == nullptr) |
| 75 |
return; |
| 76 |
|
| 77 |
pDialog->Reset(); |
| 78 |
|
| 79 |
int iPos = -1; |
| 80 |
do |
| 81 |
{ |
| 82 |
pDialog->SetHeading(CVariant{35000}); |
| 83 |
pDialog->SetUseDetails(true); |
| 84 |
|
| 85 |
pDialog->RegisterPeripheralManager(manager); |
| 86 |
|
| 87 |
pDialog->Open(); |
| 88 |
|
| 89 |
pDialog->UnregisterPeripheralManager(); |
| 90 |
|
| 91 |
iPos = pDialog->IsConfirmed() ? pDialog->GetSelectedItem() : -1; |
| 92 |
|
| 93 |
if (iPos >= 0) |
| 94 |
{ |
| 95 |
CFileItemPtr pItem = pDialog->GetItem(iPos); |
| 96 |
|
| 97 |
// Show an error if the peripheral doesn't have any settings |
| 98 |
PeripheralPtr peripheral = manager.GetByPath(pItem->GetPath()); |
| 99 |
if (!peripheral || !peripheral->HasConfigurableSettings()) |
| 100 |
{ |
| 101 |
MESSAGING::HELPERS::ShowOKDialogText(CVariant{35000}, CVariant{35004}); |
| 102 |
continue; |
| 103 |
} |
| 104 |
|
| 105 |
CGUIDialogPeripheralSettings* pSettingsDialog = |
| 106 |
CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogPeripheralSettings>( |
| 107 |
WINDOW_DIALOG_PERIPHERAL_SETTINGS); |
| 108 |
if (pItem && pSettingsDialog) |
| 109 |
{ |
| 110 |
// Pass peripheral item properties to settings dialog so skin authors |
| 111 |
// Can use it to show more detailed information about the device |
| 112 |
pSettingsDialog->SetProperty("vendor", pItem->GetProperty("vendor")); |
| 113 |
pSettingsDialog->SetProperty("product", pItem->GetProperty("product")); |
| 114 |
pSettingsDialog->SetProperty("bus", pItem->GetProperty("bus")); |
| 115 |
pSettingsDialog->SetProperty("location", pItem->GetProperty("location")); |
| 116 |
pSettingsDialog->SetProperty("class", pItem->GetProperty("class")); |
| 117 |
pSettingsDialog->SetProperty("version", pItem->GetProperty("version")); |
| 118 |
|
| 119 |
// Open settings dialog |
| 120 |
pSettingsDialog->SetFileItem(pItem.get()); |
| 121 |
pSettingsDialog->RegisterPeripheralManager(manager); |
| 122 |
pSettingsDialog->Open(); |
| 123 |
pSettingsDialog->UnregisterPeripheralManager(); |
| 124 |
} |
| 125 |
} |
| 126 |
} while (pDialog->IsConfirmed()); |
| 127 |
} |
| 128 |
|
| 129 |
bool CGUIDialogPeripherals::OnMessage(CGUIMessage& message) |
| 130 |
{ |
| 131 |
switch (message.GetMessage()) |
| 132 |
{ |
| 133 |
case GUI_MSG_REFRESH_LIST: |
| 134 |
{ |
| 135 |
if (m_manager && message.GetControlId() == -1) |
| 136 |
{ |
| 137 |
UpdatePeripheralsSync(); |
| 138 |
return true; |
| 139 |
} |
| 140 |
} |
| 141 |
default: |
| 142 |
break; |
| 143 |
} |
| 144 |
|
| 145 |
return CGUIDialogSelect::OnMessage(message); |
| 146 |
} |
| 147 |
|
| 148 |
void CGUIDialogPeripherals::FreeResources(bool immediately /* = false */) |
| 149 |
{ |
| 150 |
// Free GUI resources |
| 151 |
for (auto it = m_peripherals.begin(); it != m_peripherals.end(); ++it) |
| 152 |
(*it)->FreeMemory(); |
| 153 |
|
| 154 |
// Free ancestor resources |
| 155 |
CGUIDialogSelect::FreeResources(immediately); |
| 156 |
} |
| 157 |
|
| 158 |
void CGUIDialogPeripherals::Notify(const Observable& obs, const ObservableMessage msg) |
| 159 |
{ |
| 160 |
switch (msg) |
| 161 |
{ |
| 162 |
case ObservableMessagePeripheralsChanged: |
| 163 |
UpdatePeripheralsAsync(); |
| 164 |
break; |
| 165 |
default: |
| 166 |
break; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
void CGUIDialogPeripherals::UpdatePeripheralsAsync() |
| 171 |
{ |
| 172 |
CGUIMessage msg(GUI_MSG_REFRESH_LIST, GetID(), -1); |
| 173 |
CServiceBroker::GetAppMessenger()->SendGUIMessage(msg); |
| 174 |
} |
| 175 |
|
| 176 |
void CGUIDialogPeripherals::UpdatePeripheralsSync() |
| 177 |
{ |
| 178 |
int iPos = GetSelectedItem(); |
| 179 |
|
| 180 |
std::unique_lock<CCriticalSection> lock(m_peripheralsMutex); |
| 181 |
|
| 182 |
CFileItemPtr selectedItem; |
| 183 |
if (iPos > 0) |
| 184 |
selectedItem = GetItem(iPos); |
| 185 |
|
| 186 |
m_peripherals.Clear(); |
| 187 |
m_manager->GetDirectory("peripherals://all/", m_peripherals); |
| 188 |
SetItems(m_peripherals); |
| 189 |
} |