deviceEventManager.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #include "main.h"
  2. #include "./deviceEventManager.h"
  3. DeviceEventManager::DeviceEventManager()
  4. : ref(1)
  5. {
  6. InitializeCriticalSection(&lock);
  7. }
  8. DeviceEventManager::~DeviceEventManager()
  9. {
  10. Lock();
  11. size_t index = handlerList.size();
  12. while(index--)
  13. {
  14. ifc_deviceevent *handler = handlerList[index];
  15. handler->Release();
  16. }
  17. Unlock();
  18. DeleteCriticalSection(&lock);
  19. }
  20. HRESULT DeviceEventManager::CreateInstance(DeviceEventManager **instance)
  21. {
  22. if (NULL == instance)
  23. return E_POINTER;
  24. *instance = new DeviceEventManager();
  25. if (NULL == *instance)
  26. return E_OUTOFMEMORY;
  27. return S_OK;
  28. }
  29. size_t DeviceEventManager::AddRef()
  30. {
  31. return InterlockedIncrement((LONG*)&ref);
  32. }
  33. size_t DeviceEventManager::Release()
  34. {
  35. if (0 == ref)
  36. return ref;
  37. LONG r = InterlockedDecrement((LONG*)&ref);
  38. if (0 == r)
  39. delete(this);
  40. return r;
  41. }
  42. int DeviceEventManager::QueryInterface(GUID interface_guid, void **object)
  43. {
  44. if (NULL == object) return E_POINTER;
  45. if (IsEqualIID(interface_guid, IFC_DeviceEventManager))
  46. *object = static_cast<ifc_deviceeventmanager*>(this);
  47. else
  48. {
  49. *object = NULL;
  50. return E_NOINTERFACE;
  51. }
  52. if (NULL == *object)
  53. return E_UNEXPECTED;
  54. AddRef();
  55. return S_OK;
  56. }
  57. void DeviceEventManager::Lock()
  58. {
  59. EnterCriticalSection(&lock);
  60. }
  61. void DeviceEventManager::Unlock()
  62. {
  63. LeaveCriticalSection(&lock);
  64. }
  65. HRESULT DeviceEventManager::Advise(ifc_deviceevent *handler)
  66. {
  67. HRESULT hr;
  68. size_t index;
  69. Lock();
  70. hr = S_OK;
  71. index = handlerList.size();
  72. while(index--)
  73. {
  74. if (handler == handlerList[index])
  75. {
  76. hr = E_FAIL;
  77. break;
  78. }
  79. }
  80. if (SUCCEEDED(hr))
  81. {
  82. handlerList.push_back(handler);
  83. handler->AddRef();
  84. }
  85. Unlock();
  86. return hr;
  87. }
  88. HRESULT DeviceEventManager::Unadvise(ifc_deviceevent *handler)
  89. {
  90. Lock();
  91. HRESULT hr = S_FALSE;
  92. size_t index = handlerList.size();
  93. while(index--)
  94. {
  95. if (handler == handlerList[index])
  96. {
  97. handlerList.erase(handlerList.begin() + index);
  98. handler->Release();
  99. hr = S_OK;
  100. break;
  101. }
  102. }
  103. Unlock();
  104. return hr;
  105. }
  106. void DeviceEventManager::Notify_IconChanged(ifc_device *device)
  107. {
  108. Lock();
  109. size_t index, count = handlerList.size();
  110. for(index = 0; index < count;index++)
  111. {
  112. ifc_deviceevent *handler = handlerList[index];
  113. handler->IconChanged(device);
  114. }
  115. Unlock();
  116. }
  117. void DeviceEventManager::Notify_DisplayNameChanged(ifc_device *device, const wchar_t *displayName)
  118. {
  119. Lock();
  120. size_t index, count = handlerList.size();
  121. for(index = 0; index < count;index++)
  122. {
  123. ifc_deviceevent *handler = handlerList[index];
  124. handler->DisplayNameChanged(device, displayName);
  125. }
  126. Unlock();
  127. }
  128. void DeviceEventManager::Notify_AttachmentChanged(ifc_device *device, BOOL attached)
  129. {
  130. Lock();
  131. size_t index, count = handlerList.size();
  132. for(index = 0; index < count;index++)
  133. {
  134. ifc_deviceevent *handler = handlerList[index];
  135. handler->AttachmentChanged(device, attached);
  136. }
  137. Unlock();
  138. }
  139. void DeviceEventManager::Notify_VisibilityChanged(ifc_device *device, BOOL visible)
  140. {
  141. Lock();
  142. size_t index, count = handlerList.size();
  143. for(index = 0; index < count;index++)
  144. {
  145. ifc_deviceevent *handler = handlerList[index];
  146. handler->VisibilityChanged(device, visible);
  147. }
  148. Unlock();
  149. }
  150. void DeviceEventManager::Notify_TotalSpaceChanged(ifc_device *device, uint64_t space)
  151. {
  152. Lock();
  153. size_t index, count = handlerList.size();
  154. for(index = 0; index < count;index++)
  155. {
  156. ifc_deviceevent *handler = handlerList[index];
  157. handler->TotalSpaceChanged(device, space);
  158. }
  159. Unlock();
  160. }
  161. void DeviceEventManager::Notify_UsedSpaceChanged(ifc_device *device, uint64_t space)
  162. {
  163. Lock();
  164. size_t index, count = handlerList.size();
  165. for(index = 0; index < count;index++)
  166. {
  167. ifc_deviceevent *handler = handlerList[index];
  168. handler->UsedSpaceChanged(device, space);
  169. }
  170. Unlock();
  171. }
  172. void DeviceEventManager::Notfiy_CommandChanged(ifc_device *device)
  173. {
  174. Lock();
  175. size_t index, count = handlerList.size();
  176. for(index = 0; index < count;index++)
  177. {
  178. ifc_deviceevent *handler = handlerList[index];
  179. handler->CommandChanged(device);
  180. }
  181. Unlock();
  182. }
  183. void DeviceEventManager::Notify_ActivityStarted(ifc_device *device, ifc_deviceactivity *activity)
  184. {
  185. Lock();
  186. size_t index, count = handlerList.size();
  187. for(index = 0; index < count;index++)
  188. {
  189. ifc_deviceevent *handler = handlerList[index];
  190. handler->ActivityStarted(device, activity);
  191. }
  192. Unlock();
  193. }
  194. void DeviceEventManager::Notify_ActivityFinished(ifc_device *device, ifc_deviceactivity *activity)
  195. {
  196. Lock();
  197. size_t index, count = handlerList.size();
  198. for(index = 0; index < count;index++)
  199. {
  200. ifc_deviceevent *handler = handlerList[index];
  201. handler->ActivityFinished(device, activity);
  202. }
  203. Unlock();
  204. }
  205. void DeviceEventManager::Notify_ActivityChanged(ifc_device *device, ifc_deviceactivity *activity)
  206. {
  207. Lock();
  208. size_t index, count = handlerList.size();
  209. for(index = 0; index < count;index++)
  210. {
  211. ifc_deviceevent *handler = handlerList[index];
  212. handler->ActivityChanged(device, activity);
  213. }
  214. Unlock();
  215. }
  216. void DeviceEventManager::Notify_ModelChanged(ifc_device *device, const wchar_t *model)
  217. {
  218. Lock();
  219. size_t index, count = handlerList.size();
  220. for(index = 0; index < count;index++)
  221. {
  222. ifc_deviceevent *handler = handlerList[index];
  223. handler->ModelChanged(device, model);
  224. }
  225. Unlock();
  226. }
  227. void DeviceEventManager::Notify_StatusChanged(ifc_device *device, const wchar_t *status)
  228. {
  229. Lock();
  230. size_t index, count = handlerList.size();
  231. for(index = 0; index < count;index++)
  232. {
  233. ifc_deviceevent *handler = handlerList[index];
  234. handler->StatusChanged(device, status);
  235. }
  236. Unlock();
  237. }
  238. #define CBCLASS DeviceEventManager
  239. START_DISPATCH;
  240. CB(ADDREF, AddRef)
  241. CB(RELEASE, Release)
  242. CB(QUERYINTERFACE, QueryInterface)
  243. CB(API_ADVISE, Advise)
  244. CB(API_UNADVISE, Unadvise)
  245. VCB(API_NOTIFY_ICONCHANGED, Notify_IconChanged)
  246. VCB(API_NOTIFY_DISPLAYNAMECHANGED, Notify_DisplayNameChanged)
  247. VCB(API_NOTIFY_ATTACHMENTCHANGED, Notify_AttachmentChanged)
  248. VCB(API_NOTIFY_VISIBILITYCHANGED, Notify_VisibilityChanged)
  249. VCB(API_NOTIFY_TOTALSPACECHANGED, Notify_TotalSpaceChanged)
  250. VCB(API_NOTIFY_USEDSPACECHANGED, Notify_UsedSpaceChanged)
  251. VCB(API_NOTIFY_COMMANDCHANGED, Notfiy_CommandChanged)
  252. VCB(API_NOTIFY_ACTIVITYSTARTED, Notify_ActivityStarted)
  253. VCB(API_NOTIFY_ACTIVITYFINISHED, Notify_ActivityFinished)
  254. VCB(API_NOTIFY_ACTIVITYCHANGED, Notify_ActivityChanged)
  255. VCB(API_NOTIFY_MODELCHANGED, Notify_ModelChanged)
  256. VCB(API_NOTIFY_STATUSCHANGED, Notify_StatusChanged)
  257. END_DISPATCH;
  258. #undef CBCLASS