2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Image Management
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38 #include "asterisk/sched.h"
39 #include "asterisk/options.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/logger.h"
42 #include "asterisk/file.h"
43 #include "asterisk/image.h"
44 #include "asterisk/translate.h"
45 #include "asterisk/cli.h"
46 #include "asterisk/lock.h"
48 static struct ast_imager *list;
49 AST_MUTEX_DEFINE_STATIC(listlock);
51 int ast_image_register(struct ast_imager *img)
53 if (option_verbose > 1)
54 ast_verbose(VERBOSE_PREFIX_2 "Registered format '%s' (%s)\n", img->name, img->desc);
55 ast_mutex_lock(&listlock);
58 ast_mutex_unlock(&listlock);
62 void ast_image_unregister(struct ast_imager *img)
64 struct ast_imager *i, *prev = NULL;
65 ast_mutex_lock(&listlock);
78 ast_mutex_unlock(&listlock);
79 if (i && (option_verbose > 1))
80 ast_verbose(VERBOSE_PREFIX_2 "Unregistered format '%s' (%s)\n", img->name, img->desc);
83 int ast_supports_images(struct ast_channel *chan)
85 if (!chan || !chan->tech)
87 if (!chan->tech->send_image)
92 static int file_exists(char *filename)
96 res = stat(filename, &st);
102 static void make_filename(char *buf, int len, char *filename, char *preflang, char *ext)
104 if (filename[0] == '/') {
105 if (preflang && strlen(preflang))
106 snprintf(buf, len, "%s-%s.%s", filename, preflang, ext);
108 snprintf(buf, len, "%s.%s", filename, ext);
110 if (preflang && strlen(preflang))
111 snprintf(buf, len, "%s/%s/%s-%s.%s", ast_config_AST_VAR_DIR, "images", filename, preflang, ext);
113 snprintf(buf, len, "%s/%s/%s.%s", ast_config_AST_VAR_DIR, "images", filename, ext);
117 struct ast_frame *ast_read_image(char *filename, char *preflang, int format)
119 struct ast_imager *i;
123 struct ast_imager *found = NULL;
126 struct ast_frame *f = NULL;
127 #if 0 /* We need to have some sort of read-only lock */
128 ast_mutex_lock(&listlock);
132 if (i->format & format) {
134 strncpy(tmp, i->exts, sizeof(tmp)-1);
136 e = strsep(&stringp, "|");
138 make_filename(buf, sizeof(buf), filename, preflang, e);
139 if ((len = file_exists(buf))) {
143 make_filename(buf, sizeof(buf), filename, NULL, e);
144 if ((len = file_exists(buf))) {
148 e = strsep(&stringp, "|");
154 fd = open(buf, O_RDONLY);
156 if (!found->identify || found->identify(fd)) {
157 /* Reset file pointer */
158 lseek(fd, 0, SEEK_SET);
159 f = found->read_image(fd,len);
161 ast_log(LOG_WARNING, "%s does not appear to be a %s file\n", buf, i->name);
164 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", buf, strerror(errno));
166 ast_log(LOG_WARNING, "Image file '%s' not found\n", filename);
168 ast_mutex_unlock(&listlock);
174 int ast_send_image(struct ast_channel *chan, char *filename)
178 if (chan->tech->send_image) {
179 f = ast_read_image(filename, chan->language, -1);
181 res = chan->tech->send_image(chan, f);
188 static int show_image_formats(int fd, int argc, char *argv[])
190 #define FORMAT "%10s %10s %50s %10s\n"
191 #define FORMAT2 "%10s %10s %50s %10s\n"
192 struct ast_imager *i;
194 return RESULT_SHOWUSAGE;
195 ast_cli(fd, FORMAT, "Name", "Extensions", "Description", "Format");
198 ast_cli(fd, FORMAT2, i->name, i->exts, i->desc, ast_getformatname(i->format));
201 return RESULT_SUCCESS;
204 struct ast_cli_entry show_images =
206 { "show", "image", "formats" },
208 "Displays image formats",
209 "Usage: show image formats\n"
210 " displays currently registered image formats (if any)\n"
214 int ast_image_init(void)
216 ast_cli_register(&show_images);