deviceConnectionParser.cpp 4.1 KB

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