Skip to content

Commit

Permalink
disable JS in loadUrl if js should be disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
agentzh committed Jun 12, 2009
1 parent 6f6c902 commit 0195e07
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 23 deletions.
3 changes: 2 additions & 1 deletion VdomBrowser.pro
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ SOURCES += iteratorconfigdialog.cpp \
mainwindow.cpp \
webpage.cpp \
main.cpp
HEADERS += iteratorconfigdialog.h \
HEADERS += iterator.h \
iteratorconfigdialog.h \
aboutdialog.h \
hunterconfigdialog.h \
version.h \
Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ export PATH=$QTDIR/bin:$PATH
#make distclean
/opt/qt4.5/bin/qmake "CONFIG-=debug" -r
#qmake -r
make
make -j2

53 changes: 53 additions & 0 deletions iterator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#ifndef ITERATOR_H
#define ITERATOR_H

#include <QDebug>

class Iterator {
public:
Iterator() {
m_count = 0;
m_cur = -1;
}

void setCount (int count) {
qDebug() << "Setting count to " << count << endl;
m_count = count;
m_cur = -1;
}

int prev() {
if (m_count <= 0) {
return (m_cur = -1);
}
if (m_cur == -1 || m_cur == 0) {
return (m_cur = m_count - 1);
}
return --m_cur;
}

int next() {
if (m_count <= 0) {
return (m_cur = -1);
}
if (m_cur == -1 || m_cur == m_count - 1) {
return (m_cur = 0);
}
return ++m_cur;
}

int cur() {
return m_cur;
}

void setCur(int cur) {
m_cur = cur;
}

private:
int m_cur;
int m_count;
};

#endif // ITERATOR_H

134 changes: 127 additions & 7 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
#include "webpage.h"
#include <stdlib.h>

const static int MAX_FILE_LINE_LEN = 2048;

MainWindow::MainWindow(const QString& url): currentZoom(100) {
m_iterLabel = new QLabel(this);

QDesktopServices::setUrlHandler(QLatin1String("http"), this, "loadUrl");
m_hunterConfig = new HunterConfigDialog(this);
connect(m_hunterConfig, SIGNAL(accepted()),
Expand All @@ -24,7 +28,9 @@ MainWindow::MainWindow(const QString& url): currentZoom(100) {
this, SLOT(saveIteratorConfig()));

m_iterPrevButton = new QPushButton(tr("P&rev"), this);
connect(m_iterPrevButton, SIGNAL(clicked()), SLOT(iterPrev()));
m_iterNextButton = new QPushButton(tr("&Next"), this);
connect(m_iterNextButton, SIGNAL(clicked()), SLOT(iterNext()));

m_settings = new QSettings(
QSettings::UserScope,
Expand Down Expand Up @@ -53,7 +59,7 @@ void MainWindow::changeLocation() {
//QUrl url = guessUrlFromString(urlEdit->text());
//
QString urlStr = m_urlEdit->text().trimmed();
QRegExp test(QLatin1String("^[a-zA-Z]+\\:.*"));
QRegExp test("^[a-zA-Z]+\\:.*");

// Check if it looks like a qualified URL. Try parsing it and see.
bool hasSchema = test.exactMatch(urlStr);
Expand All @@ -65,12 +71,8 @@ void MainWindow::changeLocation() {
url.setEncodedUrl(urlStr.toUtf8(), QUrl::StrictMode);
m_urlEdit->setText(url.toEncoded());
// we might have enabled JS for UI interaction...
if (!m_enableJavascript) {
m_view->page()->settings()->setAttribute(QWebSettings::JavascriptEnabled, false);
}
//qDebug() << "Loading URL " << url.toEncoded() << "..." << endl;
m_view->page()->mainFrame()->load(url);
m_view->setFocus(Qt::OtherFocusReason);
loadUrl(url);
}

void MainWindow::loadFinished(bool done) {
Expand Down Expand Up @@ -212,11 +214,15 @@ void MainWindow::createProgressBar() {
m_progress->hide();
statusBar()->addPermanentWidget(m_progress);

m_iterLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
statusBar()->addPermanentWidget(m_iterLabel);

m_hunterLabel = new QLabel(this);
m_hunterLabel->hide();
m_hunterLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
statusBar()->addPermanentWidget(m_hunterLabel);

//m_iterLabel->show();
connect(m_view, SIGNAL(loadProgress(int)), m_progress, SLOT(show()));
connect(m_view, SIGNAL(loadProgress(int)), m_progress, SLOT(setValue(int)));
connect(m_view, SIGNAL(loadFinished(bool)), m_progress, SLOT(hide()));
Expand Down Expand Up @@ -355,6 +361,7 @@ void MainWindow::writeSettings() {

m_settings->setValue("iteratorEnabled", QVariant(m_iteratorEnabled));
m_settings->setValue("urlListFile", QVariant(m_urlListFile));
m_settings->setValue("iteratorCurrentIndex", QVariant(m_iterator.cur()));

m_settings->endGroup();
}
Expand All @@ -381,6 +388,12 @@ void MainWindow::readSettings() {
m_urlListFile = m_settings->value("urlListFile").toString();
initIteratorConfig();

m_iterator.setCur(m_settings->value("iteratorCurrentIndex", 0).toInt());
initIterator();
if (m_iteratorEnabled) {
m_iterLabel->show();
}

m_settings->endGroup();
}

Expand All @@ -404,6 +417,8 @@ void MainWindow::saveIteratorConfig() {
m_iterPrevButton->setEnabled(m_iteratorEnabled);
m_iterNextButton->setEnabled(m_iteratorEnabled);
m_urlListFile = m_iteratorConfig->listFile();

initIterator();
}

void MainWindow::hunterFinished(int exitCode, QProcess::ExitStatus) {
Expand Down Expand Up @@ -652,6 +667,11 @@ void MainWindow::initIteratorConfig() {
m_iteratorConfig->setIteratorEnabled(m_iteratorEnabled);
m_iterPrevButton->setEnabled(m_iteratorEnabled);
m_iterNextButton->setEnabled(m_iteratorEnabled);
if (m_iteratorEnabled) {
m_iterLabel->show();
} else {
m_iterLabel->hide();
}
//update();
m_iteratorConfig->setListFile(m_urlListFile);
}
Expand All @@ -661,7 +681,7 @@ void MainWindow::addUrlToList() {
opts |= QUrl::RemoveScheme;
opts |= QUrl::RemoveUserInfo;
opts |= QUrl::StripTrailingSlash;
QString s = m_view->url().toString(opts);
QString s = m_view->url().toEncoded(opts);
s = s.mid(2);
if (!s.isEmpty()) {
if (!m_urlList.contains(s))
Expand All @@ -670,3 +690,103 @@ void MainWindow::addUrlToList() {
}
}

void MainWindow::iterPrev() {
int ind = m_iterator.prev();
if (ind < 0) {
qDebug() << "Iterator index negative: " << ind << endl;
return;
}
if (ind >= m_urlList.count()) {
m_iterator.setCur(0);
m_iterator.setCount(m_urlList.count());
ind = 0;
}
m_iterLabel->setText("Page " + QString::number(ind));
if (ind >= 0 && ind < m_urlList.count()) {
QString url = "http://";
url += m_urlList[ind];
m_urlEdit->setText(url);
loadUrl(url);
}
}

void MainWindow::iterNext() {
int ind = m_iterator.next();
if (ind < 0) {
qDebug() << "Iterator index negative: " << ind << endl;
return;
}
if (ind >= m_urlList.count()) {
m_iterator.setCur(0);
m_iterator.setCount(m_urlList.count());
ind = 0;
}
m_iterLabel->setText("Page " + QString::number(ind));
if (ind >= 0 && ind < m_urlList.count()) {
QString url = "http://";
url += m_urlList[ind];
m_urlEdit->setText(url);
loadUrl(url);
}
}

void MainWindow::initIterator() {
if (!m_iteratorEnabled) {
return;
}
QFile file(m_urlListFile);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::warning(this, tr("URL List File Loader"),
QString("Failed to load url list file %1: %2")
.arg(m_urlListFile).arg(file.errorString()),
QMessageBox::NoButton);
file.close();
return;
}
m_urlList.clear();
QString line = QString::fromUtf8(file.readLine(MAX_FILE_LINE_LEN));
QRegExp emptyLinePat("^\\s*$");
while (!line.isEmpty()) {
if (emptyLinePat.exactMatch(line)) {
//qDebug() << "Empty line pattern found.\n";
} else {
//qDebug() << "Read line " << line;
line = line.trimmed();
line.replace(QRegExp("^[A-Za-z]+://"), "");
qDebug() << "URL: " << line << endl;
m_urlList.push_back(line);
}
line = QString::fromUtf8(file.readLine(MAX_FILE_LINE_LEN));
}
//QString json = QString::fromUtf8(file.readAll());
//qDebug() << "RAW JSON: " << json << endl;
file.close();
m_iterator.setCount(m_urlList.count());
m_iterLabel->setText("Page " + QString::number(m_iterator.cur()));
}

void MainWindow::execHunterConfig() {
initHunterConfig();
m_hunterConfig->exec();
}

void MainWindow::execIteratorConfig() {
initIteratorConfig();
m_iteratorConfig->exec();
}

void MainWindow::loadUrl(const QUrl& url) {
//fprintf(stderr, "Loading new url...");
QWebPage* page = m_view->page();
//page->blockSignals(true);
m_view->stop();
//page->blockSignals(false);

if (!m_enableJavascript) {
m_view->page()->settings()->setAttribute(QWebSettings::JavascriptEnabled, false);
}

page->mainFrame()->load(url);
m_view->setFocus(Qt::OtherFocusReason);
}

27 changes: 13 additions & 14 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "aboutdialog.h"
#include "hunterconfigdialog.h"
#include "iteratorconfigdialog.h"
#include "iterator.h"

class MainWindow : public QMainWindow
{
Expand All @@ -34,6 +35,9 @@ class MainWindow : public QMainWindow

protected slots:

void iterPrev();
void iterNext();

void huntOnly();

void hunterStarted() {
Expand All @@ -50,11 +54,7 @@ protected slots:
m_itemInfoEdit->append(QString::fromUtf8(m_hunter.readAllStandardError()));
}

void loadUrl(const QUrl& url) {
//fprintf(stderr, "Loading new url...");
m_view->load(url);
m_view->setFocus(Qt::OtherFocusReason);
}
void loadUrl(const QUrl& url);

void updateUrl(const QUrl& url) {
m_urlEdit->setText(url.toEncoded());
Expand Down Expand Up @@ -158,18 +158,13 @@ protected slots:
void saveHunterConfig();
void saveIteratorConfig();

void execHunterConfig() {
initHunterConfig();
m_hunterConfig->exec();
}

void execIteratorConfig() {
initIteratorConfig();
m_iteratorConfig->exec();
}
void execHunterConfig();
void execIteratorConfig();

private:

void initIterator();

QVariant evalJS(const QString& js);

void addUrlToList();
Expand Down Expand Up @@ -238,7 +233,11 @@ protected slots:
QPushButton* m_iterPrevButton;
QPushButton* m_iterNextButton;

QLabel* m_iterLabel;

JSonDriver m_jsonDriver;

Iterator m_iterator;
};

#endif
Expand Down

0 comments on commit 0195e07

Please sign in to comment.