Browse Source

All work and no play makes...

Antialize 15 years ago
parent
commit
5d5c641149
9 changed files with 178 additions and 194 deletions
  1. 17 62
      src/multipageloader.cc
  2. 3 1
      src/multipageloader.hh
  3. 61 0
      src/multipageloader_p.hh
  4. 54 111
      src/pageconverter.cc
  5. 1 2
      src/pageconverter.hh
  6. 35 15
      src/pageconverter_p.hh
  7. 2 0
      src/tempfile.cc
  8. 1 0
      src/tempfile.hh
  9. 4 3
      wkhtmltopdf.pro

+ 17 - 62
src/multipageloader.cc

@@ -1,68 +1,9 @@
-//-*- mode: c++; tab-width: 4; indent-tabs-mode: t; c-file-style: "stroustrup"; -*-
-// This file is part of wkhtmltopdf.
-//
-// wkhtmltopdf is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// wkhtmltopdf is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with wkhtmltopdf.  If not, see <http://www.gnu.org/licenses/>.
-#include "multipageloader.hh"
+#include "multipageloader_p.hh"
 #include <QFile>
 #include <QFileInfo>
-#include <QNetworkAccessManager>
-#include <QWebFrame>
-#include <QNetworkReply>
-#include <QAuthenticator>
-#include <QSignalMapper>
-#include <QAtomicInt>
-
-class MultiPageLoaderPrivate: public QObject {
-	Q_OBJECT
-public:
-	MultiPageLoader & outer;
-	Settings & settings;
-	int httpErrorCode;
-	QList<QWebPage *> pages;
-	QList<QUrl> urls;
-	QList<QString> temporaryFiles;
-
-	int loginTry;
-
-	QNetworkAccessManager networkAccessManager;
-	QHash<QObject *, int> pageToIndex;
-	
 
-	QList<int> progressList;
-	QList<bool> finishedList;
-	int progressSum;
-	int finishedSum;
-	bool loadStartedEmitted;
-	bool error;
-
-	void sslErrors(QNetworkReply *reply, const QList<QSslError> &);
-	MultiPageLoaderPrivate(Settings & s, MultiPageLoader & o);
-	~MultiPageLoaderPrivate(); 
-	QWebPage * addResource(const QUrl & url);
-	void load();
-	void clearResources();
-	void cancel();
-	void copyFile(QFile & src, QFile & dst);
-	void amfinished(QNetworkReply * reply);
-	void authenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator);
-public slots:
-	void loadStarted();
-	void loadProgress(int progress);
-	void loadFinished(bool ok);
-
-
-};
+
+
 
 /*!
  * Track and handle network errors
@@ -179,6 +120,20 @@ void MultiPageLoaderPrivate::load() {
 	finishedSum=0;
 	loadStartedEmitted=false;
 	error=false;
+
+	
+// 		if (url == "-") {
+// 			QFile in;
+// 			in.open(stdin,QIODevice::ReadOnly);
+// 			url = QDir::tempPath()+"/wktemp"+QUuid::createUuid().toString()+".html";
+// 			temporaryFiles.push_back(url);
+// 			QFile tmp(url);
+// 			tmp.open(QIODevice::WriteOnly);
+// 			copyFile(in,tmp);
+// 		}
+
+// 		page->mainFrame()->load(guessUrlFromString(url));
+	
 }
 
 void MultiPageLoaderPrivate::clearResources() {

+ 3 - 1
src/multipageloader.hh

@@ -18,9 +18,11 @@
 #include "settings.hh"
 #include <QUrl>
 #include <QWebPage>
+#include <QObject>
 
 class MultiPageLoaderPrivate;
-class MultiPageLoader {
+class MultiPageLoader: public QObject {
+	Q_OBJECT
 public:
 	MultiPageLoader(Settings & s);
 	~MultiPageLoader();

+ 61 - 0
src/multipageloader_p.hh

@@ -0,0 +1,61 @@
+//-*- mode: c++; tab-width: 4; indent-tabs-mode: t; c-file-style: "stroustrup"; -*-
+// This file is part of wkhtmltopdf.
+//
+// wkhtmltopdf is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// wkhtmltopdf is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with wkhtmltopdf.  If not, see <http://www.gnu.org/licenses/>.
+#include "multipageloader.hh"
+#include <QNetworkAccessManager>
+#include <QWebFrame>
+#include <QAtomicInt>
+#include <QNetworkReply>
+#include <QAuthenticator>
+#include <QFile>
+
+class MultiPageLoaderPrivate: public QObject {
+	Q_OBJECT
+public:
+	MultiPageLoader & outer;
+	Settings & settings;
+	int httpErrorCode;
+	QList<QWebPage *> pages;
+	QList<QUrl> urls;
+	QList<QString> temporaryFiles;
+
+	int loginTry;
+
+	QNetworkAccessManager networkAccessManager;
+	QHash<QObject *, int> pageToIndex;
+	
+
+	QList<int> progressList;
+	QList<bool> finishedList;
+	int progressSum;
+	int finishedSum;
+	bool loadStartedEmitted;
+	bool error;
+
+	void sslErrors(QNetworkReply *reply, const QList<QSslError> &);
+	MultiPageLoaderPrivate(Settings & s, MultiPageLoader & o);
+	~MultiPageLoaderPrivate(); 
+	QWebPage * addResource(const QUrl & url);
+	void load();
+	void clearResources();
+	void cancel();
+	static void copyFile(QFile & src, QFile & dst);
+	void amfinished(QNetworkReply * reply);
+	void authenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator);
+public slots:
+	void loadStarted();
+	void loadProgress(int progress);
+	void loadFinished(bool ok);
+};

+ 54 - 111
src/pageconverter.cc

@@ -21,12 +21,12 @@
 #include <QWebPage>
 #include <QWebFrame>
 #include <QDir>
-#include <QUuid>
+
 #include <QPair>
 
 
 PageConverterPrivate::PageConverterPrivate(Settings & s, PageConverter & o) :
-	outer(o), settings(s) {
+	settings(s), pageLoader(s), hfLoader(s), outer(o) {
 
 #ifdef  __EXTENSIVE_WKHTMLTOPDF_QT_HACK__
 	if (!settings.defaultEncoding.isEmpty())
@@ -46,99 +46,72 @@ PageConverterPrivate::PageConverterPrivate(Settings & s, PageConverter & o) :
 	QWebSettings::globalSettings()->setAttribute(QWebSettings::PrintElementBackgrounds, settings.background);
 	QWebSettings::globalSettings()->setAttribute(QWebSettings::PluginsEnabled, settings.enablePlugins);
 	if (!settings.userStyleSheet.isEmpty())
-		QWebSettings::globalSettings()->setUserStyleSheetUrl(guessUrlFromString(settings.userStyleSheet));
+		QWebSettings::globalSettings()->setUserStyleSheetUrl(MultiPageLoader::guessUrlFromString(settings.userStyleSheet));
 #endif
-}
-
-
 
+	connect(&pageLoader, SIGNAL(loadProgress(int)), this, SLOT(loadProgress(int)));
+	connect(&hfLoader, SIGNAL(loadProgress(int)), this, SLOT(loadProgress(int)));
+	connect(&pageLoader, SIGNAL(loadFinished(bool)), this, SLOT(preparePrint(bool)));
+	connect(&hfLoader, SIGNAL(loadFinished(bool)), this, SLOT(printPage(bool)));
+}
 
-/*!
- * Once loading is finished, we start the printing
- * \param ok did the loading finish correctly?
- */
-void PageConverterPrivate::loadFinished(bool ok) {
-	//Keep track of the number of pages currently loading
-	#warning "This is a race condition"
-	loading.deref();
-	if (!ok) {
-		//It went bad, return with 1
-		emit outer.error("Failed loading page");
-        #warning "FIX ME"
-		exit(1);
-		return;
-	}
 
-	  #warning "FIX ME"
-	//feedback.nextState("Waiting for redirect");
-	if (loading == 0) {
-		//Wait a little while for js-redirects, and then print
-		QTimer::singleShot(settings.jsredirectwait, this, SLOT(preparePrint()));
-	}
-}
+// /*!
+//  * Once loading is finished, we start the printing
+//  * \param ok did the loading finish correctly?
+//  */
+// void PageConverterPrivate::loadFinished(bool ok) {
+// 	//Keep track of the number of pages currently loading
+// 	#warning "This is a race condition"
+// 	loading.deref();
+// 	if (!ok) {
+// 		//It went bad, return with 1
+// 		emit outer.error("Failed loading page");
+//         #warning "FIX ME"
+// 		exit(1);
+// 		return;
+// 	}
 
-/*!
- * Once loading starting, this is called
- */
-void PageConverterPrivate::loadStarted() {
-	//Keep track of the number of pages currently loading
-	#warning "This is a race condition"
-	loading.ref();
-}
+// 	  #warning "FIX ME"
+// 	//feedback.nextState("Waiting for redirect");
+// 	if (loading == 0) {
+// 		//Wait a little while for js-redirects, and then print
+// 		QTimer::singleShot(settings.jsredirectwait, this, SLOT(preparePrint()));
+// 	}
+// }
 
 /*!
  * Called when the page is loading, display some progress to the using
  * \param progress the loading progress in percent
  */
 void PageConverterPrivate::loadProgress(int progress) {
-	//Print out the load status
-	#warning "fix me"
-	//feedback.progress(progress, 100, "%", false);
+	progressString = QString::number(progress) + "%";
+	emit outer.progressChanged(progress);
 }
 
 
-void PageConverterPrivate::convert() {
-	
+void PageConverterPrivate::beginConvert() {
   	if (!settings.cover.isEmpty())
 		settings.in.push_front(settings.cover);
-
 // 	if(strcmp(out,"-") != 0 && !QFileInfo(out).isWritable()) {
 // 		fprintf(stderr, "Write access to '%s' is not allowed\n", out);
 // 		exit(1);
 // 	}
+	pageLoader.clearResources();
 	foreach(QString url, settings.in) {
-		QWebPage * page = new QWebPage();
-		//Allow for network control fine touning.
-		page->setNetworkAccessManager(&networkAccessManager);
-		//When loading is progressing we want loadProgress to be called
-		connect(page, SIGNAL(loadProgress(int)), this, SLOT(loadProgress(int)));
-		//Once the loading is done we want loadFinished to be called
-		connect(page, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));
-		connect(page, SIGNAL(loadStarted()), this, SLOT(loadStarted()));
-
-		page->mainFrame()->setZoomFactor(settings.zoomFactor);
-		if (url == "-") {
-			QFile in;
-			in.open(stdin,QIODevice::ReadOnly);
-			url = QDir::tempPath()+"/wktemp"+QUuid::createUuid().toString()+".html";
-			temporaryFiles.push_back(url);
-			QFile tmp(url);
-			tmp.open(QIODevice::WriteOnly);
-			copyFile(in,tmp);
-		}
-
-		page->mainFrame()->load(guessUrlFromString(url));
+		QWebPage * page = pageLoader.addResource(url);
 		pages.push_back(page);
 	}
+	pageLoader.load();
 }
 
 
 /*!
  * Prepares printing out the document to the pdf file
  */
-void PageConverterPrivate::preparePrint() {
+void PageConverterPrivate::preparePrint(bool ok) {
 	//If there are still pages loading wait til they are done
-	if (loading != 0) return;
+	//if (loading != 0) return;
 	//Give some user feed back
 // 	if (!quiet) {
 // 		fprintf(stderr, "Outputting pages       \r");
@@ -154,8 +127,7 @@ void PageConverterPrivate::preparePrint() {
 			lout = "/dev/stdout";
 		else {
 			#warning "Sometimes we should add .ps instead here"
-			lout = QDir::tempPath()+"/wktemp"+QUuid::createUuid().toString()+".pdf";
-			temporaryFiles.push_back(lout);
+			lout = tempOut.create(".pdf");
 		}
 	}
 	//Tell the printer object to print the file <out>
@@ -317,12 +289,12 @@ void PageConverterPrivate::preparePrint() {
 			}
 		}
 	} else 
-		printPage();
+		printPage(true);
 #endif
 }
 
 
-void PageConverterPrivate::printPage() {
+void PageConverterPrivate::printPage(bool ok) {
 	//if (headerFooterLoading != 0 || loading != 0) return;
 
  	bool first=true;
@@ -524,18 +496,13 @@ void PageConverterPrivate::printPage() {
 
 
 QWebPage * PageConverterPrivate::loadHeaderFooter(QString url, int d, int page) {
-	QUrl u = guessUrlFromString(url);
+	QUrl u = MultiPageLoader::guessUrlFromString(url);
 
-	#warning "Fix me"
-// 	QHash<QString, QString> values = calculateHeaderFooterParams(d, page);
-// 	for(QHash<QString, QString>::iterator i=values.begin(); i != values.end(); ++i)
-// 		u.addQueryItem(i.key(), i.value());
+	QHash<QString, QString> values = calculateHeaderFooterParams(d, page);
+	for(QHash<QString, QString>::iterator i=values.begin(); i != values.end(); ++i)
+		u.addQueryItem(i.key(), i.value());
 	
-	QWebPage * p = new QWebPage();
-	p->setNetworkAccessManager(&networkAccessManager);
-	//connect(p, SIGNAL(loadFinished(bool)), this, SLOT(headerFooterLoadFinished(bool)));
-	p->mainFrame()->load(u);
-	return p;
+	return hfLoader.addResource(u);
 }
 
 /*!
@@ -551,26 +518,8 @@ QString PageConverterPrivate::hfreplace(const QString & q) {
 }
 
 
-
-// //void WKHtmlToPdf::headerFooterLoadStarted() {
-// //	headerFooterLoading.ref();
-// //}
-
-// void WKHtmlToPdf::headerFooterLoadFinished(bool) {
-// 	headerFooterLoading.deref();
-// 	qDebug() << "hat";
-// 	if (headerFooterLoading == 0) {
-		
-// 		//Wait a little while for js-redirects, and then print
-// 		QTimer::singleShot(100, this, SLOT(printPage()));
-// 	}
-// }
-// #endif
-
-
-// #ifdef  __EXTENSIVE_WKHTMLTOPDF_QT_HACK__
-// QHash<QString, QString> WKHtmlToPdf::calculateHeaderFooterParams(int d, int page) {
-// 	QHash<QString, QString> res;
+QHash<QString, QString> PageConverterPrivate::calculateHeaderFooterParams(int d, int page) {
+	QHash<QString, QString> res;
 	
 // 	res["frompage"] = QString::number(page_offset);
 // 	res["topage"] = QString::number(page_offset+logicalPages-1);
@@ -590,8 +539,8 @@ QString PageConverterPrivate::hfreplace(const QString & q) {
 // 	res["section"] = sec[0];
 // 	res["subsection"] = sec[1];
 // 	res["subsubsection"] = sec[2];
-// 	return res;
-// }
+	return res;
+}
 
 
 /*!
@@ -633,15 +582,8 @@ int PageConverter::currentPhase() {
   \brief return a textual description of some phase
   \param phase the phase to get a description of, -1 for current phase
 */
-QString PageConverter::phaseDescription(int phase=-1) {
-	return d->d->phaseDescriptions[(phase < 0 || d->phaseDescriptions.size() >= phase)?phase:d->currentPhase];
-}
-
-/*!
-  \brief return the progress in the current phase in percent, or -1 for current phase
-*/
-double PageConverter::progress() {
-	return d->progress;
+QString PageConverter::phaseDescription(int phase) {
+	return d->phaseDescriptions[(phase < 0 || d->phaseDescriptions.size() >= phase)?phase:d->currentPhase];
 }
 
 /*!
@@ -655,7 +597,7 @@ QString PageConverter::progressString() {
   \brief return the http return code, of the converted page
 */
 int PageConverter::httpErrorCode() {
-	return d->httpErrorCode;
+	return d->pageLoader.httpErrorCode();
 }
 
 /*!
@@ -671,6 +613,7 @@ void PageConverter::addResource(const QString & url) {
   Once convertion is done an finished signal will be emitted
 */
 void PageConverter::beginConvertion() {
+	d->beginConvert();
 }
 
 /*!

+ 1 - 2
src/pageconverter.hh

@@ -27,7 +27,6 @@ public:
 	int phaseCount();
 	int currentPhase();
 	QString phaseDescription(int phase=-1);
-	double progress();
 	QString progressString();
 	int httpErrorCode();
 	void addResource(const QString & url);
@@ -35,7 +34,7 @@ signals:
 	void warning(const QString & message);
 	void error(const QString & message);
 	void phaseChanged();
-	void progressChanged();
+	void progressChanged(int progress);
 	void finised();
 public slots:
 	void beginConvertion();

+ 35 - 15
src/pageconverter_p.hh

@@ -24,22 +24,36 @@
 #include <QPainter>
 #include <QPrinter>
 #include <QWebElement>
+#include "multipageloader.hh"
+#include "tempfile.hh"
+
 class PageConverterPrivate: public QObject {
 	Q_OBJECT
 public:
 	PageConverterPrivate(Settings & s, PageConverter & o);
 	void copyFile(QFile & src, QFile & dst);
-	QUrl guessUrlFromString(const QString &string);
+	void beginConvert();
 	void convert();
+	void cancel();
+
+	QList<QString> phaseDescriptions;
+	int currentPhase;
+	Settings & settings;
+
+	MultiPageLoader pageLoader;
+	MultiPageLoader hfLoader;
+	QString progressString;
 private:
 	PageConverter & outer;
-	Settings & settings;
-	int networkError;
-	int loginTry;
-	QNetworkAccessManager networkAccessManager;
+
+	TempFile tempOut;
+	
+// 	int networkError;
+// 	int loginTry;
+//	QNetworkAccessManager networkAccessManager;
 	//!Keep track of the numer of pages loading
- 	QAtomicInt loading; 
-	QList<QString> temporaryFiles;
+//	QAtomicInt loading; 
+//	QList<QString> temporaryFiles;
 	QList<QWebPage *> pages;
 	QPrinter * printer;
 	QPainter * painter;
@@ -48,7 +62,9 @@ private:
 	int actualPages;
 	QList<int> pageCount;
 	int tocPages;
-	
+
+
+	QHash<QString, QString> calculateHeaderFooterParams(int d, int page);
 	QHash<int, QHash<QString, QWebElement> > anchors;
 	QHash<int, QVector< QPair<QWebElement,QString> > > localLinks;
 	QHash<int, QVector< QPair<QWebElement,QString> > > externalLinks;
@@ -56,16 +72,20 @@ private:
 	QList<QWebPage *> headers;
 	QList<QWebPage *> footers;
 
+	QString hfreplace(const QString & q);
 	QWebPage * loadHeaderFooter(QString url, int d, int page);
 private slots:
-	void amfinished(QNetworkReply * r);
-	void authenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator);
-	void loadFinished(bool ok);
-	void loadStarted();
 	void loadProgress(int progress);
-	void sslErrors(QNetworkReply *reply, const QList<QSslError> &);
-	void preparePrint();
-	void printPage();
+	
+	// void amfinished(QNetworkReply * r);
+// 	void authenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator);
+// 	void loadFinished(bool ok);
+// 	void loadStarted();
+// 	void loadProgress(int progress);
+// 	void sslErrors(QNetworkReply *reply, const QList<QSslError> &);
+	void preparePrint(bool ok);
+	void printPage(bool ok);
+	
 };
 
 #endif //__TEXTUALFEEDBACK_P_HH__

+ 2 - 0
src/tempfile.cc

@@ -15,6 +15,8 @@
 // along with wkhtmltopdf.  If not, see <http://www.gnu.org/licenses/>.
 #include "tempfile.hh"
 #include <QUuid>
+#include <QFile>
+#include <QDir>
 /*!
   \file tempfile.hh
   \brief Defines the TempFile class

+ 1 - 0
src/tempfile.hh

@@ -15,6 +15,7 @@
 // along with wkhtmltopdf.  If not, see <http://www.gnu.org/licenses/>.
 #ifndef __TEMPFILE_HH__
 #define __TEMPFILE_HH__
+#include <QString>
 
 class TempFile {
 private:

+ 4 - 3
wkhtmltopdf.pro

@@ -58,9 +58,10 @@ target.path=$$INSTALLBASE/bin
 QT += webkit network
 
 # Input
-HEADERS += src/wkhtmltopdf.hh src/toc.hh src/pageconverter_p.hh src/pageconverter.hh
+HEADERS += src/wkhtmltopdf.hh src/toc.hh src/pageconverter_p.hh src/pageconverter.hh \
+           src/multipageloader_p.hh src/multipageloader.hh
 
 SOURCES += src/wkhtmltopdf.cc src/toc.cc src/arguments.cc src/commandlineparser.cc \
            src/docparts.cc src/outputter.cc src/manoutputter.cc src/settings.cc \
-           src/htmloutputter.cc src/textoutputter.cc \
-           src/pageconverter.cc
+           src/htmloutputter.cc src/textoutputter.cc src/tempfile.cc \
+           src/multipageloader.cc src/pageconverter.cc