1 /* 2 * Copyright (C) 2001-2012 Jacek Sieka, arnetheduck on gmail point com 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 17 */ 18 19 #pragma once 20 21 #include "SettingsManager.h" 22 #include "CriticalSection.h" 23 #include "HttpConnection.h" 24 #include "User.h" 25 #include "UserCommand.h" 26 #include "FavoriteUser.h" 27 #include "Singleton.h" 28 #include "ClientManagerListener.h" 29 #include "FavoriteManagerListener.h" 30 #include "HubEntry.h" 31 #include "FavHubGroup.h" 32 33 namespace dcpp { 34 35 class SimpleXML; 36 37 /** 38 * Public hub list, favorites (hub&user). Assumed to be called only by UI thread. 39 */ 40 class FavoriteManager : public Speaker<FavoriteManagerListener>, private HttpConnectionListener, public Singleton<FavoriteManager>, 41 private SettingsManagerListener, private ClientManagerListener 42 { 43 public: 44 // Public Hubs 45 enum HubTypes { 46 TYPE_NORMAL, 47 TYPE_BZIP2 48 }; 49 StringList getHubLists(); 50 void setHubList(int aHubList); getSelectedHubList()51 int getSelectedHubList() { return lastServer; } 52 void refresh(bool forceDownload = false); getHubListType()53 HubTypes getHubListType() { return listType; } getPublicHubs()54 HubEntryList getPublicHubs() { 55 Lock l(cs); 56 return publicListMatrix[publicListServer]; 57 } isDownloading()58 bool isDownloading() { return (useHttp && running); } getCurrentHubList()59 const string& getCurrentHubList() const { return publicListServer; } 60 61 // Favorite Users 62 typedef unordered_map<CID, FavoriteUser> FavoriteMap; getFavoriteUsers()63 FavoriteMap getFavoriteUsers() { Lock l(cs); return users; } 64 65 void addFavoriteUser(const UserPtr& aUser); isFavoriteUser(const UserPtr & aUser)66 bool isFavoriteUser(const UserPtr& aUser) const { Lock l(cs); return users.find(aUser->getCID()) != users.end(); } 67 void removeFavoriteUser(const UserPtr& aUser); 68 69 bool hasSlot(const UserPtr& aUser) const; 70 void setUserDescription(const UserPtr& aUser, const string& description); 71 void setAutoGrant(const UserPtr& aUser, bool grant); 72 void userUpdated(const OnlineUser& info); 73 time_t getLastSeen(const UserPtr& aUser) const; 74 std::string getUserURL(const UserPtr& aUser) const; 75 76 // Favorite Hubs getFavoriteHubs()77 const FavoriteHubEntryList& getFavoriteHubs() const { return favoriteHubs; } getFavoriteHubs()78 FavoriteHubEntryList& getFavoriteHubs() { return favoriteHubs; } 79 80 void addFavorite(const FavoriteHubEntry& aEntry); 81 void removeFavorite(FavoriteHubEntry* entry); 82 bool isFavoriteHub(const std::string& aUrl); 83 FavoriteHubEntryPtr getFavoriteHubEntry(const string& aServer) const; 84 85 // Favorite hub groups getFavHubGroups()86 const FavHubGroups& getFavHubGroups() const { return favHubGroups; } setFavHubGroups(const FavHubGroups & favHubGroups_)87 void setFavHubGroups(const FavHubGroups& favHubGroups_) { favHubGroups = favHubGroups_; } 88 89 FavoriteHubEntryList getFavoriteHubs(const string& group) const; 90 bool isPrivate(const string& url) const; 91 92 // Favorite Directories 93 bool addFavoriteDir(const string& aDirectory, const string& aName); 94 bool removeFavoriteDir(const string& aName); 95 bool renameFavoriteDir(const string& aName, const string& anotherName); getFavoriteDirs()96 StringPairList getFavoriteDirs() { return favoriteDirs; } 97 98 // User Commands 99 UserCommand addUserCommand(int type, int ctx, int flags, const string& name, const string& command, const string& to, const string& hub); 100 bool getUserCommand(int cid, UserCommand& uc); 101 int findUserCommand(const string& aName, const string& aUrl); 102 bool moveUserCommand(int cid, int pos); 103 void updateUserCommand(const UserCommand& uc); 104 void removeUserCommand(int cid); 105 void removeUserCommand(const string& srv); 106 void removeHubUserCommands(int ctx, const string& hub); 107 getUserCommands()108 UserCommand::List getUserCommands() { Lock l(cs); return userCommands; } 109 UserCommand::List getUserCommands(int ctx, const StringList& hub); 110 111 void load(); 112 void save(); 113 114 private: 115 FavoriteHubEntryList favoriteHubs; 116 FavHubGroups favHubGroups; 117 StringPairList favoriteDirs; 118 UserCommand::List userCommands; 119 int lastId; 120 121 FavoriteMap users; 122 123 mutable CriticalSection cs; 124 125 // Public Hubs 126 typedef unordered_map<string, HubEntryList> PubListMap; 127 PubListMap publicListMatrix; 128 string publicListServer; 129 bool useHttp, running; 130 HttpConnection* c; 131 int lastServer; 132 HubTypes listType; 133 string downloadBuf; 134 135 /** Used during loading to prevent saving. */ 136 bool dontSave; 137 138 friend class Singleton<FavoriteManager>; 139 140 FavoriteManager(); 141 virtual ~FavoriteManager(); 142 143 FavoriteHubEntryList::iterator getFavoriteHub(const string& aServer); 144 145 // ClientManagerListener 146 virtual void on(UserUpdated, const OnlineUser& user) noexcept; 147 virtual void on(UserConnected, const UserPtr& user) noexcept; 148 virtual void on(UserDisconnected, const UserPtr& user) noexcept; 149 150 // HttpConnectionListener 151 virtual void on(Data, HttpConnection*, const uint8_t*, size_t) noexcept; 152 virtual void on(Failed, HttpConnection*, const string&) noexcept; 153 virtual void on(Complete, HttpConnection*, const string&, bool) noexcept; 154 virtual void on(Redirected, HttpConnection*, const string&) noexcept; 155 virtual void on(TypeNormal, HttpConnection*) noexcept; 156 virtual void on(TypeBZ2, HttpConnection*) noexcept; 157 virtual void on(Retried, HttpConnection*, const bool) noexcept; 158 159 bool onHttpFinished(bool fromHttp) noexcept; 160 161 // SettingsManagerListener on(SettingsManagerListener::Load,SimpleXML & xml)162 virtual void on(SettingsManagerListener::Load, SimpleXML& xml) noexcept { 163 load(xml); 164 } 165 166 void load(SimpleXML& aXml); 167 168 string getConfigFile(); 169 }; 170 171 } // namespace dcpp 172