1 #pragma once
2 
3 #include "common/ChatterinoSetting.hpp"
4 #include "common/SignalVector.hpp"
5 #include "providers/twitch/TwitchAccount.hpp"
6 #include "util/SharedPtrElementLess.hpp"
7 
8 #include <mutex>
9 #include <vector>
10 
11 //
12 // Warning: This class is not supposed to be created directly.
13 // 			Get yourself an instance from our friends over at
14 // AccountManager.hpp
15 //
16 
17 namespace chatterino {
18 
19 class TwitchAccount;
20 class AccountController;
21 
22 class TwitchAccountManager
23 {
24     TwitchAccountManager();
25 
26 public:
27     struct UserData {
28         QString username;
29         QString userID;
30         QString clientID;
31         QString oauthToken;
32     };
33 
34     // Returns the current twitchUsers, or the anonymous user if we're not
35     // currently logged in
36     std::shared_ptr<TwitchAccount> getCurrent();
37 
38     std::vector<QString> getUsernames() const;
39 
40     std::shared_ptr<TwitchAccount> findUserByUsername(
41         const QString &username) const;
42     bool userExists(const QString &username) const;
43 
44     void reloadUsers();
45     void load();
46 
47     bool isLoggedIn() const;
48 
49     pajlada::Settings::Setting<QString> currentUsername{"/accounts/current",
50                                                         ""};
51     pajlada::Signals::NoArgSignal currentUserChanged;
52     pajlada::Signals::NoArgSignal userListUpdated;
53 
54     SignalVector<std::shared_ptr<TwitchAccount>> accounts;
55 
56 private:
57     enum class AddUserResponse {
58         UserAlreadyExists,
59         UserValuesUpdated,
60         UserAdded,
61     };
62     AddUserResponse addUser(const UserData &data);
63     bool removeUser(TwitchAccount *account);
64 
65     std::shared_ptr<TwitchAccount> currentUser_;
66 
67     std::shared_ptr<TwitchAccount> anonymousUser_;
68     mutable std::mutex mutex_;
69 
70     friend class AccountController;
71 };
72 
73 }  // namespace chatterino
74