OLD | NEW |
1 /** | 1 /** |
2 * \file sendtr.c | 2 * \file sendtr.c |
3 * Example program to send a music track to a device. | 3 * Example program to send a music track to a device. |
4 * This program is derived from the exact equivalent in libnjb. | 4 * This program is derived from the exact equivalent in libnjb. |
5 * based on Enrique Jorreto Ledesma's work on the original program by | 5 * based on Enrique Jorreto Ledesma's work on the original program by |
6 * Shaun Jackman and Linus Walleij. | 6 * Shaun Jackman and Linus Walleij. |
7 * | 7 * |
8 * Copyright (C) 2003-2010 Linus Walleij <triad@df.lth.se> | 8 * Copyright (C) 2003-2010 Linus Walleij <triad@df.lth.se> |
9 * Copyright (C) 2003-2005 Shaun Jackman | 9 * Copyright (C) 2003-2005 Shaun Jackman |
10 * Copyright (C) 2003-2005 Enrique Jorrete Ledesma | 10 * Copyright (C) 2003-2005 Enrique Jorrete Ledesma |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 } else { | 179 } else { |
180 printf("success!\n"); | 180 printf("success!\n"); |
181 } | 181 } |
182 return ret; | 182 return ret; |
183 } | 183 } |
184 | 184 |
185 int sendtrack_function(char * from_path, char * to_path, char *partist, char *pa
lbumartist, char *ptitle, char *pgenre, char *palbum, char *pcomposer, uint16_t
tracknum, uint16_t length, uint16_t year, uint32_t storageid, uint16_t quiet) | 185 int sendtrack_function(char * from_path, char * to_path, char *partist, char *pa
lbumartist, char *ptitle, char *pgenre, char *palbum, char *pcomposer, uint16_t
tracknum, uint16_t length, uint16_t year, uint32_t storageid, uint16_t quiet) |
186 { | 186 { |
187 char *filename, *parent; | 187 char *filename, *parent; |
188 char artist[80], albumartist[80], title[80], genre[80], album[80], composer[80
]; | 188 char artist[80], albumartist[80], title[80], genre[80], album[80], composer[80
]; |
| 189 char *to_path_copy = NULL; |
189 char num[80]; | 190 char num[80]; |
190 uint64_t filesize; | 191 uint64_t filesize; |
191 uint32_t parent_id = 0; | 192 uint32_t parent_id = 0; |
192 struct stat sb; | 193 struct stat sb; |
193 LIBMTP_track_t *trackmeta; | 194 LIBMTP_track_t *trackmeta; |
194 LIBMTP_album_t *albuminfo; | 195 LIBMTP_album_t *albuminfo; |
195 int ret; | 196 int ret; |
196 | 197 |
197 printf("Sending track %s to %s\n", from_path, to_path); | 198 printf("Sending track %s to %s\n", from_path, to_path); |
198 | 199 |
199 trackmeta = LIBMTP_new_track_t(); | 200 to_path_copy = strdup(to_path); |
200 albuminfo = LIBMTP_new_album_t(); | 201 parent = dirname(to_path_copy); |
201 | |
202 parent = dirname(strdup(to_path)); | |
203 filename = basename(strdup(to_path)); | |
204 parent_id = parse_path (parent,files,folders); | 202 parent_id = parse_path (parent,files,folders); |
205 if (parent_id == -1) { | 203 if (parent_id == -1) { |
| 204 free (to_path_copy); |
206 printf("Parent folder could not be found, skipping\n"); | 205 printf("Parent folder could not be found, skipping\n"); |
207 return 1; | 206 return 1; |
208 } | 207 } |
| 208 strcpy (to_path_copy,to_path); |
| 209 filename = basename(to_path_copy); |
209 | 210 |
210 if (stat(from_path, &sb) == -1) { | 211 if (stat(from_path, &sb) == -1) { |
211 fprintf(stderr, "%s: ", from_path); | 212 fprintf(stderr, "%s: ", from_path); |
212 perror("stat"); | 213 perror("stat"); |
| 214 free (to_path_copy); |
213 return 1; | 215 return 1; |
214 } | 216 } |
215 | 217 |
216 if (!S_ISREG(sb.st_mode)) | 218 if (!S_ISREG(sb.st_mode)) { |
| 219 free (to_path_copy); |
217 return 0; | 220 return 0; |
| 221 } |
218 | 222 |
219 filesize = sb.st_size; | 223 filesize = sb.st_size; |
| 224 |
| 225 trackmeta = LIBMTP_new_track_t(); |
220 trackmeta->filetype = find_filetype (from_path); | 226 trackmeta->filetype = find_filetype (from_path); |
221 if (!LIBMTP_FILETYPE_IS_TRACK(trackmeta->filetype)) { | 227 if (!LIBMTP_FILETYPE_IS_TRACK(trackmeta->filetype)) { |
222 printf("Not a valid track codec: \"%s\"\n", LIBMTP_Get_Filetype_Description(
trackmeta->filetype)); | 228 printf("Not a valid track codec: \"%s\"\n", LIBMTP_Get_Filetype_Description(
trackmeta->filetype)); |
| 229 LIBMTP_destroy_track_t(trackmeta); |
| 230 free (to_path_copy); |
223 return 1; | 231 return 1; |
224 } | 232 } |
225 | 233 |
226 if ((ptitle == NULL) && (quiet == 0)) { | 234 if ((ptitle == NULL) && (quiet == 0)) { |
227 if ( (ptitle = prompt("Title", title, 80, 0)) != NULL ) | 235 if ( (ptitle = prompt("Title", title, 80, 0)) != NULL ) |
228 if (!strlen(ptitle)) ptitle = NULL; | 236 if (!strlen(ptitle)) ptitle = NULL; |
229 } | 237 } |
230 | 238 |
231 if ((palbum == NULL) && (quiet == 0)) { | 239 if ((palbum == NULL) && (quiet == 0)) { |
232 if ( (palbum = prompt("Album", album, 80, 0)) != NULL ) | 240 if ( (palbum = prompt("Album", album, 80, 0)) != NULL ) |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 else | 284 else |
277 length = strtoul(pnum, 0, 10); | 285 length = strtoul(pnum, 0, 10); |
278 } | 286 } |
279 | 287 |
280 printf("Sending track:\n"); | 288 printf("Sending track:\n"); |
281 printf("Codec: %s\n", LIBMTP_Get_Filetype_Description(trackmeta->filetype)
); | 289 printf("Codec: %s\n", LIBMTP_Get_Filetype_Description(trackmeta->filetype)
); |
282 if (ptitle) { | 290 if (ptitle) { |
283 printf("Title: %s\n", ptitle); | 291 printf("Title: %s\n", ptitle); |
284 trackmeta->title = strdup(ptitle); | 292 trackmeta->title = strdup(ptitle); |
285 } | 293 } |
| 294 |
| 295 albuminfo = LIBMTP_new_album_t(); |
| 296 |
286 if (palbum) { | 297 if (palbum) { |
287 printf("Album: %s\n", palbum); | 298 printf("Album: %s\n", palbum); |
288 trackmeta->album = strdup(palbum); | 299 trackmeta->album = strdup(palbum); |
289 albuminfo->name = strdup(palbum); | 300 albuminfo->name = strdup(palbum); |
290 } | 301 } |
291 if (palbumartist) { | 302 if (palbumartist) { |
292 printf("Album artist: %s\n", palbumartist); | 303 printf("Album artist: %s\n", palbumartist); |
293 albuminfo->artist = strdup(palbumartist); | 304 albuminfo->artist = strdup(palbumartist); |
294 } | 305 } |
295 if (partist) { | 306 if (partist) { |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 } else { | 375 } else { |
365 printf("New track ID: %d\n", trackmeta->item_id); | 376 printf("New track ID: %d\n", trackmeta->item_id); |
366 } | 377 } |
367 | 378 |
368 /* Add here add to album call */ | 379 /* Add here add to album call */ |
369 if (palbum) | 380 if (palbum) |
370 ret = add_track_to_album(albuminfo, trackmeta); | 381 ret = add_track_to_album(albuminfo, trackmeta); |
371 | 382 |
372 LIBMTP_destroy_album_t(albuminfo); | 383 LIBMTP_destroy_album_t(albuminfo); |
373 LIBMTP_destroy_track_t(trackmeta); | 384 LIBMTP_destroy_track_t(trackmeta); |
| 385 free (to_path_copy); |
374 | 386 |
375 return ret; | 387 return ret; |
376 } | 388 } |
377 | 389 |
378 int sendtrack_command (int argc, char **argv) { | 390 int sendtrack_command (int argc, char **argv) { |
379 int opt; | 391 int opt, ret; |
380 extern int optind; | 392 extern int optind; |
381 extern char *optarg; | 393 extern char *optarg; |
382 char *partist = NULL; | 394 char *partist = NULL; |
383 char *palbumartist = NULL; | 395 char *palbumartist = NULL; |
384 char *pcomposer = NULL; | 396 char *pcomposer = NULL; |
385 char *ptitle = NULL; | 397 char *ptitle = NULL; |
386 char *pgenre = NULL; | 398 char *pgenre = NULL; |
387 char *pcodec = NULL; | 399 char *pcodec = NULL; |
388 char *palbum = NULL; | 400 char *palbum = NULL; |
389 uint16_t tracknum = 0; | 401 uint16_t tracknum = 0; |
390 uint16_t length = 0; | 402 uint16_t length = 0; |
391 uint16_t year = 0; | 403 uint16_t year = 0; |
392 uint16_t quiet = 0; | 404 uint16_t quiet = 0; |
393 uint32_t storageid = 0; | 405 uint32_t storageid = 0; |
394 while ( (opt = getopt(argc, argv, "qD:t:a:A:w:l:c:g:n:d:y:s:")) != -1 ) { | 406 while ( (opt = getopt(argc, argv, "qD:t:a:A:w:l:c:g:n:d:y:s:")) != -1 ) { |
395 switch (opt) { | 407 switch (opt) { |
396 case 't': | 408 case 't': |
| 409 free (ptitle); |
397 ptitle = strdup(optarg); | 410 ptitle = strdup(optarg); |
398 break; | 411 break; |
399 case 'a': | 412 case 'a': |
| 413 free (partist); |
400 partist = strdup(optarg); | 414 partist = strdup(optarg); |
401 break; | 415 break; |
402 case 'A': | 416 case 'A': |
| 417 free (palbumartist); |
403 palbumartist = strdup(optarg); | 418 palbumartist = strdup(optarg); |
404 break; | 419 break; |
405 case 'w': | 420 case 'w': |
| 421 free (pcomposer); |
406 pcomposer = strdup(optarg); | 422 pcomposer = strdup(optarg); |
407 break; | 423 break; |
408 case 'l': | 424 case 'l': |
| 425 free (palbum); |
409 palbum = strdup(optarg); | 426 palbum = strdup(optarg); |
410 break; | 427 break; |
411 case 'c': | 428 case 'c': |
| 429 free (pcodec); |
412 pcodec = strdup(optarg); // FIXME: DSM check for MP3, WAV or WMA | 430 pcodec = strdup(optarg); // FIXME: DSM check for MP3, WAV or WMA |
413 break; | 431 break; |
414 case 'g': | 432 case 'g': |
| 433 free (pgenre); |
415 pgenre = strdup(optarg); | 434 pgenre = strdup(optarg); |
416 break; | 435 break; |
417 case 'n': | 436 case 'n': |
418 tracknum = atoi(optarg); | 437 tracknum = atoi(optarg); |
419 break; | 438 break; |
420 case 's': | 439 case 's': |
421 storageid = (uint32_t) strtoul(optarg, NULL, 0); | 440 storageid = (uint32_t) strtoul(optarg, NULL, 0); |
422 break; | 441 break; |
423 case 'd': | 442 case 'd': |
424 length = atoi(optarg); | 443 length = atoi(optarg); |
425 break; | 444 break; |
426 case 'y': | 445 case 'y': |
427 year = atoi(optarg); | 446 year = atoi(optarg); |
428 break; | 447 break; |
429 case 'q': | 448 case 'q': |
430 quiet = 1; | 449 quiet = 1; |
431 break; | 450 break; |
432 default: | 451 default: |
433 sendtrack_usage(); | 452 sendtrack_usage(); |
434 } | 453 } |
435 } | 454 } |
436 argc -= optind; | 455 argc -= optind; |
437 argv += optind; | 456 argv += optind; |
438 | 457 |
439 if ( argc != 2 ) { | 458 if ( argc != 2 ) { |
440 printf("You need to pass a filename and destination.\n"); | 459 printf("You need to pass a filename and destination.\n"); |
441 sendtrack_usage(); | 460 sendtrack_usage(); |
442 return 0; | 461 ret = 0; |
| 462 } else { |
| 463 checklang(); |
| 464 printf("%s,%s,%s,%s,%s,%s,%s,%s,%d%d,%d,%u,%d\n",argv[0],argv[1],partist,pal
bumartist,ptitle,pgenre,palbum,pcomposer,tracknum, length, year, storageid, quie
t); |
| 465 ret = sendtrack_function(argv[0],argv[1],partist,palbumartist,ptitle,pgenre,
palbum,pcomposer, tracknum, length, year, storageid, quiet); |
443 } | 466 } |
444 | 467 free (ptitle); |
445 checklang(); | 468 free (partist); |
446 | 469 free (palbumartist); |
447 printf("%s,%s,%s,%s,%s,%s,%s,%s,%d%d,%d,%u,%d\n",argv[0],argv[1],partist,palbu
martist,ptitle,pgenre,palbum,pcomposer,tracknum, length, year, storageid, quiet)
; | 470 free (pcomposer); |
448 return sendtrack_function(argv[0],argv[1],partist,palbumartist,ptitle,pgenre,p
album,pcomposer, tracknum, length, year, storageid, quiet); | 471 free (palbum); |
| 472 free (pcodec); |
| 473 free (pgenre); |
| 474 return ret; |
449 } | 475 } |
OLD | NEW |