add "verify c/clive path" button.
[abby.git] / src / scandlg.cpp
blob92d024bc8b12d6ee2f6a77ad3d17d0682ac5a0e5
1 /*
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/>.
18 #include <QDialog>
19 #include <QSettings>
20 #include <QDebug>
21 #include <QMessageBox>
22 #include <QRegExp>
24 #include "scandlg.h"
26 ScanDialog::ScanDialog(QWidget *parent)
27 : QDialog(parent), titleMode(false)
29 setupUi(this);
31 readSettings();
33 mgr = createManager();
35 itemsTree->setColumnHidden(1, true);
38 void
39 ScanDialog::onScan() {
40 QString lnk = linkEdit->text();
42 lnk = lnk.trimmed();
44 if (lnk.isEmpty())
45 return;
47 if (!lnk.startsWith("http://", Qt::CaseInsensitive))
48 lnk.insert(0,"http://");
50 itemsTree->clear();
52 titleMode = false;
54 linkEdit->setEnabled (false);
55 scanButton->setEnabled (false);
56 titlesBox->setEnabled (false);
57 buttonBox->setEnabled (false);
59 mgr->get(QNetworkRequest(lnk));
62 void
63 ScanDialog::replyFinished(QNetworkReply* reply) {
65 bool state = false;
67 if (reply->error() == QNetworkReply::NoError) {
69 handleRedirect(reply);
71 if (!redirectedToURL.isEmpty())
72 mgr->get( QNetworkRequest(redirectedToURL) );
73 else {
74 if (!titleMode)
75 scanContent(reply);
76 else
77 parseHtmlTitle(reply);
78 redirectedToURL.clear();
79 state = true;
82 else {
83 QMessageBox::critical(this, QCoreApplication::applicationName(),
84 QString(tr("Network error: %1")).arg(reply->errorString()));
85 state = true;
88 if (state) {
89 linkEdit->setEnabled (state);
90 scanButton->setEnabled (state);
91 titlesBox->setEnabled (state);
92 buttonBox->setEnabled (state);
95 reply->deleteLater();
98 static QStringList
99 matchScanContent (const QStringList& lst, QRegExp& re, const QString& content) {
101 re.setCaseSensitivity(Qt::CaseInsensitive);
102 re.setMinimal(true);
104 QStringList matches;
106 int pos = 0;
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();
112 return matches;
115 typedef unsigned int _uint;
117 #ifdef _1_
118 static void
119 dumpScanMatches (const QStringList& lst) {
120 const register _uint size = lst.size();
121 for (register _uint i=0; i<size; ++i)
122 qDebug() << lst[i];
123 qDebug() << "total: " << lst.size();
125 #endif
127 static void
128 scanYoutubeEmbed(QStringList& lst, const QString& content) {
129 QRegExp re("\\/v\\/(.*)[\"&]");
130 QStringList matches = matchScanContent(lst, re, content);
131 //dumpScanMatches(matches);
132 lst << matches;
135 static void
136 scanYoutubeRegular(QStringList& lst, const QString& content) {
137 QRegExp re("\\/watch\\?v=(.*)[\"&]");
138 QStringList matches = matchScanContent(lst, re, content);
139 //dumpScanMatches(matches);
140 lst << matches;
143 void
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();
154 register _uint i;
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()) {
162 titleMode = true;
163 for (i=0; i<links_size; ++i)
164 mgr->get( QNetworkRequest(links[i]) );
166 else {
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);
177 void
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);
185 re.setMinimal(true);
186 re.indexIn(content);
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*)));
202 return p;
205 void
206 ScanDialog::handleRedirect(const QNetworkReply *reply) {
208 // QUrl location =
209 // reply->header(QNetworkRequest::LocationHeader).toUrl();
211 QUrl location =
212 reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
214 if (!location.isEmpty() && location != redirectedToURL)
215 redirectedToURL = location;
216 else
217 redirectedToURL.clear();
220 void
221 ScanDialog::writeSettings() {
222 QSettings s;
223 s.beginGroup("ScanDialog");
224 s.setValue("size", size());
225 s.endGroup();
228 void
229 ScanDialog::readSettings() {
230 QSettings s;
231 s.beginGroup("ScanDialog");
232 resize( s.value("size", QSize(514,295)).toSize() );
233 s.endGroup();