/src/ftk_sources_manager.c

http://ftk.googlecode.com/ · C · 155 lines · 95 code · 31 blank · 29 comment · 22 complexity · e272ef7a73a73dc7e0a35ab43d3edfc2 MD5 · raw file

  1. /*
  2. * File: ftk_source_manager.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: sources manager
  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-10-15 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "ftk_sources_manager.h"
  31. struct _FtkSourcesManager
  32. {
  33. int source_nr;
  34. int need_refresh;
  35. int max_source_nr;
  36. FtkSource* sources[1];
  37. };
  38. #define FTK_DEFAULT_SOURCE_NR 32
  39. FtkSourcesManager* ftk_sources_manager_create(int max_source_nr)
  40. {
  41. FtkSourcesManager* thiz = NULL;
  42. max_source_nr = max_source_nr < FTK_DEFAULT_SOURCE_NR ? FTK_DEFAULT_SOURCE_NR : max_source_nr;
  43. thiz = (FtkSourcesManager*)FTK_ZALLOC(sizeof(FtkSourcesManager) + sizeof(FtkSource*)*(max_source_nr + 1));
  44. if(thiz != NULL)
  45. {
  46. thiz->max_source_nr = max_source_nr;
  47. }
  48. return thiz;
  49. }
  50. Ret ftk_sources_manager_add(FtkSourcesManager* thiz, FtkSource* source)
  51. {
  52. return_val_if_fail(thiz != NULL && source != NULL, RET_FAIL);
  53. return_val_if_fail(thiz->source_nr < thiz->max_source_nr, RET_FAIL);
  54. source->active = 1;
  55. thiz->sources[thiz->source_nr++] = source;
  56. ftk_sources_manager_set_need_refresh(thiz);
  57. return RET_OK;
  58. }
  59. Ret ftk_sources_manager_remove(FtkSourcesManager* thiz, FtkSource* source)
  60. {
  61. int i = 0;
  62. return_val_if_fail(thiz != NULL && source != NULL, RET_FAIL);
  63. for(i = 0; i < thiz->source_nr; i++)
  64. {
  65. if(thiz->sources[i] == source)
  66. {
  67. break;
  68. }
  69. }
  70. if(i < thiz->source_nr)
  71. {
  72. for(; (i + 1) < thiz->source_nr; i++)
  73. {
  74. thiz->sources[i] = thiz->sources[i+1];
  75. }
  76. thiz->source_nr--;
  77. thiz->sources[thiz->source_nr] = NULL;
  78. source->active = 0;
  79. ftk_source_unref(source);
  80. }
  81. ftk_sources_manager_set_need_refresh(thiz);
  82. return RET_OK;
  83. }
  84. int ftk_sources_manager_get_count(FtkSourcesManager* thiz)
  85. {
  86. return_val_if_fail(thiz != NULL, 0);
  87. return thiz->source_nr;
  88. }
  89. FtkSource* ftk_sources_manager_get(FtkSourcesManager* thiz, int i)
  90. {
  91. return_val_if_fail(thiz != NULL && i < thiz->source_nr, NULL);
  92. return thiz->sources[i];
  93. }
  94. int ftk_sources_manager_need_refresh(FtkSourcesManager* thiz)
  95. {
  96. int need_refresh = 0;
  97. return_val_if_fail(thiz != NULL, 0);
  98. if(thiz->need_refresh > 0)
  99. {
  100. need_refresh = thiz->need_refresh;
  101. thiz->need_refresh--;
  102. }
  103. return need_refresh;
  104. }
  105. Ret ftk_sources_manager_set_need_refresh(FtkSourcesManager* thiz)
  106. {
  107. return_val_if_fail(thiz != NULL, RET_FAIL);
  108. thiz->need_refresh++;
  109. return RET_OK;
  110. }
  111. void ftk_sources_manager_destroy(FtkSourcesManager* thiz)
  112. {
  113. int i = 0;
  114. if(thiz != NULL)
  115. {
  116. for(i = 0; i < thiz->source_nr; i++)
  117. {
  118. ftk_source_unref(thiz->sources[i]);
  119. thiz->sources[i] = NULL;
  120. }
  121. FTK_ZFREE(thiz, sizeof(FtkSourcesManager) + sizeof(FtkSource*)*(thiz->max_source_nr + 1));
  122. }
  123. return;
  124. }