summaryrefslogtreecommitdiffstats
path: root/xbmc/guilib/guiinfo/GUIInfoHelper.cpp
blob: f025b5e0dfa2ab2d69d7ddb9c4961bd0d5bf991f (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) 2005-2018 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 "GUIInfoHelper.h"

#include "FileItem.h"
#include "PlayListPlayer.h"
#include "ServiceBroker.h"
#include "guilib/GUIComponent.h"
#include "guilib/GUIWindow.h"
#include "guilib/GUIWindowManager.h"
#include "guilib/IGUIContainer.h"
#include "guilib/LocalizeStrings.h"
#include "guilib/guiinfo/GUIInfoLabels.h"
#include "playlists/PlayList.h"
#include "utils/StringUtils.h"
#include "utils/URIUtils.h"
#include "windows/GUIMediaWindow.h"

namespace KODI
{
namespace GUILIB
{
namespace GUIINFO
{

// conditions for window retrieval
static const int WINDOW_CONDITION_HAS_LIST_ITEMS  = 1;
static const int WINDOW_CONDITION_IS_MEDIA_WINDOW = 2;

std::string GetPlaylistLabel(int item, PLAYLIST::Id playlistId /* = TYPE_NONE */)
{
  PLAYLIST::CPlayListPlayer& player = CServiceBroker::GetPlaylistPlayer();

  if (playlistId == PLAYLIST::TYPE_NONE)
    playlistId = player.GetCurrentPlaylist();

  switch (item)
  {
    case PLAYLIST_LENGTH:
    {
      return std::to_string(player.GetPlaylist(playlistId).size());
    }
    case PLAYLIST_POSITION:
    {
      int currentSong = player.GetCurrentSong();
      if (currentSong > -1)
        return std::to_string(currentSong + 1);
      break;
    }
    case PLAYLIST_RANDOM:
    {
      if (player.IsShuffled(playlistId))
        return g_localizeStrings.Get(16041); // 16041: On
      else
        return g_localizeStrings.Get(591); // 591: Off
    }
    case PLAYLIST_REPEAT:
    {
      PLAYLIST::RepeatState state = player.GetRepeat(playlistId);
      if (state == PLAYLIST::RepeatState::ONE)
        return g_localizeStrings.Get(592); // 592: One
      else if (state == PLAYLIST::RepeatState::ALL)
        return g_localizeStrings.Get(593); // 593: All
      else
        return g_localizeStrings.Get(594); // 594: Off
    }
  }
  return std::string();
}

namespace
{

bool CheckWindowCondition(CGUIWindow *window, int condition)
{
  // check if it satisfies our condition
  if (!window)
    return false;
  if ((condition & WINDOW_CONDITION_HAS_LIST_ITEMS) && !window->HasListItems())
    return false;
  if ((condition & WINDOW_CONDITION_IS_MEDIA_WINDOW) && !window->IsMediaWindow())
    return false;
  return true;
}

CGUIWindow* GetWindowWithCondition(int contextWindow, int condition)
{
  const CGUIWindowManager& windowMgr = CServiceBroker::GetGUI()->GetWindowManager();

  CGUIWindow *window = windowMgr.GetWindow(contextWindow);
  if (CheckWindowCondition(window, condition))
    return window;

  // try topmost dialog
  window = windowMgr.GetWindow(windowMgr.GetTopmostModalDialog());
  if (CheckWindowCondition(window, condition))
    return window;

  // try active window
  window = windowMgr.GetWindow(windowMgr.GetActiveWindow());
  if (CheckWindowCondition(window, condition))
    return window;

  return nullptr;
}

} // unnamed namespace

CGUIWindow* GetWindow(int contextWindow)
{
  return GetWindowWithCondition(contextWindow, 0);
}

CFileItemPtr GetCurrentListItemFromWindow(int contextWindow)
{
  CGUIWindow* window = GetWindowWithCondition(contextWindow, WINDOW_CONDITION_HAS_LIST_ITEMS);
  if (window)
    return window->GetCurrentListItem();

  return CFileItemPtr();
}

CGUIMediaWindow* GetMediaWindow(int contextWindow)
{
  CGUIWindow* window = GetWindowWithCondition(contextWindow, WINDOW_CONDITION_IS_MEDIA_WINDOW);
  if (window)
    return static_cast<CGUIMediaWindow*>(window);

  return nullptr;
}

CGUIControl* GetActiveContainer(int containerId, int contextWindow)
{
  CGUIWindow *window = GetWindow(contextWindow);
  if (!window)
    return nullptr;

  CGUIControl *control = nullptr;
  if (!containerId) // No container specified, so we lookup the current view container
  {
    if (window->IsMediaWindow())
      containerId = static_cast<CGUIMediaWindow*>(window)->GetViewContainerID();
    else
      control = window->GetFocusedControl();
  }

  if (!control)
    control = window->GetControl(containerId);

  if (control && control->IsContainer())
    return control;

  return nullptr;
}

CGUIListItemPtr GetCurrentListItem(int contextWindow, int containerId /* = 0 */, int itemOffset /* = 0 */, unsigned int itemFlags /* = 0 */)
{
  CGUIListItemPtr item;

  if (containerId == 0  &&
      itemOffset == 0 &&
      !(itemFlags & INFOFLAG_LISTITEM_CONTAINER) &&
      !(itemFlags & INFOFLAG_LISTITEM_ABSOLUTE) &&
      !(itemFlags & INFOFLAG_LISTITEM_POSITION))
    item = GetCurrentListItemFromWindow(contextWindow);

  if (!item)
  {
    CGUIControl* activeContainer = GetActiveContainer(containerId, contextWindow);
    if (activeContainer)
      item = static_cast<IGUIContainer *>(activeContainer)->GetListItem(itemOffset, itemFlags);
  }

  return item;
}

std::string GetFileInfoLabelValueFromPath(int info, const std::string& filenameAndPath)
{
  std::string value = filenameAndPath;

  if (info == PLAYER_PATH)
  {
    // do this twice since we want the path outside the archive if this is to be of use.
    if (URIUtils::IsInArchive(value))
      value = URIUtils::GetParentPath(value);

    value = URIUtils::GetParentPath(value);
  }
  else if (info == PLAYER_FILENAME)
  {
    value = URIUtils::GetFileName(value);
  }

  return value;
}

} // namespace GUIINFO
} // namespace GUILIB
} // namespace KODI