DownloadStatus.cpp 3.6 KB

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