#include "subtitlebrowserpage.h" #include "subtitleparser.h" #include "qtiocompressor.h" #include #include #include #include #include #include #include #include #include #include SubtitleHolder::SubtitleHolder(QWidget *parent) : QWidget(parent) { QHBoxLayout *layout = new QHBoxLayout; m_label = new QLabel; layout->addWidget(m_label); setLayout(layout); setSubtitleParser(0); } void SubtitleHolder::setSubtitleParser(SubtitleParser *parser) { if (parser == 0) { m_label->setText(tr("No subtitle is selected.")); m_subtitleParser = 0; emit subtitleParserChanged(); return; } m_subtitleParser = parser; m_label->setText(tr("Selected: %1.").arg(parser->fileName())); emit subtitleParserChanged(); } void SubtitleHolder::setErrorMessage(const QString &error) { m_label->setText(QString("%1").arg(error)); } void SubtitleHolder::setDownloading() { m_label->setText(tr("Downloading...")); } SubtitleParser* SubtitleHolder::subtitleParser() const { return m_subtitleParser; } SubtitleBrowserPage::SubtitleBrowserPage(QWidget *parent) : QWizardPage(parent), m_downloadReply(0) { setTitle(tr("Subtitle Selection")); setSubTitle(tr("Browse for the subtitle on the web")); QVBoxLayout *layout = new QVBoxLayout; m_subtitleHolder = new SubtitleHolder; registerField("subtitle*", m_subtitleHolder, "subtitleParser", SIGNAL(subtitleParserChanged())); m_browser = new QWebView; m_browser->page()->setForwardUnsupportedContent(true); connect(m_browser->page()->networkAccessManager(), SIGNAL(finished(QNetworkReply*)), this, SLOT(subtitleDownloaded(QNetworkReply*))); connect(m_browser->page(), SIGNAL(unsupportedContent(QNetworkReply*)), this, SLOT(subtitleRequested(QNetworkReply*))); layout->addWidget(m_browser); layout->addWidget(m_subtitleHolder); setLayout(layout); } void SubtitleBrowserPage::initializePage() { m_subtitleHolder->setSubtitleParser(0); QUrl url(QString("http://subscene.com/filmsearch.aspx")); url.addQueryItem(QLatin1String("q"), field("movieTitle").toString()); m_browser->load(url); } void SubtitleBrowserPage::subtitleRequested(QNetworkReply *reply) { if (reply->url().path().contains(QString("dlpath"))) { m_subtitleHolder->setDownloading(); if (reply->header(QNetworkRequest::ContentTypeHeader) == QLatin1String("application/zip")) { m_downloadReply = reply; } else { m_downloadReply = 0; reply->close(); m_subtitleHolder->setErrorMessage(tr("Only zip files are current supported.")); } } else { m_downloadReply = 0; reply->close(); } } void SubtitleBrowserPage::subtitleDownloaded(QNetworkReply *reply) { if (m_downloadReply != reply) return; bool foundSrt = false; SubtitleParser *subtitleData = 0; forever { // Zip format "local file header" fields: quint32 signature, crc, compSize, unCompSize; quint16 extractVersion, bitFlag, compMethod, modTime, modDate; quint16 nameLen, extraLen; QDataStream s(m_downloadReply); s.setByteOrder(QDataStream::LittleEndian); s >> signature; if (signature != 0x04034b50) // zip local file header magic number break; if (foundSrt) { m_subtitleHolder->setErrorMessage(tr("This zip file has multiple subtitle files. Please choose one that only has one subtitle file.")); return; } s >> extractVersion >> bitFlag >> compMethod; s >> modTime >> modDate >> crc >> compSize >> unCompSize; s >> nameLen >> extraLen; const QByteArray fileName = m_downloadReply->read(nameLen); m_downloadReply->read(extraLen); QByteArray compData = m_downloadReply->read(compSize); if (fileName.toLower().endsWith(".srt")) foundSrt = true; else break; if (compMethod == 0) { QBuffer uncompBuf(&compData); subtitleData = new SubtitleParser(&uncompBuf, fileName); } else { QBuffer compBuf(&compData); QtIOCompressor compressor(&compBuf); compressor.setStreamFormat(QtIOCompressor::RawZipFormat); compressor.open(QIODevice::ReadOnly); subtitleData = new SubtitleParser(&compressor, fileName); } } m_subtitleHolder->setSubtitleParser(subtitleData); if (subtitleData == 0) { m_subtitleHolder->setErrorMessage(tr("There were no srt files in this zip. Please choose another one.")); return; } }