add 2009 copyright
[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,2007,2008,2009  Josh Coalson
3  * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009  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 #if HAVE_CONFIG_H
23 #  include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <glib.h>
30 #include <xmms/plugin.h>
31 #include <xmms/util.h>
32 #include <xmms/configfile.h>
33 #include <xmms/titlestring.h>
34
35 #include "FLAC/metadata.h"
36 #include "plugin_common/tags.h"
37 #include "charset.h"
38 #include "configure.h"
39
40 /*
41  * Function local__extname (filename)
42  *
43  *    Return pointer within filename to its extenstion, or NULL if
44  *    filename has no extension.
45  *
46  */
47 static char *local__extname(const char *filename)
48 {
49         char *ext = strrchr(filename, '.');
50
51         if (ext != NULL)
52                 ++ext;
53
54         return ext;
55 }
56
57 static char *local__getstr(char* str)
58 {
59         if (str && strlen(str) > 0)
60                 return str;
61         return NULL;
62 }
63
64 static int local__getnum(char* str)
65 {
66         if (str && strlen(str) > 0)
67                 return atoi(str);
68         return 0;
69 }
70
71 static char *local__getfield(const FLAC__StreamMetadata *tags, const char *name)
72 {
73         if (0 != tags) {
74                 const char *utf8 = FLAC_plugin__tags_get_tag_utf8(tags, name);
75                 if (0 != utf8) {
76                         if(flac_cfg.title.convert_char_set)
77                                 return convert_from_utf8_to_user(utf8);
78                         else
79                                 return strdup(utf8);
80                 }
81         }
82
83         return 0;
84 }
85
86 static void local__safe_free(char *s)
87 {
88         if (0 != s)
89                 free(s);
90 }
91
92 /*
93  * Function flac_format_song_title (tag, filename)
94  *
95  *    Create song title according to `tag' and/or `filename' and
96  *    return it.  The title must be subsequently freed using g_free().
97  *
98  */
99 char *flac_format_song_title(char *filename)
100 {
101         char *ret = NULL;
102         TitleInput *input = NULL;
103         FLAC__StreamMetadata *tags;
104         char *title, *artist, *performer, *album, *date, *tracknumber, *genre, *description;
105
106         FLAC_plugin__tags_get(filename, &tags);
107
108         title       = local__getfield(tags, "TITLE");
109         artist      = local__getfield(tags, "ARTIST");
110         performer   = local__getfield(tags, "PERFORMER");
111         album       = local__getfield(tags, "ALBUM");
112         date        = local__getfield(tags, "DATE");
113         tracknumber = local__getfield(tags, "TRACKNUMBER");
114         genre       = local__getfield(tags, "GENRE");
115         description = local__getfield(tags, "DESCRIPTION");
116
117         XMMS_NEW_TITLEINPUT(input);
118
119         input->performer = local__getstr(performer);
120         if(!input->performer)
121                 input->performer = local__getstr(artist);
122         input->album_name = local__getstr(album);
123         input->track_name = local__getstr(title);
124         input->track_number = local__getnum(tracknumber);
125         input->year = local__getnum(date);
126         input->genre = local__getstr(genre);
127         input->comment = local__getstr(description);
128
129         input->file_name = g_basename(filename);
130         input->file_path = filename;
131         input->file_ext = local__extname(filename);
132         ret = xmms_get_titlestring(flac_cfg.title.tag_override ? flac_cfg.title.tag_format : xmms_get_gentitle_format(), input);
133         g_free(input);
134
135         if (!ret) {
136                 /*
137                  * Format according to filename.
138                  */
139                 ret = g_strdup(g_basename(filename));
140                 if (local__extname(ret) != NULL)
141                         *(local__extname(ret) - 1) = '\0';      /* removes period */
142         }
143
144         FLAC_plugin__tags_destroy(&tags);
145         local__safe_free(title);
146         local__safe_free(artist);
147         local__safe_free(performer);
148         local__safe_free(album);
149         local__safe_free(date);
150         local__safe_free(tracknumber);
151         local__safe_free(genre);
152         local__safe_free(description);
153         return ret;
154 }