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