diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/decoder.cpp | 31 | ||||
| -rw-r--r-- | lib/decoder.h | 2 | ||||
| -rw-r--r-- | lib/decoderfactory.h | 2 | ||||
| -rw-r--r-- | lib/downloader.cpp | 206 | ||||
| -rw-r--r-- | lib/downloader.h | 67 | ||||
| -rw-r--r-- | lib/lib.pro | 13 | ||||
| -rw-r--r-- | lib/qmmp/Input/ffmpeg/decoderffmpegfactory.cpp | 14 | ||||
| -rw-r--r-- | lib/qmmp/Input/ffmpeg/decoderffmpegfactory.h | 1 | ||||
| -rw-r--r-- | lib/qmmp/Input/flac/decoderflacfactory.cpp | 6 | ||||
| -rw-r--r-- | lib/qmmp/Input/flac/decoderflacfactory.h | 1 | ||||
| -rw-r--r-- | lib/qmmp/Input/mad/decodermadfactory.cpp | 7 | ||||
| -rw-r--r-- | lib/qmmp/Input/mad/decodermadfactory.h | 1 | ||||
| -rw-r--r-- | lib/qmmp/Input/mpc/decodermpcfactory.cpp | 6 | ||||
| -rw-r--r-- | lib/qmmp/Input/mpc/decodermpcfactory.h | 1 | ||||
| -rw-r--r-- | lib/qmmp/Input/vorbis/decodervorbisfactory.cpp | 7 | ||||
| -rw-r--r-- | lib/qmmp/Input/vorbis/decodervorbisfactory.h | 1 | ||||
| -rw-r--r-- | lib/soundcore.cpp | 1 | ||||
| -rw-r--r-- | lib/streamreader.cpp | 88 | ||||
| -rw-r--r-- | lib/streamreader.h | 6 |
19 files changed, 392 insertions, 69 deletions
diff --git a/lib/decoder.cpp b/lib/decoder.cpp index b77b780b4..4b940520d 100644 --- a/lib/decoder.cpp +++ b/lib/decoder.cpp @@ -15,6 +15,7 @@ #include "output.h" #include "visualization.h" #include "decoderfactory.h" +#include "streamreader.h" extern "C"{ #include "equ/iir.h" } @@ -134,8 +135,14 @@ Decoder *Decoder::create(QObject *parent, const QString &source, Output *output) { Decoder *decoder = 0; - - DecoderFactory *fact = Decoder::findFactory(source); + qDebug(qPrintable(source)); + DecoderFactory *fact = 0; + + StreamReader* reader = qobject_cast<StreamReader *>(input); + if(reader) + fact = Decoder::findByContentType(reader->contentType()); + else + fact = Decoder::findFactory(source); if (fact) { decoder = fact->create(parent, input, output); @@ -159,6 +166,26 @@ DecoderFactory *Decoder::findFactory(const QString& source) return 0; } +DecoderFactory *Decoder::findByContentType(const QString& type) +{ + checkFactories(); + for (int i=0; i<factories->size(); ++i) + { + if (!blacklist.contains(files.at(i).section('/',-1))) + { + QStringList types = factories->at(i)->contentTypes(); + for(int j=0; j<types.size(); ++j) + { + qDebug(qPrintable(types[j]+" "+type)); + if(type == types[j]) + return factories->at(i); + } + } + } + qDebug("Decoder: unable to find factory"); + return 0; +} + FileTag *Decoder::createTag(const QString& source) { DecoderFactory *fact = Decoder::findFactory(source); diff --git a/lib/decoder.h b/lib/decoder.h index c7b099067..8f2649366 100644 --- a/lib/decoder.h +++ b/lib/decoder.h @@ -12,6 +12,7 @@ #include <QMutex> #include <QWaitCondition> #include <QObject> +#include <QStringList> #include "fileinfo.h" @@ -126,6 +127,7 @@ public: //static void registerFactory(DecoderFactory *); static Decoder *create(QObject *, const QString &, QIODevice *, Output *); static DecoderFactory *findFactory(const QString&); + static DecoderFactory *findByContentType(const QString&); static FileTag *createTag(const QString&); static QString filter(); static QStringList nameFilters(); diff --git a/lib/decoderfactory.h b/lib/decoderfactory.h index 9fd5b55cd..4e030f43e 100644 --- a/lib/decoderfactory.h +++ b/lib/decoderfactory.h @@ -30,6 +30,7 @@ class QTranslator; class Decoder; class Output; class FileTag; +class QStringList; class DecoderFactory { @@ -39,6 +40,7 @@ public: virtual const QString &name() const = 0; virtual const QString &filter() const = 0; virtual const QString &description() const = 0; //i.e. file description + virtual const QStringList &contentTypes() const = 0; virtual Decoder *create(QObject *, QIODevice *, Output *) = 0; virtual FileTag *createTag(const QString &source) = 0; virtual void showDetails(QWidget *parent, const QString &path) = 0; diff --git a/lib/downloader.cpp b/lib/downloader.cpp new file mode 100644 index 000000000..be2d93e27 --- /dev/null +++ b/lib/downloader.cpp @@ -0,0 +1,206 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ilya Kotov * + * forkotov02@hotmail.ru * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ +#include "downloader.h" + +static int stop; + +//curl callbacks +static size_t curl_write_data(void *data, size_t size, size_t nmemb, + void *pointer) +{ + Downloader *dl = (Downloader *)pointer; + dl->mutex()->lock (); + size_t buf_start = dl->stream()->buf_fill; + size_t data_size = size * nmemb; + dl->stream()->buf_fill += data_size; + + dl->stream()->buf = (char *)realloc (dl->stream()->buf, dl->stream()->buf_fill); + memcpy (dl->stream()->buf + buf_start, data, data_size); + dl->mutex()->unlock(); + return data_size; +} + +static size_t curl_header(void *data, size_t size, size_t nmemb, + void *pointer) +{ + Downloader *dl = (Downloader *)pointer; + dl->mutex()->lock (); + size_t data_size = size * nmemb; + if (data_size < 2) + return data_size; + + //qDebug("header received: %s", (char*) data); + QString str = QString::fromAscii((char *) data, data_size); + str = str.trimmed (); + if (str.contains("Content-Type")) + { + str = str.right(str.indexOf(":") + 3); + qDebug(qPrintable(QString("content-type: ")+str)); + dl->stream()->content_type = str; + } + if (str.contains("content-type")) + { + str = str.right(str.indexOf(":")-2); + qDebug(qPrintable(QString("content-type: ")+str)); + dl->stream()->content_type = str; + } + dl->mutex()->unlock(); + return size * nmemb; +} + +int curl_progress(void *pointer, double dltotal, double dlnow, double ultotal, double ulnow) +{ + Downloader *dl = (Downloader *)pointer; + dl->mutex()->lock (); + bool aborted = dl->stream()->aborted; + dl->mutex()->unlock(); + if (aborted) + return -1; + return 0; +} + +Downloader::Downloader(QObject *parent, const QString &url) + : QThread(parent) +{ + m_url = url; + qDebug("Downloader: url: %s",qPrintable(url)); + curl_global_init(CURL_GLOBAL_ALL); + m_stream.buf_fill = 0; + m_stream.buf = 0; + + m_stream.aborted = TRUE; +} + + +Downloader::~Downloader() +{ + abort(); +} + + +qint64 Downloader::read(char* data, qint64 maxlen) +{ + m_mutex.lock(); + if (m_stream.buf_fill>0 && !m_stream.aborted) + { + int len = qMin<qint64>(m_stream.buf_fill, maxlen); + memcpy(data, m_stream.buf, len); + m_stream.buf_fill -= len; + memmove(m_stream.buf, m_stream.buf + len, m_stream.buf_fill); + m_mutex.unlock(); + return len; + } + m_mutex.unlock(); + return 0; +} + +Stream *Downloader::stream() +{ + return &m_stream; +} + +QMutex *Downloader::mutex() +{ + return &m_mutex; +} + +QString Downloader::contentType() +{ + return m_stream.content_type; +} + +void Downloader::abort() +{ + m_mutex.lock(); + + if (m_stream.aborted) + { + m_mutex.unlock(); + return; + } + m_stream.aborted = TRUE; + m_mutex.unlock(); + wait(); + curl_easy_cleanup(m_handle); +} + +int Downloader::bytesAvailable() +{ + m_mutex.lock(); + int b = m_stream.buf_fill; + m_mutex.unlock(); + return b; +} + +void Downloader::run() +{ + qDebug("Downloader: starting download thread"); + m_handle = curl_easy_init(); + // Set url to download + curl_easy_setopt(m_handle, CURLOPT_URL, m_url.toAscii().constData()); + //qDebug("Downloader: url: %s", qPrintable(url)); + // callback for wrting + curl_easy_setopt(m_handle, CURLOPT_WRITEFUNCTION, curl_write_data); + // Set destination file + curl_easy_setopt(m_handle, CURLOPT_WRITEDATA, this); + curl_easy_setopt(m_handle, CURLOPT_HEADERDATA, this); + curl_easy_setopt(m_handle, CURLOPT_HEADERFUNCTION, curl_header); + // Some SSL mambo jambo + curl_easy_setopt(m_handle, CURLOPT_SSL_VERIFYPEER, FALSE); + curl_easy_setopt(m_handle, CURLOPT_SSL_VERIFYHOST, 0); + // Disable progress meter + curl_easy_setopt(m_handle, CURLOPT_NOPROGRESS, 0); + + curl_easy_setopt(m_handle, CURLOPT_PROGRESSDATA, this); + curl_easy_setopt(m_handle, CURLOPT_PROGRESSFUNCTION, curl_progress); + // Any kind of authentication + curl_easy_setopt(m_handle, CURLOPT_HTTPAUTH, CURLAUTH_ANY); + //curl_easy_setopt(m_handle, CURLOPT_VERBOSE, 1); + // Auto referrer + curl_easy_setopt(m_handle, CURLOPT_AUTOREFERER, 1); + // Follow redirections + curl_easy_setopt(m_handle, CURLOPT_FOLLOWLOCATION, 1); + // user agent + curl_easy_setopt(m_handle, CURLOPT_USERAGENT, "qmmp/0.2"); + curl_easy_setopt(m_handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); + + struct curl_slist *http200_aliases = curl_slist_append(NULL, "ICY"); + struct curl_slist *http_headers = curl_slist_append(NULL, "Icy-MetaData: 1"); + curl_easy_setopt(m_handle, CURLOPT_HTTP200ALIASES, http200_aliases); + curl_easy_setopt(m_handle, CURLOPT_HTTPHEADER, http_headers); + m_mutex.lock(); + m_stream.buf_fill = 0; + m_stream.buf = 0; + m_stream.aborted = FALSE; + int return_code, response; + qDebug("Downloader: starting libcurl"); + m_mutex.unlock(); + return_code = curl_easy_perform(m_handle); + //qDebug("curl_easy_perform %d", return_code); + + m_mutex.lock(); + m_stream.aborted = TRUE; + m_stream.buf_fill = 0; + if (m_stream.buf) + delete m_stream.buf; + m_stream.buf = 0; + m_mutex.unlock(); + qDebug("Downloader: thread exited"); +} diff --git a/lib/downloader.h b/lib/downloader.h new file mode 100644 index 000000000..87ed03cfd --- /dev/null +++ b/lib/downloader.h @@ -0,0 +1,67 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ilya Kotov * + * forkotov02@hotmail.ru * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ +#ifndef DOWNLOADER_H +#define DOWNLOADER_H + +#include <QThread> +#include <QMutex> +#include <QByteArray> + +#include <curl/curl.h> + +/** + @author Ilya Kotov <forkotov02@hotmail.ru> +*/ + +struct Stream +{ + char *buf; + int buf_fill; + QString content_type; + bool aborted; +}; + +class Downloader : public QThread +{ +Q_OBJECT +public: + Downloader(QObject *parent, const QString &url); + + ~Downloader(); + + qint64 read(char* data, qint64 maxlen); + Stream *stream(); + QMutex *mutex(); + QString contentType(); + void abort(); + int bytesAvailable(); + +private: + CURL *m_handle; + QMutex m_mutex; + Stream m_stream; + QString m_url; + +protected: + void run(); + +}; + +#endif diff --git a/lib/lib.pro b/lib/lib.pro index 0bf2eb9bc..3ae6892c1 100644 --- a/lib/lib.pro +++ b/lib/lib.pro @@ -16,7 +16,8 @@ HEADERS += recycler.h \ decoderfactory.h \ soundcore.h \ visualization.h \ - streamreader.h + streamreader.h \ + downloader.h SOURCES += recycler.cpp \ decoder.cpp \ output.cpp \ @@ -24,17 +25,17 @@ SOURCES += recycler.cpp \ equ\iir_cfs.c \ equ\iir_fpu.c \ soundcore.cpp \ - streamreader.cpp + streamreader.cpp \ + downloader.cpp -QT += network TARGET = qmmp CONFIG += release \ warn_on \ qt \ -thread +thread \ +link_pkgconfig TEMPLATE = lib +PKGCONFIG += libcurl target.path = /lib INSTALLS += target - - diff --git a/lib/qmmp/Input/ffmpeg/decoderffmpegfactory.cpp b/lib/qmmp/Input/ffmpeg/decoderffmpegfactory.cpp index 0f4d4ed2b..7b7579e35 100644 --- a/lib/qmmp/Input/ffmpeg/decoderffmpegfactory.cpp +++ b/lib/qmmp/Input/ffmpeg/decoderffmpegfactory.cpp @@ -10,8 +10,8 @@ bool DecoderFFmpegFactory::supports(const QString &source) const { - - return (source.right(4).toLower() == ".wma"); + + return (source.right(4).toLower() == ".wma" || source.right(4).toLower() == ".wav"); } const QString &DecoderFFmpegFactory::name() const @@ -23,7 +23,7 @@ const QString &DecoderFFmpegFactory::name() const const QString &DecoderFFmpegFactory::filter() const { - static QString filter("*.wma"); + static QString filter("*.wma *.wav"); return filter; } @@ -34,9 +34,15 @@ const QString &DecoderFFmpegFactory::description() const return desc; } +const QStringList &DecoderFFmpegFactory::contentTypes() const +{ + static QStringList types; + //types << "" << ""; + return types; +} Decoder *DecoderFFmpegFactory::create(QObject *parent, QIODevice *input, - Output *output) + Output *output) { return new DecoderFFmpeg(parent, this, input, output); } diff --git a/lib/qmmp/Input/ffmpeg/decoderffmpegfactory.h b/lib/qmmp/Input/ffmpeg/decoderffmpegfactory.h index 6eeef53cd..5b5930ad5 100644 --- a/lib/qmmp/Input/ffmpeg/decoderffmpegfactory.h +++ b/lib/qmmp/Input/ffmpeg/decoderffmpegfactory.h @@ -44,6 +44,7 @@ public: const QString &name() const; const QString &filter() const; const QString &description() const; + const QStringList &contentTypes() const; Decoder *create(QObject *, QIODevice *, Output *); FileTag *createTag(const QString &source); void showDetails(QWidget *parent, const QString &path); diff --git a/lib/qmmp/Input/flac/decoderflacfactory.cpp b/lib/qmmp/Input/flac/decoderflacfactory.cpp index e8214924e..fe8bcf362 100644 --- a/lib/qmmp/Input/flac/decoderflacfactory.cpp +++ b/lib/qmmp/Input/flac/decoderflacfactory.cpp @@ -36,6 +36,12 @@ const QString &DecoderFLACFactory::description() const return desc; } +const QStringList &DecoderFLACFactory::contentTypes() const +{ + static QStringList types; + //types << "" << ""; + return types; +} Decoder *DecoderFLACFactory::create(QObject *parent, QIODevice *input, Output *output) diff --git a/lib/qmmp/Input/flac/decoderflacfactory.h b/lib/qmmp/Input/flac/decoderflacfactory.h index ea5d62068..56c95b248 100644 --- a/lib/qmmp/Input/flac/decoderflacfactory.h +++ b/lib/qmmp/Input/flac/decoderflacfactory.h @@ -44,6 +44,7 @@ public: const QString &name() const; const QString &filter() const; const QString &description() const; + const QStringList &contentTypes() const; Decoder *create(QObject *, QIODevice *, Output *); FileTag *createTag(const QString &source); void showDetails(QWidget *parent, const QString &path); diff --git a/lib/qmmp/Input/mad/decodermadfactory.cpp b/lib/qmmp/Input/mad/decodermadfactory.cpp index a511dc995..8c5f15305 100644 --- a/lib/qmmp/Input/mad/decodermadfactory.cpp +++ b/lib/qmmp/Input/mad/decodermadfactory.cpp @@ -40,6 +40,13 @@ const QString &DecoderMADFactory::description() const return desc; } +const QStringList &DecoderMADFactory::contentTypes() const +{ + static QStringList types; + types << "audio/mp3" << "audio/mpeg"; + return types; +} + Decoder *DecoderMADFactory::create(QObject *parent, QIODevice *input, Output *output) { return new DecoderMAD(parent, this, input, output); diff --git a/lib/qmmp/Input/mad/decodermadfactory.h b/lib/qmmp/Input/mad/decodermadfactory.h index 9becdf022..a13596c8f 100644 --- a/lib/qmmp/Input/mad/decodermadfactory.h +++ b/lib/qmmp/Input/mad/decodermadfactory.h @@ -43,6 +43,7 @@ public: const QString &name() const; const QString &filter() const; // file extension, ie. ".mp3" or ".ogg" const QString &description() const; // file type, ie. "MPEG Audio Files" + const QStringList &contentTypes() const; Decoder *create(QObject *, QIODevice *, Output *); FileTag *createTag(const QString &source); void showDetails(QWidget *parent, const QString &path); diff --git a/lib/qmmp/Input/mpc/decodermpcfactory.cpp b/lib/qmmp/Input/mpc/decodermpcfactory.cpp index 98b537045..f41090a02 100644 --- a/lib/qmmp/Input/mpc/decodermpcfactory.cpp +++ b/lib/qmmp/Input/mpc/decodermpcfactory.cpp @@ -36,6 +36,12 @@ const QString &DecoderMPCFactory::description() const return desc; } +const QStringList &DecoderMPCFactory::contentTypes() const +{ + static QStringList types; + //types << "" << ""; + return types; +} Decoder *DecoderMPCFactory::create(QObject *parent, QIODevice *input, Output *output) diff --git a/lib/qmmp/Input/mpc/decodermpcfactory.h b/lib/qmmp/Input/mpc/decodermpcfactory.h index 651e76e18..aed846358 100644 --- a/lib/qmmp/Input/mpc/decodermpcfactory.h +++ b/lib/qmmp/Input/mpc/decodermpcfactory.h @@ -44,6 +44,7 @@ public: const QString &name() const; const QString &filter() const; const QString &description() const; + const QStringList &contentTypes() const; Decoder *create(QObject *, QIODevice *, Output *); FileTag *createTag(const QString &source); void showDetails(QWidget *parent, const QString &path); diff --git a/lib/qmmp/Input/vorbis/decodervorbisfactory.cpp b/lib/qmmp/Input/vorbis/decodervorbisfactory.cpp index e4f77b191..b9c652a8d 100644 --- a/lib/qmmp/Input/vorbis/decodervorbisfactory.cpp +++ b/lib/qmmp/Input/vorbis/decodervorbisfactory.cpp @@ -35,6 +35,13 @@ const QString &DecoderVorbisFactory::description() const return desc; } +const QStringList &DecoderVorbisFactory::contentTypes() const +{ + static QStringList types; + types << "application/ogg" << "audio/x-vorbis+ogg"; + return types; +} + Decoder *DecoderVorbisFactory::create(QObject *parent, QIODevice *input, Output *output) { diff --git a/lib/qmmp/Input/vorbis/decodervorbisfactory.h b/lib/qmmp/Input/vorbis/decodervorbisfactory.h index 9c411c49d..f699830ce 100644 --- a/lib/qmmp/Input/vorbis/decodervorbisfactory.h +++ b/lib/qmmp/Input/vorbis/decodervorbisfactory.h @@ -44,6 +44,7 @@ public: const QString &name() const; const QString &filter() const; // file extension, ie. ".mp3" or ".ogg" const QString &description() const; // file type, ie. "MPEG Audio Files" + const QStringList &contentTypes() const; Decoder *create(QObject *, QIODevice *, Output *); FileTag *createTag(const QString &source); void showDetails(QWidget *parent, const QString &path); diff --git a/lib/soundcore.cpp b/lib/soundcore.cpp index 4f295d133..4e7bbf205 100644 --- a/lib/soundcore.cpp +++ b/lib/soundcore.cpp @@ -75,7 +75,6 @@ bool SoundCore::play(const QString &source) if(source.left(4) == "http") { m_input = new StreamReader(source, this); - //m_input->open(QIODevice::ReadOnly); } else m_input = new QFile(source); diff --git a/lib/streamreader.cpp b/lib/streamreader.cpp index 95df30d9d..a72d85363 100644 --- a/lib/streamreader.cpp +++ b/lib/streamreader.cpp @@ -18,36 +18,20 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include <QApplication> -#include <QHttp> #include <QUrl> +#include "downloader.h" #include "streamreader.h" StreamReader::StreamReader(const QString &name, QObject *parent) : QIODevice(parent) { - qDebug(qPrintable(name)); - m_http = new QHttp(this); - - m_pos = 0; - - connect(m_http, SIGNAL(requestFinished(int, bool)), - this, SLOT(httpRequestFinished(int, bool))); - connect(m_http, SIGNAL(dataReadProgress(int, int)), - this, SLOT(updateDataReadProgress(int, int))); - connect(m_http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), - this, SLOT(readResponseHeader(const QHttpResponseHeader &))); - - m_url = QUrl(name); - - m_http->setHost(m_url.host(), m_url.port() != -1 ? m_url.port() : 80); - if (!m_url.userName().isEmpty()) - m_http->setUser(m_url.userName(), m_url.password()); + m_downloader = new Downloader(this, name); } StreamReader::~StreamReader() { - m_http->abort(); + m_downloader->abort(); } bool StreamReader::atEnd () const @@ -57,7 +41,7 @@ bool StreamReader::atEnd () const qint64 StreamReader::bytesAvailable () const { - return m_http->bytesAvailable (); + return m_downloader->bytesAvailable (); } qint64 StreamReader::bytesToWrite () const @@ -72,8 +56,9 @@ bool StreamReader::canReadLine () const void StreamReader::close () { - m_httpRequestAborted = TRUE; - m_http->close(); + //m_httpRequestAborted = TRUE; + //m_http->close(); + m_downloader->abort(); } bool StreamReader::isSequential () const @@ -86,8 +71,8 @@ bool StreamReader::open ( OpenMode mode ) if (mode != QIODevice::ReadOnly) return FALSE; downloadFile(); - if (m_httpRequestAborted) - return FALSE; + //if (m_httpRequestAborted) + //return TRUE; setOpenMode(QIODevice::ReadOnly); return TRUE; } @@ -126,7 +111,7 @@ bool StreamReader::waitForReadyRead ( int msecs ) qint64 StreamReader::readData(char* data, qint64 maxlen) { - return m_http->read (data, maxlen); + return m_downloader->read (data, maxlen); } qint64 StreamReader::writeData(const char*, qint64) @@ -137,12 +122,20 @@ qint64 StreamReader::writeData(const char*, qint64) void StreamReader::downloadFile() { m_httpRequestAborted = FALSE; - qDebug("StreamReader: connecting..."); - m_httpGetId = m_http->get(m_url.path(), 0); + //qDebug("StreamReader: connecting..."); + //m_httpGetId = m_http->get(m_url.path(), 0); qDebug("StreamReader: buffering..."); - while (m_http->bytesAvailable () < BUFFER_SIZE*0.5 && !m_httpRequestAborted) + /*while (m_http->bytesAvailable () < BUFFER_SIZE*0.5 && !m_httpRequestAborted) + { + qApp->processEvents(); + }*/ + m_downloader->start(); + while (m_downloader->bytesAvailable () < BUFFER_SIZE*0.5 && + m_downloader->isRunning()) { qApp->processEvents(); + //qDebug("StreamReader: bytes: %d",m_downloader->bytesAvailable ()); + //sleep(1); } qDebug("StreamReader: ready"); } @@ -150,7 +143,7 @@ void StreamReader::downloadFile() void StreamReader::cancelDownload() { m_httpRequestAborted = true; - m_http->abort(); + //&& !m_httpRequestAborted->abort(); } void StreamReader::httpRequestFinished(int requestId, bool error) @@ -165,7 +158,8 @@ void StreamReader::httpRequestFinished(int requestId, bool error) if (error) { - qDebug(qPrintable(QString("StreamReader: %1").arg(m_http->errorString()))); + //qDebug(qPrintable(QString("StreamReader: %1").arg(m_http->errorString()))); + m_httpRequestAborted = TRUE; } else { @@ -174,18 +168,6 @@ void StreamReader::httpRequestFinished(int requestId, bool error) } -void StreamReader::readResponseHeader(const QHttpResponseHeader &responseHeader) -{ - m_contentType = responseHeader.contentType (); - if (responseHeader.statusCode() != 200) - { - qDebug(qPrintable(QString("Download failed: %1.") .arg(responseHeader.reasonPhrase()))); - m_httpRequestAborted = true; - m_http->abort(); - return; - } -} - void StreamReader::updateDataReadProgress(int bytesRead, int totalBytes) { m_pos = bytesRead; @@ -195,7 +177,7 @@ void StreamReader::updateDataReadProgress(int bytesRead, int totalBytes) void StreamReader::fillBuffer() { - if (m_http->bytesAvailable () > BUFFER_SIZE && !m_httpRequestAborted) + /*if (m_http->bytesAvailable () > BUFFER_SIZE && !m_httpRequestAborted) { while (m_http->bytesAvailable () > BUFFER_SIZE*0.75 && !m_httpRequestAborted) { @@ -205,22 +187,22 @@ void StreamReader::fillBuffer() qApp->processEvents(); delete data; } - } + }*/ } const QString &StreamReader::contentType() { - if(!m_contentType.isEmpty()) - return m_contentType; - m_httpRequestAborted = FALSE; - qDebug("StreamReader: reading content type"); - m_httpGetId = m_http->get(m_url.path(), 0); - while (m_contentType.isEmpty() && !m_httpRequestAborted) + //Downloader* dw = new Downloader(this, "http://127.0.0.1:8000/"); + m_downloader->start(); + while (m_contentType.isEmpty() && m_downloader->isRunning()) { + m_downloader->mutex()->lock (); + m_contentType = m_downloader->contentType(); + m_downloader->mutex()->unlock(); qApp->processEvents(); } - qDebug("StreamReader: content type: %s", qPrintable(m_contentType)); - //m_http->abort(); - m_http->close(); + //m_contentType = dw->contentType(); + m_downloader->abort(); + qDebug("StreamReader: content type: %s", qPrintable(m_contentType)); return m_contentType; } diff --git a/lib/streamreader.h b/lib/streamreader.h index a469932b7..2489402ed 100644 --- a/lib/streamreader.h +++ b/lib/streamreader.h @@ -22,12 +22,13 @@ #include <QObject> #include <QIODevice> -#include <QHttp> #include <QUrl> #define BUFFER_SIZE 524288 class QFileInfo; +class Downloader; + /** @author Ilya Kotov <forkotov02@hotmail.ru> */ @@ -70,18 +71,17 @@ private slots: void downloadFile(); void cancelDownload(); void httpRequestFinished(int, bool); - void readResponseHeader(const QHttpResponseHeader &responseHeader); void updateDataReadProgress(int bytesRead, int totalBytes); private: void fillBuffer(); QUrl m_url; - QHttp* m_http; bool m_httpRequestAborted; int m_httpGetId; int m_pos; int m_size; QString m_contentType; + Downloader *m_downloader; }; #endif |
