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