Code Sync up from tizen_2.4
[platform/upstream/libmtp.git] / examples / albums.c
1 /**
2  * \file albums.c
3  * Example program that lists the albums on the device.
4  *
5  * Copyright (C) 2006 Chris A. Debenham <chris@adebenham.com>
6  * Copyright (C) 2007 Ted Bullock <tbullock@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 (int argc, char *argv[]) {
38   LIBMTP_mtpdevice_t *device_list, *device;
39
40   int opt;
41   extern int optind;
42   extern char *optarg;
43
44   while ((opt = getopt(argc, argv, "d")) != -1 ) {
45     switch (opt) {
46     case 'd':
47       LIBMTP_Set_Debug(LIBMTP_DEBUG_PTP | LIBMTP_DEBUG_DATA);
48       break;
49     }
50   }
51
52   argc -= optind;
53   argv += optind;
54
55   LIBMTP_Init();
56
57   fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
58
59   switch(LIBMTP_Get_Connected_Devices(&device_list))
60   {
61   case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
62     fprintf(stdout, "mtp-albums: No Devices have been found\n");
63     return 0;
64   case LIBMTP_ERROR_CONNECTING:
65     fprintf(stderr, "mtp-albums: There has been an error connecting. Exit\n");
66     return 1;
67   case LIBMTP_ERROR_MEMORY_ALLOCATION:
68     fprintf(stderr, "mtp-albums: Memory Allocation Error. Exit\n");
69     return 1;
70
71   /* Unknown general errors - This should never execute */
72   case LIBMTP_ERROR_GENERAL:
73   default:
74     fprintf(stderr, "mtp-albums: Unknown error, please report "
75                     "this to the libmtp developers\n");
76   return 1;
77
78   /* Successfully connected at least one device, so continue */
79   case LIBMTP_ERROR_NONE:
80     fprintf(stdout, "mtp-albums: Successfully connected\n");
81     fflush(stdout);
82   }
83
84   /* iterate through connected MTP devices */
85   for(device = device_list; device != NULL; device = device->next)
86   {
87     char *friendlyname;
88     LIBMTP_album_t *album_list, *album, *tmp;
89
90     /* Echo the friendly name so we know which device we are working with */
91     friendlyname = LIBMTP_Get_Friendlyname(device);
92     if (friendlyname == NULL) {
93       printf("Retrieving Albums on Device with name: (NULL)\n");
94     } else {
95       printf("Retrieving Albums on Device with name: %s\n", friendlyname);
96       free(friendlyname);
97     }
98
99     album_list = LIBMTP_Get_Album_List(device);
100     album = album_list;
101     while(album != NULL)
102     {
103       dump_albuminfo(album);
104       tmp = album;
105       album = album->next;
106       LIBMTP_destroy_album_t(tmp);
107     }
108   }
109
110   LIBMTP_Release_Device_List(device_list);
111   printf("OK.\n");
112   return 0;
113 }
114