1 /*
2  * Copyright (C) 2001-2008 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18 
19 #ifndef DCPLUSPLUS_DCPP_FAVORITE_MANAGER_H
20 #define DCPLUSPLUS_DCPP_FAVORITE_MANAGER_H
21 
22 #include "SettingsManager.h"
23 
24 #include "CriticalSection.h"
25 #include "HttpConnection.h"
26 #include "User.h"
27 #include "UserCommand.h"
28 #include "FavoriteUser.h"
29 #include "Singleton.h"
30 #include "ClientManagerListener.h"
31 #include "FavoriteManagerListener.h"
32 #include "HubEntry.h"
33 
34 namespace dcpp {
35 
36 class SimpleXML;
37 
38 /**
39  * Public hub list, favorites (hub&user). Assumed to be called only by UI thread.
40  */
41 class FavoriteManager : public Speaker<FavoriteManagerListener>, private HttpConnectionListener, public Singleton<FavoriteManager>,
42 	private SettingsManagerListener, private ClientManagerListener
43 {
44 public:
45 // Public Hubs
46 	enum HubTypes {
47 		TYPE_NORMAL,
48 		TYPE_BZIP2
49 	};
50 	StringList getHubLists();
51 	void setHubList(int aHubList);
getSelectedHubList()52 	int getSelectedHubList() { return lastServer; }
53 	void refresh(bool forceDownload = false);
getHubListType()54 	HubTypes getHubListType() { return listType; }
getPublicHubs()55 	HubEntryList getPublicHubs() {
56 		Lock l(cs);
57 		return publicListMatrix[publicListServer];
58 	}
isDownloading()59 	bool isDownloading() { return (useHttp && running); }
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 	FavoriteHubEntryList& getFavoriteHubs() { return favoriteHubs; }
78 
79 	void addFavorite(const FavoriteHubEntry& aEntry);
80 	void removeFavorite(FavoriteHubEntry* entry);
81 	bool isFavoriteHub(const std::string& aUrl);
82 	FavoriteHubEntryPtr getFavoriteHubEntry(const string& aUrl);
83 
84 // Favorite Directories
85 	bool addFavoriteDir(const string& aDirectory, const string& aName);
86 	bool removeFavoriteDir(const string& aName);
87 	bool renameFavoriteDir(const string& aName, const string& anotherName);
getFavoriteDirs()88 	StringPairList getFavoriteDirs() { return favoriteDirs; }
89 
90 // User Commands
91 	UserCommand addUserCommand(int type, int ctx, int flags, const string& name, const string& command, const string& hub);
92 	bool getUserCommand(int cid, UserCommand& uc);
93 	int findUserCommand(const string& aName, const string& aUrl);
94 	bool moveUserCommand(int cid, int pos);
95 	void updateUserCommand(const UserCommand& uc);
96 	void removeUserCommand(int cid);
97 	void removeUserCommand(const string& srv);
98 	void removeHubUserCommands(int ctx, const string& hub);
99 
getUserCommands()100 	UserCommand::List getUserCommands() { Lock l(cs); return userCommands; }
101 	UserCommand::List getUserCommands(int ctx, const StringList& hub);
102 
103 	void load();
104 	void save();
105 
106 private:
107 	FavoriteHubEntryList favoriteHubs;
108 	StringPairList favoriteDirs;
109 	UserCommand::List userCommands;
110 	int lastId;
111 
112 	FavoriteMap users;
113 
114 	mutable CriticalSection cs;
115 
116 	// Public Hubs
117 	typedef unordered_map<string, HubEntryList> PubListMap;
118 	PubListMap publicListMatrix;
119 	string publicListServer;
120 	bool useHttp, running;
121 	HttpConnection* c;
122 	int lastServer;
123 	HubTypes listType;
124 	string downloadBuf;
125 
126 	/** Used during loading to prevent saving. */
127 	bool dontSave;
128 
129 	friend class Singleton<FavoriteManager>;
130 
131 	FavoriteManager();
132 	virtual ~FavoriteManager() throw();
133 
134 	FavoriteHubEntryList::iterator getFavoriteHub(const string& aServer);
135 	void loadXmlList(const string& xml);
136 
137 	// ClientManagerListener
138 	virtual void on(UserUpdated, const OnlineUser& user) throw();
139 	virtual void on(UserConnected, const UserPtr& user) throw();
140 	virtual void on(UserDisconnected, const UserPtr& user) throw();
141 
142 	// HttpConnectionListener
143 	virtual void on(Data, HttpConnection*, const uint8_t*, size_t) throw();
144 	virtual void on(Failed, HttpConnection*, const string&) throw();
145 	virtual void on(Complete, HttpConnection*, const string&) throw();
146 	virtual void on(Redirected, HttpConnection*, const string&) throw();
147 	virtual void on(TypeNormal, HttpConnection*) throw();
148 	virtual void on(TypeBZ2, HttpConnection*) throw();
149 
150 	void onHttpFinished(bool fromHttp) throw();
151 
152 	// SettingsManagerListener
on(SettingsManagerListener::Load,SimpleXML & xml)153 	virtual void on(SettingsManagerListener::Load, SimpleXML& xml) throw() {
154 		load(xml);
155 	}
156 
157 	void load(SimpleXML& aXml);
158 
getConfigFile()159 	string getConfigFile() { return Util::getPath(Util::PATH_USER_CONFIG) + "Favorites.xml"; }
160 };
161 
162 } // namespace dcpp
163 
164 #endif // !defined(FAVORITE_MANAGER_H)
165