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
|
/*
| Copyright (C) 2007 P.G. Richardson <phantom_sf at users.sourceforge.net>
| Part of the gtkpod project.
|
| URL: http://www.gtkpod.org/
| URL: http://gtkpod.sourceforge.net/
|
| 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
|
| iTunes and iPod are trademarks of Apple
|
| This product is not supported/written/published by Apple!
|
| $Id$
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <glib/gprintf.h>
#include <glib/gstdio.h>
#include <gdk/gdkkeysyms.h>
#include "libgtkpod/gp_itdb.h"
#include "libgtkpod/misc.h"
#include "plugin.h"
#include "fetchcover.h"
#undef FETCHCOVER_DEBUG
#define IMGSCALE 256
/* Display a dialog explaining the options if a file with the proposed name already exists */
static gchar *display_file_exist_dialog (Fetch_Cover *fetch_cover);
static gchar *fetchcover_check_file_exists (Fetch_Cover *fetch_cover);
#ifdef HAVE_CURL
#include <curl/curl.h>
/* Declarations */
static void *safe_realloc(void *ptr, size_t size);
static size_t curl_write_fetchcover_func(void *ptr, size_t itemsize, size_t numitems, void *data);
struct chunk
{
gchar *memory;
size_t size;
};
/* Data structure for use with curl */
struct chunk fetchcover_curl_data;
/****
* safe_realloc:
*
* @void: ptr
* @size_t: size
*
* Memory allocation function
**/
static void *safe_realloc(void *ptr, size_t size)
{
if (ptr)
return realloc(ptr, size);
else
return malloc(size);
}
/****
*
* curl_write_fetchcover_func:
*
* @void: *ptr
* @size_t: itemsize
* @size_t:numitems
* @void: *data
*
* Curl writing function
*
* @Return size_t
**/
static size_t curl_write_fetchcover_func(void *ptr, size_t itemsize, size_t numitems, void *data)
{
size_t size = itemsize * numitems;
struct chunk *mem = (struct chunk*)data;
mem->memory = (gchar*)safe_realloc(mem->memory, mem->size + size + 1);
if (mem->memory)
{
memcpy(&(mem->memory[mem->size]), ptr, size);
mem->size += size;
mem->memory[mem->size] = 0;
}
return size;
}
#endif
/*****
* fetchcover_new:
*
* @GString: url
* @GList: trks
*
* Initialise a new fetch cover object for use with the fetchcover functions
**/
Fetch_Cover *fetchcover_new (gchar *url_path, GList *trks)
{
/*Create a fetchcover object */
Fetch_Cover * fcover;
fcover = g_new0(Fetch_Cover, 1);
fcover->url = g_string_new ((gchar*) url_path);
fcover->image = NULL;
fcover->tracks = trks;
fcover->err_msg = NULL;
return fcover;
}
#ifdef HAVE_CURL
/*****
* net_retrieve_image:
*
* @GString: url
*
* Use the url acquired from the net search to fetch the image,
* save it to a file inside the track's parent directory then display
* it as a pixbuf
**/
gboolean fetchcover_net_retrieve_image (Fetch_Cover *fetch_cover)
{
g_return_val_if_fail (fetch_cover, FALSE);
if (! g_str_has_suffix(fetch_cover->url->str, ".jpg") && ! g_str_has_suffix(fetch_cover->url->str, ".JPG"))
{
fetch_cover->err_msg = g_strdup(_("Only jpg images are currently supported at this time\n"));
return FALSE;
}
gchar *path = NULL;
FILE *tmpf = NULL;
fetchcover_curl_data.size = 0;
fetchcover_curl_data.memory = NULL;
/* Use curl to retrieve the data from the net */
CURL *curl;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, fetch_cover->url->str);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_fetchcover_func);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&fetchcover_curl_data);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (fetchcover_curl_data.memory == NULL)
{
fetch_cover->err_msg = g_strdup(_("fetchcover curl data memory is NULL so failed to download anything!\n"));
return FALSE;
}
/* Check that the page returned is a valid web page */
if (strstr(fetchcover_curl_data.memory, "<html>") != NULL)
{
fetch_cover->err_msg = g_strdup(_("fetchcover memory contains <html> tag so not a valid jpg image\n"));
return FALSE;
}
if (! fetchcover_select_filename (fetch_cover))
return FALSE;
path = g_build_filename(fetch_cover->dir, fetch_cover->filename, NULL);
#if DEBUG
printf ("path of download file is %s\n", path);
#endif
if ((tmpf = fopen(path, "wb")) == NULL)
{
if (fetchcover_curl_data.memory)
{
g_free(fetchcover_curl_data.memory);
fetchcover_curl_data.memory = NULL;
fetchcover_curl_data.size = 0;
}
g_free (path);
fetch_cover->err_msg = g_strdup(_("Failed to create a file with the filename\n"));
return FALSE;
}
if (fwrite(fetchcover_curl_data.memory, fetchcover_curl_data.size, 1, tmpf) != 1)
{
if (fetchcover_curl_data.memory)
{
g_free(fetchcover_curl_data.memory);
fetchcover_curl_data.memory = NULL;
fetchcover_curl_data.size = 0;
}
fclose(tmpf);
g_free (path);
fetch_cover->err_msg = g_strdup(_("fetchcover failed to write the data to the new file\n"));
return FALSE;
}
fclose(tmpf);
/* Check the file is a valid pixbuf type file */
GdkPixbufFormat *fileformat= NULL;
fileformat = gdk_pixbuf_get_file_info (path, NULL, NULL);
if (fileformat == NULL)
{
fetch_cover->err_msg = g_strdup(_("fetchcover downloaded file is not a valid image file\n"));
return FALSE;
}
GError *error = NULL;
fetch_cover->image = gdk_pixbuf_new_from_file(path, &error);
if (error != NULL)
{
g_error_free (error);
if (fetchcover_curl_data.memory)
{
g_free(fetchcover_curl_data.memory);
fetchcover_curl_data.memory = NULL;
fetchcover_curl_data.size = 0;
}
g_free(path);
fetch_cover->err_msg = g_strconcat(_("fetchcover error occurred while creating a pixbuf from the file\n"), error->message, NULL);
return FALSE;
}
if (fetchcover_curl_data.memory)
g_free(fetchcover_curl_data.memory);
fetchcover_curl_data.memory = NULL;
fetchcover_curl_data.size = 0;
g_free(path);
return TRUE;
}
#endif
gboolean fetchcover_select_filename (Fetch_Cover *fetch_cover)
{
GList *tracks = fetch_cover->tracks;
if (tracks == NULL || g_list_length (tracks) <= 0)
{
fetch_cover->err_msg = g_strdup(_("fetchcover object's tracks list either NULL or no tracks were selected\n"));
return FALSE;
}
Track *track = g_list_nth_data (tracks, 0);
ExtraTrackData *etd = track->userdata;
fetch_cover->dir = g_path_get_dirname(etd->pc_path_utf8);
gchar *template = prefs_get_string("coverart_template");
gchar **template_items = g_strsplit(template, ";", 0);
gint i;
for (i = 0; fetch_cover->filename == NULL && i < g_strv_length (template_items); ++i)
{
fetch_cover->filename = get_string_from_template(track, template_items[i], FALSE, FALSE);
if (strlen(fetch_cover->filename) == 0)
fetch_cover->filename = NULL;
}
g_strfreev(template_items);
g_free(template);
/* Check filename still equals null then take a default stance
* to ensure the file has a name. Default stance applies if the
* extra track data has been left as NULL
*/
if (fetch_cover->filename == NULL)
fetch_cover->filename = "folder.jpg";
else
{
if (! g_str_has_suffix(fetch_cover->filename, ".jpg"))
{
gchar *oldname;
oldname = fetch_cover->filename;
fetch_cover->filename = g_strconcat(oldname, ".jpg", NULL);
g_free (oldname);
}
}
if (fetchcover_check_file_exists (fetch_cover) == NULL)
{
fetch_cover->err_msg = g_strdup(_("operation cancelled\n"));
return FALSE;
}
return TRUE;
}
/*****
* fetchcover_check_file_exists:
*
* @Fetch_Cover
*
* Save the displayed cover.
* Set thumbnails, update details window.
* Called on response to the clicking of the save button in the dialog
*
* Returns:
* Filename of chosen cover image file
***/
static gchar *fetchcover_check_file_exists (Fetch_Cover *fetch_cover)
{
gchar *newname = NULL;
/* The default cover image will have both dir and filename set
* to null because no need to save because it is already saved (!!)
* Thus, this whole process is avoided. Added bonus that pressing
* save by accident if, for instance, no images are found means the
* whole thing safely completes
*/
if (fetch_cover->dir && fetch_cover->filename)
{
/* path is valid so first move the file to be the folder.jpg or
* whatever is the preferred preference
*/
/* Assign the full path name ready to rename the file */
newname = g_build_filename(fetch_cover->dir, fetch_cover->filename, NULL);
if (g_file_test (newname, G_FILE_TEST_EXISTS))
{
newname = display_file_exist_dialog (fetch_cover);
}
}
return newname;
}
static gchar *display_file_exist_dialog (Fetch_Cover *fetch_cover)
{
gchar *filepath;
gint result;
gchar **splitarr = NULL;
gchar *basename = NULL;
gint i;
gchar *message;
filepath = g_build_filename(fetch_cover->dir, fetch_cover->filename, NULL);
message = g_strdup_printf (_("The picture file %s already exists.\n" \
"This may be associated with other music files in the directory.\n\n" \
"Do you want to overwrite the existing file, possibly associating\n" \
"other music files in the same directory with this cover art file,\n" \
"to save the file with a unique file name, or to abort the fetchcover operation?"),
filepath);
result = gtkpod_confirmation_hig (GTK_MESSAGE_WARNING,
_("Cover art file already exists"),
message,
_("Overwrite"),
_("Rename"),
_("Abort"),
NULL);
g_free (message);
switch (result)
{
case GTK_RESPONSE_APPLY:
/* Abort has been clicked so no save */
return NULL;
case GTK_RESPONSE_OK:
/*** Overwrite clicked so overwrite the file is okay. Leave final_filename intact
* and remove the original
**/
g_remove (filepath);
return filepath;
case GTK_RESPONSE_CANCEL:
/* User doesn't want to overwrite anything so need to do some work on filename */
/* Remove the suffix from the end of the filename */
splitarr = g_strsplit (fetch_cover->filename, ".", 0);
basename = splitarr[0];
gchar *newfilename = g_strdup (fetch_cover->filename);;
for (i = 1; g_file_test (filepath, G_FILE_TEST_EXISTS); ++i)
{
g_free (newfilename);
gchar *intext= NULL;
intext = g_strdup_printf ("%d.jpg", i);
newfilename = g_strconcat (basename, intext, NULL);
g_free (filepath);
g_free (intext);
filepath = g_build_filename(fetch_cover->dir, newfilename, NULL);
}
/* Should have found a filename that really doesn't exist so this needs to be returned */
g_free (fetch_cover->filename);
fetch_cover->filename = g_strdup (newfilename);
g_free (newfilename);
newfilename = NULL;
basename = NULL;
g_strfreev(splitarr);
return filepath;
default:
return NULL;
}
}
/****
* free_fetchcover:
*
* @Fetch_Cover: fcover
*
* Free the elements of the passed in Fetch_Cover structure
*/
void free_fetchcover (Fetch_Cover *fcover)
{
if (! fcover)
return;
if (fcover->url)
g_string_free (fcover->url, TRUE);
if (fcover->image)
g_object_unref (fcover->image);
if (fcover->dir)
g_free (fcover->dir);
if (fcover->filename)
g_free (fcover->filename);
if (fcover->err_msg)
g_free (fcover->err_msg);
g_free (fcover);
}
|