libktorrent  2.2.0
cache.h
1 /***************************************************************************
2  * Copyright (C) 2005 by Joris Guisson *
3  * joris.guisson@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 #ifndef BTCACHE_H
21 #define BTCACHE_H
22 
23 #include <ktorrent_export.h>
24 #include <util/constants.h>
25 #include <torrent/torrent.h>
26 #include <diskio/piecedata.h>
27 #include <QString>
28 #include <QMultiMap>
29 #include <QSet>
30 
31 
32 class QStringList;
33 
34 namespace bt
35 {
36  class TorrentFile;
37  class Chunk;
38  class PreallocationThread;
39  class TorrentFileInterface;
40  class Job;
41 
49  class KTORRENT_EXPORT Cache
50  {
51  public:
52  Cache(Torrent & tor,const QString & tmpdir,const QString & datadir);
53  virtual ~Cache();
54 
59  virtual void loadFileMap() = 0;
60 
64  virtual void saveFileMap() = 0;
65 
67  QString getDataDir() const {return datadir;}
68 
73  virtual QString getOutputPath() const = 0;
74 
80  virtual void changeTmpDir(const QString & ndir);
81 
87  virtual void changeOutputPath(const QString & outputpath) = 0;
88 
94  virtual Job* moveDataFiles(const QString & ndir) = 0;
95 
100  virtual void moveDataFilesFinished(Job* job) = 0;
101 
110  virtual PieceData::Ptr loadPiece(Chunk* c,Uint32 off,Uint32 length) = 0;
111 
120  virtual PieceData::Ptr preparePiece(Chunk* c,Uint32 off,Uint32 length) = 0;
121 
126  virtual void savePiece(PieceData::Ptr piece) = 0;
127 
131  virtual void create() = 0;
132 
136  virtual void close() = 0;
137 
141  virtual void open() = 0;
142 
144  virtual void downloadStatusChanged(TorrentFile*, bool) {};
145 
150  virtual void preparePreallocation(PreallocationThread* prealloc) = 0;
151 
153  bool hasExistingFiles() const {return preexisting_files;}
154 
159  virtual bool hasMissingFiles(QStringList & sl) = 0;
160 
166  virtual Job* deleteDataFiles() = 0;
167 
173  virtual Job* moveDataFiles(const QMap<TorrentFileInterface*,QString> & files);
174 
180  virtual void moveDataFilesFinished(const QMap<TorrentFileInterface*,QString> & files,Job* job);
181 
186  static bool mappedModeAllowed();
187 
191  virtual Uint64 diskUsage() = 0;
192 
197  virtual bool getMountPoints(QSet<QString> & mps) = 0;
198 
203  static void setPreallocationEnabled(bool on) {preallocate_files = on;}
204 
209  static bool preallocationEnabled() {return preallocate_files;}
210 
215  static void setPreallocateFully(bool on) {preallocate_fully = on;}
216 
221  static bool preallocateFully() {return preallocate_fully;}
222 
227 
232  void clearPieces(Chunk* c);
233 
238 
240  bool isStorageMounted(QStringList & missing);
241 
242  protected:
243  PieceData::Ptr findPiece(Chunk* c,Uint32 off,Uint32 len,bool read_only);
244  void insertPiece(Chunk* c,PieceData::Ptr p);
245  void clearPieceCache();
246  void cleanupPieceCache();
247  void saveMountPoints(const QSet<QString> & mp);
248 
249  protected:
250  Torrent & tor;
251  QString tmpdir;
252  QString datadir;
253  bool preexisting_files;
254  Uint32 mmap_failures;
255 
256  typedef QMultiMap<Chunk*,PieceData::Ptr> PieceCache;
257  PieceCache piece_cache;
258 
259  QSet<QString> mount_points;
260  private:
261  static bool preallocate_files;
262  static bool preallocate_fully;
263  };
264 
265 }
266 
267 #endif
bt::Cache::setPreallocationEnabled
static void setPreallocationEnabled(bool on)
Definition: cache.h:203
bt::Cache::create
virtual void create()=0
bt::Cache::changeOutputPath
virtual void changeOutputPath(const QString &outputpath)=0
bt::Cache::saveFileMap
virtual void saveFileMap()=0
bt::Cache::hasMissingFiles
virtual bool hasMissingFiles(QStringList &sl)=0
bt::Cache::setPreallocateFully
static void setPreallocateFully(bool on)
Definition: cache.h:215
bt::Chunk
Keep track of a piece of the file.
Definition: chunk.h:46
bt::Job
Definition: job.h:38
bt::Cache::close
virtual void close()=0
bt::Cache::downloadStatusChanged
virtual void downloadStatusChanged(TorrentFile *, bool)
Does nothing, can be overridden to be alerted of download status changes of a TorrentFile.
Definition: cache.h:144
bt::Cache::clearPieces
void clearPieces(Chunk *c)
bt::Cache::deleteDataFiles
virtual Job * deleteDataFiles()=0
bt::Cache::savePiece
virtual void savePiece(PieceData::Ptr piece)=0
bt::TorrentFile
Definition: torrentfile.h:42
bt::PreallocationThread
Definition: preallocationthread.h:42
bt::Torrent
Loads a .torrent file.
Definition: torrent.h:80
bt::Cache::mappedModeAllowed
static bool mappedModeAllowed()
bt::Cache::hasExistingFiles
bool hasExistingFiles() const
See if the download has existing files.
Definition: cache.h:153
bt::Cache::isStorageMounted
bool isStorageMounted(QStringList &missing)
Is the storage mounted ?
bt::Cache::loadMountPoints
void loadMountPoints()
bt::Cache::preallocateFully
static bool preallocateFully()
Definition: cache.h:221
bt::Cache::checkMemoryUsage
void checkMemoryUsage()
bt::Cache::getDataDir
QString getDataDir() const
Get the datadir.
Definition: cache.h:67
bt::Cache::open
virtual void open()=0
bt::Cache
Manages the temporary data.
Definition: cache.h:50
bt::Cache::preparePreallocation
virtual void preparePreallocation(PreallocationThread *prealloc)=0
bt::Cache::preparePiece
virtual PieceData::Ptr preparePiece(Chunk *c, Uint32 off, Uint32 length)=0
bt::Cache::changeTmpDir
virtual void changeTmpDir(const QString &ndir)
bt::Cache::loadPiece
virtual PieceData::Ptr loadPiece(Chunk *c, Uint32 off, Uint32 length)=0
bt::Cache::getMountPoints
virtual bool getMountPoints(QSet< QString > &mps)=0
bt::Cache::diskUsage
virtual Uint64 diskUsage()=0
bt::Cache::loadFileMap
virtual void loadFileMap()=0
bt::Cache::getOutputPath
virtual QString getOutputPath() const =0
bt::Cache::preallocationEnabled
static bool preallocationEnabled()
Definition: cache.h:209