File indexing completed on 2025-02-23 04:35:15

0001 // SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
0002 // SPDX-License-Identifier: GPL-3.0-or-later
0003 
0004 #pragma once
0005 
0006 #include "preferences.h"
0007 #include "sourceconfig.h"
0008 
0009 #include <QDir>
0010 #include <QObject>
0011 #include <QtQml>
0012 
0013 namespace QInvidious
0014 {
0015 class AbstractApi;
0016 }
0017 
0018 class VideoSource : public QObject
0019 {
0020     Q_OBJECT
0021     QML_ELEMENT
0022     QML_UNCREATABLE("Use from SourceManager")
0023 
0024     Q_PROPERTY(QString url READ url WRITE setUrl NOTIFY urlChanged)
0025     Q_PROPERTY(Type type READ type WRITE setType NOTIFY typeChanged)
0026     Q_PROPERTY(bool loggedIn READ loggedIn NOTIFY credentialsChanged)
0027     Q_PROPERTY(QString username READ username NOTIFY usernameChanged)
0028     Q_PROPERTY(QInvidious::Preferences preferences READ preferences WRITE setPreferences NOTIFY preferencesChanged)
0029 
0030 public:
0031     explicit VideoSource(const QString &key, QObject *parent = nullptr);
0032 
0033     enum class Type { Invidious, PeerTube, Piped };
0034     Q_ENUM(Type)
0035 
0036     [[nodiscard]] QString uuid() const;
0037 
0038     [[nodiscard]] QString url() const;
0039     void setUrl(const QString &url);
0040 
0041     [[nodiscard]] Type type() const;
0042     void setType(Type type);
0043 
0044     bool loggedIn() const;
0045     Q_INVOKABLE void logOut();
0046 
0047     void setUsername(const QString &username);
0048     QString username() const;
0049 
0050     void setCookie(const QString &cookie);
0051     QString cookie() const;
0052 
0053     QInvidious::Preferences preferences();
0054     void setPreferences(const QInvidious::Preferences &preferences);
0055 
0056     Q_INVOKABLE bool isVideoWatched(const QString &videoId);
0057     Q_INVOKABLE void markVideoWatched(const QString &videoId);
0058     void markVideoUnwatched(const QString &videoId);
0059 
0060     std::optional<bool> isSubscribedToChannel(const QString &jid) const;
0061 
0062     Q_INVOKABLE void addToPlaylist(const QString &plid, const QString &videoId);
0063 
0064     bool hasFinishedLoading() const;
0065 
0066     QInvidious::AbstractApi *api() const;
0067 
0068     Q_INVOKABLE bool supportsPopularPage() const;
0069     Q_INVOKABLE bool supportsTrendingCategories() const;
0070 
0071 Q_SIGNALS:
0072     void urlChanged();
0073     void typeChanged();
0074     void credentialsChanged();
0075     void usernameChanged();
0076     void preferencesChanged();
0077     void finishedLoading();
0078     void subscriptionsChanged();
0079 
0080 private:
0081     friend class SubscriptionController;
0082 
0083     void createApi();
0084     void setApiCookie();
0085     QString cookieKey();
0086     void fetchPreferences();
0087     void fetchSubscriptions();
0088     void fetchHistory(qint32 page = 1);
0089     void setSubscriptions(const QList<QString> &subscriptions);
0090     std::optional<QList<QString>> &subscriptions();
0091 
0092     SourceConfig m_config;
0093     QString m_key;
0094     QInvidious::AbstractApi *m_api = nullptr;
0095     QString m_cookie;
0096     QInvidious::Preferences m_preferences;
0097     std::optional<QList<QString>> m_subscriptions;
0098     QList<QString> m_watchedVideos;
0099     bool m_finishedLoading = false;
0100 };