Jellyfin Qt
QML Library for interacting with the Jellyfin multimedia server
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
serverdiscoverymodel.h
Go to the documentation of this file.
1/*
2Sailfin: a Jellyfin client written using Qt
3Copyright (C) 2021 Chris Josten
4
5This library is free software; you can redistribute it and/or
6modify it under the terms of the GNU Lesser General Public
7License as published by the Free Software Foundation; either
8version 2.1 of the License, or (at your option) any later version.
9
10This library is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13Lesser General Public License for more details.
14
15You should have received a copy of the GNU Lesser General Public
16License along with this library; if not, write to the Free Software
17Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19
20#ifndef SERVER_DISCOVERY_MODEL_H
21#define SERVER_DISCOVERY_MODEL_H
22
23#include <vector>
24
25#include <QAbstractListModel>
26#include <QHash>
27#include <QJsonDocument>
28#include <QJsonObject>
29#include <QJsonParseError>
30
31#include <QHostAddress>
32#include <QUdpSocket>
33
34namespace Jellyfin {
36 QString name;
37 QString address;
38 QString id;
39};
40
44class ServerDiscoveryModel : public QAbstractListModel {
45 Q_OBJECT
46public:
47 enum Roles {
48 ROLE_NAME = Qt::UserRole + 1,
51 };
52 explicit ServerDiscoveryModel(QObject *parent = nullptr);
53
54 QHash<int, QByteArray> roleNames() const override {
55 return {
56 {ROLE_NAME, "name"},
57 {ROLE_ADDRESS, "address"},
58 {ROLE_ID, "id"}
59 };
60 }
61
62 int rowCount(const QModelIndex &parent = QModelIndex()) const override {
63 if (parent.isValid()) return 0;
64 return static_cast<int>(m_discoveredServers.size());
65 }
66
67 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
68public slots:
72 void refresh();
73private slots:
74 void on_datagramsAvailable();
75private:
76 const QByteArray MAGIC_PACKET = "who is JellyfinServer?";
77 const quint16 BROADCAST_PORT = 7359;
78
79 std::vector<ServerDiscovery> m_discoveredServers;
80 QUdpSocket m_socket;
81};
82}
83#endif //SERVER_DISCOVERY_MODEL_H
Discovers nearby Jellyfin servers and puts them in this list.
Definition serverdiscoverymodel.h:44
Roles
Definition serverdiscoverymodel.h:47
@ ROLE_ID
Definition serverdiscoverymodel.h:50
@ ROLE_NAME
Definition serverdiscoverymodel.h:48
@ ROLE_ADDRESS
Definition serverdiscoverymodel.h:49
void refresh()
Refreshes the model and searches for new servers.
Definition serverdiscoverymodel.cpp:44
ServerDiscoveryModel(QObject *parent=nullptr)
Definition serverdiscoverymodel.cpp:22
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Definition serverdiscoverymodel.cpp:28
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Definition serverdiscoverymodel.h:62
QHash< int, QByteArray > roleNames() const override
Definition serverdiscoverymodel.h:54
Definition serverdiscoverymodel.h:35
QString address
Definition serverdiscoverymodel.h:37
QString id
Definition serverdiscoverymodel.h:38
QString name
Definition serverdiscoverymodel.h:36