archivedownloader.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "archivedownloader.h"
  2. #include <QDirIterator>
  3. #include "Utils.h"
  4. ArchiveDownloader::ArchiveDownloader(QString url, QString target,QObject *parent) : QObject(parent)
  5. {
  6. this->url = url;
  7. this->target = target;
  8. t = new QTemporaryDir();
  9. p = new QProcess();
  10. p->setProgram("wget");
  11. p->setWorkingDirectory(t->path());
  12. QStringList args;
  13. args << url;
  14. p->setArguments(args);
  15. connect(p, SIGNAL(finished(int)), this, SLOT(processFinished()));
  16. phase = 'd';
  17. p->start();
  18. }
  19. void ArchiveDownloader::processFinished()
  20. {
  21. if(phase=='d')
  22. {
  23. QDir dir(t->path());
  24. QDirIterator it(dir);
  25. QString fileName;
  26. int count=0;
  27. while (it.hasNext()) {
  28. it.next();
  29. QString file = it.fileName();
  30. if (file == "." || file == "..") continue;
  31. fileName = file;
  32. count++;
  33. }
  34. if(count==1) {
  35. p = new QProcess();
  36. p->setProgram("unar");//use unp if unar is not installed
  37. p->setWorkingDirectory(t->path());
  38. QStringList args;
  39. args << fileName;
  40. p->setArguments(args);
  41. connect(p, SIGNAL(finished(int)), this, SLOT(processFinished()));
  42. phase = 'u';
  43. p->start();
  44. }
  45. }
  46. if(phase=='u')
  47. {
  48. QDir dir(t->path());
  49. QDirIterator it(dir);
  50. QString fileName;
  51. int count=0;
  52. while (it.hasNext()) {
  53. it.next();
  54. QString file = it.fileName();
  55. if (file == "." || file == "..") continue;
  56. if(QFileInfo(t->path()+"/"+file).isDir()) {
  57. fileName = file;
  58. count++;
  59. }
  60. }
  61. if(count==1)
  62. {
  63. DEVLOG_INFO("unpacked dir"<<fileName);
  64. dir.rename(fileName,target+"/"+fileName);
  65. emit done();
  66. }
  67. //delete t
  68. }
  69. }