blob: 3dc360e3a1ef036df3d84ac9caefbe5bc9575e1d [file] [log] [blame]
Linus Walleij3d78c4c2007-02-15 12:18:12 +00001/**
2 * \file thumb.c
3 * Example program to send and associate album art to an entity
4 * on a device.
5 *
6 * Copyright (C) 2006 Robert Reardon <rreardon@monkshatch.vispa.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 */
Linus Walleij2f1b6402009-06-15 19:49:33 +000023#include "config.h"
rreardon5332f9c2006-12-05 10:18:23 +000024#include "common.h"
25#include "string.h"
rreardon5332f9c2006-12-05 10:18:23 +000026#include <fcntl.h>
27#include <errno.h>
Linus Walleij2eaaff02009-01-15 21:30:36 +000028#include <stdlib.h>
29#include <unistd.h>
30#include <limits.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
Linus Walleij2eaaff02009-01-15 21:30:36 +000035#include <sys/stat.h>
rreardon5332f9c2006-12-05 10:18:23 +000036
37static void usage(void) {
38 printf("Usage: thumb -i <fileid/trackid> <imagefile>\n");
39 exit(0);
40}
41
42int main (int argc, char **argv) {
43 int opt;
44 extern int optind;
45 extern char *optarg;
46 LIBMTP_mtpdevice_t *device = NULL;
47 int fd;
48 uint32_t id = 0;
49 uint64_t filesize;
Marcus Meissnerf59e7fd2015-10-04 14:56:03 +020050 char *imagedata = NULL;
rreardon5332f9c2006-12-05 10:18:23 +000051 char *path = NULL;
Linus Walleij20c3b672007-11-09 19:02:19 +000052 char *rest;
rreardon5332f9c2006-12-05 10:18:23 +000053 struct stat statbuff;
54 int ret;
55
tedbullockcd9f4992007-03-29 06:00:40 +000056 fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
57
rreardon5332f9c2006-12-05 10:18:23 +000058 while ( (opt = getopt(argc, argv, "hi:")) != -1 ) {
59 switch (opt) {
60 case 'h':
61 usage();
62 case 'i':
Linus Walleij20c3b672007-11-09 19:02:19 +000063 id = strtoul(optarg, &rest, 0);
rreardon5332f9c2006-12-05 10:18:23 +000064 break;
65 default:
66 usage();
67 }
68 }
69 argc -= optind;
70 argv += optind;
71
72 if ( argc != 1 ) {
73 printf("You need to pass a filename.\n");
74 usage();
75 }
76
77 path = argv[0];
78
79 if ( stat(path, &statbuff) == -1 ) {
80 fprintf(stderr, "%s: ", path);
81 perror("stat");
82 exit(1);
83 }
Marcus Meissnerf59e7fd2015-10-04 14:56:03 +020084 filesize = statbuff.st_size;
85 imagedata = malloc(filesize);
rreardon5332f9c2006-12-05 10:18:23 +000086
87#ifdef __WIN32__
Linus Walleij5b4a4d22009-01-10 12:18:14 +000088 if ( (fd = open(path, O_RDONLY|O_BINARY) == -1) ) {
rreardon5332f9c2006-12-05 10:18:23 +000089#else
90 if ( (fd = open(path, O_RDONLY)) == -1) {
91#endif
92 printf("Couldn't open image file %s (%s)\n",path,strerror(errno));
93 return 1;
Marcus Meissnerf59e7fd2015-10-04 14:56:03 +020094 } else {
95 ret = read(fd, imagedata, filesize);
96 if (ret == -1) perror("read thumb data");
rreardon5332f9c2006-12-05 10:18:23 +000097 close(fd);
98 }
99
100 LIBMTP_Init();
101 device = LIBMTP_Get_First_Device();
102 if (device == NULL) {
103 printf("No devices.\n");
104 return 0;
105 }
106
107 LIBMTP_filesampledata_t *thumb = LIBMTP_new_filesampledata_t();
Marcus Meissnerf59e7fd2015-10-04 14:56:03 +0200108 thumb->data = imagedata;
rreardon5332f9c2006-12-05 10:18:23 +0000109 thumb->size = filesize;
110 thumb->filetype = LIBMTP_FILETYPE_JPEG;
rreardon5332f9c2006-12-05 10:18:23 +0000111 ret = LIBMTP_Send_Representative_Sample(device,id,thumb);
112 if (ret != 0) {
113 printf("Couldn't send thumbnail\n");
Linus Walleij070e9b42007-01-22 08:49:28 +0000114 LIBMTP_Dump_Errorstack(device);
115 LIBMTP_Clear_Errorstack(device);
rreardon5332f9c2006-12-05 10:18:23 +0000116 }
117
Linus Walleijf1b02f22006-12-06 09:03:23 +0000118 LIBMTP_destroy_filesampledata_t(thumb);
rreardon5332f9c2006-12-05 10:18:23 +0000119
120 LIBMTP_Release_Device(device);
121 printf("OK.\n");
122 return 0;
123}