PageRenderTime 21ms CodeModel.GetById 10ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/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/*
 26 * History:
 27 * ================================================================
 28 * 2009-10-15 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31
 32#include "ftk_sources_manager.h"
 33
 34struct _FtkSourcesManager
 35{
 36	int source_nr;
 37	int need_refresh;
 38	int max_source_nr;
 39	FtkSource* sources[1];
 40};
 41
 42#define FTK_DEFAULT_SOURCE_NR 32
 43FtkSourcesManager* ftk_sources_manager_create(int max_source_nr)
 44{
 45	FtkSourcesManager* thiz = NULL;
 46	max_source_nr = max_source_nr < FTK_DEFAULT_SOURCE_NR ? FTK_DEFAULT_SOURCE_NR : max_source_nr;
 47
 48	thiz = (FtkSourcesManager*)FTK_ZALLOC(sizeof(FtkSourcesManager) + sizeof(FtkSource*)*(max_source_nr + 1));
 49
 50	if(thiz != NULL)
 51	{
 52		thiz->max_source_nr = max_source_nr;
 53	}
 54
 55	return thiz;
 56}
 57
 58Ret  ftk_sources_manager_add(FtkSourcesManager* thiz, FtkSource* source)
 59{
 60	return_val_if_fail(thiz != NULL && source != NULL, RET_FAIL);
 61	return_val_if_fail(thiz->source_nr < thiz->max_source_nr, RET_FAIL);
 62
 63	source->active = 1;
 64	thiz->sources[thiz->source_nr++] = source;
 65
 66	ftk_sources_manager_set_need_refresh(thiz);
 67
 68	return RET_OK;
 69}
 70
 71Ret  ftk_sources_manager_remove(FtkSourcesManager* thiz, FtkSource* source)
 72{
 73	int i = 0;
 74	return_val_if_fail(thiz != NULL && source != NULL, RET_FAIL);
 75
 76	for(i = 0; i < thiz->source_nr; i++)
 77	{
 78		if(thiz->sources[i] == source)
 79		{
 80			break;
 81		}
 82	}
 83	
 84	if(i < thiz->source_nr)
 85	{
 86		for(; (i + 1) < thiz->source_nr; i++)
 87		{
 88			thiz->sources[i] = thiz->sources[i+1];
 89		}
 90		thiz->source_nr--;
 91		thiz->sources[thiz->source_nr] = NULL;
 92		source->active = 0;
 93		ftk_source_unref(source);
 94	}
 95	ftk_sources_manager_set_need_refresh(thiz);
 96
 97	return RET_OK;
 98}
 99
100int  ftk_sources_manager_get_count(FtkSourcesManager* thiz)
101{
102	return_val_if_fail(thiz != NULL, 0);
103
104	return thiz->source_nr;
105}
106
107FtkSource* ftk_sources_manager_get(FtkSourcesManager* thiz, int i)
108{
109	return_val_if_fail(thiz != NULL && i < thiz->source_nr, NULL);
110
111	return thiz->sources[i];
112}
113
114int ftk_sources_manager_need_refresh(FtkSourcesManager* thiz)
115{
116	int need_refresh = 0;
117	return_val_if_fail(thiz != NULL, 0);
118
119	if(thiz->need_refresh > 0)
120	{
121		need_refresh = thiz->need_refresh;
122		thiz->need_refresh--;
123	}
124
125	return need_refresh;
126}
127
128Ret  ftk_sources_manager_set_need_refresh(FtkSourcesManager* thiz)
129{
130	return_val_if_fail(thiz != NULL, RET_FAIL);
131
132	thiz->need_refresh++;
133
134	return RET_OK;
135}
136
137void ftk_sources_manager_destroy(FtkSourcesManager* thiz)
138{
139	int i = 0;
140
141	if(thiz != NULL)
142	{
143		for(i = 0; i < thiz->source_nr; i++)
144		{
145			ftk_source_unref(thiz->sources[i]);
146			thiz->sources[i] = NULL;
147		}
148
149		FTK_ZFREE(thiz, sizeof(FtkSourcesManager) + sizeof(FtkSource*)*(thiz->max_source_nr + 1));
150	}
151
152	return;
153}
154
155