Jakob Truelsen 16 سال پیش
والد
کامیت
85340f9b89
7فایلهای تغییر یافته به همراه124 افزوده شده و 78 حذف شده
  1. 20 5
      src/multipageloader.cc
  2. 5 3
      src/multipageloader_p.hh
  3. 63 28
      src/pageconverter.cc
  4. 1 1
      src/pageconverter.hh
  5. 7 10
      src/pageconverter_p.hh
  6. 26 29
      src/wkhtmltopdf.cc
  7. 2 2
      wkhtmltopdf.pro

+ 20 - 5
src/multipageloader.cc

@@ -1,17 +1,29 @@
+//-*- 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_p.hh"
 #include <QFile>
 #include <QFileInfo>
 
-
-
-
 /*!
  * Track and handle network errors
  * \param reply The networkreply that has finished
  */
 void MultiPageLoaderPrivate::amfinished(QNetworkReply * reply) {
 	int error = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
-	if(error > 399 && httpErrorCode == 0) httpErrorCode = error;
+	if (error > 399 && httpErrorCode == 0) httpErrorCode = error;
 }
 
 /*!
@@ -121,7 +133,10 @@ void MultiPageLoaderPrivate::load() {
 	loadStartedEmitted=false;
 	error=false;
 
-	
+	for(int i=0; i < pages.size(); ++i) 
+		pages[i]->mainFrame()->load(urls[i]);
+
+
 // 		if (url == "-") {
 // 			QFile in;
 // 			in.open(stdin,QIODevice::ReadOnly);

+ 5 - 3
src/multipageloader_p.hh

@@ -44,7 +44,7 @@ public:
 	bool loadStartedEmitted;
 	bool error;
 
-	void sslErrors(QNetworkReply *reply, const QList<QSslError> &);
+
 	MultiPageLoaderPrivate(Settings & s, MultiPageLoader & o);
 	~MultiPageLoaderPrivate(); 
 	QWebPage * addResource(const QUrl & url);
@@ -52,10 +52,12 @@ public:
 	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);
+	void sslErrors(QNetworkReply *reply, const QList<QSslError> &);
+	void amfinished(QNetworkReply * reply);
+	void authenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator);
 };

+ 63 - 28
src/pageconverter.cc

@@ -21,13 +21,20 @@
 #include <QWebPage>
 #include <QWebFrame>
 #include <QDir>
-
+#include <qapplication.h>
 #include <QPair>
 
 
 PageConverterPrivate::PageConverterPrivate(Settings & s, PageConverter & o) :
 	settings(s), pageLoader(s), hfLoader(s), outer(o) {
 
+	phaseDescriptions.push_back("Loading pages");
+	phaseDescriptions.push_back("Resolving links");
+	phaseDescriptions.push_back("Counting pages");
+	phaseDescriptions.push_back("Loading headers and footers");
+	phaseDescriptions.push_back("Printing pages");
+	phaseDescriptions.push_back("Done");
+		
 #ifdef  __EXTENSIVE_WKHTMLTOPDF_QT_HACK__
 	if (!settings.defaultEncoding.isEmpty())
 		QWebSettings::globalSettings()->setDefaultTextEncoding(settings.defaultEncoding);
@@ -98,10 +105,14 @@ void PageConverterPrivate::beginConvert() {
 // 		exit(1);
 // 	}
 	pageLoader.clearResources();
-	foreach(QString url, settings.in) {
-		QWebPage * page = pageLoader.addResource(url);
-		pages.push_back(page);
-	}
+	foreach(QString url, settings.in)
+		pages.push_back(pageLoader.addResource(url));
+	
+	progressString = "0%";
+	currentPhase=0;
+	emit outer.phaseChanged();
+	loadProgress(0);
+
 	pageLoader.load();
 }
 
@@ -117,6 +128,7 @@ void PageConverterPrivate::preparePrint(bool ok) {
 // 		fprintf(stderr, "Outputting pages       \r");
 // 		fflush(stdout);
 // 	}
+	
 
 	printer = new QPrinter(settings.resolution);
 	
@@ -181,16 +193,17 @@ void PageConverterPrivate::preparePrint(bool ok) {
 
 	//Find and resolve all local links
 	if(settings.useLocalLinks || settings.useExternalLinks) {
+		currentPhase = 1;
+		emit outer.phaseChanged();
+		
 		QHash<QString, int> urlToDoc;
 		for(int d=0; d < pages.size(); ++d) 
 			urlToDoc[ pages[d]->mainFrame()->url().toString(QUrl::RemoveFragment) ]  = d;
 		
 		for(int d=0; d < pages.size(); ++d) {
-			#warning "FIX ME"
-			// if (!quiet) {
-// 				fprintf(stderr, "Resolving Links %d of %d      \r",d+1,pages.size());
-// 				fflush(stdout);
-// 			}
+
+			progressString = QString("Page ")+QString::number(d+1)+QString(" of ")+QString::number(pages.size());
+			emit outer.progressChanged((d+1)*100 / pages.size());
 			
 			foreach(const QWebElement & elm,pages[d]->mainFrame()->findAllElements("a")) {
 				QUrl href=QUrl(elm.attribute("href"));
@@ -221,17 +234,18 @@ void PageConverterPrivate::preparePrint(bool ok) {
 	}
 
 	headings.clear();
-	
+
+	currentPhase = 2;
+	emit outer.phaseChanged();
+
 	//This is the first render face, it is done to calculate:
 	// * The number of pages of each document
 	// * A visual ordering of the header elemnt
 	// * The location and page number of each header
 	for(int d=0; d < pages.size(); ++d) {
-		#warning "fix me"
-// 		if (!quiet) {
-// 			fprintf(stderr, "Counting pages %d of %d      \r",d+1,pages.size()+(print_toc?1:0));
-// 			fflush(stdout);
-// 		}
+		int tot = pages.size()+(settings.printToc?1:0);
+		progressString = QString("Page ")+QString::number(d+1)+QString(" of ")+QString::number(tot);
+		emit outer.progressChanged((d+1)*100 / tot);
 
 		painter->save();
 		QWebPrinter wp(pages[d]->mainFrame(), printer, *painter);
@@ -251,11 +265,10 @@ void PageConverterPrivate::preparePrint(bool ok) {
 	//Now that we know the ordering of the headers in each document we
 	//can calculate the number of pages in the table of content
 	if (settings.printToc) {
-		#warning "fixme"
-// 		if (!quiet) {
-// 			fprintf(stderr, "Counting pages %d of %d      \r",pages.size()+1,pages.size()+1);
-//  			fflush(stdout);
-//  		}									   
+		int k=pages.size()+1;
+		progressString = QString("Page ")+QString::number(k)+QString(" of ")+QString::number(k);
+		emit outer.progressChanged(100);
+
 //  		TocItem * root = new TocItem();
 // 		for(int d=0; d < pages.size(); ++d) {
 // 			if (cover[0] && d == 0) continue;
@@ -308,6 +321,9 @@ void PageConverterPrivate::printPage(bool ok) {
 		!settings.header.center.isEmpty() || !settings.footer.center.isEmpty() ||
 		!settings.header.right.isEmpty() || !settings.footer.right.isEmpty();
 
+	currentPhase = 4;
+	emit outer.phaseChanged();
+	
 	for(int cc_=0; cc_ < cc; ++cc_) {
 		//TODO print front here
 		//TODO print TOC here
@@ -358,11 +374,10 @@ void PageConverterPrivate::printPage(bool ok) {
 						
 			for(int p=0; p < wp.pageCount(); ++p) {
 				for(int pc_=0; pc_ < pc; ++pc_) {
-#warning "FIX ME"
-//  					if (!quiet) {
-// 						fprintf(stderr,"Printing page: %d of %d\r",actualPage, actualPages);
-//  						fflush(stdout);
-//  					}
+
+					progressString = QString("Page ") + QString::number(actualPage) + QString(" of ") + QString::number(actualPages);
+					emit outer.progressChanged(actualPage * 100 / actualPages);
+
 					if(first)
 						first=false;
 					else
@@ -457,8 +472,13 @@ void PageConverterPrivate::printPage(bool ok) {
  	painter->end();
 	
 	//if (headerFooterLoading != 0 || loading != 0) return;
+
+	currentPhase = 5;
+	emit outer.phaseChanged();
+	convertionDone = true;
+	emit outer.finished(true);
+
 	
-	#warning "FIX ME"
 // 	if (!quiet) {
 // 		fprintf(stderr,"Done                 \n");
 // 		fflush(stderr);
@@ -518,6 +538,19 @@ QString PageConverterPrivate::hfreplace(const QString & q) {
 }
 
 
+void PageConverterPrivate::convert() {
+	convertionDone=false;
+	beginConvert();
+	while(!convertionDone)
+		qApp->processEvents(QEventLoop::WaitForMoreEvents | QEventLoop::AllEvents);
+	//return true;
+}
+
+void PageConverterPrivate::cancel() {
+
+}
+
+
 QHash<QString, QString> PageConverterPrivate::calculateHeaderFooterParams(int d, int page) {
 	QHash<QString, QString> res;
 	
@@ -583,7 +616,9 @@ int PageConverter::currentPhase() {
   \param phase the phase to get a description of, -1 for current phase
 */
 QString PageConverter::phaseDescription(int phase) {
-	return d->phaseDescriptions[(phase < 0 || d->phaseDescriptions.size() >= phase)?phase:d->currentPhase];
+	if (phase < 0 || d->phaseDescriptions.size() <= phase) phase=d->currentPhase;
+	if (phase < 0 || d->phaseDescriptions.size() <= phase) return "Invalid";
+	return d->phaseDescriptions[phase];
 }
 
 /*!

+ 1 - 1
src/pageconverter.hh

@@ -35,7 +35,7 @@ signals:
 	void error(const QString & message);
 	void phaseChanged();
 	void progressChanged(int progress);
-	void finised();
+	void finished(bool ok);
 public slots:
 	void beginConvertion();
 	void convert();

+ 7 - 10
src/pageconverter_p.hh

@@ -26,15 +26,14 @@
 #include <QWebElement>
 #include "multipageloader.hh"
 #include "tempfile.hh"
+#include <QWaitCondition>
+#include <QMutex>
 
 class PageConverterPrivate: public QObject {
 	Q_OBJECT
 public:
 	PageConverterPrivate(Settings & s, PageConverter & o);
 	void copyFile(QFile & src, QFile & dst);
-	void beginConvert();
-	void convert();
-	void cancel();
 
 	QList<QString> phaseDescriptions;
 	int currentPhase;
@@ -45,12 +44,7 @@ public:
 	QString progressString;
 private:
 	PageConverter & outer;
-<<<<<<< HEAD:src/pageconverter_p.hh
-	Settings & settings;
 
-	//!Keep track of the numer of pages loading
- 	QAtomicInt loading; 
-=======
 
 	TempFile tempOut;
 	
@@ -60,7 +54,6 @@ private:
 	//!Keep track of the numer of pages loading
 //	QAtomicInt loading; 
 //	QList<QString> temporaryFiles;
->>>>>>> 5d5c6411499bee0145fb165c8b31d600dada6a80:src/pageconverter_p.hh
 	QList<QWebPage *> pages;
 	QPrinter * printer;
 	QPainter * painter;
@@ -70,6 +63,7 @@ private:
 	QList<int> pageCount;
 	int tocPages;
 
+	bool convertionDone;
 
 	QHash<QString, QString> calculateHeaderFooterParams(int d, int page);
 	QHash<int, QHash<QString, QWebElement> > anchors;
@@ -81,7 +75,7 @@ private:
 
 	QString hfreplace(const QString & q);
 	QWebPage * loadHeaderFooter(QString url, int d, int page);
-private slots:
+public slots:
 	void loadProgress(int progress);
 	
 	// void amfinished(QNetworkReply * r);
@@ -92,6 +86,9 @@ private slots:
 // 	void sslErrors(QNetworkReply *reply, const QList<QSslError> &);
 	void preparePrint(bool ok);
 	void printPage(bool ok);
+	void beginConvert();
+	void cancel();
+	void convert();
 	
 };
 

+ 26 - 29
src/wkhtmltopdf.cc

@@ -15,17 +15,16 @@
 // along with wkhtmltopdf.  If not, see <http://www.gnu.org/licenses/>.
 // #include "toc.hh"
 // #include "wkhtmltopdf.hh"
-#include <QUuid>
 #include <QtPlugin>
 #include <iostream>
-
-#include <string.h>
+#include "commandlineparser.hh"
+#include "pageconverter.hh"
+#include "progressfeedback.hh"
+#include "settings.hh"
+#include <QCommonStyle>
 #include <cstdlib>
 #include <qapplication.h>
-#include <QCommonStyle>
-#include "settings.hh"
-
-#include "pageconverter.hh"
+#include <string.h>
 
 #ifdef QT_STATIC
 //When doing a static build, we need to load the plugins to make images work
@@ -35,6 +34,7 @@ Q_IMPORT_PLUGIN(qtiff)
 Q_IMPORT_PLUGIN(qmng)
 #endif
 
+
 /*!
  * State mashine driven, shell like parser. This is used for
  * reading commandline options from stdin
@@ -110,13 +110,9 @@ void parseString(char * buff, int &nargc, char **nargv) {
 	nargv[nargc]=NULL;
 }
 
-
-#include "commandlineparser.hh"
-
 int main(int argc, char * argv[]) {
 	//This will store all our settings
 	Settings settings;
-	
 	//Create a command line parser to parse commandline arguments
 	CommandLineParser parser(settings);
 	//Setup default values in settings
@@ -137,24 +133,25 @@ int main(int argc, char * argv[]) {
 
 	//Create the actual page converter to convert the pages
 	PageConverter converter(settings);
-
-	for (int i=1; i < argc; ++i)
-		if (!strcmp(argv[i],"--read-args-from-stdin")) {
-			char buff[20400];
-			char *nargv[1000];
-			nargv[0] = argv[0];
-			while (fgets(buff,20398,stdin)) {
-				//x.resetPages();
-				int nargc=1;
-				parseString(buff,nargc,nargv);
-				parser.loadDefaults();
-				parser.parseArguments(nargc, (const char**)nargv);
-				converter.convert();
-				//x.run(nargc,(const char**)nargv);
-				//qApp->exec(); //Wait for application to terminate
-			}
-			exit(0);
-		}
+	ProgressFeedback feedback(converter);
+//  for (int i=1; i < argc; ++i)
+// 		if (!strcmp(argv[i],"--read-args-from-stdin")) {
+// 			char buff[20400];
+// 			char *nargv[1000];
+// 			nargv[0] = argv[0];
+// 			while (fgets(buff,20398,stdin)) {
+// 				//x.resetPages();
+// 				int nargc=1;
+// 				parseString(buff,nargc,nargv);
+// 				parser.loadDefaults();
+// 				parser.parseArguments(nargc, (const char**)nargv);
+// 				converter.convert();
+// 				//x.run(nargc,(const char**)nargv);
+// 				//qApp->exec(); //Wait for application to terminate
+// 			}
+// 			exit(0);
+// 		}
 	converter.convert();
+	//qApp->exec();
 	//qApp->exit();
 }

+ 2 - 2
wkhtmltopdf.pro

@@ -59,9 +59,9 @@ QT += webkit network
 
 # Input
 HEADERS += src/wkhtmltopdf.hh src/toc.hh src/pageconverter_p.hh src/pageconverter.hh \
-           src/multipageloader_p.hh src/multipageloader.hh
+           src/multipageloader_p.hh src/multipageloader.hh src/progressfeedback.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/tempfile.cc \
-           src/multipageloader.cc src/pageconverter.cc
+           src/multipageloader.cc src/pageconverter.cc src/progressfeedback.cc