Skip to content

Commit

Permalink
Services API: Fix inconsistent results
Browse files Browse the repository at this point in the history
- Fix the bug in the existing code. The code for xml translates enum
  variables to their names (if they use Q_ENUM), but the code for json
  does not. Add the missing code to the json serializer so that enum
  variables are treated the same in all cases.
- Change the Status field to be an int so that it always returns the
  numeric value
- Add a field StatusName that is defined as a RecStatus enum and returns
  the enum value name.
- Change the RecStatusToString and RecStatusToDescription methods to take
  either the numeric Status value or the enum value name.

Fixes #740
  • Loading branch information
bennettpeter committed Mar 30, 2023
1 parent 09c82a1 commit 902a1f7
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 14 deletions.
16 changes: 14 additions & 2 deletions mythtv/libs/libmythbase/http/serialisers/mythjsonserialiser.cpp
Expand Up @@ -28,7 +28,7 @@ void MythJSONSerialiser::AddObject(const QString& Name, const QVariant& Value)
m_first.top() = false;
}

void MythJSONSerialiser::AddValue(const QVariant& Value)
void MythJSONSerialiser::AddValue(const QVariant& Value, const QMetaProperty *MetaProperty)
{
if (Value.isNull() || !Value.isValid())
{
Expand All @@ -49,6 +49,18 @@ void MythJSONSerialiser::AddValue(const QVariant& Value)
return;
}

// Enum ?
if (MetaProperty && (MetaProperty->isEnumType() || MetaProperty->isFlagType()))
{
QMetaEnum metaEnum = MetaProperty->enumerator();
QString value = MetaProperty->isFlagType() ? metaEnum.valueToKeys(Value.toInt()).constData() :
metaEnum.valueToKey(Value.toInt());
// If couldn't convert to enum name, return raw value
value = (value.isEmpty() ? Value.toString() : value);
m_writer << "\"" << Encode(value) << "\"";
return;
}

switch (
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
static_cast<QMetaType::Type>(Value.type())
Expand Down Expand Up @@ -105,7 +117,7 @@ void MythJSONSerialiser::AddQObject(const QObject* Object)
continue;
QVariant value(Object->property(rawname));
m_writer << first << "\"" << name << "\": ";
AddValue(value);
AddValue(value, &metaProperty);
first = ", ";
}
}
Expand Down
Expand Up @@ -14,7 +14,7 @@ class MythJSONSerialiser : public MythSerialiser

protected:
void AddObject (const QString& Name, const QVariant& Value);
void AddValue (const QVariant& Value);
void AddValue (const QVariant& Value, const QMetaProperty *MetaProperty = nullptr);
void AddQObject (const QObject* Object);
void AddStringList(const QVariant& Values);
void AddList (const QVariant& Values);
Expand Down
46 changes: 39 additions & 7 deletions mythtv/programs/mythbackend/servicesv2/v2dvr.cpp
Expand Up @@ -1344,8 +1344,22 @@ V2ProgramList* V2Dvr::GetUpcomingList( int nStartIndex,
int nCount,
bool bShowAll,
int nRecordId,
int nRecStatus )
const QString & RecStatus )
{
int nRecStatus = 0;
if (!RecStatus.isEmpty())
{
// Handle enum name
QMetaEnum meta = QMetaEnum::fromType<RecStatus::Type>();
bool ok;
nRecStatus = meta.keyToValue(RecStatus.toLocal8Bit(), &ok);
// if enum name not valid try for int nRecStatus
if (!ok)
nRecStatus = RecStatus.toInt(&ok);
// if still not valid use 99999 to trigger an "unknown" response
if (!ok)
nRecStatus = 99999;
}
auto *pPrograms = new V2ProgramList();
int size = FillUpcomingList(pPrograms->GetPrograms(), pPrograms,
nStartIndex,
Expand Down Expand Up @@ -1904,18 +1918,36 @@ int V2Dvr::RecordedIdForPathname(const QString & pathname)
return recordedid;
}

QString V2Dvr::RecStatusToString(int RecStatus)
QString V2Dvr::RecStatusToString(const QString & RecStatus)
{
auto type = static_cast<RecStatus::Type>(RecStatus);
// Handle enum name
QMetaEnum meta = QMetaEnum::fromType<RecStatus::Type>();
bool ok;
int value = meta.keyToValue(RecStatus.toLocal8Bit(), &ok);
// if enum name not valid try for int value
if (!ok)
value = RecStatus.toInt(&ok);
// if still not valid use 0 to trigger an "unknown" response
if (!ok)
value = 0;
auto type = static_cast<RecStatus::Type>(value);
return RecStatus::toString(type);
}

QString V2Dvr::RecStatusToDescription(int RecStatus, int recType,
QString V2Dvr::RecStatusToDescription(const QString & RecStatus, int recType,
const QDateTime &StartTime)
{
//if (!StartTime.isValid())
// throw QString("StartTime appears invalid.");
auto recstatusType = static_cast<RecStatus::Type>(RecStatus);
// Handle enum name
QMetaEnum meta = QMetaEnum::fromType<RecStatus::Type>();
bool ok;
int value = meta.keyToValue(RecStatus.toLocal8Bit(), &ok);
// if enum name not valid try for int value
if (!ok)
value = RecStatus.toInt(&ok);
// if still not valid use 0 to trigger an "unknown" response
if (!ok)
value = 0;
auto recstatusType = static_cast<RecStatus::Type>(value);
auto recordingType = static_cast<RecordingType>(recType);
return RecStatus::toDescription(recstatusType, recordingType, StartTime);
}
Expand Down
6 changes: 3 additions & 3 deletions mythtv/programs/mythbackend/servicesv2/v2dvr.h
Expand Up @@ -203,7 +203,7 @@ class V2Dvr : public MythHTTPService
int Count,
bool ShowAll,
int RecordId,
int RecStatus );
const QString & RecStatus );

static V2EncoderList* GetEncoderList ( );

Expand Down Expand Up @@ -339,9 +339,9 @@ class V2Dvr : public MythHTTPService

static int RecordedIdForPathname( const QString &Pathname );

static QString RecStatusToString ( int RecStatus );
static QString RecStatusToString ( const QString & RecStatus );

static QString RecStatusToDescription ( int RecStatus,
static QString RecStatusToDescription (const QString & RecStatus,
int RecType,
const QDateTime &StartTime );

Expand Down
4 changes: 3 additions & 1 deletion mythtv/programs/mythbackend/servicesv2/v2recording.h
Expand Up @@ -26,7 +26,8 @@ class V2RecordingInfo : public QObject
Q_CLASSINFO( "Version", "1.3" );

SERVICE_PROPERTY2( uint , RecordedId )
SERVICE_PROPERTY2( RecStatus::Type , Status )
SERVICE_PROPERTY2( int , Status )
SERVICE_PROPERTY2( RecStatus::Type , StatusName )
SERVICE_PROPERTY2( int , Priority )
SERVICE_PROPERTY2( QDateTime , StartTs )
SERVICE_PROPERTY2( QDateTime , EndTs )
Expand Down Expand Up @@ -63,6 +64,7 @@ class V2RecordingInfo : public QObject
{
m_RecordedId = src->m_RecordedId ;
m_Status = src->m_Status ;
m_StatusName = src->m_StatusName ;
m_Priority = src->m_Priority ;
m_StartTs = src->m_StartTs ;
m_EndTs = src->m_EndTs ;
Expand Down
1 change: 1 addition & 0 deletions mythtv/programs/mythbackend/servicesv2/v2serviceUtil.cpp
Expand Up @@ -105,6 +105,7 @@ void V2FillProgramInfo( V2Program *pProgram,

pRecording->setRecordedId ( pRecInfo.GetRecordingID() );
pRecording->setStatus ( pRecInfo.GetRecordingStatus() );
pRecording->setStatusName ( pRecInfo.GetRecordingStatus() );
pRecording->setPriority( pRecInfo.GetRecordingPriority() );
pRecording->setStartTs ( pRecInfo.GetRecordingStartTime() );
pRecording->setEndTs ( pRecInfo.GetRecordingEndTime() );
Expand Down

0 comments on commit 902a1f7

Please sign in to comment.