📄 favoritemanager.cpp
字号:
/*
* Copyright (C) 2001-2006 Jacek Sieka, arnetheduck on gmail point com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "stdinc.h"
#include "DCPlusPlus.h"
#include "FavoriteManager.h"
#include "ClientManager.h"
#include "ResourceManager.h"
#include "CryptoManager.h"
#include "HttpConnection.h"
#include "StringTokenizer.h"
#include "SimpleXML.h"
#include "UserCommand.h"
UserCommand FavoriteManager::addUserCommand(int type, int ctx, int flags, const string& name, const string& command, const string& hub) {
// No dupes, add it...
Lock l(cs);
userCommands.push_back(UserCommand(lastId++, type, ctx, flags, name, command, hub));
UserCommand& uc = userCommands.back();
if(!uc.isSet(UserCommand::FLAG_NOSAVE))
save();
return userCommands.back();
}
bool FavoriteManager::getUserCommand(int cid, UserCommand& uc) {
Lock l(cs);
for(UserCommand::Iter i = userCommands.begin(); i != userCommands.end(); ++i) {
if(i->getId() == cid) {
uc = *i;
return true;
}
}
return false;
}
bool FavoriteManager::moveUserCommand(int cid, int pos) {
dcassert(pos == -1 || pos == 1);
Lock l(cs);
for(UserCommand::Iter i = userCommands.begin(); i != userCommands.end(); ++i) {
if(i->getId() == cid) {
swap(*i, *(i + pos));
return true;
}
}
return false;
}
void FavoriteManager::updateUserCommand(const UserCommand& uc) {
bool nosave = true;
Lock l(cs);
for(UserCommand::Iter i = userCommands.begin(); i != userCommands.end(); ++i) {
if(i->getId() == uc.getId()) {
*i = uc;
nosave = uc.isSet(UserCommand::FLAG_NOSAVE);
break;
}
}
if(!nosave)
save();
}
int FavoriteManager::findUserCommand(const string& aName) {
Lock l(cs);
for(UserCommand::Iter i = userCommands.begin(); i != userCommands.end(); ++i) {
if(i->getName() == aName) {
return i->getId();
}
}
return -1;
}
void FavoriteManager::removeUserCommand(int cid) {
bool nosave = true;
Lock l(cs);
for(UserCommand::Iter i = userCommands.begin(); i != userCommands.end(); ++i) {
if(i->getId() == cid) {
nosave = i->isSet(UserCommand::FLAG_NOSAVE);
userCommands.erase(i);
break;
}
}
if(!nosave)
save();
}
void FavoriteManager::removeUserCommand(const string& srv) {
Lock l(cs);
for(UserCommand::Iter i = userCommands.begin(); i != userCommands.end(); ) {
if((i->getHub() == srv) && i->isSet(UserCommand::FLAG_NOSAVE)) {
i = userCommands.erase(i);
} else {
++i;
}
}
}
void FavoriteManager::removeHubUserCommands(int ctx, const string& hub) {
Lock l(cs);
for(UserCommand::Iter i = userCommands.begin(); i != userCommands.end(); ) {
if(i->getHub() == hub && i->isSet(UserCommand::FLAG_NOSAVE) && i->getCtx() & ctx) {
i = userCommands.erase(i);
} else {
++i;
}
}
}
void FavoriteManager::addFavoriteUser(User::Ptr& aUser) {
Lock l(cs);
if(users.find(aUser->getCID()) == users.end()) {
aUser->setFlag(User::SAVE_NICK);
StringList urls = ClientManager::getInstance()->getHubs(aUser->getCID());
StringList nicks = ClientManager::getInstance()->getNicks(aUser->getCID());
/// @todo make this an error probably...
if(urls.empty())
urls.push_back(Util::emptyString);
if(nicks.empty())
nicks.push_back(Util::emptyString);
FavoriteMap::iterator i = users.insert(make_pair(aUser->getCID(), FavoriteUser(aUser, nicks[0], urls[0]))).first;
fire(FavoriteManagerListener::UserAdded(), i->second);
save();
}
}
void FavoriteManager::removeFavoriteUser(User::Ptr& aUser) {
Lock l(cs);
FavoriteMap::iterator i = users.find(aUser->getCID());
if(i != users.end()) {
fire(FavoriteManagerListener::UserRemoved(), i->second);
users.erase(i);
save();
}
}
void FavoriteManager::addFavorite(const FavoriteHubEntry& aEntry) {
FavoriteHubEntry* f;
FavoriteHubEntry::Iter i = getFavoriteHub(aEntry.getServer());
if(i != favoriteHubs.end()) {
return;
}
f = new FavoriteHubEntry(aEntry);
favoriteHubs.push_back(f);
fire(FavoriteManagerListener::FavoriteAdded(), f);
save();
}
void FavoriteManager::removeFavorite(FavoriteHubEntry* entry) {
FavoriteHubEntry::Iter i = find(favoriteHubs.begin(), favoriteHubs.end(), entry);
if(i == favoriteHubs.end()) {
return;
}
fire(FavoriteManagerListener::FavoriteRemoved(), entry);
favoriteHubs.erase(i);
delete entry;
save();
}
bool FavoriteManager::checkFavHubExists(const FavoriteHubEntry& aEntry){
FavoriteHubEntry::Iter i = getFavoriteHub(aEntry.getServer());
if(i != favoriteHubs.end()) {
return true;
}
return false;
}
bool FavoriteManager::addFavoriteDir(const string& aDirectory, const string & aName){
string path = aDirectory;
if( path[ path.length() -1 ] != PATH_SEPARATOR )
path += PATH_SEPARATOR;
for(StringPairIter i = favoriteDirs.begin(); i != favoriteDirs.end(); ++i) {
if((Util::strnicmp(path, i->first, i->first.length()) == 0) && (Util::strnicmp(path, i->first, path.length()) == 0)) {
return false;
}
if(Util::stricmp(aName, i->second) == 0) {
return false;
}
}
favoriteDirs.push_back(make_pair(aDirectory, aName));
save();
return true;
}
bool FavoriteManager::removeFavoriteDir(const string& aName) {
string d(aName);
if(d[d.length() - 1] != PATH_SEPARATOR)
d += PATH_SEPARATOR;
for(StringPairIter j = favoriteDirs.begin(); j != favoriteDirs.end(); ++j) {
if(Util::stricmp(j->first.c_str(), d.c_str()) == 0) {
favoriteDirs.erase(j);
save();
return true;
}
}
return false;
}
bool FavoriteManager::renameFavoriteDir(const string& aName, const string& anotherName) {
for(StringPairIter j = favoriteDirs.begin(); j != favoriteDirs.end(); ++j) {
if(Util::stricmp(j->second.c_str(), aName.c_str()) == 0) {
j->second = anotherName;
save();
return true;
}
}
return false;
}
void FavoriteManager::onHttpFinished() throw() {
string::size_type i, j;
string* x;
string bzlist;
if(listType == TYPE_BZIP2) {
try {
CryptoManager::getInstance()->decodeBZ2((u_int8_t*)downloadBuf.data(), downloadBuf.size(), bzlist);
} catch(const CryptoException&) {
bzlist.clear();
}
x = &bzlist;
} else {
x = &downloadBuf;
}
{
Lock l(cs);
publicListMatrix[publicListServer].clear();
if(x->compare(0, 5, "<?xml") == 0 || x->compare(0, 8, "\xEF\xBB\xBF<?xml") == 0) {
loadXmlList(*x);
} else {
i = 0;
string utfText = Text::acpToUtf8(*x);
while( (i < utfText.size()) && ((j=utfText.find("\r\n", i)) != string::npos)) {
StringTokenizer<string> tok(utfText.substr(i, j-i), '|');
i = j + 2;
if(tok.getTokens().size() < 4)
continue;
StringList::const_iterator k = tok.getTokens().begin();
const string& name = *k++;
const string& server = *k++;
const string& desc = *k++;
const string& usersOnline = *k++;
publicListMatrix[publicListServer].push_back(HubEntry(name, server, desc, usersOnline));
}
}
}
downloadBuf = Util::emptyString;
}
class XmlListLoader : public SimpleXMLReader::CallBack {
public:
XmlListLoader(HubEntry::List& lst) : publicHubs(lst) { }
virtual ~XmlListLoader() { }
virtual void startTag(const string& name, StringPairList& attribs, bool) {
if(name == "Hub") {
const string& name = getAttrib(attribs, "Name", 0);
const string& server = getAttrib(attribs, "Address", 1);
const string& description = getAttrib(attribs, "Description", 2);
const string& users = getAttrib(attribs, "Users", 3);
const string& country = getAttrib(attribs, "Country", 4);
const string& shared = getAttrib(attribs, "Shared", 5);
const string& minShare = getAttrib(attribs, "Minshare", 5);
const string& minSlots = getAttrib(attribs, "Minslots", 5);
const string& maxHubs = getAttrib(attribs, "Maxhubs", 5);
const string& maxUsers = getAttrib(attribs, "Maxusers", 5);
const string& reliability = getAttrib(attribs, "Reliability", 5);
const string& rating = getAttrib(attribs, "Rating", 5);
publicHubs.push_back(HubEntry(name, server, description, users, country, shared, minShare, minSlots, maxHubs, maxUsers, reliability, rating));
}
}
virtual void endTag(const string&, const string&) {
}
private:
HubEntry::List& publicHubs;
};
void FavoriteManager::loadXmlList(const string& xml) {
try {
XmlListLoader loader(publicListMatrix[publicListServer]);
SimpleXMLReader(&loader).fromXML(xml);
} catch(const SimpleXMLException&) {
}
}
void FavoriteManager::save() {
if(dontSave)
return;
Lock l(cs);
try {
SimpleXML xml;
xml.addTag("Favorites");
xml.stepIn();
xml.addTag("Hubs");
xml.stepIn();
for(FavoriteHubEntry::Iter i = favoriteHubs.begin(); i != favoriteHubs.end(); ++i) {
xml.addTag("Hub");
xml.addChildAttrib("Name", (*i)->getName());
xml.addChildAttrib("Connect", (*i)->getConnect());
xml.addChildAttrib("Description", (*i)->getDescription());
xml.addChildAttrib("Nick", (*i)->getNick(false));
xml.addChildAttrib("Password", (*i)->getPassword());
xml.addChildAttrib("Server", (*i)->getServer());
xml.addChildAttrib("UserDescription", (*i)->getUserDescription());
xml.addChildAttrib("Bottom", Util::toString((*i)->getBottom()));
xml.addChildAttrib("Top", Util::toString((*i)->getTop()));
xml.addChildAttrib("Right", Util::toString((*i)->getRight()));
xml.addChildAttrib("Left", Util::toString((*i)->getLeft()));
}
xml.stepOut();
xml.addTag("Users");
xml.stepIn();
for(FavoriteMap::iterator j = users.begin(); j != users.end(); ++j) {
j->second.getUser()->setFlag(User::SAVE_NICK);
xml.addTag("User");
xml.addChildAttrib("LastSeen", j->second.getLastSeen());
xml.addChildAttrib("GrantSlot", j->second.isSet(FavoriteUser::FLAG_GRANTSLOT));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -