/src/ftk_icon_cache.c

http://ftk.googlecode.com/ · C · 183 lines · 126 code · 28 blank · 29 comment · 33 complexity · 6839b2fce2b65194d15511ae9039a258 MD5 · raw file

  1. /*
  2. * File: ftk_icon_cache.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: cache to load icons.
  5. *
  6. * Copyright (c) 2009 - 2010 Li XianJing <xianjimli@hotmail.com>
  7. *
  8. * Licensed under the Academic Free License version 2.1
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. /*
  25. * History:
  26. * ================================================================
  27. * 2009-11-04 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "ftk_util.h"
  31. #include "ftk_globals.h"
  32. #include "ftk_icon_cache.h"
  33. #include "ftk_bitmap_factory.h"
  34. typedef struct _FtkBitmapNamePair
  35. {
  36. char* filename;
  37. FtkBitmap* bitmap;
  38. }FtkBitmapNamePair;
  39. struct _FtkIconCache
  40. {
  41. int nr;
  42. char rel_path[FTK_MAX_PATH];
  43. char path[FTK_ICON_PATH_NR][FTK_MAX_PATH];
  44. FtkBitmapNamePair pairs[FTK_ICON_CACHE_MAX];
  45. };
  46. static FtkBitmap* ftk_icon_cache_find(FtkIconCache* thiz, const char* filename)
  47. {
  48. int i = 0;
  49. return_val_if_fail(thiz != NULL && filename != NULL, NULL);
  50. for(i = 0; i < thiz->nr; i++)
  51. {
  52. if(strcmp(filename, thiz->pairs[i].filename) == 0)
  53. {
  54. ftk_bitmap_ref(thiz->pairs[i].bitmap);
  55. return thiz->pairs[i].bitmap;
  56. }
  57. }
  58. return NULL;
  59. }
  60. static FtkBitmap* ftk_icon_cache_real_load(FtkIconCache* thiz, const char* filename)
  61. {
  62. size_t i = 0;
  63. FtkBitmap* bitmap = NULL;
  64. char path[FTK_MAX_PATH+1] = {0};
  65. return_val_if_fail(thiz != NULL && filename != NULL && strlen(filename) > 0, NULL);
  66. for(i = 0; i < FTK_ICON_PATH_NR; i++)
  67. {
  68. ftk_strs_cat(path, FTK_MAX_PATH, thiz->path[i], "/", thiz->rel_path, "/", filename, NULL);
  69. ftk_normalize_path(path);
  70. if((bitmap = ftk_bitmap_factory_load(ftk_default_bitmap_factory(), path)) != NULL)
  71. {
  72. return bitmap;
  73. }
  74. }
  75. return bitmap;
  76. }
  77. static const char* s_default_path[FTK_ICON_PATH_NR];
  78. static void ftk_init_default_path()
  79. {
  80. s_default_path[0] = ftk_config_get_data_root_dir(ftk_default_config());;
  81. s_default_path[1] = ftk_config_get_data_dir(ftk_default_config());
  82. s_default_path[2] = ftk_config_get_test_data_dir(ftk_default_config());
  83. return;
  84. }
  85. FtkIconCache* ftk_icon_cache_create(const char* root_path[FTK_ICON_PATH_NR], const char* rel_path)
  86. {
  87. FtkIconCache* thiz = FTK_NEW(FtkIconCache);
  88. ftk_init_default_path();
  89. if(thiz != NULL)
  90. {
  91. size_t i = 0;
  92. if(root_path == NULL)
  93. {
  94. root_path = s_default_path;
  95. }
  96. if(rel_path != NULL)
  97. {
  98. ftk_strncpy(thiz->rel_path, rel_path, FTK_MAX_PATH);
  99. }
  100. else
  101. {
  102. thiz->rel_path[0] = '\0';
  103. }
  104. for(i = 0; i < FTK_ICON_PATH_NR; i++)
  105. {
  106. if(root_path[i] != NULL)
  107. {
  108. ftk_strncpy(thiz->path[i], root_path[i], FTK_MAX_PATH);
  109. }
  110. else
  111. {
  112. thiz->path[i][0] = '\0';
  113. }
  114. }
  115. }
  116. return thiz;
  117. }
  118. static Ret ftk_icon_cache_add(FtkIconCache* thiz, const char* filename, FtkBitmap* bitmap)
  119. {
  120. if(thiz->nr < FTK_ICON_CACHE_MAX)
  121. {
  122. ftk_bitmap_ref(bitmap);
  123. thiz->pairs[thiz->nr].bitmap = bitmap;
  124. thiz->pairs[thiz->nr].filename = FTK_STRDUP(filename);
  125. thiz->nr++;
  126. }
  127. return RET_OK;
  128. }
  129. FtkBitmap* ftk_icon_cache_load(FtkIconCache* thiz, const char* filename)
  130. {
  131. FtkBitmap* bitmap = NULL;
  132. return_val_if_fail(thiz != NULL && filename != NULL, NULL);
  133. if((bitmap = ftk_icon_cache_find(thiz, filename)) == NULL)
  134. {
  135. if((bitmap = ftk_icon_cache_real_load(thiz, filename)) != NULL)
  136. {
  137. ftk_icon_cache_add(thiz, filename, bitmap);
  138. }
  139. }
  140. return bitmap;
  141. }
  142. void ftk_icon_cache_destroy(FtkIconCache* thiz)
  143. {
  144. int i = 0;
  145. if(thiz != NULL)
  146. {
  147. for(i = 0; i < thiz->nr; i++)
  148. {
  149. ftk_bitmap_unref(thiz->pairs[i].bitmap);
  150. FTK_FREE(thiz->pairs[i].filename);
  151. }
  152. FTK_ZFREE(thiz, sizeof(FtkIconCache));
  153. }
  154. return;
  155. }