aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortrialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38>2007-07-17 11:11:31 +0000
committertrialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38>2007-07-17 11:11:31 +0000
commit6eead162266621160c78c0b06b904cc5955c29af (patch)
tree0e3a05f0ac71336b5e57ecbd0988495f6ab8c3bc
parent45f2ee21a34d1899dd45e76061d77ad96ab63404 (diff)
downloadqmmp-6eead162266621160c78c0b06b904cc5955c29af.tar.gz
qmmp-6eead162266621160c78c0b06b904cc5955c29af.tar.bz2
qmmp-6eead162266621160c78c0b06b904cc5955c29af.zip
added contentType() function for StreamReader class
git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@27 90c681e8-e032-0410-971d-27865f9a5e38
-rw-r--r--lib/streamreader.cpp88
-rw-r--r--lib/streamreader.h22
2 files changed, 69 insertions, 41 deletions
diff --git a/lib/streamreader.cpp b/lib/streamreader.cpp
index c3b69161b..95df30d9d 100644
--- a/lib/streamreader.cpp
+++ b/lib/streamreader.cpp
@@ -26,24 +26,30 @@
StreamReader::StreamReader(const QString &name, QObject *parent)
: QIODevice(parent)
{
-
- m_name =name;
qDebug(qPrintable(name));
- http = new QHttp(this);
+ m_http = new QHttp(this);
+
+ m_pos = 0;
- connect(http, SIGNAL(requestFinished(int, bool)),
+ connect(m_http, SIGNAL(requestFinished(int, bool)),
this, SLOT(httpRequestFinished(int, bool)));
- connect(http, SIGNAL(dataReadProgress(int, int)),
+ connect(m_http, SIGNAL(dataReadProgress(int, int)),
this, SLOT(updateDataReadProgress(int, int)));
- connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
+ 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());
}
+
StreamReader::~StreamReader()
{
- http->abort();
+ m_http->abort();
}
-
bool StreamReader::atEnd () const
{
return FALSE;
@@ -51,7 +57,7 @@ bool StreamReader::atEnd () const
qint64 StreamReader::bytesAvailable () const
{
- return http->bytesAvailable ();
+ return m_http->bytesAvailable ();
}
qint64 StreamReader::bytesToWrite () const
@@ -66,8 +72,8 @@ bool StreamReader::canReadLine () const
void StreamReader::close ()
{
- httpRequestAborted = TRUE;
- http->close();
+ m_httpRequestAborted = TRUE;
+ m_http->close();
}
bool StreamReader::isSequential () const
@@ -80,7 +86,7 @@ bool StreamReader::open ( OpenMode mode )
if (mode != QIODevice::ReadOnly)
return FALSE;
downloadFile();
- if (httpRequestAborted)
+ if (m_httpRequestAborted)
return FALSE;
setOpenMode(QIODevice::ReadOnly);
return TRUE;
@@ -88,7 +94,7 @@ bool StreamReader::open ( OpenMode mode )
qint64 StreamReader::pos () const
{
- return 0;
+ return m_pos;
}
bool StreamReader::reset ()
@@ -120,7 +126,7 @@ bool StreamReader::waitForReadyRead ( int msecs )
qint64 StreamReader::readData(char* data, qint64 maxlen)
{
- return http->read (data, maxlen);
+ return m_http->read (data, maxlen);
}
qint64 StreamReader::writeData(const char*, qint64)
@@ -130,18 +136,11 @@ qint64 StreamReader::writeData(const char*, qint64)
void StreamReader::downloadFile()
{
- QUrl url(m_name);
-
- http->setHost(url.host(), url.port() != -1 ? url.port() : 80);
- if (!url.userName().isEmpty())
- http->setUser(url.userName(), url.password());
-
- httpRequestAborted = false;
- f = false;
+ m_httpRequestAborted = FALSE;
qDebug("StreamReader: connecting...");
- httpGetId = http->get(url.path(), 0);
+ m_httpGetId = m_http->get(m_url.path(), 0);
qDebug("StreamReader: buffering...");
- while (http->bytesAvailable () < BUFFER_SIZE*0.5 && !httpRequestAborted)
+ while (m_http->bytesAvailable () < BUFFER_SIZE*0.5 && !m_httpRequestAborted)
{
qApp->processEvents();
}
@@ -150,23 +149,23 @@ void StreamReader::downloadFile()
void StreamReader::cancelDownload()
{
- httpRequestAborted = true;
- http->abort();
+ m_httpRequestAborted = true;
+ m_http->abort();
}
void StreamReader::httpRequestFinished(int requestId, bool error)
{
- if (httpRequestAborted)
+ if (m_httpRequestAborted)
{
return;
}
- if (requestId != httpGetId)
+ if (requestId != m_httpGetId)
return;
if (error)
{
- qDebug(qPrintable(QString("StreamReader: %1").arg(http->errorString())));
+ qDebug(qPrintable(QString("StreamReader: %1").arg(m_http->errorString())));
}
else
{
@@ -177,32 +176,51 @@ 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())));
- httpRequestAborted = true;
- http->abort();
+ m_httpRequestAborted = true;
+ m_http->abort();
return;
}
}
void StreamReader::updateDataReadProgress(int bytesRead, int totalBytes)
{
+ m_pos = bytesRead;
+ m_size = totalBytes;
fillBuffer();
- //qDebug("%d, %d, %d",bytesRead,totalBytes,http->bytesAvailable ());
}
void StreamReader::fillBuffer()
{
- if (http->bytesAvailable () > BUFFER_SIZE && !httpRequestAborted)
+ if (m_http->bytesAvailable () > BUFFER_SIZE && !m_httpRequestAborted)
{
- while (http->bytesAvailable () > BUFFER_SIZE*0.75 && !httpRequestAborted)
+ while (m_http->bytesAvailable () > BUFFER_SIZE*0.75 && !m_httpRequestAborted)
{
qDebug("StreamReader: skipping data...");
char *data = new char[BUFFER_SIZE/20];
- http->read (data, BUFFER_SIZE/20);
+ m_http->read (data, BUFFER_SIZE/20);
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)
+ {
+ qApp->processEvents();
+ }
+ qDebug("StreamReader: content type: %s", qPrintable(m_contentType));
+ //m_http->abort();
+ m_http->close();
+ return m_contentType;
+}
diff --git a/lib/streamreader.h b/lib/streamreader.h
index f8bcc753c..a469932b7 100644
--- a/lib/streamreader.h
+++ b/lib/streamreader.h
@@ -23,9 +23,9 @@
#include <QObject>
#include <QIODevice>
#include <QHttp>
+#include <QUrl>
#define BUFFER_SIZE 524288
-class QUrl;
class QFileInfo;
/**
@@ -39,6 +39,9 @@ public:
~StreamReader();
+ /**
+ * QIODevice API
+ */
bool atEnd () const;
qint64 bytesAvailable () const;
qint64 bytesToWrite () const;
@@ -53,6 +56,11 @@ public:
bool waitForBytesWritten ( int msecs );
bool waitForReadyRead ( int msecs );
+ /**
+ * returns content type of a stream
+ */
+ const QString &contentType();
+
protected:
qint64 readData(char*, qint64);
qint64 writeData(const char*, qint64);
@@ -67,11 +75,13 @@ private slots:
private:
void fillBuffer();
- QString m_name;
- QHttp *http;
- bool httpRequestAborted;
- int httpGetId;
- bool f;
+ QUrl m_url;
+ QHttp* m_http;
+ bool m_httpRequestAborted;
+ int m_httpGetId;
+ int m_pos;
+ int m_size;
+ QString m_contentType;
};
#endif