| 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 "BaseEvent.h" |
| 10 |
|
| 11 |
#include "guilib/LocalizeStrings.h" |
| 12 |
#include "utils/StringUtils.h" |
| 13 |
|
| 14 |
#include <chrono> |
| 15 |
#include <inttypes.h> |
| 16 |
|
| 17 |
CBaseEvent::CBaseEvent(const std::string& identifier, const CVariant& label, const CVariant& description, EventLevel level /* = EventLevel::Information */) |
| 18 |
: m_level(level), |
| 19 |
m_identifier(identifier), |
| 20 |
m_icon(), |
| 21 |
m_label(label), |
| 22 |
m_description(description), |
| 23 |
m_details(), |
| 24 |
m_executionLabel(), |
| 25 |
m_timestamp(GetInternalTimestamp()), |
| 26 |
m_dateTime(CDateTime::GetCurrentDateTime()) |
| 27 |
{ } |
| 28 |
|
| 29 |
CBaseEvent::CBaseEvent(const std::string& identifier, const CVariant& label, const CVariant& description, const std::string& icon, EventLevel level /* = EventLevel::Information */) |
| 30 |
: m_level(level), |
| 31 |
m_identifier(identifier), |
| 32 |
m_icon(icon), |
| 33 |
m_label(label), |
| 34 |
m_description(description), |
| 35 |
m_details(), |
| 36 |
m_executionLabel(), |
| 37 |
m_timestamp(GetInternalTimestamp()), |
| 38 |
m_dateTime(CDateTime::GetCurrentDateTime()) |
| 39 |
{ } |
| 40 |
|
| 41 |
CBaseEvent::CBaseEvent(const std::string& identifier, const CVariant& label, const CVariant& description, const std::string& icon, const CVariant& details, EventLevel level /* = EventLevel::Information */) |
| 42 |
: m_level(level), |
| 43 |
m_identifier(identifier), |
| 44 |
m_icon(icon), |
| 45 |
m_label(label), |
| 46 |
m_description(description), |
| 47 |
m_details(details), |
| 48 |
m_executionLabel(), |
| 49 |
m_timestamp(GetInternalTimestamp()), |
| 50 |
m_dateTime(CDateTime::GetCurrentDateTime()) |
| 51 |
{ } |
| 52 |
|
| 53 |
CBaseEvent::CBaseEvent(const std::string& identifier, const CVariant& label, const CVariant& description, const std::string& icon, const CVariant& details, const CVariant& executionLabel, EventLevel level /* = EventLevel::Information */) |
| 54 |
: m_level(level), |
| 55 |
m_identifier(identifier), |
| 56 |
m_icon(icon), |
| 57 |
m_label(label), |
| 58 |
m_description(description), |
| 59 |
m_details(details), |
| 60 |
m_executionLabel(executionLabel), |
| 61 |
m_timestamp(GetInternalTimestamp()), |
| 62 |
m_dateTime(CDateTime::GetCurrentDateTime()) |
| 63 |
{ } |
| 64 |
|
| 65 |
uint64_t CBaseEvent::GetInternalTimestamp() |
| 66 |
{ |
| 67 |
return std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now()).time_since_epoch().count(); |
| 68 |
} |
| 69 |
|
| 70 |
std::string CBaseEvent::GetLabel() const |
| 71 |
{ |
| 72 |
return VariantToLocalizedString(m_label); |
| 73 |
} |
| 74 |
|
| 75 |
std::string CBaseEvent::GetDescription() const |
| 76 |
{ |
| 77 |
return VariantToLocalizedString(m_description); |
| 78 |
} |
| 79 |
|
| 80 |
std::string CBaseEvent::GetDetails() const |
| 81 |
{ |
| 82 |
return VariantToLocalizedString(m_details); |
| 83 |
} |
| 84 |
|
| 85 |
std::string CBaseEvent::GetExecutionLabel() const |
| 86 |
{ |
| 87 |
return VariantToLocalizedString(m_executionLabel); |
| 88 |
} |
| 89 |
|
| 90 |
std::string CBaseEvent::VariantToLocalizedString(const CVariant& variant) |
| 91 |
{ |
| 92 |
if (variant.isString()) |
| 93 |
return variant.asString(); |
| 94 |
|
| 95 |
if (variant.isInteger() && variant.asInteger() > 0) |
| 96 |
return g_localizeStrings.Get(static_cast<uint32_t>(variant.asInteger())); |
| 97 |
if (variant.isUnsignedInteger() && variant.asUnsignedInteger() > 0) |
| 98 |
return g_localizeStrings.Get(static_cast<uint32_t>(variant.asUnsignedInteger())); |
| 99 |
|
| 100 |
return ""; |
| 101 |
} |
| 102 |
|
| 103 |
void CBaseEvent::ToSortable(SortItem& sortable, Field field) const |
| 104 |
{ |
| 105 |
if (field == FieldDate) |
| 106 |
sortable[FieldDate] = StringUtils::Format("{:020}", m_timestamp); |
| 107 |
} |