1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
#include "subtitlebrowserpage.h"
#include "subtitleparser.h"
#include "qtiocompressor.h"
#include <QVBoxLayout>
#include <QtWebKit>
#include <QNetworkReply>
#include <QMessageBox>
#include <QDataStream>
#include <QTextStream>
#include <QByteArray>
#include <QBuffer>
#include <QTextEdit>
#include <QLabel>
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("<big>No subtitle is selected.</big>"));
m_subtitleParser = 0;
emit subtitleParserChanged();
return;
}
m_subtitleParser = parser;
m_label->setText(tr("<big><font color=green>Selected: %1.</font></big>").arg(parser->fileName()));
emit subtitleParserChanged();
}
void SubtitleHolder::setErrorMessage(const QString &error)
{
m_label->setText(QString("<big><font color=red>%1</font></big>").arg(error));
}
void SubtitleHolder::setDownloading()
{
m_label->setText(tr("<big>Downloading...</big>"));
}
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;
}
}
|