1 /* This file is part of the KDE project
2 
3    Copyright (C) 2008 Lukas Appelhans <l.appelhans@gmx.de>
4 
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public
7    License as published by the Free Software Foundation; either
8    version 2 of the License, or (at your option) any later version.
9 */
10 #ifndef KGETBTCACHE_H
11 #define KGETBTCACHE_H
12 
13 #include <diskio/cache.h>
14 #include <interfaces/cachefactory.h>
15 
16 #include <KIO/Job>
17 
18 #include <QByteArray>
19 #include <QString>
20 
21 class QStringList;
22 class KJob;
23 
24 namespace bt
25 {
26     class Torrent;
27     class TorrentFile;
28     class Chunk;
29     class PreallocationThread;
30 }
31 
32 using namespace bt;
33 
34 class BTCache : public QObject, public bt::Cache
35 {
36     Q_OBJECT
37     public:
38         BTCache(bt::Torrent & tor,const QString & tmpdir,const QString & datadir);
39         ~BTCache();
40 
41         /**
42          * Load the file map of a torrent.
43          * If it doesn't exist, it needs to be created.
44          */
loadFileMap()45         virtual void loadFileMap() {}
46 
47         /**
48          * Save the file map of a torrent
49          */
saveFileMap()50         virtual void saveFileMap() {}
51 
52         /**
53          * Get the actual output path.
54          * @return The output path
55          */
getOutputPath()56         virtual QString getOutputPath() const {return QString();}
57 
58         /**
59          * Changes the tmp dir. All data files should already been moved.
60          * This just modifies the tmpdir variable.
61          * @param ndir The new tmpdir
62          */
changeTmpDir(const QString & ndir)63         virtual void changeTmpDir(const QString & ndir) {Q_UNUSED(ndir)}
64 
65         /**
66          * Changes output path. All data files should already been moved.
67          * This just modifies the datadir variable.
68          * @param outputpath New output path
69          */
changeOutputPath(const QString & outputpath)70         virtual void changeOutputPath(const QString & outputpath) {Q_UNUSED(outputpath)}
71 
72         /**
73          * Move the data files to a new directory.
74          * @param ndir The directory
75          * @return The job doing the move
76          */
moveDataFiles(const QString & ndir)77         virtual KJob* moveDataFiles(const QString & ndir) {return nullptr;}
78 
79         /**
80          * A move of a bunch of data files has finished
81          * @param job The job doing the move
82          */
moveDataFilesFinished(KJob * job)83         virtual void moveDataFilesFinished(KJob* job) {Q_UNUSED(job)}
84 
85         /**
86          * Load a chunk into memory. If something goes wrong,
87          * an Error should be thrown.
88          * @param c The Chunk
89          */
90         virtual void load(Chunk* c);
91 
92         /**
93          * Save a chunk to disk. If something goes wrong,
94          * an Error should be thrown.
95          * @param c The Chunk
96          */
97         virtual void save(Chunk* c);
98 
99         /**
100          * Prepare a chunk for downloading.
101          * @param c The Chunk
102          * @return true if ok, false otherwise
103          */
104         virtual bool prep(Chunk* c);
105 
106         /**
107          * Create all the data files to store the data.
108          */
create()109         virtual void create() {}
110 
111         /**
112          * Close the cache file(s).
113          */
close()114         virtual void close() {}
115 
116         /**
117          * Open the cache file(s)
118          */
open()119         virtual void open() {}
120 
121         /// Does nothing, can be overridden to be alerted of download status changes of a TorrentFile
downloadStatusChanged(TorrentFile *,bool)122         virtual void downloadStatusChanged(TorrentFile*, bool) {}
123 
124         /**
125          * Preallocate diskspace for all files
126          * @param prealloc The thread doing the preallocation
127          */
preallocateDiskSpace(PreallocationThread * prealloc)128         virtual void preallocateDiskSpace(PreallocationThread* prealloc) {Q_UNUSED(prealloc)}
129 
130         /**
131          * Test all files and see if they are not missing.
132          * If so put them in a list
133          */
hasMissingFiles(QStringList & sl)134         virtual bool hasMissingFiles(QStringList & sl) {return false;} //We never have missing files, cause we don't have files :P
135 
136         /**
137          * Delete all data files, in case of multi file torrents
138          * empty directories should also be deleted.
139          */
deleteDataFiles()140         virtual KJob* deleteDataFiles() {return nullptr;}//TODO: Implement!!!
loadPiece(bt::Chunk *,bt::Uint32,bt::Uint32)141         virtual bt::PieceData* loadPiece(bt::Chunk*, bt::Uint32, bt::Uint32) {return nullptr;}
preparePiece(bt::Chunk *,bt::Uint32,bt::Uint32)142         virtual bt::PieceData* preparePiece(bt::Chunk*, bt::Uint32, bt::Uint32) {return nullptr;}
savePiece(bt::PieceData *)143         virtual void savePiece(bt::PieceData*) {}
144 
145         /**
146          * Get the number of bytes all the files of this torrent are currently using on disk.
147          * */
diskUsage()148         virtual Uint64 diskUsage() {return 0;};//We always use 0 Bytes on HDD, cause we don't write to HDD
149 
150     Q_SIGNALS:
151         void dataArrived(const KIO::fileoffset_t &offset, const QByteArray &data);
152 
153     private:
154         Torrent *m_tor;
155 };
156 
157 class BTCacheFactory : public QObject, public CacheFactory
158 {
159     Q_OBJECT
160     public:
BTCacheFactory()161         BTCacheFactory() {}
~BTCacheFactory()162         ~BTCacheFactory() {}
163 
164         virtual Cache* create(Torrent & tor,const QString & tmpdir,const QString & datadir);
165 
166     Q_SIGNALS:
167         void cacheAdded(BTCache* cache);
168 };
169 
170 #endif
171