1 /*
2 * Copyright (C) 2008, Nokia <ivan.frade@nokia.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 * Authors:
20 * Philip Van Hoof <philip@codeminded.be>
21 */
22
23 #include <gdk-pixbuf/gdk-pixbuf.h>
24
25 #include "tracker-media-art-generic.h"
26
27 #include "tracker-main.h"
28
29 void
30 tracker_media_art_plugin_init (void)
31 {
32 }
33
34 void
35 tracker_media_art_plugin_shutdown (void)
36 {
37 }
38
39 gboolean
40 tracker_media_art_file_to_jpeg (const gchar *filename,
41 const gchar *target)
42 {
43 GdkPixbuf *pixbuf;
44 GError *error = NULL;
45 TrackerConfig *config = tracker_main_get_config ();
46 gint max_media_art_width = tracker_config_get_max_media_art_width (config);
47
48 if (max_media_art_width < 0) {
49 g_debug ("Not saving album art from file, disabled in config");
50 return TRUE;
51 }
52
53 /* TODO: Add resizing support */
54
55 pixbuf = gdk_pixbuf_new_from_file (filename, &error);
56
57 if (error) {
58 g_clear_error (&error);
59
60 return FALSE;
61 } else {
62 gdk_pixbuf_save (pixbuf, target, "jpeg", &error, NULL);
63 g_object_unref (pixbuf);
64
65 if (error) {
66 g_clear_error (&error);
67 return FALSE;
68 }
69 }
70
71 return TRUE;
72 }
73
74 static void
75 size_prepared_cb (GdkPixbufLoader *loader,
76 gint width,
77 gint height,
78 gpointer user_data)
79 {
80 TrackerConfig *config = tracker_main_get_config ();
81 gint max_media_art_width = tracker_config_get_max_media_art_width (config);
82 gfloat scale;
83
84 if (max_media_art_width < 1 || width <= max_media_art_width) {
85 return;
86 }
87
88 g_debug ("Resizing media art to %d width", max_media_art_width);
89
90 scale = width / (gfloat) max_media_art_width;
91
92 gdk_pixbuf_loader_set_size (loader, (gint) (width / scale), (gint) (height / scale));
93 }
94
95 gboolean
96 tracker_media_art_buffer_to_jpeg (const unsigned char *buffer,
97 size_t len,
98 const gchar *buffer_mime,
99 const gchar *target)
100 {
101 TrackerConfig *config = tracker_main_get_config ();
102 gint max_media_art_width = tracker_config_get_max_media_art_width (config);
103
104 if (max_media_art_width < 0) {
105 g_debug ("Not saving album art from buffer, disabled in config");
106 return TRUE;
107 }
108
109 /* FF D8 FF are the three first bytes of JPeg images */
110 if (max_media_art_width == 0 &&
111 (g_strcmp0 (buffer_mime, "image/jpeg") == 0 ||
112 g_strcmp0 (buffer_mime, "JPG") == 0) &&
113 (buffer && len > 2 && buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff)) {
114 g_debug ("Saving album art using raw data as uri:'%s'", target);
115 g_file_set_contents (target, buffer, (gssize) len, NULL);
pointer targets in passing argument 2 of 'g_file_set_contents' differ in signedness
(emitted by gcc)
116 } else {
117 GdkPixbuf *pixbuf;
118 GdkPixbufLoader *loader;
119 GError *error = NULL;
120
121 g_debug ("Saving album art using GdkPixbufLoader for uri:'%s' (max width:%d)",
122 target,
123 max_media_art_width);
124
125 loader = gdk_pixbuf_loader_new ();
126 if (max_media_art_width > 0) {
127 g_signal_connect (loader,
128 "size-prepared",
129 G_CALLBACK (size_prepared_cb),
130 NULL);
131 }
132
133 if (!gdk_pixbuf_loader_write (loader, buffer, len, &error)) {
134 g_warning ("Could not write with GdkPixbufLoader when setting album art, %s",
135 error ? error->message : "no error given");
136
137 g_clear_error (&error);
138 gdk_pixbuf_loader_close (loader, NULL);
139 g_object_unref (loader);
140
141 return FALSE;
142 }
143
144 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
145
146 if (pixbuf == NULL) {
147 g_warning ("Could not get pixbuf from GdkPixbufLoader when setting album art");
148
149 gdk_pixbuf_loader_close (loader, NULL);
150 g_object_unref (loader);
151
152 return FALSE;
153 }
154
155 if (!gdk_pixbuf_save (pixbuf, target, "jpeg", &error, NULL)) {
156 g_warning ("Could not save GdkPixbuf when setting album art, %s",
157 error ? error->message : "no error given");
158
159 g_clear_error (&error);
160 gdk_pixbuf_loader_close (loader, NULL);
161 g_object_unref (loader);
162
163 return FALSE;
164 }
165
166 if (!gdk_pixbuf_loader_close (loader, &error)) {
167 g_warning ("Could not close GdkPixbufLoader when setting album art, %s",
168 error ? error->message : "no error given");
169 g_clear_error (&error);
170 }
171
172 g_object_unref (loader);
173 }
174
175 return TRUE;
176 }