3 * Example program that lists all folders on a device.
5 * Copyright (C) 2005-2011 Linus Walleij <triad@df.lth.se>
6 * Copyright (C) 2007 Ted Bullock <tbullock@canada.com>
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.
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.
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.
26 static void dump_folder_list(LIBMTP_folder_t *folderlist, int level)
29 if(folderlist==NULL) {
33 printf("%u\t", folderlist->folder_id);
34 for(i=0;i<level;i++) printf(" ");
36 printf("%s\n", folderlist->name);
38 dump_folder_list(folderlist->child, level+1);
39 dump_folder_list(folderlist->sibling, level);
42 int main (int argc, char **argv)
44 LIBMTP_raw_device_t *rawdevices;
49 printf("Attempting to connect device(s)\n");
51 switch (LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices)) {
52 case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
53 printf("mtp-folders: no devices found\n");
55 case LIBMTP_ERROR_CONNECTING:
56 fprintf(stderr, "mtp-folders: There has been an error connecting. Exit\n");
58 case LIBMTP_ERROR_MEMORY_ALLOCATION:
59 fprintf(stderr, "mtp-folders: Memory Allocation Error. Exit\n");
62 /* Unknown general errors - This should never execute */
63 case LIBMTP_ERROR_GENERAL:
65 fprintf(stderr, "mtp-folders: Unknown error, please report "
66 "this to the libmtp developers\n");
69 /* Successfully connected at least one device, so continue */
70 case LIBMTP_ERROR_NONE:
71 printf("mtp-folders: Successfully connected\n");
74 /* iterate through connected MTP devices */
75 for (i = 0; i < numrawdevices; i++) {
76 LIBMTP_mtpdevice_t *device;
77 LIBMTP_devicestorage_t *storage;
81 device = LIBMTP_Open_Raw_Device(&rawdevices[i]);
83 fprintf(stderr, "Unable to open raw device %d\n", i);
87 /* Echo the friendly name so we know which device we are working with */
88 friendlyname = LIBMTP_Get_Friendlyname(device);
89 if (friendlyname == NULL) {
90 printf("Friendly name: (NULL)\n");
92 printf("Friendly name: %s\n", friendlyname);
96 LIBMTP_Dump_Errorstack(device);
97 LIBMTP_Clear_Errorstack(device);
99 /* Get all storages for this device */
100 ret = LIBMTP_Get_Storage(device, LIBMTP_STORAGE_SORTBY_NOTSORTED);
102 perror("LIBMTP_Get_Storage()\n");
103 LIBMTP_Dump_Errorstack(device);
104 LIBMTP_Clear_Errorstack(device);
108 /* Loop over storages, dump folder for each one */
109 for (storage = device->storage; storage != 0; storage = storage->next) {
110 LIBMTP_folder_t *folders;
112 printf("Storage: %s\n", storage->StorageDescription);
113 folders = LIBMTP_Get_Folder_List_For_Storage(device, storage->id);
115 if (folders == NULL) {
116 fprintf(stdout, "No folders found\n");
117 LIBMTP_Dump_Errorstack(device);
118 LIBMTP_Clear_Errorstack(device);
120 dump_folder_list(folders,0);
122 LIBMTP_destroy_folder_t(folders);
124 LIBMTP_Release_Device(device);