| 1 | /* |
| 2 | * Copyright (C) 2005-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 | #pragma once |
| 10 | |
| 11 | /*! |
| 12 | \file GUIInfoLabel.h |
| 13 | \brief |
| 14 | */ |
| 15 | |
| 16 | #include "interfaces/info/Info.h" |
| 17 | |
| 18 | #include <functional> |
| 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
| 22 | class CGUIListItem; |
| 23 | |
| 24 | namespace KODI |
| 25 | { |
| 26 | namespace GUILIB |
| 27 | { |
| 28 | namespace GUIINFO |
| 29 | { |
| 30 | |
| 31 | class CGUIInfoLabel |
| 32 | { |
| 33 | public: |
| 34 | CGUIInfoLabel() = default; |
| 35 | CGUIInfoLabel(const std::string& label, |
| 36 | const std::string& fallback = "", |
| 37 | int context = INFO::DEFAULT_CONTEXT); |
| 38 | |
| 39 | void SetLabel(const std::string& label, |
| 40 | const std::string& fallback, |
| 41 | int context = INFO::DEFAULT_CONTEXT); |
| 42 | |
| 43 | /*! |
| 44 | \brief Gets a label (or image) for a given window context from the info manager. |
| 45 | \param contextWindow the context in which to evaluate the expression. |
| 46 | \param preferImage caller is specifically wanting an image rather than a label. Defaults to false. |
| 47 | \param fallback if non-NULL, is set to an alternate value to use should the actual value be not appropriate. Defaults to NULL. |
| 48 | \return label (or image). |
| 49 | */ |
| 50 | const std::string &GetLabel(int contextWindow, bool preferImage = false, std::string *fallback = NULL) const; |
| 51 | |
| 52 | /*! |
| 53 | \brief Gets the label and returns it as an int value |
| 54 | \param contextWindow the context in which to evaluate the expression. |
| 55 | \return int value. |
| 56 | \sa GetLabel |
| 57 | */ |
| 58 | int GetIntValue(int contextWindow) const; |
| 59 | |
| 60 | /*! |
| 61 | \brief Gets a label (or image) for a given listitem from the info manager. |
| 62 | \param item listitem in question. |
| 63 | \param preferImage caller is specifically wanting an image rather than a label. Defaults to false. |
| 64 | \param fallback if non-NULL, is set to an alternate value to use should the actual value be not appropriate. Defaults to nullptr. |
| 65 | \return label (or image). |
| 66 | */ |
| 67 | const std::string& GetItemLabel(const CGUIListItem* item, |
| 68 | bool preferImage = false, |
| 69 | std::string* fallback = nullptr) const; |
| 70 | |
| 71 | bool IsConstant() const; |
| 72 | bool IsEmpty() const; |
| 73 | |
| 74 | const std::string& GetFallback() const { return m_fallback; } |
| 75 | |
| 76 | static std::string GetLabel(const std::string& label, |
| 77 | int contextWindow, |
| 78 | bool preferImage = false); |
| 79 | static std::string GetItemLabel(const std::string &label, const CGUIListItem *item, bool preferImage = false); |
| 80 | |
| 81 | /*! |
| 82 | \brief Replaces instances of $LOCALIZE[number] with the appropriate localized string |
| 83 | \param label text to replace |
| 84 | \return text with any localized strings filled in. |
| 85 | */ |
| 86 | static std::string ReplaceLocalize(const std::string &label); |
| 87 | |
| 88 | /*! |
| 89 | \brief Replaces instances of $ADDON[id number] with the appropriate localized addon string |
| 90 | \param label text to replace |
| 91 | \return text with any localized strings filled in. |
| 92 | */ |
| 93 | static std::string ReplaceAddonStrings(std::string &&label); |
| 94 | |
| 95 | /*! |
| 96 | * \brief Replaces instances of $FEATURE[feature name, controller ID] with |
| 97 | * the appropriate localized controller string |
| 98 | * |
| 99 | * \param label The text to replace |
| 100 | * |
| 101 | * \return text with any controller strings filled in |
| 102 | */ |
| 103 | static std::string ReplaceControllerStrings(std::string&& label); |
| 104 | |
| 105 | typedef std::function<std::string(const std::string&)> StringReplacerFunc; |
| 106 | |
| 107 | /*! |
| 108 | \brief Replaces instances of $strKeyword[value] with the appropriate resolved string |
| 109 | \param strInput text to replace |
| 110 | \param strKeyword keyword to look for |
| 111 | \param func function that does the actual replacement of each bracketed value found |
| 112 | \param strOutput the output string |
| 113 | \return whether anything has been replaced. |
| 114 | */ |
| 115 | static bool ReplaceSpecialKeywordReferences(const std::string &strInput, const std::string &strKeyword, const StringReplacerFunc &func, std::string &strOutput); |
| 116 | |
| 117 | /*! |
| 118 | \brief Replaces instances of $strKeyword[value] with the appropriate resolved string in-place |
| 119 | \param work text to replace in-place |
| 120 | \param strKeyword keyword to look for |
| 121 | \param func function that does the actual replacement of each bracketed value found |
| 122 | \return whether anything has been replaced. |
| 123 | */ |
| 124 | static bool ReplaceSpecialKeywordReferences(std::string &work, const std::string &strKeyword, const StringReplacerFunc &func); |
| 125 | |
| 126 | private: |
| 127 | class CInfoPortion |
| 128 | { |
| 129 | public: |
| 130 | CInfoPortion(int info, const std::string &prefix, const std::string &postfix, bool escaped = false); |
| 131 | bool NeedsUpdate(const std::string &label) const; |
| 132 | std::string Get() const; |
| 133 | int m_info; |
| 134 | private: |
| 135 | bool m_escaped; |
| 136 | mutable std::string m_label; |
| 137 | std::string m_prefix; |
| 138 | std::string m_postfix; |
| 139 | }; |
| 140 | |
| 141 | /*! \brief Parse a provided label string into the list of info portions that may compose the label. Info portions mean the portions of complex |
| 142 | infolabels along with their composition operations (and, or, etc) or skin variables. |
| 143 | The label is also rewritten to resolve localized strings and other dynamic expressions |
| 144 | \param label [in, out] the string label in question (e.g. "$LOCALIZE[xxx]") |
| 145 | \param infoPortion [in, out] the list of info portions that may have been parsed from the string |
| 146 | \param context the context where the info expressions need to be evaluated (currently window ids) |
| 147 | */ |
| 148 | void Parse(const std::string& label, std::vector<CInfoPortion>& infoPortion, int context); |
| 149 | |
| 150 | /*! \brief return (and cache) built label from info portions. |
| 151 | \param rebuild whether we need to rebuild the label |
| 152 | \sa GetLabel, GetItemLabel |
| 153 | */ |
| 154 | const std::string& CacheLabel(bool rebuild) const; |
| 155 | |
| 156 | /*! \brief Rebuild a label value, based on the provided already resolved info portions (a localized string, multiple infolabels, etc) |
| 157 | \param label[in, out] label value where to store the processed result |
| 158 | \param infoPortion the list of info portions |
| 159 | */ |
| 160 | void RebuildLabel(std::string& label, const std::vector<CInfoPortion>& infoPortion) const; |
| 161 | |
| 162 | /*! \brief Checks if a given label needs to be updated, based on the provided info portions (a localized string, multiple infolabels, etc) |
| 163 | \param context the context where the info expressions need to be evaluated (currently window ids) |
| 164 | \param preferImages caller is specifically wanting an image rather than a label. |
| 165 | \param fallback if non-NULL, is set to an alternate value to use should the actual value be not appropriate. This is used by infoproviders to |
| 166 | to re-write the fallback label |
| 167 | \param infoPortion the list of info portions |
| 168 | \return true if an update is needed, false otherwise |
| 169 | */ |
| 170 | bool LabelNeedsUpdate(int context, |
| 171 | bool preferImages, |
| 172 | std::string* fallback, |
| 173 | const std::vector<CInfoPortion>& infoPortion) const; |
| 174 | /*! \brief Checks if a given item label needs to be updated, based on the provided info portions (a localized string, multiple infolabels, etc) |
| 175 | \param item listItem in question. |
| 176 | \param preferImages caller is specifically wanting an image rather than a label. |
| 177 | \param fallback if non-NULL, is set to an alternate value to use should the actual value be not appropriate. This is used by infoproviders to |
| 178 | to re-write the fallback label |
| 179 | \param infoPortion the list of info portions |
| 180 | \return true if an update is needed, false otherwise |
| 181 | */ |
| 182 | bool ItemLabelNeedsUpdate(const CGUIListItem* item, |
| 183 | bool preferImages, |
| 184 | std::string* fallback, |
| 185 | const std::vector<CInfoPortion>& infoPortion) const; |
| 186 | |
| 187 | mutable bool m_dirty = false; |
| 188 | mutable std::string m_label; |
| 189 | mutable std::string m_fallback; |
| 190 | std::vector<CInfoPortion> m_infoLabel; |
| 191 | std::vector<CInfoPortion> m_infoFallback; |
| 192 | }; |
| 193 | |
| 194 | } // namespace GUIINFO |
| 195 | } // namespace GUILIB |
| 196 | } // namespace KODI |
| 197 |