Linus Walleij | 543badf | 2007-02-05 19:07:38 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file albumart.c |
| 3 | * Example program to send album art. |
| 4 | * |
| 5 | * Copyright (C) 2006 Andy Kelk <andy@mopoke.co.uk> |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the |
| 19 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 20 | * Boston, MA 02111-1307, USA. |
| 21 | */ |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 22 | #include "common.h" |
| 23 | #include "string.h" |
Linus Walleij | 2eaaff0 | 2009-01-15 21:30:36 +0000 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <limits.h> |
| 26 | #include <unistd.h> |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 27 | #include <fcntl.h> |
| 28 | #include <errno.h> |
Linus Walleij | 2eaaff0 | 2009-01-15 21:30:36 +0000 | [diff] [blame] | 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
Linus Walleij | 6f05002 | 2009-05-06 21:14:41 +0000 | [diff] [blame] | 31 | #ifdef HAVE_SYS_UIO_H |
Linus Walleij | 2eaaff0 | 2009-01-15 21:30:36 +0000 | [diff] [blame] | 32 | #include <sys/uio.h> |
Linus Walleij | 6f05002 | 2009-05-06 21:14:41 +0000 | [diff] [blame] | 33 | #endif |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 34 | |
| 35 | static void usage(void) { |
| 36 | printf("Usage: albumart -i <fileid/trackid> -n <albumname> <imagefile>\n"); |
| 37 | exit(0); |
| 38 | } |
| 39 | |
| 40 | int main (int argc, char **argv) { |
| 41 | int opt; |
| 42 | extern int optind; |
| 43 | extern char *optarg; |
| 44 | LIBMTP_mtpdevice_t *device = NULL; |
| 45 | int idcount = 0; |
| 46 | int fd; |
| 47 | uint32_t *ids = NULL; |
| 48 | uint32_t *tmp = NULL; |
| 49 | uint64_t filesize; |
| 50 | char *imagedata = NULL; |
| 51 | char *albumname = NULL; |
| 52 | char *path = NULL; |
Linus Walleij | 20c3b67 | 2007-11-09 19:02:19 +0000 | [diff] [blame] | 53 | char *rest; |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 54 | struct stat statbuff; |
| 55 | |
tedbullock | cd9f499 | 2007-03-29 06:00:40 +0000 | [diff] [blame] | 56 | fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n"); |
| 57 | |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 58 | while ( (opt = getopt(argc, argv, "hn:i:")) != -1 ) { |
| 59 | switch (opt) { |
| 60 | case 'h': |
| 61 | usage(); |
| 62 | case 'i': |
| 63 | idcount++; |
| 64 | if ((tmp = realloc(ids, sizeof(uint32_t) * (idcount))) == NULL) { |
| 65 | printf("realloc failed\n"); |
| 66 | return 1; |
| 67 | } |
| 68 | ids = tmp; |
Linus Walleij | 20c3b67 | 2007-11-09 19:02:19 +0000 | [diff] [blame] | 69 | ids[(idcount-1)] = strtoul(optarg, &rest, 0); |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 70 | break; |
| 71 | case 'n': |
| 72 | albumname = strdup(optarg); |
| 73 | break; |
| 74 | default: |
| 75 | usage(); |
| 76 | } |
| 77 | } |
| 78 | argc -= optind; |
| 79 | argv += optind; |
| 80 | |
| 81 | if ( argc != 1 ) { |
| 82 | printf("You need to pass a filename.\n"); |
| 83 | usage(); |
| 84 | } |
| 85 | |
| 86 | if ( albumname == NULL) { |
| 87 | printf("You need to supply an album name.\n"); |
| 88 | usage(); |
| 89 | } |
| 90 | |
| 91 | if (idcount == 0) { |
| 92 | printf("You need to supply one or more track IDs\n"); |
| 93 | usage(); |
| 94 | } |
| 95 | |
| 96 | path = argv[0]; |
| 97 | |
| 98 | if ( stat(path, &statbuff) == -1 ) { |
| 99 | fprintf(stderr, "%s: ", path); |
| 100 | perror("stat"); |
| 101 | exit(1); |
| 102 | } |
| 103 | filesize = (uint64_t) statbuff.st_size; |
| 104 | imagedata = malloc(filesize * sizeof(uint8_t)); |
| 105 | |
| 106 | #ifdef __WIN32__ |
Linus Walleij | 5b4a4d2 | 2009-01-10 12:18:14 +0000 | [diff] [blame] | 107 | if ( (fd = open(path, O_RDONLY|O_BINARY) == -1) ) { |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 108 | #else |
| 109 | if ( (fd = open(path, O_RDONLY)) == -1) { |
| 110 | #endif |
| 111 | printf("Couldn't open image file %s (%s)\n",path,strerror(errno)); |
| 112 | return 1; |
| 113 | } |
| 114 | else { |
| 115 | read(fd, imagedata, filesize); |
| 116 | close(fd); |
| 117 | } |
| 118 | |
| 119 | LIBMTP_Init(); |
| 120 | device = LIBMTP_Get_First_Device(); |
| 121 | if (device == NULL) { |
| 122 | printf("No devices.\n"); |
| 123 | return 0; |
| 124 | } |
| 125 | |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 126 | LIBMTP_filesampledata_t *albumart = LIBMTP_new_filesampledata_t(); |
| 127 | albumart->data = imagedata; |
| 128 | albumart->size = filesize; |
| 129 | albumart->filetype = LIBMTP_FILETYPE_JPEG; |
| 130 | |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 131 | LIBMTP_album_t *album = LIBMTP_new_album_t(); |
| 132 | album->name = albumname; |
| 133 | album->no_tracks = idcount; |
| 134 | album->tracks = ids; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 135 | album->parent_id = 0; |
| 136 | album->storage_id = 0; |
| 137 | int ret = LIBMTP_Create_New_Album(device,album); |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 138 | if (ret == 0) { |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 139 | ret = LIBMTP_Send_Representative_Sample(device,album->album_id, albumart); |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 140 | if (ret != 0) { |
| 141 | printf("Couldn't send album art\n"); |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 142 | LIBMTP_Dump_Errorstack(device); |
| 143 | LIBMTP_Clear_Errorstack(device); |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | else { |
| 147 | printf("Couldn't create album object\n"); |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 148 | LIBMTP_Dump_Errorstack(device); |
| 149 | LIBMTP_Clear_Errorstack(device); |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 152 | LIBMTP_destroy_filesampledata_t(albumart); |
| 153 | LIBMTP_destroy_album_t(album); |
| 154 | |
mopoke | 11235ea | 2006-10-29 08:34:25 +0000 | [diff] [blame] | 155 | LIBMTP_Release_Device(device); |
| 156 | printf("OK.\n"); |
| 157 | return 0; |
| 158 | } |
| 159 | |