listWidgetConnection.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include "main.h"
  2. #include "./listWidgetInternal.h"
  3. #define LISTWIDGETCONNECTION_IMAGE_WIDTH 16
  4. #define LISTWIDGETCONNECTION_IMAGE_HEIGHT 16
  5. #define REQUEST_STRING ((wchar_t*)1)
  6. #define REQUEST_IMAGE ((DeviceImage*)1)
  7. typedef struct ListWidgetConnection
  8. {
  9. char *name;
  10. wchar_t *title;
  11. DeviceImage *image;
  12. } ListWidgetConnection;
  13. ListWidgetConnection *
  14. ListWidget_CreateConnection(const char *name)
  15. {
  16. ListWidgetConnection *self;
  17. if (NULL == name)
  18. return NULL;
  19. self = (ListWidgetConnection*)malloc(sizeof(ListWidgetConnection));
  20. if (NULL == self)
  21. return NULL;
  22. ZeroMemory(self, sizeof(ListWidgetConnection));
  23. self->name = AnsiString_Duplicate(name);
  24. if (NULL == self->name)
  25. {
  26. ListWidget_DestroyConnection(self);
  27. return NULL;
  28. }
  29. self->title = REQUEST_STRING;
  30. self->image = REQUEST_IMAGE;
  31. return self;
  32. }
  33. void
  34. ListWidget_DestroyConnection(ListWidgetConnection *connection)
  35. {
  36. if (NULL == connection)
  37. return;
  38. AnsiString_Free(connection->name);
  39. if (NULL != connection->image && REQUEST_IMAGE != connection->image)
  40. DeviceImage_Release(connection->image);
  41. if (REQUEST_STRING != connection->title)
  42. String_Free(connection->title);
  43. free(connection);
  44. }
  45. const wchar_t *
  46. ListWidget_GetConnectionTitle(ListWidgetConnection *connection)
  47. {
  48. if (NULL == connection || NULL == connection->title)
  49. return NULL;
  50. if (REQUEST_STRING == connection->title)
  51. {
  52. ifc_deviceconnection *info;
  53. connection->title = NULL;
  54. if (NULL != WASABI_API_DEVICES &&
  55. S_OK == WASABI_API_DEVICES->ConnectionFind(connection->name, &info))
  56. {
  57. wchar_t buffer[512] = {0};
  58. if (SUCCEEDED(info->GetDisplayName(buffer, ARRAYSIZE(buffer))))
  59. connection->title = String_Duplicate(buffer);
  60. info->Release();
  61. }
  62. }
  63. return connection->title;
  64. }
  65. HBITMAP
  66. ListWidget_GetConnectionImage(WidgetStyle *style, ListWidgetConnection *connection, int width, int height)
  67. {
  68. if (NULL == style || NULL == connection || NULL == connection->image)
  69. return NULL;
  70. if (REQUEST_IMAGE == connection->image)
  71. {
  72. ifc_deviceconnection *info;
  73. connection->image = NULL;
  74. if (NULL != WASABI_API_DEVICES &&
  75. S_OK == WASABI_API_DEVICES->ConnectionFind(connection->name, &info))
  76. {
  77. wchar_t buffer[MAX_PATH * 2] = {0};
  78. if (FAILED(info->GetIcon(buffer, ARRAYSIZE(buffer), width, height)))
  79. buffer[0] = L'\0';
  80. info->Release();
  81. if (L'\0' != buffer[0])
  82. {
  83. connection->image = DeviceImageCache_GetImage(Plugin_GetImageCache(), buffer,
  84. width, height, NULL, NULL);
  85. }
  86. }
  87. }
  88. if (NULL == connection->image)
  89. return NULL;
  90. return DeviceImage_GetBitmap(connection->image, DeviceImage_Normal);
  91. }
  92. BOOL
  93. ListWidget_ConnectionResetColors(WidgetStyle *style, ListWidgetConnection *connection)
  94. {
  95. return FALSE;
  96. }
  97. void
  98. ListWidget_ResetConnnectionsColors(ListWidget *self, WidgetStyle *style)
  99. {
  100. return;
  101. }
  102. BOOL
  103. ListWidget_UpdateConnectionImageSize(ListWidgetConnection *connection, int width, int height)
  104. {
  105. if (NULL == connection)
  106. return FALSE;
  107. if (NULL == connection->image || REQUEST_IMAGE == connection->image)
  108. return TRUE;
  109. DeviceImage_Release(connection->image);
  110. connection->image = REQUEST_IMAGE;
  111. return TRUE;
  112. }
  113. ListWidgetConnection *
  114. ListWidget_FindConnection(ListWidget *self, const char *name)
  115. {
  116. if (NULL == self || FALSE != IS_STRING_EMPTY(name))
  117. return NULL;
  118. size_t index = self->connections.size();
  119. while(index--)
  120. {
  121. ListWidgetConnection *connection = self->connections[index];
  122. if (CSTR_EQUAL == CompareStringA(CSTR_INVARIANT, 0, name, -1, connection->name, -1))
  123. return connection;
  124. }
  125. return NULL;
  126. }
  127. BOOL
  128. ListWidget_AddConnection(ListWidget *self, ListWidgetConnection *connection)
  129. {
  130. if (NULL == self || NULL == connection)
  131. return FALSE;
  132. self->connections.push_back(connection);
  133. return TRUE;
  134. }
  135. void
  136. ListWidget_RemoveConnection(ListWidget *self, const char *name)
  137. {
  138. size_t iCategory, iGroup, iItem;
  139. ListWidgetCategory *category;
  140. ListWidgetGroup *group;
  141. ListWidgetItem *item;
  142. if (NULL == self || FALSE != IS_STRING_EMPTY(name))
  143. return;
  144. size_t index = self->connections.size();
  145. while(index--)
  146. {
  147. ListWidgetConnection *connection = self->connections[index];
  148. if (CSTR_EQUAL == CompareStringA(CSTR_INVARIANT, 0, name, -1, connection->name, -1))
  149. {
  150. for (iCategory = 0; iCategory < self->categories.size(); iCategory++)
  151. {
  152. category = self->categories[iCategory];
  153. for(iGroup = 0; iGroup < category->groups.size(); iGroup++)
  154. {
  155. group = category->groups[iGroup];
  156. for(iItem = 0; iItem < group->items.size(); iItem++)
  157. {
  158. item = group->items[iItem];
  159. if(item->connection == connection)
  160. item->connection = NULL;
  161. }
  162. }
  163. }
  164. self->connections.erase(self->connections.begin() + index);
  165. ListWidget_DestroyConnection(connection);
  166. }
  167. }
  168. }
  169. void
  170. ListWidget_RemoveAllConnections(ListWidget *self)
  171. {
  172. if (NULL == self)
  173. return;
  174. size_t index = self->connections.size();
  175. if (index > 0)
  176. {
  177. size_t iCategory, iGroup, iItem;
  178. ListWidgetCategory *category;
  179. ListWidgetGroup *group;
  180. ListWidgetItem *item;
  181. for (iCategory = 0; iCategory < self->categories.size(); iCategory++)
  182. {
  183. category = self->categories[iCategory];
  184. for(iGroup = 0; iGroup < category->groups.size(); iGroup++)
  185. {
  186. group = category->groups[iGroup];
  187. for(iItem = 0; iItem < group->items.size(); iItem++)
  188. {
  189. item = group->items[iItem];
  190. if(NULL != item->connection)
  191. {
  192. item->connection = NULL;
  193. }
  194. }
  195. }
  196. }
  197. while(index--)
  198. {
  199. ListWidgetConnection *connection = self->connections[index];
  200. ListWidget_DestroyConnection(connection);
  201. }
  202. self->connections.clear();
  203. }
  204. }