2 * abby Copyright (C) 2009 Toni Gundogdu.
3 * This file is part of abby.
5 * abby is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * abby is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <QMessageBox>
26 ScanDialog::ScanDialog(QWidget
*parent
)
27 : QDialog(parent
), titleMode(false)
33 mgr
= createManager();
35 itemsTree
->setColumnHidden(1, true);
39 ScanDialog::onScan() {
40 QString lnk
= linkEdit
->text();
47 if (!lnk
.startsWith("http://", Qt::CaseInsensitive
))
48 lnk
.insert(0,"http://");
54 linkEdit
->setEnabled (false);
55 scanButton
->setEnabled (false);
56 titlesBox
->setEnabled (false);
57 buttonBox
->setEnabled (false);
59 mgr
->get(QNetworkRequest(lnk
));
63 ScanDialog::replyFinished(QNetworkReply
* reply
) {
67 if (reply
->error() == QNetworkReply::NoError
) {
69 handleRedirect(reply
);
71 if (!redirectedToURL
.isEmpty())
72 mgr
->get( QNetworkRequest(redirectedToURL
) );
77 parseHtmlTitle(reply
);
78 redirectedToURL
.clear();
83 QMessageBox::critical(this, QCoreApplication::applicationName(),
84 QString(tr("Network error: %1")).arg(reply
->errorString()));
89 linkEdit
->setEnabled (state
);
90 scanButton
->setEnabled (state
);
91 titlesBox
->setEnabled (state
);
92 buttonBox
->setEnabled (state
);
99 matchScanContent (const QStringList
& lst
, QRegExp
& re
, const QString
& content
) {
101 re
.setCaseSensitivity(Qt::CaseInsensitive
);
107 while ((pos
= re
.indexIn(content
, pos
)) != -1) {
108 if (!matches
.contains(re
.cap(1)) && !lst
.contains(re
.cap(1)))
109 matches
<< re
.cap(1);
110 pos
+= re
.matchedLength();
115 typedef unsigned int _uint
;
119 dumpScanMatches (const QStringList
& lst
) {
120 const register _uint size
= lst
.size();
121 for (register _uint i
=0; i
<size
; ++i
)
123 qDebug() << "total: " << lst
.size();
128 scanYoutubeEmbed(QStringList
& lst
, const QString
& content
) {
129 QRegExp
re("\\/v\\/(.*)[\"&]");
130 QStringList matches
= matchScanContent(lst
, re
, content
);
131 //dumpScanMatches(matches);
136 scanYoutubeRegular(QStringList
& lst
, const QString
& content
) {
137 QRegExp
re("\\/watch\\?v=(.*)[\"&]");
138 QStringList matches
= matchScanContent(lst
, re
, content
);
139 //dumpScanMatches(matches);
144 ScanDialog::scanContent(QNetworkReply
*reply
) {
146 const QString content
= QString::fromLocal8Bit(reply
->readAll());
148 QStringList IDs
, links
;
150 scanYoutubeEmbed (IDs
, content
);
151 scanYoutubeRegular (IDs
, content
);
153 const register _uint ids_size
= IDs
.size();
156 for (i
=0; i
<ids_size
; ++i
)
157 links
<< "http://www.youtube.com/watch?v="+IDs
[i
];
159 const register _uint links_size
= links
.size();
161 if (titlesBox
->checkState()) {
163 for (i
=0; i
<links_size
; ++i
)
164 mgr
->get( QNetworkRequest(links
[i
]) );
167 for (i
=0; i
<links_size
; ++i
) {
168 QTreeWidgetItem
*item
= new QTreeWidgetItem
;
169 item
->setCheckState(0, Qt::Unchecked
);
170 item
->setText(0, links
[i
]);
171 item
->setText(1, links
[i
]);
172 itemsTree
->addTopLevelItem(item
);
178 ScanDialog::parseHtmlTitle(QNetworkReply
*reply
) {
180 const QString content
= QString::fromLocal8Bit(reply
->readAll());
181 const QString link
= reply
->url().toString();
183 QRegExp
re("<title>(.*)<\\/title>"); // TODO: improve.
184 re
.setCaseSensitivity(Qt::CaseInsensitive
);
188 QTreeWidgetItem
*item
= new QTreeWidgetItem
;
189 item
->setCheckState(0, Qt::Unchecked
);
190 item
->setText(0, re
.cap(1));
191 item
->setText(1, link
);
192 itemsTree
->addTopLevelItem(item
);
195 QNetworkAccessManager
*
196 ScanDialog::createManager() {
197 QNetworkAccessManager
*p
= new QNetworkAccessManager(this);
199 connect(p
, SIGNAL(finished(QNetworkReply
*)),
200 this, SLOT(replyFinished(QNetworkReply
*)));
206 ScanDialog::handleRedirect(const QNetworkReply
*reply
) {
209 // reply->header(QNetworkRequest::LocationHeader).toUrl();
212 reply
->attribute(QNetworkRequest::RedirectionTargetAttribute
).toUrl();
214 if (!location
.isEmpty() && location
!= redirectedToURL
)
215 redirectedToURL
= location
;
217 redirectedToURL
.clear();
221 ScanDialog::writeSettings() {
223 s
.beginGroup("ScanDialog");
224 s
.setValue("size", size());
229 ScanDialog::readSettings() {
231 s
.beginGroup("ScanDialog");
232 resize( s
.value("size", QSize(514,295)).toSize() );