Home | History | Annotate | Download | only in examples
      1 /**
      2  * \file albums.c
      3  * Example program that lists the albums on the device.
      4  *
      5  * Copyright (C) 2006 Chris A. Debenham <chris (at) adebenham.com>
      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_albuminfo(LIBMTP_album_t *album)
     27 {
     28   printf("Album ID: %d\n",album->album_id);
     29   printf("    Parent ID:   %d\n",album->parent_id);
     30   printf("    Name:   %s\n",album->name);
     31   printf("    Artist: %s\n", album->artist);
     32   printf("    Composer:  %s\n", album->composer);
     33   printf("    Genre:  %s\n", album->genre);
     34   printf("    Tracks: %d\n\n",album->no_tracks);
     35 }
     36 
     37 int main () {
     38   LIBMTP_mtpdevice_t *device_list, *iter;
     39 
     40   LIBMTP_Init();
     41 
     42   fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
     43 
     44   switch(LIBMTP_Get_Connected_Devices(&device_list))
     45   {
     46   case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
     47     fprintf(stdout, "mtp-albums: No Devices have been found\n");
     48     return 0;
     49   case LIBMTP_ERROR_CONNECTING:
     50     fprintf(stderr, "mtp-albums: There has been an error connecting. Exit\n");
     51     return 1;
     52   case LIBMTP_ERROR_MEMORY_ALLOCATION:
     53     fprintf(stderr, "mtp-albums: Memory Allocation Error. Exit\n");
     54     return 1;
     55 
     56   /* Unknown general errors - This should never execute */
     57   case LIBMTP_ERROR_GENERAL:
     58   default:
     59     fprintf(stderr, "mtp-albums: Unknown error, please report "
     60                     "this to the libmtp developers\n");
     61   return 1;
     62 
     63   /* Successfully connected at least one device, so continue */
     64   case LIBMTP_ERROR_NONE:
     65     fprintf(stdout, "mtp-albums: Successfully connected\n");
     66     fflush(stdout);
     67   }
     68 
     69   /* iterate through connected MTP devices */
     70   for(iter = device_list; iter != NULL; iter = iter->next)
     71   {
     72     char *friendlyname;
     73     LIBMTP_album_t *album_list, *album, *tmp;
     74 
     75     /* Echo the friendly name so we know which device we are working with */
     76     friendlyname = LIBMTP_Get_Friendlyname(iter);
     77     if (friendlyname == NULL) {
     78       printf("Retrieving Albums on Device with name: (NULL)\n");
     79     } else {
     80       printf("Retrieving Albums on Device with name: %s\n", friendlyname);
     81       free(friendlyname);
     82     }
     83 
     84     album_list = LIBMTP_Get_Album_List(iter);
     85     album = album_list;
     86     while(album != NULL)
     87     {
     88       dump_albuminfo(album);
     89       tmp = album;
     90       album = album->next;
     91       LIBMTP_destroy_album_t(tmp);
     92     }
     93   }
     94 
     95   LIBMTP_Release_Device_List(device_list);
     96   printf("OK.\n");
     97   return 0;
     98 }
     99 
    100