Home | History | Annotate | Download | only in examples
      1 /**
      2  * \file tracks.c
      3  * Example program to list the tracks on a device.
      4  *
      5  * Copyright (C) 2005-2007 Linus Walleij <triad (at) df.lth.se>
      6  * Copyright (C) 2007 Ted Bullock <tbullock (at) canada.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  */
     23 #include "common.h"
     24 #include <stdlib.h>
     25 
     26 static void dump_trackinfo(LIBMTP_track_t *track)
     27 {
     28   printf("Track ID: %u\n", track->item_id);
     29   if (track->title != NULL)
     30     printf("   Title: %s\n", track->title);
     31   if (track->artist != NULL)
     32     printf("   Artist: %s\n", track->artist);
     33   if (track->genre != NULL)
     34     printf("   Genre: %s\n", track->genre);
     35   if (track->composer != NULL)
     36     printf("   Composer: %s\n", track->composer);
     37   if (track->album != NULL)
     38     printf("   Album: %s\n", track->album);
     39   if (track->date != NULL)
     40     printf("   Date: %s\n", track->date);
     41   if (track->filename != NULL)
     42     printf("   Origfilename: %s\n", track->filename);
     43   printf("   Track number: %d\n", track->tracknumber);
     44   printf("   Duration: %d milliseconds\n", track->duration);
     45 #ifdef __WIN32__
     46   printf("   File size %I64u bytes\n", track->filesize);
     47 #else
     48   printf("   File size %llu bytes\n", (long long unsigned int) track->filesize);
     49 #endif
     50   printf("   Filetype: %s\n", LIBMTP_Get_Filetype_Description(track->filetype));
     51   if (track->samplerate != 0) {
     52     printf("   Sample rate: %u Hz\n", track->samplerate);
     53   }
     54   if (track->nochannels != 0) {
     55     printf("   Number of channels: %u\n", track->nochannels);
     56   }
     57   if (track->wavecodec != 0) {
     58     printf("   WAVE fourCC code: 0x%08X\n", track->wavecodec);
     59   }
     60   if (track->bitrate != 0) {
     61     printf("   Bitrate: %u bits/s\n", track->bitrate);
     62   }
     63   if (track->bitratetype != 0) {
     64     if (track->bitratetype == 1) {
     65       printf("   Bitrate type: Constant\n");
     66     } else if (track->bitratetype == 2) {
     67       printf("   Bitrate type: Variable (VBR)\n");
     68     } else if (track->bitratetype == 3) {
     69       printf("   Bitrate type: Free\n");
     70     } else {
     71       printf("   Bitrate type: Unknown/Erroneous value\n");
     72     }
     73   }
     74   if (track->rating != 0) {
     75     printf("   User rating: %u (out of 100)\n", track->rating);
     76   }
     77   if (track->usecount != 0) {
     78     printf("   Use count: %u times\n", track->usecount);
     79   }
     80 }
     81 
     82 int main (int argc, char **argv)
     83 {
     84   LIBMTP_mtpdevice_t *device_list, *iter;
     85   LIBMTP_track_t *tracks;
     86 
     87   LIBMTP_Init();
     88   fprintf(stdout, "Attempting to connect device(s)\n");
     89 
     90   switch(LIBMTP_Get_Connected_Devices(&device_list))
     91   {
     92   case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
     93     fprintf(stdout, "mtp-tracks: No Devices have been found\n");
     94     return 0;
     95   case LIBMTP_ERROR_CONNECTING:
     96     fprintf(stderr, "mtp-tracks: There has been an error connecting. Exit\n");
     97     return 1;
     98   case LIBMTP_ERROR_MEMORY_ALLOCATION:
     99     fprintf(stderr, "mtp-tracks: Memory Allocation Error. Exit\n");
    100     return 1;
    101 
    102   /* Unknown general errors - This should never execute */
    103   case LIBMTP_ERROR_GENERAL:
    104   default:
    105     fprintf(stderr, "mtp-tracks: Unknown error, please report "
    106                     "this to the libmtp developers\n");
    107   return 1;
    108 
    109   /* Successfully connected at least one device, so continue */
    110   case LIBMTP_ERROR_NONE:
    111     fprintf(stdout, "mtp-tracks: Successfully connected\n");
    112     fflush(stdout);
    113   }
    114 
    115   /* iterate through connected MTP devices */
    116   for(iter = device_list; iter != NULL; iter = iter->next)
    117   {
    118   	char *friendlyname;
    119     /* Echo the friendly name so we know which device we are working with */
    120     friendlyname = LIBMTP_Get_Friendlyname(iter);
    121     if (friendlyname == NULL) {
    122       printf("Friendly name: (NULL)\n");
    123     } else {
    124       printf("Friendly name: %s\n", friendlyname);
    125       free(friendlyname);
    126     }
    127 
    128 	  // Get track listing.
    129 	  tracks = LIBMTP_Get_Tracklisting_With_Callback(iter, NULL, NULL);
    130 	  if (tracks == NULL) {
    131 	    printf("No tracks.\n");
    132 	  } else {
    133 	    LIBMTP_track_t *track, *tmp;
    134 	    track = tracks;
    135 	    while (track != NULL) {
    136 	      dump_trackinfo(track);
    137 	      tmp = track;
    138 	      track = track->next;
    139 	      LIBMTP_destroy_track_t(tmp);
    140 	    }
    141 	  }
    142   }
    143 
    144   LIBMTP_Release_Device_List(device_list);
    145   printf("OK.\n");
    146   exit (0);
    147 }
    148 
    149