| 1 |
/* |
| 2 |
* Copyright (C) 2022 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 "FavouritesURL.h" |
| 10 |
|
| 11 |
#include "FileItem.h" |
| 12 |
#include "ServiceBroker.h" |
| 13 |
#include "URL.h" |
| 14 |
#include "addons/AddonManager.h" |
| 15 |
#include "addons/IAddon.h" |
| 16 |
#include "guilib/LocalizeStrings.h" |
| 17 |
#include "input/WindowTranslator.h" |
| 18 |
#include "utils/ExecString.h" |
| 19 |
#include "utils/StringUtils.h" |
| 20 |
#include "utils/SystemInfo.h" |
| 21 |
#include "utils/URIUtils.h" |
| 22 |
#include "utils/log.h" |
| 23 |
|
| 24 |
#include <vector> |
| 25 |
|
| 26 |
namespace |
| 27 |
{ |
| 28 |
std::string GetActionString(CFavouritesURL::Action action) |
| 29 |
{ |
| 30 |
switch (action) |
| 31 |
{ |
| 32 |
case CFavouritesURL::Action::ACTIVATE_WINDOW: |
| 33 |
return "activatewindow"; |
| 34 |
case CFavouritesURL::Action::PLAY_MEDIA: |
| 35 |
return "playmedia"; |
| 36 |
case CFavouritesURL::Action::SHOW_PICTURE: |
| 37 |
return "showpicture"; |
| 38 |
case CFavouritesURL::Action::RUN_SCRIPT: |
| 39 |
return "runscript"; |
| 40 |
case CFavouritesURL::Action::RUN_ADDON: |
| 41 |
return "runaddon"; |
| 42 |
case CFavouritesURL::Action::START_ANDROID_ACTIVITY: |
| 43 |
return "startandroidactivity"; |
| 44 |
default: |
| 45 |
return {}; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
CFavouritesURL::Action GetActionId(const std::string& actionString) |
| 50 |
{ |
| 51 |
if (actionString == "activatewindow") |
| 52 |
return CFavouritesURL::Action::ACTIVATE_WINDOW; |
| 53 |
else if (actionString == "playmedia") |
| 54 |
return CFavouritesURL::Action::PLAY_MEDIA; |
| 55 |
else if (actionString == "showpicture") |
| 56 |
return CFavouritesURL::Action::SHOW_PICTURE; |
| 57 |
else if (actionString == "runscript") |
| 58 |
return CFavouritesURL::Action::RUN_SCRIPT; |
| 59 |
else if (actionString == "runaddon") |
| 60 |
return CFavouritesURL::Action::RUN_ADDON; |
| 61 |
else if (actionString == "startandroidactivity") |
| 62 |
return CFavouritesURL::Action::START_ANDROID_ACTIVITY; |
| 63 |
else |
| 64 |
return CFavouritesURL::Action::UNKNOWN; |
| 65 |
} |
| 66 |
} // namespace |
| 67 |
|
| 68 |
CFavouritesURL::CFavouritesURL(const std::string& favouritesURL) |
| 69 |
{ |
| 70 |
const CURL url(favouritesURL); |
| 71 |
if (url.GetProtocol() != "favourites") |
| 72 |
{ |
| 73 |
CLog::LogF(LOGERROR, "Invalid protocol"); |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
m_exec = CExecString(CURL::Decode(url.GetHostName())); |
| 78 |
if (m_exec.IsValid()) |
| 79 |
m_valid = Parse(GetActionId(m_exec.GetFunction()), m_exec.GetParams()); |
| 80 |
} |
| 81 |
|
| 82 |
CFavouritesURL::CFavouritesURL(const CExecString& execString) : m_exec(execString) |
| 83 |
{ |
| 84 |
if (m_exec.IsValid()) |
| 85 |
m_valid = Parse(GetActionId(m_exec.GetFunction()), m_exec.GetParams()); |
| 86 |
} |
| 87 |
|
| 88 |
CFavouritesURL::CFavouritesURL(Action action, const std::vector<std::string>& params) |
| 89 |
: m_exec(GetActionString(action), params) |
| 90 |
{ |
| 91 |
if (m_exec.IsValid()) |
| 92 |
m_valid = Parse(action, params); |
| 93 |
} |
| 94 |
|
| 95 |
CFavouritesURL::CFavouritesURL(const CFileItem& item, int contextWindow) |
| 96 |
: m_exec(item, std::to_string(contextWindow)) |
| 97 |
{ |
| 98 |
if (m_exec.IsValid()) |
| 99 |
m_valid = Parse(GetActionId(m_exec.GetFunction()), m_exec.GetParams()); |
| 100 |
} |
| 101 |
|
| 102 |
bool CFavouritesURL::Parse(CFavouritesURL::Action action, const std::vector<std::string>& params) |
| 103 |
{ |
| 104 |
m_action = action; |
| 105 |
|
| 106 |
bool pathIsAddonID = false; |
| 107 |
|
| 108 |
switch (action) |
| 109 |
{ |
| 110 |
case Action::ACTIVATE_WINDOW: |
| 111 |
if (params.empty()) |
| 112 |
{ |
| 113 |
CLog::LogF(LOGERROR, "Missing parameter"); |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
// mandatory: window name/id |
| 118 |
m_windowId = CWindowTranslator::TranslateWindow(params[0]); |
| 119 |
|
| 120 |
if (params.size() > 1) |
| 121 |
{ |
| 122 |
// optional: target url/path |
| 123 |
m_target = StringUtils::DeParamify(params[1]); |
| 124 |
} |
| 125 |
m_actionLabel = |
| 126 |
StringUtils::Format(g_localizeStrings.Get(15220), // Show content in '<windowname>' |
| 127 |
g_localizeStrings.Get(m_windowId)); |
| 128 |
break; |
| 129 |
case Action::PLAY_MEDIA: |
| 130 |
if (params.empty()) |
| 131 |
{ |
| 132 |
CLog::LogF(LOGERROR, "Missing parameter"); |
| 133 |
return false; |
| 134 |
} |
| 135 |
m_target = StringUtils::DeParamify(params[0]); |
| 136 |
m_actionLabel = g_localizeStrings.Get(15218); // Play media |
| 137 |
break; |
| 138 |
case Action::SHOW_PICTURE: |
| 139 |
if (params.empty()) |
| 140 |
{ |
| 141 |
CLog::LogF(LOGERROR, "Missing parameter"); |
| 142 |
return false; |
| 143 |
} |
| 144 |
m_target = StringUtils::DeParamify(params[0]); |
| 145 |
m_actionLabel = g_localizeStrings.Get(15219); // Show picture |
| 146 |
break; |
| 147 |
case Action::RUN_SCRIPT: |
| 148 |
if (params.empty()) |
| 149 |
{ |
| 150 |
CLog::LogF(LOGERROR, "Missing parameter"); |
| 151 |
return false; |
| 152 |
} |
| 153 |
m_target = StringUtils::DeParamify(params[0]); |
| 154 |
m_actionLabel = g_localizeStrings.Get(15221); // Execute script |
| 155 |
pathIsAddonID = true; |
| 156 |
break; |
| 157 |
case Action::RUN_ADDON: |
| 158 |
if (params.empty()) |
| 159 |
{ |
| 160 |
CLog::LogF(LOGERROR, "Missing parameter"); |
| 161 |
return false; |
| 162 |
} |
| 163 |
m_target = StringUtils::DeParamify(params[0]); |
| 164 |
m_actionLabel = g_localizeStrings.Get(15223); // Execute add-on |
| 165 |
pathIsAddonID = true; |
| 166 |
break; |
| 167 |
case Action::START_ANDROID_ACTIVITY: |
| 168 |
if (params.empty()) |
| 169 |
{ |
| 170 |
CLog::LogF(LOGERROR, "Missing parameter"); |
| 171 |
return false; |
| 172 |
} |
| 173 |
m_target = StringUtils::DeParamify(params[0]); |
| 174 |
m_actionLabel = g_localizeStrings.Get(15222); // Execute Android app |
| 175 |
break; |
| 176 |
default: |
| 177 |
if (params.empty()) |
| 178 |
{ |
| 179 |
CLog::LogF(LOGERROR, "Missing parameter"); |
| 180 |
return false; |
| 181 |
} |
| 182 |
m_action = CFavouritesURL::Action::UNKNOWN; |
| 183 |
m_target = StringUtils::DeParamify(params[0]); |
| 184 |
m_actionLabel = g_localizeStrings.Get(15224); // Other / Unknown |
| 185 |
break; |
| 186 |
} |
| 187 |
|
| 188 |
m_path = StringUtils::Format("favourites://{}", CURL::Encode(GetExecString())); |
| 189 |
m_isDir = URIUtils::HasSlashAtEnd(m_target, true); |
| 190 |
|
| 191 |
if (pathIsAddonID || URIUtils::IsPlugin(m_target)) |
| 192 |
{ |
| 193 |
// get the add-on name |
| 194 |
const std::string plugin = pathIsAddonID ? m_target : CURL(m_target).GetHostName(); |
| 195 |
|
| 196 |
ADDON::AddonPtr addon; |
| 197 |
CServiceBroker::GetAddonMgr().GetAddon(plugin, addon, ADDON::OnlyEnabled::CHOICE_NO); |
| 198 |
if (addon) |
| 199 |
m_providerLabel = addon->Name(); |
| 200 |
} |
| 201 |
if (m_providerLabel.empty()) |
| 202 |
m_providerLabel = CSysInfo::GetAppName(); |
| 203 |
|
| 204 |
return true; |
| 205 |
} |