aboutsummaryrefslogtreecommitdiff
path: root/xbmc/video/guilib/VideoVersionHelper.cpp
blob: faf4c1e6de946ec8361667589552a57dc6cacd5e (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 *  Copyright (C) 2023 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 "VideoVersionHelper.h"

#include "FileItem.h"
#include "FileItemList.h"
#include "ServiceBroker.h"
#include "URL.h"
#include "dialogs/GUIDialogSelect.h"
#include "guilib/GUIComponent.h"
#include "guilib/GUIWindowManager.h"
#include "guilib/LocalizeStrings.h"
#include "settings/Settings.h"
#include "settings/SettingsComponent.h"
#include "settings/lib/Setting.h"
#include "utils/StringUtils.h"
#include "utils/log.h"
#include "video/VideoDatabase.h"
#include "video/VideoFileItemClassify.h"
#include "video/VideoManagerTypes.h"
#include "video/VideoThumbLoader.h"

using namespace KODI::VIDEO;
using namespace VIDEO::GUILIB;

namespace
{
class CVideoChooser
{
public:
  explicit CVideoChooser(const std::shared_ptr<const CFileItem>& item) : m_item(item) {}
  virtual ~CVideoChooser() = default;

  void EnableTypeSwitch(bool enable) { m_enableTypeSwitch = enable; }
  void SetInitialAssetType(VideoAssetType type) { m_initialAssetType = type; }

  std::shared_ptr<const CFileItem> ChooseVideo();

private:
  CVideoChooser() = delete;
  std::shared_ptr<const CFileItem> ChooseVideoVersion();
  std::shared_ptr<const CFileItem> ChooseVideoExtra();
  std::shared_ptr<const CFileItem> ChooseVideo(CGUIDialogSelect& dialog,
                                               int headingId,
                                               int buttonId,
                                               CFileItemList& itemsToDisplay,
                                               const CFileItemList& itemsToSwitchTo);

  const std::shared_ptr<const CFileItem> m_item;
  bool m_enableTypeSwitch{false};
  VideoAssetType m_initialAssetType{VideoAssetType::UNKNOWN};
  bool m_switchType{false};
  CFileItemList m_videoVersions;
  CFileItemList m_videoExtras;
};

std::shared_ptr<const CFileItem> CVideoChooser::ChooseVideo()
{
  m_switchType = false;
  m_videoVersions.Clear();
  m_videoExtras.Clear();

  std::shared_ptr<const CFileItem> result;
  if (m_enableTypeSwitch && !m_item->HasVideoVersions() && !m_item->HasVideoExtras())
    return result;

  if (!m_enableTypeSwitch && m_initialAssetType == VideoAssetType::VERSION &&
      !m_item->HasVideoVersions())
    return result;

  if (!m_enableTypeSwitch && m_initialAssetType == VideoAssetType::EXTRA &&
      !m_item->HasVideoExtras())
    return result;

  CVideoDatabase db;
  if (!db.Open())
  {
    CLog::LogF(LOGERROR, "Unable to open video database!");
    return result;
  }

  if (m_initialAssetType == VideoAssetType::VERSION || m_enableTypeSwitch)
  {
    db.GetAssetsForVideo(m_item->GetVideoContentType(), m_item->GetVideoInfoTag()->m_iDbId,
                         VideoAssetType::VERSION, m_videoVersions);

    // find default version item in list and select it
    for (const auto& item : m_videoVersions)
    {
      item->Select(item->GetVideoInfoTag()->IsDefaultVideoVersion());
    }
  }

  if (m_initialAssetType == VideoAssetType::EXTRA || m_enableTypeSwitch)
    db.GetAssetsForVideo(m_item->GetVideoContentType(), m_item->GetVideoInfoTag()->m_iDbId,
                         VideoAssetType::EXTRA, m_videoExtras);

  VideoAssetType itemType{m_initialAssetType};
  while (true)
  {
    if (itemType == VideoAssetType::VERSION)
    {
      result = ChooseVideoVersion();
      itemType = VideoAssetType::EXTRA;
    }
    else
    {
      result = ChooseVideoExtra();
      itemType = VideoAssetType::VERSION;
    }

    if (!m_switchType)
      break;

    // switch type button pressed. Re-open, this time with the "other" type to select.
  }
  return result;
}

std::shared_ptr<const CFileItem> CVideoChooser::ChooseVideoVersion()
{
  CGUIDialogSelect* dialog{CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogSelect>(
      WINDOW_DIALOG_SELECT_VIDEO_VERSION)};
  if (!dialog)
  {
    CLog::LogF(LOGERROR, "Unable to get WINDOW_DIALOG_SELECT_VIDEO_VERSION dialog instance!");
    return {};
  }

  return ChooseVideo(*dialog, 40208 /* Choose version */, 40211 /* Extras */, m_videoVersions,
                     m_videoExtras);
}

std::shared_ptr<const CFileItem> CVideoChooser::ChooseVideoExtra()
{
  CGUIDialogSelect* dialog{CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogSelect>(
      WINDOW_DIALOG_SELECT_VIDEO_EXTRA)};
  if (!dialog)
  {
    CLog::LogF(LOGERROR, "Unable to get WINDOW_DIALOG_SELECT_VIDEO_EXTRA dialog instance!");
    return {};
  }

  return ChooseVideo(*dialog, 40214 /* Choose extra */, 40210 /* Versions */, m_videoExtras,
                     m_videoVersions);
}

std::shared_ptr<const CFileItem> CVideoChooser::ChooseVideo(CGUIDialogSelect& dialog,
                                                            int headingId,
                                                            int buttonId,
                                                            CFileItemList& itemsToDisplay,
                                                            const CFileItemList& itemsToSwitchTo)
{
  CVideoThumbLoader thumbLoader;
  thumbLoader.Load(itemsToDisplay);
  for (auto& item : itemsToDisplay)
    item->SetLabel2(item->GetVideoInfoTag()->m_strFileNameAndPath);

  dialog.Reset();

  const std::string heading{
      StringUtils::Format(g_localizeStrings.Get(headingId), m_item->GetVideoInfoTag()->GetTitle())};
  dialog.SetHeading(heading);

  dialog.EnableButton(m_enableTypeSwitch && !itemsToSwitchTo.IsEmpty(), buttonId);
  dialog.SetUseDetails(true);
  dialog.SetMultiSelection(false);
  dialog.SetItems(itemsToDisplay);

  dialog.Open();

  if (thumbLoader.IsLoading())
    thumbLoader.StopThread();

  m_switchType = dialog.IsButtonPressed();
  if (dialog.IsConfirmed())
    return dialog.GetSelectedFileItem();

  return {};
}
} // unnamed namespace

std::shared_ptr<CFileItem> CVideoVersionHelper::ChooseVideoFromAssets(
    const std::shared_ptr<CFileItem>& item)
{
  std::shared_ptr<const CFileItem> video;

  VideoAssetType assetType{static_cast<int>(
      item->GetProperty("video_asset_type").asInteger(static_cast<int>(VideoAssetType::UNKNOWN)))};
  bool allAssetTypes{false};
  bool hasMultipleChoices{false};

  switch (assetType)
  {
    case VideoAssetType::UNKNOWN:
      // asset type not provided means all types are allowed and the user can switch between types
      allAssetTypes = true;
      if (item->HasVideoVersions() || item->HasVideoExtras())
        hasMultipleChoices = true;
      break;

    case VideoAssetType::VERSION:
      if (item->HasVideoVersions())
        hasMultipleChoices = true;
      break;

    case VideoAssetType::EXTRA:
      if (item->HasVideoExtras())
        hasMultipleChoices = true;
      break;

    default:
      CLog::LogF(LOGERROR, "unknown asset type ({})", static_cast<int>(assetType));
      return {};
  }

  if (hasMultipleChoices)
  {
    if (!item->GetProperty("needs_resolved_video_asset").asBoolean(false))
    {
      // auto select the default video version
      const auto settings{CServiceBroker::GetSettingsComponent()->GetSettings()};
      if (settings->GetBool(CSettings::SETTING_MYVIDEOS_SELECTDEFAULTVERSION))
      {
        if (item->GetVideoInfoTag()->IsDefaultVideoVersion())
        {
          video = std::make_shared<const CFileItem>(*item);
        }
        else
        {
          CVideoDatabase db;
          if (!db.Open())
          {
            CLog::LogF(LOGERROR, "Unable to open video database!");
          }
          else
          {
            CFileItem defaultVersion;
            if (!db.GetDefaultVersionForVideo(item->GetVideoContentType(),
                                              item->GetVideoInfoTag()->m_iDbId, defaultVersion))
              CLog::LogF(LOGERROR, "Unable to get default version from video database!");
            else
              video = std::make_shared<const CFileItem>(defaultVersion);
          }
        }
      }
    }

    if (!video && (item->GetProperty("needs_resolved_video_asset").asBoolean(false) ||
                   !item->GetProperty("has_resolved_video_asset").asBoolean(false)))
    {
      CVideoChooser chooser{item};

      if (allAssetTypes)
      {
        chooser.EnableTypeSwitch(true);
        chooser.SetInitialAssetType(VideoAssetType::VERSION);
      }
      else
      {
        chooser.EnableTypeSwitch(false);
        chooser.SetInitialAssetType(assetType);
      }

      const auto result{chooser.ChooseVideo()};
      if (result)
        video = result;
      else
        return {};
    }
  }

  if (video)
    return std::make_shared<CFileItem>(*video);

  return item;
}

bool VIDEO::IsVideoAssetFile(const CFileItem& item)
{
  if (item.m_bIsFolder || !IsVideoDb(item))
    return false;

  // @todo maybe in the future look for prefix videodb://movies/videoversions in path instead
  // @todo better encoding of video assets as path, they won't always be tied with movies.
  const CURL itemUrl{item.GetPath()};
  if (itemUrl.HasOption("videoversionid"))
    return true;

  return false;
}