summaryrefslogtreecommitdiffstats
path: root/xbmc/music/dialogs/GUIDialogSongInfo.cpp
blob: 8efca94ab4e2c0cfca527b02405178d9543c29a2 (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
 *  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 "GUIDialogSongInfo.h"

#include "GUIDialogMusicInfo.h"
#include "GUIPassword.h"
#include "GUIUserMessages.h"
#include "ServiceBroker.h"
#include "TextureCache.h"
#include "Util.h"
#include "dialogs/GUIDialogBusy.h"
#include "dialogs/GUIDialogFileBrowser.h"
#include "guilib/GUIComponent.h"
#include "guilib/GUIWindowManager.h"
#include "guilib/LocalizeStrings.h"
#include "input/Key.h"
#include "music/MusicDatabase.h"
#include "music/MusicUtils.h"
#include "music/tags/MusicInfoTag.h"
#include "music/windows/GUIWindowMusicBase.h"
#include "profiles/ProfileManager.h"
#include "settings/MediaSourceSettings.h"
#include "settings/SettingsComponent.h"
#include "storage/MediaManager.h"
#include "utils/FileUtils.h"

#define CONTROL_BTN_REFRESH       6
#define CONTROL_USERRATING        7
#define CONTROL_BTN_PLAY 8
#define CONTROL_BTN_GET_THUMB     10
#define CONTROL_ALBUMINFO         12

#define CONTROL_LIST              50

#define TIME_TO_BUSY_DIALOG 500



class CGetSongInfoJob : public CJob
{
public:
  ~CGetSongInfoJob(void) override = default;

  // Fetch full song information including art types list
  bool DoWork() override
  {
    CGUIDialogSongInfo *dialog = CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogSongInfo>(WINDOW_DIALOG_SONG_INFO);
    if (!dialog)
      return false;
    if (dialog->IsCancelled())
      return false;
    CFileItemPtr m_song = dialog->GetCurrentListItem();

    // Fetch tag data from library using filename of item path, or scanning file
    // (if item does not already have this loaded)
    if (!m_song->LoadMusicTag())
    {
      // Stop SongInfoDialog waiting
      dialog->FetchComplete();
      return false;
    }
    if (dialog->IsCancelled())
      return false;
    // Fetch album and primary song artist data from library as properties
    // and lyrics by scanning tags from file
    MUSIC_INFO::CMusicInfoLoader::LoadAdditionalTagInfo(m_song.get());
    if (dialog->IsCancelled())
      return false;

    // Get album path (for use in browsing art selection)
    std::string albumpath;
    CMusicDatabase db;
    db.Open();
    db.GetAlbumPath(m_song->GetMusicInfoTag()->GetAlbumId(), albumpath);
    m_song->SetProperty("album_path", albumpath);
    db.Close();
    if (dialog->IsCancelled())
      return false;

    // Load song art.
    // For songs in library this includes related album and artist(s) art.
    // Also fetches artist art for non library songs when artist can be found
    // uniquely by name, otherwise just embedded or cached thumb is fetched.
    CMusicThumbLoader loader;
    loader.LoadItem(m_song.get());
    if (dialog->IsCancelled())
      return false;

    // For songs in library fill vector of possible art types, with current art when it exists
    // for display on the art type selection dialog
    CFileItemList artlist;
    MUSIC_UTILS::FillArtTypesList(*m_song, artlist);
    dialog->SetArtTypeList(artlist);
    if (dialog->IsCancelled())
      return false;

    // Tell waiting SongInfoDialog that job is complete
    dialog->FetchComplete();

    return true;
  }
};

CGUIDialogSongInfo::CGUIDialogSongInfo(void)
    : CGUIDialog(WINDOW_DIALOG_SONG_INFO, "DialogMusicInfo.xml")
    , m_song(new CFileItem)
{
  m_cancelled = false;
  m_hasUpdatedUserrating = false;
  m_startUserrating = -1;
  m_artTypeList.Clear();
  m_loadType = KEEP_IN_MEMORY;
}

CGUIDialogSongInfo::~CGUIDialogSongInfo(void) = default;

bool CGUIDialogSongInfo::OnMessage(CGUIMessage& message)
{
  switch (message.GetMessage())
  {
  case GUI_MSG_WINDOW_DEINIT:
    {
      m_artTypeList.Clear();
      if (m_startUserrating != m_song->GetMusicInfoTag()->GetUserrating())
      {
        m_hasUpdatedUserrating = true;

        // Asynchronously update song userrating in library
        MUSIC_UTILS::UpdateSongRatingJob(m_song, m_song->GetMusicInfoTag()->GetUserrating());

        // Send a message to all windows to tell them to update the fileitem
        // This communicates the rating change to the music lib window, current playlist and OSD.
        // The music lib window item is updated to but changes to the rating when it is the sort
        // do not show on screen until refresh() that fetches the list from scratch, sorts etc.
        CGUIMessage msg(GUI_MSG_NOTIFY_ALL, 0, 0, GUI_MSG_UPDATE_ITEM, 0, m_song);
        CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg);
      }
      CGUIMessage msg(GUI_MSG_LABEL_RESET, GetID(), CONTROL_LIST);
      OnMessage(msg);
      break;
    }
  case GUI_MSG_WINDOW_INIT:
    CGUIDialog::OnMessage(message);
    Update();
    m_cancelled = false;
    break;

  case GUI_MSG_CLICKED:
    {
      int iControl = message.GetSenderId();
      if (iControl == CONTROL_USERRATING)
      {
        OnSetUserrating();
      }
      else if (iControl == CONTROL_ALBUMINFO)
      {
        CGUIDialogMusicInfo::ShowForAlbum(m_albumId);
        return true;
      }
      else if (iControl == CONTROL_BTN_GET_THUMB)
      {
        OnGetArt();
        return true;
      }
      else if (iControl == CONTROL_LIST)
      {
        int iAction = message.GetParam1();
        if ((ACTION_SELECT_ITEM == iAction || ACTION_MOUSE_LEFT_CLICK == iAction))
        {
          CGUIMessage msg(GUI_MSG_ITEM_SELECTED, GetID(), iControl);
          CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg);
          int iItem = msg.GetParam1();
          if (iItem < 0 || iItem >= static_cast<int>(m_song->GetMusicInfoTag()->GetContributors().size()))
            break;
          int idArtist = m_song->GetMusicInfoTag()->GetContributors()[iItem].GetArtistId();
          if (idArtist > 0)
            CGUIDialogMusicInfo::ShowForArtist(idArtist);
          return true;
        }
      }
      else if (iControl == CONTROL_BTN_PLAY)
      {
        OnPlaySong(m_song);
        return true;
      }
      return false;
    }
    break;
  }

  return CGUIDialog::OnMessage(message);
}

bool CGUIDialogSongInfo::OnAction(const CAction& action)
{
  int userrating = m_song->GetMusicInfoTag()->GetUserrating();
  if (action.GetID() == ACTION_INCREASE_RATING)
  {
    SetUserrating(userrating + 1);
    return true;
  }
  else if (action.GetID() == ACTION_DECREASE_RATING)
  {
    SetUserrating(userrating - 1);
    return true;
  }
  else if (action.GetID() == ACTION_SHOW_INFO)
  {
    Close();
    return true;
  }
  return CGUIDialog::OnAction(action);
}

bool CGUIDialogSongInfo::OnBack(int actionID)
{
  m_cancelled = true;
  return CGUIDialog::OnBack(actionID);
}

void CGUIDialogSongInfo::FetchComplete()
{
  //Trigger the event to indicate data has been fetched
  m_event.Set();
}

void CGUIDialogSongInfo::OnInitWindow()
{
  // Enable album info button when we know album
  m_albumId = m_song->GetMusicInfoTag()->GetAlbumId();

  CONTROL_ENABLE_ON_CONDITION(CONTROL_ALBUMINFO, m_albumId > 0);

  // Disable music user rating button for plugins as they don't have tables to save this
  if (m_song->IsPlugin())
    CONTROL_DISABLE(CONTROL_USERRATING);
  else
    CONTROL_ENABLE(CONTROL_USERRATING);

  // Disable the Choose Art button if the user isn't allowed it
  const std::shared_ptr<CProfileManager> profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager();
  CONTROL_ENABLE_ON_CONDITION(CONTROL_BTN_GET_THUMB,
    profileManager->GetCurrentProfile().canWriteDatabases() || g_passwordManager.bMasterUser);

  SET_CONTROL_HIDDEN(CONTROL_BTN_REFRESH);
  SET_CONTROL_LABEL(CONTROL_USERRATING, 38023);
  SET_CONTROL_LABEL(CONTROL_BTN_GET_THUMB, 13511);
  SET_CONTROL_LABEL(CONTROL_ALBUMINFO, 10523);
  SET_CONTROL_LABEL(CONTROL_BTN_PLAY, 208);

  CGUIDialog::OnInitWindow();
}

void CGUIDialogSongInfo::Update()
{
  CFileItemList items;
  for (const auto& contributor : m_song->GetMusicInfoTag()->GetContributors())
  {
    auto item = std::make_shared<CFileItem>(contributor.GetRoleDesc());
    item->SetLabel2(contributor.GetArtist());
    item->GetMusicInfoTag()->SetDatabaseId(contributor.GetArtistId(), MediaTypeArtist);
    items.Add(std::move(item));
  }
  CGUIMessage message(GUI_MSG_LABEL_BIND, GetID(), CONTROL_LIST, 0, 0, &items);
  OnMessage(message);
}

void CGUIDialogSongInfo::SetUserrating(int userrating)
{
  userrating = std::max(userrating, 0);
  userrating = std::min(userrating, 10);
  if (userrating != m_song->GetMusicInfoTag()->GetUserrating())
  {
    m_song->GetMusicInfoTag()->SetUserrating(userrating);
  }
}

bool CGUIDialogSongInfo::SetSong(CFileItem* item)
{
  *m_song = *item;
  m_event.Reset();
  m_cancelled = false;  // SetSong happens before win_init
  // In a separate job fetch song info and fill list of art types.
  int jobid =
      CServiceBroker::GetJobManager()->AddJob(new CGetSongInfoJob(), nullptr, CJob::PRIORITY_LOW);

  // Wait to get all data before show, allowing user to cancel if fetch is slow
  if (!CGUIDialogBusy::WaitOnEvent(m_event, TIME_TO_BUSY_DIALOG))
  {
    // Cancel job still waiting in queue (unlikely)
    CServiceBroker::GetJobManager()->CancelJob(jobid);
    // Flag to stop job already in progress
    m_cancelled = true;
    return false;
  }

  // Store initial userrating
  m_startUserrating = m_song->GetMusicInfoTag()->GetUserrating();
  m_hasUpdatedUserrating = false;
  return true;
}

void CGUIDialogSongInfo::SetArtTypeList(CFileItemList& artlist)
{
  m_artTypeList.Copy(artlist);
}

CFileItemPtr CGUIDialogSongInfo::GetCurrentListItem(int offset)
{
  return m_song;
}

std::string CGUIDialogSongInfo::GetContent()
{
  return "songs";
}

/*
  Allow user to choose artwork for the song
  For each type of art the options are:
  1.  Current art
  2.  Local art (thumb found by filename)
  3.  Embedded art (@todo)
  4.  None
  Note that songs are not scraped, hence there is no list of urls for possible remote art
*/
void CGUIDialogSongInfo::OnGetArt()
{
  std::string type = MUSIC_UTILS::ShowSelectArtTypeDialog(m_artTypeList);
  if (type.empty())
    return; // Cancelled

  CFileItemList items;
  CGUIListItem::ArtMap primeArt = m_song->GetArt(); // Song art without fallbacks
  bool bHasArt = m_song->HasArt(type);
  bool bFallback(false);
  if (bHasArt)
  {
    // Check if that type of art is actually a fallback, e.g. album thumb or artist fanart
    CGUIListItem::ArtMap::const_iterator i = primeArt.find(type);
    bFallback = (i == primeArt.end());
  }

  // Build list of possible images of that art type
  if (bHasArt)
  {
    // Add item for current artwork, could a fallback from album/artist
    CFileItemPtr item(new CFileItem("thumb://Current", false));
    item->SetArt("thumb", m_song->GetArt(type));
    item->SetArt("icon", "DefaultPicture.png");
    item->SetLabel(g_localizeStrings.Get(13512));  //! @todo: label fallback art so user knows?
    items.Add(item);
  }
  else if (m_song->HasArt("thumb"))
  { // For missing art of that type add the thumb (when it exists and not a fallback)
    CGUIListItem::ArtMap::const_iterator i = primeArt.find("thumb");
    if (i != primeArt.end())
    {
      CFileItemPtr item(new CFileItem("thumb://Thumb", false));
      item->SetArt("thumb", m_song->GetArt("thumb"));
      item->SetArt("icon", "DefaultAlbumCover.png");
      item->SetLabel(g_localizeStrings.Get(21371));
      items.Add(item);
    }
  }

  std::string localThumb;
  if (type == "thumb")
  { // Local thumb type art held in <filename>.tbn (for non-library items)
    localThumb = m_song->GetUserMusicThumb(true);
    if (m_song->IsMusicDb())
    {
      CFileItem item(m_song->GetMusicInfoTag()->GetURL(), false);
      localThumb = item.GetUserMusicThumb(true);
    }
    if (CFileUtils::Exists(localThumb))
    {
      CFileItemPtr item(new CFileItem("thumb://Local", false));
      item->SetArt("thumb", localThumb);
      item->SetLabel(g_localizeStrings.Get(20017));
      items.Add(item);
    }
  }

  // Clear these local images from cache so user will see any recent
  // local file changes immediately
  for (auto& item : items)
  {
    std::string thumb(item->GetArt("thumb"));
    if (thumb.empty())
      continue;
    CServiceBroker::GetTextureCache()->ClearCachedImage(thumb);
    // Remove any thumbnail of local image too (created when browsing files)
    std::string thumbthumb(CTextureUtils::GetWrappedThumbURL(thumb));
    CServiceBroker::GetTextureCache()->ClearCachedImage(thumbthumb);
  }

  if (bHasArt && !bFallback)
  { // Actually has this type of art (not a fallback) so
    // allow the user to delete it by selecting "no art".
    CFileItemPtr item(new CFileItem("thumb://None", false));
    item->SetArt("thumb", "DefaultAlbumCover.png");
    item->SetLabel(g_localizeStrings.Get(13515));
    items.Add(item);
  }

  //! @todo: Add support for extracting embedded art

  // Show list of possible art for user selection
  std::string result;
  VECSOURCES sources(*CMediaSourceSettings::GetInstance().GetSources("music"));
  // Add album folder as source (could be disc set)
  std::string albumpath = m_song->GetProperty("album_path").asString();
  if (!albumpath.empty())
  {
    CFileItem pathItem(albumpath, true);
    CGUIDialogMusicInfo::AddItemPathToFileBrowserSources(sources, pathItem);
  }
  else  // Add parent folder of song
    CGUIDialogMusicInfo::AddItemPathToFileBrowserSources(sources, *m_song);
  CServiceBroker::GetMediaManager().GetLocalDrives(sources);
  if (CGUIDialogFileBrowser::ShowAndGetImage(items, sources, g_localizeStrings.Get(13511), result) &&
    result != "thumb://Current")
  {
    // User didn't choose the one they have, or the fallback image.
    // Overwrite with the new art or clear it
    std::string newArt;
    if (result == "thumb://Thumb")
      newArt = m_song->GetArt("thumb");
    else if (result == "thumb://Local")
      newArt = localThumb;
//    else if (result == "thumb://Embedded")
//      newArt = embeddedArt;
    else if (CFileUtils::Exists(result))
      newArt = result;
    else // none
      newArt.clear();

    // Asynchronously update that type of art in the database
    MUSIC_UTILS::UpdateArtJob(m_song, type, newArt);

    // Update local song with current art
    if (newArt.empty())
    {
      // Remove that type of art from the song
      primeArt.erase(type);
      m_song->SetArt(primeArt);
    }
    else
      // Add or modify the type of art
      m_song->SetArt(type, newArt);

    // Update local art list with current art
    // Show any fallback art when song art removed
    if (newArt.empty() && m_song->HasArt(type))
      newArt = m_song->GetArt(type);
    for (const auto& artitem : m_artTypeList)
    {
      if (artitem->GetProperty("artType") == type)
      {
        artitem->SetArt("thumb", newArt);
        break;
      }
    }

    // Get new artwork to show in other places e.g. on music lib window,
    // current playlist and player OSD.
    CGUIMessage msg(GUI_MSG_NOTIFY_ALL, 0, 0, GUI_MSG_UPDATE_ITEM, 0, m_song);
    CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg);

  }

  // Re-open the art type selection dialog as we come back from
  // the image selection dialog
  OnGetArt();
}

void CGUIDialogSongInfo::OnSetUserrating()
{
  int userrating = MUSIC_UTILS::ShowSelectRatingDialog(m_song->GetMusicInfoTag()->GetUserrating());
  if (userrating < 0) // Nothing selected, so rating unchanged
    return;

  SetUserrating(userrating);
}

void CGUIDialogSongInfo::ShowFor(CFileItem* pItem)
{
  if (pItem->m_bIsFolder)
    return;
  if (!pItem->IsMusicDb())
    pItem->LoadMusicTag();
  if (!pItem->HasMusicInfoTag())
    return;

  CGUIDialogSongInfo *dialog = CServiceBroker::GetGUI()->GetWindowManager().
    GetWindow<CGUIDialogSongInfo>(WINDOW_DIALOG_SONG_INFO);
  if (dialog)
  {
    if (dialog->SetSong(pItem))  // Fetch full song info asynchronously
    {
      dialog->Open();
      if (dialog->HasUpdatedUserrating())
      {
        auto window = CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIWindowMusicBase>(WINDOW_MUSIC_NAV);
        if (window)
          window->RefreshContent("songs");
      }
    }
  }
}

void CGUIDialogSongInfo::OnPlaySong(const std::shared_ptr<CFileItem>& item)
{
  Close(true);
  MUSIC_UTILS::PlayItem(item);
}