Jellyfin Qt
QML Library for interacting with the Jellyfin multimedia server
Loading...
Searching...
No Matches
Jellyfin::ApiClient Class Reference

An Api client for Jellyfin. Handles requests and authentication. More...

#include <apiclient.h>

Inheritance diagram for Jellyfin::ApiClient:

Public Types

enum  ApiError { JSON_ERROR , UNEXPECTED_REPLY , UNEXPECTED_STATUS , INVALID_PASSWORD }
 

Public Slots

void restoreSavedSession ()
 Tries to access credentials and connect to a server. If nothing has been configured yet, emits setupRequired();.
 
void setupConnection ()
 
void authenticate (QString username, QString password, bool storeCredentials=false)
 
void submitQuickConnectCode (const QString &code)
 
void deleteSession ()
 Logs the user out and clears the session.
 
void postCapabilities ()
 Shares the capabilities of this device to the server.
 
QString downloadUrl (const QString &itemId) const
 

Signals

void authenticationRequired ()
 
void authenticationError (ApiError error)
 
void connectionFailed (ApiError error)
 
void connectionSuccess (QString loginMessage)
 
void networkError (QNetworkReply::NetworkError error)
 
void authenticatedChanged (bool authenticated)
 
void baseUrlChanged (const QString &baseUrl)
 
void appNameChanged (const QString &newAppName)
 
void settingsChanged ()
 
void setupRequired ()
 Set-up is required. You'll need to manually set up the baseUrl-property, call setupConnection afterwards and finally call authenticate.
 
void userIdChanged (QString userId)
 
void deviceProfileChanged ()
 
void deviceTypeChanged ()
 
void supportedCommandsChanged ()
 
void onlineChanged ()
 
void quickConnectAccepted ()
 Emitted after submitQuickConnectCode succeeded.
 
void quickConnectRejected ()
 Emitted after submitQuickConnectCode failed.
 
void userDataChanged (const QString &itemId, UserData *userData)
 onUserDataChanged Emitted when the user data of an item is changed on the server.
 

Public Member Functions

 ApiClient (QObject *parent=nullptr)
 
virtual ~ApiClient ()
 
bool authenticated () const
 
void setBaseUrl (const QString &url)
 
void setAppName (const QString &appName)
 
void setDeviceType (Model::DeviceType deviceType)
 
QNetworkReply * get (const QString &path, const QUrlQuery &params=QUrlQuery())
 
QNetworkReply * post (const QString &path, const QJsonDocument &data, const QUrlQuery &params=QUrlQuery())
 
QNetworkReply * post (const QString &path, const QByteArray &data=QByteArray(), const QUrlQuery &params=QUrlQuery())
 
const QString & baseUrl () const
 
const QString & appName () const
 
const QString & userId () const
 
const QString & deviceId () const
 
Model::DeviceType deviceType () const
 
QVariantList supportedCommands () const
 QML applications can set this type to indicate which commands they support.
 
void setSupportedCommands (QVariantList newSupportedCommands)
 
const QJsonObject deviceProfileJson () const
 
QSharedPointer< DTO::DeviceProfiledeviceProfile () const
 
const QJsonObject clientCapabilities () const
 
const QString & token () const
 Retrieves the authentication token. Null QString if not authenticated.
 
QString version () const
 
bool online () const
 
EventBuseventbus () const
 
WebSocketwebsocket () const
 
ViewModel::Settingssettings () const
 
void setDefaultErrorHandler (QNetworkReply *rep)
 Sets the error handler of a reply to this classes default error handler.
 

Protected Slots

void defaultNetworkErrorHandler (QNetworkReply::NetworkError error)
 
void onUserDataChanged (const QString &itemId, UserData *newData)
 
void credManagerServersListed (QStringList users)
 
void credManagerUsersListed (const QString &server, QStringList users)
 
void credManagerTokenRetrieved (const QString &server, const QString &user, const QString &token)
 
void classBegin () override
 
void componentComplete () override
 

Protected Member Functions

void addBaseRequestHeaders (QNetworkRequest &request, const QString &path, const QUrlQuery &params=QUrlQuery()) const
 Adds default headers to each request, like authentication headers etc.
 
void addTokenHeader (QNetworkRequest &request) const
 Adds the authorization to the header.
 
void getBrandingConfiguration ()
 getBrandingConfiguration Gets the login message and custom CSS (which we ignore)
 
void generateDeviceProfile ()
 Generates a profile, containing the name of the application, manufacturer and most importantly, which media types this device supports.
 

Properties

QString baseUrl
 
QString appName
 
Jellyfin::Model::DeviceTypeClass::Value deviceType
 
bool authenticated
 
QString userId
 
QJsonObject deviceProfile
 
QString version
 
Jellyfin::EventBuseventbus
 
Jellyfin::WebSocketwebsocket
 
QVariantList supportedCommands
 
Jellyfin::ViewModel::Settingssettings
 
bool online
 

Friends

class WebSocket
 
class PlaybackManager
 

Detailed Description

An Api client for Jellyfin. Handles requests and authentication.

This class should also be given to certain models and other sources, so they are able to make requests to the correct server.

General usage is as follows:

  1. (Optional) Call restoreSavedSession(). This will try to load previously saved credentials and connect to the server. If all succeeds, the property authenticated should be set to true and its signal should be emitted. All is done. If it fails, setupRequired will be emitted. Continue following these steps.
  2. If opting in to manually manage the session or restoreSavedSession() failed, you'll need to set the property baseUrl to the root of the Jellyfin server, e.g. "https://jellyfin.example.com:8098", so not the url to the web interface! Nearby servers can be discovered using Jellyfin::ServerDiscoveryModel.
  3. Call setupConnection(). First of all, the client will try to resolve any redirects and will update the baseUrl property if following redirects. Then it will emit connectionSuccess(QString). The QString from the signal contains a user-oriented login message configured by the user that should be displayed in the URL somewhere.
  4. After connected is emitted, call authenticate(QString, QString, bool). with the username and password. The last boolean argument is used if you want to have the ApiClient store your credentials, so that they later can be used with restoreSavedSession().
  5. If the authenticated property is set to true, you are now authenticated! If loginError() is emitted, you aren't and you should go back to step 4.

These steps might change. I'm considering decoupling CredentialsManager from this class to clean some code up.

Member Enumeration Documentation

◆ ApiError

Enumerator
JSON_ERROR 
UNEXPECTED_REPLY 
UNEXPECTED_STATUS 
INVALID_PASSWORD 

Constructor & Destructor Documentation

◆ ApiClient()

Jellyfin::ApiClient::ApiClient ( QObject * parent = nullptr)
explicit

◆ ~ApiClient()

Jellyfin::ApiClient::~ApiClient ( )
virtual

Member Function Documentation

◆ addBaseRequestHeaders()

void Jellyfin::ApiClient::addBaseRequestHeaders ( QNetworkRequest & request,
const QString & path,
const QUrlQuery & params = QUrlQuery() ) const
protected

Adds default headers to each request, like authentication headers etc.

Parameters
requestThe request to add headers to
pathThe path to which the request is being made

◆ addTokenHeader()

void Jellyfin::ApiClient::addTokenHeader ( QNetworkRequest & request) const
protected

Adds the authorization to the header.

Parameters
Therequest to add the header to

◆ appName()

const QString & Jellyfin::ApiClient::appName ( ) const

◆ appNameChanged

void Jellyfin::ApiClient::appNameChanged ( const QString & newAppName)
signal

◆ authenticate

void Jellyfin::ApiClient::authenticate ( QString username,
QString password,
bool storeCredentials = false )
slot

◆ authenticated()

bool Jellyfin::ApiClient::authenticated ( ) const

◆ authenticatedChanged

void Jellyfin::ApiClient::authenticatedChanged ( bool authenticated)
signal

◆ authenticationError

void Jellyfin::ApiClient::authenticationError ( ApiError error)
signal

◆ authenticationRequired

void Jellyfin::ApiClient::authenticationRequired ( )
signal

◆ baseUrl()

const QString & Jellyfin::ApiClient::baseUrl ( ) const

◆ baseUrlChanged

void Jellyfin::ApiClient::baseUrlChanged ( const QString & baseUrl)
signal

◆ classBegin

void Jellyfin::ApiClient::classBegin ( )
overrideprotectedslot

◆ clientCapabilities()

const QJsonObject Jellyfin::ApiClient::clientCapabilities ( ) const

◆ componentComplete

void Jellyfin::ApiClient::componentComplete ( )
overrideprotectedslot

◆ connectionFailed

void Jellyfin::ApiClient::connectionFailed ( ApiError error)
signal

◆ connectionSuccess

void Jellyfin::ApiClient::connectionSuccess ( QString loginMessage)
signal

◆ credManagerServersListed

void Jellyfin::ApiClient::credManagerServersListed ( QStringList users)
protectedslot

◆ credManagerTokenRetrieved

void Jellyfin::ApiClient::credManagerTokenRetrieved ( const QString & server,
const QString & user,
const QString & token )
protectedslot

◆ credManagerUsersListed

void Jellyfin::ApiClient::credManagerUsersListed ( const QString & server,
QStringList users )
protectedslot

◆ defaultNetworkErrorHandler

void Jellyfin::ApiClient::defaultNetworkErrorHandler ( QNetworkReply::NetworkError error)
protectedslot

◆ deleteSession

void Jellyfin::ApiClient::deleteSession ( )
slot

Logs the user out and clears the session.

◆ deviceId()

const QString & Jellyfin::ApiClient::deviceId ( ) const

◆ deviceProfile()

QSharedPointer< DTO::DeviceProfile > Jellyfin::ApiClient::deviceProfile ( ) const

◆ deviceProfileChanged

void Jellyfin::ApiClient::deviceProfileChanged ( )
signal

◆ deviceProfileJson()

const QJsonObject Jellyfin::ApiClient::deviceProfileJson ( ) const

◆ deviceType()

Model::DeviceType Jellyfin::ApiClient::deviceType ( ) const

◆ deviceTypeChanged

void Jellyfin::ApiClient::deviceTypeChanged ( )
signal

◆ downloadUrl

QString Jellyfin::ApiClient::downloadUrl ( const QString & itemId) const
slot

◆ eventbus()

EventBus * Jellyfin::ApiClient::eventbus ( ) const

◆ generateDeviceProfile()

void Jellyfin::ApiClient::generateDeviceProfile ( )
protected

Generates a profile, containing the name of the application, manufacturer and most importantly, which media types this device supports.

The actual detection of supported media types is done within jellyfindeviceprofile.cpp, since the code is a big mess and should be safely contained in it's own file.

◆ get()

QNetworkReply * Jellyfin::ApiClient::get ( const QString & path,
const QUrlQuery & params = QUrlQuery() )

◆ getBrandingConfiguration()

void Jellyfin::ApiClient::getBrandingConfiguration ( )
protected

getBrandingConfiguration Gets the login message and custom CSS (which we ignore)

◆ networkError

void Jellyfin::ApiClient::networkError ( QNetworkReply::NetworkError error)
signal

◆ online()

bool Jellyfin::ApiClient::online ( ) const

◆ onlineChanged

void Jellyfin::ApiClient::onlineChanged ( )
signal

◆ onUserDataChanged

void Jellyfin::ApiClient::onUserDataChanged ( const QString & itemId,
UserData * newData )
protectedslot

◆ post() [1/2]

QNetworkReply * Jellyfin::ApiClient::post ( const QString & path,
const QByteArray & data = QByteArray(),
const QUrlQuery & params = QUrlQuery() )

◆ post() [2/2]

QNetworkReply * Jellyfin::ApiClient::post ( const QString & path,
const QJsonDocument & data,
const QUrlQuery & params = QUrlQuery() )

◆ postCapabilities

void Jellyfin::ApiClient::postCapabilities ( )
slot

Shares the capabilities of this device to the server.

◆ quickConnectAccepted

void Jellyfin::ApiClient::quickConnectAccepted ( )
signal

Emitted after submitQuickConnectCode succeeded.

◆ quickConnectRejected

void Jellyfin::ApiClient::quickConnectRejected ( )
signal

Emitted after submitQuickConnectCode failed.

◆ restoreSavedSession

void Jellyfin::ApiClient::restoreSavedSession ( )
slot

Tries to access credentials and connect to a server. If nothing has been configured yet, emits setupRequired();.

◆ setAppName()

void Jellyfin::ApiClient::setAppName ( const QString & appName)

◆ setBaseUrl()

void Jellyfin::ApiClient::setBaseUrl ( const QString & url)

◆ setDefaultErrorHandler()

void Jellyfin::ApiClient::setDefaultErrorHandler ( QNetworkReply * rep)
inline

Sets the error handler of a reply to this classes default error handler.

Parameters
repThe reply to set the error handler on.

Motivation for this helper is because I forget the correct signature each time, with all the funky casts.

◆ setDeviceType()

void Jellyfin::ApiClient::setDeviceType ( Model::DeviceType deviceType)

◆ setSupportedCommands()

void Jellyfin::ApiClient::setSupportedCommands ( QVariantList newSupportedCommands)

◆ settings()

ViewModel::Settings * Jellyfin::ApiClient::settings ( ) const

◆ settingsChanged

void Jellyfin::ApiClient::settingsChanged ( )
signal

◆ setupConnection

void Jellyfin::ApiClient::setupConnection ( )
slot

◆ setupRequired

void Jellyfin::ApiClient::setupRequired ( )
signal

Set-up is required. You'll need to manually set up the baseUrl-property, call setupConnection afterwards and finally call authenticate.

◆ submitQuickConnectCode

void Jellyfin::ApiClient::submitQuickConnectCode ( const QString & code)
slot

◆ supportedCommands()

QVariantList Jellyfin::ApiClient::supportedCommands ( ) const

QML applications can set this type to indicate which commands they support.

These commands can be sent by other Jellyfin clients to instruct this Jellyfin client to play a certain item, control playback and so on.

This property must be set before restoreSavedSession() is called and not be changed afterwards. The list support commands will be sent to the Jellyfin server. QML applications should listen to the events emitted by the eventBus and act accordingly.

◆ supportedCommandsChanged

void Jellyfin::ApiClient::supportedCommandsChanged ( )
signal

◆ token()

const QString & Jellyfin::ApiClient::token ( ) const

Retrieves the authentication token. Null QString if not authenticated.

Note
This is not the full authentication header, just the token.

◆ userDataChanged

void Jellyfin::ApiClient::userDataChanged ( const QString & itemId,
UserData * userData )
signal

onUserDataChanged Emitted when the user data of an item is changed on the server.

Parameters
itemIdThe id of the item being changed
userDataThe new user data

Note: only Jellyfin::UserData should connect to this signal, they will update themselves! Note: the userData is only valid during this callback, afterwards it is deleted!

◆ userId()

const QString & Jellyfin::ApiClient::userId ( ) const

◆ userIdChanged

void Jellyfin::ApiClient::userIdChanged ( QString userId)
signal

◆ version()

QString Jellyfin::ApiClient::version ( ) const

◆ websocket()

WebSocket * Jellyfin::ApiClient::websocket ( ) const

Friends And Related Symbol Documentation

◆ PlaybackManager

friend class PlaybackManager
friend

◆ WebSocket

friend class WebSocket
friend

Property Documentation

◆ appName

QString Jellyfin::ApiClient::appName
readwrite

◆ authenticated

bool Jellyfin::ApiClient::authenticated
readwrite

◆ baseUrl

QString Jellyfin::ApiClient::baseUrl
readwrite

◆ deviceProfile

QJsonObject Jellyfin::ApiClient::deviceProfile
read

◆ deviceType

Jellyfin::Model::DeviceTypeClass::Value Jellyfin::ApiClient::deviceType
readwrite

◆ eventbus

Jellyfin::EventBus * Jellyfin::ApiClient::eventbus
read

◆ online

bool Jellyfin::ApiClient::online
read

Wether this ApiClient operates in "online mode".

When operating in offline mode, this client will not make network requests and only use a local cache, making certain features unavailable.

◆ settings

Jellyfin::ViewModel::Settings * Jellyfin::ApiClient::settings
read

◆ supportedCommands

QVariantList Jellyfin::ApiClient::supportedCommands
readwrite

◆ userId

QString Jellyfin::ApiClient::userId
read

◆ version

QString Jellyfin::ApiClient::version
read

◆ websocket

Jellyfin::WebSocket * Jellyfin::ApiClient::websocket
read

The documentation for this class was generated from the following files: