Tweak: labels, variable names, etc.
[abby.git] / src / scandlg.cpp
blob58133b28f3b245565312d67eaa9cb11d70ba7089
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)
29 setupUi(this);
30 readSettings();
31 mgr = createManager();
32 itemsTree->setColumnHidden(1, true);
35 void
36 ScanDialog::onScan() {
37 QString lnk = linkEdit->text();
39 if (lnk.isEmpty())
40 return;
42 itemsTree->clear();
44 scanButton->setEnabled(false);
45 buttonBox->setEnabled(false);
47 QNetworkRequest req(lnk);
48 req.setRawHeader("User-Agent", "Mozilla/5.0");
49 mgr->get(req);
52 void
53 ScanDialog::replyFinished(QNetworkReply* reply) {
54 if (reply->error() == QNetworkReply::NoError) {
55 QVariant tmp =
56 reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
58 redirectUrl =
59 redirect(tmp.toUrl(), redirectUrl);
61 if (!redirectUrl.isEmpty()) {
62 QNetworkRequest req(redirectUrl);
63 req.setRawHeader("User-Agent", "Mozilla/5.0");
64 mgr->get(req);
66 else {
67 redirectUrl.clear();
68 scanHTML(reply->readAll());
70 reply->deleteLater();
72 else {
73 QMessageBox mb(this);
74 mb.setText("Network error occurred");
75 mb.setInformativeText(reply->errorString());
76 mb.setStandardButtons(QMessageBox::Ok);
77 mb.setDefaultButton(QMessageBox::Ok);
78 mb.setIcon(QMessageBox::Critical);
79 mb.exec();
82 scanButton->setEnabled(true);
83 buttonBox->setEnabled(true);
86 void
87 ScanDialog::scanHTML(QString html) {
90 QNetworkAccessManager*
91 ScanDialog::createManager() {
92 QNetworkAccessManager *p = new QNetworkAccessManager(this);
94 connect(p, SIGNAL(finished(QNetworkReply*)),
95 this, SLOT(replyFinished(QNetworkReply*)));
97 return p;
100 QUrl
101 ScanDialog::redirect(const QUrl& to, const QUrl& from) const {
102 QUrl redirectTo;
103 if (!to.isEmpty() && to != from)
104 redirectTo = to;
105 return redirectTo;
108 void
109 ScanDialog::writeSettings() {
110 QSettings s;
111 s.beginGroup("ScanDialog");
112 s.setValue("size", size());
113 s.endGroup();
116 void
117 ScanDialog::readSettings() {
118 QSettings s;
119 s.beginGroup("ScanDialog");
120 resize( s.value("size", QSize(514,295)).toSize() );
121 s.endGroup();