Linus Walleij | 7fe4a39 | 2012-01-12 22:16:19 +0100 | [diff] [blame] | 1 | /** |
Linus Walleij | 543badf | 2007-02-05 19:07:38 +0000 | [diff] [blame] | 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> |
tedbullock | 20eb820 | 2007-02-23 00:33:36 +0000 | [diff] [blame] | 6 | * Copyright (C) 2007 Ted Bullock <tbullock@canada.com> |
Linus Walleij | 543badf | 2007-02-05 19:07:38 +0000 | [diff] [blame] | 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 | */ |
cjdebenh | 6f23b50 | 2006-11-08 02:24:27 +0000 | [diff] [blame] | 23 | #include "common.h" |
Linus Walleij | 2eaaff0 | 2009-01-15 21:30:36 +0000 | [diff] [blame] | 24 | #include <stdlib.h> |
cjdebenh | 6f23b50 | 2006-11-08 02:24:27 +0000 | [diff] [blame] | 25 | |
| 26 | static void dump_albuminfo(LIBMTP_album_t *album) |
| 27 | { |
Linus Walleij | ff506ef | 2007-04-23 07:27:53 +0000 | [diff] [blame] | 28 | printf("Album ID: %d\n",album->album_id); |
Linus Walleij | 5ce59db | 2008-03-12 21:22:58 +0000 | [diff] [blame] | 29 | printf(" Parent ID: %d\n",album->parent_id); |
Linus Walleij | ff506ef | 2007-04-23 07:27:53 +0000 | [diff] [blame] | 30 | printf(" Name: %s\n",album->name); |
| 31 | printf(" Artist: %s\n", album->artist); |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 32 | printf(" Composer: %s\n", album->composer); |
Linus Walleij | ff506ef | 2007-04-23 07:27:53 +0000 | [diff] [blame] | 33 | printf(" Genre: %s\n", album->genre); |
Linus Walleij | 5ce59db | 2008-03-12 21:22:58 +0000 | [diff] [blame] | 34 | printf(" Tracks: %d\n\n",album->no_tracks); |
cjdebenh | 6f23b50 | 2006-11-08 02:24:27 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Linus Walleij | 3651b23 | 2015-08-21 22:36:04 +0200 | [diff] [blame] | 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 | |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 70 | int main (int argc, char *argv[]) { |
Linus Walleij | 3651b23 | 2015-08-21 22:36:04 +0200 | [diff] [blame] | 71 | LIBMTP_raw_device_t *rawdevices; |
| 72 | int numrawdevices; |
| 73 | LIBMTP_error_number_t err; |
| 74 | int i; |
cjdebenh | 6f23b50 | 2006-11-08 02:24:27 +0000 | [diff] [blame] | 75 | |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 76 | int opt; |
| 77 | extern int optind; |
| 78 | extern char *optarg; |
| 79 | |
| 80 | while ((opt = getopt(argc, argv, "d")) != -1 ) { |
| 81 | switch (opt) { |
| 82 | case 'd': |
Catalin Patulea | 406d5f9 | 2012-07-20 19:00:24 -0400 | [diff] [blame] | 83 | LIBMTP_Set_Debug(LIBMTP_DEBUG_PTP | LIBMTP_DEBUG_DATA); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 84 | break; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | argc -= optind; |
| 89 | argv += optind; |
| 90 | |
cjdebenh | 6f23b50 | 2006-11-08 02:24:27 +0000 | [diff] [blame] | 91 | LIBMTP_Init(); |
Linus Walleij | 7fe4a39 | 2012-01-12 22:16:19 +0100 | [diff] [blame] | 92 | |
tedbullock | cd9f499 | 2007-03-29 06:00:40 +0000 | [diff] [blame] | 93 | fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n"); |
| 94 | |
Linus Walleij | 3651b23 | 2015-08-21 22:36:04 +0200 | [diff] [blame] | 95 | err = LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices); |
| 96 | switch(err) |
tedbullock | 20eb820 | 2007-02-23 00:33:36 +0000 | [diff] [blame] | 97 | { |
tedbullock | 433d217 | 2007-02-23 22:39:12 +0000 | [diff] [blame] | 98 | case LIBMTP_ERROR_NO_DEVICE_ATTACHED: |
tedbullock | 20eb820 | 2007-02-23 00:33:36 +0000 | [diff] [blame] | 99 | fprintf(stdout, "mtp-albums: No Devices have been found\n"); |
cjdebenh | 6f23b50 | 2006-11-08 02:24:27 +0000 | [diff] [blame] | 100 | return 0; |
tedbullock | 20eb820 | 2007-02-23 00:33:36 +0000 | [diff] [blame] | 101 | case LIBMTP_ERROR_CONNECTING: |
| 102 | fprintf(stderr, "mtp-albums: There has been an error connecting. Exit\n"); |
| 103 | return 1; |
| 104 | case LIBMTP_ERROR_MEMORY_ALLOCATION: |
| 105 | fprintf(stderr, "mtp-albums: Memory Allocation Error. Exit\n"); |
| 106 | return 1; |
Linus Walleij | 7fe4a39 | 2012-01-12 22:16:19 +0100 | [diff] [blame] | 107 | |
tedbullock | 20eb820 | 2007-02-23 00:33:36 +0000 | [diff] [blame] | 108 | /* Unknown general errors - This should never execute */ |
| 109 | case LIBMTP_ERROR_GENERAL: |
| 110 | default: |
| 111 | fprintf(stderr, "mtp-albums: Unknown error, please report " |
| 112 | "this to the libmtp developers\n"); |
| 113 | return 1; |
cjdebenh | 6f23b50 | 2006-11-08 02:24:27 +0000 | [diff] [blame] | 114 | |
tedbullock | 20eb820 | 2007-02-23 00:33:36 +0000 | [diff] [blame] | 115 | /* Successfully connected at least one device, so continue */ |
| 116 | case LIBMTP_ERROR_NONE: |
| 117 | fprintf(stdout, "mtp-albums: Successfully connected\n"); |
| 118 | fflush(stdout); |
Linus Walleij | 3651b23 | 2015-08-21 22:36:04 +0200 | [diff] [blame] | 119 | break; |
cjdebenh | 6f23b50 | 2006-11-08 02:24:27 +0000 | [diff] [blame] | 120 | } |
Linus Walleij | 7fe4a39 | 2012-01-12 22:16:19 +0100 | [diff] [blame] | 121 | |
tedbullock | 20eb820 | 2007-02-23 00:33:36 +0000 | [diff] [blame] | 122 | /* iterate through connected MTP devices */ |
Linus Walleij | 3651b23 | 2015-08-21 22:36:04 +0200 | [diff] [blame] | 123 | for (i = 0; i < numrawdevices; i++) { |
| 124 | LIBMTP_mtpdevice_t *device; |
| 125 | LIBMTP_devicestorage_t *storage; |
tedbullock | 20eb820 | 2007-02-23 00:33:36 +0000 | [diff] [blame] | 126 | char *friendlyname; |
Linus Walleij | 3651b23 | 2015-08-21 22:36:04 +0200 | [diff] [blame] | 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 | } |
Linus Walleij | 7fe4a39 | 2012-01-12 22:16:19 +0100 | [diff] [blame] | 133 | |
tedbullock | 20eb820 | 2007-02-23 00:33:36 +0000 | [diff] [blame] | 134 | /* Echo the friendly name so we know which device we are working with */ |
Linus Walleij | 7fe4a39 | 2012-01-12 22:16:19 +0100 | [diff] [blame] | 135 | friendlyname = LIBMTP_Get_Friendlyname(device); |
tedbullock | 20eb820 | 2007-02-23 00:33:36 +0000 | [diff] [blame] | 136 | if (friendlyname == NULL) { |
| 137 | printf("Retrieving Albums on Device with name: (NULL)\n"); |
| 138 | } else { |
| 139 | printf("Retrieving Albums on Device with name: %s\n", friendlyname); |
| 140 | free(friendlyname); |
| 141 | } |
Linus Walleij | 7fe4a39 | 2012-01-12 22:16:19 +0100 | [diff] [blame] | 142 | |
Linus Walleij | 3651b23 | 2015-08-21 22:36:04 +0200 | [diff] [blame] | 143 | LIBMTP_Dump_Errorstack(device); |
| 144 | LIBMTP_Clear_Errorstack(device); |
| 145 | |
| 146 | /* Loop over storages */ |
| 147 | for (storage = device->storage; storage != 0; storage = storage->next) { |
Stanisław Pitucha | 4c162fa | 2017-02-04 09:35:54 +1100 | [diff] [blame] | 148 | dump_albums(device, storage->id, LIBMTP_FILES_AND_FOLDERS_ROOT); |
tedbullock | 20eb820 | 2007-02-23 00:33:36 +0000 | [diff] [blame] | 149 | } |
Linus Walleij | 3651b23 | 2015-08-21 22:36:04 +0200 | [diff] [blame] | 150 | LIBMTP_Release_Device(device); |
tedbullock | 20eb820 | 2007-02-23 00:33:36 +0000 | [diff] [blame] | 151 | } |
Linus Walleij | 7fe4a39 | 2012-01-12 22:16:19 +0100 | [diff] [blame] | 152 | |
Linus Walleij | 3651b23 | 2015-08-21 22:36:04 +0200 | [diff] [blame] | 153 | free(rawdevices); |
| 154 | |
cjdebenh | 6f23b50 | 2006-11-08 02:24:27 +0000 | [diff] [blame] | 155 | printf("OK.\n"); |
| 156 | return 0; |
| 157 | } |
| 158 | |