blob: 350ba4507d98917228742edbffa73cb461014488 [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 */
Linus Walleij2f1b6402009-06-15 19:49:33 +000022#include "config.h"
mopoke11235ea2006-10-29 08:34:25 +000023#include "common.h"
24#include "string.h"
Linus Walleij2eaaff02009-01-15 21:30:36 +000025#include <stdlib.h>
26#include <limits.h>
27#include <unistd.h>
mopoke11235ea2006-10-29 08:34:25 +000028#include <fcntl.h>
29#include <errno.h>
Linus Walleij2eaaff02009-01-15 21:30:36 +000030#include <sys/stat.h>
31#include <sys/types.h>
Linus Walleij6f050022009-05-06 21:14:41 +000032#ifdef HAVE_SYS_UIO_H
Linus Walleij2eaaff02009-01-15 21:30:36 +000033#include <sys/uio.h>
Linus Walleij6f050022009-05-06 21:14:41 +000034#endif
mopoke11235ea2006-10-29 08:34:25 +000035
36static void usage(void) {
nicklas79daadbf22009-09-28 18:19:34 +000037 printf("Usage: albumart -d -i <fileid/trackid> -n <albumname> -s <storage_id> -p <parent_id> <imagefile>\n");
mopoke11235ea2006-10-29 08:34:25 +000038 exit(0);
39}
40
41int main (int argc, char **argv) {
42 int opt;
43 extern int optind;
44 extern char *optarg;
45 LIBMTP_mtpdevice_t *device = NULL;
46 int idcount = 0;
47 int fd;
48 uint32_t *ids = NULL;
49 uint32_t *tmp = NULL;
50 uint64_t filesize;
51 char *imagedata = NULL;
52 char *albumname = NULL;
53 char *path = NULL;
Linus Walleij20c3b672007-11-09 19:02:19 +000054 char *rest;
mopoke11235ea2006-10-29 08:34:25 +000055 struct stat statbuff;
nicklas79aac47292009-09-28 18:16:45 +000056 uint32_t storageid = 0;
57 uint32_t parentid = 0;
Marcus Meissner9a28fa62015-10-04 15:11:40 +020058 int ret;
mopoke11235ea2006-10-29 08:34:25 +000059
tedbullockcd9f4992007-03-29 06:00:40 +000060 fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
61
nicklas79daadbf22009-09-28 18:19:34 +000062 while ( (opt = getopt(argc, argv, "dhn:i:s:p:")) != -1 ) {
mopoke11235ea2006-10-29 08:34:25 +000063 switch (opt) {
64 case 'h':
65 usage();
nicklas79daadbf22009-09-28 18:19:34 +000066 case 'd':
Catalin Patulea406d5f92012-07-20 19:00:24 -040067 LIBMTP_Set_Debug(LIBMTP_DEBUG_PTP | LIBMTP_DEBUG_DATA);
nicklas79daadbf22009-09-28 18:19:34 +000068 break;
mopoke11235ea2006-10-29 08:34:25 +000069 case 'i':
70 idcount++;
71 if ((tmp = realloc(ids, sizeof(uint32_t) * (idcount))) == NULL) {
72 printf("realloc failed\n");
73 return 1;
74 }
75 ids = tmp;
Linus Walleij20c3b672007-11-09 19:02:19 +000076 ids[(idcount-1)] = strtoul(optarg, &rest, 0);
mopoke11235ea2006-10-29 08:34:25 +000077 break;
78 case 'n':
79 albumname = strdup(optarg);
80 break;
nicklas79aac47292009-09-28 18:16:45 +000081 case 's':
82 storageid = (uint32_t) strtoul(optarg, NULL, 0);
83 break;
84 case 'p':
85 parentid = (uint32_t) strtoul(optarg, NULL, 0);
86 break;
mopoke11235ea2006-10-29 08:34:25 +000087 default:
88 usage();
89 }
90 }
91 argc -= optind;
92 argv += optind;
93
94 if ( argc != 1 ) {
95 printf("You need to pass a filename.\n");
96 usage();
97 }
98
99 if ( albumname == NULL) {
100 printf("You need to supply an album name.\n");
101 usage();
102 }
103
104 if (idcount == 0) {
105 printf("You need to supply one or more track IDs\n");
106 usage();
107 }
108
109 path = argv[0];
110
111 if ( stat(path, &statbuff) == -1 ) {
112 fprintf(stderr, "%s: ", path);
113 perror("stat");
114 exit(1);
115 }
116 filesize = (uint64_t) statbuff.st_size;
117 imagedata = malloc(filesize * sizeof(uint8_t));
118
119#ifdef __WIN32__
Linus Walleij5b4a4d22009-01-10 12:18:14 +0000120 if ( (fd = open(path, O_RDONLY|O_BINARY) == -1) ) {
mopoke11235ea2006-10-29 08:34:25 +0000121#else
122 if ( (fd = open(path, O_RDONLY)) == -1) {
123#endif
124 printf("Couldn't open image file %s (%s)\n",path,strerror(errno));
125 return 1;
126 }
127 else {
Marcus Meissner9a28fa62015-10-04 15:11:40 +0200128 ret = read(fd, imagedata, filesize);
129 if (ret == -1) perror("read");
mopoke11235ea2006-10-29 08:34:25 +0000130 close(fd);
131 }
132
133 LIBMTP_Init();
134 device = LIBMTP_Get_First_Device();
135 if (device == NULL) {
136 printf("No devices.\n");
137 return 0;
138 }
139
rreardon5332f9c2006-12-05 10:18:23 +0000140 LIBMTP_filesampledata_t *albumart = LIBMTP_new_filesampledata_t();
141 albumart->data = imagedata;
142 albumart->size = filesize;
143 albumart->filetype = LIBMTP_FILETYPE_JPEG;
144
mopoke11235ea2006-10-29 08:34:25 +0000145 LIBMTP_album_t *album = LIBMTP_new_album_t();
146 album->name = albumname;
147 album->no_tracks = idcount;
148 album->tracks = ids;
nicklas79aac47292009-09-28 18:16:45 +0000149 album->parent_id = parentid;
150 album->storage_id = storageid;
Marcus Meissner9a28fa62015-10-04 15:11:40 +0200151
152 ret = LIBMTP_Create_New_Album(device,album);
mopoke11235ea2006-10-29 08:34:25 +0000153 if (ret == 0) {
rreardon5332f9c2006-12-05 10:18:23 +0000154 ret = LIBMTP_Send_Representative_Sample(device,album->album_id, albumart);
mopoke11235ea2006-10-29 08:34:25 +0000155 if (ret != 0) {
156 printf("Couldn't send album art\n");
Linus Walleij070e9b42007-01-22 08:49:28 +0000157 LIBMTP_Dump_Errorstack(device);
158 LIBMTP_Clear_Errorstack(device);
mopoke11235ea2006-10-29 08:34:25 +0000159 }
160 }
161 else {
162 printf("Couldn't create album object\n");
Linus Walleij070e9b42007-01-22 08:49:28 +0000163 LIBMTP_Dump_Errorstack(device);
164 LIBMTP_Clear_Errorstack(device);
mopoke11235ea2006-10-29 08:34:25 +0000165 }
166
Linus Walleijf1b02f22006-12-06 09:03:23 +0000167 LIBMTP_destroy_filesampledata_t(albumart);
168 LIBMTP_destroy_album_t(album);
169
mopoke11235ea2006-10-29 08:34:25 +0000170 LIBMTP_Release_Device(device);
171 printf("OK.\n");
172 return 0;
173}
174