1
0

Item.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "Item.h"
  2. #include "util.h"
  3. #include "defaults.h"
  4. #include <shlwapi.h>
  5. #include <strsafe.h>
  6. void RSS::Item::Reset()
  7. {
  8. free(itemName);
  9. free(url);
  10. free(sourceUrl);
  11. free(guid);
  12. free(description);
  13. free(link);
  14. free(duration);
  15. }
  16. void RSS::Item::Init()
  17. {
  18. listened = false;
  19. publishDate = 0;
  20. generatedDate = true;
  21. downloaded=false;
  22. itemName=0;
  23. url=0;
  24. sourceUrl=0;
  25. guid=0;
  26. description=0;
  27. link=0;
  28. duration=0;
  29. size=0;
  30. }
  31. RSS::Item::Item()
  32. {
  33. Init();
  34. }
  35. RSS::Item::~Item()
  36. {
  37. Reset();
  38. }
  39. RSS::Item::Item(const RSS::Item &copy)
  40. {
  41. Init();
  42. operator =(copy);
  43. }
  44. const RSS::Item &RSS::Item::operator =(const RSS::Item &copy)
  45. {
  46. Reset();
  47. Init();
  48. listened=copy.listened;
  49. publishDate = copy.publishDate;
  50. generatedDate = copy.generatedDate;
  51. downloaded=copy.downloaded;
  52. itemName=_wcsdup(copy.itemName);
  53. url=_wcsdup(copy.url);
  54. sourceUrl=_wcsdup(copy.sourceUrl);
  55. guid=_wcsdup(copy.guid);
  56. description=_wcsdup(copy.description);
  57. link=_wcsdup(copy.link);
  58. duration=wcsdup(copy.duration);
  59. size = copy.size;
  60. return *this;
  61. }
  62. HRESULT RSS::Item::GetDownloadFileName(const wchar_t *channelName, wchar_t *buffer, int bufferMax, BOOL fValidatePath) const
  63. {
  64. if (NULL == buffer || NULL == channelName) return E_INVALIDARG;
  65. buffer[0] = L'\0';
  66. WCHAR szBuffer[MAX_PATH] = {0};
  67. if (FAILED(StringCchCopyN(szBuffer, ARRAYSIZE(szBuffer), channelName, 100)))
  68. return E_UNEXPECTED;
  69. Plugin_CleanDirectory(szBuffer);
  70. Plugin_ReplaceBadPathChars(szBuffer);
  71. if (L'\0' == *szBuffer)
  72. StringCchCopy(szBuffer, ARRAYSIZE(szBuffer), L"UnknownChannel");
  73. if (FALSE == PathCombine(buffer, defaultDownloadPath, szBuffer))
  74. return E_FAIL;
  75. if (FALSE != fValidatePath && FAILED(Plugin_EnsurePathExist(buffer)))
  76. return E_FAIL;
  77. LPWSTR cursor = szBuffer;
  78. size_t remaining = ARRAYSIZE(szBuffer);
  79. tm* time = _localtime64(&publishDate);
  80. if(NULL != time && publishDate > 0)
  81. {
  82. StringCchPrintfEx(cursor, remaining, &cursor, &remaining, STRSAFE_NULL_ON_FAILURE,
  83. L"%04d-%02d-%02d - ", time->tm_year+1900, time->tm_mon+1, time->tm_mday);
  84. }
  85. LPWSTR t = cursor;
  86. if (FAILED(StringCchCopyNEx(cursor, remaining, itemName, 100, &cursor, &remaining, 0)))
  87. return E_UNEXPECTED;
  88. INT offset = Plugin_CleanDirectory(t);
  89. if (0 != offset)
  90. {
  91. remaining += offset;
  92. cursor -= offset;
  93. }
  94. if (t == cursor)
  95. StringCchCopyEx(cursor, remaining, L"UnknownItem", &cursor, &remaining, 0);
  96. else
  97. Plugin_ReplaceBadPathChars(t);
  98. if (FAILED(Plugin_FileExtensionFromUrl(cursor, (INT)remaining, url, L".mp3")))
  99. return E_UNEXPECTED;
  100. if (FALSE == PathAppend(buffer, szBuffer))
  101. return E_FAIL;
  102. return S_OK;
  103. }
  104. void RSS::MutableItem::SetLink(const wchar_t *value)
  105. {
  106. free(link);
  107. link = _wcsdup(value);
  108. }
  109. void RSS::MutableItem::SetItemName(const wchar_t *value)
  110. {
  111. free(itemName);
  112. itemName = _wcsdup(value);
  113. }
  114. void RSS::MutableItem::SetURL(const wchar_t *value)
  115. {
  116. free(url);
  117. url = _wcsdup(value);
  118. }
  119. void RSS::MutableItem::SetSourceURL(const wchar_t *value)
  120. {
  121. free(sourceUrl);
  122. sourceUrl = _wcsdup(value);
  123. }
  124. void RSS::MutableItem::SetGUID(const wchar_t *value)
  125. {
  126. free(guid);
  127. guid = _wcsdup(value);
  128. }
  129. void RSS::MutableItem::SetDescription(const wchar_t *value)
  130. {
  131. free(description);
  132. description = _wcsdup(value);
  133. }
  134. void RSS::MutableItem::SetDuration(const wchar_t *value)
  135. {
  136. free(duration);
  137. duration = _wcsdup(value);
  138. }
  139. void RSS::MutableItem::SetSize(const wchar_t * _size)
  140. {
  141. if (_size)
  142. size = _wtoi64(_size);
  143. else
  144. size=0;
  145. }