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