1
0

deviceCommands.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include "main.h"
  2. #include "deviceCommands.h"
  3. typedef struct DeviceCommandInfo
  4. {
  5. const char *name;
  6. unsigned int title;
  7. unsigned int description;
  8. unsigned int smallIcon;
  9. unsigned int largeIcon;
  10. } DeviceCommandInfo;
  11. static DeviceCommandInfo registeredCommands[] =
  12. {
  13. { "sync",
  14. IDS_SYNC_COMMAND_TITLE,
  15. IDS_SYNC_COMMAND_DESC,
  16. IDR_SYNC_COMMAND_SMALL_IMAGE,
  17. IDR_SYNC_COMMAND_LARGE_IMAGE },
  18. { "cancel_sync",
  19. IDS_CANCEL_SYNC_COMMAND_TITLE,
  20. IDS_CANCEL_SYNC_COMMAND_DESC,
  21. IDR_CANCEL_SYNC_COMMAND_SMALL_IMAGE,
  22. IDR_CANCEL_SYNC_COMMAND_LARGE_IMAGE },
  23. { "attach",
  24. IDS_ATTACH_COMMAND_TITLE,
  25. IDS_ATTACH_COMMAND_DESC,
  26. IDR_ATTACH_COMMAND_SMALL_IMAGE,
  27. IDR_ATTACH_COMMAND_LARGE_IMAGE },
  28. { "detach",
  29. IDS_DETACH_COMMAND_TITLE,
  30. IDS_DETACH_COMMAND_DESC,
  31. IDR_DETACH_COMMAND_SMALL_IMAGE,
  32. IDR_DETACH_COMMAND_LARGE_IMAGE },
  33. { "eject",
  34. IDS_EJECT_COMMAND_TITLE,
  35. IDS_EJECT_COMMAND_DESC,
  36. IDR_EJECT_COMMAND_SMALL_IMAGE,
  37. IDR_EJECT_COMMAND_LARGE_IMAGE },
  38. { "rename",
  39. IDS_RENAME_COMMAND_TITLE,
  40. IDS_RENAME_COMMAND_DESC,
  41. IDR_RENAME_COMMAND_SMALL_IMAGE,
  42. IDR_RENAME_COMMAND_LARGE_IMAGE },
  43. { "view_open",
  44. IDS_VIEW_OPEN_COMMAND_TITLE,
  45. IDS_VIEW_OPEN_COMMAND_DESC,
  46. IDR_VIEW_OPEN_COMMAND_SMALL_IMAGE,
  47. IDR_VIEW_OPEN_COMMAND_LARGE_IMAGE },
  48. { "preferences",
  49. IDS_PREFERENCES_COMMAND_TITLE,
  50. IDS_PREFERENCES_COMMAND_DESC,
  51. IDR_PREFERENCES_COMMAND_SMALL_IMAGE,
  52. IDR_PREFERENCES_COMMAND_LARGE_IMAGE },
  53. { "playlist_create",
  54. IDS_PLAYLIST_CREATE_COMMAND_TITLE,
  55. IDS_PLAYLIST_CREATE_COMMAND_DESC,
  56. IDR_PLAYLIST_CREATE_COMMAND_SMALL_IMAGE,
  57. IDR_PLAYLIST_CREATE_COMMAND_LARGE_IMAGE },
  58. };
  59. static ifc_devicecommand * _cdecl
  60. DeviceCommands_RegisterCommandCb(const char *name, void *user)
  61. {
  62. size_t index;
  63. wchar_t buffer[2048] = {0};
  64. DeviceCommandInfo *commandInfo;
  65. ifc_devicecommand *command;
  66. ifc_devicecommandeditor *editor;
  67. commandInfo = NULL;
  68. for(index = 0; index < ARRAYSIZE(registeredCommands); index++)
  69. {
  70. if (name == registeredCommands[index].name)
  71. {
  72. commandInfo = &registeredCommands[index];
  73. break;
  74. }
  75. }
  76. if (NULL == commandInfo)
  77. return NULL;
  78. if (NULL == WASABI_API_DEVICES)
  79. return NULL;
  80. if (FAILED(WASABI_API_DEVICES->CreateCommand(commandInfo->name, &command)))
  81. return NULL;
  82. if (FAILED(command->QueryInterface(IFC_DeviceCommandEditor, (void**)&editor)))
  83. {
  84. command->Release();
  85. return NULL;
  86. }
  87. if (0 != commandInfo->title)
  88. {
  89. WASABI_API_LNGSTRINGW_BUF(commandInfo->title, buffer, ARRAYSIZE(buffer));
  90. editor->SetDisplayName(buffer);
  91. }
  92. if (0 != commandInfo->description)
  93. {
  94. WASABI_API_LNGSTRINGW_BUF(commandInfo->description, buffer, ARRAYSIZE(buffer));
  95. editor->SetDescription(buffer);
  96. }
  97. if (0 != commandInfo->smallIcon || 0 != commandInfo->largeIcon)
  98. {
  99. ifc_deviceiconstore *iconStore;
  100. if (SUCCEEDED(editor->GetIconStore(&iconStore)))
  101. {
  102. if (0 != commandInfo->smallIcon)
  103. {
  104. if (FALSE != Plugin_GetResourceString(MAKEINTRESOURCE(commandInfo->smallIcon), RT_RCDATA, buffer, ARRAYSIZE(buffer)))
  105. iconStore->Add(buffer, 16, 16, TRUE);
  106. }
  107. if (0 != commandInfo->largeIcon)
  108. {
  109. if (FALSE != Plugin_GetResourceString(MAKEINTRESOURCE(commandInfo->largeIcon), RT_RCDATA, buffer, ARRAYSIZE(buffer)))
  110. iconStore->Add(buffer, 43, 24, TRUE);
  111. }
  112. iconStore->Release();
  113. }
  114. }
  115. editor->Release();
  116. return command;
  117. }
  118. BOOL
  119. DeviceCommands_Register()
  120. {
  121. const char *commands[ARRAYSIZE(registeredCommands)];
  122. size_t index;
  123. if (NULL == WASABI_API_DEVICES)
  124. return FALSE;
  125. for(index = 0; index < ARRAYSIZE(commands); index++)
  126. {
  127. commands[index] = registeredCommands[index].name;
  128. }
  129. WASABI_API_DEVICES->CommandRegisterIndirect(commands, ARRAYSIZE(commands), DeviceCommands_RegisterCommandCb, NULL);
  130. return TRUE;
  131. }
  132. BOOL
  133. DeviceCommand_GetSupported(ifc_device *device, const char *name, DeviceCommandContext context,
  134. ifc_devicesupportedcommand **commandOut)
  135. {
  136. ifc_devicesupportedcommandenum *enumerator;
  137. ifc_devicesupportedcommand *command;
  138. BOOL foundCommand;
  139. if (NULL == device ||
  140. NULL == name ||
  141. FAILED(device->EnumerateCommands(&enumerator, context)))
  142. {
  143. return FALSE;
  144. }
  145. foundCommand = FALSE;
  146. while (S_OK == enumerator->Next(&command, 1, NULL))
  147. {
  148. if (CSTR_EQUAL == CompareStringA(CSTR_INVARIANT, 0, name, -1, command->GetName(), -1))
  149. {
  150. foundCommand = TRUE;
  151. if (NULL != commandOut)
  152. *commandOut = command;
  153. else
  154. command->Release();
  155. break;
  156. }
  157. command->Release();
  158. }
  159. enumerator->Release();
  160. return foundCommand;
  161. }
  162. BOOL
  163. DeviceCommand_GetEnabled(ifc_device *device, const char *name, DeviceCommandContext context)
  164. {
  165. ifc_devicesupportedcommand *command;
  166. DeviceCommandFlags flags;
  167. if (FALSE == DeviceCommand_GetSupported(device, name, context, &command))
  168. return FALSE;
  169. if (FAILED(command->GetFlags(&flags)))
  170. flags = DeviceCommandFlag_Disabled;
  171. command->Release();
  172. return (0 == (DeviceCommandFlag_Disabled & flags));
  173. }