| 1 |
/* |
| 2 |
* Copyright (C) 2015-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 "EventLog.h" |
| 10 |
|
| 11 |
#include "FileItem.h" |
| 12 |
#include "GUIUserMessages.h" |
| 13 |
#include "ServiceBroker.h" |
| 14 |
#include "dialogs/GUIDialogKaiToast.h" |
| 15 |
#include "dialogs/GUIDialogSelect.h" |
| 16 |
#include "filesystem/EventsDirectory.h" |
| 17 |
#include "guilib/GUIComponent.h" |
| 18 |
#include "guilib/GUIWindowManager.h" |
| 19 |
#include "guilib/WindowIDs.h" |
| 20 |
#include "profiles/ProfileManager.h" |
| 21 |
#include "settings/Settings.h" |
| 22 |
#include "settings/SettingsComponent.h" |
| 23 |
|
| 24 |
#include <mutex> |
| 25 |
#include <utility> |
| 26 |
|
| 27 |
std::string CEventLog::EventLevelToString(EventLevel level) |
| 28 |
{ |
| 29 |
switch (level) |
| 30 |
{ |
| 31 |
case EventLevel::Basic: |
| 32 |
return "basic"; |
| 33 |
|
| 34 |
case EventLevel::Warning: |
| 35 |
return "warning"; |
| 36 |
|
| 37 |
case EventLevel::Error: |
| 38 |
return "error"; |
| 39 |
|
| 40 |
case EventLevel::Information: |
| 41 |
default: |
| 42 |
break; |
| 43 |
} |
| 44 |
|
| 45 |
return "information"; |
| 46 |
} |
| 47 |
|
| 48 |
EventLevel CEventLog::EventLevelFromString(const std::string& level) |
| 49 |
{ |
| 50 |
if (level == "basic") |
| 51 |
return EventLevel::Basic; |
| 52 |
if (level == "warning") |
| 53 |
return EventLevel::Warning; |
| 54 |
if (level == "error") |
| 55 |
return EventLevel::Error; |
| 56 |
|
| 57 |
return EventLevel::Information; |
| 58 |
} |
| 59 |
|
| 60 |
Events CEventLog::Get() const |
| 61 |
{ |
| 62 |
return m_events; |
| 63 |
} |
| 64 |
|
| 65 |
Events CEventLog::Get(EventLevel level, bool includeHigherLevels /* = false */) const |
| 66 |
{ |
| 67 |
Events events; |
| 68 |
|
| 69 |
std::unique_lock<CCriticalSection> lock(m_critical); |
| 70 |
for (const auto& eventPtr : m_events) |
| 71 |
{ |
| 72 |
if (eventPtr->GetLevel() == level || |
| 73 |
(includeHigherLevels && eventPtr->GetLevel() > level)) |
| 74 |
events.push_back(eventPtr); |
| 75 |
} |
| 76 |
|
| 77 |
return events; |
| 78 |
} |
| 79 |
|
| 80 |
EventPtr CEventLog::Get(const std::string& eventPtrIdentifier) const |
| 81 |
{ |
| 82 |
if (eventPtrIdentifier.empty()) |
| 83 |
return EventPtr(); |
| 84 |
|
| 85 |
std::unique_lock<CCriticalSection> lock(m_critical); |
| 86 |
const auto& eventPtr = m_eventsMap.find(eventPtrIdentifier); |
| 87 |
if (eventPtr == m_eventsMap.end()) |
| 88 |
return EventPtr(); |
| 89 |
|
| 90 |
return eventPtr->second; |
| 91 |
} |
| 92 |
|
| 93 |
void CEventLog::Add(const EventPtr& eventPtr) |
| 94 |
{ |
| 95 |
if (eventPtr == nullptr || eventPtr->GetIdentifier().empty() || |
| 96 |
!CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(CSettings::SETTING_EVENTLOG_ENABLED) || |
| 97 |
(eventPtr->GetLevel() == EventLevel::Information && !CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(CSettings::SETTING_EVENTLOG_ENABLED_NOTIFICATIONS))) |
| 98 |
return; |
| 99 |
|
| 100 |
std::unique_lock<CCriticalSection> lock(m_critical); |
| 101 |
if (m_eventsMap.find(eventPtr->GetIdentifier()) != m_eventsMap.end()) |
| 102 |
return; |
| 103 |
|
| 104 |
// store the event |
| 105 |
m_events.push_back(eventPtr); |
| 106 |
m_eventsMap.insert(std::make_pair(eventPtr->GetIdentifier(), eventPtr)); |
| 107 |
|
| 108 |
SendMessage(eventPtr, GUI_MSG_EVENT_ADDED); |
| 109 |
} |
| 110 |
|
| 111 |
void CEventLog::Add(const EventPtr& eventPtr, bool withNotification, bool withSound /* = true */) |
| 112 |
{ |
| 113 |
if (!withNotification) |
| 114 |
Add(eventPtr); |
| 115 |
else |
| 116 |
AddWithNotification(eventPtr, withSound); |
| 117 |
} |
| 118 |
|
| 119 |
void CEventLog::AddWithNotification(const EventPtr& eventPtr, |
| 120 |
unsigned int displayTime /* = NOTIFICATION_DISPLAY_TIME */, |
| 121 |
unsigned int messageTime /* = NOTIFICATION_MESSAGE_TIME */, |
| 122 |
bool withSound /* = true */) |
| 123 |
{ |
| 124 |
if (eventPtr == nullptr) |
| 125 |
return; |
| 126 |
|
| 127 |
Add(eventPtr); |
| 128 |
|
| 129 |
// queue the eventPtr as a kai toast notification |
| 130 |
if (!eventPtr->GetIcon().empty()) |
| 131 |
CGUIDialogKaiToast::QueueNotification(eventPtr->GetIcon(), eventPtr->GetLabel(), eventPtr->GetDescription(), displayTime, withSound, messageTime); |
| 132 |
else |
| 133 |
{ |
| 134 |
CGUIDialogKaiToast::eMessageType type = CGUIDialogKaiToast::Info; |
| 135 |
if (eventPtr->GetLevel() == EventLevel::Warning) |
| 136 |
type = CGUIDialogKaiToast::Warning; |
| 137 |
else if (eventPtr->GetLevel() == EventLevel::Error) |
| 138 |
type = CGUIDialogKaiToast::Error; |
| 139 |
|
| 140 |
CGUIDialogKaiToast::QueueNotification(type, eventPtr->GetLabel(), eventPtr->GetDescription(), displayTime, withSound, messageTime); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
void CEventLog::AddWithNotification(const EventPtr& eventPtr, bool withSound) |
| 145 |
{ |
| 146 |
AddWithNotification(eventPtr, NOTIFICATION_DISPLAY_TIME, NOTIFICATION_MESSAGE_TIME, withSound); |
| 147 |
} |
| 148 |
|
| 149 |
void CEventLog::Remove(const EventPtr& eventPtr) |
| 150 |
{ |
| 151 |
if (eventPtr == nullptr) |
| 152 |
return; |
| 153 |
|
| 154 |
Remove(eventPtr->GetIdentifier()); |
| 155 |
} |
| 156 |
|
| 157 |
void CEventLog::Remove(const std::string& eventPtrIdentifier) |
| 158 |
{ |
| 159 |
if (eventPtrIdentifier.empty()) |
| 160 |
return; |
| 161 |
|
| 162 |
std::unique_lock<CCriticalSection> lock(m_critical); |
| 163 |
const auto& itEvent = m_eventsMap.find(eventPtrIdentifier); |
| 164 |
if (itEvent == m_eventsMap.end()) |
| 165 |
return; |
| 166 |
|
| 167 |
EventPtr eventPtr = itEvent->second; |
| 168 |
m_eventsMap.erase(itEvent); |
| 169 |
m_events.erase(std::remove(m_events.begin(), m_events.end(), eventPtr), m_events.end()); |
| 170 |
|
| 171 |
SendMessage(eventPtr, GUI_MSG_EVENT_REMOVED); |
| 172 |
} |
| 173 |
|
| 174 |
void CEventLog::Clear() |
| 175 |
{ |
| 176 |
std::unique_lock<CCriticalSection> lock(m_critical); |
| 177 |
m_events.clear(); |
| 178 |
m_eventsMap.clear(); |
| 179 |
} |
| 180 |
|
| 181 |
void CEventLog::Clear(EventLevel level, bool includeHigherLevels /* = false */) |
| 182 |
{ |
| 183 |
EventsList eventsCopy = m_events; |
| 184 |
for (const auto& eventPtr : eventsCopy) |
| 185 |
{ |
| 186 |
|
| 187 |
if (eventPtr->GetLevel() == level || |
| 188 |
(includeHigherLevels && eventPtr->GetLevel() > level)) |
| 189 |
Remove(eventPtr); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
bool CEventLog::Execute(const std::string& eventPtrIdentifier) |
| 194 |
{ |
| 195 |
if (eventPtrIdentifier.empty()) |
| 196 |
return false; |
| 197 |
|
| 198 |
std::unique_lock<CCriticalSection> lock(m_critical); |
| 199 |
const auto& itEvent = m_eventsMap.find(eventPtrIdentifier); |
| 200 |
if (itEvent == m_eventsMap.end()) |
| 201 |
return false; |
| 202 |
|
| 203 |
return itEvent->second->Execute(); |
| 204 |
} |
| 205 |
|
| 206 |
void CEventLog::ShowFullEventLog(EventLevel level /* = EventLevel::Basic */, bool includeHigherLevels /* = true */) |
| 207 |
{ |
| 208 |
// put together the path |
| 209 |
std::string path = "events://"; |
| 210 |
if (level != EventLevel::Basic || !includeHigherLevels) |
| 211 |
{ |
| 212 |
// add the level to the path |
| 213 |
path += EventLevelToString(level); |
| 214 |
// add whether to include higher levels or not to the path |
| 215 |
if (includeHigherLevels) |
| 216 |
path += "+"; |
| 217 |
} |
| 218 |
|
| 219 |
// activate the full eventPtr log window |
| 220 |
std::vector<std::string> params; |
| 221 |
params.push_back(path); |
| 222 |
params.emplace_back("return"); |
| 223 |
CServiceBroker::GetGUI()->GetWindowManager().ActivateWindow(WINDOW_EVENT_LOG, params); |
| 224 |
} |
| 225 |
|
| 226 |
void CEventLog::SendMessage(const EventPtr& eventPtr, int message) |
| 227 |
{ |
| 228 |
CGUIMessage msg(GUI_MSG_NOTIFY_ALL, 0, 0, message, 0, XFILE::CEventsDirectory::EventToFileItem(eventPtr)); |
| 229 |
CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage(msg); |
| 230 |
} |