File indexing completed on 2024-04-28 04:57:29

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 #include "btcache.h"
0011 
0012 #include <diskio/chunk.h>
0013 
0014 #include <QDataStream>
0015 
0016 using namespace bt;
0017 // TODO: Support buffered mode?
0018 BTCache::BTCache(Torrent &tor, const QString &tmpdir, const QString &datadir)
0019     : Cache(tor, tmpdir, datadir)
0020     , QObject(nullptr)
0021 {
0022 }
0023 
0024 BTCache::~BTCache()
0025 {
0026 }
0027 
0028 void BTCache::load(Chunk *c)
0029 {
0030     c->setData(0, Chunk::MMAPPED);
0031 }
0032 
0033 void BTCache::save(Chunk *c)
0034 {
0035     /*if (c->getStatus() == Chunk::MMAPPED)
0036     {
0037         KIO::fileoffset_t off = c->getIndex() * tor.getChunkSize();
0038         qCDebug(KGET_DEBUG) << "Fileoffset is: " + QString::number(off);
0039         QByteArray data;
0040         QDataStream s(&data, QIODevice::WriteOnly | QIODevice::Unbuffered);
0041         s << c->getData();
0042         emit dataArrived(off, data);
0043         c->clear();
0044         c->setStatus(Chunk::ON_DISK);
0045     }
0046     else if (c->getStatus() == Chunk::BUFFERED)
0047     {*/
0048     KIO::fileoffset_t off = c->getIndex() * tor.getChunkSize();
0049     qCDebug(KGET_DEBUG) << "Fileoffset is: " + QString::number(off);
0050     QByteArray data;
0051     QDataStream s(&data, QIODevice::WriteOnly | QIODevice::Unbuffered);
0052     s << c->getData();
0053     emit dataArrived(off, data);
0054     // fd->write(c->getData(),c->getSize(),off);//Send a signal here that the signal has arrived
0055     c->clear();
0056     c->setStatus(Chunk::ON_DISK);
0057     //}
0058 }
0059 
0060 bool BTCache::prep(Chunk *c)
0061 {
0062     c->setData(0, Chunk::MMAPPED);
0063     return true;
0064 }
0065 
0066 void BTCache::deleteDataFiles()
0067 {
0068 }
0069 
0070 Cache *BTCacheFactory::create(Torrent &tor, const QString &tmpdir, const QString &datadir)
0071 {
0072     BTCache *newcache = new BTCache(tor, tmpdir, datadir);
0073     emit cacheAdded(newcache);
0074     return newcache;
0075 }
0076 
0077 #include "moc_btcache.cpp"