aboutsummaryrefslogtreecommitdiff
path: root/src/idop.c
blob: c95383611978082102dbd96d963f1cc229035b79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* This file is part of Idest.
   Copyright (C) 2009-2011 Sergey Poznyakoff
 
   Idest is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.
 
   Idest is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with Idest.  If not, see <http://www.gnu.org/licenses/>. */

#include "idest.h"
#include <file.h>
#include <signal.h>

void
set_frame_value(struct id3_frame *frame, const char *value)
{
	union id3_field *field;
	enum id3_field_textencoding encoding =
		(latin1_option ? ID3_FIELD_TEXTENCODING_ISO_8859_1 
		               : ID3_FIELD_TEXTENCODING_UTF_8);
	id3_ucs4_t *ucs4;
	
	switch (frame_id(frame->id)) {
	case item_title:
	case item_artist:
	case item_album:
	case item_year:
	case item_track:
	case item_genre:
		field = id3_frame_field(frame, 0);
		id3_field_settextencoding(field, encoding);

		if (latin1_option)
			ucs4 = id3_latin1_ucs4duplicate(
				     (const id3_latin1_t *) value);
		else
			ucs4 = id3_utf8_ucs4duplicate(
				     (const id3_utf8_t *)value);
		
		field = id3_frame_field(frame, 1);
		id3_field_setstrings(field, 1, &ucs4);
		free(ucs4);
		break;
		
	case item_comment:
		/* Field 0: Encoding */
		field = id3_frame_field(frame, 0);
		id3_field_settextencoding(field, encoding);
		/* FIXME: Field 1: Language */
		/* FIXME: Field 2: String */
		/* Field 3: Comment */
		field = id3_frame_field(frame, 3);
		if (latin1_option)
			ucs4 = id3_latin1_ucs4duplicate(
				     (const id3_latin1_t *) value);
		else
			ucs4 = id3_utf8_ucs4duplicate(
				     (const id3_utf8_t *)value);
		id3_field_setfullstring(field, ucs4);
		free(ucs4);
		break;
		
	default:
		error(1, 0, "don't know how to set field %s", frame->id);
	}
}

void
safe_id3_file_update_and_close(struct id3_file *file)
{
	sigset_t set, oldset;

	sigemptyset(&set);
	sigaddset(&set, SIGINT);
	sigaddset(&set, SIGTERM);

	sigprocmask(SIG_BLOCK, &set, &oldset);
	id3_file_update(file);
	id3_file_close(file);
	sigprocmask(SIG_SETMASK, &oldset, NULL);
}

void
set_tags(const char *name)
{
	struct id3_file *file;
	struct id3_tag *tag;
	int modified = 0;
	
	file = id3_file_open(name, ID3_FILE_MODE_READWRITE);
	if (!file)
		error(1, errno, "cannot open file %s", name);
	tag = id3_file_tag(file);
	if (!tag) 
		abort(); /* FIXME */

	if (ed_list) {
		gl_list_iterator_t itr;
		const void *p;
		
		itr = gl_list_iterator(ed_list);
		while (gl_list_iterator_next(&itr, &p, NULL)) {
			const struct ed_item *item = p;
			struct id3_frame *frame
				= id3_tag_findframe(tag, item->id, 0);
			if (!frame) {
				frame = id3_frame_new(item->id);
				if (id3_tag_attachframe(tag, frame))
					error(1, 0, "cannot attach new frame");
			}
			set_frame_value(frame, item->v.value);
			modified |= 1;
		}
		gl_list_iterator_free(&itr);
	}

	/* FIXME */
	modified |= guile_transform(name, tag);

	switch (convert_version) {
	case 1:
		id3_tag_options(tag,
				ID3_TAG_OPTION_NO_ID3V2,
				ID3_TAG_OPTION_NO_ID3V2);
		version_option = 1;
		modified |= 1;
		break;

	case 2:
		id3_tag_options(tag, ID3_TAG_OPTION_ID3V1, 0);
		version_option = 0;
		modified |= 1;
		break;
	}
	
	if (version_option == 1) {
		id3_tag_options(tag, ID3_TAG_OPTION_ID3V1,
				ID3_TAG_OPTION_ID3V1);
		modified |= 1;
	}
	if (modified)
		safe_id3_file_update_and_close(file);
	else
		id3_file_close(file);
}

void
del_tags(const char *name)
{
	struct id3_file *file;
	struct id3_tag *tag;
	
	file = id3_file_open(name, ID3_FILE_MODE_READWRITE);
	if (!file)
		error(1, errno, "cannot open file %s", name);
	tag = id3_file_tag(file);
	if (!tag) 
		abort(); /* FIXME */
	id3_tag_clearframes(tag);
	safe_id3_file_update_and_close(file);
}


char *
idest_ucs4_cvt(id3_ucs4_t const *ucs4)
{
	if (latin1_option) 
		return (char*)id3_ucs4_latin1duplicate(ucs4);
	else
		return (char*)id3_ucs4_utf8duplicate(ucs4);
}

static void
add_stringlist(gl_list_t list, struct id3_frame *frame,
	       union id3_field *field)
{
	unsigned i, nstrings = id3_field_getnstrings(field);
	for (i = 0; i < nstrings; i++) {
		id3_ucs4_t const *ucs4;
		char *str;
		
		ucs4 = id3_field_getstrings(field, i);
		if (!ucs4)
			continue;
		if (strcmp(frame->id, ID3_FRAME_GENRE) == 0)
			ucs4 = id3_genre_name(ucs4);
		str = idest_ucs4_cvt(ucs4);
		gl_list_add_last(list, str);
	}
}

static void
add_field(gl_list_t list, struct id3_frame *frame, union id3_field *field)
{
	id3_ucs4_t const *ucs4;
	char *str;
	
	switch (id3_field_type(field)) {
	case ID3_FIELD_TYPE_TEXTENCODING:
		break;
	case ID3_FIELD_TYPE_LATIN1:
	case ID3_FIELD_TYPE_LATIN1FULL:
	case ID3_FIELD_TYPE_LATIN1LIST:
		break;
	case ID3_FIELD_TYPE_STRING:
		break;
	case ID3_FIELD_TYPE_STRINGFULL:
		ucs4 = id3_field_getfullstring(field);
		str = idest_ucs4_cvt(ucs4);
		gl_list_add_last(list, str);
		break;
	case ID3_FIELD_TYPE_STRINGLIST:
		add_stringlist(list, frame, field);
		break;
	case ID3_FIELD_TYPE_LANGUAGE:
	case ID3_FIELD_TYPE_FRAMEID:
	case ID3_FIELD_TYPE_DATE:
	case ID3_FIELD_TYPE_INT8:
	case ID3_FIELD_TYPE_INT16:
	case ID3_FIELD_TYPE_INT24:
	case ID3_FIELD_TYPE_INT32:
	case ID3_FIELD_TYPE_INT32PLUS:
	case ID3_FIELD_TYPE_BINARYDATA:;
	}
}

static gl_list_t 
frame_to_list(struct id3_frame *frame)
{
	gl_list_t list;
	unsigned i;
	union id3_field *field;

	list = new_string_list(true);
	for (i = 0; (field = id3_frame_field(frame, i)); i++) 
		add_field(list, frame, field);
	return list;
}

static void
show_tags(struct id3_tag *tag)
{
	struct id3_frame *frame;
	unsigned i;

	for (i = 0; (frame = id3_tag_findframe(tag, NULL, i)); i++) {
		gl_list_t list = frame_to_list(frame);
		if (gl_list_size(list) > 0)
			ed_list_add_item(frame->id, list);
		else
			gl_list_free(list);
	}
	ed_list_print();
	ed_list_clear();
}

void
query_tags(const char *name)
{
	struct id3_file *file;
	struct id3_tag *tag;
	
	file = id3_file_open(name, ID3_FILE_MODE_READONLY);
	if (!file)
		error(1, errno, "cannot open file %s", name);

	tag = id3_file_tag(file);
	if (tag) {
		if (guile_list(name, tag) == 0)
			show_tags(tag);
	}
	
	id3_file_close(file);
}

static int
prinfo(struct id3_tag *tag, unsigned long location,
       id3_length_t length, void *data)
{
	unsigned int ver = id3_tag_version(tag);
	printf("version: %u.%u\n",
	       ID3_TAG_VERSION_MAJOR(ver),
	       ID3_TAG_VERSION_MINOR(ver));
	printf("offset: %lu\n", location);
	printf("length: %lu\n", length);
	return 0;
}

void
info_id3(const char *name)
{
	struct id3_file *file;
	unsigned long ntags;
	
	file = id3_file_open(name, ID3_FILE_MODE_READONLY);
	if (!file)
		error(1, errno, "cannot open file %s", name);
	ntags = id3_file_struct_ntags(file);
	printf("file: %s\n", name);
	printf("ntags: %lu\n", ntags);
	id3_file_struct_iterate(file, prinfo, NULL);
	id3_file_close(file);
}


Return to:

Send suggestions and report system problems to the System administrator.