MythTV  master
v2music.cpp
Go to the documentation of this file.
1 // Program Name: music.cpp
3 // Created : July 20, 2017
4 //
5 // Copyright (c) 2017 Paul Harrison <pharrison@mythtv.org>
6 //
7 // Licensed under the GPL v2 or later, see COPYING for details
8 //
10 
11 // C/C++
12 #include <cmath>
13 #include <unistd.h>
14 
15 // MythTV
17 #include "libmythbase/mythversion.h"
19 
20 // MythBackend
21 #include "v2music.h"
22 #include "v2serviceUtil.h"
23 
25 //
27 
28 // This will be initialised in a thread safe manner on first use
30  (MUSIC_HANDLE, V2Music::staticMetaObject, &V2Music::RegisterCustomTypes))
31 
33 {
34  qRegisterMetaType<V2MusicMetadataInfoList*>("V2MusicMetadataInfoList");
35  qRegisterMetaType<V2MusicMetadataInfo*>("V2MusicMetadataInfo");
36 }
37 
39  : MythHTTPService(s_service)
40 {
41 }
42 
44  int nCount)
45 {
46  auto *all_music = new AllMusic();
47 
48  while (!all_music->doneLoading())
49  {
50  qApp->processEvents();
51  usleep(50000);
52  }
53 
54  MetadataPtrList *musicList = all_music->getAllMetadata();
55 
56  // ----------------------------------------------------------------------
57  // Build Response
58  // ----------------------------------------------------------------------
59 
60  auto *pMusicMetadataInfos = new V2MusicMetadataInfoList();
61 
62  int musicListCount = musicList->count();
63  nStartIndex = (nStartIndex > 0) ? std::min( nStartIndex, musicListCount ) : 0;
64  nCount = (nCount > 0) ? std::min( nCount, musicListCount ) : musicListCount;
65  int nEndIndex = std::min((nStartIndex + nCount), musicListCount );
66 
67  for( int n = nStartIndex; n < nEndIndex; n++ )
68  {
69  V2MusicMetadataInfo *pMusicMetadataInfo = pMusicMetadataInfos->AddNewMusicMetadataInfo();
70 
71  MusicMetadata *metadata = musicList->at(n);
72 
73  if (metadata)
74  V2FillMusicMetadataInfo ( pMusicMetadataInfo, metadata, true );
75  }
76 
77  int curPage = 0;
78  int totalPages = 0;
79  if (nCount == 0)
80  totalPages = 1;
81  else
82  totalPages = (int)std::ceil((float)musicList->count() / nCount);
83 
84  if (totalPages == 1)
85  curPage = 1;
86  else
87  {
88  curPage = (int)std::ceil((float)nStartIndex / nCount) + 1;
89  }
90 
91  pMusicMetadataInfos->setStartIndex ( nStartIndex );
92  pMusicMetadataInfos->setCount ( nCount );
93  pMusicMetadataInfos->setCurrentPage ( curPage );
94  pMusicMetadataInfos->setTotalPages ( totalPages );
95  pMusicMetadataInfos->setTotalAvailable( musicList->count() );
96  pMusicMetadataInfos->setAsOf ( MythDate::current() );
97  pMusicMetadataInfos->setVersion ( MYTH_BINARY_VERSION );
98  pMusicMetadataInfos->setProtoVer ( MYTH_PROTO_VERSION );
99 
100  delete all_music;
101 
102  return pMusicMetadataInfos;
103 }
104 
106 //
108 
110 {
111  auto *all_music = new AllMusic();
112 
113  while (!all_music->doneLoading())
114  {
115  qApp->processEvents();
116  usleep(50000);
117  }
118 
119  MusicMetadata *metadata = all_music->getMetadata(Id);
120 
121  if (!metadata)
122  {
123  delete all_music;
124  throw(QString("No metadata found for selected ID!."));
125  }
126 
127  auto *pMusicMetadataInfo = new V2MusicMetadataInfo();
128 
129  V2FillMusicMetadataInfo(pMusicMetadataInfo, metadata, true);
130 
131  delete all_music;
132 
133  return pMusicMetadataInfo;
134 }
v2music.h
V2MusicMetadataInfoList
Definition: v2musicMetadataInfoList.h:20
V2Music::RegisterCustomTypes
static void RegisterCustomTypes()
AllMusic
Definition: musicmetadata.h:418
mythhttpmetaservice.h
MusicMetadata
Definition: musicmetadata.h:80
V2Music::GetTrackList
static V2MusicMetadataInfoList * GetTrackList(int StartIndex, int Count)
Definition: v2music.cpp:43
V2Music::GetTrack
static V2MusicMetadataInfo * GetTrack(int Id)
Definition: v2music.cpp:109
MythDate::current
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:14
V2Music::V2Music
V2Music()
Definition: v2music.cpp:38
MUSIC_HANDLE
#define MUSIC_HANDLE
Definition: v2music.h:18
v2serviceUtil.h
MythHTTPService
Definition: mythhttpservice.h:19
V2MusicMetadataInfo
Definition: v2musicMetadataInfo.h:22
V2FillMusicMetadataInfo
void V2FillMusicMetadataInfo(V2MusicMetadataInfo *pVideoMetadataInfo, MusicMetadata *pMetadata, bool bDetails)
Definition: v2serviceUtil.cpp:488
Q_GLOBAL_STATIC_WITH_ARGS
Q_GLOBAL_STATIC_WITH_ARGS(MythHTTPMetaService, s_service,(MUSIC_HANDLE, V2Music::staticMetaObject, &V2Music::RegisterCustomTypes)) void V2Music
Definition: v2music.cpp:29
MetadataPtrList
QList< MusicMetadata * > MetadataPtrList
Definition: musicmetadata.h:396
musicmetadata.h
MythHTTPMetaService
Definition: mythhttpmetaservice.h:10