libktorrent  2.1.1
torrent.h
1 /***************************************************************************
2  * Copyright (C) 2005 by *
3  * Joris Guisson <joris.guisson@gmail.com> *
4  * Ivan Vasic <ivasic@gmail.com> *
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License as published by *
8  * the Free Software Foundation; either version 2 of the License, or *
9  * (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with this program; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20  ***************************************************************************/
21 #ifndef BTTORRENT_H
22 #define BTTORRENT_H
23 
24 #include <QUrl>
25 #include <QVector>
26 #include <QList>
27 #include <util/sha1hash.h>
28 #include <util/constants.h>
29 #include <interfaces/torrentinterface.h>
30 #include <peer/peerid.h>
31 #include <ktorrent_export.h>
32 #include "torrentfile.h"
33 
34 class QTextCodec;
35 
36 
37 namespace bt
38 {
39  class BNode;
40  class BDictNode;
41  class BListNode;
42 
43 
44  struct TrackerTier
45  {
46  QList<QUrl> urls;
47  TrackerTier* next;
48 
49  TrackerTier() : next(0)
50  {}
51 
52  ~TrackerTier()
53  {
54  delete next;
55  }
56  };
57 
63  class KTORRENT_EXPORT FilePriorityListener
64  {
65  public:
66  virtual ~FilePriorityListener() {}
67 
68  virtual void downloadPriorityChanged(TorrentFile* tf,Priority newpriority,Priority oldpriority) = 0;
69  };
70 
71 
79  class KTORRENT_EXPORT Torrent
80  {
81  public:
82  Torrent();
83  Torrent(const bt::SHA1Hash & hash);
84  virtual ~Torrent();
85 
90  void setFilePriorityListener(FilePriorityListener* l) {file_prio_listener = l;}
91 
98  void downloadPriorityChanged(TorrentFile* tf,Priority newpriority,Priority oldpriority);
99 
105  void filePercentageChanged(TorrentFile* tf,float perc);
106 
112  void filePreviewChanged(TorrentFile* tf,bool preview);
113 
120  void load(const QByteArray & data,bool verbose);
121 
122  void debugPrintInfo();
123 
125  QString getComments() const {return comments;}
126 
128  Uint32 getNumChunks() const {return hash_pieces.size();}
129 
131  Uint64 getChunkSize() const {return chunk_size;}
132 
134  Uint64 getLastChunkSize() const {return last_chunk_size;}
135 
137  const SHA1Hash & getInfoHash() const {return info_hash;}
138 
140  const PeerID & getPeerID() const {return peer_id;}
141 
143  Uint64 getTotalSize() const {return total_size;}
144 
146  QString getNameSuggestion() const {return name_suggestion;}
147 
155  bool verifyHash(const SHA1Hash & h,Uint32 index);
156 
158  unsigned int getNumTrackerURLs() const;
159 
166  const SHA1Hash & getHash(Uint32 idx) const;
167 
169  bool isMultiFile() const {return files.count() > 0;}
170 
173  Uint32 getNumFiles() const {return files.count();}
174 
181  TorrentFile & getFile(Uint32 idx);
182 
189  const TorrentFile & getFile(Uint32 idx) const;
190 
199  void calcChunkPos(Uint32 chunk,QList<Uint32> & file_list) const;
200 
204  bool isMultimedia() const;
205 
207  bool isPrivate() const {return priv_torrent;}
208 
210  bool isLoaded() const {return loaded;}
211 
213  const TrackerTier* getTrackerList() const { return trackers; }
214 
216  Uint32 getNumDHTNodes() const {return nodes.count();}
217 
219  const DHTNode & getDHTNode(Uint32 i) {return nodes[i];}
220 
226 
231  void updateFilePercentage(Uint32 chunk,ChunkManager & cman);
232 
236  const QList<QUrl>& getWebSeeds() const {return web_seeds;}
237 
239  void changeTextCodec(QTextCodec* codec);
240 
242  const QTextCodec* getTextCodec() {return text_codec;}
243 
245  void setMonitor(MonitorInterface* m) {tmon = m;}
246 
248  const QByteArray & getMetaData() const {return metadata;}
249 
250  private:
251  void loadInfo(BDictNode* node);
252  void loadTrackerURL(const QString & s);
253  void loadHash(BDictNode* dict);
254  void loadFiles(BListNode* node);
255  void loadNodes(BListNode* node);
256  void loadAnnounceList(BNode* node);
257  void loadWebSeeds(BListNode* node);
258  bool checkPathForDirectoryTraversal(const QString & p);
259 
260  private:
261  QString name_suggestion;
262  QByteArray unencoded_name;
263  QString comments;
264  QByteArray metadata;
265 
266  SHA1Hash info_hash;
267  QVector<SHA1Hash> hash_pieces;
268  QVector<TorrentFile> files;
269  QVector<DHTNode> nodes;
270  QList<QUrl> web_seeds;
271  PeerID peer_id;
272 
273  TrackerTier* trackers;
274  Uint64 chunk_size;
275  Uint64 last_chunk_size;
276  Uint64 total_size;
277  QTextCodec* text_codec;
278  FilePriorityListener* file_prio_listener;
279  mutable Uint32 pos_cache_chunk;
280  mutable Uint32 pos_cache_file;
281  MonitorInterface* tmon;
282  bool priv_torrent;
283  bool loaded;
284  };
285 
286 }
287 
288 #endif
bt::Torrent::isPrivate
bool isPrivate() const
See if the torrent is private.
Definition: torrent.h:207
bt::Torrent::getNameSuggestion
QString getNameSuggestion() const
Get the suggested name.
Definition: torrent.h:146
bt::Torrent::getNumChunks
Uint32 getNumChunks() const
Get the number of chunks.
Definition: torrent.h:128
bt::Torrent::getNumTrackerURLs
unsigned int getNumTrackerURLs() const
Get the number of tracker URL's.
bt::Torrent::getNumFiles
Uint32 getNumFiles() const
Definition: torrent.h:173
bt::BNode
Base class for a node in a b-encoded piece of data.
Definition: bnode.h:62
bt::Torrent::isLoaded
bool isLoaded() const
Is the torrent loaded.
Definition: torrent.h:210
bt::Torrent::getHash
const SHA1Hash & getHash(Uint32 idx) const
bt::ChunkManager
Definition: chunkmanager.h:61
bt::Torrent::calcChunkPos
void calcChunkPos(Uint32 chunk, QList< Uint32 > &file_list) const
bt::DHTNode
Definition: torrentinterface.h:88
bt::Torrent::load
void load(const QByteArray &data, bool verbose)
bt::Torrent::setFilePriorityListener
void setFilePriorityListener(FilePriorityListener *l)
Definition: torrent.h:90
bt::Torrent::updateFilePercentage
void updateFilePercentage(Uint32 chunk, ChunkManager &cman)
bt::Torrent::isMultimedia
bool isMultimedia() const
bt::Torrent::updateFilePercentage
void updateFilePercentage(ChunkManager &cman)
bt::TorrentFile
Definition: torrentfile.h:61
bt::Torrent::getWebSeeds
const QList< QUrl > & getWebSeeds() const
Definition: torrent.h:236
bt::Torrent::getTrackerList
const TrackerTier * getTrackerList() const
Gets a pointer to AnnounceList.
Definition: torrent.h:213
bt::Torrent::getComments
QString getComments() const
Return the comments in the torrent.
Definition: torrent.h:125
bt::Torrent::getMetaData
const QByteArray & getMetaData() const
Get the metadata.
Definition: torrent.h:248
bt::Torrent
Loads a .torrent file.
Definition: torrent.h:80
bt::Torrent::getLastChunkSize
Uint64 getLastChunkSize() const
Get the size of the last chunk.
Definition: torrent.h:134
bt::Torrent::getFile
TorrentFile & getFile(Uint32 idx)
bt::Torrent::verifyHash
bool verifyHash(const SHA1Hash &h, Uint32 index)
bt::MonitorInterface
Interface for classes who want to monitor a TorrentInterface.
Definition: monitorinterface.h:57
bt::Torrent::changeTextCodec
void changeTextCodec(QTextCodec *codec)
Change the text codec.
bt::Torrent::getTextCodec
const QTextCodec * getTextCodec()
Get the text codec.
Definition: torrent.h:242
bt::Torrent::downloadPriorityChanged
void downloadPriorityChanged(TorrentFile *tf, Priority newpriority, Priority oldpriority)
bt::Torrent::getChunkSize
Uint64 getChunkSize() const
Get the size of a chunk.
Definition: torrent.h:131
bt::SHA1Hash
Stores a SHA1 hash.
Definition: sha1hash.h:41
bt::Torrent::filePreviewChanged
void filePreviewChanged(TorrentFile *tf, bool preview)
bt::BListNode
Represents a list in bencoded data.
Definition: bnode.h:190
bt::Torrent::getTotalSize
Uint64 getTotalSize() const
Get the file size in number of bytes.
Definition: torrent.h:143
bt::Torrent::getDHTNode
const DHTNode & getDHTNode(Uint32 i)
Get a DHT node.
Definition: torrent.h:219
bt::TrackerTier
Definition: torrent.h:45
bt::Torrent::getNumDHTNodes
Uint32 getNumDHTNodes() const
Get the number of initial DHT nodes.
Definition: torrent.h:216
bt::Torrent::isMultiFile
bool isMultiFile() const
See if we have a multi file torrent.
Definition: torrent.h:169
bt::Torrent::filePercentageChanged
void filePercentageChanged(TorrentFile *tf, float perc)
bt::BDictNode
Represents a dictionary in bencoded data.
Definition: bnode.h:120
bt::PeerID
Definition: peerid.h:51
bt::Torrent::setMonitor
void setMonitor(MonitorInterface *m)
Set the monitor.
Definition: torrent.h:245
bt::FilePriorityListener
Definition: torrent.h:64
bt::Torrent::getInfoHash
const SHA1Hash & getInfoHash() const
Get the info_hash.
Definition: torrent.h:137
bt::Torrent::getPeerID
const PeerID & getPeerID() const
Get our peer_id.
Definition: torrent.h:140
bt::Torrent::getFile
const TorrentFile & getFile(Uint32 idx) const