1
0

modelInfo.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include "main.h"
  2. #include "./modelInfo.h"
  3. #include "./api.h"
  4. #include "./resource.h"
  5. #include <strsafe.h>
  6. typedef struct ManufacturerInfo
  7. {
  8. const wchar_t *name;
  9. const ModelInfo *records;
  10. size_t count;
  11. } ManufacturerInfo;
  12. #define MANUFACTURER(_name, _model_table)\
  13. { (_name), (_model_table), ARRAYSIZE(_model_table) }
  14. #define MODEL(_name, _title_res_id, _small_icon_res_id, _large_icon_res_id)\
  15. { (_name), MAKEINTRESOURCE(_title_res_id), MAKEINTRESOURCE(_small_icon_res_id), MAKEINTRESOURCE(_large_icon_res_id)}
  16. const struct ModelInfo HtcModelTable[] =
  17. {
  18. MODEL(L"PC36100", IDS_DEVICE_MODEL_HTC_EVO, IDB_EVO_16, IDB_EVO_160),
  19. MODEL(L"ADR6300", IDS_DEVICE_MODEL_HTC_INCREDIBLE, IDB_INCREDIBLE_16, IDB_INCREDIBLE_160),
  20. MODEL(L"Nexus One", IDS_DEVICE_MODEL_HTC_NEXUS_ONE, IDB_NEXUSONE_16, IDB_NEXUSONE_160),
  21. MODEL(L"Desire", IDS_DEVICE_MODEL_HTC_DESIRE, IDB_HTC_DESIRE_16, IDB_HTC_DESIRE_160),
  22. MODEL(L"Wildfire", IDS_DEVICE_MODEL_HTC_WILDFIRE, 0, 0),
  23. MODEL(L"Hero", IDS_DEVICE_MODEL_HTC_HERO, 0, 0),
  24. };
  25. const struct ModelInfo MotorolaModelTable[] =
  26. {
  27. MODEL(L"Droid", IDS_DEVICE_MODEL_MOTOROLA_DROID, IDB_DROID_16, IDB_DROID_160),
  28. MODEL(L"DROID2", IDS_DEVICE_MODEL_MOTOROLA_DROID2, IDB_DROID_16, IDB_DROID_160),
  29. MODEL(L"DROIDX", IDS_DEVICE_MODEL_MOTOROLA_DROIDX, IDB_DROIDX_16, IDB_DROIDX_160),
  30. MODEL(L"Milestone", IDS_DEVICE_MODEL_MOTOROLA_MILESTONE, IDB_DROID_16, IDB_DROID_160),
  31. MODEL(L"DROIDX2", IDS_DEVICE_MODEL_MOTOROLA_DROIDX2, IDB_DROID_16, IDB_DROID_160),
  32. };
  33. const struct ModelInfo SamsungModelTable[] =
  34. {
  35. MODEL(L"GT I9000", IDS_DEVICE_MODEL_SAMSUNG_GALAXY_S, 0, 0),
  36. };
  37. const static ManufacturerInfo ManufacturerTable[] =
  38. {
  39. MANUFACTURER(L"HTC", HtcModelTable),
  40. MANUFACTURER(L"motorola", MotorolaModelTable),
  41. MANUFACTURER(L"samsung", SamsungModelTable),
  42. };
  43. const static ModelInfo defaultModel = { L"", L"", MAKEINTRESOURCE(IDB_GENERIC_16), MAKEINTRESOURCE(IDB_GENERIC_160)};
  44. const ModelInfo *
  45. FindModelInfo(const wchar_t *manufacturer, const wchar_t *model, BOOL allowDefault)
  46. {
  47. if (NULL != manufacturer &&
  48. NULL != model)
  49. {
  50. const ManufacturerInfo *manufacturerInfo;
  51. size_t i;
  52. unsigned long lcid;
  53. lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
  54. for (i = 0; i < ARRAYSIZE(ManufacturerTable); i++)
  55. {
  56. manufacturerInfo = &ManufacturerTable[i];
  57. if (CSTR_EQUAL == CompareString(lcid, NORM_IGNORECASE, manufacturerInfo->name, -1, manufacturer, -1))
  58. {
  59. for (i = 0; i < manufacturerInfo->count; i++)
  60. {
  61. const ModelInfo *modelInfo = &manufacturerInfo->records[i];
  62. if (CSTR_EQUAL == CompareString(lcid, NORM_IGNORECASE, modelInfo->name, -1, model, -1))
  63. {
  64. return modelInfo;
  65. }
  66. }
  67. break;
  68. }
  69. }
  70. }
  71. if (FALSE != allowDefault)
  72. return &defaultModel;
  73. return NULL;
  74. }
  75. const ModelInfo*
  76. GetDefaultModelInfo()
  77. {
  78. return &defaultModel;
  79. }
  80. HRESULT ModelInfo_CopyDisplayName(const ModelInfo *modelInfo, wchar_t *buffer, size_t bufferMax)
  81. {
  82. if (NULL == modelInfo)
  83. return E_INVALIDARG;
  84. if (NULL == buffer)
  85. return E_POINTER;
  86. if (NULL == modelInfo->displayName)
  87. return E_UNEXPECTED;
  88. if (FALSE != IS_INTRESOURCE(modelInfo->displayName))
  89. {
  90. WASABI_API_LNGSTRINGW_BUF((int)(intptr_t)modelInfo->displayName, buffer, bufferMax);
  91. return S_OK;
  92. }
  93. if (FAILED(StringCchCopy(buffer, bufferMax, modelInfo->displayName)))
  94. return E_FAIL;
  95. return S_OK;
  96. }
  97. const wchar_t *ModelInfo_GetIconName(const ModelInfo *modelInfo, int width, int height, BOOL allowDefault)
  98. {
  99. if (NULL == modelInfo)
  100. {
  101. if (FALSE == allowDefault)
  102. return NULL;
  103. modelInfo = GetDefaultModelInfo();
  104. if (NULL == modelInfo)
  105. return NULL;
  106. }
  107. if (width <= 16 && height <= 16)
  108. {
  109. if (NULL == modelInfo->smallIcon &&
  110. FALSE != allowDefault)
  111. {
  112. modelInfo = GetDefaultModelInfo();
  113. if (NULL == modelInfo)
  114. return NULL;
  115. }
  116. return modelInfo->smallIcon;
  117. }
  118. if (NULL == modelInfo->largeIcon &&
  119. FALSE != allowDefault)
  120. {
  121. modelInfo = GetDefaultModelInfo();
  122. if (NULL == modelInfo)
  123. return NULL;
  124. }
  125. return modelInfo->largeIcon;
  126. }
  127. HRESULT ModelInfo_GetIconPath(const ModelInfo *modelInfo, int width, int height, wchar_t *buffer, size_t bufferMax, BOOL allowDefault)
  128. {
  129. const wchar_t *iconName;
  130. iconName = ModelInfo_GetIconName(modelInfo, width, height, allowDefault);
  131. if (NULL == iconName)
  132. return E_FAIL;
  133. if (FALSE == FormatResProtocol(iconName, L"PNG", buffer, bufferMax))
  134. return E_FAIL;
  135. return S_OK;
  136. }