summaryrefslogtreecommitdiffstats
path: root/xbmc/favourites/FavouritesURL.cpp
blob: a2772569e8627c3d036df9bf79858cb16e392599 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 *  Copyright (C) 2022 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "FavouritesURL.h"

#include "FileItem.h"
#include "ServiceBroker.h"
#include "URL.h"
#include "addons/AddonManager.h"
#include "addons/IAddon.h"
#include "guilib/LocalizeStrings.h"
#include "input/WindowTranslator.h"
#include "utils/ExecString.h"
#include "utils/StringUtils.h"
#include "utils/SystemInfo.h"
#include "utils/URIUtils.h"
#include "utils/log.h"

#include <vector>

namespace
{
std::string GetActionString(CFavouritesURL::Action action)
{
  switch (action)
  {
    case CFavouritesURL::Action::ACTIVATE_WINDOW:
      return "activatewindow";
    case CFavouritesURL::Action::PLAY_MEDIA:
      return "playmedia";
    case CFavouritesURL::Action::SHOW_PICTURE:
      return "showpicture";
    case CFavouritesURL::Action::RUN_SCRIPT:
      return "runscript";
    case CFavouritesURL::Action::RUN_ADDON:
      return "runaddon";
    case CFavouritesURL::Action::START_ANDROID_ACTIVITY:
      return "startandroidactivity";
    default:
      return {};
  }
}

CFavouritesURL::Action GetActionId(const std::string& actionString)
{
  if (actionString == "activatewindow")
    return CFavouritesURL::Action::ACTIVATE_WINDOW;
  else if (actionString == "playmedia")
    return CFavouritesURL::Action::PLAY_MEDIA;
  else if (actionString == "showpicture")
    return CFavouritesURL::Action::SHOW_PICTURE;
  else if (actionString == "runscript")
    return CFavouritesURL::Action::RUN_SCRIPT;
  else if (actionString == "runaddon")
    return CFavouritesURL::Action::RUN_ADDON;
  else if (actionString == "startandroidactivity")
    return CFavouritesURL::Action::START_ANDROID_ACTIVITY;
  else
    return CFavouritesURL::Action::UNKNOWN;
}
} // namespace

CFavouritesURL::CFavouritesURL(const std::string& favouritesURL)
{
  const CURL url(favouritesURL);
  if (url.GetProtocol() != "favourites")
  {
    CLog::LogF(LOGERROR, "Invalid protocol");
    return;
  }

  m_exec = CExecString(CURL::Decode(url.GetHostName()));
  if (m_exec.IsValid())
    m_valid = Parse(GetActionId(m_exec.GetFunction()), m_exec.GetParams());
}

CFavouritesURL::CFavouritesURL(const CExecString& execString) : m_exec(execString)
{
  if (m_exec.IsValid())
    m_valid = Parse(GetActionId(m_exec.GetFunction()), m_exec.GetParams());
}

CFavouritesURL::CFavouritesURL(Action action, const std::vector<std::string>& params)
  : m_exec(GetActionString(action), params)
{
  if (m_exec.IsValid())
    m_valid = Parse(action, params);
}

CFavouritesURL::CFavouritesURL(const CFileItem& item, int contextWindow)
  : m_exec(item, std::to_string(contextWindow))
{
  if (m_exec.IsValid())
    m_valid = Parse(GetActionId(m_exec.GetFunction()), m_exec.GetParams());
}

bool CFavouritesURL::Parse(CFavouritesURL::Action action, const std::vector<std::string>& params)
{
  m_action = action;

  bool pathIsAddonID = false;

  switch (action)
  {
    case Action::ACTIVATE_WINDOW:
      if (params.empty())
      {
        CLog::LogF(LOGERROR, "Missing parameter");
        return false;
      }

      // mandatory: window name/id
      m_windowId = CWindowTranslator::TranslateWindow(params[0]);

      if (params.size() > 1)
      {
        // optional: target url/path
        m_target = StringUtils::DeParamify(params[1]);
      }
      m_actionLabel =
          StringUtils::Format(g_localizeStrings.Get(15220), // Show content in '<windowname>'
                              g_localizeStrings.Get(m_windowId));
      break;
    case Action::PLAY_MEDIA:
      if (params.empty())
      {
        CLog::LogF(LOGERROR, "Missing parameter");
        return false;
      }
      m_target = StringUtils::DeParamify(params[0]);
      m_actionLabel = g_localizeStrings.Get(15218); // Play media
      break;
    case Action::SHOW_PICTURE:
      if (params.empty())
      {
        CLog::LogF(LOGERROR, "Missing parameter");
        return false;
      }
      m_target = StringUtils::DeParamify(params[0]);
      m_actionLabel = g_localizeStrings.Get(15219); // Show picture
      break;
    case Action::RUN_SCRIPT:
      if (params.empty())
      {
        CLog::LogF(LOGERROR, "Missing parameter");
        return false;
      }
      m_target = StringUtils::DeParamify(params[0]);
      m_actionLabel = g_localizeStrings.Get(15221); // Execute script
      pathIsAddonID = true;
      break;
    case Action::RUN_ADDON:
      if (params.empty())
      {
        CLog::LogF(LOGERROR, "Missing parameter");
        return false;
      }
      m_target = StringUtils::DeParamify(params[0]);
      m_actionLabel = g_localizeStrings.Get(15223); // Execute add-on
      pathIsAddonID = true;
      break;
    case Action::START_ANDROID_ACTIVITY:
      if (params.empty())
      {
        CLog::LogF(LOGERROR, "Missing parameter");
        return false;
      }
      m_target = StringUtils::DeParamify(params[0]);
      m_actionLabel = g_localizeStrings.Get(15222); // Execute Android app
      break;
    default:
      if (params.empty())
      {
        CLog::LogF(LOGERROR, "Missing parameter");
        return false;
      }
      m_action = CFavouritesURL::Action::UNKNOWN;
      m_target = StringUtils::DeParamify(params[0]);
      m_actionLabel = g_localizeStrings.Get(15224); // Other / Unknown
      break;
  }

  m_path = StringUtils::Format("favourites://{}", CURL::Encode(GetExecString()));
  m_isDir = URIUtils::HasSlashAtEnd(m_target, true);

  if (pathIsAddonID || URIUtils::IsPlugin(m_target))
  {
    // get the add-on name
    const std::string plugin = pathIsAddonID ? m_target : CURL(m_target).GetHostName();

    ADDON::AddonPtr addon;
    CServiceBroker::GetAddonMgr().GetAddon(plugin, addon, ADDON::OnlyEnabled::CHOICE_NO);
    if (addon)
      m_providerLabel = addon->Name();
  }
  if (m_providerLabel.empty())
    m_providerLabel = CSysInfo::GetAppName();

  return true;
}