PageRenderTime 102ms CodeModel.GetById 81ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/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/*
 26 * History:
 27 * ================================================================
 28 * 2009-11-04 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31#include "ftk_util.h"
 32#include "ftk_globals.h"
 33#include "ftk_icon_cache.h"
 34#include "ftk_bitmap_factory.h"
 35
 36typedef struct _FtkBitmapNamePair
 37{
 38	char* filename;
 39	FtkBitmap* bitmap;
 40}FtkBitmapNamePair;
 41
 42struct _FtkIconCache
 43{
 44	int nr;
 45	char rel_path[FTK_MAX_PATH];
 46	char path[FTK_ICON_PATH_NR][FTK_MAX_PATH];
 47	FtkBitmapNamePair pairs[FTK_ICON_CACHE_MAX];
 48};
 49
 50static FtkBitmap* ftk_icon_cache_find(FtkIconCache* thiz, const char* filename)
 51{
 52	int i = 0;
 53	return_val_if_fail(thiz != NULL && filename != NULL, NULL);
 54
 55	for(i = 0; i < thiz->nr; i++)
 56	{
 57		if(strcmp(filename, thiz->pairs[i].filename) == 0)
 58		{
 59			ftk_bitmap_ref(thiz->pairs[i].bitmap);
 60			return thiz->pairs[i].bitmap;
 61		}
 62	}
 63
 64	return NULL;
 65}
 66
 67static FtkBitmap* ftk_icon_cache_real_load(FtkIconCache* thiz, const char* filename)
 68{
 69	size_t i = 0;
 70	FtkBitmap* bitmap = NULL;
 71	char path[FTK_MAX_PATH+1] = {0};
 72	return_val_if_fail(thiz != NULL && filename != NULL && strlen(filename) > 0, NULL);
 73
 74	for(i = 0; i < FTK_ICON_PATH_NR; i++)
 75	{
 76
 77		ftk_strs_cat(path, FTK_MAX_PATH, thiz->path[i], "/", thiz->rel_path, "/", filename, NULL);
 78		ftk_normalize_path(path);
 79		if((bitmap = ftk_bitmap_factory_load(ftk_default_bitmap_factory(), path)) != NULL)
 80		{
 81			return bitmap;
 82		}
 83	}
 84
 85	return bitmap;
 86}
 87
 88static const char* s_default_path[FTK_ICON_PATH_NR];
 89static void ftk_init_default_path()
 90{
 91	s_default_path[0] = ftk_config_get_data_root_dir(ftk_default_config());;
 92	s_default_path[1] = ftk_config_get_data_dir(ftk_default_config());
 93	s_default_path[2] = ftk_config_get_test_data_dir(ftk_default_config());
 94
 95	return;
 96}
 97
 98FtkIconCache* ftk_icon_cache_create(const char* root_path[FTK_ICON_PATH_NR], const char* rel_path)
 99{
100	FtkIconCache* thiz = FTK_NEW(FtkIconCache);
101
102	ftk_init_default_path();
103	if(thiz != NULL)
104	{
105		size_t i = 0;
106		if(root_path == NULL)
107		{
108			root_path = s_default_path;
109		}
110
111		if(rel_path != NULL)
112		{
113			ftk_strncpy(thiz->rel_path, rel_path, FTK_MAX_PATH);
114		}
115		else
116		{
117			thiz->rel_path[0] = '\0';
118		}
119
120		for(i = 0; i < FTK_ICON_PATH_NR; i++)
121		{
122			if(root_path[i] != NULL)
123			{
124				ftk_strncpy(thiz->path[i], root_path[i], FTK_MAX_PATH);
125			}
126			else
127			{
128				thiz->path[i][0] = '\0';
129			}
130		}
131	}
132
133	return thiz;
134}
135
136static Ret ftk_icon_cache_add(FtkIconCache* thiz, const char* filename, FtkBitmap* bitmap)
137{
138	if(thiz->nr < FTK_ICON_CACHE_MAX)
139	{
140		ftk_bitmap_ref(bitmap);
141		thiz->pairs[thiz->nr].bitmap = bitmap;
142		thiz->pairs[thiz->nr].filename = FTK_STRDUP(filename);
143		thiz->nr++;
144	}
145
146	return RET_OK;
147}
148
149FtkBitmap* ftk_icon_cache_load(FtkIconCache* thiz, const char* filename)
150{
151	FtkBitmap* bitmap = NULL;
152	return_val_if_fail(thiz != NULL && filename != NULL, NULL);
153
154	if((bitmap = ftk_icon_cache_find(thiz, filename)) == NULL)
155	{
156		if((bitmap = ftk_icon_cache_real_load(thiz, filename)) != NULL)
157		{
158			ftk_icon_cache_add(thiz, filename, bitmap);
159		}
160	}
161
162	return bitmap;
163}
164
165void ftk_icon_cache_destroy(FtkIconCache* thiz)
166{
167	int i = 0;
168	
169	if(thiz != NULL)
170	{
171		for(i = 0; i < thiz->nr; i++)
172		{
173			ftk_bitmap_unref(thiz->pairs[i].bitmap);
174			FTK_FREE(thiz->pairs[i].filename);
175		}
176
177		FTK_ZFREE(thiz, sizeof(FtkIconCache));
178	}
179
180	return;
181}
182
183