blob: 7c047d107c63eb27cc5a21beb39dde64050040e1 [file] [log] [blame]
Linus Walleij543badf2007-02-05 19:07:38 +00001/**
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 */
mopoke11235ea2006-10-29 08:34:25 +000022#include "common.h"
23#include "string.h"
Linus Walleij2eaaff02009-01-15 21:30:36 +000024#include <stdlib.h>
25#include <limits.h>
26#include <unistd.h>
mopoke11235ea2006-10-29 08:34:25 +000027#include <fcntl.h>
28#include <errno.h>
Linus Walleij2eaaff02009-01-15 21:30:36 +000029#include <sys/stat.h>
30#include <sys/types.h>
31#include <sys/uio.h>
mopoke11235ea2006-10-29 08:34:25 +000032
33static void usage(void) {
34 printf("Usage: albumart -i <fileid/trackid> -n <albumname> <imagefile>\n");
35 exit(0);
36}
37
38int main (int argc, char **argv) {
39 int opt;
40 extern int optind;
41 extern char *optarg;
42 LIBMTP_mtpdevice_t *device = NULL;
43 int idcount = 0;
44 int fd;
45 uint32_t *ids = NULL;
46 uint32_t *tmp = NULL;
47 uint64_t filesize;
48 char *imagedata = NULL;
49 char *albumname = NULL;
50 char *path = NULL;
Linus Walleij20c3b672007-11-09 19:02:19 +000051 char *rest;
mopoke11235ea2006-10-29 08:34:25 +000052 struct stat statbuff;
53
tedbullockcd9f4992007-03-29 06:00:40 +000054 fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
55
mopoke11235ea2006-10-29 08:34:25 +000056 while ( (opt = getopt(argc, argv, "hn:i:")) != -1 ) {
57 switch (opt) {
58 case 'h':
59 usage();
60 case 'i':
61 idcount++;
62 if ((tmp = realloc(ids, sizeof(uint32_t) * (idcount))) == NULL) {
63 printf("realloc failed\n");
64 return 1;
65 }
66 ids = tmp;
Linus Walleij20c3b672007-11-09 19:02:19 +000067 ids[(idcount-1)] = strtoul(optarg, &rest, 0);
mopoke11235ea2006-10-29 08:34:25 +000068 break;
69 case 'n':
70 albumname = strdup(optarg);
71 break;
72 default:
73 usage();
74 }
75 }
76 argc -= optind;
77 argv += optind;
78
79 if ( argc != 1 ) {
80 printf("You need to pass a filename.\n");
81 usage();
82 }
83
84 if ( albumname == NULL) {
85 printf("You need to supply an album name.\n");
86 usage();
87 }
88
89 if (idcount == 0) {
90 printf("You need to supply one or more track IDs\n");
91 usage();
92 }
93
94 path = argv[0];
95
96 if ( stat(path, &statbuff) == -1 ) {
97 fprintf(stderr, "%s: ", path);
98 perror("stat");
99 exit(1);
100 }
101 filesize = (uint64_t) statbuff.st_size;
102 imagedata = malloc(filesize * sizeof(uint8_t));
103
104#ifdef __WIN32__
Linus Walleij5b4a4d22009-01-10 12:18:14 +0000105 if ( (fd = open(path, O_RDONLY|O_BINARY) == -1) ) {
mopoke11235ea2006-10-29 08:34:25 +0000106#else
107 if ( (fd = open(path, O_RDONLY)) == -1) {
108#endif
109 printf("Couldn't open image file %s (%s)\n",path,strerror(errno));
110 return 1;
111 }
112 else {
113 read(fd, imagedata, filesize);
114 close(fd);
115 }
116
117 LIBMTP_Init();
118 device = LIBMTP_Get_First_Device();
119 if (device == NULL) {
120 printf("No devices.\n");
121 return 0;
122 }
123
rreardon5332f9c2006-12-05 10:18:23 +0000124 LIBMTP_filesampledata_t *albumart = LIBMTP_new_filesampledata_t();
125 albumart->data = imagedata;
126 albumart->size = filesize;
127 albumart->filetype = LIBMTP_FILETYPE_JPEG;
128
mopoke11235ea2006-10-29 08:34:25 +0000129 LIBMTP_album_t *album = LIBMTP_new_album_t();
130 album->name = albumname;
131 album->no_tracks = idcount;
132 album->tracks = ids;
Linus Walleijea68f1f2008-06-22 21:54:44 +0000133 album->parent_id = 0;
134 album->storage_id = 0;
135 int ret = LIBMTP_Create_New_Album(device,album);
mopoke11235ea2006-10-29 08:34:25 +0000136 if (ret == 0) {
rreardon5332f9c2006-12-05 10:18:23 +0000137 ret = LIBMTP_Send_Representative_Sample(device,album->album_id, albumart);
mopoke11235ea2006-10-29 08:34:25 +0000138 if (ret != 0) {
139 printf("Couldn't send album art\n");
Linus Walleij070e9b42007-01-22 08:49:28 +0000140 LIBMTP_Dump_Errorstack(device);
141 LIBMTP_Clear_Errorstack(device);
mopoke11235ea2006-10-29 08:34:25 +0000142 }
143 }
144 else {
145 printf("Couldn't create album object\n");
Linus Walleij070e9b42007-01-22 08:49:28 +0000146 LIBMTP_Dump_Errorstack(device);
147 LIBMTP_Clear_Errorstack(device);
mopoke11235ea2006-10-29 08:34:25 +0000148 }
149
Linus Walleijf1b02f22006-12-06 09:03:23 +0000150 LIBMTP_destroy_filesampledata_t(albumart);
151 LIBMTP_destroy_album_t(album);
152
mopoke11235ea2006-10-29 08:34:25 +0000153 LIBMTP_Release_Device(device);
154 printf("OK.\n");
155 return 0;
156}
157