Home | History | Annotate | Download | only in examples
      1 /**
      2  * \file getplaylist.c
      3  * Example program that lists the abstract playlists on the device.
      4  *
      5  * Copyright (C) 2005-2007 Linus Walleij <triad (at) df.lth.se>
      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 "common.h"
     23 #include <stdlib.h>
     24 #include <limits.h>
     25 
     26 static uint32_t dump_playlist(LIBMTP_mtpdevice_t *device, LIBMTP_playlist_t *pl)
     27 {
     28   uint32_t i;
     29 
     30   printf("Number of items: %u\n", pl->no_tracks);
     31   if(pl->no_tracks > 0) {
     32     for(i=0;i<pl->no_tracks;i++) {
     33       LIBMTP_track_t *track;
     34 
     35       track = LIBMTP_Get_Trackmetadata(device, pl->tracks[i]);
     36       if (track != NULL) {
     37 	printf("   %u: %s - %s\n", pl->tracks[i], track->artist, track->title);
     38 	LIBMTP_destroy_track_t(track);
     39       } else {
     40 	printf("   %u: INVALID TRACK REFERENCE!\n", pl->tracks[i]);
     41 	LIBMTP_Dump_Errorstack(device);
     42 	LIBMTP_Clear_Errorstack(device);
     43       }
     44     }
     45   }
     46   return 0;
     47 }
     48 
     49 int main (int argc, char **argv)
     50 {
     51   LIBMTP_mtpdevice_t *device;
     52   LIBMTP_playlist_t *playlist;
     53   uint32_t id;
     54   char *endptr;
     55 
     56   fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
     57 
     58   // We need file ID
     59   if ( argc != 2 ) {
     60     fprintf(stderr, "Just a playlist ID is required\n");
     61     return 1;
     62   }
     63 
     64   // Sanity check playlist ID
     65   id = strtoul(argv[1], &endptr, 10);
     66   if ( *endptr != 0 ) {
     67     fprintf(stderr, "illegal value %s\n", argv[1]);
     68     return 1;
     69   } else if ( ! id ) {
     70     fprintf(stderr, "bad playlist id %u\n", id);
     71     return 1;
     72  }
     73 
     74   LIBMTP_Init();
     75   device = LIBMTP_Get_First_Device();
     76   if (device == NULL) {
     77     printf("No devices. Connect/replug device and try again.\n");
     78     exit (0);
     79   }
     80 
     81   playlist = LIBMTP_Get_Playlist(device,id);
     82 
     83   if (playlist != NULL) {
     84     dump_playlist(device,playlist);
     85   }
     86 
     87   LIBMTP_destroy_playlist_t(playlist);
     88 
     89   LIBMTP_Release_Device(device);
     90   printf("OK.\n");
     91   exit (0);
     92 }
     93 
     94