TorrentRequestInfo.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "TorrentRequestInfo.h"
  23. #include "DownloadEngineFactory.h"
  24. #include "prefs.h"
  25. #include "Util.h"
  26. extern RequestInfo* requestInfo;
  27. extern void setSignalHander(int signal, void (*handler)(int), int flags);
  28. void torrentHandler(int signal) {
  29. ((TorrentDownloadEngine*)requestInfo->getDownloadEngine())->
  30. torrentMan->setHalt(true);
  31. }
  32. RequestInfo* TorrentRequestInfo::execute() {
  33. if(op->get(PREF_SHOW_FILES) == V_TRUE) {
  34. showFileEntry();
  35. return 0;
  36. }
  37. e = DownloadEngineFactory::newTorrentConsoleEngine(op,
  38. torrentFile,
  39. targetFiles);
  40. setSignalHander(SIGINT, torrentHandler, SA_RESETHAND);
  41. setSignalHander(SIGTERM, torrentHandler, SA_RESETHAND);
  42. try {
  43. e->run();
  44. if(e->torrentMan->downloadComplete()) {
  45. printDownloadCompeleteMessage();
  46. }
  47. } catch(Exception* e) {
  48. logger->error("Exception caught", e);
  49. delete e;
  50. fail = true;
  51. }
  52. setSignalHander(SIGINT, SIG_DFL, 0);
  53. setSignalHander(SIGTERM, SIG_DFL, 0);
  54. delete e;
  55. return 0;
  56. }
  57. // TODO should be const TorrentMan* torrentMan
  58. void TorrentRequestInfo::showFileEntry()
  59. {
  60. TorrentMan torrentMan;
  61. torrentMan.option = op;
  62. FileEntries fileEntries =
  63. torrentMan.readFileEntryFromMetaInfoFile(torrentFile);
  64. cout << _("Files:") << endl;
  65. cout << "idx|path/length" << endl;
  66. cout << "===+===========================================================================" << endl;
  67. int count = 1;
  68. for(FileEntries::const_iterator itr = fileEntries.begin();
  69. itr != fileEntries.end(); count++, itr++) {
  70. printf("%3d|%s\n |%s Bytes\n", count, itr->path.c_str(),
  71. Util::llitos(itr->length, true).c_str());
  72. cout << "---+---------------------------------------------------------------------------" << endl;
  73. }
  74. }