remove id3 support from the plugins
[flac.git] / src / plugin_xmms / tag.c
1 /* libxmms-flac - XMMS FLAC input plugin
2  * Copyright (C) 2000,2001,2002,2003,2004  Josh Coalson
3  * Copyright (C) 2002,2003,2004  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/canonical_tag.h"
33 #include "plugin_common/vorbiscomment.h"
34 #include "charset.h"
35 #include "configure.h"
36
37 /*
38  * Function local__extname (filename)
39  *
40  *    Return pointer within filename to its extenstion, or NULL if
41  *    filename has no extension.
42  *
43  */
44 static char *local__extname(const char *filename)
45 {
46         char *ext = strrchr(filename, '.');
47
48         if (ext != NULL)
49                 ++ext;
50
51         return ext;
52 }
53
54 static char *local__getstr(char* str)
55 {
56         if (str && strlen(str) > 0)
57                 return str;
58         return NULL;
59 }
60
61 static int local__getnum(char* str)
62 {
63         if (str && strlen(str) > 0)
64                 return atoi(str);
65         return 0;
66 }
67
68 static char *local__getfield(FLAC_Plugin__CanonicalTag *tag, const wchar_t *name)
69 {
70         const wchar_t *ucs2 = FLAC_plugin__canonical_get(tag, name);
71         if (0 != ucs2) {
72                 char *utf8 = FLAC_plugin__convert_ucs2_to_utf8(FLAC_plugin__canonical_get(tag, name));
73                 if(flac_cfg.title.convert_char_set) {
74                         char *user = convert_from_utf8_to_user(utf8);
75                         free(utf8);
76                         return user;
77                 }
78                 else
79                         return utf8;
80         }
81         else
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_Plugin__CanonicalTag tag;
103         char *title, *artist, *performer, *album, *date, *tracknumber, *genre, *description;
104
105         FLAC_plugin__canonical_tag_init(&tag);
106
107         FLAC_plugin__vorbiscomment_get(filename, &tag, /*sep=*/0);
108
109         title       = local__getfield(&tag, L"TITLE");
110         artist      = local__getfield(&tag, L"ARTIST");
111         performer   = local__getfield(&tag, L"PERFORMER");
112         album       = local__getfield(&tag, L"ALBUM");
113         date        = local__getfield(&tag, L"DATE");
114         tracknumber = local__getfield(&tag, L"TRACKNUMBER");
115         genre       = local__getfield(&tag, L"GENRE");
116         description = local__getfield(&tag, L"DESCRIPTION");
117
118         XMMS_NEW_TITLEINPUT(input);
119
120         input->performer = local__getstr(performer);
121         if(!input->performer)
122                 input->performer = local__getstr(artist);
123         input->album_name = local__getstr(album);
124         input->track_name = local__getstr(title);
125         input->track_number = local__getnum(tracknumber);
126         input->year = local__getnum(date);
127         input->genre = local__getstr(genre);
128         input->comment = local__getstr(description);
129
130         input->file_name = g_basename(filename);
131         input->file_path = filename;
132         input->file_ext = local__extname(filename);
133         ret = xmms_get_titlestring(flac_cfg.title.tag_override ? flac_cfg.title.tag_format : xmms_get_gentitle_format(), input);
134         g_free(input);
135
136         if (!ret) {
137                 /*
138                  * Format according to filename.
139                  */
140                 ret = g_strdup(g_basename(filename));
141                 if (local__extname(ret) != NULL)
142                         *(local__extname(ret) - 1) = '\0';      /* removes period */
143         }
144
145         FLAC_plugin__canonical_tag_clear(&tag);
146         local__safe_free(title);
147         local__safe_free(artist);
148         local__safe_free(performer);
149         local__safe_free(album);
150         local__safe_free(date);
151         local__safe_free(tracknumber);
152         local__safe_free(genre);
153         local__safe_free(description);
154         return ret;
155 }