OLD | NEW |
1 /** | 1 /** |
2 * \file albums.c | 2 * \file albums.c |
3 * Example program that lists the albums on the device. | 3 * Example program that lists the albums on the device. |
4 * | 4 * |
5 * Copyright (C) 2006 Chris A. Debenham <chris@adebenham.com> | 5 * Copyright (C) 2006 Chris A. Debenham <chris@adebenham.com> |
6 * Copyright (C) 2007 Ted Bullock <tbullock@canada.com> | 6 * Copyright (C) 2007 Ted Bullock <tbullock@canada.com> |
7 * | 7 * |
8 * This library is free software; you can redistribute it and/or | 8 * This library is free software; you can redistribute it and/or |
9 * modify it under the terms of the GNU Lesser General Public | 9 * modify it under the terms of the GNU Lesser General Public |
10 * License as published by the Free Software Foundation; either | 10 * License as published by the Free Software Foundation; either |
(...skipping 16 matching lines...) Expand all Loading... |
27 { | 27 { |
28 printf("Album ID: %d\n",album->album_id); | 28 printf("Album ID: %d\n",album->album_id); |
29 printf(" Parent ID: %d\n",album->parent_id); | 29 printf(" Parent ID: %d\n",album->parent_id); |
30 printf(" Name: %s\n",album->name); | 30 printf(" Name: %s\n",album->name); |
31 printf(" Artist: %s\n", album->artist); | 31 printf(" Artist: %s\n", album->artist); |
32 printf(" Composer: %s\n", album->composer); | 32 printf(" Composer: %s\n", album->composer); |
33 printf(" Genre: %s\n", album->genre); | 33 printf(" Genre: %s\n", album->genre); |
34 printf(" Tracks: %d\n\n",album->no_tracks); | 34 printf(" Tracks: %d\n\n",album->no_tracks); |
35 } | 35 } |
36 | 36 |
| 37 static void |
| 38 dump_albums(LIBMTP_mtpdevice_t *device, uint32_t storageid, int leaf) |
| 39 { |
| 40 LIBMTP_file_t *files; |
| 41 |
| 42 /* Get file listing. */ |
| 43 files = LIBMTP_Get_Files_And_Folders(device, |
| 44 storageid, |
| 45 leaf); |
| 46 if (files == NULL) { |
| 47 LIBMTP_Dump_Errorstack(device); |
| 48 LIBMTP_Clear_Errorstack(device); |
| 49 } else { |
| 50 LIBMTP_file_t *file, *tmp; |
| 51 file = files; |
| 52 while (file != NULL) { |
| 53 /* Please don't print these */ |
| 54 if (file->filetype == LIBMTP_FILETYPE_FOLDER) { |
| 55 dump_albums(device, storageid, file->item_id); |
| 56 } else if (file->filetype == LIBMTP_FILETYPE_ALBUM) { |
| 57 LIBMTP_album_t *album; |
| 58 |
| 59 album = LIBMTP_Get_Album(device, file->item_id); |
| 60 dump_albuminfo(album); |
| 61 LIBMTP_destroy_album_t(album); |
| 62 } |
| 63 tmp = file; |
| 64 file = file->next; |
| 65 LIBMTP_destroy_file_t(tmp); |
| 66 } |
| 67 } |
| 68 } |
| 69 |
37 int main (int argc, char *argv[]) { | 70 int main (int argc, char *argv[]) { |
38 LIBMTP_mtpdevice_t *device_list, *device; | 71 LIBMTP_raw_device_t *rawdevices; |
| 72 int numrawdevices; |
| 73 LIBMTP_error_number_t err; |
| 74 int i; |
39 | 75 |
40 int opt; | 76 int opt; |
41 extern int optind; | 77 extern int optind; |
42 extern char *optarg; | 78 extern char *optarg; |
43 | 79 |
44 while ((opt = getopt(argc, argv, "d")) != -1 ) { | 80 while ((opt = getopt(argc, argv, "d")) != -1 ) { |
45 switch (opt) { | 81 switch (opt) { |
46 case 'd': | 82 case 'd': |
47 LIBMTP_Set_Debug(LIBMTP_DEBUG_PTP | LIBMTP_DEBUG_DATA); | 83 LIBMTP_Set_Debug(LIBMTP_DEBUG_PTP | LIBMTP_DEBUG_DATA); |
48 break; | 84 break; |
49 } | 85 } |
50 } | 86 } |
51 | 87 |
52 argc -= optind; | 88 argc -= optind; |
53 argv += optind; | 89 argv += optind; |
54 | 90 |
55 LIBMTP_Init(); | 91 LIBMTP_Init(); |
56 | 92 |
57 fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n"); | 93 fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n"); |
58 | 94 |
59 switch(LIBMTP_Get_Connected_Devices(&device_list)) | 95 err = LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices); |
| 96 switch(err) |
60 { | 97 { |
61 case LIBMTP_ERROR_NO_DEVICE_ATTACHED: | 98 case LIBMTP_ERROR_NO_DEVICE_ATTACHED: |
62 fprintf(stdout, "mtp-albums: No Devices have been found\n"); | 99 fprintf(stdout, "mtp-albums: No Devices have been found\n"); |
63 return 0; | 100 return 0; |
64 case LIBMTP_ERROR_CONNECTING: | 101 case LIBMTP_ERROR_CONNECTING: |
65 fprintf(stderr, "mtp-albums: There has been an error connecting. Exit\n"); | 102 fprintf(stderr, "mtp-albums: There has been an error connecting. Exit\n"); |
66 return 1; | 103 return 1; |
67 case LIBMTP_ERROR_MEMORY_ALLOCATION: | 104 case LIBMTP_ERROR_MEMORY_ALLOCATION: |
68 fprintf(stderr, "mtp-albums: Memory Allocation Error. Exit\n"); | 105 fprintf(stderr, "mtp-albums: Memory Allocation Error. Exit\n"); |
69 return 1; | 106 return 1; |
70 | 107 |
71 /* Unknown general errors - This should never execute */ | 108 /* Unknown general errors - This should never execute */ |
72 case LIBMTP_ERROR_GENERAL: | 109 case LIBMTP_ERROR_GENERAL: |
73 default: | 110 default: |
74 fprintf(stderr, "mtp-albums: Unknown error, please report " | 111 fprintf(stderr, "mtp-albums: Unknown error, please report " |
75 "this to the libmtp developers\n"); | 112 "this to the libmtp developers\n"); |
76 return 1; | 113 return 1; |
77 | 114 |
78 /* Successfully connected at least one device, so continue */ | 115 /* Successfully connected at least one device, so continue */ |
79 case LIBMTP_ERROR_NONE: | 116 case LIBMTP_ERROR_NONE: |
80 fprintf(stdout, "mtp-albums: Successfully connected\n"); | 117 fprintf(stdout, "mtp-albums: Successfully connected\n"); |
81 fflush(stdout); | 118 fflush(stdout); |
| 119 break; |
82 } | 120 } |
83 | 121 |
84 /* iterate through connected MTP devices */ | 122 /* iterate through connected MTP devices */ |
85 for(device = device_list; device != NULL; device = device->next) | 123 for (i = 0; i < numrawdevices; i++) { |
86 { | 124 LIBMTP_mtpdevice_t *device; |
| 125 LIBMTP_devicestorage_t *storage; |
87 char *friendlyname; | 126 char *friendlyname; |
88 LIBMTP_album_t *album_list, *album, *tmp; | 127 |
| 128 device = LIBMTP_Open_Raw_Device_Uncached(&rawdevices[i]); |
| 129 if (device == NULL) { |
| 130 fprintf(stderr, "Unable to open raw device %d\n", i); |
| 131 continue; |
| 132 } |
89 | 133 |
90 /* Echo the friendly name so we know which device we are working with */ | 134 /* Echo the friendly name so we know which device we are working with */ |
91 friendlyname = LIBMTP_Get_Friendlyname(device); | 135 friendlyname = LIBMTP_Get_Friendlyname(device); |
92 if (friendlyname == NULL) { | 136 if (friendlyname == NULL) { |
93 printf("Retrieving Albums on Device with name: (NULL)\n"); | 137 printf("Retrieving Albums on Device with name: (NULL)\n"); |
94 } else { | 138 } else { |
95 printf("Retrieving Albums on Device with name: %s\n", friendlyname); | 139 printf("Retrieving Albums on Device with name: %s\n", friendlyname); |
96 free(friendlyname); | 140 free(friendlyname); |
97 } | 141 } |
98 | 142 |
99 album_list = LIBMTP_Get_Album_List(device); | 143 LIBMTP_Dump_Errorstack(device); |
100 album = album_list; | 144 LIBMTP_Clear_Errorstack(device); |
101 while(album != NULL) | 145 |
102 { | 146 /* Loop over storages */ |
103 dump_albuminfo(album); | 147 for (storage = device->storage; storage != 0; storage = storage->next) { |
104 tmp = album; | 148 dump_albums(device, storage->id, 0); |
105 album = album->next; | |
106 LIBMTP_destroy_album_t(tmp); | |
107 } | 149 } |
| 150 LIBMTP_Release_Device(device); |
108 } | 151 } |
109 | 152 |
110 LIBMTP_Release_Device_List(device_list); | 153 free(rawdevices); |
| 154 |
111 printf("OK.\n"); | 155 printf("OK.\n"); |
112 return 0; | 156 return 0; |
113 } | 157 } |
114 | 158 |
OLD | NEW |