File: gbemol-utils.c

package info (click to toggle)
gbemol 0.3.2-2
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, jessie, jessie-kfreebsd, sid, stretch, trixie, wheezy
  • size: 2,168 kB
  • ctags: 1,450
  • sloc: sh: 10,066; ansic: 8,228; makefile: 89
file content (537 lines) | stat: -rw-r--r-- 12,055 bytes parent folder | download
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*
 * Copyright (C) 2007 Felipe Weckx <felipe.weckx@gmail.com>
 *
 * This program 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 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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 this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "gbemol-utils.h"
#include "gbemol-mpd.h"
#include "gbemol-net.h"
#include "gbemol-amazon.h"

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h>

#ifdef ENABLE_ID3_APIC
/* ID3 Picture Tag */
#include <id3.h>
#endif

#include <gtk/gtk.h>
#include <glib/gstdio.h>

/* Got from brasero (http://perso.wanadoo.fr/bonfire) */
GtkWidget *
gbemol_utils_make_button (char *text, char *stock)
{
	GtkWidget *button;
	GtkWidget *box, *lbl;

	box = gtk_hbox_new (FALSE, 6);
	gtk_box_pack_start (GTK_BOX (box),
			    gtk_image_new_from_stock (stock, GTK_ICON_SIZE_BUTTON),
			    FALSE,
			    FALSE,
			    0);

	button = gtk_button_new ();
	if (text)
	{
		lbl = gtk_label_new_with_mnemonic (text);
		gtk_box_pack_start (GTK_BOX (box),
				    lbl,
				    FALSE,
				    FALSE,
				    0);
		gtk_label_set_mnemonic_widget (GTK_LABEL (lbl), button);
	}

	gtk_container_add (GTK_CONTAINER (button), box);
	gtk_button_set_alignment (GTK_BUTTON (button), 0.5, 0.5);

	return button;
}

/* Same as above, but returns the label as well */
GtkWidget *
gbemol_utils_make_button_and_label (char *text, char *stock, GtkWidget** lbl)
{
	GtkWidget *button;
	GtkWidget *box;

	box = gtk_hbox_new (FALSE, 6);
	gtk_box_pack_start (GTK_BOX (box),
			    gtk_image_new_from_stock (stock, GTK_ICON_SIZE_BUTTON),
			    FALSE,
			    FALSE,
			    0);

	button = gtk_button_new ();
	if (text)
	{
		*lbl = gtk_label_new_with_mnemonic (text);
		gtk_box_pack_start (GTK_BOX (box),
				    *lbl,
				    FALSE,
				    FALSE,
				    0);
		gtk_label_set_mnemonic_widget (GTK_LABEL (*lbl), button);
	}

	gtk_container_add (GTK_CONTAINER (button), box);
	gtk_button_set_alignment (GTK_BUTTON (button), 0.5, 0.5);

	return button;
}

void gbemol_utils_char_list_free (GList *l)
{
	if (l)
	{
		g_list_foreach (l, (GFunc) g_free, NULL);
		g_list_free (l);
	}
}

void gbemol_utils_song_list_free (GList *l)
{
	if (l)
	{
		g_list_foreach (l, (GFunc) gbemol_mpd_free_song, NULL);
		g_list_free (l);
	}
}

GList* 
gbemol_utils_get_selected (GtkTreeView* tvw)
{
	GtkTreeModel *model;
	GtkTreeSelection *select;
	GList *rows = NULL;

	select = gtk_tree_view_get_selection (tvw);
	if (gtk_tree_selection_count_selected_rows (select) > 0)
	{
		rows = gtk_tree_selection_get_selected_rows (select, &model);
		return rows;
	} 
	else
		return NULL;
}

void 
gbemol_utils_selected_list_free (GList* l)
{
        if (l)
        {
                g_list_foreach (l, (GFunc) gtk_tree_path_free, NULL);
                g_list_free (l);
        }
}

/*
 * Config file functions
 */
gboolean gbemol_cfg_create_file ()
{
	gchar* path;
	gint fd;

	path = g_strdup_printf ("%s/.gbemol.cfg", g_get_home_dir());

	if ((fd = open (path, O_CREAT, 0777)) != -1)
	{
		chmod (path, 0644);
		close (fd);
		/* Initial configs */
		gbemol_cfg_set_int ("win_main", "visible", 1);
		return TRUE;
	}

	return FALSE;
}

/*
 * Opens and reads the key file
 */
GKeyFile* gbemol_cfg_load_file ()
{
	gchar* path, *path2;
	GKeyFile* file;
	GError* error = NULL;

	path = g_strdup_printf ("%s/gbemol/gbemol.cfg", g_get_user_config_dir());
	file = g_key_file_new ();

	if (g_file_test (path, G_FILE_TEST_EXISTS))
		/* File exists, load it */
		g_key_file_load_from_file (file, path, G_KEY_FILE_NONE, &error);
	else
	{
		/* File doesn't exist, check if the directory exists */
		path2 = g_strdup_printf ("%s/gbemol", g_get_user_config_dir());		
		if (!(g_file_test (path2, G_FILE_TEST_EXISTS) && g_file_test (path2, G_FILE_TEST_IS_DIR)))
			g_mkdir (path2, 0755);
		g_free (path2);
	}

	g_free (path);
	
	return file;
}

gboolean gbemol_cfg_save_file (GKeyFile* file)
{
	gchar* path, *data;
	gsize len;
	FILE* cfg;
	
	path = g_strdup_printf ("%s/gbemol/gbemol.cfg", g_get_user_config_dir ());
	data = g_key_file_to_data (file, &len, NULL);

	cfg = fopen (path, "w");

	if (cfg)
	{
		fprintf (cfg, "%s", data);
		fclose (cfg);
		g_free (path);
		g_free (data);
		return TRUE;
	}

	g_free (path);
	g_free (data);
	g_key_file_free (file);

	return FALSE;
}
	
void gbemol_cfg_set_string (gchar* group, gchar* key, const gchar* value)
{
	GKeyFile *file;

	file = gbemol_cfg_load_file ();
	g_key_file_set_string (file, group, key, value);
	gbemol_cfg_save_file (file);
}

gchar* gbemol_cfg_get_string (gchar* group, gchar* key)
{
	GKeyFile *file;
	gchar* str;

	file = gbemol_cfg_load_file ();
	str = g_key_file_get_string (file, group, key, NULL);
	g_key_file_free (file);

	return str;
}

void gbemol_cfg_set_int (gchar* group, gchar* key, gint value)
{
	GKeyFile *file;

	file = gbemol_cfg_load_file ();
	g_key_file_set_integer (file, group, key, value);
	gbemol_cfg_save_file (file);
	g_key_file_free (file);

}

gint gbemol_cfg_get_int (gchar* group, gchar* key)
{
	GKeyFile *file;
	gint value;

	file = gbemol_cfg_load_file ();
	value = g_key_file_get_integer (file, group, key, NULL);
	g_key_file_free (file);

	return value;
}

void gbemol_cfg_set_bool(gchar* group, gchar* key, gboolean value)
{
	GKeyFile *file;

	file = gbemol_cfg_load_file ();
	g_key_file_set_boolean(file, group, key, value);
	gbemol_cfg_save_file (file);
	g_key_file_free (file);

}

gboolean gbemol_cfg_get_bool(gchar* group, gchar* key)
{
	GKeyFile *file;
	gboolean value;

	file = gbemol_cfg_load_file ();
	value = g_key_file_get_boolean(file, group, key, NULL);
	g_key_file_free (file);

	return value;
}

gboolean gbemol_cfg_get_bool_with_default(gchar* group, gchar* key, gboolean def)
{
	GKeyFile *file;
	gboolean value;
	GError* e = NULL;

	file = gbemol_cfg_load_file();
	value = g_key_file_get_boolean(file, group, key, &e);
	if ((e != NULL) && (e->code == G_KEY_FILE_ERROR_KEY_NOT_FOUND))
		value = def;
	g_key_file_free (file);
	
	return value;
}

void gbemol_cfg_set_double (gchar* group, gchar* key, gdouble value)
{
	GKeyFile *file;

	file = gbemol_cfg_load_file ();
	g_key_file_set_double (file, group, key, value);
	gbemol_cfg_save_file (file);

}

gdouble gbemol_cfg_get_double (gchar* group, gchar* key)
{
	GKeyFile *file;
	gdouble value;

	file = gbemol_cfg_load_file ();
	value = g_key_file_get_double (file, group, key, NULL);
	g_key_file_free (file);

	return value;
}

#ifdef ENABLE_ID3_APIC
/*
 * Got from EasyTag (http://easytag.sf.net)
 */
ID3_C_EXPORT size_t ID3Tag_Link_1 (ID3Tag *id3tag, const char *filename)
{
    size_t offset;

#   if (0) // Link the file with the both tags may cause damage to unicode strings
//#   if ( (ID3LIB_MAJOR >= 3) && (ID3LIB_MINOR >= 8) && (ID3LIB_PATCH >= 1) ) // Same test used in Id3tag_Read_File_Tag to use ID3Tag_HasTagType
        /* No problem of priority, so we link the file with the both tags
         * to manage => ID3Tag_HasTagType works correctly */
        offset = ID3Tag_LinkWithFlags(id3tag,filename,ID3TT_ID3V1 | ID3TT_ID3V2);
#   elif ( (ID3LIB_MAJOR >= 3) && (ID3LIB_MINOR >= 8) )
        /* Version 3.8.0pre2 gives priority to tag id3v1 instead of id3v2, so we
         * try to fix it by linking the file with the id3v2 tag first. This bug 
         * was fixed in the final version of 3.8.0 but we can't know it... */
        /* First, try to get the ID3v2 tags */
        offset = ID3Tag_LinkWithFlags(id3tag,filename,ID3TT_ID3V2);
        if (offset == 0)
        {
            /* No ID3v2 tags available => try to get the ID3v1 tags */
            offset = ID3Tag_LinkWithFlags(id3tag,filename,ID3TT_ID3V1);
        }
#   else
        /* Function 'ID3Tag_LinkWithFlags' is not defined up to id3lib-.3.7.13 */
        offset = ID3Tag_Link(id3tag,filename);
#   endif
    //g_print("ID3 TAG SIZE: %d\t%s\n",offset,g_path_get_basename(filename));
    return offset;
}

#endif

GdkPixbuf* gbemol_get_song_image_from_id3 (GbemolMpdSong* song)
{
	GdkPixbuf *pix = NULL;
#ifdef ENABLE_ID3_APIC	
	size_t offset;
	ID3Tag* id3_tag = NULL;
	ID3Frame* id3_frame;
	ID3Field* id3_field;
	gchar *path, *songs_path;
	GdkPixbufLoader *load;
	gulong size = 0;
	guchar  *data = NULL;

	if (!(songs_path = gbemol_cfg_get_string ("MPD", "music_dir")))
		return NULL;

	path = g_strdup_printf ("%s/%s", songs_path, song->file);

	id3_tag = ID3Tag_New();
	offset = ID3Tag_LinkWithFlags (id3_tag, path, ID3TT_ID3V1 | ID3TT_ID3V2);

	g_free (path);
	g_free (songs_path);

	if (!offset)
		return NULL;

	id3_frame = ID3Tag_FindFrameWithID(id3_tag, ID3FID_PICTURE);
	if (!id3_frame)
		return NULL;

	if ( (id3_field = ID3Frame_GetField(id3_frame, ID3FN_DATA)) )
        {
            size = ID3Field_Size(id3_field);
            data = g_malloc(size);
            ID3Field_GetBINARY(id3_field, data, size);
        }

	load = gdk_pixbuf_loader_new ();
	if (load)
        	if (gdk_pixbuf_loader_write (load, data, size, 0))
			pix = gdk_pixbuf_loader_get_pixbuf (load);

	g_free (data);
	g_free (id3_tag);
	g_free (id3_frame);
	g_free (id3_field);
#endif
	return pix;
}


/* Returns the cover filename and creates ~/.cover if it doesn't exists */
gchar* gbemol_utils_generate_cover_filename(GbemolMpdSong* song)
{
	gchar* str_dir, *str;
	GDir* dir_covers;
	
	str_dir = g_strdup_printf ("%s/.covers/", g_get_home_dir());	
	dir_covers = g_dir_open(str_dir, 0, NULL);
	if (dir_covers == NULL) {
		g_mkdir(str_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
	} else {
		g_dir_close(dir_covers);
	}
	g_free(str_dir);
	str = g_strdup_printf ("%s/.covers/%s-%s.jpg", g_get_home_dir(), song->artist, song->album);
	return str;
}

/**
 * Try to load image from file in ~/.covers
 */
GdkPixbuf* gbemol_utils_get_cover_image_from_file(GbemolMpdSong* song)
{
	gchar *str;
	GdkPixbuf* img = NULL;
	
	str = gbemol_utils_generate_cover_filename(song);
	
	if (g_file_test (str, G_FILE_TEST_EXISTS))
	{
		GError *e = NULL;
		img = gdk_pixbuf_new_from_file (str, &e);
		if (e) 
		{
			g_message (e->message, "%s");		
			img = NULL;
		}
	}
	g_free(str);
	
	return img;
}

/**
 * Try to fetch image from Amazon.com
 */
GdkPixbuf* gbemol_utils_get_cover_image_from_amazon(GbemolMpdSong* song)
{
	GdkPixbuf* img = NULL;;	
	gchar *result = NULL;	
	GSList* results;
	
	results = gbemol_amazon_search_cover(song);
	if (results == NULL)
	{
		return NULL;
	}
	else
	{
		GError* err = NULL;
		gchar* str = gbemol_utils_generate_cover_filename(song);
		/* There's a cover, save it */
		result = results->data;		
		gbemol_net_download_file(result, str, &err);
		if (err != NULL)
		{
			g_message(err->message, "%s");
		}
		else
		{
			img = gdk_pixbuf_new_from_file (str, NULL);
		}
		g_free (str);		
	}
	
	/* Free everything */
	g_slist_foreach(results, (GFunc) g_free, NULL);

	return img;
}

GdkPixbuf* gbemol_utils_get_song_image (GbemolMpdSong* song)
{
	GdkPixbuf *img = NULL;
	
	if (gbemol_cfg_get_bool("cover_art", "file"))
		img = gbemol_utils_get_cover_image_from_file(song);
	
	/* No picture in  ~/.covers, try amazon */
	if (!img && gbemol_cfg_get_bool("cover_art", "amazon"))
	{
		img = gbemol_utils_get_cover_image_from_amazon(song);
	}

	return img;
}

GtkWidget* gbemol_utils_label_new_with_markup (gchar* text)
{
	GtkWidget* lbl;

	lbl = gtk_label_new (text);
	gtk_label_set_use_markup (GTK_LABEL (lbl), TRUE);
	gtk_misc_set_alignment (GTK_MISC (lbl), 0.0, 0.5);

	return lbl;
}

void gbemol_debug_msg (char *msg, ...)
{
#ifdef DEBUG
	va_list args;
	char *str;

	va_start (args);

	str = g_strdup_vprintf ("DEBUG: %s\n", args);
	g_print (str);
	g_free (str);
#endif
}