deviceCommandParser.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include "main.h"
  2. #include "./deviceCommandParser.h"
  3. #include "../../xml/obj_xml.h"
  4. typedef void (*COMMANDTAGCALLBACK)(DeviceCommandParser* /*self*/, ifc_devicecommandeditor* /*editor*/, const wchar_t* /*value*/);
  5. typedef struct COMMANDTAG
  6. {
  7. const wchar_t *name;
  8. BOOL multiEntry;
  9. COMMANDTAGCALLBACK callback;
  10. } COMMANDTAG;
  11. static void
  12. DeviceCommandParser_DisplayNameCb(DeviceCommandParser *self, ifc_devicecommandeditor *editor, const wchar_t *value)
  13. {
  14. editor->SetDisplayName(value);
  15. }
  16. static void
  17. DeviceCommandParser_IconCb(DeviceCommandParser *self, ifc_devicecommandeditor *editor, const wchar_t *value)
  18. {
  19. ifc_deviceiconstore *iconStore;
  20. if (SUCCEEDED(editor->GetIconStore(&iconStore)))
  21. {
  22. iconStore->Add(value, self->iconSize.cx, self->iconSize.cy, TRUE);
  23. iconStore->Release();
  24. }
  25. }
  26. static void
  27. DeviceCommandParser_DescirptionCb(DeviceCommandParser *self, ifc_devicecommandeditor *editor, const wchar_t *value)
  28. {
  29. editor->SetDescription(value);
  30. }
  31. static const COMMANDTAG knownTags[COMMAND_TAG_MAX] =
  32. {
  33. {L"displayName", FALSE, DeviceCommandParser_DisplayNameCb},
  34. {L"icon", TRUE, DeviceCommandParser_IconCb},
  35. {L"description", FALSE, DeviceCommandParser_DescirptionCb},
  36. };
  37. DeviceCommandParser::DeviceCommandParser()
  38. : editor(NULL)
  39. {
  40. }
  41. DeviceCommandParser::~DeviceCommandParser()
  42. {
  43. if (NULL != editor)
  44. editor->Release();
  45. }
  46. BOOL DeviceCommandParser::Begin(obj_xml *reader, ifc_xmlreaderparams *params)
  47. {
  48. const wchar_t *name;
  49. ifc_devicecommand *command;
  50. char *nameAnsi;
  51. if (NULL != editor)
  52. return FALSE;
  53. if (NULL == reader || NULL == params)
  54. return FALSE;
  55. name = params->getItemValue(L"name");
  56. if (NULL == name)
  57. return FALSE;
  58. nameAnsi = String_ToAnsi(CP_UTF8, 0, name, -1, NULL, NULL);
  59. if (NULL == nameAnsi)
  60. return FALSE;
  61. if (NULL != WASABI_API_DEVICES &&
  62. SUCCEEDED(WASABI_API_DEVICES->CreateCommand(nameAnsi, &command)))
  63. {
  64. if(FAILED(command->QueryInterface(IFC_DeviceCommandEditor, (void**)&editor)))
  65. editor = NULL;
  66. command->Release();
  67. }
  68. AnsiString_Free(nameAnsi);
  69. if (NULL == editor)
  70. return FALSE;
  71. reader->xmlreader_registerCallback(L"testprovider\fcommands\fcommand\fdisplayName", this);
  72. reader->xmlreader_registerCallback(L"testprovider\fcommands\fcommand\fdescription", this);
  73. reader->xmlreader_registerCallback(L"testprovider\fcommands\fcommand\ficon", this);
  74. ZeroMemory(hitList, sizeof(hitList));
  75. return TRUE;
  76. }
  77. BOOL DeviceCommandParser::End(obj_xml *reader, ifc_devicecommand **command)
  78. {
  79. BOOL result;
  80. if (NULL != reader)
  81. reader->xmlreader_unregisterCallback(this);
  82. if (NULL == command)
  83. return FALSE;
  84. if (NULL == editor)
  85. return FALSE;
  86. if (NULL != command)
  87. {
  88. if (FAILED(editor->QueryInterface(IFC_DeviceCommand, (void**)command)))
  89. result = FALSE;
  90. else
  91. result = TRUE;
  92. }
  93. else
  94. result = TRUE;
  95. editor->Release();
  96. editor = NULL;
  97. return result;
  98. }
  99. void DeviceCommandParser::Event_XmlStartElement(const wchar_t *xmlpath, const wchar_t *xmltag, ifc_xmlreaderparams *params)
  100. {
  101. elementString.Clear();
  102. if (CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, L"icon", -1, xmltag, -1))
  103. {
  104. const wchar_t *sVal;
  105. int iVal;
  106. sVal = params->getItemValue(L"width");
  107. if (NULL == sVal ||
  108. FALSE == StrToIntEx(sVal, STIF_DEFAULT, &iVal))
  109. {
  110. iVal = 0;
  111. }
  112. iconSize.cx = iVal;
  113. sVal = params->getItemValue(L"height");
  114. if (NULL == sVal ||
  115. FALSE == StrToIntEx(sVal, STIF_DEFAULT, &iVal))
  116. {
  117. iVal = 0;
  118. }
  119. iconSize.cy = iVal;
  120. }
  121. }
  122. void DeviceCommandParser::Event_XmlEndElement(const wchar_t *xmlpath, const wchar_t *xmltag)
  123. {
  124. if (NULL == editor)
  125. return;
  126. for (size_t i = 0; i < COMMAND_TAG_MAX; i++)
  127. {
  128. if (FALSE == hitList[i] &&
  129. CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, knownTags[i].name, -1, xmltag, -1))
  130. {
  131. knownTags[i].callback(this, editor, elementString.Get());
  132. if (FALSE == knownTags[i].multiEntry)
  133. hitList[i] = TRUE;
  134. break;
  135. }
  136. }
  137. }
  138. void DeviceCommandParser::Event_XmlCharData(const wchar_t *xmlpath, const wchar_t *xmltag, const wchar_t *value)
  139. {
  140. elementString.Append(value);
  141. }
  142. void DeviceCommandParser::Event_XmlError(int linenum, int errcode, const wchar_t *errstr)
  143. {
  144. elementString.Clear();
  145. }
  146. #define CBCLASS DeviceCommandParser
  147. START_DISPATCH;
  148. VCB(ONSTARTELEMENT, Event_XmlStartElement)
  149. VCB(ONENDELEMENT, Event_XmlEndElement)
  150. VCB(ONCHARDATA, Event_XmlCharData)
  151. VCB(ONERROR, Event_XmlError)
  152. END_DISPATCH;
  153. #undef CBCLASS