blob: bce6415cd00b104d6e1992a53a6d4874cc1207ac [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>
Linus Walleij6f050022009-05-06 21:14:41 +000031#ifdef HAVE_SYS_UIO_H
Linus Walleij2eaaff02009-01-15 21:30:36 +000032#include <sys/uio.h>
Linus Walleij6f050022009-05-06 21:14:41 +000033#endif
mopoke11235ea2006-10-29 08:34:25 +000034
35static void usage(void) {
36 printf("Usage: albumart -i <fileid/trackid> -n <albumname> <imagefile>\n");
37 exit(0);
38}
39
40int 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 Walleij20c3b672007-11-09 19:02:19 +000053 char *rest;
mopoke11235ea2006-10-29 08:34:25 +000054 struct stat statbuff;
55
tedbullockcd9f4992007-03-29 06:00:40 +000056 fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
57
mopoke11235ea2006-10-29 08:34:25 +000058 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 Walleij20c3b672007-11-09 19:02:19 +000069 ids[(idcount-1)] = strtoul(optarg, &rest, 0);
mopoke11235ea2006-10-29 08:34:25 +000070 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 Walleij5b4a4d22009-01-10 12:18:14 +0000107 if ( (fd = open(path, O_RDONLY|O_BINARY) == -1) ) {
mopoke11235ea2006-10-29 08:34:25 +0000108#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
rreardon5332f9c2006-12-05 10:18:23 +0000126 LIBMTP_filesampledata_t *albumart = LIBMTP_new_filesampledata_t();
127 albumart->data = imagedata;
128 albumart->size = filesize;
129 albumart->filetype = LIBMTP_FILETYPE_JPEG;
130
mopoke11235ea2006-10-29 08:34:25 +0000131 LIBMTP_album_t *album = LIBMTP_new_album_t();
132 album->name = albumname;
133 album->no_tracks = idcount;
134 album->tracks = ids;
Linus Walleijea68f1f2008-06-22 21:54:44 +0000135 album->parent_id = 0;
136 album->storage_id = 0;
137 int ret = LIBMTP_Create_New_Album(device,album);
mopoke11235ea2006-10-29 08:34:25 +0000138 if (ret == 0) {
rreardon5332f9c2006-12-05 10:18:23 +0000139 ret = LIBMTP_Send_Representative_Sample(device,album->album_id, albumart);
mopoke11235ea2006-10-29 08:34:25 +0000140 if (ret != 0) {
141 printf("Couldn't send album art\n");
Linus Walleij070e9b42007-01-22 08:49:28 +0000142 LIBMTP_Dump_Errorstack(device);
143 LIBMTP_Clear_Errorstack(device);
mopoke11235ea2006-10-29 08:34:25 +0000144 }
145 }
146 else {
147 printf("Couldn't create album object\n");
Linus Walleij070e9b42007-01-22 08:49:28 +0000148 LIBMTP_Dump_Errorstack(device);
149 LIBMTP_Clear_Errorstack(device);
mopoke11235ea2006-10-29 08:34:25 +0000150 }
151
Linus Walleijf1b02f22006-12-06 09:03:23 +0000152 LIBMTP_destroy_filesampledata_t(albumart);
153 LIBMTP_destroy_album_t(album);
154
mopoke11235ea2006-10-29 08:34:25 +0000155 LIBMTP_Release_Device(device);
156 printf("OK.\n");
157 return 0;
158}
159