OLD | NEW |
1 /** | 1 /** |
2 * \file tracks.c | 2 * \file tracks.c |
3 * Example program to list the tracks on a device. | 3 * Example program to list the tracks on a device. |
4 * | 4 * |
5 * Copyright (C) 2005-2012 Linus Walleij <triad@df.lth.se> | 5 * Copyright (C) 2005-2012 Linus Walleij <triad@df.lth.se> |
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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 } | 72 } |
73 } | 73 } |
74 if (track->rating != 0) { | 74 if (track->rating != 0) { |
75 printf(" User rating: %u (out of 100)\n", track->rating); | 75 printf(" User rating: %u (out of 100)\n", track->rating); |
76 } | 76 } |
77 if (track->usecount != 0) { | 77 if (track->usecount != 0) { |
78 printf(" Use count: %u times\n", track->usecount); | 78 printf(" Use count: %u times\n", track->usecount); |
79 } | 79 } |
80 } | 80 } |
81 | 81 |
| 82 static void |
| 83 dump_tracks(LIBMTP_mtpdevice_t *device, uint32_t storageid, int leaf) |
| 84 { |
| 85 LIBMTP_file_t *files; |
| 86 |
| 87 /* Get track listing. */ |
| 88 files = LIBMTP_Get_Files_And_Folders(device, |
| 89 storageid, |
| 90 leaf); |
| 91 if (files == NULL) { |
| 92 LIBMTP_Dump_Errorstack(device); |
| 93 LIBMTP_Clear_Errorstack(device); |
| 94 } else { |
| 95 LIBMTP_file_t *file, *tmp; |
| 96 |
| 97 file = files; |
| 98 while (file != NULL) { |
| 99 /* Please don't print these */ |
| 100 if (file->filetype == LIBMTP_FILETYPE_FOLDER) { |
| 101 dump_tracks(device, storageid, file->item_id); |
| 102 } else if (LIBMTP_FILETYPE_IS_TRACK(file->filetype)) { |
| 103 LIBMTP_track_t *track; |
| 104 |
| 105 track = LIBMTP_Get_Trackmetadata(device, file->item_id); |
| 106 dump_trackinfo(track); |
| 107 LIBMTP_destroy_track_t(track); |
| 108 } |
| 109 tmp = file; |
| 110 file = file->next; |
| 111 LIBMTP_destroy_file_t(tmp); |
| 112 } |
| 113 } |
| 114 } |
| 115 |
82 int main (int argc, char **argv) | 116 int main (int argc, char **argv) |
83 { | 117 { |
84 LIBMTP_mtpdevice_t *device_list, *device; | 118 LIBMTP_raw_device_t *rawdevices; |
85 LIBMTP_track_t *tracks; | 119 int numrawdevices; |
| 120 LIBMTP_error_number_t err; |
| 121 int i; |
86 | 122 |
87 LIBMTP_Init(); | 123 LIBMTP_Init(); |
88 fprintf(stdout, "Attempting to connect device(s)\n"); | 124 fprintf(stdout, "Attempting to connect device(s)\n"); |
89 | 125 |
90 switch(LIBMTP_Get_Connected_Devices(&device_list)) | 126 err = LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices); |
| 127 switch(err) |
91 { | 128 { |
92 case LIBMTP_ERROR_NO_DEVICE_ATTACHED: | 129 case LIBMTP_ERROR_NO_DEVICE_ATTACHED: |
93 fprintf(stdout, "mtp-tracks: No Devices have been found\n"); | 130 fprintf(stdout, "mtp-tracks: No Devices have been found\n"); |
94 return 0; | 131 return 0; |
95 case LIBMTP_ERROR_CONNECTING: | 132 case LIBMTP_ERROR_CONNECTING: |
96 fprintf(stderr, "mtp-tracks: There has been an error connecting. Exit\n"); | 133 fprintf(stderr, "mtp-tracks: There has been an error connecting. Exit\n"); |
97 return 1; | 134 return 1; |
98 case LIBMTP_ERROR_MEMORY_ALLOCATION: | 135 case LIBMTP_ERROR_MEMORY_ALLOCATION: |
99 fprintf(stderr, "mtp-tracks: Memory Allocation Error. Exit\n"); | 136 fprintf(stderr, "mtp-tracks: Memory Allocation Error. Exit\n"); |
100 return 1; | 137 return 1; |
101 | 138 |
102 /* Unknown general errors - This should never execute */ | 139 /* Unknown general errors - This should never execute */ |
103 case LIBMTP_ERROR_GENERAL: | 140 case LIBMTP_ERROR_GENERAL: |
104 default: | 141 default: |
105 fprintf(stderr, "mtp-tracks: Unknown error, please report " | 142 fprintf(stderr, "mtp-tracks: Unknown error, please report " |
106 "this to the libmtp developers\n"); | 143 "this to the libmtp developers\n"); |
107 return 1; | 144 return 1; |
108 | 145 |
109 /* Successfully connected at least one device, so continue */ | 146 /* Successfully connected at least one device, so continue */ |
110 case LIBMTP_ERROR_NONE: | 147 case LIBMTP_ERROR_NONE: |
111 fprintf(stdout, "mtp-tracks: Successfully connected\n"); | 148 fprintf(stdout, "mtp-tracks: Successfully connected\n"); |
112 fflush(stdout); | 149 fflush(stdout); |
| 150 break; |
113 } | 151 } |
114 | 152 |
115 /* iterate through connected MTP devices */ | 153 /* Iterate through connected MTP devices */ |
116 for(device = device_list; device != NULL; device = device->next) { | 154 for (i = 0; i < numrawdevices; i++) { |
| 155 LIBMTP_mtpdevice_t *device; |
| 156 LIBMTP_devicestorage_t *storage; |
117 char *friendlyname; | 157 char *friendlyname; |
118 | 158 |
| 159 device = LIBMTP_Open_Raw_Device_Uncached(&rawdevices[i]); |
| 160 if (device == NULL) { |
| 161 fprintf(stderr, "Unable to open raw device %d\n", i); |
| 162 continue; |
| 163 } |
| 164 |
119 /* Echo the friendly name so we know which device we are working with */ | 165 /* Echo the friendly name so we know which device we are working with */ |
120 friendlyname = LIBMTP_Get_Friendlyname(device); | 166 friendlyname = LIBMTP_Get_Friendlyname(device); |
121 if (friendlyname == NULL) { | 167 if (friendlyname == NULL) { |
122 printf("Friendly name: (NULL)\n"); | 168 printf("Friendly name: (NULL)\n"); |
123 } else { | 169 } else { |
124 printf("Friendly name: %s\n", friendlyname); | 170 printf("Friendly name: %s\n", friendlyname); |
125 free(friendlyname); | 171 free(friendlyname); |
126 } | 172 } |
127 // Get track listing. | 173 |
128 tracks = LIBMTP_Get_Tracklisting_With_Callback(device, NULL, NULL); | 174 LIBMTP_Dump_Errorstack(device); |
129 if (tracks == NULL) { | 175 LIBMTP_Clear_Errorstack(device); |
130 printf("No tracks.\n"); | 176 |
131 } else { | 177 /* Loop over storages */ |
132 LIBMTP_track_t *track, *tmp; | 178 for (storage = device->storage; storage != 0; storage = storage->next) { |
133 track = tracks; | 179 dump_tracks(device, storage->id, 0); |
134 while (track != NULL) { | |
135 » dump_trackinfo(track); | |
136 » tmp = track; | |
137 » track = track->next; | |
138 » LIBMTP_destroy_track_t(tmp); | |
139 } | |
140 } | 180 } |
| 181 |
| 182 LIBMTP_Release_Device(device); |
141 } | 183 } |
142 | 184 |
143 LIBMTP_Release_Device_List(device_list); | |
144 printf("OK.\n"); | 185 printf("OK.\n"); |
145 exit (0); | 186 exit (0); |
146 } | 187 } |
147 | 188 |
OLD | NEW |