diff options
author | Remko Tronçon <git@el-tramo.be> | 2011-04-25 07:26:20 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2011-04-25 07:48:15 (GMT) |
commit | d80935c165ac2709b1dfba1f38c06f2cc1d91ee9 (patch) | |
tree | c3e2b7dd23c89d07b17802686bd0a6db80b7b5d4 /Swiften/Avatars | |
parent | 20f9d6330351b373d40b40222433e11a2a0f2713 (diff) | |
download | swift-contrib-d80935c165ac2709b1dfba1f38c06f2cc1d91ee9.zip swift-contrib-d80935c165ac2709b1dfba1f38c06f2cc1d91ee9.tar.bz2 |
Moved file storages to Swift.
Diffstat (limited to 'Swiften/Avatars')
-rw-r--r-- | Swiften/Avatars/AvatarFileStorage.cpp | 105 | ||||
-rw-r--r-- | Swiften/Avatars/AvatarFileStorage.h | 41 | ||||
-rw-r--r-- | Swiften/Avatars/SConscript | 1 |
3 files changed, 0 insertions, 147 deletions
diff --git a/Swiften/Avatars/AvatarFileStorage.cpp b/Swiften/Avatars/AvatarFileStorage.cpp deleted file mode 100644 index 9effc16..0000000 --- a/Swiften/Avatars/AvatarFileStorage.cpp +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#include <Swiften/Avatars/AvatarFileStorage.h> - -#include <iostream> -#include <boost/filesystem/fstream.hpp> -#include <boost/filesystem.hpp> - -#include <Swiften/Base/foreach.h> -#include <Swiften/Base/String.h> -#include <Swiften/StringCodecs/SHA1.h> -#include <Swiften/StringCodecs/Hexify.h> - -namespace Swift { - -AvatarFileStorage::AvatarFileStorage(const boost::filesystem::path& avatarsDir, const boost::filesystem::path& avatarsFile) : avatarsDir(avatarsDir), avatarsFile(avatarsFile) { - if (boost::filesystem::exists(avatarsFile)) { - try { - boost::filesystem::ifstream file(avatarsFile); - std::string line; - if (file.is_open()) { - while (!file.eof()) { - getline(file, line); - std::pair<std::string, std::string> r = String::getSplittedAtFirst(line, ' '); - JID jid(r.second); - if (jid.isValid()) { - jidAvatars.insert(std::make_pair(jid, r.first)); - } - else if (!r.first.empty() || !r.second.empty()) { - std::cerr << "Invalid entry in avatars file: " << r.second << std::endl; - } - } - } - } - catch (...) { - std::cerr << "Error reading avatars file" << std::endl; - } - } -} - -bool AvatarFileStorage::hasAvatar(const std::string& hash) const { - return boost::filesystem::exists(getAvatarPath(hash)); -} - -void AvatarFileStorage::addAvatar(const std::string& hash, const ByteArray& avatar) { - assert(Hexify::hexify(SHA1::getHash(avatar)) == hash); - - boost::filesystem::path avatarPath = getAvatarPath(hash); - if (!boost::filesystem::exists(avatarPath.parent_path())) { - try { - boost::filesystem::create_directories(avatarPath.parent_path()); - } - catch (const boost::filesystem::filesystem_error& e) { - std::cerr << "ERROR: " << e.what() << std::endl; - } - } - boost::filesystem::ofstream file(avatarPath, boost::filesystem::ofstream::binary|boost::filesystem::ofstream::out); - file.write(reinterpret_cast<const char*>(avatar.getData()), static_cast<std::streamsize>(avatar.getSize())); - file.close(); -} - -boost::filesystem::path AvatarFileStorage::getAvatarPath(const std::string& hash) const { - return avatarsDir / hash; -} - -ByteArray AvatarFileStorage::getAvatar(const std::string& hash) const { - ByteArray data; - data.readFromFile(getAvatarPath(hash).string()); - return data; -} - -void AvatarFileStorage::setAvatarForJID(const JID& jid, const std::string& hash) { - std::pair<JIDAvatarMap::iterator, bool> r = jidAvatars.insert(std::make_pair(jid, hash)); - if (r.second) { - saveJIDAvatars(); - } - else if (r.first->second != hash) { - r.first->second = hash; - saveJIDAvatars(); - } -} - -std::string AvatarFileStorage::getAvatarForJID(const JID& jid) const { - JIDAvatarMap::const_iterator i = jidAvatars.find(jid); - return i == jidAvatars.end() ? "" : i->second; -} - -void AvatarFileStorage::saveJIDAvatars() { - try { - boost::filesystem::ofstream file(avatarsFile); - for (JIDAvatarMap::const_iterator i = jidAvatars.begin(); i != jidAvatars.end(); ++i) { - file << i->second << " " << i->first.toString() << std::endl; - } - file.close(); - } - catch (...) { - std::cerr << "Error writing avatars file" << std::endl; - } -} - -} diff --git a/Swiften/Avatars/AvatarFileStorage.h b/Swiften/Avatars/AvatarFileStorage.h deleted file mode 100644 index b7e73f5..0000000 --- a/Swiften/Avatars/AvatarFileStorage.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#pragma once - -#include <map> -#include <string> -#include <boost/filesystem/path.hpp> - -#include <Swiften/JID/JID.h> -#include "Swiften/Base/ByteArray.h" -#include "Swiften/Avatars/AvatarStorage.h" - -namespace Swift { - class AvatarFileStorage : public AvatarStorage { - public: - AvatarFileStorage(const boost::filesystem::path& avatarsDir, const boost::filesystem::path& avatarsFile); - - virtual bool hasAvatar(const std::string& hash) const; - virtual void addAvatar(const std::string& hash, const ByteArray& avatar); - virtual ByteArray getAvatar(const std::string& hash) const; - - virtual boost::filesystem::path getAvatarPath(const std::string& hash) const; - - virtual void setAvatarForJID(const JID& jid, const std::string& hash); - virtual std::string getAvatarForJID(const JID& jid) const; - - private: - void saveJIDAvatars(); - - private: - boost::filesystem::path avatarsDir; - boost::filesystem::path avatarsFile; - typedef std::map<JID, std::string> JIDAvatarMap; - JIDAvatarMap jidAvatars; - }; - -} diff --git a/Swiften/Avatars/SConscript b/Swiften/Avatars/SConscript index 46ae897..9c219a4 100644 --- a/Swiften/Avatars/SConscript +++ b/Swiften/Avatars/SConscript @@ -1,7 +1,6 @@ Import("swiften_env") objects = swiften_env.SwiftenObject([ - "AvatarFileStorage.cpp", "VCardUpdateAvatarManager.cpp", "VCardAvatarManager.cpp", "OfflineAvatarManager.cpp", |