File indexing completed on 2024-04-21 04:57:12

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2008 Lukas Appelhans <l.appelhans@gmx.de>
0004 
0005    This program is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 */
0010 #ifndef KGETBTCACHE_H
0011 #define KGETBTCACHE_H
0012 
0013 #include <diskio/cache.h>
0014 #include <interfaces/cachefactory.h>
0015 
0016 #include <KIO/Job>
0017 
0018 #include <QByteArray>
0019 #include <QString>
0020 
0021 class QStringList;
0022 class KJob;
0023 
0024 namespace bt
0025 {
0026 class Torrent;
0027 class TorrentFile;
0028 class Chunk;
0029 class PreallocationThread;
0030 }
0031 
0032 using namespace bt;
0033 
0034 class BTCache : public QObject, public bt::Cache
0035 {
0036     Q_OBJECT
0037 public:
0038     BTCache(bt::Torrent &tor, const QString &tmpdir, const QString &datadir);
0039     ~BTCache();
0040 
0041     /**
0042      * Load the file map of a torrent.
0043      * If it doesn't exist, it needs to be created.
0044      */
0045     virtual void loadFileMap()
0046     {
0047     }
0048 
0049     /**
0050      * Save the file map of a torrent
0051      */
0052     virtual void saveFileMap()
0053     {
0054     }
0055 
0056     /**
0057      * Get the actual output path.
0058      * @return The output path
0059      */
0060     virtual QString getOutputPath() const
0061     {
0062         return QString();
0063     }
0064 
0065     /**
0066      * Changes the tmp dir. All data files should already been moved.
0067      * This just modifies the tmpdir variable.
0068      * @param ndir The new tmpdir
0069      */
0070     virtual void changeTmpDir(const QString &ndir)
0071     {
0072         Q_UNUSED(ndir)
0073     }
0074 
0075     /**
0076      * Changes output path. All data files should already been moved.
0077      * This just modifies the datadir variable.
0078      * @param outputpath New output path
0079      */
0080     virtual void changeOutputPath(const QString &outputpath)
0081     {
0082         Q_UNUSED(outputpath)
0083     }
0084 
0085     /**
0086      * Move the data files to a new directory.
0087      * @param ndir The directory
0088      * @return The job doing the move
0089      */
0090     virtual KJob *moveDataFiles(const QString &ndir)
0091     {
0092         return nullptr;
0093     }
0094 
0095     /**
0096      * A move of a bunch of data files has finished
0097      * @param job The job doing the move
0098      */
0099     virtual void moveDataFilesFinished(KJob *job)
0100     {
0101         Q_UNUSED(job)
0102     }
0103 
0104     /**
0105      * Load a chunk into memory. If something goes wrong,
0106      * an Error should be thrown.
0107      * @param c The Chunk
0108      */
0109     virtual void load(Chunk *c);
0110 
0111     /**
0112      * Save a chunk to disk. If something goes wrong,
0113      * an Error should be thrown.
0114      * @param c The Chunk
0115      */
0116     virtual void save(Chunk *c);
0117 
0118     /**
0119      * Prepare a chunk for downloading.
0120      * @param c The Chunk
0121      * @return true if ok, false otherwise
0122      */
0123     virtual bool prep(Chunk *c);
0124 
0125     /**
0126      * Create all the data files to store the data.
0127      */
0128     virtual void create()
0129     {
0130     }
0131 
0132     /**
0133      * Close the cache file(s).
0134      */
0135     virtual void close()
0136     {
0137     }
0138 
0139     /**
0140      * Open the cache file(s)
0141      */
0142     virtual void open()
0143     {
0144     }
0145 
0146     /// Does nothing, can be overridden to be alerted of download status changes of a TorrentFile
0147     virtual void downloadStatusChanged(TorrentFile *, bool)
0148     {
0149     }
0150 
0151     /**
0152      * Preallocate diskspace for all files
0153      * @param prealloc The thread doing the preallocation
0154      */
0155     virtual void preallocateDiskSpace(PreallocationThread *prealloc)
0156     {
0157         Q_UNUSED(prealloc)
0158     }
0159 
0160     /**
0161      * Test all files and see if they are not missing.
0162      * If so put them in a list
0163      */
0164     virtual bool hasMissingFiles(QStringList &sl)
0165     {
0166         return false;
0167     } // We never have missing files, cause we don't have files :P
0168 
0169     /**
0170      * Delete all data files, in case of multi file torrents
0171      * empty directories should also be deleted.
0172      */
0173     virtual KJob *deleteDataFiles()
0174     {
0175         return nullptr;
0176     } // TODO: Implement!!!
0177     virtual bt::PieceData *loadPiece(bt::Chunk *, bt::Uint32, bt::Uint32)
0178     {
0179         return nullptr;
0180     }
0181     virtual bt::PieceData *preparePiece(bt::Chunk *, bt::Uint32, bt::Uint32)
0182     {
0183         return nullptr;
0184     }
0185     virtual void savePiece(bt::PieceData *)
0186     {
0187     }
0188 
0189     /**
0190      * Get the number of bytes all the files of this torrent are currently using on disk.
0191      * */
0192     virtual Uint64 diskUsage()
0193     {
0194         return 0;
0195     }; // We always use 0 Bytes on HDD, cause we don't write to HDD
0196 
0197 Q_SIGNALS:
0198     void dataArrived(const KIO::fileoffset_t &offset, const QByteArray &data);
0199 
0200 private:
0201     Torrent *m_tor;
0202 };
0203 
0204 class BTCacheFactory : public QObject, public CacheFactory
0205 {
0206     Q_OBJECT
0207 public:
0208     BTCacheFactory()
0209     {
0210     }
0211     ~BTCacheFactory()
0212     {
0213     }
0214 
0215     virtual Cache *create(Torrent &tor, const QString &tmpdir, const QString &datadir);
0216 
0217 Q_SIGNALS:
0218     void cacheAdded(BTCache *cache);
0219 };
0220 
0221 #endif