httpmgr: fix: emit finished if != http/200.
[abby.git] / src / scandlg.cpp
blobcd663b7d1112305654816355de3ce6bb2b05c28a
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/>.
19 #include <QDialog>
20 #include <QSettings>
21 //#include <QDebug>
22 #include <QMessageBox>
23 #include <QRegExp>
25 #include "util.h"
26 #include "scan.h"
27 #include "scandlg.h"
29 ScanDialog::ScanDialog(QWidget *parent)
30 : QDialog(parent), errorOccurred(false)
32 setupUi(this);
34 itemsTree->setColumnHidden(1, true);
36 readSettings();
38 mgr= new QHttpManager(this);
39 mgr->setProgressBar(progressBar);
41 connect(mgr, SIGNAL(fetchLink(QString)),
42 this, SLOT(onFetchLink(QString)));
44 connect(mgr, SIGNAL(fetchFinished()),
45 this, SLOT(onFetchFinished()));
47 connect(mgr, SIGNAL(fetchError(QString)),
48 this, SLOT(onFetchError(QString)));
51 void
52 ScanDialog::updateCount() {
53 totalLabel->setText(
54 QString(tr("Total: %1"))
55 .arg(Util::countItems(itemsTree))
59 void
60 ScanDialog::enableWidgets(const bool state/*=true*/) {
61 linkEdit->setEnabled (state);
62 titlesBox->setEnabled(state);
65 void
66 ScanDialog::onScan() {
68 if (!linkEdit->isEnabled()) {
69 // Assumes it is disabled when we're scanning.
70 if (mgr)
71 mgr->abort();
72 if (mgrt)
73 mgrt->abort();
74 return;
77 QString lnk = linkEdit->text().simplified();
79 if (lnk.isEmpty())
80 return;
82 if (!lnk.startsWith("http://", Qt::CaseInsensitive))
83 lnk.insert(0,"http://");
85 itemsTree->clear();
86 updateCount();
88 logEdit->clear();
90 enableWidgets(false);
91 progressBar->setTextVisible(false);
92 scanButton->setText(tr("&Abort"));
94 errorOccurred = false;
96 mgr->fetch(lnk);
99 void
100 ScanDialog::writeSettings() {
101 QSettings s;
102 s.beginGroup("ScanDialog");
103 s.setValue("size", size());
104 s.endGroup();
107 void
108 ScanDialog::readSettings() {
109 QSettings s;
110 s.beginGroup("ScanDialog");
111 resize( s.value("size", QSize(514,295)).toSize() );
112 s.endGroup();
115 void
116 ScanDialog::resetUI() {
117 enableWidgets(true);
118 scanButton->setText(tr("&Scan"));
119 updateCount();
122 void
123 ScanDialog::onSelectAll() {
124 Util::checkAllItems(itemsTree, Qt::Checked);
127 void
128 ScanDialog::onInvert() {
129 Util::invertAllCheckableItems(itemsTree);
132 void
133 ScanDialog::onFetchFinished() {
135 if (errorOccurred) {
136 resetUI();
137 return;
140 QStringList found;
141 Scan::youtube(mgr->getData(), found);
143 const int size = found.size();
145 Util::appendLog(logEdit,
146 QString(tr("Found %1 video links.")).arg(size));
148 if ( !titlesBox->checkState() ) {
150 for (int i=0; i<size; ++i) {
151 QTreeWidgetItem *item = new QTreeWidgetItem;
152 item->setCheckState(0, Qt::Unchecked);
153 for (int j=0; j<2; ++j)
154 item->setText(j, found[i]);
155 itemsTree->addTopLevelItem(item);
158 resetUI();
159 Util::appendLog(logEdit, tr("Done."));
161 else {
162 Util::appendLog(logEdit,
163 tr("Fetch titles for the found videos."));
165 mgrt = new QHttpManager(this);
167 connect(mgrt, SIGNAL(fetchLink(QString)),
168 this, SLOT(onFetchLink(QString)));
170 connect(mgrt, SIGNAL(fetchError(QString)),
171 this, SLOT(onFetchError(QString)));
173 // Note the use of different slot here.
175 connect(mgrt, SIGNAL(fetchFinished()),
176 this, SLOT(onFetchTitlesFinished()));
178 // Do not set progressbar for this manager.
179 // We'll use fetched/expected for that instead.
181 fetchedTitles = -1;
182 expectedTitles = size;
184 progressBar->setValue(fetchedTitles);
185 progressBar->setMaximum(expectedTitles);
187 videoLinks = found;
189 progressBar->setTextVisible(true);
191 emit onFetchTitlesFinished(); // Start iteration.
195 void
196 ScanDialog::onFetchTitlesFinished() {
198 if (errorOccurred) {
199 resetUI();
200 return;
203 if (fetchedTitles >= 0) {
204 QString title;
205 if (!Scan::title(mgrt->getData(), title))
206 title = videoLinks[fetchedTitles]; // Fallback to link.
208 QTreeWidgetItem *item = new QTreeWidgetItem;
209 item->setCheckState(0, Qt::Unchecked);
210 item->setText(0, title);
211 item->setText(1, videoLinks[fetchedTitles]);
212 itemsTree->addTopLevelItem(item);
214 updateCount();
215 progressBar->setValue(++fetchedTitles);
217 if (fetchedTitles == expectedTitles) {
218 resetUI();
219 videoLinks.clear();
220 mgrt->deleteLater();
221 Util::appendLog(logEdit, tr("Done."));
222 return;
225 else
226 fetchedTitles++;
228 mgrt->fetch(videoLinks[fetchedTitles]);
231 void
232 ScanDialog::onFetchError(QString errorString) {
233 errorOccurred = true;
234 Util::appendLog(logEdit, errorString);
237 void
238 ScanDialog::onFetchLink(QString url) {
239 Util::appendLog(logEdit, tr("Fetch ... ")+url);