converter.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
  2. // vi:set ts=4 sts=4 sw=4 noet :
  3. //
  4. // Copyright 2010 wkhtmltopdf authors
  5. //
  6. // This file is part of wkhtmltopdf.
  7. //
  8. // wkhtmltopdf is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU Lesser General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // wkhtmltopdf is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Lesser General Public License
  19. // along with wkhtmltopdf. If not, see <http://www.gnu.org/licenses/>.
  20. #include "converter_p.hh"
  21. #include "multipageloader.hh"
  22. #include <QWebFrame>
  23. #include <qapplication.h>
  24. #ifdef QT4_STATICPLUGIN_TEXTCODECS
  25. #include <QtPlugin>
  26. Q_IMPORT_PLUGIN(qcncodecs)
  27. Q_IMPORT_PLUGIN(qjpcodecs)
  28. Q_IMPORT_PLUGIN(qkrcodecs)
  29. Q_IMPORT_PLUGIN(qtwcodecs)
  30. #endif
  31. namespace wkhtmltopdf {
  32. void ConverterPrivate::updateWebSettings(QWebSettings * ws, const settings::Web & s) const {
  33. if (!s.defaultEncoding.isEmpty())
  34. ws->setDefaultTextEncoding(s.defaultEncoding);
  35. #ifdef __EXTENSIVE_WKHTMLTOPDF_QT_HACK__
  36. if (!s.enableIntelligentShrinking) {
  37. ws->setPrintingMaximumShrinkFactor(1.0);
  38. ws->setPrintingMinimumShrinkFactor(1.0);
  39. }
  40. ws->setPrintingMediaType(s.printMediaType?"print":"screen");
  41. #endif
  42. ws->setAttribute(QWebSettings::JavaEnabled, s.enablePlugins);
  43. ws->setAttribute(QWebSettings::JavascriptEnabled, s.enableJavascript);
  44. ws->setAttribute(QWebSettings::JavascriptCanOpenWindows, false);
  45. ws->setAttribute(QWebSettings::JavascriptCanAccessClipboard, false);
  46. ws->setFontSize(QWebSettings::MinimumFontSize, s.minimumFontSize);
  47. //Newer versions of QT have even more settings to change
  48. ws->setAttribute(QWebSettings::PrintElementBackgrounds, s.background);
  49. ws->setAttribute(QWebSettings::AutoLoadImages, s.loadImages);
  50. ws->setAttribute(QWebSettings::PluginsEnabled, s.enablePlugins);
  51. if (!s.userStyleSheet.isEmpty())
  52. ws->setUserStyleSheetUrl(MultiPageLoader::guessUrlFromString(s.userStyleSheet));
  53. }
  54. void ConverterPrivate::fail() {
  55. error = true;
  56. conversionDone = true;
  57. clearResources();
  58. emit outer().finished(false);
  59. qApp->exit(0); // quit qt's event handling
  60. }
  61. /*!
  62. * Called when the page is loading, display some progress to the using
  63. * \param progress the loading progress in percent
  64. */
  65. void ConverterPrivate::loadProgress(int progress) {
  66. progressString = QString::number(progress) + "%";
  67. emit outer().progressChanged(progress);
  68. }
  69. void ConverterPrivate::forwardError(QString error) {
  70. emit outer().error(error);
  71. }
  72. void ConverterPrivate::forwardWarning(QString warning) {
  73. emit outer().warning(warning);
  74. }
  75. void ConverterPrivate::cancel() {
  76. error=true;
  77. }
  78. bool ConverterPrivate::convert() {
  79. conversionDone=false;
  80. beginConvert();
  81. while (!conversionDone)
  82. qApp->processEvents(QEventLoop::WaitForMoreEvents | QEventLoop::AllEvents);
  83. return !error;
  84. }
  85. /*!
  86. \brief Count the number of phases that the conversion process goes though
  87. */
  88. int Converter::phaseCount() {
  89. return priv().phaseDescriptions.size();
  90. }
  91. /*!
  92. \brief return the current phase of conversion
  93. */
  94. int Converter::currentPhase() {
  95. return priv().currentPhase;
  96. }
  97. /*!
  98. \brief return a textual description of some phase
  99. \param phase the phase to get a description of, -1 for current phase
  100. */
  101. QString Converter::phaseDescription(int phase) {
  102. if (phase < 0 || priv().phaseDescriptions.size() <= phase) phase=priv().currentPhase;
  103. if (phase < 0 || priv().phaseDescriptions.size() <= phase) return "Invalid";
  104. return priv().phaseDescriptions[phase];
  105. }
  106. /*!
  107. \brief return a textual description of progress in the current phase
  108. */
  109. QString Converter::progressString() {
  110. return priv().progressString;
  111. }
  112. /*!
  113. \brief return the HTTP return code, of the converted page
  114. */
  115. int Converter::httpErrorCode() {
  116. return priv().errorCode;
  117. }
  118. /*!
  119. \brief Start a asynchronous conversion of html pages to a pdf document.
  120. Once conversion is done an finished signal will be emitted
  121. */
  122. void Converter::beginConversion() {
  123. priv().beginConvert();
  124. }
  125. /*!
  126. \brief Synchronous convert html pages to a pdf document.
  127. */
  128. bool Converter::convert() {
  129. return priv().convert();
  130. }
  131. /*!
  132. \brief Cancel a running conversion
  133. */
  134. void Converter::cancel() {
  135. priv().cancel();
  136. }
  137. void Converter::emitCheckboxSvgs(const settings::LoadPage & ls) {
  138. emit checkboxSvgChanged(ls.checkboxSvg);
  139. emit checkboxCheckedSvgChanged(ls.checkboxCheckedSvg);
  140. emit radiobuttonSvgChanged(ls.radiobuttonSvg);
  141. emit radiobuttonCheckedSvgChanged(ls.radiobuttonCheckedSvg);
  142. }
  143. }