blob: 34ea9ce30f7e3b1ac49fdc919a9ccf31f031b1be [file] [log] [blame]
Linus Walleij8a199282011-03-05 22:28:41 +01001/**
Linus Walleij543badf2007-02-05 19:07:38 +00002 * \file folders.c
3 * Example program that lists all folders on a device.
4 *
Linus Walleij8a199282011-03-05 22:28:41 +01005 * Copyright (C) 2005-2011 Linus Walleij <triad@df.lth.se>
tedbullock69a445b2007-02-15 07:41:04 +00006 * Copyright (C) 2007 Ted Bullock <tbullock@canada.com>
Linus Walleij543badf2007-02-05 19:07:38 +00007 *
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 Walleij9c6ca022006-04-21 10:24:15 +000023#include "common.h"
Linus Walleij2eaaff02009-01-15 21:30:36 +000024#include <stdlib.h>
Linus Walleij9c6ca022006-04-21 10:24:15 +000025
Linus Walleija823a702006-08-27 21:27:46 +000026static void dump_folder_list(LIBMTP_folder_t *folderlist, int level)
Linus Walleij9c6ca022006-04-21 10:24:15 +000027{
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
42int main (int argc, char **argv)
43{
Lei Zhang1cde5182013-09-04 17:25:41 -070044 LIBMTP_raw_device_t *rawdevices;
45 int numrawdevices;
46 int i;
Linus Walleij9c6ca022006-04-21 10:24:15 +000047
48 LIBMTP_Init();
Linus Walleij8a199282011-03-05 22:28:41 +010049 printf("Attempting to connect device(s)\n");
tedbullock69a445b2007-02-15 07:41:04 +000050
Lei Zhang1cde5182013-09-04 17:25:41 -070051 switch (LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices)) {
tedbullock433d2172007-02-23 22:39:12 +000052 case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
Linus Walleij1e4dfea2011-03-05 22:50:39 +010053 printf("mtp-folders: no devices found\n");
tedbullock69a445b2007-02-15 07:41:04 +000054 return 0;
55 case LIBMTP_ERROR_CONNECTING:
56 fprintf(stderr, "mtp-folders: There has been an error connecting. Exit\n");
57 return 1;
58 case LIBMTP_ERROR_MEMORY_ALLOCATION:
59 fprintf(stderr, "mtp-folders: Memory Allocation Error. Exit\n");
60 return 1;
Linus Walleij8a199282011-03-05 22:28:41 +010061
tedbullock69a445b2007-02-15 07:41:04 +000062 /* Unknown general errors - This should never execute */
63 case LIBMTP_ERROR_GENERAL:
64 default:
65 fprintf(stderr, "mtp-folders: Unknown error, please report "
66 "this to the libmtp developers\n");
Lei Zhang1cde5182013-09-04 17:25:41 -070067 return 1;
tedbullock69a445b2007-02-15 07:41:04 +000068
69 /* Successfully connected at least one device, so continue */
70 case LIBMTP_ERROR_NONE:
Linus Walleij8a199282011-03-05 22:28:41 +010071 printf("mtp-folders: Successfully connected\n");
Linus Walleij9c6ca022006-04-21 10:24:15 +000072 }
Linus Walleij8a199282011-03-05 22:28:41 +010073
tedbullock69a445b2007-02-15 07:41:04 +000074 /* iterate through connected MTP devices */
Lei Zhang1cde5182013-09-04 17:25:41 -070075 for (i = 0; i < numrawdevices; i++) {
76 LIBMTP_mtpdevice_t *device;
Linus Walleij1e4dfea2011-03-05 22:50:39 +010077 LIBMTP_devicestorage_t *storage;
Linus Walleij8a199282011-03-05 22:28:41 +010078 char *friendlyname;
Linus Walleij1e4dfea2011-03-05 22:50:39 +010079 int ret;
Linus Walleij8a199282011-03-05 22:28:41 +010080
Lei Zhang1cde5182013-09-04 17:25:41 -070081 device = LIBMTP_Open_Raw_Device(&rawdevices[i]);
82 if (device == NULL) {
83 fprintf(stderr, "Unable to open raw device %d\n", i);
84 continue;
85 }
86
tedbullockf47695a2007-02-22 22:00:28 +000087 /* Echo the friendly name so we know which device we are working with */
Linus Walleij7fe4a392012-01-12 22:16:19 +010088 friendlyname = LIBMTP_Get_Friendlyname(device);
tedbullockf47695a2007-02-22 22:00:28 +000089 if (friendlyname == NULL) {
90 printf("Friendly name: (NULL)\n");
91 } else {
92 printf("Friendly name: %s\n", friendlyname);
93 free(friendlyname);
94 }
Linus Walleij8a199282011-03-05 22:28:41 +010095
Linus Walleij7fe4a392012-01-12 22:16:19 +010096 LIBMTP_Dump_Errorstack(device);
97 LIBMTP_Clear_Errorstack(device);
tedbullock69a445b2007-02-15 07:41:04 +000098
Linus Walleij1e4dfea2011-03-05 22:50:39 +010099 /* Get all storages for this device */
100 ret = LIBMTP_Get_Storage(device, LIBMTP_STORAGE_SORTBY_NOTSORTED);
101 if (ret != 0) {
102 perror("LIBMTP_Get_Storage()\n");
Linus Walleij7fe4a392012-01-12 22:16:19 +0100103 LIBMTP_Dump_Errorstack(device);
104 LIBMTP_Clear_Errorstack(device);
Linus Walleij1e4dfea2011-03-05 22:50:39 +0100105 continue;
tedbullock69a445b2007-02-15 07:41:04 +0000106 }
107
Linus Walleij1e4dfea2011-03-05 22:50:39 +0100108 /* Loop over storages, dump folder for each one */
109 for (storage = device->storage; storage != 0; storage = storage->next) {
110 LIBMTP_folder_t *folders;
111
112 printf("Storage: %s\n", storage->StorageDescription);
Linus Walleij7fe4a392012-01-12 22:16:19 +0100113 folders = LIBMTP_Get_Folder_List_For_Storage(device, storage->id);
Linus Walleij1e4dfea2011-03-05 22:50:39 +0100114
115 if (folders == NULL) {
116 fprintf(stdout, "No folders found\n");
Linus Walleij7fe4a392012-01-12 22:16:19 +0100117 LIBMTP_Dump_Errorstack(device);
118 LIBMTP_Clear_Errorstack(device);
Linus Walleij1e4dfea2011-03-05 22:50:39 +0100119 } else {
120 dump_folder_list(folders,0);
121 }
122 LIBMTP_destroy_folder_t(folders);
123 }
Lei Zhang1cde5182013-09-04 17:25:41 -0700124 LIBMTP_Release_Device(device);
Linus Walleij9c6ca022006-04-21 10:24:15 +0000125 }
126
Lei Zhang1cde5182013-09-04 17:25:41 -0700127 free(rawdevices);
Linus Walleij9c6ca022006-04-21 10:24:15 +0000128 printf("OK.\n");
tedbullock69a445b2007-02-15 07:41:04 +0000129
130 return 0;
Linus Walleij9c6ca022006-04-21 10:24:15 +0000131}