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 -i <fileid/trackid> -n <albumname> <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 
     57   fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
     58 
     59   while ( (opt = getopt(argc, argv, "hn:i:")) != -1 ) {
     60     switch (opt) {
     61     case 'h':
     62       usage();
     63     case 'i':
     64       idcount++;
     65       if ((tmp = realloc(ids, sizeof(uint32_t) * (idcount))) == NULL) {
     66         printf("realloc failed\n");
     67         return 1;
     68       }
     69       ids = tmp;
     70       ids[(idcount-1)] = strtoul(optarg, &rest, 0);
     71       break;
     72     case 'n':
     73       albumname = strdup(optarg);
     74       break;
     75     default:
     76       usage();
     77     }
     78   }
     79   argc -= optind;
     80   argv += optind;
     81 
     82   if ( argc != 1 ) {
     83     printf("You need to pass a filename.\n");
     84     usage();
     85   }
     86 
     87   if ( albumname == NULL) {
     88     printf("You need to supply an album name.\n");
     89     usage();
     90   }
     91 
     92   if (idcount == 0) {
     93     printf("You need to supply one or more track IDs\n");
     94     usage();
     95   }
     96 
     97   path = argv[0];
     98 
     99   if ( stat(path, &statbuff) == -1 ) {
    100     fprintf(stderr, "%s: ", path);
    101     perror("stat");
    102     exit(1);
    103   }
    104   filesize = (uint64_t) statbuff.st_size;
    105   imagedata = malloc(filesize * sizeof(uint8_t));
    106 
    107 #ifdef __WIN32__
    108   if ( (fd = open(path, O_RDONLY|O_BINARY) == -1) ) {
    109 #else
    110   if ( (fd = open(path, O_RDONLY)) == -1) {
    111 #endif
    112     printf("Couldn't open image file %s (%s)\n",path,strerror(errno));
    113     return 1;
    114   }
    115   else {
    116     read(fd, imagedata, filesize);
    117     close(fd);
    118   }
    119 
    120   LIBMTP_Init();
    121   device = LIBMTP_Get_First_Device();
    122   if (device == NULL) {
    123     printf("No devices.\n");
    124     return 0;
    125   }
    126 
    127   LIBMTP_filesampledata_t *albumart = LIBMTP_new_filesampledata_t();
    128   albumart->data = imagedata;
    129   albumart->size = filesize;
    130   albumart->filetype = LIBMTP_FILETYPE_JPEG;
    131 
    132   LIBMTP_album_t *album = LIBMTP_new_album_t();
    133   album->name = albumname;
    134   album->no_tracks = idcount;
    135   album->tracks = ids;
    136   album->parent_id = 0;
    137   album->storage_id = 0;
    138   int ret = LIBMTP_Create_New_Album(device,album);
    139   if (ret == 0) {
    140     ret = LIBMTP_Send_Representative_Sample(device,album->album_id, albumart);
    141     if (ret != 0) {
    142       printf("Couldn't send album art\n");
    143       LIBMTP_Dump_Errorstack(device);
    144       LIBMTP_Clear_Errorstack(device);
    145     }
    146   }
    147   else {
    148     printf("Couldn't create album object\n");
    149     LIBMTP_Dump_Errorstack(device);
    150     LIBMTP_Clear_Errorstack(device);
    151   }
    152 
    153   LIBMTP_destroy_filesampledata_t(albumart);
    154   LIBMTP_destroy_album_t(album);
    155 
    156   LIBMTP_Release_Device(device);
    157   printf("OK.\n");
    158   return 0;
    159 }
    160 
    161