iconStore.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #include "main.h"
  2. #include "./iconStore.h"
  3. #include <vector>
  4. typedef struct IconStoreRecord
  5. {
  6. unsigned int width;
  7. unsigned int height;
  8. wchar_t *path;
  9. } IconStoreRecord;
  10. typedef std::vector<IconStoreRecord> RecodList;
  11. struct IconStore
  12. {
  13. RecodList list;
  14. wchar_t *basePath;
  15. };
  16. IconStore *
  17. IconStore_Create()
  18. {
  19. IconStore *self;
  20. self = new IconStore();
  21. if (NULL == self)
  22. return NULL;
  23. self->basePath = NULL;
  24. return self;
  25. }
  26. void
  27. IconStore_Destroy(IconStore *self)
  28. {
  29. size_t index;
  30. IconStoreRecord *record;
  31. if (NULL == self)
  32. return;
  33. index = self->list.size();
  34. while(index--)
  35. {
  36. record = &self->list[index];
  37. String_Free(record->path);
  38. }
  39. String_Free(self->basePath);
  40. }
  41. BOOL
  42. IconStore_Add(IconStore *self, const wchar_t *path, unsigned int width, unsigned int height)
  43. {
  44. IconStoreRecord record, *prec;
  45. size_t index;
  46. if (NULL == self)
  47. return FALSE;
  48. if(width < 1)
  49. width = 1;
  50. if(height < 1)
  51. height = 1;
  52. index = self->list.size();
  53. while(index--)
  54. {
  55. prec = &self->list[index];
  56. if (width == prec->width &&
  57. height == prec->height)
  58. {
  59. String_Free(prec->path);
  60. prec->path = String_Duplicate(path);
  61. return TRUE;
  62. }
  63. }
  64. record.path = String_Duplicate(path);
  65. record.width = width;
  66. record.height = height;
  67. self->list.push_back(record);
  68. return TRUE;
  69. }
  70. BOOL
  71. IconStore_RemovePath(IconStore *self, const wchar_t *path)
  72. {
  73. size_t index;
  74. IconStoreRecord *record;
  75. if (NULL == self)
  76. return FALSE;
  77. index = self->list.size();
  78. while(index--)
  79. {
  80. record = &self->list[index];
  81. if(CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, record->path, -1, path, -1))
  82. {
  83. self->list.eraseAt(index);
  84. }
  85. }
  86. return TRUE;
  87. }
  88. BOOL
  89. IconStore_Remove(IconStore *self, unsigned int width, unsigned int height)
  90. {
  91. size_t index;
  92. IconStoreRecord *record;
  93. if (NULL == self)
  94. return FALSE;
  95. index = self->list.size();
  96. while(index--)
  97. {
  98. record = &self->list[index];
  99. if(record->width == width &&
  100. record->height == height)
  101. {
  102. self->list.eraseAt(index);
  103. }
  104. }
  105. return TRUE;
  106. }
  107. static BOOL
  108. IconStore_GetFullPath(IconStore *self, const wchar_t *path, wchar_t *buffer, size_t bufferMax)
  109. {
  110. if (NULL == buffer)
  111. return FALSE;
  112. if (NULL == self)
  113. return FALSE;
  114. if (FALSE != IS_STRING_EMPTY(path))
  115. {
  116. *buffer = L'\0';
  117. return TRUE;
  118. }
  119. if (FALSE == PathIsRelative(path) ||
  120. IS_STRING_EMPTY(self->basePath))
  121. {
  122. if (0 == String_CopyTo(buffer, path, bufferMax) &&
  123. FALSE == IS_STRING_EMPTY(path))
  124. return FALSE;
  125. else
  126. return TRUE;
  127. }
  128. if (NULL == PathCombine(buffer, self->basePath, path))
  129. return FALSE;
  130. return TRUE;
  131. }
  132. BOOL
  133. IconStore_Get(IconStore *self, wchar_t *buffer, size_t bufferMax, unsigned int width, unsigned int height)
  134. {
  135. const wchar_t *icon;
  136. IconStoreRecord *record;
  137. size_t index;
  138. double widthDbl, heightDbl;
  139. double scaleMin, scaleHorz, scaleVert;
  140. if (NULL == self)
  141. return FALSE;
  142. if (NULL == buffer)
  143. return FALSE;
  144. icon = NULL;
  145. widthDbl = width;
  146. heightDbl = height;
  147. index = self->list.size();
  148. if (index > 0)
  149. {
  150. record = &self->list[--index];
  151. scaleHorz = widthDbl/record->width;
  152. scaleVert = heightDbl/record->height;
  153. scaleMin = (scaleHorz < scaleVert) ? scaleHorz : scaleVert;
  154. icon = record->path;
  155. if (1.0 != scaleMin)
  156. {
  157. scaleMin = fabs(1.0 - scaleMin);
  158. while(index--)
  159. {
  160. record = &self->list[index];
  161. scaleHorz = widthDbl/record->width;
  162. scaleVert = heightDbl/record->height;
  163. if (scaleHorz > scaleVert)
  164. scaleHorz = scaleVert;
  165. if (1.0 == scaleHorz)
  166. {
  167. icon = record->path;
  168. break;
  169. }
  170. scaleHorz = fabs(1.0 - scaleHorz);
  171. if (scaleHorz < scaleMin)
  172. {
  173. scaleMin = scaleHorz;
  174. icon = record->path;
  175. }
  176. }
  177. }
  178. }
  179. return IconStore_GetFullPath(self, icon, buffer, bufferMax);
  180. }
  181. BOOL
  182. IconStore_SetBasePath(IconStore *self, const wchar_t *path)
  183. {
  184. if (NULL == self)
  185. return FALSE;
  186. String_Free(self->basePath);
  187. self->basePath = String_Duplicate(path);
  188. return TRUE;
  189. }
  190. IconStore *
  191. IconStore_Clone(IconStore *self)
  192. {
  193. size_t index;
  194. IconStore *clone;
  195. IconStoreRecord targetRec;
  196. const IconStoreRecord *sourceRec;
  197. if (NULL == self)
  198. return NULL;
  199. clone = IconStore_Create();
  200. if (NULL == clone)
  201. return NULL;
  202. clone->basePath = String_Duplicate(self->basePath);
  203. for(index = 0; index < self->list.size(); index++)
  204. {
  205. sourceRec = &self->list[index];
  206. targetRec.path = String_Duplicate(sourceRec->path);
  207. targetRec.width = sourceRec->width;
  208. targetRec.height = sourceRec->height;
  209. clone->list.push_back(targetRec);
  210. }
  211. return clone;
  212. }
  213. BOOL
  214. IconStore_Enumerate(IconStore *self, IconEnumerator callback, void *user)
  215. {
  216. size_t index;
  217. wchar_t buffer[MAX_PATH*2];
  218. IconStoreRecord *record;
  219. if (NULL == self || NULL == callback)
  220. return FALSE;
  221. index = self->list.size();
  222. while(index--)
  223. {
  224. record = &self->list[index];
  225. if (FALSE != IconStore_GetFullPath(self, record->path, buffer, ARRAYSIZE(buffer)))
  226. {
  227. if (FALSE == callback(buffer, record->width, record->height, user))
  228. return TRUE;
  229. }
  230. }
  231. return TRUE;
  232. }