ed2kfile.cpp

Go to the documentation of this file.
00001 /**
00002  *  Copyright (C) 2004-2005 Alo Sarv <madcat_@users.sourceforge.net>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00019 /** @file ed2kfile.cpp Implementation of ED2KFile class  */
00020 
00021 #include <hn/hnprec.h>
00022 #include "ed2kfile.h"
00023 #include "opcodes.h"
00024 #include "ed2k.h"
00025 #include "tag.h"
00026 #include <hn/sharedfile.h>
00027 
00028 // ED2KFile class
00029 // --------------
00030 // Constructor.
00031 // Convert HydraNode FileType enum to eDonkey2000-compatible file type string.
00032 ED2KFile::ED2KFile(
00033         Hash<ED2KHash> h, const std::string &name, uint32_t size,
00034         FileType type, uint8_t flags
00035 ) : m_hash(h), m_name(name), m_size(size), m_hnType(type), m_flags(flags),
00036 m_id(0), m_port(0) {
00037         m_ed2kType = HNType2ED2KType(m_hnType);
00038 }
00039 
00040 // Constructor
00041 // Convert eDonkey2000-compatible filetype string into HydraNode FileType enum
00042 ED2KFile::ED2KFile(
00043         Hash<ED2KHash> h, const std::string &name, uint32_t size,
00044         const std::string &type, uint8_t flags
00045 ) : m_hash(h), m_name(name), m_size(size), m_ed2kType(type), m_flags(flags),
00046 m_id(0), m_port(0) {
00047         m_hnType = ED2KType2HNType(type);
00048 }
00049 ED2KFile::ED2KFile(
00050         Hash<ED2KHash> h, const std::string &name, uint32_t size,
00051         uint32_t id, uint16_t port
00052 ) : m_hash(h), m_name(name), m_size(size), m_hnType(FT_UNKNOWN), m_id(id),
00053 m_port(port) {}
00054 
00055 std::ostream& operator<<(std::ostream &o, const ED2KFile &f) {
00056         Utils::putVal(o, f.m_hash.getData(), 16);
00057         if (f.m_flags & ED2KFile::FL_USECOMPLETEINFO) {
00058                 if (f.m_flags & ED2KFile::FL_COMPLETE) {
00059                         Utils::putVal<uint32_t>(o, FL_COMPLETE_ID);
00060                         Utils::putVal<uint16_t>(o, FL_COMPLETE_PORT);
00061                 } else {
00062                         Utils::putVal<uint32_t>(o, FL_PARTIAL_ID);
00063                         Utils::putVal<uint16_t>(o, FL_PARTIAL_PORT);
00064                 }
00065         } else {
00066                 Utils::putVal<uint32_t>(o, ED2K::instance().getId());
00067                 Utils::putVal<uint16_t>(o, ED2K::instance().getTcpPort());
00068         }
00069         uint32_t tagCount = 2;
00070         if (f.getStrType().size()) {
00071                 tagCount++;
00072         }
00073         Utils::putVal<uint32_t>(o, tagCount);
00074         o << Tag(CT_FILENAME, f.getName());
00075         o << Tag(CT_FILESIZE, f.getSize());
00076         if (f.getStrType().size()) {
00077                 o << Tag(CT_FILETYPE, f.getStrType());
00078         }
00079         return o;
00080 }
00081 
00082 // Type conversions
00083 std::string ED2KFile::HNType2ED2KType(FileType type) {
00084         switch (type) {
00085                 case FT_VIDEO:    return FT_ED2K_VIDEO;
00086                 case FT_AUDIO:    return FT_ED2K_AUDIO;
00087                 case FT_IMAGE:    return FT_ED2K_IMAGE;
00088                 case FT_DOCUMENT: return FT_ED2K_DOCUMENT;
00089                 case FT_ARCHIVE:
00090                 case FT_PROGRAM:
00091                 case FT_CDIMAGE:  return FT_ED2K_PROGRAM;
00092                 default:          return "";
00093         }
00094 }
00095 FileType ED2KFile::ED2KType2HNType(const std::string &type) {
00096         if (type == FT_ED2K_AUDIO) {
00097                 return FT_AUDIO;
00098         } else if (type == FT_ED2K_VIDEO) {
00099                 return FT_VIDEO;
00100         } else if (type == FT_ED2K_IMAGE) {
00101                 return FT_IMAGE;
00102         } else if (type == FT_ED2K_DOCUMENT) {
00103                 return FT_DOCUMENT;
00104         } else if (type == FT_ED2K_PROGRAM) {
00105                 return FT_PROGRAM;
00106         } else {
00107                 return FT_UNKNOWN;
00108         }
00109 }
00110 
00111 // construct ED2KFile from data
00112 boost::shared_ptr<ED2KFile> makeED2KFile(
00113         SharedFile *sf, MetaData *md, HashSetBase *hs, bool useCompleteInfo
00114 ) {
00115         uint8_t flags = 0;
00116         if (sf->isComplete()) {
00117                 flags &= ED2KFile::FL_COMPLETE;
00118         }
00119         if (useCompleteInfo) {
00120                 flags &= ED2KFile::FL_USECOMPLETEINFO;
00121         }
00122         return boost::shared_ptr<ED2KFile>(
00123                 new ED2KFile(
00124                         hs->getFileHash().getData(), sf->getName(),
00125                         sf->getSize(), md->getFileType(), flags
00126                 )
00127         );
00128 }
00129 
00130 std::string ED2KFile::ratingToString(const ED2KFile::Rating &r) {
00131         switch (r) {
00132                 case FR_INVALID:   return "Invalid";
00133                 case FR_POOR:      return "Poor";
00134                 case FR_GOOD:      return "Good";
00135                 case FR_FAIR:      return "Fair";
00136                 case FR_EXCELLENT: return "Excellent";
00137                 case FR_NORATING:
00138                 default:           return "";
00139         }
00140 }