RequestInfo.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #ifndef _D_REQUEST_INFO_H_
  23. #define _D_REQUEST_INFO_H_
  24. #include "common.h"
  25. #include "LogFactory.h"
  26. #include "Option.h"
  27. #include "DownloadEngine.h"
  28. #include "Util.h"
  29. #include "Checksum.h"
  30. #include <signal.h>
  31. class FileInfo {
  32. public:
  33. string filename;
  34. long long int length;
  35. Checksum checksum;
  36. public:
  37. FileInfo():length(0) {}
  38. ~FileInfo() {}
  39. bool isEmpty() const {
  40. return filename.size() == 0 && length == 0;
  41. }
  42. bool checkReady() const {
  43. #ifdef ENABLE_MESSAGE_DIGEST
  44. return !isEmpty() && !checksum.isEmpty();
  45. #else
  46. return false;
  47. #endif // ENABLE_MESSAGE_DIGEST
  48. }
  49. bool check() const {
  50. #ifdef ENABLE_MESSAGE_DIGEST
  51. unsigned char md[MAX_MD_LENGTH];
  52. Util::fileChecksum(filename, md, checksum.getDigestAlgo());
  53. return Util::toHex(md,
  54. MessageDigestContext::digestLength(checksum.getDigestAlgo()))
  55. == checksum.getMessageDigest();
  56. #else
  57. return false;
  58. #endif // ENABLE_MESSAGE_DIGEST
  59. }
  60. bool isTorrentFile() const {
  61. return Util::endsWith(filename, ".torrent");
  62. }
  63. bool isMetalinkFile() const {
  64. return Util::endsWith(filename, ".metalink");
  65. }
  66. };
  67. class RequestInfo {
  68. protected:
  69. const Option* op;
  70. const Logger* logger;
  71. Checksum checksum;
  72. FileInfo fileInfo;
  73. bool fail;
  74. void printDownloadCompeleteMessage(const string& filename) {
  75. printf(_("\nThe download was complete. <%s>\n"), filename.c_str());
  76. }
  77. void printDownloadCompeleteMessage() {
  78. printf("\nThe download was complete.\n");
  79. }
  80. void printDownloadAbortMessage() {
  81. printf(_("\nThe download was not complete because of errors. Check the log.\n"));
  82. }
  83. public:
  84. RequestInfo(const Option* op):
  85. op(op),
  86. fail(false)
  87. {
  88. logger = LogFactory::getInstance();
  89. }
  90. virtual ~RequestInfo() {}
  91. virtual RequestInfo* execute() = 0;
  92. virtual DownloadEngine* getDownloadEngine() = 0;
  93. bool isFail() const { return fail; }
  94. void setChecksum(const Checksum& checksum) {
  95. this->checksum = checksum;
  96. }
  97. const Checksum& getChecksum() const { return checksum; }
  98. const FileInfo& getFileInfo() const { return fileInfo; }
  99. };
  100. #endif // _D_REQUEST_INFO_H_