remotegenerator.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "remotegenerator.h"
  2. #include "mainwindow.h"
  3. #include "directoryutilities.h"
  4. RemoteGenerator::RemoteGenerator(MainWindow *mainWindow)
  5. : QObject(mainWindow),
  6. m_mainWindow(mainWindow)
  7. {
  8. }
  9. RemoteGenerator::~RemoteGenerator()
  10. {
  11. }
  12. void RemoteGenerator::getPage(QUrl url, QString name, bool waitForFinished)
  13. {
  14. // Clean the dir, otherwise new files will just pile up at every wget call
  15. m_outputDir = QDir(QDir::tempPath() + HAG_TEMP_DIR + "\\" + name);
  16. DirectoryUtilities::cleanDirectory(m_outputDir.absolutePath(), true, m_outputDir.absolutePath());
  17. qDebug() << "getting" << url << name;
  18. // Using -nd in wget causes all files to be placed in one directory. This is not optimal,
  19. // since it will mess files from different domains, but using separate directories would
  20. // be hard for the generator: we would have to pass information which of the folders is
  21. // the one where to look for the correct index.html. Now the additional files will be renamed
  22. // to something like "index.html.1.html", and the site's main file will be index.html.
  23. // (Hopefully -- if there's index.html and it's not the main file, things won't work.)
  24. QString wget = "wget.exe -E -H -k -nd -p -P \"" + m_outputDir.absolutePath() + "\" " + url.toString();
  25. qDebug() << wget;
  26. QProcess *process = new QProcess(this);
  27. connect(process, SIGNAL(readyReadStandardError()), this, SLOT(debugProcess()));
  28. connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(debugProcess()));
  29. connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processFinished(int,QProcess::ExitStatus)));
  30. process->start(wget);
  31. if (waitForFinished)
  32. {
  33. // When using the browser interface, without this we will
  34. // get connection reset
  35. process->waitForFinished();
  36. }
  37. }
  38. void RemoteGenerator::debugProcess()
  39. {
  40. QProcess *pr = (QProcess*)sender();
  41. QString value(pr->readAllStandardOutput());
  42. if (!value.isEmpty())
  43. {
  44. qDebug() << value;
  45. emit message(value);
  46. }
  47. value = pr->readAllStandardError();
  48. if (!value.isEmpty())
  49. {
  50. emit message(value);
  51. qDebug() << value;
  52. }
  53. }
  54. void RemoteGenerator::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
  55. {
  56. qDebug() << "Finished with code" << exitCode << ", status" << exitStatus;
  57. m_mainWindow->setActivePath(m_outputDir.canonicalPath());
  58. qDebug() << "Disconnecting all debug message listeners";
  59. disconnect(this, SIGNAL(message(QString)), 0, 0);
  60. emit finished();
  61. }