Linus Walleij | 8a19928 | 2011-03-05 22:28:41 +0100 | [diff] [blame] | 1 | /** |
Linus Walleij | 543badf | 2007-02-05 19:07:38 +0000 | [diff] [blame] | 2 | * \file folders.c |
| 3 | * Example program that lists all folders on a device. |
| 4 | * |
Linus Walleij | 8a19928 | 2011-03-05 22:28:41 +0100 | [diff] [blame] | 5 | * Copyright (C) 2005-2011 Linus Walleij <triad@df.lth.se> |
tedbullock | 69a445b | 2007-02-15 07:41:04 +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 | */ |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 23 | #include "common.h" |
Linus Walleij | 2eaaff0 | 2009-01-15 21:30:36 +0000 | [diff] [blame] | 24 | #include <stdlib.h> |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 25 | |
Linus Walleij | a823a70 | 2006-08-27 21:27:46 +0000 | [diff] [blame] | 26 | static void dump_folder_list(LIBMTP_folder_t *folderlist, int level) |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 27 | { |
| 28 | int i; |
| 29 | if(folderlist==NULL) { |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | printf("%u\t", folderlist->folder_id); |
| 34 | for(i=0;i<level;i++) printf(" "); |
| 35 | |
| 36 | printf("%s\n", folderlist->name); |
| 37 | |
| 38 | dump_folder_list(folderlist->child, level+1); |
| 39 | dump_folder_list(folderlist->sibling, level); |
| 40 | } |
| 41 | |
| 42 | int main (int argc, char **argv) |
| 43 | { |
Linus Walleij | 7fe4a39 | 2012-01-12 22:16:19 +0100 | [diff] [blame^] | 44 | LIBMTP_mtpdevice_t *device_list, *device; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 45 | |
| 46 | LIBMTP_Init(); |
Linus Walleij | 8a19928 | 2011-03-05 22:28:41 +0100 | [diff] [blame] | 47 | printf("Attempting to connect device(s)\n"); |
tedbullock | 69a445b | 2007-02-15 07:41:04 +0000 | [diff] [blame] | 48 | |
Linus Walleij | 7fe4a39 | 2012-01-12 22:16:19 +0100 | [diff] [blame^] | 49 | switch(LIBMTP_Get_Connected_Devices(&device_list)) |
tedbullock | 69a445b | 2007-02-15 07:41:04 +0000 | [diff] [blame] | 50 | { |
tedbullock | 433d217 | 2007-02-23 22:39:12 +0000 | [diff] [blame] | 51 | case LIBMTP_ERROR_NO_DEVICE_ATTACHED: |
Linus Walleij | 1e4dfea | 2011-03-05 22:50:39 +0100 | [diff] [blame] | 52 | printf("mtp-folders: no devices found\n"); |
tedbullock | 69a445b | 2007-02-15 07:41:04 +0000 | [diff] [blame] | 53 | return 0; |
| 54 | case LIBMTP_ERROR_CONNECTING: |
| 55 | fprintf(stderr, "mtp-folders: There has been an error connecting. Exit\n"); |
| 56 | return 1; |
| 57 | case LIBMTP_ERROR_MEMORY_ALLOCATION: |
| 58 | fprintf(stderr, "mtp-folders: Memory Allocation Error. Exit\n"); |
| 59 | return 1; |
Linus Walleij | 8a19928 | 2011-03-05 22:28:41 +0100 | [diff] [blame] | 60 | |
tedbullock | 69a445b | 2007-02-15 07:41:04 +0000 | [diff] [blame] | 61 | /* Unknown general errors - This should never execute */ |
| 62 | case LIBMTP_ERROR_GENERAL: |
| 63 | default: |
| 64 | fprintf(stderr, "mtp-folders: Unknown error, please report " |
| 65 | "this to the libmtp developers\n"); |
| 66 | return 1; |
| 67 | |
| 68 | /* Successfully connected at least one device, so continue */ |
| 69 | case LIBMTP_ERROR_NONE: |
Linus Walleij | 8a19928 | 2011-03-05 22:28:41 +0100 | [diff] [blame] | 70 | printf("mtp-folders: Successfully connected\n"); |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 71 | } |
Linus Walleij | 8a19928 | 2011-03-05 22:28:41 +0100 | [diff] [blame] | 72 | |
tedbullock | 69a445b | 2007-02-15 07:41:04 +0000 | [diff] [blame] | 73 | /* iterate through connected MTP devices */ |
Linus Walleij | 7fe4a39 | 2012-01-12 22:16:19 +0100 | [diff] [blame^] | 74 | for(device = device_list; device != NULL; device = device->next) |
tedbullock | 69a445b | 2007-02-15 07:41:04 +0000 | [diff] [blame] | 75 | { |
Linus Walleij | 1e4dfea | 2011-03-05 22:50:39 +0100 | [diff] [blame] | 76 | LIBMTP_devicestorage_t *storage; |
Linus Walleij | 8a19928 | 2011-03-05 22:28:41 +0100 | [diff] [blame] | 77 | char *friendlyname; |
Linus Walleij | 1e4dfea | 2011-03-05 22:50:39 +0100 | [diff] [blame] | 78 | int ret; |
Linus Walleij | 8a19928 | 2011-03-05 22:28:41 +0100 | [diff] [blame] | 79 | |
tedbullock | f47695a | 2007-02-22 22:00:28 +0000 | [diff] [blame] | 80 | /* 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^] | 81 | friendlyname = LIBMTP_Get_Friendlyname(device); |
tedbullock | f47695a | 2007-02-22 22:00:28 +0000 | [diff] [blame] | 82 | if (friendlyname == NULL) { |
| 83 | printf("Friendly name: (NULL)\n"); |
| 84 | } else { |
| 85 | printf("Friendly name: %s\n", friendlyname); |
| 86 | free(friendlyname); |
| 87 | } |
Linus Walleij | 8a19928 | 2011-03-05 22:28:41 +0100 | [diff] [blame] | 88 | |
Linus Walleij | 7fe4a39 | 2012-01-12 22:16:19 +0100 | [diff] [blame^] | 89 | LIBMTP_Dump_Errorstack(device); |
| 90 | LIBMTP_Clear_Errorstack(device); |
tedbullock | 69a445b | 2007-02-15 07:41:04 +0000 | [diff] [blame] | 91 | |
Linus Walleij | 1e4dfea | 2011-03-05 22:50:39 +0100 | [diff] [blame] | 92 | /* Get all storages for this device */ |
| 93 | ret = LIBMTP_Get_Storage(device, LIBMTP_STORAGE_SORTBY_NOTSORTED); |
| 94 | if (ret != 0) { |
| 95 | perror("LIBMTP_Get_Storage()\n"); |
Linus Walleij | 7fe4a39 | 2012-01-12 22:16:19 +0100 | [diff] [blame^] | 96 | LIBMTP_Dump_Errorstack(device); |
| 97 | LIBMTP_Clear_Errorstack(device); |
Linus Walleij | 1e4dfea | 2011-03-05 22:50:39 +0100 | [diff] [blame] | 98 | continue; |
tedbullock | 69a445b | 2007-02-15 07:41:04 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Linus Walleij | 1e4dfea | 2011-03-05 22:50:39 +0100 | [diff] [blame] | 101 | /* Loop over storages, dump folder for each one */ |
| 102 | for (storage = device->storage; storage != 0; storage = storage->next) { |
| 103 | LIBMTP_folder_t *folders; |
| 104 | |
| 105 | printf("Storage: %s\n", storage->StorageDescription); |
Linus Walleij | 7fe4a39 | 2012-01-12 22:16:19 +0100 | [diff] [blame^] | 106 | folders = LIBMTP_Get_Folder_List_For_Storage(device, storage->id); |
Linus Walleij | 1e4dfea | 2011-03-05 22:50:39 +0100 | [diff] [blame] | 107 | |
| 108 | if (folders == NULL) { |
| 109 | fprintf(stdout, "No folders found\n"); |
Linus Walleij | 7fe4a39 | 2012-01-12 22:16:19 +0100 | [diff] [blame^] | 110 | LIBMTP_Dump_Errorstack(device); |
| 111 | LIBMTP_Clear_Errorstack(device); |
Linus Walleij | 1e4dfea | 2011-03-05 22:50:39 +0100 | [diff] [blame] | 112 | } else { |
| 113 | dump_folder_list(folders,0); |
| 114 | } |
| 115 | LIBMTP_destroy_folder_t(folders); |
| 116 | } |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Linus Walleij | 7fe4a39 | 2012-01-12 22:16:19 +0100 | [diff] [blame^] | 119 | LIBMTP_Release_Device_List(device_list); |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 120 | printf("OK.\n"); |
tedbullock | 69a445b | 2007-02-15 07:41:04 +0000 | [diff] [blame] | 121 | |
| 122 | return 0; |
Linus Walleij | 9c6ca02 | 2006-04-21 10:24:15 +0000 | [diff] [blame] | 123 | } |