add 2206 to copyright notice
[platform/upstream/flac.git] / src / plugin_xmms / tag.c
1 /* libxmms-flac - XMMS FLAC input plugin
2  * Copyright (C) 2000,2001,2002,2003,2004,2005,2006  Josh Coalson
3  * Copyright (C) 2002,2003,2004,2005,2006  Daisuke Shimamura
4  *
5  * Based on FLAC plugin.c and mpg123 plugin
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <glib.h>
26 #include <xmms/plugin.h>
27 #include <xmms/util.h>
28 #include <xmms/configfile.h>
29 #include <xmms/titlestring.h>
30
31 #include "FLAC/metadata.h"
32 #include "plugin_common/tags.h"
33 #include "charset.h"
34 #include "configure.h"
35
36 /*
37  * Function local__extname (filename)
38  *
39  *    Return pointer within filename to its extenstion, or NULL if
40  *    filename has no extension.
41  *
42  */
43 static char *local__extname(const char *filename)
44 {
45         char *ext = strrchr(filename, '.');
46
47         if (ext != NULL)
48                 ++ext;
49
50         return ext;
51 }
52
53 static char *local__getstr(char* str)
54 {
55         if (str && strlen(str) > 0)
56                 return str;
57         return NULL;
58 }
59
60 static int local__getnum(char* str)
61 {
62         if (str && strlen(str) > 0)
63                 return atoi(str);
64         return 0;
65 }
66
67 static char *local__getfield(const FLAC__StreamMetadata *tags, const char *name)
68 {
69         if (0 != tags) {
70                 const char *utf8 = FLAC_plugin__tags_get_tag_utf8(tags, name);
71                 if (0 != utf8) {
72                         if(flac_cfg.title.convert_char_set)
73                                 return convert_from_utf8_to_user(utf8);
74                         else
75                                 return strdup(utf8);
76                 }
77         }
78
79         return 0;
80 }
81
82 static void local__safe_free(char *s)
83 {
84         if (0 != s)
85                 free(s);
86 }
87
88 /*
89  * Function flac_format_song_title (tag, filename)
90  *
91  *    Create song title according to `tag' and/or `filename' and
92  *    return it.  The title must be subsequently freed using g_free().
93  *
94  */
95 char *flac_format_song_title(char *filename)
96 {
97         char *ret = NULL;
98         TitleInput *input = NULL;
99         FLAC__StreamMetadata *tags;
100         char *title, *artist, *performer, *album, *date, *tracknumber, *genre, *description;
101
102         FLAC_plugin__tags_get(filename, &tags);
103
104         title       = local__getfield(tags, "TITLE");
105         artist      = local__getfield(tags, "ARTIST");
106         performer   = local__getfield(tags, "PERFORMER");
107         album       = local__getfield(tags, "ALBUM");
108         date        = local__getfield(tags, "DATE");
109         tracknumber = local__getfield(tags, "TRACKNUMBER");
110         genre       = local__getfield(tags, "GENRE");
111         description = local__getfield(tags, "DESCRIPTION");
112
113         XMMS_NEW_TITLEINPUT(input);
114
115         input->performer = local__getstr(performer);
116         if(!input->performer)
117                 input->performer = local__getstr(artist);
118         input->album_name = local__getstr(album);
119         input->track_name = local__getstr(title);
120         input->track_number = local__getnum(tracknumber);
121         input->year = local__getnum(date);
122         input->genre = local__getstr(genre);
123         input->comment = local__getstr(description);
124
125         input->file_name = g_basename(filename);
126         input->file_path = filename;
127         input->file_ext = local__extname(filename);
128         ret = xmms_get_titlestring(flac_cfg.title.tag_override ? flac_cfg.title.tag_format : xmms_get_gentitle_format(), input);
129         g_free(input);
130
131         if (!ret) {
132                 /*
133                  * Format according to filename.
134                  */
135                 ret = g_strdup(g_basename(filename));
136                 if (local__extname(ret) != NULL)
137                         *(local__extname(ret) - 1) = '\0';      /* removes period */
138         }
139
140         FLAC_plugin__tags_destroy(&tags);
141         local__safe_free(title);
142         local__safe_free(artist);
143         local__safe_free(performer);
144         local__safe_free(album);
145         local__safe_free(date);
146         local__safe_free(tracknumber);
147         local__safe_free(genre);
148         local__safe_free(description);
149         return ret;
150 }