1 /*
2  * Copyright © 2015-2016 Antti Lamminsalo
3  *
4  * This file is part of Orion.
5  *
6  * Orion is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with Orion.  If not, see <http://www.gnu.org/licenses/>.
13  */
14 
15 #pragma once
16 
17 #include "channel.h"
18 #include "channellistmodel.h"
19 #include "gamelistmodel.h"
20 #include "game.h"
21 #include "../network/networkmanager.h"
22 #include "settingsmanager.h"
23 
24 #include <QSettings>
25 #include <QSortFilterProxyModel>
26 
27 #include "singletonprovider.h"
28 
29 #define DEFAULT_LOGO_URL    "http://static-cdn.jtvnw.net/jtv_user_pictures/xarth/404_user_150x150.png"
30 
31 class ChannelManager: public QObject
32 {
33     QML_SINGLETON
34     Q_OBJECT
35 
36     NetworkManager* netman;
37     SettingsManager *settingsManager;
38 
39     ChannelListModel* tempFavourites;
40     ChannelListModel* favouritesModel;
41     QSortFilterProxyModel* favouritesProxy;
42     ChannelListModel* resultsModel;
43 
44     //Games (and game search results)
45     GameListModel* gamesModel;
46 
47     //Oauth
48     QString user_name;
49     quint64 user_id;
50 
51     /**
52      * @brief createFollowedChannelsModel
53      * Creates ready-to-use followed channels model
54      * @return
55      */
56     ChannelListModel *createFollowedChannelsModel();
isAccessTokenAvailable()57     bool isAccessTokenAvailable() { return settingsManager->hasAccessToken(); }
58 
59     ChannelManager();
60 
61 public:
62     static ChannelManager *getInstance();
63 
64     ~ChannelManager();
65 
66     void load();
67     void save();
68 
69     Q_INVOKABLE bool containsFavourite(const quint32&);
70 
71     void checkStreams(const QList<Channel*>&);
72 
73     ChannelListModel *getFavouritesModel() const;
74 
75     QSortFilterProxyModel *getFavouritesProxy() const;
76 
77     ChannelListModel *getResultsModel() const;
78     GameListModel *getGamesModel() const;
79 
80     quint64 getUser_id() const;
81 
82 signals:
83     void pushNotification(const QString &title, const QString &message, const QString &imgUrl);
84     void resultsUpdated(int numAdded, int total);
85     void searchingStarted();
86     void foundPlaybackStream(const QVariantMap &streams);
87     void deletedChannel(const quint32 &chanid);
88     void addedChannel(const quint32 &chanid);
89     void gamesSearchStarted();
90     void gamesUpdated();
91     void followedUpdated();
92 
93     //oauth methods
94     void accessTokenUpdated();
95     void userNameUpdated(const QString name);
96     void login(const QString &username, const QString &password);
97 
98 public slots:
99     void checkFavourites();
100     void addToFavourites(const quint32&);
101     void removeFromFavourites(const quint32&);
102     void searchChannels(QString, const quint32&, const quint32&, bool);
103     void notify(Channel*);
104     void notifyMultipleChannelsOnline(const QList<Channel*> &);
105     void findPlaybackStream(const QString&);
106     void addToFavourites(const quint32 &id, const QString &serviceName, const QString &title,
107                                      const  QString &info, const QString &logo, const QString &preview,
108                                      const QString& game, const qint32 &viewers, bool online);
109     void getFollowedChannels(const quint32 &limit = FOLLOWED_FETCH_LIMIT, const quint32 &offset = 0);
110     void searchGames(QString, const quint32&, const quint32&);
111     QString username() const;
112 
113 private slots:
114     void addSearchResults(const QList<Channel*>&, const int total);
115     void updateFavourites(const QList<Channel*>&);
116     void updateStreams(const QList<Channel*>&);
117     void addGames(const QList<Game*>&);
118     void onUserUpdated(const QString &name, const quint64 userId);
119     void addFollowedResults(const QList<Channel*>&, const quint32, const quint32);
120     void slotNetworkAccessChanged(bool);
121     void updateAccessToken(QString accessToken);
122 
123     // QObject interface
124 protected:
125     void timerEvent(QTimerEvent *event);
126 };
127