Skip to content
Browse files

disable JS in loadUrl if js should be disabled

  • Loading branch information...
1 parent 6f6c902 commit 0195e07a5d206c075cafdd250222c31bb4a627d7 agentzh committed Jun 12, 2009
Showing with 196 additions and 23 deletions.
  1. +2 −1 VdomBrowser.pro
  2. +1 −1 build.sh
  3. +53 −0 iterator.h
  4. +127 −7 mainwindow.cpp
  5. +13 −14 mainwindow.h
View
3 VdomBrowser.pro
@@ -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 \
View
2 build.sh
@@ -9,5 +9,5 @@ export PATH=$QTDIR/bin:$PATH
#make distclean
/opt/qt4.5/bin/qmake "CONFIG-=debug" -r
#qmake -r
-make
+make -j2
View
53 iterator.h
@@ -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
+
View
134 mainwindow.cpp
@@ -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()),
@@ -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,
@@ -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);
@@ -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) {
@@ -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()));
@@ -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();
}
@@ -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();
}
@@ -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) {
@@ -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);
}
@@ -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))
@@ -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);
+}
+
View
27 mainwindow.h
@@ -13,6 +13,7 @@
#include "aboutdialog.h"
#include "hunterconfigdialog.h"
#include "iteratorconfigdialog.h"
+#include "iterator.h"
class MainWindow : public QMainWindow
{
@@ -34,6 +35,9 @@ class MainWindow : public QMainWindow
protected slots:
+ void iterPrev();
+ void iterNext();
+
void huntOnly();
void hunterStarted() {
@@ -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());
@@ -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();
@@ -238,7 +233,11 @@ protected slots:
QPushButton* m_iterPrevButton;
QPushButton* m_iterNextButton;
+ QLabel* m_iterLabel;
+
JSonDriver m_jsonDriver;
+
+ Iterator m_iterator;
};
#endif

0 comments on commit 0195e07

Please sign in to comment.
Something went wrong with that request. Please try again.