Torrent.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2006-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #ifndef PROTOCOLS__BIT_TORRENT__TORRENTS__TORRENT_H
00024 #define PROTOCOLS__BIT_TORRENT__TORRENTS__TORRENT_H
00025 
00026 #include "Imports.h"
00027 
00028 namespace Protocols {
00029 namespace BitTorrent {
00030 namespace Torrents {
00031 
00033 
00083 class Torrent
00084 {
00085 public:
00086     VALUE_OBJECT (Torrent)
00087 
00088 public:
00090     typedef QList <QList <Uri> > AnnounceList;
00091 
00093     typedef FixedSizeByteArray <20> Piece;
00094 
00096     typedef QList <Piece> PieceList;
00097 
00099 
00102     typedef FixedSizeByteArray <20> InfoHash;
00103 
00105 
00112     class FileInfo
00113     {
00114     public:
00115         VALUE_OBJECT (FileInfo)
00116 
00117     public:
00119         typedef FixedSizeByteArray <32> Checksum;
00120 
00121                             ~FileInfo();
00122 
00123         void                swap (FileInfo &other);
00124 
00125         QString             path() const;
00126         qint64              length() const;
00127         Checksum            checksum() const;
00128 
00129     private:
00130         // See private section of the Torrent class for an explanation
00131         friend class TorrentParser;
00132 
00133                             FileInfo();
00134 
00135         QString             filePath;       
00136         qint64              fileLength;     
00137         Checksum            fileChecksum;   
00138     };
00139 
00141     typedef QList <FileInfo> FileInfoList;
00142 
00143                             Torrent();
00144                             ~Torrent();
00145 
00146     Uri                     announce() const;
00147     AnnounceList            announceList() const;
00148     QDateTime               creationDate() const;
00149     QString                 comment() const;
00150     QString                 createdBy() const;
00151     int                     pieceLength() const;
00152     PieceList               pieces() const;
00153     bool                    isPrivate() const;
00154     FileInfoList            files() const;
00155     InfoHash                infoHash() const;
00156 
00157 private:
00158     // TorrentParser is friend because of torrent data validation
00159     // (now it's possible to validate data only in the TorrentParser and
00160     // there is no way how the user can corrupt them externally, for example
00161     // setting invalid pieceLength)
00162     friend class TorrentParser;
00163 
00165     struct PrivateData : public QSharedData
00166     {
00167         Uri                 announce;       
00168         AnnounceList        announceList;   
00169         QDateTime           creationDate;   
00170         QString             comment;        
00171         QString             createdBy;      
00172         int                 pieceLength;    
00173         PieceList           pieces;         
00174         bool                isPrivate;      
00175         FileInfoList        files;          
00176         InfoHash            infoHash;       
00177     };
00178 
00179     QSharedDataPointer <PrivateData> d;     
00180 };
00181 
00183 inline QString Torrent::FileInfo::path() const
00184 { return filePath; }
00185 
00187 inline qint64 Torrent::FileInfo::length() const
00188 { return fileLength; }
00189 
00191 inline Torrent::FileInfo::Checksum Torrent::FileInfo::checksum() const
00192 { return fileChecksum; }
00193 
00195 inline Uri Torrent::announce() const
00196 { return d->announce; }
00197 
00199 inline Torrent::AnnounceList Torrent::announceList() const
00200 { return d->announceList; }
00201 
00203 inline QDateTime Torrent::creationDate() const
00204 { return d->creationDate; }
00205 
00207 inline QString Torrent::comment() const
00208 { return d->comment; }
00209 
00211 inline QString Torrent::createdBy() const
00212 { return d->createdBy; }
00213 
00215 inline int Torrent::pieceLength() const
00216 { return d->pieceLength; }
00217 
00219 inline Torrent::PieceList Torrent::pieces() const
00220 { return d->pieces; }
00221 
00223 inline bool Torrent::isPrivate() const
00224 { return d->isPrivate; }
00225 
00227 inline Torrent::FileInfoList Torrent::files() const
00228 { return d->files; }
00229 
00231 inline Torrent::InfoHash Torrent::infoHash() const
00232 { return d->infoHash; }
00233 
00234 } // namespace Torrents
00235 } // namespace BitTorrent
00236 } // namespace Protocols
00237 
00238 #endif // PROTOCOLS__BIT_TORRENT__TORRENTS__TORRENT_H