MythTV master
v2music.cpp
Go to the documentation of this file.
1
2// 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 <thread>
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 std::this_thread::sleep_for(50ms);
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 {
86 curPage = 1;
87 }
88 else
89 {
90 curPage = (int)std::ceil((float)nStartIndex / nCount) + 1;
91 }
92
93 pMusicMetadataInfos->setStartIndex ( nStartIndex );
94 pMusicMetadataInfos->setCount ( nCount );
95 pMusicMetadataInfos->setCurrentPage ( curPage );
96 pMusicMetadataInfos->setTotalPages ( totalPages );
97 pMusicMetadataInfos->setTotalAvailable( musicList->count() );
98 pMusicMetadataInfos->setAsOf ( MythDate::current() );
99 pMusicMetadataInfos->setVersion ( MYTH_BINARY_VERSION );
100 pMusicMetadataInfos->setProtoVer ( MYTH_PROTO_VERSION );
101
102 delete all_music;
103
104 return pMusicMetadataInfos;
105}
106
108//
110
112{
113 auto *all_music = new AllMusic();
114
115 while (!all_music->doneLoading())
116 {
117 qApp->processEvents();
118 std::this_thread::sleep_for(50ms);
119 }
120
121 MusicMetadata *metadata = all_music->getMetadata(Id);
122
123 if (!metadata)
124 {
125 delete all_music;
126 throw(QString("No metadata found for selected ID!."));
127 }
128
129 auto *pMusicMetadataInfo = new V2MusicMetadataInfo();
130
131 V2FillMusicMetadataInfo(pMusicMetadataInfo, metadata, true);
132
133 delete all_music;
134
135 return pMusicMetadataInfo;
136}
static V2MusicMetadataInfo * GetTrack(int Id)
Definition: v2music.cpp:111
static V2MusicMetadataInfoList * GetTrackList(int StartIndex, int Count)
Definition: v2music.cpp:43
static void RegisterCustomTypes()
V2Music()
Definition: v2music.cpp:38
QList< MusicMetadata * > MetadataPtrList
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:15
Q_GLOBAL_STATIC_WITH_ARGS(MythHTTPMetaService, s_service,(MUSIC_HANDLE, V2Music::staticMetaObject, &V2Music::RegisterCustomTypes)) void V2Music
Definition: v2music.cpp:29
#define MUSIC_HANDLE
Definition: v2music.h:18
void V2FillMusicMetadataInfo(V2MusicMetadataInfo *pVideoMetadataInfo, MusicMetadata *pMetadata, bool bDetails)