1
0

DownloadsParse.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "Main.h"
  2. #include "DownloadsParse.h"
  3. #include "Downloaded.h"
  4. #include "Defaults.h"
  5. #include "ParseUtil.h"
  6. #include <wchar.h>
  7. #include <locale.h>
  8. static __time64_t filetime( const wchar_t *file )
  9. {
  10. if ( !file || !*file )
  11. return 0;
  12. WIN32_FIND_DATAW f = { 0 };
  13. HANDLE h = FindFirstFileW( file, &f );
  14. if ( h == INVALID_HANDLE_VALUE )
  15. {
  16. /*wchar_t tmp[128] = {0};
  17. wsprintf(tmp,L"%d",GetLastError());
  18. MessageBox(0,file,tmp,0);*/
  19. return 0;
  20. }
  21. FindClose( h );
  22. SYSTEMTIME s = { 0 };
  23. FileTimeToSystemTime( &f.ftCreationTime, &s );
  24. tm t = { 0 };
  25. t.tm_year = s.wYear - 1900;
  26. t.tm_mon = s.wMonth - 1;
  27. t.tm_mday = s.wDay;
  28. t.tm_hour = s.wHour;
  29. t.tm_min = s.wMinute;
  30. t.tm_sec = s.wMinute;
  31. __time64_t ret = _mktime64(&t);
  32. /*wchar_t tmp[128] = {0};
  33. wsprintf(tmp,L"%d",ret);
  34. MessageBox(0,tmp,0,0);*/
  35. return ret;
  36. }
  37. size_t GetFileSize(LPCWSTR path)
  38. {
  39. WIN32_FIND_DATAW data = {0};
  40. if (path && *path)
  41. {
  42. HANDLE h = FindFirstFileW(path, &data);
  43. if (h == INVALID_HANDLE_VALUE)
  44. return -1;
  45. FindClose(h);
  46. }
  47. return data.nFileSizeLow/* | (__int64)data.nFileSizeHigh << 32*/;
  48. }
  49. static void ReadDownload( const XMLNode *item )
  50. {
  51. DownloadedFile newDownloaded;
  52. const wchar_t *source = GetContent( item, L"source" );
  53. if ( !source ) source = GetContent( item, L"channel" );
  54. newDownloaded.SetSource( source );
  55. const wchar_t *title = GetContent( item, L"title" );
  56. if ( !title ) title = GetContent( item, L"item" );;
  57. newDownloaded.SetTitle( title );
  58. const wchar_t *url = GetContent( item, L"url" );
  59. newDownloaded.SetURL( url );
  60. const wchar_t *path = GetContent( item, L"path" );
  61. newDownloaded.SetPath( path );
  62. // TODO ideally should be able to cope with __int64
  63. // but the db is setup for int currently...
  64. const wchar_t *size = GetContent( item, L"size" );
  65. if ( size && size[ 0 ] )
  66. {
  67. size_t val = _wtoi( size );
  68. if ( val > 0 ) newDownloaded.totalSize = val;
  69. else if ( !val )
  70. {
  71. val = GetFileSize( path );
  72. if ( val > 0 ) newDownloaded.totalSize = val;
  73. else newDownloaded.totalSize = -1;
  74. dirty++;
  75. }
  76. }
  77. else
  78. {
  79. size_t val = GetFileSize( path );
  80. if ( val > 0 ) newDownloaded.totalSize = val;
  81. else newDownloaded.totalSize = -1;
  82. dirty++;
  83. }
  84. const wchar_t *downloadDate = GetContent( item, L"downloadDate" );
  85. if ( downloadDate && downloadDate[ 0 ] )
  86. {
  87. __time64_t val = (__time64_t)_wtoi( downloadDate );
  88. if ( time > 0 ) newDownloaded.downloadDate = val;
  89. }
  90. else
  91. {
  92. __time64_t val = filetime( newDownloaded.path );
  93. if ( time > 0 ) newDownloaded.downloadDate = val;
  94. }
  95. const wchar_t *status = GetContent( item, L"downloadStatus" );
  96. if ( status && status[ 0 ] )
  97. {
  98. newDownloaded.downloadStatus = _wtoi( status );
  99. }
  100. else
  101. {
  102. newDownloaded.downloadStatus = DownloadedFile::DOWNLOAD_SUCCESS;
  103. }
  104. downloadedFiles.downloadList.push_back( newDownloaded );
  105. }
  106. void DownloadsParse::ReadNodes( const wchar_t *url )
  107. {
  108. XMLNode::NodeList::const_iterator itr;
  109. const XMLNode *curNode = xmlDOM.GetRoot();
  110. if ( curNode->Present( L"winamp:preferences" ) )
  111. curNode = curNode->Get( L"winamp:preferences" );
  112. curNode = curNode->Get( L"downloads" );
  113. if ( curNode )
  114. {
  115. const wchar_t *prop = curNode->GetProperty( L"downloadsTitleWidth" );
  116. if ( prop && prop[ 0 ] )
  117. downloadsTitleWidth = _wtoi( prop );
  118. if ( downloadsTitleWidth <= 0 )
  119. downloadsTitleWidth = DOWNLOADSTITLEWIDTHDEFAULT;
  120. prop = curNode->GetProperty( L"downloadsProgressWidth" );
  121. if ( prop && prop[ 0 ] )
  122. downloadsProgressWidth = _wtoi( prop );
  123. if ( downloadsProgressWidth <= 0 )
  124. downloadsProgressWidth = DOWNLOADSPROGRESSWIDTHDEFAULT;
  125. prop = curNode->GetProperty( L"downloadsDateWidth" );
  126. if ( prop && prop[ 0 ] )
  127. downloadsDateWidth = _wtoi( prop );
  128. if ( downloadsDateWidth <= 0 )
  129. downloadsDateWidth = DOWNLOADSDATEWIDTHDEFAULTS;
  130. prop = curNode->GetProperty( L"downloadsSourceWidth" );
  131. if ( prop && prop[ 0 ] )
  132. downloadsSourceWidth = _wtoi( prop );
  133. if ( downloadsSourceWidth <= 0 )
  134. downloadsSourceWidth = DOWNLOADSSOURCEWIDTHDEFAULT;
  135. prop = curNode->GetProperty( L"downloadsPathWidth" );
  136. if ( prop && prop[ 0 ] )
  137. downloadsPathWidth = _wtoi( prop );
  138. if ( downloadsPathWidth <= 0 )
  139. downloadsPathWidth = DOWNLOADSPATHWIDTHDEFAULTS;
  140. prop = curNode->GetProperty( L"downloadsItemSort" );
  141. if ( prop && prop[ 0 ] )
  142. downloadsItemSort = _wtoi( prop );
  143. prop = curNode->GetProperty( L"downloadsSortAscending" );
  144. if ( prop && prop[ 0 ] )
  145. downloadsSortAscending = !PropertyIsFalse( curNode, L"downloadsSortAscending" );
  146. const XMLNode::NodeList *downloadsList = curNode->GetList( L"download" );
  147. if ( downloadsList )
  148. {
  149. for ( itr = downloadsList->begin(); itr != downloadsList->end(); itr++ )
  150. ReadDownload( *itr );
  151. }
  152. }
  153. }