Home | History | Annotate | Download | only in examples
      1 /**
      2  * \file albumart.c
      3  * Example program to send album art.
      4  *
      5  * Copyright (C) 2006 Andy Kelk <andy (at) 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  */
     22 #include "config.h"
     23 #include "common.h"
     24 #include "string.h"
     25 #include <stdlib.h>
     26 #include <limits.h>
     27 #include <unistd.h>
     28 #include <fcntl.h>
     29 #include <errno.h>
     30 #include <sys/stat.h>
     31 #include <sys/types.h>
     32 #ifdef HAVE_SYS_UIO_H
     33 #include <sys/uio.h>
     34 #endif
     35 
     36 static void usage(void) {
     37   printf("Usage: albumart -d -i <fileid/trackid> -n <albumname> -s <storage_id> -p <parent_id> <imagefile>\n");
     38   exit(0);
     39 }
     40 
     41 int 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;
     54   char *rest;
     55   struct stat statbuff;
     56   uint32_t storageid = 0;
     57   uint32_t parentid = 0;
     58   int ret;
     59 
     60   fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
     61 
     62   while ( (opt = getopt(argc, argv, "dhn:i:s:p:")) != -1 ) {
     63     switch (opt) {
     64     case 'h':
     65       usage();
     66     case 'd':
     67       LIBMTP_Set_Debug(LIBMTP_DEBUG_PTP | LIBMTP_DEBUG_DATA);
     68       break;
     69     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;
     76       ids[(idcount-1)] = strtoul(optarg, &rest, 0);
     77       break;
     78     case 'n':
     79       albumname = strdup(optarg);
     80       break;
     81     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;
     87     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__
    120   if ( (fd = open(path, O_RDONLY|O_BINARY) == -1) ) {
    121 #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 {
    128     ret = read(fd, imagedata, filesize);
    129     if (ret == -1) perror("read");
    130     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 
    140   LIBMTP_filesampledata_t *albumart = LIBMTP_new_filesampledata_t();
    141   albumart->data = imagedata;
    142   albumart->size = filesize;
    143   albumart->filetype = LIBMTP_FILETYPE_JPEG;
    144 
    145   LIBMTP_album_t *album = LIBMTP_new_album_t();
    146   album->name = albumname;
    147   album->no_tracks = idcount;
    148   album->tracks = ids;
    149   album->parent_id = parentid;
    150   album->storage_id = storageid;
    151 
    152   ret = LIBMTP_Create_New_Album(device,album);
    153   if (ret == 0) {
    154     ret = LIBMTP_Send_Representative_Sample(device,album->album_id, albumart);
    155     if (ret != 0) {
    156       printf("Couldn't send album art\n");
    157       LIBMTP_Dump_Errorstack(device);
    158       LIBMTP_Clear_Errorstack(device);
    159     }
    160   }
    161   else {
    162     printf("Couldn't create album object\n");
    163     LIBMTP_Dump_Errorstack(device);
    164     LIBMTP_Clear_Errorstack(device);
    165   }
    166 
    167   LIBMTP_destroy_filesampledata_t(albumart);
    168   LIBMTP_destroy_album_t(album);
    169 
    170   LIBMTP_Release_Device(device);
    171   printf("OK.\n");
    172   return 0;
    173 }
    174 
    175