DownloadStatus.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "main.h"
  2. #include "api__ml_wire.h"
  3. #include "DownloadStatus.h"
  4. #include "DownloadsDialog.h"
  5. #include "./navigation.h"
  6. #include <strsafe.h>
  7. DownloadStatus downloadStatus;
  8. using namespace Nullsoft::Utility;
  9. DownloadStatus::Status::Status()
  10. {
  11. Init();
  12. }
  13. DownloadStatus::Status::Status( size_t _downloaded, size_t _maxSize, const wchar_t *_channel, const wchar_t *_item, const wchar_t *_path )
  14. {
  15. Init();
  16. downloaded = _downloaded;
  17. maxSize = _maxSize;
  18. channel = _wcsdup( _channel );
  19. item = _wcsdup( _item );
  20. path = _wcsdup( _path );
  21. }
  22. const DownloadStatus::Status &DownloadStatus::Status::operator =( const DownloadStatus::Status &copy )
  23. {
  24. Reset();
  25. Init();
  26. downloaded = copy.downloaded;
  27. maxSize = copy.maxSize;
  28. channel = _wcsdup( copy.channel );
  29. item = _wcsdup( copy.item );
  30. path = _wcsdup( copy.path );
  31. killswitch = copy.killswitch;
  32. return *this;
  33. }
  34. DownloadStatus::Status::~Status()
  35. {
  36. Reset();
  37. }
  38. void DownloadStatus::Status::Init()
  39. {
  40. downloaded = 0;
  41. maxSize = 0;
  42. killswitch = 0;
  43. channel = 0;
  44. item = 0;
  45. path = 0;
  46. }
  47. void DownloadStatus::Status::Reset()
  48. {
  49. if ( channel )
  50. {
  51. free( channel );
  52. channel = 0;
  53. }
  54. if ( item )
  55. {
  56. free( item );
  57. item = 0;
  58. }
  59. if ( path )
  60. {
  61. free( path );
  62. path = 0;
  63. }
  64. }
  65. void DownloadStatus::AddDownloadThread(DownloadToken token, const wchar_t *channel, const wchar_t *item, const wchar_t *path)
  66. {
  67. {
  68. AutoLock lock(statusLock);
  69. downloads[token] = Status(0,0,channel,item,path);
  70. DownloadsUpdated(downloads[token],token);
  71. }
  72. Navigation_ShowService(SERVICE_DOWNLOADS, SHOWMODE_AUTO);
  73. }
  74. void DownloadStatus::DownloadThreadDone(DownloadToken token)
  75. {
  76. {
  77. AutoLock lock(statusLock);
  78. downloads.erase(token);
  79. }
  80. Navigation_ShowService(SERVICE_DOWNLOADS, SHOWMODE_AUTO);
  81. }
  82. bool DownloadStatus::UpdateStatus(DownloadToken token, size_t downloaded, size_t maxSize)
  83. {
  84. AutoLock lock(statusLock);
  85. downloads[token].downloaded = downloaded;
  86. downloads[token].maxSize = maxSize;
  87. return !!downloads[token].killswitch;
  88. }
  89. bool DownloadStatus::CurrentlyDownloading()
  90. {
  91. AutoLock lock(statusLock);
  92. return !downloads.empty();
  93. }
  94. void DownloadStatus::GetStatusString( wchar_t *status, size_t len )
  95. {
  96. AutoLock lock( statusLock );
  97. Downloads::iterator itr;
  98. size_t bytesDownloaded = 0, bytesTotal = 0, numDownloads = 0;
  99. bool unknownTotal = false;
  100. for ( itr = downloads.begin(); itr != downloads.end(); itr++ )
  101. {
  102. Status &dlstatus = itr->second;
  103. if ( dlstatus.maxSize )
  104. {
  105. numDownloads++;
  106. bytesDownloaded += dlstatus.downloaded;
  107. bytesTotal += dlstatus.maxSize;
  108. }
  109. else // don't have a max size
  110. {
  111. if ( dlstatus.downloaded ) // if we've downloaded some then we just don't know the total
  112. {
  113. unknownTotal = true;
  114. numDownloads++;
  115. bytesDownloaded += dlstatus.downloaded;
  116. }
  117. }
  118. }
  119. if ( 0 == numDownloads )
  120. {
  121. status[ 0 ] = L'\0';
  122. }
  123. else
  124. {
  125. if ( unknownTotal || bytesTotal == 0 )
  126. StringCchPrintf( status, len, WASABI_API_LNGSTRINGW( IDS_DOWNLOADING_KB_COMPLETE ), numDownloads, bytesDownloaded / 1024);
  127. else
  128. StringCchPrintf( status, len, WASABI_API_LNGSTRINGW( IDS_DOWNLOADING_KB_PROGRESS ), numDownloads, bytesDownloaded / 1024, bytesTotal / 1024, MulDiv( (int)bytesDownloaded, 100, (int)bytesTotal ) );
  129. }
  130. }